29 auto [last,
op] = hole->find();
33 if (
auto t = last->type())
34 if (
auto new_t = t->zonk_mut(); new_t != t) last->set_type(new_t);
38 if (!
is_set())
return this;
41 for (
auto def :
deps())
44 if (
auto imm = mut->immutabilize())
return imm;
52 return DefVec(defs, [](
const Def* def) {
return def->
zonk(); });
64 auto h = def->isa_mut<Hole>();
70 auto root = def ? def : last;
73 for (
auto h =
this; h != last;) {
74 auto next = h->op()->as_mut<Hole>();
89 holes[0] = w.mut_hole(sigma->op(0));
90 for (
size_t i = 1; i != n; ++i) {
91 rw.map(sigma->var(n, i - 1), holes[i - 1]);
92 holes[i] = w.mut_hole(rw.rewrite(sigma->op(i)));
95 for (
size_t i = 0; i != n; ++i)
96 holes[i] = w.mut_hole(
type()->
proj(n, i));
99 auto tuple = w.tuple(holes);
108#ifdef MIM_ENABLE_CHECKS
109template<Checker::Mode mode>
110bool Checker::fail() {
111 if (mode ==
Check &&
world().flags().break_on_alpha) fe::breakpoint();
115const Def* Checker::fail() {
116 if (
world().flags().break_on_alpha) fe::breakpoint();
122 if (defs.empty())
return nullptr;
123 auto first = defs.front();
124 auto same = [first](
const Def* def) {
return alpha<Test>(first, def); };
125 return std::ranges::all_of(defs.subspan(1), same) ? first :
nullptr;
128const Def* Checker::assignable_(
const Def* type,
const Def* val) {
130 if (!val_t)
return fail();
131 auto val_ty = val_t->zonk();
132 if (type == val_ty)
return val;
144 val = w.app(val, w.mut_hole(ipi->dom()));
147 if (type == val_ty)
return val;
150 if (
auto sigma =
type->isa<Sigma>()) {
151 if (!alpha_<Check>(
type->arity(), val_ty->arity()))
return fail();
153 size_t a = sigma->num_ops();
154 auto red = sigma->reduce(val);
156 for (
size_t i = 0; i !=
a; ++i) {
157 auto new_val = assignable_(red[i], val->
proj(a, i));
158 if (!new_val)
return fail();
159 new_ops[i] = new_val;
161 return w.tuple(new_ops);
164 if (
auto uniq = val_ty->isa<Uniq>()) {
165 if (
auto new_val =
assignable(type, uniq->op()))
return new_val;
169 return alpha_<Check>(type, val_ty) ? val : fail();
172std::pair<Checker::Binders::iterator, bool> Checker::bind(
Def* mut,
const Def* d) {
173 if (!mut)
return {binders_.end(),
true};
175 auto res = binders_.emplace(mut, d);
178 for (
auto& memo : memo_)
181 if (
auto var = mut->has_var()) bound_ =
world().
vars().insert(bound_, var);
189 auto seq = def->isa<
Seq>();
198 if (
auto arr = shape->type()->zonk_mut()->isa<
Arr>())
return Hole::isa_unset(arr->arity()->zonk_mut());
206 auto n = def->
node();
210template<Checker::Mode mode>
211std::optional<bool> Checker::try_alpha_(
const Def* d1,
const Def* d2) {
213 if (d1 == d2 && (d1->isa_mut() || bound_.empty() || !d1->has_free_vars_in(bound_)))
return true;
216 if ((d1->node() != d2->node() || d1->flags() != d2->flags()) && d1->is_ground() && d2->is_ground() && !
is_flex(d1)
223template<Checker::Mode mode>
224bool Checker::alpha_(
const Def* d1,
const Def* d2) {
225 if (
auto res = try_alpha_<mode>(d1, d2))
return *res;
227 auto& memo = memo_[
mode];
228 auto key = memo_key(d1, d2);
229 if (memo.contains(key))
return true;
230 if (!alpha_impl_<mode>(d1, d2))
return false;
235template<Checker::Mode mode>
236bool Checker::alpha_impl_(
const Def* d1,
const Def* d2) {
237 for (
bool todo =
true; todo;) {
243 if (
auto res = try_alpha_<mode>(d1, d2); res.has_value())
return *res;
245 auto h1 = d1->isa_mut<
Hole>();
246 auto h2 = d2->isa_mut<
Hole>();
249 if (h1)
return check(h1, d2);
250 if (h2)
return check(h2, d1);
254 if (!d1->is_set() || !d2->is_set())
return fail<mode>();
256 auto mut1 = d1->isa_mut();
257 auto mut2 = d2->isa_mut();
259 if (mut1 && mut2 && mut1 == mut2)
return true;
263 if (d1->isa<Global>() || d2->isa<Global>())
return false;
265 if (
auto [i, ins] = bind(mut1, d2); !ins)
return i->second == d2;
266 if (
auto [i, ins] = bind(mut2, d1); !ins)
return i->second == d1;
270 auto t1 = d1->type();
271 auto t2 = d2->type();
272 if (t1 && t2 && !alpha_<mode>(t1, t2))
return fail<mode>();
277 if (!check_rank(d1->as<Seq>(), rank, d2))
return fail<Check>();
282 if (!check_rank(d2->as<Seq>(), rank, d1))
return fail<Check>();
288 if (!alpha_<mode>(d1->arity(), d2->arity()))
return fail<mode>();
290 auto new_d1 = d1->zonk_mut();
291 auto new_d2 = d2->zonk_mut();
292 if (new_d1 != d1 || new_d2 != d2) {
299 auto seq1 = d1->isa<Seq>();
300 auto seq2 = d2->isa<Seq>();
303 if (
auto umax = d1->isa<UMax>(); umax && !d2->isa<UMax>())
return check(umax, d2);
304 if (
auto umax = d2->isa<UMax>(); umax && !d1->isa<UMax>())
return check(umax, d1);
306 if (seq1 && seq1->arity() ==
world().lit_nat_1() && !seq2)
return check1(seq1, d2);
307 if (seq2 && seq2->arity() ==
world().lit_nat_1() && !seq1)
return check1(seq2, d1);
310 if (
auto mut_seq = seq1->isa_mut<Seq>(); mut_seq && seq2->isa_imm())
return check(mut_seq, seq2);
311 if (
auto mut_seq = seq2->isa_mut<Seq>(); mut_seq && seq1->isa_imm())
return check(mut_seq, seq1);
315 if (
auto prod = d1->isa<Prod>())
return check<mode>(prod, d2);
316 if (
auto prod = d2->isa<Prod>())
return check<mode>(prod, d1);
317 if (seq1 && seq2)
return alpha_<mode>(seq1->body(), seq2->body());
319 if (d1->node() != d2->node() || d1->flags() != d2->flags())
return fail<mode>();
321 if (
auto var1 = d1->isa<Var>()) {
322 auto var2 = d2->as<
Var>();
323 if (
auto i = binders_.find(var1->binder()); i != binders_.end())
return i->second == var2->binder();
324 if (
auto i = binders_.find(var2->binder()); i != binders_.end())
return fail<mode>();
329 for (
size_t i = 0, e = d1->num_ops(); i != e; ++i)
330 if (!alpha_<mode>(d1->op(i), d2->op(i)))
return fail<mode>();
334template<Checker::Mode mode>
335bool Checker::check(
const Prod* prod,
const Def* def) {
336 size_t a = prod->num_ops();
337 for (
size_t i = 0; i !=
a; ++i)
338 if (!alpha_<mode>(prod->op(i), def->proj(a, i)))
return fail<mode>();
344 auto umax = def->isa<
UMax>();
345 if (!umax)
return def;
348 for (
auto op : umax->ops())
349 if (op->zonk_mut() != hole) ops.emplace_back(op);
355bool Checker::check(Hole* hole,
const Def* def) {
358 if (def->unfold_type()) {
359 if (
auto new_def = assignable_(hole->type(), def))
362 return fail<Check>();
364 return hole->set(def),
true;
369bool Checker::check_rank(
const Seq* seq,
Hole* rank,
const Def* def) {
370 auto body = seq->body();
374 for (
size_t i = 1;
isa_dim(def); ++i) {
375 def = def->as<Seq>()->body();
376 if (alpha_<Test>(body, def)) n = i, ++num;
379 if (num != 1)
return fail<Check>();
381 rank->set(
world().lit_nat(n));
387bool Checker::check1(
const Seq* seq,
const Def* def) {
389 if (!alpha_<Check>(body, def))
return fail<Check>();
390 if (
auto mut_seq = seq->isa_mut<Seq>()) mut_seq->set(
world().lit_nat_1(), body->zonk());
396bool Checker::check(
Seq* mut_seq,
const Seq* imm_seq) {
397 auto mut_body = mut_seq->reduce(
world().
top(
world().type_idx(mut_seq->arity())));
398 if (!alpha_<Check>(mut_body, imm_seq->body()))
return fail<Check>();
400 mut_seq->set(mut_seq->arity(), mut_body->zonk());
404bool Checker::check(
const UMax* umax,
const Def* def) {
405 for (
auto op :
umax->ops())
411template bool Checker::alpha_<Checker::Check>(
const Def*,
const Def*);
412template bool Checker::alpha_<Checker::Test>(
const Def*,
const Def*);
420 return w.sigma(
DefVec(
ops, [](
const Def*
op) {
return op->unfold_type(); }));
439 auto lam = isa<Lam>();
440 if (!lam)
return def;
444 def->
blame(
"filter `{}` of lambda is of type `{}` but must be of type `Bool`", def,
type_of(def)).bail();
448 def->
blame(
"function body is not assignable to its declared codomain")
449 .n(
"expected `{}`, got `{}`", lam->codom(),
type_of(def))
450 .n(lam->codom()->loc(),
"codomain `{}` declared here", lam->codom())
460 auto t =
Pi::infer(pi->dom(), pi->codom());
463 ->blame(
"declared sort `{}` of function type does not match inferred sort `{}`",
type(), t)
468 auto t = as<Arr>()->body()->unfold_type();
470 type()->blame(
"declared sort `{}` of array does not match inferred sort `{}`",
type(), t).bail();
476 type()->blame(
"declared sort `{}` of rule type does not match inferred sort `{}`",
type(), t).bail();
482 w.log().w(
"expected type {} for {} but keeping the declared {} due to clos-conv bugs", t,
this,
type());
486 auto rule = as<Rule>();
487 auto t1 = rule->lhs()->unfold_type();
488 auto t2 = rule->rhs()->unfold_type();
491 ->blame(
"type mismatch between rule sides: lhs has type `{}` but rhs has type `{}`", t1, t2)
495 ->blame(
"condition `{}` of rewrite rule is of type `{}` but must be of type `Bool`", rule->guard(),
500 default:
return type();
A (possibly paramterized) Array.
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)
@ Check
In Mode::Check, type inference is happening and Holes will be resolved, if possible.
static const Def * assignable(const Def *type, const Def *value)
Can value be assigned to sth of type?
const Def * zonk_mut() const
If mutable, zonk()s all ops and tries to immutabilize it; otherwise just zonk.
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.
constexpr Node node() const noexcept
bool has_dep() const noexcept
Defs deps() const noexcept
const Def * zonk() const
If Holes have been filled, reconstruct the program without them.
World & world() const noexcept
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 * op(size_t i) const noexcept
std::pair< D *, const Var * > isa_binder() const
Is this a mutable that introduces a Var?
const Def * var(nat_t a, nat_t i) noexcept
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
const Def * arity() const
Number of elements available to Extract / Insert (may be dynamic).
constexpr auto reduce(const Def *arg) const
const T * isa_imm() const
bool needs_zonk() const
Yields true, if Def::local_muts() contain a Hole that is set.
const Def * check()
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
This node is a hole in the IR that is inferred by its context later on.
std::pair< Hole *, const Def * > find()
Transitively walks up Holes until the last one while path-compressing everything.
Hole * set(const Def *op)
const Def * tuplefy(nat_t)
If unset, explode to Tuple.
static Hole * isa_unset(const Def *def)
static const Def * isa_set(const Def *def)
A dependent function type.
static const Def * infer(const Def *dom, const Def *codom)
const Def * codom() const
static Pi * isa_implicit(const Def *d)
Is d an Pi::is_implicit (mutable) Pi?
Base class for Sigma and Tuple.
static constexpr bool isa_node(mim::Node n) noexcept
Prod groups Sigma and Tuple; see fe::NodeSetable.
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
Base class for Arr and Pack.
static constexpr bool isa_node(mim::Node n) noexcept
Seq groups Arr and Pack; see fe::NodeSetable.
static const Def * infer(World &, Defs)
static const Def * infer(World &, Defs)
VarRewriter(World &world)
const Def * rewire_mut(Def *)
const Def * rewrite(const Def *) final
static Hole * isa_flex_rank(const Def *def)
The rank of «s; T» with s: «r; Nat» is unknown as long as r is: World::seq cannot un-nest it yet.
static const Def * drop_self(Hole *hole, const Def *def)
fe::View< const Def * > Defs
static bool isa_dim(const Def *def)
Is def a Seq that spans exactly one dimension, i.e. one that a rank can be peeled off?
fe::Vector< const Def * > DefVec
auto type_of(const Def *def)
Def::unfold_type of def for a diagnostic - Univ is the one Def that has no type at all.
static bool is_flex(const Def *def)