16bool is_shape(
const Def* s) {
17 if (
s->isa<
Nat>())
return true;
18 if (
auto arr =
s->isa<
Arr>())
return arr->body()->isa<
Nat>();
19 if (
auto sig =
s->isa_imm<
Sigma>())
20 return std::ranges::all_of(sig->ops(), [](
const Def* op) { return op->isa<Nat>(); });
31#if (!defined(_MSC_VER) && defined(NDEBUG))
32bool World::Lock::guard_ =
false;
54 data_.lit_bool[0] =
lit_idx(2, 0_u64);
55 data_.lit_bool[1] =
lit_idx(2, 1_u64);
63 for (
auto def : move_.defs) def->~Def();
78 DLOG(
"register: 0x{x} -> {}", f, def);
80 if (
driver().is_loaded(plugin)) {
90 def->external_ =
true;
96 def->external_ =
false;
97 auto num = move_.sym2external.erase(def->
sym());
98 assert_unused(num == 1);
107 error(level->
loc(),
"argument `{}` to `Type` must be of type `Univ` but is of type `{}`", level, level->
type());
109 return unify<Type>(1, level)->as<
Type>();
114 error(op->
loc(),
"operand '{}' of a universe increment must be of type `Univ` but is of type `{}`", op,
118 return unify<UInc>(1, op, offset);
125 if (
auto um = op->isa<
UMax>())
126 ops.insert(ops.end(), um->ops().begin(), um->ops().end());
128 ops.emplace_back(op);
131 for (
auto& op : ops) {
139 error(r->loc(),
"operand '{}' must be a Type of some level", r);
142 if (!r->type()->isa<
Univ>())
143 error(r->loc(),
"operand '{}' of a universe max must be of type 'Univ' but is of type '{}'", r, r->type());
148 lvl = std::max(lvl, *l);
157 std::ranges::sort(ops, [](
auto op1,
auto op2) {
return op1->gid() < op2->gid(); });
158 ops.erase(std::unique(ops.begin(), ops.end()), ops.end());
159 ldef = unify<UMax>(ops.size(), *
this, ops);
172 if (
auto var = mut->var_)
return var;
173 return mut->var_ = unify<Var>(1,
type, mut);
182 callee = callee->
zonk();
184 if (
auto pi = callee->
type()->isa<
Pi>()) {
186 arg = new_arg->
zonk();
187 if (
auto imm = callee->
isa_imm<
Lam>())
return imm->body();
190 if (rw.rewrite(
lam->filter()) ==
lit_tt()) {
191 DLOG(
"partial evaluate: {} ({})",
lam, arg);
192 return rw.rewrite(
lam->body());
196 auto type =
pi->reduce(arg)->zonk();
197 callee = callee->
zonk();
200 curry = curry == 0 ? trip : curry;
203 if (
auto normalizer =
axiom->normalizer(); Normalize && normalizer && curry == 0) {
204 if (
auto norm = normalizer(
type, callee, arg))
return norm;
212 .
error(arg->
loc(),
"cannot apply argument to callee")
213 .
note(callee->
loc(),
"callee: '{}'", callee)
214 .
note(arg->
loc(),
"argument: '{}'", arg)
215 .
note(callee->
loc(),
"vvv domain type vvv\n'{}'\n'{}'",
pi->dom(), arg->
type())
216 .
note(arg->
loc(),
"^^^ argument type ^^^");
220 .
error(callee->
loc(),
"called expression not of function type")
221 .
error(callee->
loc(),
"'{}' <--- callee type", callee->
type());
227 curry = curry == 0 ? trip : curry;
235 return unify<App>(2,
axiom, curry, trip,
type, callee, arg);
240 if (n == 0)
return sigma();
241 if (n == 1)
return ops[0];
243 return unify<Sigma>(ops.size(),
Sigma::infer(*
this, ops), ops);
248 if (n == 0)
return tuple();
249 if (n == 1)
return ops[0];
255 error(t->loc(),
"cannot assign tuple '{}' of type '{}' to incompatible tuple type '{}'", t, t->type(),
sigma);
265 if (n == 0)
return tuple();
266 if (n == 1)
return ops[0];
273 if (
auto extract = ops[0]->isa<Extract>()) {
275 bool eta = tup->type() ==
type;
276 for (
size_t i = 0; i != n && eta; ++i) {
277 if (
auto extract = ops[i]->isa<Extract>()) {
279 if (eta &=
u64(i) == *index) {
280 eta &=
extract->tuple() == tup;
292 return unify<Tuple>(ops.size(),
type, ops);
297 std::ranges::transform(
sym, std::back_inserter(defs), [
this](
auto c) {
return lit_i8(c); });
302 if (index->isa<
Tuple>()) {
304 auto idx =
DefVec(n, [&](
size_t i) {
return index->
op(i); });
305 auto ops =
DefVec(n, [&](
size_t i) {
return d->proj(n,
Lit::as(idx[i])); });
307 }
else if (index->isa<
Pack>()) {
308 auto ops =
DefVec(index->
as_lit_arity(), [&](
size_t) { return extract(d, index->ops().back()); });
313 const Def*
type = d->unfold_type()->zonk();
315 if (
auto l =
Lit::isa(size); l && *l == 1) {
316 if (
auto l =
Lit::isa(index); !l || *l != 0)
WLOG(
"unknown Idx of size 1: {}", index);
324 if (
auto pack = d->isa_imm<
Pack>())
return pack->body();
327 error(index->
loc(),
"index '{}' does not fit within arity '{}'", index,
type->arity());
335 if (
auto hole = d->isa_mut<
Hole>()) d = hole->tuplefy();
346 return unify<Extract>(2, t, d, index);
349 return unify<Extract>(2,
sigma->op(*i), d, index);
355 elem_t =
arr->reduce(index);
360 return unify<Extract>(2, elem_t, d, index);
364 auto type = d->unfold_type();
369 error(index->
loc(),
"index '{}' does not fit within arity '{}'", index,
type->arity());
372 auto elem_type =
type->proj(*lidx);
376 .
error(val->
loc(),
"value to be inserted not assignable to element")
377 .
note(val->
loc(),
"vvv value type vvv \n'{}'\n'{}'", val->
type(), elem_type)
378 .
note(val->
loc(),
"^^^ element type ^^^", elem_type);
383 if (
auto l =
Lit::isa(size); l && *l == 1)
384 return tuple(d, {val});
387 if (
auto t = d->isa<
Tuple>(); t && lidx)
return t->refine(*lidx, val);
391 if (
auto a =
pack->isa_lit_arity(); a && *a <
flags().scalarize_threshold) {
393 new_ops[*lidx] = val;
403 return unify<Insert>(3, d, index, val);
408 if (!is_shape(shape->type()))
error(shape->loc(),
"expected shape but got '{}' of type '{}'", shape, shape->type());
411 if (*a == 0)
return sigma();
412 if (*a == 1)
return body;
416 if (
auto ex = shape->isa<
Extract>()) {
417 if (
auto tup = ex->tuple()->isa<
Tuple>()) {
418 auto arrs =
DefVec(tup->num_ops(), [&](
size_t i) { return arr(tup->op(i), body); });
427 if (
auto p = shape->isa<
Pack>()) {
428 if (
auto s =
Lit::isa(p->shape()))
return arr(*s,
arr(
pack(*s - 1, p->body()), body));
431 return unify<Arr>(2, body->
unfold_type(), shape, body);
435 if (!is_shape(shape->type()))
error(shape->loc(),
"expected shape but got '{}' of type '{}'", shape, shape->type());
438 if (*a == 0)
return tuple();
439 if (*a == 1)
return body;
446 if (
auto p = shape->isa<
Pack>()) {
451 return unify<Pack>(1,
type, body);
455 if (shape.empty())
return body;
456 return arr(shape.rsubspan(1),
arr(shape.back(), body));
460 if (shape.empty())
return body;
461 return pack(shape.rsubspan(1),
pack(shape.back(), body));
467 if (*s != 0 && val >= *s)
error(
type->loc(),
"index '{}' does not fit within arity '{}'", size, val);
468 }
else if (val != 0) {
469 error(
type->loc(),
"cannot create literal '{}' of 'Idx {}' as size is unknown", val, size);
473 return unify<Lit>(0,
type, val);
484 return unify<TExt<Up>>(0,
type);
491 if (std::ranges::any_of(ops, [&](
const Def* op) {
return Up ?
bool(op->isa<
Top>()) :
bool(op->isa<
Bot>()); }))
495 DefVec cpy(ops.begin(), ops.end());
496 auto [_, end] = std::ranges::copy_if(ops, cpy.begin(), [&](
const Def* op) { return !op->isa<Ext>(); });
500 end = std::unique(cpy.begin(), end);
501 cpy.resize(std::distance(cpy.begin(), end));
503 if (cpy.size() == 0)
return ext<!Up>(kind);
504 if (cpy.size() == 1)
return cpy[0];
508 return unify<TBound<Up>>(cpy.size(), kind, cpy);
513 auto types =
DefVec(ops.size(), [&](
size_t i) { return ops[i]->type(); });
514 return unify<Ac>(ops.size(),
meet(types), ops);
517 assert(ops.size() == 1);
524 if (
type->isa<
Join>())
return unify<Vel>(1,
type, value);
531 auto m_pi =
match->type()->isa<
Pi>();
532 auto c_pi = clash->
type()->isa<
Pi>();
535 assert(m_pi && c_pi);
536 auto a = m_pi->dom()->isa_lit_arity();
537 assert_unused(a && *a == 2);
540 auto codom =
join({m_pi->codom(), c_pi->
codom()});
541 return unify<Test>(4,
pi(c_pi->
dom(), codom), value, probe,
match, clash);
547 auto name = symbol.str();
549 auto pos =
name.find(suffix);
550 if (pos != std::string::npos) {
551 auto num =
name.substr(pos + suffix.size());
556 num = std::to_string(std::stoi(num) + 1);
557 name =
name.substr(0, pos + suffix.size()) +
"_" + num;
570#ifdef MIM_ENABLE_CHECKS
575 auto i = std::ranges::find_if(move_.defs, [=](
auto def) { return def->gid() == gid; });
576 if (i == move_.defs.end())
return nullptr;
581 for (
auto mut :
externals()) assert(mut->is_closed() && mut->is_set());
582 for (
auto anx :
annexes()) assert(anx->is_closed());
A (possibly paramterized) Array.
static std::tuple< const Axiom *, u8, u8 > get(const Def *def)
Yields currying counter of def.
static constexpr u8 Trip_End
static const Def * is_uniform(Defs defs)
Yields defs.front(), if all defs are Check::alpha-equivalent (Mode::Test) and nullptr otherwise.
static bool alpha(const Def *d1, const Def *d2)
static const Def * assignable(const Def *type, const Def *value)
Can value be assigned to sth of type?
nat_t as_lit_arity() const
constexpr auto ops() const noexcept
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
const Def * op(size_t i) const noexcept
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
const T * isa_imm() const
bool is_closed() const
Has no free_vars()?
constexpr size_t num_ops() const noexcept
Some "global" variables needed all over the place.
Error & error(Loc loc, const char *s, Args &&... args)
Error & note(Loc loc, const char *s, Args &&... args)
This node is a hole in the IR that is inferred by its context later on.
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
static std::optional< T > isa(const Def *def)
static T as(const Def *def)
Facility to log what you are doing.
A (possibly paramterized) Tuple.
A dependent function type.
const Def * codom() const
static Pi * isa_implicit(const Def *d)
Is d an Pi::is_implicit (mutable) Pi?
virtual const Def * rewrite(const Def *)
static const Def * infer(World &, Defs)
Data constructor for a Sigma.
static const Def * infer(World &, Defs)
const Lit * lit_idx(nat_t size, u64 val)
Constructs a Lit of type Idx of size size.
const Def * insert(const Def *d, const Def *i, const Def *val)
const Def * meet(Defs ops)
const Def * uinc(const Def *op, level_t offset=1)
const Def * raw_app(const Axiom *axiom, u8 curry, u8 trip, const Def *type, const Def *callee, const Def *arg)
const Lit * lit(const Def *type, u64 val)
const Def * ac(const Def *type, Defs ops)
const Type * type(const Def *level)
void make_external(Def *)
const Driver & driver() const
const Def * sigma(Defs ops)
const Def * pack(const Def *arity, const Def *body)
const Def * app(const Def *callee, const Def *arg)
const Def * arr(const Def *shape, const Def *body)
const Pi * pi(const Def *dom, const Def *codom, bool implicit=false)
const Axiom * axiom(NormalizeFn n, u8 curry, u8 trip, const Def *type, plugin_t p, tag_t t, sub_t s)
World & verify()
Verifies that all externals() and annexes() are Def::is_closed(), if MIM_ENABLE_CHECKS.
const Def * pick(const Def *type, const Def *value)
Hole * mut_hole(const Def *type)
const Lam * lam(const Pi *pi, Lam::Filter f, const Def *body)
const Def * tuple(Defs ops)
const Def * gid2def(u32 gid)
Lookup Def by gid.
Flags & flags()
Retrieve compile Flags.
const Def * implicit_app(const Def *callee, const Def *arg)
Places Holes as demanded by Pi::is_implicit() and then apps arg.
const Def * var(const Def *type, Def *mut)
const Def * extract(const Def *d, const Def *i)
Sym sym(std::string_view)
const Def * bound(Defs ops)
const Def * join(Defs ops)
const Def * ext(const Def *type)
Sym append_suffix(Sym name, std::string suffix)
Appends a suffix or an increasing number if the suffix already exists.
void make_internal(Def *)
const Lit * lit_univ(u64 level)
const Tuple * tuple()
the unit value of type []
const Def * vel(const Def *type, const Def *value)
const Def * uniq(const Def *inhabitant)
const Sigma * sigma()
The unit type within Type 0.
const Lit * lit_nat(nat_t a)
const State & state() const
const Def * register_annex(flags_t f, const Def *)
void breakpoint(u32 gid)
Trigger breakpoint in your debugger when creating Def with Def::gid gid.
Sigma * mut_sigma(const Def *type, size_t size)
const Def * test(const Def *value, const Def *probe, const Def *match, const Def *clash)
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Vector< const Def * > DefVec
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
void error(Loc loc, const char *f, Args &&... args)
auto match(const Def *def)
Compiler switches that must be saved and looked up in later phases of compilation.
static Sym demangle(Driver &, plugin_t plugin)
Reverts an Axiom::mangled string to a Sym.