8using namespace std::literals;
40 void add_names(
const Def* def,
const fe::SymMap<size_t>& sym2idx) {
41 auto& names = def2sym2idx_[def];
42 for (
auto [sym, i] : sym2idx)
43 if (
auto [j, ins] = names.emplace(sym, i); !ins && j->second != i) j->second = Ambiguous;
49 if (decl == nom || !adopted_.emplace(nom).second)
return;
57 auto names = std::vector<std::string>();
58 for (
auto i : std::views::iota(
size_t(0), variant->
num_ops())) {
59 auto syms = ctor_syms(variant, i);
60 names.emplace_back(syms.empty() ? std::format(
"_{}", i) : syms.front());
62 return fe::StreamFn{[variant, names = std::move(names)](std::ostream& os) -> std::ostream& {
63 if (
auto sym = variant->
sym(); sym && sym !=
'_')
return std::print(os,
"{}", variant), os;
64 if (names.empty())
return os <<
"(|)";
66 for (
auto sep =
"";
auto i : std::views::iota(
size_t(0), names.size())) {
67 std::print(os,
"{}| {}", sep, names[i]);
68 if (
auto op = variant->
op(i); op != variant->
world().
sigma()) std::print(os,
": {}", op);
76 if (
auto i = def2sym2idx_.find(def); i != def2sym2idx_.end()) {
77 if (
auto j = i->second.find(dbg.sym()); j != i->second.end()) {
78 if (j->second == Ambiguous) {
81 .e(dbg.loc(),
"constructor `{}` is ambiguous in variant `{}`", dbg,
variant_str(def))
82 .n(
"variants of the same shape name it at different positions")
83 .n(
"select the case by index instead, as in `T#0`")
86 .e(dbg.loc(),
"field `{}` is ambiguous in `{}`", dbg, def)
87 .n(
"sigmas of the same shape name it at different positions")
88 .n(
"select the field by index instead, as in `t#0_2`")
99 auto sym2idx = fe::SymMap<size_t>();
100 for (
size_t i = 0, n = expr->
num_ctors(); i != n; ++i) {
101 auto dbg = expr->
ctor(i)->
dbg();
102 if (!sym2idx.emplace(dbg.sym(), i).second)
error().e(dbg.loc(),
"constructor `{}` declared twice", dbg);
108 if (
auto i =
find_name(variant, dbg))
return *i;
109 error().e(dbg.loc(),
"variant `{}` has no constructor `{}`",
variant_str(variant), dbg).bail();
114 auto names = ctor_syms(variant, i);
115 if (names.empty())
return std::format(
"`{}`", i);
116 for (
auto& name : names)
117 name = std::format(
"`{}`", name);
118 return std::format(
"{}", fe::Join(names,
" / "));
127 fe::Vector<const ModDecl*>
reset_mods() {
return std::exchange(mods_, {}); }
128 void restore_mods(fe::Vector<const ModDecl*> mods) { mods_ = std::move(mods); }
141 auto i = key2nom_.find(nom->
key());
142 return i != key2nom_.end() ? i->second :
NomInfo{nom,
nullptr};
145 bool within(
const ModDecl* mod)
const {
return std::ranges::find(mods_, mod) != mods_.end(); }
149 static constexpr size_t Ambiguous = size_t(-1);
153 if (decl == inst || decl->isa<
Nom>() || inst->isa<
Nom>() || !visited.emplace(decl).second)
return;
154 if (
auto i = def2sym2idx_.find(decl); i != def2sym2idx_.end()) {
155 auto sym2idx = i->second;
159 for (
size_t k = 0, n = decl->
num_ops(); k != n; ++k)
161 }
else if (
auto sigma = decl->isa<Sigma>()) {
163 for (
size_t k = 0; k != n; ++k)
168 std::vector<std::string> ctor_syms(
const Def* variant,
size_t i) {
169 auto names = std::vector<std::string>();
170 if (
auto j = def2sym2idx_.find(variant); j != def2sym2idx_.end())
171 for (
const auto& [sym, k] : j->second)
172 if (k == i) names.emplace_back(sym.str());
173 std::ranges::sort(names);
179 fe::Vector<const ModDecl*> mods_;
180 ankerl::unordered_dense::map<flags_t, NomInfo> key2nom_;
194 if (emitted_)
return;
198 auto mods = e.reset_mods();
200 auto _ = e.world().push(
loc());
204 e.restore_mods(std::move(
mods));
217 if (
auto id = ptrn->isa<
IdPtrn>())
return id->dbg().is_anon();
219 return std::ranges::all_of(tuple->ptrns(), [](
auto p) { return is_wildcard(p.get()); });
229 .e(ptrn->
loc(),
"value bound to `{}` is not assignable to its annotated type", ptrn->
dbg().sym())
230 .n(
"expected `{}`, got `{}`", type,
type_of(def))
247 auto _ = e.world().push(
loc());
261 auto _ = e.world().push(
loc());
266 if (
auto a =
Lit::isa(t->arity()); a && *a != n)
267 e.error().e(
loc(),
"pattern has {} components, but a value of type `{}` has {}", n, t, *a).bail();
268 for (
size_t i = 0; i != n; ++i)
280 auto _ = e.world().push(
loc());
281 return emitted_type_ =
type() ?
type()->
emit(e) : e.world().mut_hole_type();
292 auto _ = e.world().push(
loc());
298 sigma = e.world().mut_sigma(e.world().type_pending(), n);
299 auto var = sigma->
var();
300 auto sym2idx = fe::SymMap<size_t>();
302 for (
size_t i = 0; i != n; ++i) {
305 if (
auto sym =
field(i)) sym2idx[sym] = i;
308 e.add_names(sigma, sym2idx);
311 if (imm && n > 1) e.add_names(imm, sym2idx);
312 return imm ? imm : sigma;
316 auto _ = e.world().push(
loc());
317 type = type ? type : e.world().type_infer_level();
318 return e.world().mut_sigma(type,
num_ptrns());
326 auto _ = e.world().push(
loc());
331 auto _ = e.world().push(
loc());
336 auto _ = e.world().push(
loc());
345 if (
auto def =
decl()->def())
return def;
346 if (
auto nom =
decl()->isa<NomDecl>()) {
347 if (
auto def = nom->emit_ahead(e))
return def;
349 .e(
loc(),
"nominal type `{}` refers to itself",
dbg().sym())
350 .n(
"a `nom` can only refer to itself through a sigma, a variant, or a function type")
353 e.error().e(
loc(),
"`{}` is a module and not a value",
dbg().sym()).bail();
358 return e.world().type(l);
363 return e.world().reform(m);
369 case Tag::K_Level:
return e.world().type_level();
370 case Tag::K_Nat:
return e.world().type_nat();
371 case Tag::K_Idx:
return e.world().type_idx();
372 case Tag::K_Bool:
return e.world().type_bool();
373 case Tag::K_ff:
return e.world().lit_ff();
374 case Tag::K_tt:
return e.world().lit_tt();
375 case Tag::K_i1:
return e.world().lit_i1();
376 case Tag::K_i8:
return e.world().lit_i8();
377 case Tag::K_i16:
return e.world().lit_i16();
378 case Tag::K_i32:
return e.world().lit_i32();
379 case Tag::K_i64:
return e.world().lit_i64();
380 case Tag::K_I1:
return e.world().type_i1();
381 case Tag::K_I8:
return e.world().type_i8();
382 case Tag::K_I16:
return e.world().type_i16();
383 case Tag::K_I32:
return e.world().type_i32();
384 case Tag::K_I64:
return e.world().type_i64();
385 case Tag::T_star:
return e.world().type<0>();
386 case Tag::T_box:
return e.world().type<1>();
387 default: std::unreachable();
395 auto math_f = e.world().annex(e.world().sym(
"math.F"));
396 if (
auto app = type->zonk()->isa<
App>(); math_f && app && app->
callee() == math_f) {
397 if (
auto [p, ex] = app->arg()->projs<2>([](
auto op) { return Lit::isa(op); }); p && ex) {
398 if (*p == 10 && *ex == 5)
return 16;
399 if (*p == 23 && *ex == 8)
return 32;
400 if (*p == 52 && *ex == 11)
return 64;
409 auto val = std::bit_cast<f64>(bits);
411#if defined(__STDCPP_FLOAT16_T__)
412 case 16:
return std::bit_cast<u16>(f16(val));
414 case 16: e.error().e(loc,
"16-bit floating-point literals are not supported on this platform").bail();
416 case 32:
return std::bit_cast<u32>(
f32(val));
427 case Tag::L_f:
return t ? e.world().lit(t,
encode_f(e,
loc(), t,
tok().lit_u())) : e.world().lit_nat(
tok().lit_u());
429 if (
auto size = t ?
Idx::isa_lit(t) : std::nullopt; size && *size != 0) {
431 return e.world().lit(t, (*size - mag % *size) % *size);
434 case Tag::L_u:
return t ? e.world().lit(t,
tok().lit_u()) : e.world().lit_nat(
tok().lit_u());
435 case Tag::L_i: {
auto [size, val] =
tok().
lit_i();
return e.world().lit_idx(size, val); }
436 case Tag::L_c:
return e.world().lit_i8(
tok().lit_c());
437 case Tag::L_str:
return e.world().tuple(
tok().sym());
438 case Tag::T_bot:
return t ? e.world().bot(t) : e.world().type_bot();
439 case Tag::T_top:
return t ? e.world().top(t) : e.world().type_top();
440 default: std::unreachable();
447 for (
auto decl :
decls() | std::views::reverse)
450 for (
auto decl :
decls())
456 assert(
op().isa(Tag::T_arrow_r));
457 return pi_ = e.world().mut_pi(type,
false);
462 pi_->set_codom(
rhs()->
emit(e));
469 types.emplace_back(infix->rhs()->emit(e));
471 types.emplace_back(expr->
emit(e));
475const Def* InfixExpr::emit_index(Emitter& e,
const Def* tup)
const {
478 if (
auto path =
rhs()->isa<PathExpr>(); path && path->path()->dbgs().size() == 1) {
479 auto dbg = path->dbg();
480 if (
auto i = e.find_name(tup->
type(), dbg)) {
481 if (
auto decl = path->decl())
483 .w(dbg.loc(),
"`{}` names both field {} of `{}` and a declaration in scope; using the field", dbg,
485 .n(decl->
loc(),
"declaration here")
486 .n(
"parenthesize it, as in `#({})`, to extract by the declaration instead", dbg);
487 return w.lit(w.type_idx(tup->
type()->
arity()), *i);
489 if (!path->decl()) e.error().e(dbg.loc(),
"cannot resolve field `{}` for extraction", dbg).bail();
496 auto [decl, owner] = e.nom_decl(nom);
497 if (!owner || e.within(owner))
return;
498 auto where = owner->isa<
File>() ? fe::format_cite(
"file `{}`", owner->loc().src->path().filename().string())
499 : fe::format_cite(
"module `{}`", owner->dbg());
501 .e(loc,
"the representation of private nominal type `{}` is only accessible within {}", nom, where)
502 .n(decl->
loc(),
"declared here")
516 if (!nom || !path || path->path()->dbgs().size() != 1)
return nullptr;
518 if (!e.find_name(nom->op(), path->dbg()))
return nullptr;
520 return e.world().struc(tup);
525 switch (
op().tag()) {
527 if (
auto nom = def->isa_type<
Nom>()) {
529 return e.world().struc(def);
531 if (!def->zonk()->isa_type<
Single>()) {
532 auto& err = e.error().e(
533 loc(),
"operand of prefix `#` is of type `{}` but must be of nominal or singleton type",
535 if (def->zonk()->isa<
Nom>()) err.n(
"`{}` is a nominal type itself; `#` unwraps a value of it", def);
538 return e.world().unwrap(def);
539 default: std::unreachable();
547 auto i = std::optional<nat_t>();
549 if (
auto path = index->isa<
PathExpr>(); path && path->
path()->
dbgs().size() == 1) {
550 i = e.find_name(variant, path->
dbg());
551 if (!i && !path->decl())
553 .e(path->dbg().loc(),
"variant `{}` has no constructor `{}`", e.variant_str(variant), path->dbg())
557 if (!i) e.error().e(index->
loc(),
"a case of a variant must be selected by name or by literal index").bail();
560 .e(index->
loc(),
"variant `{}` has {} cases but case {} was requested", e.variant_str(variant),
564 auto type = nom ?
static_cast<const Def*
>(nom) : variant;
565 auto payload = variant->
op(*i);
566 if (payload == w.sigma())
return w.inj(type, *i, w.tuple());
567 auto ctor = w.mut_lam(w.pi(payload, type));
568 return ctor->set(
true, w.inj(type, *i, ctor->var()));
574 switch (
op().tag()) {
578 return w.join(types);
580 case Tag::T_extract: {
581 auto tup = w.implicit_holes(
lhs()->
emit(e));
583 if (
auto nom = tup->isa<
Nom>()) {
584 if (
auto variant = nom->op()->isa<
Variant>()) {
589 if (
auto value =
unwrap_field(e,
this, tup)) tup = value;
590 return w.extract(tup, emit_index(e, tup));
592 case Tag::T_arrow_l: {
593 fe::Vector<const InfixExpr*> exs;
596 exs.emplace_back(ex);
603 .n(
"an update needs a component, as in `tuple#index {} value`",
Tok::tag2str(
op().tag()))
606 auto tup = base->emit(e);
608 fe::Vector<const Nom*> noms;
609 for (
auto ex : exs | std::views::reverse) {
611 noms.emplace_back(value ? tup->
isa_type<
Nom>() :
nullptr);
612 if (value) tup = value;
613 auto idx = ex->emit_index(e, tup);
614 tups.emplace_back(tup);
615 idxs.emplace_back(idx);
616 tup = w.extract(tup, idx);
620 for (
size_t i = tups.size(); i-- != 0;) {
621 val = w.insert(tups[i], idxs[i], val);
622 if (noms[i]) val = w.name(noms[i], val);
633 switch (
op().tag()) {
634 case Tag::T_arrow_r:
return w.pi(l, r);
635 case Tag::T_at:
return w.app(l, r);
637 r = w.implicit_holes(r);
640 default:
return w.implicit_app(c, w.tuple({l, r}));
645 auto _ = e.world().push(
loc());
650 .e(
ptrn()->
loc(),
"pattern of type `{}` does not match the constructor's payload `{}`", dom_t, dom)
652 if (!dom) dom = dom_t;
654 auto lam = e.world().mut_lam(e.world().pi(dom, e.world().mut_hole_type()));
664 if (*index >= variant->
num_ops())
666 .e(
arm->
loc(),
"variant `{}` has {} cases but case {} was requested", e.variant_str(variant),
670 }
else if (
auto ctor =
arm->
ctor()) {
671 i = e.ctor(variant, ctor);
672 }
else if (
auto id =
arm->
ptrn()->isa<IdPtrn>();
id && !id->type()) {
673 i = e.ctor(variant, id->dbg());
676 .e(
arm->
ptrn()->
loc(),
"an arm of a match on variant `{}` must name a constructor",
677 e.variant_str(variant))
681 e.error().w(
arm->
loc(),
"this arm is unreachable: constructor {} is already handled",
682 e.ctor_names(variant, i));
688 auto missing = std::vector<std::string>();
689 for (
size_t i = 0, n = sel.size(); i != n; ++i)
690 if (!sel[i]) missing.emplace_back(
e.ctor_names(variant, i));
691 if (!missing.empty())
e.error().e(
loc(),
"match expression has no arm for {}", fe::Join(missing,
", ")).bail();
694 ops.append_range(sel);
695 return e.world().match(ops);
701 if (
auto variant = ops.front()->
isa_type<
Variant>())
return emit_variant(e, ops.front(), variant);
702 if (
auto nom = ops.front()->isa_type<
Nom>())
703 if (
auto variant = nom->
op()->isa<
Variant>();
706 return emit_variant(e, e.world().struc(ops.front()), variant);
712 .e(
arm->
loc(),
"a constructor pattern needs a variant scrutinee")
713 .n(
"but the scrutinee has type `{}`",
type_of(ops.front()))
715 ops.emplace_back(
arm->
emit(e));
717 auto res = e.world().match(ops);
721 for (
size_t i = 0, n =
num_arms(); i != n; ++i)
722 if (std::ranges::none_of(cases, [&](
const Def* c) {
return Match::accepts(ops[i + 1], c); }))
723 e.error().w(
arm(i)->
loc(),
"this arm is unreachable: no case of `{}` matches `{}`",
type_of(ops.front()),
724 ops[i + 1]->isa_type<
Pi>()->
dom());
732 auto _ = e.world().push(
loc());
733 auto dom_t =
ptrn()->emit_type(e);
736 auto sigma = e.world().mut_sigma(2);
737 auto var = sigma->var();
738 sigma->set(0, dom_t);
739 ptrn()->emit_proj(e, var, 2, 0);
741 sigma->set(1, ret_t);
743 auto imm = sigma->immutabilize();
744 pi_->set_dom(imm ? imm : sigma);
752 return dom()->decl_ = e.world().mut_pi(type, dom()->is_implicit());
759 auto cod = codom() ? codom()->emit(e) : e.world().type_bot();
760 auto pi = dom()->pi_->set_codom(cod);
761 if (
auto imm = pi->immutabilize())
return imm;
775 auto c =
callee()->emit(e);
776 auto a =
arg()->emit(e);
777 return e.world().implicit_app(c, a);
782 auto c =
callee()->emit(e);
783 if (
auto pi = c->type()->isa<
Pi>())
784 if (
auto cn = pi->ret_pi()) {
785 auto a =
arg()->emit(e);
787 if (
auto sigma = pi->dom()->isa_mut<
Sigma>()) {
788 auto n = sigma->num_ops();
789 auto args =
DefVec(n, [&](
size_t i) {
790 return i + 1 == n ? w.bot(sigma->op(i)) : n == 2 ? a : a->proj(n - 1, i);
792 if (
auto inst = sigma->
reduce(w.tuple(args)).back()->isa<
Pi>()) cn = inst;
794 auto con = w.mut_lam(cn);
795 auto pair = w.tuple({a, con});
796 auto app = w.app(c, pair);
797 ptrn()->emit_value(e, con->var());
803 .e(
callee()->
loc(),
"callee of a `ret` expression must be a returning continuation, but `{}` has type `{}`", c,
813 return DefVec(num_ctors(), [&](
size_t i) ->
const Def* {
814 auto type = ctor(i)->type();
815 return type ? type->emit(e) : e.world().sigma();
820 auto variant = e.world().variant(emit_payloads(e));
821 e.add_ctors(variant,
this);
826 return e.world().mut_variant(type,
num_ctors());
831 variant->
set(emit_payloads(e));
832 e.add_ctors(variant,
this);
837 return e.world().tuple(
elems);
841 auto s =
shape()->emit(e);
842 if (
auto lit_s =
Lit::isa(s); lit_s && *lit_s == 0)
return e.world().prod(
is_pack());
844 if (
index()->dbg().is_anon()) {
845 auto b =
body()->emit(e);
846 return e.world().seq(
is_pack(), s, b);
849 auto a = e.world().mut_arr(e.world().type_pending());
853 auto p = e.world().mut_pack(a);
857 auto b =
body()->emit(e);
859 auto arr_b = b->type();
860 if (
auto pvar = var->isa<
Var>())
862 arr_b =
VarRewriter(pvar, a->var()).rewrite(arr_b);
864 return e.world().fuse(p);
869 return e.world().fuse(a);
874 auto def =
body()->emit(e);
875 return is_wrap() ? e.world().wrap(def) : e.world().single(def);
883 auto _ = e.world().push(
loc());
884 auto type = type_->emit(e);
887 if (e.world().inhabitant(
type))
888 e.error().e(type_->loc(),
"axiom of singleton type `{}` carries no information",
type).bail();
890 if (curry_.lit_u() > n_curry)
891 e.error().e(curry_.loc(),
"curry counter cannot be greater than {}", n_curry).bail();
893 n_curry = curry_.lit_u();
897 if (trip_.lit_u() > n_curry)
898 e.error().e(trip_.loc(),
"trip counter cannot be greater than curry counter {}", (
int)n_curry).bail();
900 n_trip = trip_.lit_u();
903 for (
auto name :
names()) {
904 auto annex = name->annex_;
905 if (!annex)
continue;
906 auto plugin = annex->plugin_id();
907 auto tag = annex->id.tag;
908 auto sub = name->sub_;
909 auto norm = e.driver().normalizer(plugin, tag, sub);
910 auto sym = annex->qualified(e.driver(), name->dbg().sym());
911 auto axm = e.world().axm(norm, n_curry, n_trip,
type, plugin, tag, sub)->set(sym);
913 e.world().annexes().attach(plugin, tag, sub, sym, axm);
914 for (
auto alias : name->aliases())
915 e.attach_alias(annex, sub, alias.sym());
921 auto target =
path()->decl();
922 def_ = target->def();
924 e.world().annexes().attach_alias(annex_->plugin_id(), annex_->id.tag, sub_, foreign_);
926 e.attach_alias(annex_, sub_,
dbg().sym());
930 for (
auto decl :
decls()) {
932 e.world().solve_levels();
943 auto _ = e.world().push(
loc());
944 auto v =
value()->emit(e);
946 if (
auto id =
ptrn()->isa<IdPtrn>()) e.attach(id->annex_, id->sub_, id->dbg().sym(),
def_);
955 for (
auto decl :
decls())
957 for (
auto decl :
decls())
962 auto _ = e.world().push(
loc());
963 def_ =
body()->emit_decl(e, e.world().type_infer_level());
968 auto _ = e.world().push(
loc());
976 auto _ = e.world().push(
loc());
977 emit_nom(e,
body()->
emit_decl(e, e.world().type_infer_level()));
981 auto _ = e.world().push(
loc());
1004void NomDecl::emit_nom(
Emitter& e,
const Def* body)
const {
1006 auto sym = annex_ ? annex_->qualified(e.driver(), dbg().sym()) : dbg().sym();
1007 auto nom = e.world().nom(e.world().next_gid(), body);
1009 e.add_nom(nom,
this);
1015 lam_ = e.world().mut_lam(
pi_);
1016 auto _ = e.world().push(
loc());
1019 auto var = lam_->var();
1020 ptrn()->emit_proj(e, var, 2, 0);
1021 ret()->emit(var->proj(2, 1));
1030 auto _ = e.world().push(
loc());
1031 bool is_cps = !
ISA(tag_,
C_DS);
1034 for (
size_t i = 0, n =
num_doms(); i != n; ++i) {
1035 for (
auto dom :
doms() | std::views::drop(i))
1038 auto cod =
codom() ?
codom()->emit(e) : is_cps ? e.world().type_bot() : e.world().mut_hole_type();
1039 for (
auto dom :
doms() | std::views::drop(i) | std::views::reverse)
1040 cod =
dom->pi_->set_codom(cod);
1043 auto lam = cur->emit_value(e);
1044 if (
auto filter = cur->filter()) {
1045 auto _filter = e.world().push(filter->loc());
1046 lam->set_filter(filter->emit(e));
1048 lam->set_filter(i + 1 == n && is_cps ? e.world().lit_ff() : e.world().lit_tt());
1054 dom(i - 1)->lam_->set_body(lam);
1059 if (!
body())
return;
1061 auto _ = e.world().push(
loc());
1063 auto _body = e.world().push(
body()->
loc());
1064 auto lam =
doms().back()->lam_;
1066 nom->emit_sugar(e, lam);
1072 for (
size_t i = 0, n =
num_doms(); i != n; ++i) {
1074 auto lam =
dom(i)->lam_;
1075 auto pi = lam->type()->as_mut<
Pi>();
1076 for (
auto dom :
doms() | std::views::drop(i)) {
1078 if (
auto var = pi->has_var())
1079 if (
auto lam_var =
dom->lam_->has_var()) rw.add(lam_var, var);
1080 auto cod = pi->codom();
1081 if (!cod || !cod->isa_mut<
Pi>())
break;
1082 pi = cod->as_mut<
Pi>();
1085 if (
auto cod = pi->codom(); cod && cod->has_hole()) pi->
set(pi->dom(), rw.rewrite(cod));
1088 for (
auto dom :
doms() | std::views::reverse) {
1089 if (
auto imm =
dom->pi_->immutabilize()) {
1090 auto f =
dom->lam_->filter();
1091 auto b =
dom->lam_->body();
1092 dom->lam_->unset()->set_type(imm)->as<
Lam>()->set(f, b);
1097 auto lam =
doms().front()->lam_;
1098 if (!lam->is_closed())
1101 "external function `{}` is not closed: its inferred type escapes into the scope of `{}`. This "
1102 "usually means an unannotated parameter's type could only be inferred to depend on a variable bound "
1103 "in an inner/sibling scope; add an explicit type annotation to the offending parameter.",
1104 dbg().sym(), lam->free_vars().min()->binder()->sym())
1106 if (
auto prev = e.world().externals()[
dbg().sym()])
1108 .e(
loc(),
"external function `{}` is already defined",
dbg().sym())
1109 .n(prev->loc(),
"previous definition here")
1117 auto _ = e.world().push(
loc());
1118 auto meta_t = e.world().reform(
var()->emit_type(e));
1119 auto rule = e.world().mut_rule(meta_t)->set(
dbg());
1120 var()->emit_value(e, rule->var());
1121 auto l =
lhs()->emit(e);
1122 auto r =
rhs()->emit(e);
1123 auto g =
guard()->emit(e);
const Def * callee() const
static std::pair< u8, u8 > infer_curry_and_trip(const Def *type)
static const Def * coerce(const Def *type, const Def *value)
Can value be assigned to sth of type?
static bool alpha(const Def *d1, const Def *d2)
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
Def * set(size_t i, const Def *)
Successively set from left to right.
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
const Def * zonk() const
If Holes have been filled, reconstruct the program without them.
World & world() const noexcept
const Def * op(size_t i) const noexcept
const Def * var(nat_t a, nat_t i) noexcept
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
const Def * immutabilize()
const Def * arity() const
Number of elements available to Extract / Insert (may be dynamic).
constexpr auto reduce(const Def *arg) const
Dbg dbg() const
Looks up Def::dbg_ in Driver::dbg.
const T * isa_type() const
Is Def::unfold_type a T? Yields nullptr for Level, which has no type at all.
constexpr size_t num_ops() const noexcept
Some "global" variables needed all over the place.
static Hole * isa_unset(const Def *def)
static std::optional< nat_t > isa_lit(const Def *def)
Lam * set_body(const Def *body)
Set body second.
static std::optional< T > isa(const Def *def)
static bool accepts(const Def *arm, const Def *c, bool infer=false)
Does arm handle c?
static DefVec cases(const Def *scrutinee)
Nominal newtype formation.
flags_t key() const
What tells this Nom apart from one over the same Nom::op.
const Def * op() const
The wrapped type.
A dependent function type.
Pi * set(const Def *dom, const Def *codom)
Sigma * set(size_t i, const Def *def)
Singleton type formation.
VarRewriter(World &world)
A variable introduced by a binder (mutable).
A sum type whose cases are positional: unlike a Join, they are neither sorted nor deduplicated.
Variant * set(size_t i, const Def *def)
void attach_alias(flags_t, Sym)
Registers a further Sym for an already attach()ed annex, sharing its flags_t;.
const Def * attach(flags_t, Sym, const Def *)
The World represents the whole program and manages creation of MimIR nodes (Defs).
const Def * sigma(Defs ops)
Owns the arena all AST nodes live in as well as the AnnexInfos of all plugins.
void emit(Emitter &) const override
Dbg dbg() const override
The name this Decl introduces; anonymous if it has none.
const Path * path() const
const Def * emit_value(Emitter &, const Def *) const override
const Def * emit_type(Emitter &) const override
const Ptrn * ptrn() const
Dbg dbg() const override
The name this Decl introduces; anonymous if it has none.
const Def * emit_(Emitter &) const override
const Expr * callee() const
void emit(Emitter &) const override
const Expr * type() const
const Expr * expr() const
const Def * emit_(Emitter &) const override
virtual Dbg dbg() const
The name this Decl introduces; anonymous if it has none.
bool within(const ModDecl *mod) const
void add_ctors(const Def *variant, const VariantExpr *expr)
fe::Vector< const ModDecl * > reset_mods()
void attach(AnnexInfo *annex, sub_t sub, Sym name, const Def *def)
name is this registration's own (unqualified) Dbg::sym; AnnexInfo::qualified turns it into the full p...
std::optional< size_t > find_name(const Def *def, Dbg dbg)
void push_mod(const ModDecl *mod)
void adopt_names(const Nom *nom)
Copies the names of nom's declaration onto the corresponding parts of nom - an instance of it.
void add_nom(const Nom *nom, const NomDecl *decl)
void add_names(const Def *def, const fe::SymMap< size_t > &sym2idx)
void restore_mods(fe::Vector< const ModDecl * > mods)
NomInfo nom_decl(const Nom *nom) const
The declared nom that nom instantiates and, if its representation is private, the mod owning it.
size_t ctor(const Def *variant, Dbg dbg)
std::string ctor_names(const Def *variant, size_t i)
Every name case i goes by, sorted for a deterministic diagnostic; its index if it has none.
void attach_alias(AnnexInfo *annex, sub_t sub, Sym name)
auto variant_str(const Def *variant)
A declared variant prints under its name; an anonymous one spells out its cases, named as source does...
const Def * emit_(Emitter &) const override
const Def * emit_type(Emitter &) const override
const Def * emit_value(Emitter &, const Def *) const override
Base class of all expressions.
const Def * emit(Emitter &) const
const Def * emit_decl(Emitter &, const Def *type) const
virtual const Def * emit_decl_(Emitter &, const Def *) const
virtual const Def * emit_(Emitter &) const =0
void emit_body(Emitter &, const Def *decl) const
virtual void emit_body_(Emitter &, const Def *) const
The AST of one source file: an anonymous ModDecl that a UseDecl binds under a name of its own.
auto implicit_imports() const
Imports the driver was told about via -p; they precede everything the file itself declares.
const Def * emit_type(Emitter &) const override
const Def * emit_value(Emitter &, const Def *) const override
const IdPtrn * id() const
const Def * emit_(Emitter &) const override
const Def * emit(const Def *) const
Dbg dbg() const override
The name this Decl introduces; anonymous if it has none.
const Def * emit_type(Emitter &) const override
Dbg dbg() const override
The name this Decl introduces; anonymous if it has none.
const Def * emit_value(Emitter &, const Def *) const override
const Expr * type() const
lhs op rhs; InfixExpr::op picks the meaning - see MIM_INFIX.
void emit_body_(Emitter &, const Def *decl) const override
static const InfixExpr * isa_op(Tok::Tag tag, const Expr *expr)
const Expr * callee() const
const Def * emit_(Emitter &) const override
const Def * emit_decl_(Emitter &, const Def *type) const override
Lam * emit_value(Emitter &) const
const IdDecl * ret() const
const Expr * codom() const
void emit_decl(Emitter &) const override
void emit_body(Emitter &) const override
const NomDecl * nom_sugar() const
The nom of lam I dom+ = nom I = e; I if nom I dom+ = e desugared to this; within e,...
const Dom * dom(size_t i) const
void emit_body_(Emitter &, const Def *decl) const override
const Def * emit_(Emitter &) const override
const LamDecl * lam() const
const Def * emit_decl_(Emitter &, const Def *type) const override
const Expr * value() const
const Ptrn * ptrn() const
void emit(Emitter &) const override
const Expr * type() const
const Def * emit_(Emitter &) const override
const Expr * body() const
Dbg ctor() const
The constructor in Cons (h, t); a bare Nil is an unannotated IdPtrn instead.
Lam * emit(Emitter &, const Def *dom=nullptr) const
dom is the case's payload type if the scrutinee is a Variant; otherwise ptrn() determines the domain.
const Ptrn * ptrn() const
std::optional< nat_t > index() const
The case this arm selects by literal index, as in 1 (h, t).
const Def * emit_(Emitter &) const override
const Expr * scrutinee() const
const Arm * arm(size_t i) const
mod dbg decls end; also the base of the anonymous File.
void emit_decls(Emitter &) const
void emit(Emitter &) const override
void emit(Emitter &) const override
nom dbg = body; - a nominal newtype.
void emit_sugar(Emitter &, Lam *lam) const
Sets lam's body to this LamDecl::nom_sugar before emitting the latter's body.
const Def * emit_ahead(Emitter &) const
Emits a nom without a mutable body as soon as a sibling refers to it; nullptr on a cycle.
void emit_body(Emitter &) const override
Vis rep_vis() const
The visibility of this nom's representation: that of the enclosing top-level declaration.
void emit_decl(Emitter &) const override
const Def * emit_(Emitter &) const override
const Path * path() const
const Decl * decl() const
virtual void emit_type(Emitter &) const
const Expr * ret_type() const
const Ptrn * ptrn() const
const Def * emit_(Emitter &) const override
const Def * emit_decl_(Emitter &, const Def *type) const override
void emit_body_(Emitter &, const Def *decl) const override
const Def * emit_(Emitter &) const override
const Def * emit_(Emitter &) const override
Base class of all patterns.
virtual const Def * emit_value(Emitter &, const Def *) const =0
const Def * emit_proj(Emitter &, const Def *def, size_t n, size_t i) const
Ptrn::emit_value on def's i-th of n projections - with this Ptrn's Loc, so the projection is blamed o...
virtual const Def * emit_type(Emitter &) const =0
void emit(Emitter &) const override
virtual void emit_body(Emitter &) const
bool has_mut_body() const
Only such a body is emitted as a mutable, so only it may refer to the declaration itself.
virtual void emit_decl(Emitter &) const
Dbg dbg() const override
The name this Decl introduces; anonymous if it has none.
const Expr * body() const
const Ptrn * ptrn() const
const Def * emit_(Emitter &) const override
const Expr * body() const
const Expr * callee() const
const Expr * guard() const
void emit(Emitter &) const override
Dbg dbg() const override
The name this Decl introduces; anonymous if it has none.
const Def * emit_(Emitter &) const override
const Expr * body() const
const IdDecl * index() const
const Def * emit_(Emitter &) const override
const Expr * shape() const
const TuplePtrn * ptrn() const
const Def * emit_decl_(Emitter &, const Def *type) const override
void emit_body_(Emitter &, const Def *decl) const override
const Def * emit_(Emitter &) const override
const Def * emit_(Emitter &) const override
const Expr * body() const
std::pair< uint64_t, uint64_t > lit_i() const
static const char * tag2str(Tok::Tag)
const Expr * elem(size_t i) const
const Def * emit_(Emitter &) const override
(ptrn_0, ..., ptrn_n-1), [ptrn_0, ..., ptrn_n-1], or {ptrn_0, ..., ptrn_n-1}
const Ptrn * ptrn(size_t i) const
const Def * emit_type(Emitter &) const override
const Def * emit_decl(Emitter &, const Def *type) const
Sym field(size_t i) const
The field name of ptrn(i), or the empty Sym if it binds none.
const Def * emit_value(Emitter &, const Def *) const override
const Def * emit_body(Emitter &, const Def *decl) const
const Def * emit_(Emitter &) const override
const Expr * level() const
void emit(Emitter &) const override
const File * file() const
const Mods & mods() const
bool is_extern() const override
Whether this decl crosses the Mim/native boundary (extern); body presence picks which side supplies i...
| ctor_0: type_0 | ... | ctor_n-1: type_n-1; a constructor without a type carries [].
void emit_body_(Emitter &, const Def *decl) const override
const Ctor * ctor(size_t i) const
const Def * emit_decl_(Emitter &, const Def *type) const override
const Def * emit_(Emitter &) const override
Families of Tok::Tag as reusable case labels; include this in *.cpp files only.
#define C_DS
Direct-style binders; all other binders are CPS.
#define ISA(tag, family)
Turns such a family into a predicate - a case label is of no use outside of a switch.
static bool is_wildcard(const Ptrn *ptrn)
A wildcard binds nothing, so the Def it would name need not exist - and a dump would then have to nam...
static const Def * unwrap_field(Emitter &e, const InfixExpr *ex, const Def *tup)
Unwraps tup if it is a value of a nom and ex names one of its fields; nullptr otherwise.
static const Def * assign(Emitter &e, const Ptrn *ptrn, const Def *type, const Def *def)
static u64 encode_f(Emitter &e, Loc loc, const Def *t, u64 bits)
A float Tok stores its value as mim::f64 bits; re-encode them for the width of the annotated type t.
static void emit_var(Emitter &e, const Ptrn *ptrn, Def *mut)
Binds ptrn to the Var of mut - which a wildcard does not create.
static void check_nom_scope(Emitter &e, Loc loc, const Nom *nom)
A private nom's representation is confined to the mod that declared it; the type itself is not.
static const Def * emit_ctor(Emitter &e, const Variant *variant, const Expr *index, const Nom *nom=nullptr)
T#C or T#i selects case C or i of the Variant T: a constructor function - or its value for a [] paylo...
static void open_nom(Emitter &e, Loc loc, const Nom *nom)
Checks and prepares access to nom's representation.
static void emit_union(Emitter &e, const Expr *expr, DefVec &types)
a ∪ b ∪ c is one n-ary Join, so flatten the left spine the left-associative parse built.
static std::optional< nat_t > isa_math_f(Emitter &e, const Def *type)
If type is a math.F type of known precision/exponent, yields its bit width.
GIDMap< const Def *, To > DefMap
fe::Vector< const Def * > DefVec
GIDSet< const Def * > DefSet
auto type_of(const Def *def)
Def::unfold_type of def for a diagnostic - Level is the one Def that has no type at all.
Bookkeeping of an annex introduced by an AxmDecl.
Sym qualified(Driver &driver, Sym own) const
Fully-qualified plugin.tag[.sub] name for own (this decl's own Dbg::sym), registered for by-name look...
struct mim::ast::AnnexInfo::@177100250272201136376142224053244231100060214216 id
plugin_t plugin_id() const
The mangled plugin part of the flags.