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 return alpha_<Check>(type, val_ty) ? val : fail();
167std::pair<Checker::Binders::iterator, bool> Checker::bind(
Def* mut,
const Def* d) {
168 if (!mut)
return {binders_.end(),
true};
170 auto res = binders_.emplace(mut, d);
173 for (
auto& memo : memo_)
176 if (
auto var = mut->has_var()) bound_ =
world().
vars().insert(bound_, var);
184 if (
auto seq = def->
zonk_mut()->isa<
Seq>())
return seq->shape().rank();
193 if (
auto arr = shape->type()->zonk_mut()->isa<
Arr>())
return Hole::isa_unset(arr->arity()->zonk_mut());
201 auto n = def->
node();
205template<Checker::Mode mode>
206std::optional<bool> Checker::try_alpha_(
const Def* d1,
const Def* d2) {
208 if (d1 == d2 && (d1->isa_mut() || bound_.empty() || !d1->has_free_vars_in(bound_)))
return true;
211 if ((d1->node() != d2->node() || d1->flags() != d2->flags()) && d1->is_ground() && d2->is_ground() && !
is_flex(d1)
218template<Checker::Mode mode>
219bool Checker::alpha_(
const Def* d1,
const Def* d2) {
220 if (
auto res = try_alpha_<mode>(d1, d2))
return *res;
222 auto& memo = memo_[
mode];
223 auto key = memo_key(d1, d2);
224 if (memo.contains(key))
return true;
225 if (!alpha_impl_<mode>(d1, d2))
return false;
230template<Checker::Mode mode>
231bool Checker::alpha_impl_(
const Def* d1,
const Def* d2) {
232 for (
bool todo =
true; todo;) {
238 if (
auto res = try_alpha_<mode>(d1, d2); res.has_value())
return *res;
240 auto h1 = d1->isa_mut<
Hole>();
241 auto h2 = d2->isa_mut<
Hole>();
244 if (h1)
return check(h1, d2);
245 if (h2)
return check(h2, d1);
249 if (!d1->is_set() || !d2->is_set())
return fail<mode>();
251 auto mut1 = d1->isa_mut();
252 auto mut2 = d2->isa_mut();
254 if (mut1 && mut2 && mut1 == mut2)
return true;
258 if (d1->isa<Global>() || d2->isa<Global>())
return false;
260 if (
auto [i, ins] = bind(mut1, d2); !ins)
return i->second == d2;
261 if (
auto [i, ins] = bind(mut2, d1); !ins)
return i->second == d1;
265 auto t1 = d1->type();
266 auto t2 = d2->type();
267 if (t1 && t2 && !alpha_<mode>(t1, t2))
return fail<mode>();
274 if (!check_rank(d1->as<Seq>(), rank, d2))
return fail<Check>();
279 if (!check_rank(d2->as<Seq>(), rank, d1))
return fail<Check>();
285 if (!alpha_<mode>(d1->arity(), d2->arity()))
return fail<mode>();
287 auto new_d1 = d1->zonk_mut();
288 auto new_d2 = d2->zonk_mut();
289 if (new_d1 != d1 || new_d2 != d2) {
296 auto seq1 = d1->isa<Seq>();
297 auto seq2 = d2->isa<Seq>();
300 if (
auto umax = d1->isa<UMax>(); umax && !d2->isa<UMax>())
return check(umax, d2);
301 if (
auto umax = d2->isa<UMax>(); umax && !d1->isa<UMax>())
return check(umax, d1);
303 if (seq1 && !seq1->shape().is_fused() && seq1->arity() ==
world().lit_nat_1() && !seq2)
return check1(seq1, d2);
304 if (seq2 && !seq2->shape().is_fused() && seq2->arity() ==
world().lit_nat_1() && !seq1)
return check1(seq2, d1);
307 if (
auto mut_seq = seq1->isa_mut<Seq>(); mut_seq && seq2->isa_imm())
return check(mut_seq, seq2);
308 if (
auto mut_seq = seq2->isa_mut<Seq>(); mut_seq && seq1->isa_imm())
return check(mut_seq, seq1);
312 if (
auto prod = d1->isa<Prod>())
return check<mode>(prod, d2);
313 if (
auto prod = d2->isa<Prod>())
return check<mode>(prod, d1);
314 if (seq1 && seq2)
return check<mode>(seq1, seq2);
316 if (d1->node() != d2->node() || d1->flags() != d2->flags())
return fail<mode>();
318 if (
auto var1 = d1->isa<Var>()) {
319 auto var2 = d2->as<
Var>();
320 if (
auto i = binders_.find(var1->binder()); i != binders_.end())
return i->second == var2->binder();
321 if (
auto i = binders_.find(var2->binder()); i != binders_.end())
return fail<mode>();
326 for (
size_t i = 0, e = d1->num_ops(); i != e; ++i)
327 if (!alpha_<mode>(d1->op(i), d2->op(i)))
return fail<mode>();
331template<Checker::Mode mode>
332bool Checker::check(
const Prod* prod,
const Def* def) {
333 size_t a = prod->num_ops();
334 for (
size_t i = 0; i !=
a; ++i)
335 if (!alpha_<mode>(prod->op(i), def->proj(a, i)))
return fail<mode>();
341 auto umax = def->isa<
UMax>();
342 if (!umax)
return def;
345 for (
auto op : umax->ops())
346 if (op->zonk_mut() != hole) ops.emplace_back(op);
352bool Checker::check(Hole* hole,
const Def* def) {
355 if (def->unfold_type()) {
356 if (
auto new_def = assignable_(hole->type(), def))
359 return fail<Check>();
361 return hole->set(def),
true;
366bool Checker::check_rank(
const Seq* seq,
Hole* rank,
const Def* def) {
368 if (!r)
return fail<Check>();
371 auto body = seq->body()->zonk_mut();
373 if (
auto bseq = body->isa<Seq>())
374 if (
auto q = bseq->shape().rank(); q && *q <= *r) n = *
r - *q;
375 if (n == 0)
return fail<Check>();
377 rank->set(
world().lit_nat(n));
384template<Checker::Mode mode>
385bool Checker::check(
const Seq* seq1,
const Seq* seq2) {
386 auto r1 = seq1->shape().rank();
387 auto r2 = seq2->shape().rank();
388 if (r1 && r2 && *r1 != *r2) {
389 auto k = std::min(*r1, *r2);
392 if (!rest1 || !rest2)
return fail<mode>();
393 for (
size_t i = 0; i != k; ++i)
394 if (!alpha_<mode>(seq1->shape()[i], seq2->shape()[i]))
return fail<mode>();
395 return alpha_<mode>(rest1, rest2);
398 return alpha_<mode>(*seq1->shape(), *seq2->shape()) && alpha_<mode>(seq1->body(), seq2->body());
402bool Checker::check1(
const Seq* seq,
const Def* def) {
403 auto body = seq->reduce(
world().lit_idx_1_0());
404 if (!alpha_<Check>(body, def))
return fail<Check>();
405 if (
auto mut_seq = seq->isa_mut<Seq>()) mut_seq->set(
world().lit_nat_1(), body->zonk());
411bool Checker::check(
Seq* mut_seq,
const Seq* imm_seq) {
414 auto r = mut_seq->shape().rank();
415 auto rest =
r ?
world().
drop(imm_seq, *r) :
nullptr;
417 if (!rest)
return fail<Check>();
419 auto mut_body = mut_seq->reduce(
world().
top(
world().type_indices(mut_seq->shape())));
420 if (!alpha_<Check>(mut_body, rest))
return fail<Check>();
422 mut_seq->set(*mut_seq->shape(), mut_body->zonk());
426bool Checker::check(
const UMax* umax,
const Def* def) {
427 for (
auto op :
umax->ops())
433template bool Checker::alpha_<Checker::Check>(
const Def*,
const Def*);
434template bool Checker::alpha_<Checker::Test>(
const Def*,
const Def*);
442 return w.sigma(
DefVec(
ops, [](
const Def*
op) {
return op->unfold_type(); }));
465 auto lam = isa<Lam>();
466 if (!lam)
return def;
470 def->
blame(
"filter of a lambda is of type `{}` but must be of type `Bool`",
type_of(def)).bail();
474 def->
blame(
"function body is not assignable to its declared codomain")
475 .n(
"expected `{}`, got `{}`", lam->codom(),
type_of(def))
476 .n(lam->codom()->loc(),
"codomain `{}` declared here", lam->codom())
486 auto t =
Pi::infer(pi->dom(), pi->codom());
488 type()->blame(
"declared sort of function type does not match inferred sort `{}`", t).bail();
492 auto t = as<Arr>()->body()->unfold_type();
494 type()->blame(
"declared sort of array does not match inferred sort `{}`", t).bail();
497 case MutNode::Sigma: {
500 w.log().w(
"expected type {} for {} but keeping the declared {} due to clos-conv bugs", t,
this,
type());
503 case MutNode::Variant: {
506 type()->blame(
"declared sort of variant does not match inferred sort `{}`", t).bail();
509 case MutNode::Rule: {
510 auto rule = as<Rule>();
511 auto t1 = rule->lhs()->unfold_type();
512 auto t2 = rule->rhs()->unfold_type();
515 ->blame(
"type mismatch between rule sides: lhs has type `{}` but rhs has type `{}`", t1, t2)
519 ->blame(
"condition of a rewrite rule is of type `{}` but must be of type `Bool`",
527 case MutNode::Global:
528 case MutNode::Hole:
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.
constexpr MutNode mut_node() const noexcept
node() of a Def whose Node may be mutable, whether this one is or not; see MutNode.
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)
static const Def * infer(World &, Defs)
const Def * drop(const Seq *s, nat_t k)
s without its leading k axes - its body once k covers all of them; nullptr if that isn't a type.
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, and so is Def::arity.
static const Def * drop_self(Hole *hole, const Def *def)
fe::View< const Def * > Defs
fe::Vector< const Def * > DefVec
static std::optional< nat_t > known_rank(const Def *def)
The statically known rank of def: 0 if it isn't a Seq at all, nullopt if its own rank is dynamic.
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)