MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
world.cpp
Go to the documentation of this file.
1#include "mim/world.h"
2
3#include <ranges>
4
5#include "mim/check.h"
6#include "mim/def.h"
7#include "mim/driver.h"
8#include "mim/rewrite.h"
9#include "mim/schedule.h"
10#include "mim/tuple.h"
11
12#include "mim/util/util.h"
13
14namespace mim {
15
16namespace {
17
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>(); });
23
24 return false;
25}
26
27} // namespace
28
30 assert(!def->is_external());
31 assert(def->is_closed());
32 def->external_ = true;
33 assert_emplace(sym2mut_, def->sym(), def);
34}
35
37 assert(def->is_external());
38 def->external_ = false;
39 auto num = sym2mut_.erase(def->sym());
40 assert_unused(num == 1);
41}
42
43const Def* World::Annexes::attach(flags_t flags, Sym sym, const Def* def) {
44 driver().TLOG("register: 0x{:x} -> {} ({})", flags, def, sym);
45 auto plugin = Annex::demangle(driver(), flags);
46 if (driver().is_loaded(plugin)) {
47 assert_emplace(flags2entry_, flags, Annexes::Entry{sym, def});
48 assert_emplace(sym2flags_, sym, flags);
49 def->annex_ = true;
50 return def;
51 }
52 return nullptr;
53}
54
55/*
56 * constructor & destructor
57 */
58
59#if (!defined(_MSC_VER) && defined(NDEBUG))
60bool World::Lock::guard_ = false;
61#endif
62
64 : driver_(driver)
65 , zonker_(*this)
66 , state_(state)
67 , move_(driver) {
68 data_.univ = insert<Univ>(*this);
69 data_.lit_univ_0 = lit_univ(0);
70 data_.lit_univ_1 = lit_univ(1);
71 data_.type_0 = type(lit_univ_0());
72 data_.type_1 = type(lit_univ_1());
73 data_.type_bot = insert<Bot>(type());
74 data_.type_top = insert<Top>(type());
75 data_.sigma = unify<Sigma>(type(), Defs{})->as<Sigma>();
76 data_.tuple = unify<Tuple>(sigma(), Defs{})->as<Tuple>();
77 data_.type_nat = insert<mim::Nat>(*this);
78 data_.type_idx = insert<mim::Idx>(pi(type_nat(), type()));
79 data_.top_nat = insert<Top>(type_nat());
80 data_.lit_nat_0 = lit_nat(0);
81 data_.lit_nat_1 = lit_nat(1);
82 data_.lit_idx_1_0 = lit_idx(1, 0);
83 data_.type_bool = type_idx(2);
84 data_.lit_bool[0] = lit_idx(2, 0_u64);
85 data_.lit_bool[1] = lit_idx(2, 1_u64);
86 data_.lit_nat_max = lit_nat(nat_t(-1));
87}
88
91
93 for (auto def : move_.defs)
94 def->~Def();
95}
96
97/*
98 * Driver
99 */
100
101Log& World::log() const { return driver().log(); }
102Flags& World::flags() { return driver().flags(); }
103
104Sym World::sym(const char* s) { return driver().sym(s); }
105Sym World::sym(std::string_view s) { return driver().sym(s); }
106Sym World::sym(const std::string& s) { return driver().sym(s); }
107
108/*
109 * factory methods
110 */
111
112const Type* World::type(const Def* level) {
113 if (!level) return nullptr;
114 level = level->zonk();
115
116 if (!level->type()->isa<Univ>())
117 error(level->loc(), "argument `{}` to `Type` must be of type `Univ` but is of type `{}`", level, level->type());
118
119 return unify<Type>(level)->as<Type>();
120}
121
122const Def* World::uinc(const Def* op, level_t offset) {
123 op = op->zonk();
124
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,
127 op->type());
128
129 if (auto l = Lit::isa(op)) return lit_univ(*l + 1);
130 return unify<UInc>(op, offset);
131}
132
133static void flatten_umax(DefVec& ops, const Def* def) {
134 if (auto umax = def->isa<UMax>())
135 for (auto op : umax->ops())
136 flatten_umax(ops, op);
137 else
138 ops.emplace_back(def);
139}
140
141template<int sort>
142const Def* World::umax(Defs ops_) {
143 DefVec ops;
144 for (auto op : ops_) {
145 op = op->zonk();
146
147 if constexpr (sort == UMax::Term) op = op->unfold_type();
148 if constexpr (sort >= UMax::Type) op = op->unfold_type();
149 if constexpr (sort >= UMax::Kind) {
150 if (auto type = op->isa<Type>())
151 op = type->level();
152 else
153 error(op->loc(), "operand '{}' must be a Type of some level", op); // TODO better error message
154 }
155
156 flatten_umax(ops, op);
157 }
158
159 level_t lvl = 0;
160 DefVec res;
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,
164 op->type());
165
166 if (auto l = Lit::isa(op))
167 lvl = std::max(lvl, *l);
168 else
169 res.emplace_back(op);
170 }
171
172 const Def* l = lit_univ(lvl);
173 if (res.empty()) return sort == UMax::Univ ? l : type(l);
174 if (lvl > 0) res.emplace_back(l);
175
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);
179 return sort == UMax::Univ ? umax : type(umax);
180}
181
182// TODO more thorough & consistent checks for singleton types
183
184const Def* World::var(Def* mut) {
185 if (auto var = mut->var_) return var;
186
187 if (auto var_type = mut->var_type()) { // could be nullptr, if frozen
188 if (auto s = Idx::isa(var_type)) {
189 if (auto l = Lit::isa(s); l && l == 1) return lit_idx_1_0();
190 } else if (auto s = var_type->isa<Sigma>(); s && s->num_ops() == 0)
191 return tuple(s, {});
192 }
193
194 return mut->var_ = unify<Var>(mut);
195}
196
197template<bool Normalize>
198const Def* World::implicit_app(const Def* callee, const Def* arg) {
199 while (auto pi = Pi::isa_implicit(callee->type()))
200 callee = app(callee, mut_hole(pi->dom()));
201 return app<Normalize>(callee, arg);
202}
203
204template<bool Normalize>
205const Def* World::app(const Def* callee, const Def* arg) {
206 callee = callee->zonk();
207 arg = arg->zonk();
208
209 if (auto pi = callee->type()->isa<Pi>()) {
210 if (auto new_arg = Checker::assignable(pi->dom(), arg)) {
211 arg = new_arg->zonk();
212 if (auto imm = callee->isa_imm<Lam>()) return imm->body();
213
214 if (auto lam = callee->isa_mut<Lam>(); lam && lam->is_set() && lam->filter() != lit_ff()) {
215 if (auto var = lam->has_var()) {
216 if (auto i = move_.substs.find({var, arg}); i != move_.substs.end()) {
217 // Is there a cached version?
218 auto [filter, body] = i->second->defs<2>();
219 if (filter == lit_tt()) return body;
220 } else {
221 // First check filter, If true, reduce body and cache reduct.
222 auto rw = VarRewriter(var, arg);
223 auto filter = rw.rewrite(lam->filter());
224 if (filter == lit_tt()) {
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;
232 assert_emplace(move_.substs, std::pair{var, arg}, reduct);
233 return body;
234 }
235 }
236 } else if (lam->filter() == lit_tt()) {
237 return lam->body();
238 }
239 }
240
241 auto type = pi->reduce(arg)->zonk();
242 callee = callee->zonk();
243 auto [axm, curry, trip] = Axm::get(callee);
244 if (axm) {
245 curry = curry == 0 ? trip : curry;
246 curry = curry == Axm::Trip_End ? curry : curry - 1;
247
248 if (auto normalizer = axm->normalizer(); Normalize && normalizer && curry == 0) {
249 if (auto norm = normalizer(type, callee, arg)) return norm;
250 }
251 }
252
253 return raw_app(axm, curry, trip, type, callee, arg);
254 }
255
256 throw Error()
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 ^^^");
262 }
263
264 throw Error()
265 .error(callee->loc(), "called expression not of function type")
266 .error(callee->loc(), "'{}' <--- callee type", callee->type());
267}
268
269const Def* World::raw_app(const Def* type, const Def* callee, const Def* arg) {
270 type = type->zonk();
271 callee = callee->zonk();
272 arg = arg->zonk();
273
274 auto [axm, curry, trip] = Axm::get(callee);
275 if (axm) {
276 curry = curry == 0 ? trip : curry;
277 curry = curry == Axm::Trip_End ? curry : curry - 1;
278 }
279
280 return raw_app(axm, curry, trip, type, callee, arg);
281}
282
283const Def* World::raw_app(const Axm* axm, u8 curry, u8 trip, const Def* type, const Def* callee, const Def* arg) {
284 return unify<App>(axm, curry, trip, type, callee, arg);
285}
286
287const Def* World::sigma(Defs ops) {
288 auto n = ops.size();
289 if (n == 0) return sigma();
290 if (n == 1) return ops[0]->zonk();
291
292 auto zops = Def::zonk(ops);
293 if (auto uni = Checker::is_uniform(zops)) return arr(n, uni);
294 return unify<Sigma>(Sigma::infer(*this, zops), zops);
295}
296
297const Def* World::tuple(Defs ops) {
298 auto n = ops.size();
299 if (n == 0) return tuple();
300 if (n == 1) return ops[0]->zonk();
301
302 auto zops = Def::zonk(ops);
303 auto sigma = Tuple::infer(*this, zops);
304 auto t = tuple(sigma, zops);
305 auto new_t = Checker::assignable(sigma, t);
306 if (!new_t)
307 error(t->loc(), "cannot assign tuple '{}' of type '{}' to incompatible tuple type '{}'", t, t->type(), sigma);
308
309 return new_t;
310}
311
312const Def* World::tuple(const Def* type, Defs ops_) {
313 // TODO type-check type vs inferred type
314 type = type->zonk();
315 auto ops = Def::zonk(ops_);
316
317 auto n = ops.size();
318 if (!type->isa_mut<Sigma>()) {
319 if (n == 0) return tuple();
320 if (n == 1) return ops[0];
321 if (auto uni = Checker::is_uniform(ops)) return pack(n, uni);
322 }
323
324 if (n != 0) {
325 // eta rule for tuples:
326 // (extract(tup, 0), extract(tup, 1), extract(tup, 2)) -> tup
327 if (auto extract = ops[0]->isa<Extract>()) {
328 auto tup = extract->tuple();
329 bool eta = tup->type() == type;
330 for (size_t i = 0; i != n && eta; ++i) {
331 if (auto extract = ops[i]->isa<Extract>()) {
332 if (auto index = Lit::isa(extract->index())) {
333 if (eta &= u64(i) == *index) {
334 eta &= extract->tuple() == tup;
335 continue;
336 }
337 }
338 }
339 eta = false;
340 }
341
342 if (eta) return tup;
343 }
344 }
345
346 return unify<Tuple>(type, ops);
347}
348
349const Def* World::tuple(Sym sym) {
350 DefVec defs;
351 std::ranges::transform(sym, std::back_inserter(defs), [this](auto c) { return lit_i8(c); });
352 return tuple(defs);
353}
354
355bool isa_indicies(const Def* def) {
356 if (Idx::isa(def)) return true;
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());
359 return false;
360}
361
362const Def* World::extract(const Def* d, const Def* index) {
363 if (!d || !index) return nullptr; // can happen if frozen
364 d = d->zonk();
365 index = index->zonk();
366
367 if (!isa_indicies(index->type()))
368 error(index->loc(), "index '{}' is not of Idx type but of type '{}'", index, index->type());
369
370 if (auto tuple = index->isa<Tuple>()) {
371 for (auto op : tuple->ops())
372 d = extract(d, op);
373 return d;
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) {
377 auto idx = pack->has_var() ? pack->reduce(lit_idx(*a, i)) : pack->body();
378 d = extract(d, idx);
379 }
380 return d;
381 }
382 }
383
384 auto size = Idx::isa(index->type());
385 auto type = d->unfold_type();
386
387 if (size) {
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);
390 if (auto sigma = type->isa_mut<Sigma>(); sigma && sigma->num_ops() == 1) {
391 // mut sigmas can be 1-tuples; TODO mutables Arr?
392 } else {
393 return d;
394 }
395 }
396 }
397
398 if (size && !Checker::alpha<Checker::Check>(type->arity(), size))
399 error(index->loc(), "index '{}' does not fit within arity '{}'", index, type->arity());
400 // TODO if we have indices we need to check as well that this is compatible with `d`
401
402 if (auto pack = d->isa<Pack>()) {
403 if (pack->has_var())
404 return pack->reduce(index);
405 else
406 return pack->body();
407 }
408
409 // extract(insert(x, index, val), index) -> val
410 if (auto insert = d->isa<Insert>()) {
411 if (index == insert->index()) return insert->value();
412 }
413
414 if (auto i = Lit::isa(index)) {
415 if (auto hole = d->isa_mut<Hole>()) d = hole->tuplefy(Idx::as_lit(index->type()));
416 if (auto tuple = d->isa<Tuple>()) return tuple->op(*i);
417
418 // extract(insert(x, j, val), i) -> extract(x, i) where i != j (guaranteed by rule above)
419 if (auto insert = d->isa<Insert>()) {
420 if (insert->index()->isa<Lit>()) return extract(insert->tuple(), index);
421 }
422
423 if (auto sigma = type->isa<Sigma>()) {
424 if (auto var = sigma->has_var()) {
425 if (is_frozen()) return nullptr; // if frozen, we don't risk rewriting
426 auto t = VarRewriter(var, d).rewrite(sigma->op(*i));
427 return unify<Extract>(t, d, index);
428 }
429
430 return unify<Extract>(sigma->op(*i), d, index);
431 }
432 }
433
434 const Def* elem_t;
435 if (auto arr = type->isa<Arr>())
436 elem_t = arr->reduce(index);
437 else
438 elem_t = join(type->as<Sigma>()->ops());
439
440 if (index->isa<Top>()) {
441 if (auto hole = Hole::isa_unset(d)) {
442 auto elem_hole = mut_hole(elem_t);
443 hole->set(pack(size, elem_hole));
444 return elem_hole;
445 }
446 }
447
448 assert(d);
449 return unify<Extract>(elem_t, d, index);
450}
451
452const Def* World::insert(const Def* d, const Def* index, const Def* val) {
453 d = d->zonk();
454 index = index->zonk();
455 val = val->zonk();
456
457 auto type = d->unfold_type();
458 auto size = Idx::isa(index->type());
459 auto lidx = Lit::isa(index);
460
461 if (!size) error(d->loc(), "index '{}' must be of type 'Idx' but is of type '{}'", index, index->type());
462
463 if (!Checker::alpha<Checker::Check>(type->arity(), size))
464 error(index->loc(), "index '{}' does not fit within arity '{}'", index, type->arity());
465
466 if (lidx) {
467 auto elem_type = type->proj(*lidx);
468 auto new_val = Checker::assignable(elem_type, val);
469 if (!new_val) {
470 throw Error()
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);
474 }
475 val = new_val;
476 }
477
478 if (auto l = Lit::isa(size); l && *l == 1)
479 return tuple(d, {val}); // d could be mut - that's why the tuple ctor is needed
480
481 // insert((a, b, c, d), 2, x) -> (a, b, x, d)
482 if (auto t = d->isa<Tuple>(); t && lidx) return t->refine(*lidx, val);
483
484 // insert(‹4; x›, 2, y) -> (x, x, y, x)
485 if (auto pack = d->isa<Pack>(); pack && lidx) {
486 if (auto a = Lit::isa(pack->arity()); a && *a < flags().scalarize_threshold) {
487 auto new_ops = DefVec(*a, pack->body());
488 new_ops[*lidx] = val;
489 return tuple(type, new_ops);
490 }
491 }
492
493 // insert(insert(x, index, y), index, val) -> insert(x, index, val)
494 if (auto insert = d->isa<Insert>()) {
495 if (insert->index() == index) d = insert->tuple();
496 }
497
498 return unify<Insert>(d, index, val);
499}
500
501const Def* World::seq(bool term, const Def* arity, const Def* body) {
502 arity = arity->zonk(); // TODO use zonk_mut all over the place and rmeove zonk from is_shape?
503 body = body->zonk();
504
505 auto arity_ty = arity->unfold_type();
506 if (!is_shape(arity_ty)) error(arity->loc(), "expected arity but got `{}` of type `{}`", arity, arity_ty);
507
508 if (auto a = Lit::isa(arity)) {
509 if (*a == 0) return unit(term);
510 if (*a == 1) return body;
511 }
512
513 // «(a, b, c); body» -> «a; «(b, c); body»»
514 // e.g. when var, but still has array type
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));
519 }
520
521 if (term) {
522 auto type = arr(arity, body->type());
523 return unify<Pack>(type, body);
524 } else {
525 return unify<Arr>(body->unfold_type(), arity, body);
526 }
527}
528
529const Def* World::seq(bool term, Defs shape, const Def* body) {
530 if (shape.empty()) return body;
531 return seq(term, shape.rsubspan(1), seq(term, shape.back(), body));
532}
533
534const Lit* World::lit(const Def* type, u64 val) {
535 if (!type) return nullptr;
536 type = type->zonk();
537
538 if (auto size = Idx::isa(type)) {
539 if (size->isa<Top>()) {
540 // unsafe but fine
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) { // 0 of any size is allowed
544 error(type->loc(), "cannot create literal '{}' of 'Idx {}' as size is unknown", val, size);
545 }
546 }
547
548 return unify<Lit>(type, val);
549}
550
551/*
552 * set
553 */
554
555template<bool Up>
556const Def* World::ext(const Def* type) {
557 type = type->zonk();
558
559 if (auto arr = type->isa<Arr>()) return pack(arr->arity(), ext<Up>(arr->body()));
560 if (auto sigma = type->isa<Sigma>())
561 return tuple(sigma, DefVec(sigma->num_ops(), [&](size_t i) { return ext<Up>(sigma->op(i)); }));
562 return unify<TExt<Up>>(type);
563}
564
565template<bool Up>
566const Def* World::bound(Defs ops_) {
567 auto ops = DefVec();
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); // ignore: ext<!Up>
571 }
572
573 auto kind = umax<UMax::Type>(ops);
574
575 // has ext<Up> value?
576 if (std::ranges::any_of(ops, [&](const Def* op) -> bool { return op->isa<TExt<Up>>(); })) return ext<Up>(kind);
577
578 // sort and remove duplicates
579 std::ranges::sort(ops, GIDLt<const Def*>());
580 ops.resize(std::distance(ops.begin(), std::unique(ops.begin(), ops.end())));
581
582 if (ops.size() == 0) return ext<!Up>(kind);
583 if (ops.size() == 1) return ops[0];
584
585 // TODO simplify mixed terms with joins and meets?
586 return unify<TBound<Up>>(kind, ops);
587}
588
589const Def* World::merge(const Def* type, Defs ops_) {
590 type = type->zonk();
591 auto ops = Def::zonk(ops_);
592
593 if (type->isa<Meet>()) {
594 auto types = DefVec(ops.size(), [&](size_t i) { return ops[i]->type(); });
595 return unify<Merge>(meet(types), ops);
596 }
597
598 assert(ops.size() == 1);
599 return ops[0];
600}
601
602const Def* World::merge(Defs ops_) {
603 auto ops = Def::zonk(ops_);
604 return merge(umax<UMax::Term>(ops), ops);
605}
606
607const Def* World::inj(const Def* type, const Def* value) {
608 type = type->zonk();
609 value = value->zonk();
610
611 if (type->isa<Join>()) return unify<Inj>(type, value);
612 return value;
613}
614
615const Def* World::split(const Def* type, const Def* value) {
616 type = type->zonk();
617 value = value->zonk();
618
619 return unify<Split>(type, value);
620}
621
622const Def* World::match(Defs ops_) {
623 auto ops = Def::zonk(ops_);
624 if (ops.size() == 1) return ops.front();
625
626 auto scrutinee = ops.front();
627 auto arms = ops.span().subspan(1);
628 auto join = scrutinee->type()->isa<Join>();
629
630 if (!join) error(scrutinee->loc(), "scrutinee of a test expression must be of union type");
631
632 if (arms.size() != join->num_ops())
633 error(scrutinee->loc(), "test expression has {} arms but union type has {} cases", arms.size(),
634 join->num_ops());
635
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());
639
640 std::ranges::sort(arms, [](const Def* arm1, const Def* arm2) {
641 return arm1->type()->as<Pi>()->dom()->gid() < arm2->type()->as<Pi>()->dom()->gid();
642 });
643
644 const Def* type = nullptr;
645 for (size_t i = 0, e = arms.size(); i != e; ++i) {
646 auto arm = arms[i];
647 auto pi = arm->type()->as<Pi>();
648 if (!Checker::alpha<Checker::Check>(pi->dom(), join->op(i)))
649 error(arm->loc(),
650 "domain type '{}' of arm in a test expression does not match case type '{}' in union type", pi->dom(),
651 join->op(i));
652 type = type ? this->join({type, pi->codom()}) : pi->codom();
653 }
654
655 return unify<Match>(type, ops);
656}
657
658const Def* World::uniq(const Def* inhabitant) {
659 inhabitant = inhabitant->zonk();
660 return unify<Uniq>(inhabitant->type()->unfold_type(), inhabitant);
661}
662
663Sym World::append_suffix(Sym symbol, std::string suffix) {
664 auto name = symbol.str();
665
666 auto pos = name.find(suffix);
667 if (pos != std::string::npos) {
668 auto num = name.substr(pos + suffix.size());
669 if (num.empty()) {
670 name += "_1";
671 } else {
672 num = num.substr(1);
673 num = std::to_string(std::stoi(num) + 1);
674 name = name.substr(0, pos + suffix.size()) + "_" + num;
675 }
676 } else {
677 name += suffix;
678 }
679
680 return sym(std::move(name));
681}
682
683Defs World::reduce(const Var* var, const Def* arg) {
684 auto mut = var->mut();
685 auto offset = mut->reduction_offset();
686 auto size = mut->num_ops() - offset;
687
688 if (auto i = move_.substs.find({var, arg}); i != move_.substs.end()) return i->second->defs();
689
690 auto buf = move_.arena.substs.allocate(sizeof(Reduct) + size * sizeof(const Def*), alignof(const Def*));
691 auto reduct = new (buf) Reduct(size);
692 auto rw = VarRewriter(var, arg);
693 for (size_t i = 0; i != size; ++i)
694 reduct->defs_[i] = rw.rewrite(mut->op(i + offset));
695 assert_emplace(move_.substs, std::pair{var, arg}, reduct);
696 return reduct->defs();
697}
698
699void World::for_each(bool elide_empty, std::function<void(Def*)> f, bool schedule /* = false */) {
701 for (auto mut : externals().muts())
702 queue.push(mut);
703
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);
708
709 for (auto op : mut->deps())
710 for (auto mut : op->local_muts())
711 queue.push(mut);
712 }
713
714 // Schedules the mutables in post-order to ensure that they
715 // are emitted in the correct order of dependencies.
716 if (schedule) {
717 const auto mut_nest = Nest(muts);
718 auto schedule = Scheduler::schedule(mut_nest) | std::views::reverse | std::views::filter([&](Def* mut) {
719 return mut->is_closed() && (!elide_empty || mut->is_set());
720 });
721 for (auto* mut : schedule)
722 f(mut);
723 } else {
724 for (auto* mut : muts)
725 f(mut);
726 }
727}
728
729/*
730 * debugging
731 */
732
733#ifdef MIM_ENABLE_CHECKS
734
735void World::breakpoint(u32 gid) { state_.breakpoints.emplace(gid); }
736void World::watchpoint(u32 gid) { state_.watchpoints.emplace(gid); }
737
738const Def* World::gid2def(u32 gid) {
739 auto i = std::ranges::find_if(move_.defs, [=](auto def) { return def->gid() == gid; });
740 if (i == move_.defs.end()) return nullptr;
741 return *i;
742}
743
745 for (auto mut : externals().muts())
746 assert(mut->is_closed() && mut->is_set());
747 for (auto anx : annexes().defs())
748 assert(anx->is_closed());
749 return *this;
750}
751
752#endif
753
754#ifndef DOXYGEN
755template const Def* World::umax<UMax::Term>(Defs);
756template const Def* World::umax<UMax::Type>(Defs);
757template const Def* World::umax<UMax::Kind>(Defs);
758template const Def* World::umax<UMax::Univ>(Defs);
759template const Def* World::ext<true>(const Def*);
760template const Def* World::ext<false>(const Def*);
761template const Def* World::bound<true>(Defs);
762template const Def* World::bound<false>(Defs);
763template const Def* World::app<true>(const Def*, const Def*);
764template const Def* World::app<false>(const Def*, const Def*);
765template const Def* World::implicit_app<true>(const Def*, const Def*);
766template const Def* World::implicit_app<false>(const Def*, const Def*);
767#endif
768
769} // namespace mim
A (possibly paramterized) Array.
Definition tuple.h:117
Definition axm.h:9
static constexpr u8 Trip_End
Definition axm.h:135
static std::tuple< const Axm *, u8, u8 > get(const Def *def)
Yields currying counter of def.
Definition axm.cpp:38
static const Def * is_uniform(Defs defs)
Yields defs.front(), if all defs are Check::alpha-equivalent (Mode::Test) and nullptr otherwise.
Definition check.cpp:114
static bool alpha(const Def *d1, const Def *d2)
Definition check.h:96
static const Def * assignable(const Def *type, const Def *value)
Can value be assigned to sth of type?
Definition check.h:103
Base class for all Defs.
Definition def.h:246
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:298
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.
Definition def.cpp:593
const Def * zonk() const
If Holes have been filled, reconstruct the program without them.
Definition check.cpp:21
constexpr auto ops() const noexcept
Definition def.h:301
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:493
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
Definition def.cpp:457
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:452
bool is_external() const noexcept
Definition def.h:474
Loc loc() const
Definition def.h:514
Sym sym() const
Definition def.h:515
const Def * var_type()
If this is a binder, compute the type of its Variable.
Definition def.cpp:315
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
Definition def.h:266
const T * isa_imm() const
Definition def.h:487
bool is_closed() const
Has no free_vars()?
Definition def.cpp:418
Some "global" variables needed all over the place.
Definition driver.h:19
Log & log() const
Definition driver.h:36
Flags & flags()
Definition driver.h:34
Error & error(Loc loc, std::format_string< Args... > s, Args &&... args)
Definition dbg.h:69
Error & note(Loc loc, std::format_string< Args... > s, Args &&... args)
Definition dbg.h:71
This node is a hole in the IR that is inferred by its context later on.
Definition check.h:14
static Hole * isa_unset(const Def *def)
Definition check.h:53
static nat_t as_lit(const Def *def)
Definition def.h:894
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
Definition def.cpp:616
Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
Definition tuple.h:233
A function.
Definition lam.h:110
static std::optional< T > isa(const Def *def)
Definition def.h:838
Facility to log what you are doing.
Definition log.h:18
Builds a nesting tree for all mutables/binders.
Definition nest.h:18
A (possibly paramterized) Tuple.
Definition tuple.h:166
A dependent function type.
Definition lam.h:14
static Pi * isa_implicit(const Def *d)
Is d an Pi::is_implicit (mutable) Pi?
Definition lam.h:54
static Schedule schedule(const Nest &)
Definition schedule.cpp:125
Base class for Arr and Pack.
Definition tuple.h:84
A dependent tuple type.
Definition tuple.h:20
static const Def * infer(World &, Defs)
Definition check.cpp:291
Extremum. Either Top (Up) or Bottom.
Definition lattice.h:152
Data constructor for a Sigma.
Definition tuple.h:68
static const Def * infer(World &, Defs)
Definition check.cpp:284
@ Type
Definition def.h:759
@ Univ
Definition def.h:759
@ Term
Definition def.h:759
@ Kind
Definition def.h:759
Extends Rewriter for variable substitution.
Definition rewrite.h:98
const Def * rewrite(const Def *) final
Definition rewrite.cpp:277
A variable introduced by a binder (mutable).
Definition def.h:714
const Def * attach(flags_t, Sym, const Def *)
Definition world.cpp:43
void internalize(Def *)
Definition world.cpp:36
void externalize(Def *)
Definition world.cpp:29
const Lit * lit_idx(nat_t size, u64 val)
Constructs a Lit of type Idx of size size.
Definition world.h:553
const Def * insert(const Def *d, const Def *i, const Def *val)
Definition world.cpp:452
const Def * meet(Defs ops)
Definition world.h:595
const Def * uinc(const Def *op, level_t offset=1)
Definition world.cpp:122
const Lit * lit(const Def *type, u64 val)
Definition world.cpp:534
const Def * seq(bool is_pack, const Def *arity, const Def *body)
Definition world.cpp:501
auto & muts()
Definition world.h:693
const Lit * lit_i8()
Definition world.h:547
World(Driver *, Sym name)
Definition world.cpp:89
void watchpoint(u32 gid)
Trigger breakpoint in your debugger when Def::setting a Def with this gid.
Definition world.cpp:736
const Type * type(const Def *level)
Definition world.cpp:112
const Driver & driver() const
Definition world.h:93
const Lit * lit_tt()
Definition world.h:580
const Def * filter(Lam::Filter filter)
Definition world.h:401
const Def * sigma(Defs ops)
Definition world.cpp:287
const Def * pack(const Def *arity, const Def *body)
Definition world.h:475
const Def * app(const Def *callee, const Def *arg)
Definition world.cpp:205
const Def * match(Defs)
Definition world.cpp:622
const Pi * pi(const Def *dom, const Def *codom, bool implicit=false)
Definition world.h:377
const Def * unit(bool is_pack)
Definition world.h:494
World & verify()
Verifies that all externals() and annexes() are Def::is_closed(), if MIM_ENABLE_CHECKS.
Definition world.cpp:744
const Idx * type_idx()
Definition world.h:613
const Lit * lit_univ_0()
Definition world.h:538
Sym name() const
Definition world.h:97
const Lit * lit_univ_1()
Definition world.h:539
const Nat * type_nat()
Definition world.h:612
void for_each(bool elide_empty, std::function< void(Def *)>, bool schedule=false)
Definition world.cpp:699
Hole * mut_hole(const Def *type)
Definition world.h:344
const Lam * lam(const Pi *pi, Lam::Filter f, const Def *body)
Definition world.h:405
const Def * tuple(Defs ops)
Definition world.cpp:297
const Def * gid2def(u32 gid)
Lookup Def by gid.
Definition world.cpp:738
Flags & flags()
Retrieve compile Flags.
Definition world.cpp:102
const Def * implicit_app(const Def *callee, const Def *arg)
Definition world.cpp:198
Annexes & annexes()
Definition world.h:285
const Def * inj(const Def *type, const Def *value)
Definition world.cpp:607
const Type * type()
Definition world.h:333
const Axm * axm(NormalizeFn n, u8 curry, u8 trip, const Def *type, plugin_t p, tag_t t, sub_t s)
Definition world.h:359
const Def * extract(const Def *d, const Def *i)
Definition world.cpp:362
bool is_frozen() const
Definition world.h:147
const Def * arr(const Def *arity, const Def *body)
Definition world.h:474
Sym sym(std::string_view)
Definition world.cpp:105
const Lit * lit_ff()
Definition world.h:579
const Def * bound(Defs ops)
Definition world.cpp:566
const Def * join(Defs ops)
Definition world.h:594
const Def * ext(const Def *type)
Definition world.cpp:556
Sym append_suffix(Sym name, std::string suffix)
Appends a suffix or an increasing number if the suffix already exists.
Definition world.cpp:663
const Lit * lit_idx_1_0()
Definition world.h:544
const Lit * lit_univ(u64 level)
Definition world.h:537
const Def * var(Def *mut)
Definition world.cpp:184
const Tuple * tuple()
the unit value of type []
Definition world.h:510
const Def * uniq(const Def *inhabitant)
Definition world.cpp:658
const Def * raw_app(const Axm *axm, u8 curry, u8 trip, const Def *type, const Def *callee, const Def *arg)
Definition world.cpp:283
const Externals & externals() const
Definition world.h:282
const Def * umax(Defs)
Definition world.cpp:142
const Def * merge(const Def *type, Defs ops)
Definition world.cpp:589
const Sigma * sigma()
The unit type within Type 0.
Definition world.h:462
const Lit * lit_nat(nat_t a)
Definition world.h:540
const State & state() const
Definition world.h:92
Defs reduce(const Var *var, const Def *arg)
Yields the new body of [mut->var() -> arg]mut.
Definition world.cpp:683
void breakpoint(u32 gid)
Trigger breakpoint in your debugger when creating a Def with this gid.
Definition world.cpp:735
const Def * split(const Def *type, const Def *value)
Definition world.cpp:615
Log & log() const
Definition world.cpp:101
bool empty() const
Definition util.h:141
bool push(T val)
Definition util.h:133
#define WLOG(...)
Definition log.h:89
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:94
Definition ast.h:14
View< const Def * > Defs
Definition def.h:78
u64 nat_t
Definition types.h:37
Vector< const Def * > DefVec
Definition def.h:79
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:117
u64 flags_t
Definition types.h:39
TBound< true > Join
AKA union.
Definition lattice.h:174
u64 level_t
Definition types.h:36
TExt< true > Top
Definition lattice.h:172
uint32_t u32
Definition types.h:27
void error(std::format_string< Args... > fmt, Args &&... args)
Wraps std::format to throw T with a formatted message.
Definition dbg.h:17
static void flatten_umax(DefVec &ops, const Def *def)
Definition world.cpp:133
uint64_t u64
Definition types.h:27
bool isa_indicies(const Def *def)
Definition world.cpp:355
uint8_t u8
Definition types.h:27
TBound< false > Meet
AKA intersection.
Definition lattice.h:173
Compiler switches that must be saved and looked up in later phases of compilation.
Definition flags.h:11
static Sym demangle(Driver &, plugin_t plugin)
Reverts an Axm::mangled string to a Sym.
Definition plugin.cpp:37