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