18bool is_shape(
const Def* s) {
19 if (
s->isa<
Nat>())
return true;
20 if (
auto arr =
s->isa<
Arr>())
return arr->body()->zonk()->isa<
Nat>();
21 if (
auto sig =
s->isa_imm<
Sigma>())
22 return std::ranges::all_of(sig->ops(), [](
const Def* op) { return op->isa<Nat>(); });
32 def->external_ =
true;
38 def->external_ =
false;
39 auto num = sym2mut_.erase(def->
sym());
40 assert_unused(num == 1);
46 if (
driver().is_loaded(plugin)) {
59#if (!defined(_MSC_VER) && defined(NDEBUG))
60bool World::Lock::guard_ =
false;
75 data_.sigma = unify<Sigma>(
type(),
Defs{})->as<Sigma>();
76 data_.tuple = unify<Tuple>(
sigma(),
Defs{})->as<Tuple>();
82 data_.lit_idx_1_0 =
lit_idx(1, 0);
84 data_.lit_bool[0] =
lit_idx(2, 0_u64);
85 data_.lit_bool[1] =
lit_idx(2, 1_u64);
93 for (
auto def : move_.defs)
113 if (!level)
return nullptr;
114 level = level->
zonk();
117 error(level->
loc(),
"argument `{}` to `Type` must be of type `Univ` but is of type `{}`", level, level->
type());
119 return unify<Type>(level)->as<
Type>();
125 if (!op->type()->isa<
Univ>())
126 error(op->loc(),
"operand '{}' of a universe increment must be of type `Univ` but is of type `{}`", op,
130 return unify<UInc>(op, offset);
134 if (
auto umax = def->isa<
UMax>())
135 for (
auto op : umax->ops())
138 ops.emplace_back(def);
144 for (
auto op : ops_) {
147 if constexpr (sort ==
UMax::Term) op = op->unfold_type();
148 if constexpr (sort >=
UMax::Type) op = op->unfold_type();
153 error(op->loc(),
"operand '{}' must be a Type of some level", op);
161 for (
auto op : ops) {
162 if (!op->type()->isa<
Univ>())
163 error(op->loc(),
"operand '{}' of a universe max must be of type 'Univ' but is of type '{}'", op,
167 lvl = std::max(lvl, *l);
169 res.emplace_back(op);
174 if (lvl > 0) res.emplace_back(l);
176 std::ranges::sort(res, [](
auto op1,
auto op2) {
return op1->gid() < op2->gid(); });
177 res.erase(std::unique(res.begin(), res.end()), res.end());
178 const Def*
umax = unify<UMax>(*
this, res);
185 if (
auto var = mut->var_)
return var;
187 if (
auto var_type = mut->
var_type()) {
190 }
else if (
auto s = var_type->isa<
Sigma>(); s && s->num_ops() == 0)
194 return mut->var_ = unify<Var>(mut);
197template<
bool Normalize>
204template<
bool Normalize>
206 callee = callee->
zonk();
209 if (
auto pi = callee->
type()->isa<
Pi>()) {
211 arg = new_arg->
zonk();
212 if (
auto imm = callee->
isa_imm<
Lam>())
return imm->body();
215 if (
auto var =
lam->has_var()) {
216 if (
auto i = move_.substs.find({var, arg}); i != move_.substs.end()) {
218 auto [
filter, body] = i->second->defs<2>();
225 DLOG(
"partial evaluate: {} ({})",
lam, arg);
226 auto body = rw.rewrite(
lam->body());
227 auto num_bytes =
sizeof(Reduct) + 2 *
sizeof(
const Def*);
228 auto buf = move_.arena.substs.allocate(num_bytes,
alignof(
const Def*));
229 auto reduct =
new (buf) Reduct(2);
230 reduct->defs_[0] =
filter;
231 reduct->defs_[1] = body;
241 auto type =
pi->reduce(arg)->zonk();
242 callee = callee->
zonk();
245 curry = curry == 0 ? trip : curry;
248 if (
auto normalizer =
axm->normalizer(); Normalize && normalizer && curry == 0) {
249 if (
auto norm = normalizer(
type, callee, arg))
return norm;
257 .
error(arg->
loc(),
"cannot apply argument to callee")
258 .
note(callee->
loc(),
"callee: '{}'", callee)
259 .
note(arg->
loc(),
"argument: '{}'", arg)
260 .
note(callee->
loc(),
"vvv domain type vvv\n'{}'\n'{}'",
pi->dom(), arg->
type())
261 .
note(arg->
loc(),
"^^^ argument type ^^^");
265 .
error(callee->
loc(),
"called expression not of function type")
266 .
error(callee->
loc(),
"'{}' <--- callee type", callee->
type());
271 callee = callee->
zonk();
276 curry = curry == 0 ? trip : curry;
284 return unify<App>(
axm, curry, trip,
type, callee, arg);
289 if (n == 0)
return sigma();
290 if (n == 1)
return ops[0]->zonk();
299 if (n == 0)
return tuple();
300 if (n == 1)
return ops[0]->zonk();
307 error(t->loc(),
"cannot assign tuple '{}' of type '{}' to incompatible tuple type '{}'", t, t->type(),
sigma);
319 if (n == 0)
return tuple();
320 if (n == 1)
return ops[0];
327 if (
auto extract = ops[0]->isa<Extract>()) {
329 bool eta = tup->type() ==
type;
330 for (
size_t i = 0; i != n && eta; ++i) {
331 if (
auto extract = ops[i]->isa<Extract>()) {
333 if (eta &=
u64(i) == *index) {
334 eta &=
extract->tuple() == tup;
346 return unify<Tuple>(
type, ops);
351 std::ranges::transform(
sym, std::back_inserter(defs), [
this](
auto c) {
return lit_i8(c); });
357 if (
auto sigma = def->isa<
Sigma>())
return std::ranges::all_of(sigma->ops(), [](
auto op) { return Idx::isa(op); });
358 if (
auto arr = def->isa<
Arr>())
return Idx::isa(arr->body());
363 if (!d || !index)
return nullptr;
365 index = index->zonk();
368 error(index->loc(),
"index '{}' is not of Idx type but of type '{}'", index, index->type());
371 for (
auto op :
tuple->ops())
374 }
else if (
auto pack = index->isa<
Pack>()) {
375 if (
auto a =
Lit::isa(index->arity())) {
376 for (
nat_t i = 0, e = *a; i != e; ++i) {
384 auto size =
Idx::isa(index->type());
385 auto type = d->unfold_type();
388 if (
auto l =
Lit::isa(size); l && *l == 1) {
389 if (
auto l =
Lit::isa(index); !l || *l != 0)
WLOG(
"unknown Idx of size 1: {}", index);
399 error(index->loc(),
"index '{}' does not fit within arity '{}'", index,
type->arity());
404 return pack->reduce(index);
415 if (
auto hole = d->isa_mut<
Hole>()) d = hole->tuplefy(
Idx::as_lit(index->type()));
427 return unify<Extract>(t, d, index);
430 return unify<Extract>(
sigma->op(*i), d, index);
436 elem_t =
arr->reduce(index);
440 if (index->isa<
Top>()) {
443 hole->set(
pack(size, elem_hole));
449 return unify<Extract>(elem_t, d, index);
454 index = index->zonk();
457 auto type = d->unfold_type();
458 auto size =
Idx::isa(index->type());
461 if (!size)
error(d->loc(),
"index '{}' must be of type 'Idx' but is of type '{}'", index, index->type());
464 error(index->loc(),
"index '{}' does not fit within arity '{}'", index,
type->arity());
467 auto elem_type =
type->proj(*lidx);
471 .
error(val->
loc(),
"value to be inserted not assignable to element")
472 .
note(val->
loc(),
"vvv value type vvv \n'{}'\n'{}'", val->
type(), elem_type)
473 .
note(val->
loc(),
"^^^ element type ^^^", elem_type);
478 if (
auto l =
Lit::isa(size); l && *l == 1)
479 return tuple(d, {val});
482 if (
auto t = d->isa<
Tuple>(); t && lidx)
return t->refine(*lidx, val);
488 new_ops[*lidx] = val;
498 return unify<Insert>(d, index, val);
502 arity = arity->
zonk();
506 if (!is_shape(arity_ty))
error(arity->
loc(),
"expected arity but got `{}` of type `{}`", arity, arity_ty);
509 if (*a == 0)
return unit(term);
510 if (*a == 1)
return body;
515 if (
auto arr_arity = arity->
type()->isa<
Seq>())
516 if (
auto lit_arity_arity =
Lit::isa(arr_arity->arity())) {
517 DefVec inner_arity(*lit_arity_arity - 1, [&](
u64 i) {
return arity->
proj(*lit_arity_arity, i + 1); });
518 return seq(term, arity->
proj(*lit_arity_arity, 0),
seq(term,
tuple(inner_arity), body));
523 return unify<Pack>(
type, body);
525 return unify<Arr>(body->
unfold_type(), arity, body);
530 if (shape.empty())
return body;
531 return seq(term, shape.rsubspan(1),
seq(term, shape.back(), body));
535 if (!
type)
return nullptr;
539 if (size->isa<
Top>()) {
541 }
else if (
auto s =
Lit::isa(size)) {
542 if (*s != 0 && val >= *s)
error(
type->loc(),
"index '{}' does not fit within arity '{}'", size, val);
543 }
else if (val != 0) {
544 error(
type->loc(),
"cannot create literal '{}' of 'Idx {}' as size is unknown", val, size);
548 return unify<Lit>(
type, val);
562 return unify<TExt<Up>>(
type);
568 for (
size_t i = 0, e = ops_.size(); i != e; ++i) {
569 auto op = ops_[i]->zonk();
570 if (!op->isa<
TExt<!Up>>()) ops.emplace_back(op);
576 if (std::ranges::any_of(ops, [&](
const Def* op) ->
bool {
return op->isa<
TExt<Up>>(); }))
return ext<Up>(kind);
580 ops.resize(std::distance(ops.begin(), std::unique(ops.begin(), ops.end())));
582 if (ops.size() == 0)
return ext<!Up>(kind);
583 if (ops.size() == 1)
return ops[0];
586 return unify<TBound<Up>>(kind, ops);
594 auto types =
DefVec(ops.size(), [&](
size_t i) { return ops[i]->type(); });
595 return unify<Merge>(
meet(types), ops);
598 assert(ops.size() == 1);
609 value = value->
zonk();
617 value = value->
zonk();
619 return unify<Split>(
type, value);
624 if (ops.size() == 1)
return ops.front();
626 auto scrutinee = ops.front();
627 auto arms = ops.span().subspan(1);
628 auto join = scrutinee->type()->isa<
Join>();
630 if (!
join)
error(scrutinee->loc(),
"scrutinee of a test expression must be of union type");
632 if (arms.size() !=
join->num_ops())
633 error(scrutinee->loc(),
"test expression has {} arms but union type has {} cases", arms.size(),
636 for (
auto arm : arms)
637 if (!arm->type()->isa<
Pi>())
638 error(arm->loc(),
"arm of test expression does not have a function type but is of type '{}'", arm->type());
640 std::ranges::sort(arms, [](
const Def* arm1,
const Def* arm2) {
645 for (
size_t i = 0, e = arms.size(); i != e; ++i) {
647 auto pi = arm->type()->as<
Pi>();
650 "domain type '{}' of arm in a test expression does not match case type '{}' in union type",
pi->dom(),
655 return unify<Match>(
type, ops);
659 inhabitant = inhabitant->
zonk();
664 auto name = symbol.str();
666 auto pos =
name.find(suffix);
667 if (pos != std::string::npos) {
668 auto num =
name.substr(pos + suffix.size());
673 num = std::to_string(std::stoi(num) + 1);
674 name =
name.substr(0, pos + suffix.size()) +
"_" + num;
684 auto mut =
var->mut();
685 auto offset = mut->reduction_offset();
686 auto size = mut->num_ops() - offset;
688 if (
auto i = move_.substs.find({var, arg}); i != move_.substs.end())
return i->second->defs();
690 auto buf = move_.arena.substs.allocate(
sizeof(Reduct) + size *
sizeof(
const Def*),
alignof(
const Def*));
691 auto reduct =
new (buf) Reduct(size);
693 for (
size_t i = 0; i != size; ++i)
694 reduct->defs_[i] = rw.rewrite(mut->op(i + offset));
696 return reduct->defs();
704 std::vector<Def*>
muts;
705 while (!queue.
empty()) {
706 auto mut = queue.
pop();
707 if (mut && mut->is_closed() && (!elide_empty || mut->is_set()))
muts.push_back(mut);
709 for (
auto op : mut->deps())
710 for (
auto mut : op->local_muts())
721 for (
auto* mut : schedule)
724 for (
auto* mut :
muts)
733#ifdef MIM_ENABLE_CHECKS
739 auto i = std::ranges::find_if(move_.defs, [=](
auto def) { return def->gid() == gid; });
740 if (i == move_.defs.end())
return nullptr;
746 assert(mut->is_closed() && mut->is_set());
747 for (
auto anx :
annexes().defs())
748 assert(anx->is_closed());
A (possibly paramterized) Array.
static constexpr u8 Trip_End
static std::tuple< const Axm *, u8, u8 > get(const Def *def)
Yields currying counter of def.
static const Def * is_uniform(Defs defs)
Yields defs.front(), if all defs are Check::alpha-equivalent (Mode::Test) and nullptr otherwise.
static bool alpha(const Def *d1, const Def *d2)
static const Def * assignable(const Def *type, const Def *value)
Can value be assigned to sth of type?
bool is_set() const
Yields true if empty or the last op is set.
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
const Def * zonk() const
If Holes have been filled, reconstruct the program without them.
constexpr auto ops() const noexcept
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
bool is_external() const noexcept
const Def * var_type()
If this is a binder, compute the type of its Variable.
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
const T * isa_imm() const
bool is_closed() const
Has no free_vars()?
Some "global" variables needed all over the place.
Error & error(Loc loc, std::format_string< Args... > s, Args &&... args)
Error & note(Loc loc, std::format_string< Args... > s, Args &&... args)
This node is a hole in the IR that is inferred by its context later on.
static Hole * isa_unset(const Def *def)
static nat_t as_lit(const Def *def)
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
static std::optional< T > isa(const Def *def)
Facility to log what you are doing.
Builds a nesting tree for all mutables/binders.
A (possibly paramterized) Tuple.
A dependent function type.
static Pi * isa_implicit(const Def *d)
Is d an Pi::is_implicit (mutable) Pi?
static Schedule schedule(const Nest &)
Base class for Arr and Pack.
static const Def * infer(World &, Defs)
Extremum. Either Top (Up) or Bottom.
Data constructor for a Sigma.
static const Def * infer(World &, Defs)
Extends Rewriter for variable substitution.
const Def * rewrite(const Def *) final
A variable introduced by a binder (mutable).
const Def * attach(flags_t, Sym, const Def *)
const Lit * lit_idx(nat_t size, u64 val)
Constructs a Lit of type Idx of size size.
const Def * insert(const Def *d, const Def *i, const Def *val)
const Def * meet(Defs ops)
const Def * uinc(const Def *op, level_t offset=1)
const Lit * lit(const Def *type, u64 val)
const Def * seq(bool is_pack, const Def *arity, const Def *body)
World(Driver *, Sym name)
void watchpoint(u32 gid)
Trigger breakpoint in your debugger when Def::setting a Def with this gid.
const Type * type(const Def *level)
const Driver & driver() const
const Def * filter(Lam::Filter filter)
const Def * sigma(Defs ops)
const Def * pack(const Def *arity, const Def *body)
const Def * app(const Def *callee, const Def *arg)
const Pi * pi(const Def *dom, const Def *codom, bool implicit=false)
const Def * unit(bool is_pack)
World & verify()
Verifies that all externals() and annexes() are Def::is_closed(), if MIM_ENABLE_CHECKS.
void for_each(bool elide_empty, std::function< void(Def *)>, bool schedule=false)
Hole * mut_hole(const Def *type)
const Lam * lam(const Pi *pi, Lam::Filter f, const Def *body)
const Def * tuple(Defs ops)
const Def * gid2def(u32 gid)
Lookup Def by gid.
Flags & flags()
Retrieve compile Flags.
const Def * implicit_app(const Def *callee, const Def *arg)
const Def * inj(const Def *type, const Def *value)
const Axm * axm(NormalizeFn n, u8 curry, u8 trip, const Def *type, plugin_t p, tag_t t, sub_t s)
const Def * extract(const Def *d, const Def *i)
const Def * arr(const Def *arity, const Def *body)
Sym sym(std::string_view)
const Def * bound(Defs ops)
const Def * join(Defs ops)
const Def * ext(const Def *type)
Sym append_suffix(Sym name, std::string suffix)
Appends a suffix or an increasing number if the suffix already exists.
const Lit * lit_idx_1_0()
const Lit * lit_univ(u64 level)
const Def * var(Def *mut)
const Tuple * tuple()
the unit value of type []
const Def * uniq(const Def *inhabitant)
const Def * raw_app(const Axm *axm, u8 curry, u8 trip, const Def *type, const Def *callee, const Def *arg)
const Externals & externals() const
const Def * merge(const Def *type, Defs ops)
const Sigma * sigma()
The unit type within Type 0.
const Lit * lit_nat(nat_t a)
const State & state() const
Defs reduce(const Var *var, const Def *arg)
Yields the new body of [mut->var() -> arg]mut.
void breakpoint(u32 gid)
Trigger breakpoint in your debugger when creating a Def with this gid.
const Def * split(const Def *type, const Def *value)
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Vector< const Def * > DefVec
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
TBound< true > Join
AKA union.
void error(std::format_string< Args... > fmt, Args &&... args)
Wraps std::format to throw T with a formatted message.
static void flatten_umax(DefVec &ops, const Def *def)
bool isa_indicies(const Def *def)
TBound< false > Meet
AKA intersection.
Compiler switches that must be saved and looked up in later phases of compilation.
static Sym demangle(Driver &, plugin_t plugin)
Reverts an Axm::mangled string to a Sym.