MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
def.h
Go to the documentation of this file.
1#pragma once
2
3#include <limits>
4#include <optional>
5#include <span>
6
7#include <fe/assert.h>
8#include <fe/cast.h>
9#include <fe/enum.h>
10
11#include "mim/config.h"
12
13#include "mim/util/dbg.h"
14#include "mim/util/sets.h"
15#include "mim/util/util.h"
16#include "mim/util/vector.h"
17
18// clang-format off
19#define MIM_NODE(X) \
20 X(Lit, Judge::Intro) /* keep this first - causes Lit to appear left in Def::less/Def::greater*/ \
21 X(Axm, Judge::Intro) \
22 X(Var, Judge::Intro) \
23 X(Global, Judge::Intro) \
24 X(Proxy, Judge::Intro) \
25 X(Hole, Judge::Hole ) \
26 X(Type, Judge::Meta ) X(Univ, Judge::Meta ) X(UMax, Judge::Meta) X(UInc, (Judge::Meta )) \
27 X(Pi, Judge::Form ) X(Lam, Judge::Intro) X(App, Judge::Elim) \
28 X(Sigma, Judge::Form ) X(Tuple, Judge::Intro) X(Extract, Judge::Elim) X(Insert, (Judge::Intro | Judge::Elim)) \
29 X(Arr, Judge::Form ) X(Pack, Judge::Intro) \
30 X(Join, Judge::Form ) X(Inj, Judge::Intro) X(Match, Judge::Elim) X(Top, (Judge::Intro )) \
31 X(Meet, Judge::Form ) X(Merge, Judge::Intro) X(Split, Judge::Elim) X(Bot, (Judge::Intro )) \
32 X(Reform, Judge::Form ) X(Rule, Judge::Intro) \
33 X(Uniq, Judge::Form ) \
34 X(Nat, Judge::Form ) \
35 X(Idx, Judge::Intro)
36
37#define MIM_IMM_NODE(X) \
38 X(Lit) \
39 X(Axm) \
40 X(Var) \
41 X(Proxy) \
42 X(Type) X(Univ) X(UMax) X(UInc) \
43 X(Pi) X(Lam) X(App) \
44 X(Sigma) X(Tuple) X(Extract) X(Insert) \
45 X(Arr) X(Pack) \
46 X(Join) X(Inj) X(Match) X(Top) \
47 X(Meet) X(Merge) X(Split) X(Bot) \
48 X(Reform) X(Rule) \
49 X(Uniq) \
50 X(Nat) \
51 X(Idx)
52
53#define MIM_MUT_NODE(X) \
54 X(Global) \
55 X(Hole) \
56 X(Pi) X(Lam) \
57 X(Sigma) \
58 X(Arr) X(Pack) \
59 X(Rule)
60// clang-format on
61
62namespace mim {
63
64class App;
65class Axm;
66class Var;
67class Def;
68class World;
69
70/// @name Def
71/// GIDSet / GIDMap keyed by Def::gid of `conset Def*`.
72///@{
73template<class To>
79///@}
80
81/// @name Def (Mutable)
82/// GIDSet / GIDMap keyed by Def::gid of `Def*`.
83///@{
84template<class To>
89///@}
90
91/// @name Var
92/// GIDSet / GIDMap keyed by Var::gid of `const Var*`.
93///@{
94template<class To>
99///@}
100
101using NormalizeFn = const Def* (*)(const Def*, const Def*, const Def*);
102
103using fe::operator&;
104using fe::operator|;
105using fe::operator^;
106using fe::operator<=>;
107using fe::operator==;
108using fe::operator!=;
109
110/// @name Enums that classify certain aspects of Def%s.
111///@{
112
113enum class Node : node_t {
114#define CODE(node, _) node,
116#undef CODE
117};
118
119#define CODE(node, _) +size_t(1)
120static constexpr size_t Num_Nodes = size_t(0) MIM_NODE(CODE);
121#undef CODE
122
123/// Tracks a dependency to certain Def%s transitively through the Def::deps() up to but excliding *mutables*.
124enum class Dep : unsigned {
125 None = 0,
126 Mut = 1 << 0,
127 Var = 1 << 1,
128 Hole = 1 << 2,
129 Proxy = 1 << 3,
130};
131
132/// [Judgement](https://ncatlab.org/nlab/show/judgment).
133enum class Judge : u32 {
134 // clang-format off
135 Form = 1 << 0, ///< [Type Formation](https://ncatlab.org/nlab/show/type+formation) like `T -> T`.
136 Intro = 1 << 1, ///< [Term Introduction](https://ncatlab.org/nlab/show/natural+deduction) like `λ(x: Nat): Nat = x`.
137 Elim = 1 << 2, ///< [Term Elimination](https://ncatlab.org/nlab/show/term+elimination) like `f a`.
138 Meta = 1 << 3, ///< Meta rules for Univ%erse and Type levels.
139 Hole = 1 << 4, ///< Special rule for Hole.
140 // clang-format on
141};
142
143/// [Judgement](https://ncatlab.org/nlab/show/judgment).
144enum class Mut {
145 // clang-format off
146 Mut = 1 << 0, ///< Node may be mutable.
147 Imm = 1 << 1, ///< Node may be immmutable.
148 // clang-format on
149};
150///@}
151
152} // namespace mim
153
154#ifndef DOXYGEN
155// clang-format off
156template<> struct fe::is_bit_enum<mim::Dep> : std::true_type {};
157template<> struct fe::is_bit_enum<mim::Judge> : std::true_type {};
158template<> struct fe::is_bit_enum<mim::Mut> : std::true_type {};
159// clang-format on
160#endif
161
162namespace mim {
163
164/// Use as mixin to wrap all kind of Def::proj and Def::projs variants.
165#define MIM_PROJ(NAME, CONST) \
166 nat_t num_##NAME##s() CONST noexcept { return ((const Def*)NAME())->num_projs(); } \
167 nat_t num_t##NAME##s() CONST noexcept { return ((const Def*)NAME())->num_tprojs(); } \
168 const Def* NAME(nat_t a, nat_t i) CONST noexcept { return ((const Def*)NAME())->proj(a, i); } \
169 const Def* NAME(nat_t i) CONST noexcept { return ((const Def*)NAME())->proj(i); } \
170 const Def* t##NAME(nat_t i) CONST noexcept { return ((const Def*)NAME())->tproj(i); } \
171 template<nat_t A = std::dynamic_extent, class F> \
172 auto NAME##s(F f) CONST noexcept { \
173 return ((const Def*)NAME())->projs<A, F>(f); \
174 } \
175 template<class F> \
176 auto t##NAME##s(F f) CONST noexcept { \
177 return ((const Def*)NAME())->tprojs<F>(f); \
178 } \
179 template<nat_t A = std::dynamic_extent> \
180 auto NAME##s() CONST noexcept { \
181 return ((const Def*)NAME())->projs<A>(); \
182 } \
183 auto t##NAME##s() CONST noexcept { return ((const Def*)NAME())->tprojs(); } \
184 template<class F> \
185 auto NAME##s(nat_t a, F f) CONST noexcept { \
186 return ((const Def*)NAME())->projs<F>(a, f); \
187 } \
188 auto NAME##s(nat_t a) CONST noexcept { return ((const Def*)NAME())->projs(a); }
189
190/// CRTP-based mixin to declare setters for Def::loc \& Def::name using a *covariant* return type.
191template<class P, class D = Def>
192class // D is only needed to make the resolution `D::template set` lazy
193#ifdef _MSC_VER
194 __declspec(empty_bases)
195#endif
196 Setters {
197private:
198 P* super() { return static_cast<P*>(this); }
199 const P* super() const { return static_cast<const P*>(this); }
200
201public:
202 // clang-format off
203 template<bool Ow = false> const P* set(Loc l ) const { super()->D::template set<Ow>(l); return super(); }
204 template<bool Ow = false> P* set(Loc l ) { super()->D::template set<Ow>(l); return super(); }
205 template<bool Ow = false> const P* set( Sym s ) const { super()->D::template set<Ow>(s); return super(); }
206 template<bool Ow = false> P* set( Sym s ) { super()->D::template set<Ow>(s); return super(); }
207 template<bool Ow = false> const P* set( std::string s) const { super()->D::template set<Ow>(std::move(s)); return super(); }
208 template<bool Ow = false> P* set( std::string s) { super()->D::template set<Ow>(std::move(s)); return super(); }
209 template<bool Ow = false> const P* set(Loc l, Sym s ) const { super()->D::template set<Ow>(l, s); return super(); }
210 template<bool Ow = false> P* set(Loc l, Sym s ) { super()->D::template set<Ow>(l, s); return super(); }
211 template<bool Ow = false> const P* set(Loc l, std::string s) const { super()->D::template set<Ow>(l, std::move(s)); return super(); }
212 template<bool Ow = false> P* set(Loc l, std::string s) { super()->D::template set<Ow>(l, std::move(s)); return super(); }
213 template<bool Ow = false> const P* set(Dbg d ) const { super()->D::template set<Ow>(d); return super(); }
214 template<bool Ow = false> P* set(Dbg d ) { super()->D::template set<Ow>(d); return super(); }
215 // clang-format on
216};
217
218/// Base class for all Def%s.
219///
220/// These are the most important subclasses:
221/// | Type Formation | Term Introduction | Term Elimination |
222/// | ----------------- | ----------------- | ----------------- |
223/// | Pi | Lam | App |
224/// | Sigma / Arr | Tuple / Pack | Extract |
225/// | | Insert | Insert |
226/// | Uniq | Wrap | Unwrap |
227/// | Join | Inj | Match |
228/// | Meet | Merge | Split |
229/// | Reform | Rule | |
230/// | Nat | Lit | |
231/// | Idx | Lit | |
232/// In addition there is:
233/// * Var: A variable. Currently the following Def%s may be binders:
234/// * Pi, Lam, Sigma, Arr, Pack
235/// * Axm: To introduce new entities.
236/// * Proxy: Used for intermediate values during optimizations.
237/// * Hole: A metavariable filled in by the type inference (always mutable as holes are filled in later).
238/// * Type, Univ, UMax, UInc: To keep track of type levels.
239///
240/// The data layout (see World::alloc and Def::deps) looks like this:
241/// ```
242/// Def| type | op(0) ... op(num_ops-1) |
243/// |-----------ops-----------|
244/// deps
245/// |--------------------------------| if type() != nullptr && is_set()
246/// |-------------------------| if type() == nullptr && is_set()
247/// |------| if type() != nullptr && !is_set()
248/// || if type() == nullptr && !is_set()
249/// ```
250/// @attention This means that any subclass of Def **must not** introduce additional members.
251/// @see @ref mut
252class Def : public fe::RuntimeCast<Def> {
253private:
254 Def& operator=(const Def&) = delete;
255 Def(const Def&) = delete;
256
257protected:
258 /// @name C'tors and D'tors
259 ///@{
260 Def(World*, Node, const Def* type, Defs ops, flags_t flags); ///< Constructor for an *immutable* Def.
261 Def(Node, const Def* type, Defs ops, flags_t flags); ///< As above but World retrieved from @p type.
262 Def(Node, const Def* type, size_t num_ops, flags_t flags); ///< Constructor for a *mutable* Def.
263 virtual ~Def() = default;
264 ///@}
265
266public:
267 /// @name Getters
268 ///@{
269 World& world() const noexcept;
270 constexpr flags_t flags() const noexcept { return flags_; }
271 constexpr u32 gid() const noexcept { return gid_; } ///< Global id - *unique* number for this Def.
272 constexpr u32 tid() const noexcept { return tid_; } ///< Trie id - only used in Trie.
273 constexpr u32 mark() const noexcept { return mark_; } ///< Used internally by free_vars().
274 constexpr size_t hash() const noexcept { return hash_; }
275 constexpr Node node() const noexcept { return node_; }
276 std::string_view node_name() const;
277 ///@}
278
279 /// @name Judgement
280 /// What kind of Judge%ment represents this Def?
281 ///@{
282 u32 judge() const noexcept;
283 // clang-format off
284 bool is_form() const noexcept { return judge() & Judge::Form; }
285 bool is_intro() const noexcept { return judge() & Judge::Intro; }
286 bool is_elim() const noexcept { return judge() & Judge::Elim; }
287 bool is_meta() const noexcept { return judge() & Judge::Meta; }
288 // clang-format on
289 ///@}
290
291 /// @name type
292 ///@{
293
294 /// Yields the "raw" type of this Def (maybe `nullptr`).
295 /// @see Def::unfold_type.
296 const Def* type() const noexcept;
297 /// Yields the type of this Def and builds a new `Type (UInc n)` if necessary.
298 const Def* unfold_type() const;
299 bool is_term() const;
300 virtual const Def* arity() const;
301 ///@}
302
303 /// @name ops
304 ///@{
305 template<size_t N = std::dynamic_extent>
306 constexpr auto ops() const noexcept {
307 return View<const Def*, N>(ops_ptr(), num_ops_);
308 }
309 const Def* op(size_t i) const noexcept { return ops()[i]; }
310 constexpr size_t num_ops() const noexcept { return num_ops_; }
311 ///@}
312
313 /// @name Setting Ops (Mutables Only)
314 /// @anchor set_ops
315 /// You can set and change the Def::ops of a mutable after construction.
316 /// However, you have to obey the following rules:
317 /// If Def::is_set() is ...
318 /// * `false`, [set](@ref Def::set) the [operands](@ref Def::ops) from left to right.
319 /// * `true`, Def::unset() the operands first and then start over:
320 /// ```
321 /// mut->unset()->set({a, b, c});
322 /// ```
323 ///
324 /// MimIR assumes that a mutable is *final*, when its last operand is set.
325 /// Then, Def::check() will be invoked.
326 ///@{
327 bool is_set() const; ///< Yields `true` if empty or the last op is set.
328 Def* set(size_t i, const Def*); ///< Successively set from left to right.
329 Def* set(Defs ops); ///< Set @p ops all at once (no Def::unset necessary beforehand).
330 Def* unset(); ///< Unsets all Def::ops; works even, if not set at all or only partially set.
331
332 /// Update type.
333 /// @warning Only make type-preserving updates such as removing Hole%s.
334 /// Do this even before updating all other ops()!
335 Def* set_type(const Def*);
336 ///@}
337
338 /// @name deps
339 /// All *dependencies* of a Def and includes:
340 /// * Def::type() (if not `nullptr`) and
341 /// * the other Def::ops() (only included, if Def::is_set()) in this order.
342 ///@{
343 Defs deps() const noexcept;
344 const Def* dep(size_t i) const noexcept { return deps()[i]; }
345 size_t num_deps() const noexcept { return deps().size(); }
346 ///@}
347
348 /// @name has_dep
349 /// Checks whether one Def::deps() contains specific elements defined in Dep.
350 /// This works up to the next *mutable*.
351 /// For example, consider the Tuple `tup`: `(?, lam (x: Nat) = y)`:
352 /// ```
353 /// bool has_hole = tup->has_dep(Dep::Hole); // true
354 /// bool has_mut = tup->has_dep(Dep::Mut); // true
355 /// bool has_var = tup->has_dep(Dep::Var); // false - y is contained in another mutable
356 /// ```
357 ///@{
358 bool has_dep() const { return dep_ != 0; }
359 bool has_dep(Dep d) const { return has_dep(unsigned(d)); }
360 bool has_dep(unsigned u) const { return dep_ & u; }
361 ///@}
362
363 /// @name proj
364 /// @anchor proj
365 /// Splits this Def via Extract%s or directly accessing the Def::ops in the case of Sigma%s or Arr%ays.
366 /// ```
367 /// std::array<const Def*, 2> ab = def->projs<2>();
368 /// std::array<u64, 2> xy = def->projs<2>([](auto def) { return Lit::as(def); });
369 /// auto [a, b] = def->projs<2>();
370 /// auto [x, y] = def->projs<2>([](auto def) { return Lit::as(def); });
371 /// Vector<const Def*> projs1 = def->projs(); // "projs1" has def->num_projs() many elements
372 /// Vector<const Def*> projs2 = def->projs(n);// "projs2" has n elements - asserts if incorrect
373 /// // same as above but applies Lit::as<nat_t>(def) to each element
374 /// Vector<const Lit*> lits1 = def->projs( [](auto def) { return Lit::as(def); });
375 /// Vector<const Lit*> lits2 = def->projs(n, [](auto def) { return Lit::as(def); });
376 /// ```
377 ///@{
378
379 /// Yields Def::arity(), if it is a Lit, or `1` otherwise.
380 nat_t num_projs() const;
381 nat_t num_tprojs() const; ///< As above but yields 1, if Flags::scalarize_threshold is exceeded.
382
383 /// Similar to World::extract while assuming an arity of @p a, but also works on Sigma%s and Arr%ays.
384 const Def* proj(nat_t a, nat_t i) const;
385 const Def* proj(nat_t i) const { return proj(num_projs(), i); } ///< As above but takes Def::num_projs as arity.
386 const Def* tproj(nat_t i) const { return proj(num_tprojs(), i); } ///< As above but takes Def::num_tprojs.
387
388 /// Splits this Def via Def::proj%ections into an Array (if `A == std::dynamic_extent`) or `std::array` (otherwise).
389 /// Applies @p f to each element.
390 template<nat_t A = std::dynamic_extent, class F>
391 auto projs(F f) const {
392 using R = std::decay_t<decltype(f(this))>;
393 if constexpr (A == std::dynamic_extent) {
394 return projs(num_projs(), f);
395 } else {
396 std::array<R, A> array;
397 for (nat_t i = 0; i != A; ++i)
398 array[i] = f(proj(A, i));
399 return array;
400 }
401 }
402
403 template<class F>
404 auto tprojs(F f) const {
405 return projs(num_tprojs(), f);
406 }
407
408 template<class F>
409 auto projs(nat_t a, F f) const {
410 using R = std::decay_t<decltype(f(this))>;
411 return Vector<R>(a, [&](nat_t i) { return f(proj(a, i)); });
412 }
413 template<nat_t A = std::dynamic_extent>
414 auto projs() const {
415 return projs<A>([](const Def* def) { return def; });
416 }
417 auto tprojs() const {
418 return tprojs([](const Def* def) { return def; });
419 }
420 auto projs(nat_t a) const {
421 return projs(a, [](const Def* def) { return def; });
422 }
423 ///@}
424
425 /// @name var
426 /// @anchor var
427 /// Retrieve Var for *mutables*.
428 /// @see @ref proj
429 ///@{
431 const Def* var(); ///< Not necessarily a Var: E.g., if the return type is `[]`, this will yield `()`.
432 const Def* var_type(); ///< If `this` is a binder, compute the type of its Var%iable.
433
434 const Var* has_var() { return var_; } ///< Only returns not `nullptr`, if Var of this mutable has ever been created.
435 /// As above if `this` is a *mutable*.
436 const Var* has_var() const {
437 if (auto mut = isa_mut()) return mut->has_var();
438 return nullptr;
439 }
440
441 /// Is `this` a mutable that introduces a Var?
442 /// @returns `{nullptr, nullptr}` otherwise.
443 template<class D = Def>
444 std::pair<D*, const Var*> isa_binder() const {
445 if (auto mut = isa_mut<D>()) {
446 if (auto var = mut->has_var()) return {mut, var};
447 }
448 return {nullptr, nullptr};
449 }
450 ///@}
451
452 /// @name Free Vars and Muts
453 /// * local_muts() / local_vars() are cached and hash-consed.
454 /// * free_vars() are computed on demand and cached in mutables.
455 /// They will be transitively invalidated by following users(), if a mutable is mutated.
456 ///@{
457
458 /// Mutables reachable by following *immutable* deps(); `mut->local_muts()` is by definition the set `{ mut }`.
459 Muts local_muts() const;
460
461 /// Var%s reachable by following *immutable* deps().
462 /// @note `var->local_vars()` is by definition the set `{ var }`.
463 Vars local_vars() const;
464
465 /// Compute a global solution by transitively following *mutables* as well.
466 Vars free_vars() const;
467 Vars free_vars();
468 Muts users() { return muts_; } ///< Set of mutables where this mutable is locally referenced.
469 bool is_open() const; ///< Has free_vars()?
470 bool is_closed() const; ///< Has no free_vars()?
471
472 /// Transitively walks up free_vars() till the outermoust binder has been found.
473 /// @returns `nullptr`, if is_closed() and not a mutable.
474 Def* outermost_binder() const;
475 ///@}
476
477 /// @name external
478 ///@{
479 bool is_external() const noexcept { return external_; }
480 void externalize();
481 void internalize();
482 void transfer_external(Def* to);
483 bool is_annex() const noexcept { return annex_; }
484 ///@}
485
486 /// @name Casts
487 /// @see @ref cast_builtin
488 ///@{
489 bool is_mutable() const noexcept { return mut_; }
490
491 // clang-format off
492 template<class T = Def> const T* isa_imm() const { return isa_mut<T, true>(); }
493 template<class T = Def> const T* as_imm() const { return as_mut<T, true>(); }
494 // clang-format on
495
496 /// If `this` is *mutable*, it will cast `const`ness away and perform a `dynamic_cast` to @p T.
497 template<class T = Def, bool invert = false>
498 T* isa_mut() const {
499 if constexpr (std::is_same<T, Def>::value)
500 return mut_ ^ invert ? const_cast<Def*>(this) : nullptr;
501 else
502 return mut_ ^ invert ? const_cast<Def*>(this)->template isa<T>() : nullptr;
503 }
504
505 /// Asserts that `this` is a *mutable*, casts `const`ness away and performs a `static_cast` to @p T.
506 template<class T = Def, bool invert = false>
507 T* as_mut() const {
508 assert(mut_ ^ invert);
509 if constexpr (std::is_same<T, Def>::value)
510 return const_cast<Def*>(this);
511 else
512 return const_cast<Def*>(this)->template as<T>();
513 }
514 ///@}
515
516 /// @name Dbg Getters
517 ///@{
518 Dbg dbg() const { return dbg_; }
519 Loc loc() const { return dbg_.loc(); }
520 Sym sym() const { return dbg_.sym(); }
521 std::string unique_name() const; ///< name + "_" + Def::gid
522 ///@}
523
524 /// @name Dbg Setters
525 /// Every subclass `S` of Def has the same setters that return `S*`/`const S*` via the mixin Setters.
526 ///@{
527 // clang-format off
528 template<bool Ow = false> const Def* set(Loc l) const { if (Ow || !dbg_.loc()) dbg_.set(l); return this; }
529 template<bool Ow = false> Def* set(Loc l) { if (Ow || !dbg_.loc()) dbg_.set(l); return this; }
530 template<bool Ow = false> const Def* set(Sym s) const { if (Ow || !dbg_.sym()) dbg_.set(s); return this; }
531 template<bool Ow = false> Def* set(Sym s) { if (Ow || !dbg_.sym()) dbg_.set(s); return this; }
532 template<bool Ow = false> const Def* set( std::string s) const { set<Ow>(sym(std::move(s))); return this; }
533 template<bool Ow = false> Def* set( std::string s) { set<Ow>(sym(std::move(s))); return this; }
534 template<bool Ow = false> const Def* set(Loc l, Sym s ) const { set<Ow>(l); set<Ow>(s); return this; }
535 template<bool Ow = false> Def* set(Loc l, Sym s ) { set<Ow>(l); set<Ow>(s); return this; }
536 template<bool Ow = false> const Def* set(Loc l, std::string s) const { set<Ow>(l); set<Ow>(sym(std::move(s))); return this; }
537 template<bool Ow = false> Def* set(Loc l, std::string s) { set<Ow>(l); set<Ow>(sym(std::move(s))); return this; }
538 template<bool Ow = false> const Def* set(Dbg d) const { set<Ow>(d.loc(), d.sym()); return this; }
539 template<bool Ow = false> Def* set(Dbg d) { set<Ow>(d.loc(), d.sym()); return this; }
540 // clang-format on
541 ///@}
542
543 /// @name debug_prefix/suffix
544 /// Prepends/Appends a prefix/suffix to Def::name - but only in `Debug` build.
545 ///@{
546#ifndef NDEBUG
547 const Def* debug_prefix(std::string) const;
548 const Def* debug_suffix(std::string) const;
549#else
550 const Def* debug_prefix(std::string) const { return this; }
551 const Def* debug_suffix(std::string) const { return this; }
552#endif
553 ///@}
554
555 /// @name Rebuild
556 ///@{
557 Def* stub(World& w, const Def* type) { return stub_(w, type)->set(dbg()); }
558 Def* stub(const Def* type) { return stub(world(), type); }
559
560 /// Def::rebuild%s this Def while using @p new_op as substitute for its @p i'th Def::op
561 const Def* rebuild(World& w, const Def* type, Defs ops) const {
562 assert(isa_imm());
563 return rebuild_(w, type, ops)->set(dbg());
564 }
565 const Def* rebuild(const Def* type, Defs ops) const { return rebuild(world(), type, ops); }
566
567 /// Tries to make an immutable from a mutable.
568 /// This usually works if the mutable isn't recursive and its var isn't used.
569 virtual const Def* immutabilize() { return nullptr; }
570 bool is_immutabilizable();
571
572 const Def* refine(size_t i, const Def* new_op) const;
573
574 /// @see World::reduce
575 template<size_t N = std::dynamic_extent>
576 constexpr auto reduce(const Def* arg) const {
577 return reduce_(arg).span<N>();
578 }
579
580 /// First Def::op that needs to be dealt with during reduction; e.g. for a Pi we don't reduce the Pi::dom.
581 /// @see World::reduce
582 virtual constexpr size_t reduction_offset() const noexcept { return size_t(-1); }
583 ///@}
584
585 /// @name Type Checking
586 ///@{
587
588 /// Checks whether the `i`th operand can be set to `def`.
589 /// The method returns a possibly updated version of `def` (e.g. where Hole%s have been resolved).
590 /// This is the actual `def` that will be set as the `i`th operand.
591 virtual const Def* check([[maybe_unused]] size_t i, const Def* def) { return def; }
592
593 /// After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
594 /// The method returns a possibly updated version of its type (e.g. where Hole%s have been resolved).
595 /// If different from Def::type, it will update its Def::type to a Def::zonk%ed version of that.
596 virtual const Def* check() { return type(); }
597
598 /// Yields `true`, if Def::local_muts() contain a Hole that is set.
599 /// Rewriting (Def::zonk%ing) will resolve the Hole to its operand.
600 bool needs_zonk() const;
601
602 /// If Hole%s have been filled, reconstruct the program without them.
603 /// Only goes up to but excluding other mutables.
604 /// @see https://stackoverflow.com/questions/31889048/what-does-the-ghc-source-mean-by-zonk
605 const Def* zonk() const;
606
607 /// If *mutable*, zonk()%s all ops and tries to immutabilize it; otherwise just zonk.
608 const Def* zonk_mut() const;
609 ///@}
610
611 /// zonk%s all @p defs and returns a new DefVec.
612 static DefVec zonk(Defs defs);
613
614 /// @name dump
615 /// @note While this output uses Mim syntax, it does usually **not** produce programs that can be read back.
616 /// It uses an unscheduled visiting algorithm, and is only meant for debugging purposes.
617 ///@{
618 void dump() const;
619 void dump(int max) const;
620 void write(int max) const;
621 void write(int max, const char* file) const;
622 std::ostream& stream(std::ostream&, int max) const;
623 ///@}
624
625 /// @name Syntactic Comparison
626 ///
627 enum class Cmp {
628 L, ///< Less
629 G, ///< Greater
630 E, ///< Equal
631 U, ///< Unknown
632 };
633 /// @name Syntactic Comparison
634 ///@{
635 [[nodiscard]] static Cmp cmp(const Def* a, const Def* b);
636 [[nodiscard]] static bool less(const Def* a, const Def* b);
637 [[nodiscard]] static bool greater(const Def* a, const Def* b);
638 ///@}
639
640 /// @name dot
641 /// Streams dot to @p os while obeying maximum recursion depth of @p max.
642 /// if @p types is `true`, Def::type() dependencies will be followed as well.
643 ///@{
644 void dot(std::ostream& os, int max = std::numeric_limits<int>::max(), bool types = false) const;
645 /// Same as above but write to @p file or `std::cout` if @p file is `nullptr`.
646 void dot(const char* file = nullptr, int max = std::numeric_limits<int>::max(), bool types = false) const;
647 void dot(const std::string& file, int max = std::numeric_limits<int>::max(), bool types = false) const {
648 return dot(file.c_str(), max, types);
649 }
650 ///@}
651
652protected:
653 /// @name Wrappers for World::sym
654 /// These are here to have Def::set%ters inline without including `mim/world.h`.
655 ///@{
656 Sym sym(const char*) const;
657 Sym sym(std::string_view) const;
658 Sym sym(std::string) const;
659 ///@}
660
661private:
662 Defs reduce_(const Def* arg) const;
663 virtual Def* stub_(World&, const Def*) { fe::unreachable(); }
664 virtual const Def* rebuild_(World& w, const Def* type, Defs ops) const = 0;
665
666 template<bool init>
667 Vars free_vars(bool&, uint32_t);
668 void invalidate();
669 const Def** ops_ptr() const {
670 return reinterpret_cast<const Def**>(reinterpret_cast<char*>(const_cast<Def*>(this + 1)));
671 }
672 bool equal(const Def* other) const;
673
674 template<Cmp>
675 [[nodiscard]] static bool cmp_(const Def* a, const Def* b);
676
677protected:
678 mutable Dbg dbg_;
679 union {
680 NormalizeFn normalizer_; ///< Axm only: Axm%s use this member to store their normalizer.
681 const Axm* axm_; ///< App only: Curried App%s of Axm%s use this member to propagate the Axm.
682 const Var* var_; ///< Mutable only: Var of a mutable.
683 mutable World* world_;
684 };
688
689private:
690 Node node_; // 8
691 bool mut_ : 1;
692 bool external_ : 1;
693 mutable bool annex_ : 1;
694 unsigned dep_ : 5;
695 u32 mark_ = 0;
696#ifndef NDEBUG
697 size_t curr_op_ = 0;
698#endif
699 u32 gid_;
700 u32 num_ops_;
701 size_t hash_;
702 Vars vars_; // Mutable: local vars; Immutable: free vars.
703 Muts muts_; // Immutable: local_muts; Mutable: users;
704 mutable u32 tid_ = 0;
705 mutable const Def* type_;
706
707 template<class D, size_t N>
708 friend class Sets;
709 friend class World;
710 friend void swap(World&, World&) noexcept;
711 friend std::ostream& operator<<(std::ostream&, const Def*);
712};
713
714/// A variable introduced by a binder (mutable).
715/// @note Var will keep its type_ field as `nullptr`.
716/// Instead, Def::type() and Var::type() will compute the type via Def::var_type().
717/// The reason is that the type could need a Def::zonk().
718/// But we don't want to have several Var%s that belong to the same binder.
719class Var : public Def, public Setters<Var> {
720private:
721 Var(Def* mut)
722 : Def(Node, nullptr, Defs{mut}, 0) {}
723
724public:
725 using Setters<Var>::set;
726
727 const Def* type() const { return mut()->var_type(); }
728
729 /// @name ops
730 ///@{
731 Def* mut() const { return op(0)->as_mut(); }
732 ///@}
733
734 static constexpr auto Node = mim::Node::Var;
735 static constexpr size_t Num_Ops = 1;
736
737private:
738 const Def* rebuild_(World&, const Def*, Defs) const final;
739
740 friend class World;
741};
742
743class Univ : public Def, public Setters<Univ> {
744public:
745 using Setters<Univ>::set;
746 static constexpr auto Node = mim::Node::Univ;
747 static constexpr size_t Num_Ops = 0;
748
749private:
750 Univ(World& world)
751 : Def(&world, Node, nullptr, Defs{}, 0) {}
752
753 const Def* rebuild_(World&, const Def*, Defs) const final;
754
755 friend class World;
756};
757
758class UMax : public Def, public Setters<UMax> {
759public:
760 using Setters<UMax>::set;
761 static constexpr auto Node = mim::Node::UMax;
762 static constexpr size_t Num_Ops = std::dynamic_extent;
763
764 enum Sort { Univ, Kind, Type, Term };
765
766private:
767 UMax(World&, Defs ops);
768
769 const Def* rebuild_(World&, const Def*, Defs) const final;
770
771 friend class World;
772};
773
774class UInc : public Def, public Setters<UInc> {
775private:
776 UInc(const Def* op, level_t offset)
777 : Def(Node, op->type()->as<Univ>(), {op}, offset) {}
778
779public:
780 using Setters<UInc>::set;
781
782 /// @name ops
783 ///@{
784 const Def* op() const { return Def::op(0); }
785 level_t offset() const { return flags(); }
786 ///@}
787
788 static constexpr auto Node = mim::Node::UInc;
789 static constexpr size_t Num_Ops = 1;
790
791private:
792 const Def* rebuild_(World&, const Def*, Defs) const final;
793
794 friend class World;
795};
796
797class Type : public Def, public Setters<Type> {
798private:
799 Type(const Def* level)
800 : Def(Node, nullptr, {level}, 0) {}
801
802public:
803 using Setters<Type>::set;
804
805 /// @name ops
806 ///@{
807 const Def* level() const { return op(0); }
808 ///@}
809
810 static constexpr auto Node = mim::Node::Type;
811 static constexpr size_t Num_Ops = 1;
812
813private:
814 const Def* rebuild_(World&, const Def*, Defs) const final;
815
816 friend class World;
817};
818
819class Lit : public Def, public Setters<Lit> {
820private:
821 Lit(const Def* type, flags_t val)
822 : Def(Node, type, Defs{}, val) {}
823
824public:
825 using Setters<Lit>::set;
826
827 /// @name Get actual Constant
828 ///@{
829 template<class T = flags_t>
830 T get() const {
831 static_assert(sizeof(T) <= 8);
832 return bitcast<T>(flags_);
833 }
834 ///@}
835
836 using Def::as;
837 using Def::isa;
838
839 /// @name Casts
840 ///@{
841 /// @see @ref cast_lit
842 template<class T = nat_t>
843 static std::optional<T> isa(const Def* def) {
844 if (!def) return {};
845 if (auto lit = def->isa<Lit>()) return lit->get<T>();
846 return {};
847 }
848 template<class T = nat_t>
849 static T as(const Def* def) {
850 return def->as<Lit>()->get<T>();
851 }
852 ///@}
853
854 static constexpr auto Node = mim::Node::Lit;
855 static constexpr size_t Num_Ops = 0;
856
857private:
858 const Def* rebuild_(World&, const Def*, Defs) const final;
859
860 friend class World;
861};
862
863class Nat : public Def, public Setters<Nat> {
864public:
865 using Setters<Nat>::set;
866 static constexpr auto Node = mim::Node::Nat;
867 static constexpr size_t Num_Ops = 0;
868
869private:
870 Nat(World& world);
871
872 const Def* rebuild_(World&, const Def*, Defs) const final;
873
874 friend class World;
875};
876
877/// A built-in constant of type `Nat -> *`.
878class Idx : public Def, public Setters<Idx> {
879private:
880 Idx(const Def* type)
881 : Def(Node, type, Defs{}, 0) {}
882
883public:
884 using Setters<Idx>::set;
885 using Def::as;
886 using Def::isa;
887
888 /// @name isa
889 ///@{
890
891 /// Checks if @p def is a `Idx s` and returns `s` or `nullptr` otherwise.
892 static const Def* isa(const Def* def);
893 static const Def* as(const Def* def) {
894 auto res = isa(def);
895 assert(res);
896 return res;
897 }
898 static std::optional<nat_t> isa_lit(const Def* def);
899 static nat_t as_lit(const Def* def) {
900 auto res = isa_lit(def);
901 assert(res.has_value());
902 return *res;
903 }
904 ///@}
905
906 /// @name Convert between Idx::isa and bitwidth and vice versa
907 ///@{
908 // clang-format off
909 static constexpr nat_t bitwidth2size(nat_t n) { assert(n != 0); return n == 64 ? 0 : (1_n << n); }
910 static constexpr nat_t size2bitwidth(nat_t n) { return n == 0 ? 64 : std::bit_width(n - 1_n); }
911 // clang-format on
912 static std::optional<nat_t> size2bitwidth(const Def* size);
913 ///@}
914
915 static constexpr auto Node = mim::Node::Idx;
916 static constexpr size_t Num_Ops = 0;
917
918private:
919 const Def* rebuild_(World&, const Def*, Defs) const final;
920
921 friend class World;
922};
923
924class Proxy : public Def, public Setters<Proxy> {
925private:
926 Proxy(const Def* type, u32 pass, u32 tag, Defs ops)
927 : Def(Node, type, ops, (u64(pass) << 32_u64) | u64(tag)) {}
928
929public:
930 using Setters<Proxy>::set;
931
932 /// @name Getters
933 ///@{
934 u32 pass() const { return u32(flags() >> 32_u64); } ///< IPass::index within PassMan.
935 u32 tag() const { return u32(flags()); }
936 ///@}
937
938 static constexpr auto Node = mim::Node::Proxy;
939 static constexpr size_t Num_Ops = std::dynamic_extent;
940
941private:
942 const Def* rebuild_(World&, const Def*, Defs) const final;
943
944 friend class World;
945};
946
947/// @deprecated A global variable in the data segment.
948/// A Global may be mutable or immutable.
949/// @deprecated Will be removed.
950class Global : public Def, public Setters<Global> {
951private:
952 Global(const Def* type, bool is_mutable)
953 : Def(Node, type, 1, is_mutable) {}
954
955public:
956 using Setters<Global>::set;
957
958 /// @name ops
959 ///@{
960 const Def* init() const { return op(0); }
961 void set(const Def* init) { Def::set(0, init); }
962 ///@}
963
964 /// @name type
965 ///@{
966 const App* type() const;
967 const Def* alloced_type() const;
968 ///@}
969
970 /// @name Getters
971 ///@{
972 bool is_mutable() const { return flags(); }
973 ///@}
974
975 /// @name Rebuild
976 ///@{
977 Global* stub(const Def* type) { return stub_(world(), type)->set(dbg()); }
978 ///@}
979
980 static constexpr auto Node = mim::Node::Global;
981 static constexpr size_t Num_Ops = 1;
982
983private:
984 const Def* rebuild_(World&, const Def*, Defs) const final;
985 Global* stub_(World&, const Def*) final;
986
987 friend class World;
988};
989
990} // namespace mim
Definition axm.h:9
Base class for all Defs.
Definition def.h:252
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:298
size_t num_deps() const noexcept
Definition def.h:345
const Def * zonk_mut() const
If mutable, zonk()s all ops and tries to immutabilize it; otherwise just zonk.
Definition check.cpp:23
const Def * set(Dbg d) const
Definition def.h:538
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
constexpr Node node() const noexcept
Definition def.h:275
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:266
virtual const Def * check(size_t i, const Def *def)
Checks whether the ith operand can be set to def.
Definition def.h:591
void dot(std::ostream &os, int max=std::numeric_limits< int >::max(), bool types=false) const
Definition dot.cpp:155
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition def.h:507
const Var * has_var() const
As above if this is a mutable.
Definition def.h:436
void dump() const
Definition dump.cpp:514
Defs deps() const noexcept
Definition def.cpp:475
auto projs() const
Definition def.h:414
u8 trip_
Definition def.h:687
bool has_dep(Dep d) const
Definition def.h:359
bool is_elim() const noexcept
Definition def.h:286
nat_t num_tprojs() const
As above but yields 1, if Flags::scalarize_threshold is exceeded.
Definition def.cpp:588
virtual ~Def()=default
const Def * zonk() const
If Holes have been filled, reconstruct the program without them.
Definition check.cpp:21
bool has_dep(unsigned u) const
Definition def.h:360
World & world() const noexcept
Definition def.cpp:444
virtual const Def * check()
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
Definition def.h:596
const Def * refine(size_t i, const Def *new_op) const
Definition def.cpp:232
Def * set_type(const Def *)
Update type.
Definition def.cpp:283
std::string_view node_name() const
Definition def.cpp:465
auto projs(nat_t a, F f) const
Definition def.h:409
bool is_intro() const noexcept
Definition def.h:285
constexpr auto ops() const noexcept
Definition def.h:306
Vars local_vars() const
Vars reachable by following immutable deps().
Definition def.cpp:348
constexpr flags_t flags() const noexcept
Definition def.h:270
Dbg dbg_
Definition def.h:678
virtual Def * stub_(World &, const Def *)
Definition def.h:663
const Def * set(std::string s) const
Definition def.h:532
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:498
constexpr u32 mark() const noexcept
Used internally by free_vars().
Definition def.h:273
constexpr u32 tid() const noexcept
Trie id - only used in Trie.
Definition def.h:272
auto projs(nat_t a) const
Definition def.h:420
u8 curry_
Definition def.h:686
auto tprojs() const
Definition def.h:417
void externalize()
Definition def.cpp:575
friend std::ostream & operator<<(std::ostream &, const Def *)
This will stream def as an operand.
Definition dump.cpp:485
bool is_term() const
Definition def.cpp:492
const Def * debug_prefix(std::string) const
Definition def.cpp:504
const Def * op(size_t i) const noexcept
Definition def.h:309
bool is_immutabilizable()
Definition def.cpp:176
virtual const Def * rebuild_(World &w, const Def *type, Defs ops) const =0
std::pair< D *, const Var * > isa_binder() const
Is this a mutable that introduces a Var?
Definition def.h:444
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:430
void transfer_external(Def *to)
Definition def.cpp:578
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 * proj(nat_t i) const
As above but takes Def::num_projs as arity.
Definition def.h:385
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:391
bool is_open() const
Has free_vars()?
Definition def.cpp:426
constexpr size_t hash() const noexcept
Definition def.h:274
friend class World
Definition def.h:709
virtual const Def * immutabilize()
Tries to make an immutable from a mutable.
Definition def.h:569
bool is_form() const noexcept
Definition def.h:284
Def * set(Dbg d)
Definition def.h:539
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
Definition def.cpp:332
Def * set(Sym s)
Definition def.h:531
const Def * debug_suffix(std::string) const
Definition def.cpp:505
const Def * set(Sym s) const
Definition def.h:530
Def * set(Loc l)
Definition def.h:529
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:452
Def * outermost_binder() const
Transitively walks up free_vars() till the outermoust binder has been found.
Definition def.cpp:431
const Def * dep(size_t i) const noexcept
Definition def.h:344
bool is_mutable() const noexcept
Definition def.h:489
bool is_external() const noexcept
Definition def.h:479
const Def * rebuild(World &w, const Def *type, Defs ops) const
Def::rebuilds this Def while using new_op as substitute for its i'th Def::op.
Definition def.h:561
void internalize()
Definition def.cpp:576
const Def * set(Loc l, Sym s) const
Definition def.h:534
static bool less(const Def *a, const Def *b)
Definition def.cpp:554
Def * set(std::string s)
Definition def.h:533
bool is_meta() const noexcept
Definition def.h:287
static bool greater(const Def *a, const Def *b)
Definition def.cpp:555
bool has_dep() const
Definition def.h:358
const Def * set(Loc l) const
Definition def.h:528
Loc loc() const
Definition def.h:519
void write(int max) const
Definition dump.cpp:522
auto tprojs(F f) const
Definition def.h:404
Def * set(Loc l, std::string s)
Definition def.h:537
friend class Sets
Definition def.h:708
const T * as_imm() const
Definition def.h:493
Def * stub(const Def *type)
Definition def.h:558
static Cmp cmp(const Def *a, const Def *b)
Definition def.cpp:512
virtual constexpr size_t reduction_offset() const noexcept
First Def::op that needs to be dealt with during reduction; e.g.
Definition def.h:582
Sym sym() const
Definition def.h:520
nat_t num_projs() const
Yields Def::arity(), if it is a Lit, or 1 otherwise.
Definition def.cpp:586
std::ostream & stream(std::ostream &, int max) const
Definition dump.cpp:494
flags_t flags_
Definition def.h:685
friend void swap(World &, World &) noexcept
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:271
virtual const Def * arity() const
Definition def.cpp:558
Def * unset()
Unsets all Def::ops; works even, if not set at all or only partially set.
Definition def.cpp:289
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:584
constexpr auto reduce(const Def *arg) const
Definition def.h:576
const Def * set(Loc l, std::string s) const
Definition def.h:536
const T * isa_imm() const
Definition def.h:492
bool needs_zonk() const
Yields true, if Def::local_muts() contain a Hole that is set.
Definition check.cpp:12
Muts users()
Set of mutables where this mutable is locally referenced.
Definition def.h:468
bool is_closed() const
Has no free_vars()?
Definition def.cpp:418
void dot(const std::string &file, int max=std::numeric_limits< int >::max(), bool types=false) const
Definition def.h:647
const Def * rebuild(const Def *type, Defs ops) const
Definition def.h:565
Vars free_vars() const
Compute a global solution by transitively following mutables as well.
Definition def.cpp:337
Def * set(Loc l, Sym s)
Definition def.h:535
Dbg dbg() const
Definition def.h:518
Def * stub(World &w, const Def *type)
Definition def.h:557
u32 judge() const noexcept
Definition def.cpp:482
const Def * tproj(nat_t i) const
As above but takes Def::num_tprojs.
Definition def.h:386
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Definition def.h:434
bool is_annex() const noexcept
Definition def.h:483
constexpr size_t num_ops() const noexcept
Definition def.h:310
@ E
Equal.
Definition def.h:630
@ U
Unknown.
Definition def.h:631
@ L
Less.
Definition def.h:628
@ G
Greater.
Definition def.h:629
const Def * init() const
Definition def.h:960
void set(const Def *init)
Definition def.h:961
Global * stub(const Def *type)
Definition def.h:977
const Def * alloced_type() const
Definition def.cpp:641
friend class World
Definition def.h:987
bool is_mutable() const
Definition def.h:972
const App * type() const
Definition def.cpp:640
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:110
static constexpr size_t Num_Ops
Definition def.h:981
Global * stub_(World &, const Def *) final
Definition def.cpp:151
static constexpr auto Node
Definition def.h:980
This node is a hole in the IR that is inferred by its context later on.
Definition check.h:14
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:111
static constexpr auto Node
Definition def.h:915
static nat_t as_lit(const Def *def)
Definition def.h:899
static constexpr nat_t size2bitwidth(nat_t n)
Definition def.h:910
static constexpr nat_t bitwidth2size(nat_t n)
Definition def.h:909
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
Definition def.cpp:616
friend class World
Definition def.h:921
static std::optional< nat_t > isa_lit(const Def *def)
Definition def.cpp:624
static constexpr size_t Num_Ops
Definition def.h:916
static const Def * as(const Def *def)
Definition def.h:893
static constexpr auto Node
Definition def.h:854
static std::optional< T > isa(const Def *def)
Definition def.h:843
friend class World
Definition def.h:860
T get() const
Definition def.h:830
static T as(const Def *def)
Definition def.h:849
static constexpr size_t Num_Ops
Definition def.h:855
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:120
friend class World
Definition def.h:874
static constexpr auto Node
Definition def.h:866
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:112
static constexpr size_t Num_Ops
Definition def.h:867
static constexpr size_t Num_Ops
Definition def.h:939
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:124
friend class World
Definition def.h:944
static constexpr auto Node
Definition def.h:938
u32 pass() const
IPass::index within PassMan.
Definition def.h:934
u32 tag() const
Definition def.h:935
CRTP-based mixin to declare setters for Def::loc & Def::name using a covariant return type.
Definition def.h:196
P * set(Loc l, Sym s)
Definition def.h:210
P * set(std::string s)
Definition def.h:208
const P * set(Sym s) const
Definition def.h:205
const P * set(Loc l, Sym s) const
Definition def.h:209
const P * set(Loc l) const
Definition def.h:203
P * set(Loc l)
Definition def.h:204
const P * set(Dbg d) const
Definition def.h:213
const P * set(Loc l, std::string s) const
Definition def.h:211
P * set(Sym s)
Definition def.h:206
P * set(Dbg d)
Definition def.h:214
P * set(Loc l, std::string s)
Definition def.h:212
const P * set(std::string s) const
Definition def.h:207
friend class World
Definition def.h:816
static constexpr size_t Num_Ops
Definition def.h:811
static constexpr auto Node
Definition def.h:810
const Def * level() const
Definition def.h:807
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:131
const Def * op() const
Definition def.h:784
static constexpr auto Node
Definition def.h:788
friend class World
Definition def.h:794
level_t offset() const
Definition def.h:785
static constexpr size_t Num_Ops
Definition def.h:789
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:132
@ Type
Definition def.h:764
@ Univ
Definition def.h:764
@ Term
Definition def.h:764
@ Kind
Definition def.h:764
static constexpr auto Node
Definition def.h:761
friend class World
Definition def.h:771
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:133
static constexpr size_t Num_Ops
Definition def.h:762
static constexpr size_t Num_Ops
Definition def.h:747
static constexpr auto Node
Definition def.h:746
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:113
friend class World
Definition def.h:755
A variable introduced by a binder (mutable).
Definition def.h:719
const Def * type() const
Definition def.h:727
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:135
static constexpr auto Node
Definition def.h:734
friend class World
Definition def.h:740
static constexpr size_t Num_Ops
Definition def.h:735
Def * mut() const
Definition def.h:731
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
Definition vector.h:18
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:34
#define MIM_NODE(X)
Definition def.h:19
#define MIM_PROJ(NAME, CONST)
Use as mixin to wrap all kind of Def::proj and Def::projs variants.
Definition def.h:165
Definition ast.h:14
View< const Def * > Defs
Definition def.h:77
u64 nat_t
Definition types.h:44
DefMap< const Def * > Def2Def
Definition def.h:76
Vector< const Def * > DefVec
Definition def.h:78
D bitcast(const S &src)
A bitcast from src of type S to D.
Definition util.h:23
Dep
Tracks a dependency to certain Defs transitively through the Def::deps() up to but excliding mutables...
Definition def.h:124
@ None
Definition def.h:125
u64 flags_t
Definition types.h:46
u8 node_t
Definition types.h:45
Span< const T, N > View
Definition span.h:98
absl::flat_hash_map< K, V, GIDHash< K > > GIDMap
Definition util.h:195
GIDSet< const Var * > VarSet
Definition def.h:96
GIDMap< const Var *, To > VarMap
Definition def.h:95
Sets< Def >::Set Muts
Definition def.h:88
u64 level_t
Definition types.h:43
GIDMap< const Def *, To > DefMap
Definition def.h:74
GIDSet< Def * > MutSet
Definition def.h:86
MutMap< Def * > Mut2Mut
Definition def.h:87
GIDSet< const Def * > DefSet
Definition def.h:75
static constexpr size_t Num_Nodes
Definition def.h:120
const Def *(*)(const Def *, const Def *, const Def *) NormalizeFn
Definition def.h:101
Sets< const Var >::Set Vars
Definition def.h:98
uint32_t u32
Definition types.h:35
Mut
Judgement.
Definition def.h:144
@ Imm
Node may be immmutable.
Definition def.h:147
uint64_t u64
Definition types.h:35
Judge
Judgement.
Definition def.h:133
@ Intro
Term Introduction like λ(x: Nat): Nat = x.
Definition def.h:136
@ Meta
Meta rules for Universe and Type levels.
Definition def.h:138
@ Form
Type Formation like T -> T.
Definition def.h:135
@ Elim
Term Elimination like f a.
Definition def.h:137
absl::flat_hash_set< K, GIDHash< K > > GIDSet
Definition util.h:196
uint8_t u8
Definition types.h:35
Node
Definition def.h:113
@ Nat
Definition def.h:115
@ Univ
Definition def.h:115
@ Idx
Definition def.h:115
@ UInc
Definition def.h:115
@ Global
Definition def.h:115
@ Var
Definition def.h:115
@ Axm
Definition def.h:115
@ Type
Definition def.h:115
@ App
Definition def.h:115
@ Lit
Definition def.h:115
@ Proxy
Definition def.h:115
@ UMax
Definition def.h:115
Vector(I, I, A=A()) -> Vector< typename std::iterator_traits< I >::value_type, Default_Inlined_Size< typename std::iterator_traits< I >::value_type >, A >
GIDMap< Def *, To > MutMap
Definition def.h:85
VarMap< const Var * > Var2Var
Definition def.h:97
Definition span.h:122
#define CODE(name,...)
Definition tok.h:38