3#include <absl/container/fixed_array.h>
24 if (!
is_set())
return this;
27 if (
auto hole = mut->isa<
Hole>()) {
28 auto [last,
op] = hole->find();
29 return op ?
op->zonk() : last;
32 for (
auto def :
deps())
35 if (
auto imm = mut->immutabilize())
return imm;
43 return DefVec(defs.size(), [defs](
size_t i) { return defs[i]->zonk(); });
55 if (
auto h = def->isa_mut<Hole>()) {
63 auto root = def ? def : last;
66 for (
auto h =
this; h != last;) {
67 auto next = h->op()->as_mut<Hole>();
79 auto holes = absl::FixedArray<const Def*>(n);
82 holes[0] = w.mut_hole(sigma->op(0));
83 for (
size_t i = 1; i != n; ++i) {
84 rw.map(sigma->var(n, i - 1), holes[i - 1]);
85 holes[i] = w.mut_hole(rw.rewrite(sigma->op(i)));
88 for (
size_t i = 0; i != n; ++i)
89 holes[i] = w.mut_hole(
type()->
proj(n, i));
92 auto tuple = w.tuple(holes);
101#ifdef MIM_ENABLE_CHECKS
102template<Checker::Mode mode>
103bool Checker::fail() {
104 if (mode ==
Check &&
world().flags().break_on_alpha) fe::breakpoint();
108const Def* Checker::fail() {
109 if (
world().flags().break_on_alpha) fe::breakpoint();
115 if (defs.empty())
return nullptr;
116 auto first = defs.front();
117 for (
size_t i = 1, e = defs.size(); i != e; ++i)
122const Def* Checker::assignable_(
const Def* type,
const Def* val) {
124 if (type == val_ty)
return val;
127 if (
auto sigma = type->isa<
Sigma>()) {
128 if (!alpha_<Check>(type->arity(), val_ty->arity()))
return fail();
130 size_t a = sigma->num_ops();
131 auto red = sigma->reduce(val);
132 auto new_ops = absl::FixedArray<const Def*>(red.size());
133 for (
size_t i = 0; i != a; ++i) {
134 auto new_val = assignable_(red[i], val->
proj(a, i));
136 new_ops[i] = new_val;
140 return w.tuple(new_ops);
141 }
else if (
auto uniq = val_ty->isa<
Uniq>()) {
142 if (
auto new_val =
assignable(type, uniq->op()))
return new_val;
146 return alpha_<Check>(type, val_ty) ? val : fail();
149template<Checker::Mode mode>
150bool Checker::alpha_(
const Def* d1,
const Def* d2) {
151 auto& memo = memo_[
mode];
152 if (memo.contains({d1, d2}) || memo.contains({d2, d1}))
return true;
153 if (!alpha_impl_<mode>(d1, d2))
return false;
154 memo.emplace(d1, d2);
158template<Checker::Mode mode>
159bool Checker::alpha_impl_(
const Def* d1,
const Def* d2) {
160 for (
bool todo =
true; todo;) {
169 if (!d1->has_dep(
Dep::Var) && !d2->has_dep(
Dep::Var) && d1 == d2)
return true;
171 auto h1 = d1->isa_mut<
Hole>();
172 auto h2 = d2->isa_mut<
Hole>();
175 if (h1)
return h1->set(d2),
true;
176 if (h2)
return h2->set(d1),
true;
180 if (!d1->is_set() || !d2->is_set())
return fail<mode>();
182 auto mut1 = d1->isa_mut();
183 auto mut2 = d2->isa_mut();
185 if (mut1 && mut2 && mut1 == mut2)
return true;
189 if (d1->isa<
Global>() || d2->isa<
Global>())
return false;
191 if (
auto [i, ins] = bind(mut1, d2); !ins)
return i->second == d2;
192 if (
auto [i, ins] = bind(mut2, d1); !ins)
return i->second == d1;
196 if (
auto t1 = d1->type()) {
197 if (
auto t2 = d2->type()) {
198 if (!alpha_<mode>(t1, t2))
return fail<mode>();
202 if (!alpha_<mode>(d1->arity(), d2->arity()))
return fail<mode>();
204 auto new_d1 = d1->zonk_mut();
205 auto new_d2 = d2->zonk_mut();
206 if (new_d1 != d1 || new_d2 != d2) {
213 auto seq1 = d1->isa<Seq>();
214 auto seq2 = d2->isa<Seq>();
217 if (
auto umax = d1->isa<
UMax>(); umax && !d2->isa<
UMax>())
return check(umax, d2);
218 if (
auto umax = d2->isa<
UMax>(); umax && !d1->isa<
UMax>())
return check(umax, d1);
220 if (seq1 && seq1->arity() ==
world().lit_nat_1() && !seq2)
return check1(seq1, d2);
221 if (seq2 && seq2->arity() ==
world().lit_nat_1() && !seq1)
return check1(seq2, d1);
224 if (
auto mut_seq = seq1->isa_mut<Seq>(); mut_seq && seq2->isa_imm())
return check(mut_seq, seq2);
225 if (
auto mut_seq = seq2->isa_mut<Seq>(); mut_seq && seq1->isa_imm())
return check(mut_seq, seq1);
229 if (
auto prod = d1->isa<Prod>())
return check<mode>(prod, d2);
230 if (
auto prod = d2->isa<Prod>())
return check<mode>(prod, d1);
231 if (seq1 && seq2)
return alpha_<mode>(seq1->body(), seq2->body());
233 if (d1->node() != d2->node() || d1->flags() != d2->flags())
return fail<mode>();
235 if (
auto var1 = d1->isa<
Var>()) {
236 auto var2 = d2->as<
Var>();
237 if (
auto i = binders_.find(var1->binder()); i != binders_.end())
return i->second == var2->binder();
238 if (
auto i = binders_.find(var2->binder()); i != binders_.end())
return fail<mode>();
243 for (
size_t i = 0, e = d1->num_ops(); i != e; ++i)
244 if (!alpha_<mode>(d1->op(i), d2->op(i)))
return fail<mode>();
248template<Checker::Mode mode>
249bool Checker::check(
const Prod* prod,
const Def* def) {
250 size_t a =
prod->num_ops();
251 for (
size_t i = 0; i !=
a; ++i)
252 if (!alpha_<mode>(
prod->op(i), def->proj(a, i)))
return fail<mode>();
257bool Checker::check1(
const Seq* seq,
const Def* def) {
258 auto body = seq->reduce(
world().lit_idx_1_0());
259 if (!alpha_<Check>(body, def))
return fail<Check>();
260 if (
auto mut_seq = seq->isa_mut<Seq>()) mut_seq->set(
world().lit_nat_1(), body->zonk());
266bool Checker::check(
Seq* mut_seq,
const Seq* imm_seq) {
267 auto mut_body = mut_seq->reduce(
world().
top(
world().type_idx(mut_seq->arity())));
268 if (!alpha_<Check>(mut_body, imm_seq->body()))
return fail<Check>();
270 mut_seq->set(mut_seq->arity(), mut_body->zonk());
274bool Checker::check(
const UMax* umax,
const Def* def) {
275 for (
auto op :
umax->ops())
289 error(
type()->
loc(),
"declared sort '{}' of array does not match inferred one '{}'",
type(), t);
294 auto elems = absl::FixedArray<const Def*>(
ops.size());
295 for (
size_t i = 0, e =
ops.size(); i != e; ++i)
296 elems[i] =
ops[i]->unfold_type();
297 return world.sigma(elems);
301 auto elems = absl::FixedArray<const Def*>(
ops.size());
302 for (
size_t i = 0, e =
ops.size(); i != e; ++i)
303 elems[i] =
ops[i]->unfold_type();
317 "incorrect type '{}' for '{}'. Correct one would be: '{}'. I'll keep this one nevertheless due to "
327 auto& w =
dom->world();
331const Def*
Pi::check(
size_t,
const Def* def) {
return def; }
336 error(
type()->
loc(),
"declared sort '{}' of function type does not match inferred one '{}'",
type(), t);
343 throw Error().
error(
filter()->
loc(),
"filter '{}' of lambda is of type '{}' but must be of type 'Bool'",
349 .
error(def->
loc(),
"body of function is not assignable to declared codomain")
350 .
note(def->
loc(),
"body: '{}'", def)
358 error(
type()->
loc(),
"declared sort '{}' of rule type does not match inferred one '{}'",
type(), t);
368 error(
type()->
loc(),
"type mismatch: '{}' for lhs, but '{}' for rhs", t1, t2);
370 error(
guard()->
loc(),
"condition '{}' of rewrite is of type '{}' but must be of type 'Bool'",
guard(),
382template bool Checker::alpha_<Checker::Check>(
const Def*,
const Def*);
383template bool Checker::alpha_<Checker::Test>(
const Def*,
const Def*);
const Def * check() final
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
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?
bool is_set() const
Yields true if empty or the last op is set.
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.
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
virtual const Def * check()
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
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).
bool needs_zonk() const
Yields true, if Def::local_muts() contain a Hole that is set.
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.
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 const Def * isa_set(const Def *def)
const Def * filter() const
const Def * codom() const
const Def * check() final
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
static const Def * infer(const Def *dom, const Def *codom)
const Def * codom() const
Base class for Sigma and Tuple.
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
const Def * guard() const
const Def * check() override
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
const Reform * type() const
Base class for Arr and Pack.
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
const Def * check() final
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
static const Def * infer(World &, Defs)
static const Def * infer(World &, Defs)
Extends Rewriter for variable substitution.
const Def * rewire_mut(Def *)
const Def * rewrite(const Def *) final
Vector< const Def * > DefVec
void error(Loc loc, std::format_string< Args... > f, Args &&... args)