MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
world.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <string_view>
6#include <type_traits>
7
8#include <absl/container/btree_map.h>
9#include <fe/arena.h>
10
11#include "mim/axm.h"
12#include "mim/rewrite.h"
13
14#include "mim/util/dbg.h"
15#include "mim/util/log.h"
16
17namespace mim {
18
19class Driver;
20struct Flags;
21
22/// The World represents the whole program and manages creation of MimIR nodes (Def%s).
23/// Def%s are hashed into an internal HashSet.
24/// The World's factory methods just calculate a hash and lookup the Def, if it is already present, or create a new one
25/// otherwise. This corresponds to value numbering.
26///
27/// You can create several worlds.
28/// All worlds are completely independent from each other.
29///
30/// Note that types are also just Def%s and will be hashed as well.
31class World {
32public:
33 /// @name State
34 ///@{
35 struct State {
36 State() = default;
37
38 /// [Plain Old Data](https://en.cppreference.com/w/cpp/named_req/PODType)
39 struct POD {
42 Loc loc;
43 Sym name;
44 mutable bool frozen = false;
45 } pod;
46
47#ifdef MIM_ENABLE_CHECKS
48 absl::flat_hash_set<uint32_t> breakpoints;
49 absl::flat_hash_set<uint32_t> watchpoints;
50#endif
51 friend void swap(State& s1, State& s2) noexcept {
52 using std::swap;
53 assert((!s1.pod.loc || !s2.pod.loc) && "Why is get_loc() still set?");
54 swap(s1.pod, s2.pod);
55#ifdef MIM_ENABLE_CHECKS
56 swap(s1.breakpoints, s2.breakpoints);
57 swap(s1.watchpoints, s2.watchpoints);
58#endif
59 }
60 };
61
62 /// @name Construction & Destruction
63 ///@{
64 World& operator=(World) = delete;
65
66 explicit World(Driver*);
67 World(Driver*, const State&);
68 World(World&& other) noexcept
69 : World(&other.driver(), other.state()) {
70 swap(*this, other);
71 }
72 ~World();
73
74 /// Inherits the State into the new World.
75 /// World::curr_gid will be offset to not collide with the original World.
76 std::unique_ptr<World> inherit() {
77 auto s = state();
78 s.pod.curr_gid += move_.defs.size();
79 return std::make_unique<World>(&driver(), s);
80 }
81 ///@}
82
83 /// @name Getters/Setters
84 ///@{
85 const State& state() const { return state_; }
86 const Driver& driver() const { return *driver_; }
87 Driver& driver() { return *driver_; }
88 Zonker& zonker() { return zonker_; }
89
90 Sym name() const { return state_.pod.name; }
91 void set(Sym name) { state_.pod.name = name; }
92 void set(std::string_view name) { state_.pod.name = sym(name); }
93
94 /// Manage global identifier - a unique number for each Def.
95 u32 curr_gid() const { return state_.pod.curr_gid; }
96 u32 next_gid() { return ++state_.pod.curr_gid; }
97 u32 next_run() { return ++data_.curr_run; }
98
99 /// Retrieve compile Flags.
100 Flags& flags();
101 ///@}
102
103 /// @name Loc
104 ///@{
105 struct ScopedLoc {
106 ScopedLoc(World& world, Loc old_loc)
107 : world_(world)
108 , old_loc_(old_loc) {}
109 ~ScopedLoc() { world_.set_loc(old_loc_); }
110
111 private:
112 World& world_;
113 Loc old_loc_;
114 };
115
116 Loc get_loc() const { return state_.pod.loc; }
117 void set_loc(Loc loc = {}) { state_.pod.loc = loc; }
118 ScopedLoc push(Loc loc) {
119 auto sl = ScopedLoc(*this, get_loc());
120 set_loc(loc);
121 return sl;
122 }
123 ///@}
124
125 /// @name Sym
126 ///@{
127 Sym sym(std::string_view);
128 Sym sym(const char*);
129 Sym sym(const std::string&);
130 /// Appends a @p suffix or an increasing number if the suffix already exists.
131 Sym append_suffix(Sym name, std::string suffix);
132 ///@}
133
134 /// @name Freeze
135 /// In frozen state the World does not create any nodes.
136 ///@{
137 bool is_frozen() const { return state_.pod.frozen; }
138
139 /// Yields old frozen state.
140 bool freeze(bool on = true) const {
141 bool old = state_.pod.frozen;
142 state_.pod.frozen = on;
143 return old;
144 }
145
146 /// Use to World::freeze and automatically unfreeze at the end of scope.
147 struct Freezer {
149 : world(world)
150 , old(world.freeze(true)) {}
151 ~Freezer() { world.freeze(old); }
152
153 const World& world;
154 bool old;
155 };
156 ///@}
157
158 /// @name Debugging Features
159 ///@{
160#ifdef MIM_ENABLE_CHECKS
161 const auto& breakpoints() { return state_.breakpoints; }
162 const auto& watchpoints() { return state_.watchpoints; }
163
164 const Def* gid2def(u32 gid); ///< Lookup Def by @p gid.
165 void breakpoint(u32 gid); ///< Trigger breakpoint in your debugger when creating a Def with this @p gid.
166 void watchpoint(u32 gid); ///< Trigger breakpoint in your debugger when Def::set%ting a Def with this @p gid.
167
168 World& verify(); ///< Verifies that all externals() and annexes() are Def::is_closed(), if `MIM_ENABLE_CHECKS`.
169#else
170 World& verify() { return *this; }
171#endif
172 ///@}
173
174 /// @name Annexes
175 ///@{
176 const auto& flags2annex() const { return move_.flags2annex; }
177 auto annexes() const { return move_.flags2annex | std::views::values; }
178
179 /// Lookup annex by Axm::id.
180 template<class Id>
181 const Def* annex(Id id) {
182 auto flags = static_cast<flags_t>(id);
183 if (auto i = move_.flags2annex.find(flags); i != move_.flags2annex.end()) return i->second;
184 error("Axm with ID '{x}' not found; demangled plugin name is '{}'", flags, Annex::demangle(driver(), flags));
185 }
186
187 /// Get Axm from a plugin.
188 /// Can be used to get an Axm without sub-tags.
189 /// E.g. use `w.annex<mem::M>();` to get the `%mem.M` Axm.
190 template<annex_without_subs id>
191 const Def* annex() {
192 return annex(Annex::base<id>());
193 }
194
195 const Def* register_annex(flags_t f, const Def*);
196 const Def* register_annex(plugin_t p, tag_t t, sub_t s, const Def* def) {
197 return register_annex(p | (flags_t(t) << 8_u64) | flags_t(s), def);
198 }
199 ///@}
200
201 /// @name Externals
202 ///@{
203 class Externals {
204 public:
205 ///@name Get syms/muts
206 ///@{
207 const auto& sym2mut() const { return sym2mut_; }
208 auto syms() const { return sym2mut_ | std::views::keys; }
209 auto muts() const { return sym2mut_ | std::views::values; }
210 /// Returns a copy of @p muts() in a Vector; this allows you modify the Externals while iterating.
211 /// @note The iteration will see all old externals, of course.
212 Vector<Def*> mutate() const { return {muts().begin(), muts().end()}; }
213 Def* operator[](Sym name) const { return mim::lookup(sym2mut_, name); } ///< Lookup by @p name.
214 ///@}
215
216 ///@name externalize/internalize
217 ///@{
218 void externalize(Def*);
219 void internalize(Def*);
220 ///@}
221
222 /// @name Iterators
223 ///@{
224 auto begin() const { return sym2mut_.cbegin(); }
225 auto end() const { return sym2mut_.cend(); }
226 ///@}
227
228 friend void swap(Externals& ex1, Externals& ex2) noexcept {
229 using std::swap;
230 swap(ex1.sym2mut_, ex2.sym2mut_);
231 }
232
233 private:
234 fe::SymMap<Def*> sym2mut_;
235 };
236
237 const Externals& externals() const { return move_.externals; }
238 Externals& externals() { return move_.externals; }
239 ///@}
240
241 /// @name Univ, Type, Var, Proxy, Hole
242 ///@{
243 const Univ* univ() { return data_.univ; }
244 const Def* uinc(const Def* op, level_t offset = 1);
245 template<int sort = UMax::Univ>
246 const Def* umax(Defs);
247 const Type* type(const Def* level);
248 const Type* type_infer_univ() { return type(mut_hole_univ()); }
249 template<level_t level = 0>
250 const Type* type() {
251 if constexpr (level == 0)
252 return data_.type_0;
253 else if constexpr (level == 1)
254 return data_.type_1;
255 else
256 return type(lit_univ(level));
257 }
258 const Def* var(Def* mut);
259 const Proxy* proxy(const Def* type, Defs ops, u32 index, u32 tag) { return unify<Proxy>(type, index, tag, ops); }
260
261 Hole* mut_hole(const Def* type) { return insert<Hole>(type); }
262 Hole* mut_hole_univ() { return mut_hole(univ()); }
264
265 /// Either a value `?:?:Type ?` or a type `?:Type ?:Type ?`.
267 auto t = type_infer_univ();
268 auto res = mut_hole(mut_hole(t));
269 assert(this == &res->world());
270 return res;
271 }
272 ///@}
273
274 /// @name Axm
275 ///@{
276 const Axm* axm(NormalizeFn n, u8 curry, u8 trip, const Def* type, plugin_t p, tag_t t, sub_t s) {
277 return unify<Axm>(n, curry, trip, type, p, t, s);
278 }
279 const Axm* axm(const Def* type, plugin_t p, tag_t t, sub_t s) { return axm(nullptr, 0, 0, type, p, t, s); }
280
281 /// Builds a fresh Axm with descending Axm::sub.
282 /// This is useful during testing to come up with some entity of a specific type.
283 /// It uses the plugin Axm::Global_Plugin and starts with `0` for Axm::sub and counts up from there.
284 /// The Axm::tag is set to `0` and the Axm::normalizer to `nullptr`.
285 const Axm* axm(NormalizeFn n, u8 curry, u8 trip, const Def* type) {
286 return axm(n, curry, trip, type, Annex::Global_Plugin, 0, state_.pod.curr_sub++);
287 }
288 const Axm* axm(const Def* type) { return axm(nullptr, 0, 0, type); } ///< See above.
289 ///@}
290
291 /// @name Pi
292 ///@{
293 // clang-format off
294 const Pi* pi(const Def* dom, const Def* codom, bool implicit = false) { return unify<Pi>(Pi::infer(dom, codom), dom, codom, implicit); }
295 const Pi* pi(Defs dom, const Def* codom, bool implicit = false) { return pi(sigma(dom), codom, implicit); }
296 const Pi* pi(const Def* dom, Defs codom, bool implicit = false) { return pi(dom, sigma(codom), implicit); }
297 const Pi* pi(Defs dom, Defs codom, bool implicit = false) { return pi(sigma(dom), sigma(codom), implicit); }
298 Pi* mut_pi(const Def* type, bool implicit = false) { return insert<Pi>(type, implicit); }
299 // clang-format on
300 ///@}
301
302 /// @name Cn
303 /// Pi with codom mim::Bot%tom
304 ///@{
305 // clang-format off
306 const Pi* cn( ) { return cn(sigma( ), false); }
307 const Pi* cn(const Def* dom, bool implicit = false) { return pi( dom , type_bot(), implicit); }
308 const Pi* cn(Defs dom, bool implicit = false) { return cn(sigma(dom), implicit); }
309 const Pi* fn(const Def* dom, const Def* codom, bool implicit = false) { return cn({ dom , cn(codom)}, implicit); }
310 const Pi* fn(Defs dom, const Def* codom, bool implicit = false) { return fn(sigma(dom), codom, implicit); }
311 const Pi* fn(const Def* dom, Defs codom, bool implicit = false) { return fn( dom , sigma(codom), implicit); }
312 const Pi* fn(Defs dom, Defs codom, bool implicit = false) { return fn(sigma(dom), sigma(codom), implicit); }
313 // clang-format on
314 ///@}
315
316 /// @name Lam
317 ///@{
319 if (auto b = std::get_if<bool>(&filter)) return lit_bool(*b);
320 return std::get<const Def*>(filter);
321 }
322 const Lam* lam(const Pi* pi, Lam::Filter f, const Def* body) { return unify<Lam>(pi, filter(f), body); }
323 Lam* mut_lam(const Pi* pi) { return insert<Lam>(pi); }
324 // clang-format off
325 const Lam* con(const Def* dom, Lam::Filter f, const Def* body) { return unify<Lam>(cn(dom ), filter(f), body); }
326 const Lam* con(Defs dom, Lam::Filter f, const Def* body) { return unify<Lam>(cn(dom ), filter(f), body); }
327 const Lam* lam(const Def* dom, const Def* codom, Lam::Filter f, const Def* body) { return unify<Lam>(pi(dom, codom), filter(f), body); }
328 const Lam* lam(Defs dom, const Def* codom, Lam::Filter f, const Def* body) { return unify<Lam>(pi(dom, codom), filter(f), body); }
329 const Lam* lam(const Def* dom, Defs codom, Lam::Filter f, const Def* body) { return unify<Lam>(pi(dom, codom), filter(f), body); }
330 const Lam* lam(Defs dom, Defs codom, Lam::Filter f, const Def* body) { return unify<Lam>(pi(dom, codom), filter(f), body); }
331 const Lam* fun(const Def* dom, const Def* codom, Lam::Filter f, const Def* body) { return unify<Lam>(fn(dom, codom), filter(f), body); }
332 const Lam* fun(Defs dom, const Def* codom, Lam::Filter f, const Def* body) { return unify<Lam>(fn(dom, codom), filter(f), body); }
333 const Lam* fun(const Def* dom, Defs codom, Lam::Filter f, const Def* body) { return unify<Lam>(fn(dom, codom), filter(f), body); }
334 const Lam* fun(Defs dom, Defs codom, Lam::Filter f, const Def* body) { return unify<Lam>(fn(dom, codom), filter(f), body); }
335 Lam* mut_con(const Def* dom ) { return insert<Lam>(cn(dom )); }
336 Lam* mut_con(Defs dom ) { return insert<Lam>(cn(dom )); }
337 Lam* mut_lam(const Def* dom, const Def* codom) { return insert<Lam>(pi(dom, codom)); }
338 Lam* mut_lam(Defs dom, const Def* codom) { return insert<Lam>(pi(dom, codom)); }
339 Lam* mut_lam(const Def* dom, Defs codom) { return insert<Lam>(pi(dom, codom)); }
340 Lam* mut_lam(Defs dom, Defs codom) { return insert<Lam>(pi(dom, codom)); }
341 Lam* mut_fun(const Def* dom, const Def* codom) { return insert<Lam>(fn(dom, codom)); }
342 Lam* mut_fun(Defs dom, const Def* codom) { return insert<Lam>(fn(dom, codom)); }
343 Lam* mut_fun(const Def* dom, Defs codom) { return insert<Lam>(fn(dom, codom)); }
344 Lam* mut_fun(Defs dom, Defs codom) { return insert<Lam>(fn(dom, codom)); }
345 // clang-format on
346 ///@}
347
348 /// @name Rewrite Rules
349 ///@{
350 const Reform* reform(const Def* meta_type) { return unify<Reform>(Reform::infer(meta_type), meta_type); }
351 Rule* mut_rule(const Reform* type) { return insert<Rule>(type); }
352 const Rule* rule(const Reform* type, const Def* lhs, const Def* rhs, const Def* guard) {
353 return mut_rule(type)->set(lhs, rhs, guard);
354 }
355 const Rule* rule(const Def* meta_type, const Def* lhs, const Def* rhs, const Def* guard) {
356 return rule(reform(meta_type), lhs, rhs, guard);
357 }
358 ///@}
359
360 /// @name App
361 ///@{
362 template<bool Normalize = true>
363 const Def* app(const Def* callee, const Def* arg);
364 template<bool Normalize = true>
365 const Def* app(const Def* callee, Defs args) {
366 return app<Normalize>(callee, tuple(args));
367 }
368 const Def* raw_app(const Axm* axm, u8 curry, u8 trip, const Def* type, const Def* callee, const Def* arg);
369 const Def* raw_app(const Def* type, const Def* callee, const Def* arg);
370 const Def* raw_app(const Def* type, const Def* callee, Defs args) { return raw_app(type, callee, tuple(args)); }
371 ///@}
372
373 /// @name Sigma
374 ///@{
375 Sigma* mut_sigma(const Def* type, size_t size) { return insert<Sigma>(type, size); }
376 /// A *mutable* Sigma of type @p level.
377 template<level_t level = 0>
378 Sigma* mut_sigma(size_t size) {
379 return mut_sigma(type<level>(), size);
380 }
381 const Def* sigma(Defs ops);
382 const Sigma* sigma() { return data_.sigma; } ///< The unit type within Type 0.
383 ///@}
384
385 /// @name Arr
386 ///@{
387 // clang-format off
388 const Def* unit(bool term) { return term ? (const Def*)tuple() : sigma(); }
389
390 Seq* mut_seq(bool term, const Def* type) { return term ? (Seq*)insert<Pack>(type) : insert<Arr>(type); }
391 const Def* seq(bool term, const Def* arity, const Def* body);
392 const Def* seq(bool term, Defs shape, const Def* body);
393 const Def* seq(bool term, u64 n, const Def* body) { return seq(term, lit_nat(n), body); }
394 const Def* seq(bool term, View<u64> shape, const Def* body) { return seq(term, DefVec(shape.size(), [&](size_t i) { return lit_nat(shape[i]); }), body); }
395 const Def* seq_unsafe(bool term, const Def* body) { return seq(term, top_nat(), body); }
396
397 template<level_t level = 0>
399 return mut_arr(type<level>());
400 }
401
402 Arr * mut_arr (const Def* type) { return mut_seq(false, type)->as<Arr >(); }
403 Pack* mut_pack(const Def* type) { return mut_seq(true , type)->as<Pack>(); }
404 const Def* arr (const Def* arity, const Def* body) { return seq(false, arity, body); }
405 const Def* pack(const Def* arity, const Def* body) { return seq(true , arity, body); }
406 const Def* arr (Defs shape, const Def* body) { return seq(false, shape, body); }
407 const Def* pack(Defs shape, const Def* body) { return seq(true , shape, body); }
408 const Def* arr (u64 n, const Def* body) { return seq(false, n, body); }
409 const Def* pack(u64 n, const Def* body) { return seq(true , n, body); }
410 const Def* arr (View<u64> shape, const Def* body) { return seq(false, shape, body); }
411 const Def* pack(View<u64> shape, const Def* body) { return seq(true , shape, body); }
412 const Def* arr_unsafe( const Def* body) { return seq_unsafe(false, body); }
413 const Def* pack_unsafe( const Def* body) { return seq_unsafe(true , body); }
414
415 const Def* prod(bool term, Defs ops) { return term ? tuple(ops) : sigma(ops); }
416 const Def* prod(bool term) { return term ? (const Def*)tuple() : (const Def*)sigma(); }
417 // clang-format on
418 ///@}
419
420 /// @name Tuple
421 ///@{
422 const Def* tuple(Defs ops);
423 /// Ascribes @p type to this tuple - needed for dependently typed and mutable Sigma%s.
424 const Def* tuple(const Def* type, Defs ops);
425 const Tuple* tuple() { return data_.tuple; } ///< the unit value of type `[]`
426 const Def* tuple(Sym sym); ///< Converts @p sym to a tuple of type '«n; I8»'.
427 ///@}
428
429 /// @name Extract
430 /// @see core::extract_unsafe
431 ///@{
432 const Def* extract(const Def* d, const Def* i);
433 const Def* extract(const Def* d, u64 a, u64 i) { return extract(d, lit_idx(a, i)); }
434 const Def* extract(const Def* d, u64 i) { return extract(d, Lit::as(d->arity()), i); }
435
436 /// Builds `(f, t)#cond`.
437 /// @note Expects @p cond as first, @p t as second, and @p f as third argument.
438 const Def* select(const Def* cond, const Def* t, const Def* f) { return extract(tuple({f, t}), cond); }
439 ///@}
440
441 /// @name Insert
442 /// @see core::insert_unsafe
443 ///@{
444 const Def* insert(const Def* d, const Def* i, const Def* val);
445 const Def* insert(const Def* d, u64 a, u64 i, const Def* val) { return insert(d, lit_idx(a, i), val); }
446 const Def* insert(const Def* d, u64 i, const Def* val) { return insert(d, Lit::as(d->arity()), i, val); }
447 ///@}
448
449 /// @name Lit
450 ///@{
451 const Lit* lit(const Def* type, u64 val);
452 const Lit* lit_univ(u64 level) { return lit(univ(), level); }
453 const Lit* lit_univ_0() { return data_.lit_univ_0; }
454 const Lit* lit_univ_1() { return data_.lit_univ_1; }
455 const Lit* lit_nat(nat_t a) { return lit(type_nat(), a); }
456 const Lit* lit_nat_0() { return data_.lit_nat_0; }
457 const Lit* lit_nat_1() { return data_.lit_nat_1; }
458 const Lit* lit_nat_max() { return data_.lit_nat_max; }
459 const Lit* lit_idx_1_0() { return data_.lit_idx_1_0; }
460 // clang-format off
461 const Lit* lit_i1() { return lit_nat(Idx::bitwidth2size( 1)); };
462 const Lit* lit_i8() { return lit_nat(Idx::bitwidth2size( 8)); };
463 const Lit* lit_i16() { return lit_nat(Idx::bitwidth2size(16)); };
464 const Lit* lit_i32() { return lit_nat(Idx::bitwidth2size(32)); };
465 const Lit* lit_i64() { return lit_nat(Idx::bitwidth2size(64)); };
466 /// Constructs a Lit of type Idx of size @p size.
467 /// @note `size = 0` means `2^64`.
468 const Lit* lit_idx(nat_t size, u64 val) { return lit(type_idx(size), val); }
469 const Lit* lit_idx_unsafe(u64 val) { return lit(type_idx(top(type_nat())), val); }
470
471 template<class I> const Lit* lit_idx(I val) {
472 static_assert(std::is_integral<I>());
473 return lit_idx(Idx::bitwidth2size(sizeof(I) * 8), val);
474 }
475
476 /// Constructs a Lit @p of type Idx of size 2^width.
477 /// `val = 64` will be automatically converted to size `0` - the encoding for 2^64.
478 const Lit* lit_int(nat_t width, u64 val) { return lit_idx(Idx::bitwidth2size(width), val); }
479 const Lit* lit_i1 (bool val) { return lit_int( 1, u64(val)); }
480 const Lit* lit_i2 (u8 val) { return lit_int( 2, u64(val)); }
481 const Lit* lit_i4 (u8 val) { return lit_int( 4, u64(val)); }
482 const Lit* lit_i8 (u8 val) { return lit_int( 8, u64(val)); }
483 const Lit* lit_i16(u16 val) { return lit_int(16, u64(val)); }
484 const Lit* lit_i32(u32 val) { return lit_int(32, u64(val)); }
485 const Lit* lit_i64(u64 val) { return lit_int(64, u64(val)); }
486 // clang-format on
487
488 /// Constructs a Lit of type Idx of size @p mod.
489 /// The value @p val will be adjusted modulo @p mod.
490 /// @note `mod == 0` is the special case for 2^64 and no modulo will be performed on @p val.
491 const Lit* lit_idx_mod(nat_t mod, u64 val) { return lit_idx(mod, mod == 0 ? val : (val % mod)); }
492
493 const Lit* lit_bool(bool val) { return data_.lit_bool[size_t(val)]; }
494 const Lit* lit_ff() { return data_.lit_bool[0]; }
495 const Lit* lit_tt() { return data_.lit_bool[1]; }
496 ///@}
497
498 /// @name Lattice
499 ///@{
500 template<bool Up>
501 const Def* ext(const Def* type);
502 const Def* bot(const Def* type) { return ext<false>(type); }
503 const Def* top(const Def* type) { return ext<true>(type); }
504 const Def* type_bot() { return data_.type_bot; }
505 const Def* type_top() { return data_.type_top; }
506 const Def* top_nat() { return data_.top_nat; }
507 template<bool Up>
508 const Def* bound(Defs ops);
509 const Def* join(Defs ops) { return bound<true>(ops); }
510 const Def* meet(Defs ops) { return bound<false>(ops); }
511 const Def* merge(const Def* type, Defs ops);
512 const Def* merge(Defs ops); ///< Infers the type using a Meet.
513 const Def* inj(const Def* type, const Def* value);
514 const Def* split(const Def* type, const Def* value);
515 const Def* match(Defs);
516 const Def* uniq(const Def* inhabitant);
517 ///@}
518
519 /// @name Globals
520 /// @deprecated Will be removed.
521 ///@{
522 Global* global(const Def* type, bool is_mutable = true) { return insert<Global>(type, is_mutable); }
523 ///@}
524
525 /// @name Types
526 ///@{
527 const Nat* type_nat() { return data_.type_nat; }
528 const Idx* type_idx() { return data_.type_idx; }
529 /// @note `size = 0` means `2^64`.
530 const Def* type_idx(const Def* size) { return app(type_idx(), size); }
531 /// @note `size = 0` means `2^64`.
532 const Def* type_idx(nat_t size) { return type_idx(lit_nat(size)); }
533
534 /// Constructs a type Idx of size 2^width.
535 /// `width = 64` will be automatically converted to size `0` - the encoding for 2^64.
536 const Def* type_int(nat_t width) { return type_idx(lit_nat(Idx::bitwidth2size(width))); }
537 // clang-format off
538 const Def* type_bool() { return data_.type_bool; }
539 const Def* type_i1() { return data_.type_bool; }
540 const Def* type_i2() { return type_int( 2); };
541 const Def* type_i4() { return type_int( 4); };
542 const Def* type_i8() { return type_int( 8); };
543 const Def* type_i16() { return type_int(16); };
544 const Def* type_i32() { return type_int(32); };
545 const Def* type_i64() { return type_int(64); };
546 // clang-format on
547 ///@}
548
549 /// @name Cope with implicit Arguments
550 ///@{
551
552 /// Places Hole%s as demanded by Pi::is_implicit() and then apps @p arg.
553 template<bool Normalize = true>
554 const Def* implicit_app(const Def* callee, const Def* arg);
555 template<bool Normalize = true>
556 const Def* implicit_app(const Def* callee, Defs args) {
557 return implicit_app<Normalize>(callee, tuple(args));
558 }
559 template<bool Normalize = true>
560 const Def* implicit_app(const Def* callee, nat_t arg) {
561 return implicit_app<Normalize>(callee, lit_nat(arg));
562 }
563 template<bool Normalize = true, class E>
564 const Def* implicit_app(const Def* callee, E arg)
565 requires std::is_enum_v<E> && std::is_same_v<std::underlying_type_t<E>, nat_t>
566 {
567 return implicit_app<Normalize>(callee, lit_nat((nat_t)arg));
568 }
569
570 /// Complete curried call of annexes obeying implicits.
571 // clang-format off
572 template<class Id, bool Normalize = true, class... Args> const Def* call(Id id, Args&&... args) { return call_<Normalize>(annex(id), std::forward<Args>(args)...); }
573 template<class Id, bool Normalize = true, class... Args> const Def* call( Args&&... args) { return call_<Normalize>(annex<Id>(), std::forward<Args>(args)...); }
574 // clang-format on
575 ///@}
576
577 /// @name Vars & Muts
578 /// Manges sets of Vars and Muts.
579 ///@{
580 [[nodiscard]] auto& vars() { return move_.vars; }
581 [[nodiscard]] auto& muts() { return move_.muts; }
582 [[nodiscard]] const auto& vars() const { return move_.vars; }
583 [[nodiscard]] const auto& muts() const { return move_.muts; }
584
585 /// Yields the new body of `[mut->var() -> arg]mut`.
586 /// The new body may have fewer elements as `mut->num_ops()` according to Def::reduction_offset.
587 /// E.g. a Pi has a Pi::reduction_offset of 1, and only Pi::dom will be reduced - *not* Pi::codom.
588 Defs reduce(const Var* var, const Def* arg);
589 ///@}
590
591 /// @name for_each
592 /// Visits all closed mutables in this World.
593 ///@{
594 void for_each(bool elide_empty, std::function<void(Def*)>);
595
596 template<class M>
597 void for_each(bool elide_empty, std::function<void(M*)> f) {
598 for_each(elide_empty, [f](Def* m) {
599 if (auto mut = m->template isa<M>()) f(mut);
600 });
601 }
602 ///@}
603
604 /// @name dump/log
605 ///@{
606 Log& log() const;
607 void dump(std::ostream& os); ///< Dump to @p os.
608 void dump(); ///< Dump to `std::cout`.
609 void debug_dump(); ///< Dump in Debug build if World::log::level is Log::Level::Debug.
610 void write(const char* file); ///< Write to a file named @p file.
611 void write(); ///< Same above but file name defaults to World::name.
612 ///@}
613
614 /// @name dot
615 /// GraphViz output.
616 ///@{
617
618 /// Dumps DOT to @p os.
619 /// @param os Output stream
620 /// @param annexes If `true`, include all annexes - even if unused.
621 /// @param types Follow type dependencies?
622 void dot(std::ostream& os, bool annexes = false, bool types = false) const;
623 /// Same as above but write to @p file or `std::cout` if @p file is `nullptr`.
624 void dot(const char* file = nullptr, bool annexes = false, bool types = false) const;
625 ///@}
626
627private:
628 /// @name call_
629 /// Helpers to unwind World::call with variadic templates.
630 ///@{
631 template<bool Normalize = true, class T, class... Args>
632 const Def* call_(const Def* callee, T arg, Args&&... args) {
633 return call_<Normalize>(implicit_app(callee, arg), std::forward<Args>(args)...);
634 }
635 template<bool Normalize = true, class T>
636 const Def* call_(const Def* callee, T arg) {
637 return implicit_app<Normalize>(callee, arg);
638 }
639 ///@}
640
641 /// @name Put into Sea of Nodes
642 ///@{
643 template<class T, class... Args>
644 const T* unify(Args&&... args) {
645 auto num_ops = T::Num_Ops;
646 if constexpr (T::Num_Ops == std::dynamic_extent) {
647 auto&& last = std::get<sizeof...(Args) - 1>(std::forward_as_tuple(std::forward<Args>(args)...));
648 num_ops = last.size();
649 }
650
651 auto state = move_.arena.defs.state();
652 auto def = allocate<T>(num_ops, std::forward<Args&&>(args)...);
653 assert(!def->isa_mut());
654
655 if (auto loc = get_loc()) def->set(loc);
656
657#ifdef MIM_ENABLE_CHECKS
658 if (flags().trace_gids) outln("{}: {} - {}", def->node_name(), def->gid(), def->flags());
659 if (flags().reeval_breakpoints && breakpoints().contains(def->gid())) fe::breakpoint();
660#endif
661
662 if (is_frozen()) {
663 auto i = move_.defs.find(def);
664 deallocate<T>(state, def);
665 if (i != move_.defs.end()) return static_cast<const T*>(*i);
666 return nullptr;
667 }
668
669 if (auto [i, ins] = move_.defs.emplace(def); !ins) {
670 deallocate<T>(state, def);
671 return static_cast<const T*>(*i);
672 }
673
674#ifdef MIM_ENABLE_CHECKS
675 if (!flags().reeval_breakpoints && breakpoints().contains(def->gid())) fe::breakpoint();
676#endif
677 return def;
678 }
679
680 template<class T>
681 void deallocate(fe::Arena::State state, const T* ptr) {
682 --state_.pod.curr_gid;
683 ptr->~T();
684 move_.arena.defs.deallocate(state);
685 }
686
687 template<class T, class... Args>
688 T* insert(Args&&... args) {
689 if (is_frozen()) return nullptr;
690
691 auto num_ops = T::Num_Ops;
692 if constexpr (T::Num_Ops == std::dynamic_extent)
693 num_ops = std::get<sizeof...(Args) - 1>(std::forward_as_tuple(std::forward<Args>(args)...));
694
695 auto def = allocate<T>(num_ops, std::forward<Args>(args)...);
696 if (auto loc = get_loc()) def->set(loc);
697
698#ifdef MIM_ENABLE_CHECKS
699 if (flags().trace_gids) outln("{}: {} - {}", def->node_name(), def->gid(), def->flags());
700 if (breakpoints().contains(def->gid())) fe::breakpoint();
701#endif
702 assert_emplace(move_.defs, def);
703 return def;
704 }
705
706#if (!defined(_MSC_VER) && defined(NDEBUG))
707 struct Lock {
708 Lock() { assert((guard_ = !guard_) && "you are not allowed to recursively invoke allocate"); }
709 ~Lock() { guard_ = !guard_; }
710 static bool guard_;
711 };
712#else
713 struct Lock {
714 ~Lock() {}
715 };
716#endif
717
718 template<class T, class... Args>
719 T* allocate(size_t num_ops, Args&&... args) {
720 static_assert(sizeof(Def) == sizeof(T),
721 "you are not allowed to introduce any additional data in subclasses of Def");
722 auto lock = Lock();
723 auto num_bytes = sizeof(Def) + sizeof(uintptr_t) * num_ops;
724 auto ptr = move_.arena.defs.allocate(num_bytes, alignof(T));
725 auto res = new (ptr) T(std::forward<Args>(args)...);
726 assert(res->num_ops() == num_ops);
727 return res;
728 }
729 ///@}
730
731 Driver* driver_;
732 Zonker zonker_;
733 State state_;
734
735 struct SeaHash {
736 size_t operator()(const Def* def) const { return def->hash(); }
737 };
738
739 struct SeaEq {
740 bool operator()(const Def* d1, const Def* d2) const { return d1->equal(d2); }
741 };
742
743 class Reduct {
744 public:
745 constexpr Reduct(size_t size) noexcept
746 : size_(size) {}
747
748 template<size_t N = std::dynamic_extent>
749 constexpr auto defs() const noexcept {
750 return View<const Def*, N>{defs_, size_};
751 }
752
753 private:
754 size_t size_;
755 const Def* defs_[];
756
757 friend class World;
758 };
759
760 struct Move {
761 struct {
762 fe::Arena defs, substs;
763 } arena;
764
766 absl::btree_map<flags_t, const Def*> flags2annex;
767 absl::flat_hash_set<const Def*, SeaHash, SeaEq> defs;
770 absl::flat_hash_map<std::pair<const Var*, const Def*>, const Reduct*> substs;
771
772 friend void swap(Move& m1, Move& m2) noexcept {
773 using std::swap;
774 // clang-format off
775 swap(m1.arena.defs, m2.arena.defs);
776 swap(m1.arena.substs, m2.arena.substs);
777 swap(m1.defs, m2.defs);
778 swap(m1.substs, m2.substs);
779 swap(m1.vars, m2.vars);
780 swap(m1.muts, m2.muts);
781 swap(m1.flags2annex, m2.flags2annex);
782 swap(m1.externals, m2.externals);
783 // clang-format on
784 }
785 } move_;
786
787 struct {
788 const Univ* univ;
789 const Type* type_0;
790 const Type* type_1;
791 const Bot* type_bot;
792 const Top* type_top;
793 const Def* type_bool;
794 const Top* top_nat;
795 const Sigma* sigma;
796 const Tuple* tuple;
797 const Nat* type_nat;
798 const Idx* type_idx;
799 const Def* table_id;
800 const Def* table_not;
801 const Lit* lit_univ_0;
802 const Lit* lit_univ_1;
803 const Lit* lit_nat_0;
804 const Lit* lit_nat_1;
805 const Lit* lit_nat_max;
806 const Lit* lit_idx_1_0;
807 std::array<const Lit*, 2> lit_bool;
808 u32 curr_run = 0;
809 } data_;
810
811 friend void swap(World& w1, World& w2) noexcept {
812 using std::swap;
813 // clang-format off
814 swap(w1.driver_, w2.driver_ );
815 swap(w1.zonker_, w2.zonker_ );
816 swap(w1.state_, w2.state_);
817 swap(w1.data_, w2.data_ );
818 swap(w1.move_, w2.move_ );
819 // clang-format on
820
821 swap(w1.data_.univ->world_, w2.data_.univ->world_);
822 assert(&w1.univ()->world() == &w1);
823 assert(&w2.univ()->world() == &w2);
824 }
825};
826
827} // namespace mim
A (possibly paramterized) Array.
Definition tuple.h:117
Definition axm.h:9
Base class for all Defs.
Definition def.h:251
Some "global" variables needed all over the place.
Definition driver.h:17
This node is a hole in the IR that is inferred by its context later on.
Definition check.h:14
A built-in constant of type Nat -> *.
Definition def.h:859
static constexpr nat_t bitwidth2size(nat_t n)
Definition def.h:890
A function.
Definition lam.h:111
std::variant< bool, const Def * > Filter
Definition lam.h:119
static T as(const Def *def)
Definition def.h:830
Facility to log what you are doing.
Definition log.h:17
A (possibly paramterized) Tuple.
Definition tuple.h:166
A dependent function type.
Definition lam.h:15
static const Def * infer(const Def *dom, const Def *codom)
Definition check.cpp:318
Type formation of a rewrite Rule.
Definition rule.h:9
static const Def * infer(const Def *meta_type)
Definition check.cpp:355
A rewrite rule.
Definition rule.h:38
Rule * set(const Def *lhs, const Def *rhs)
Definition rule.h:62
Base class for Arr and Pack.
Definition tuple.h:84
A dependent tuple type.
Definition tuple.h:20
Data constructor for a Sigma.
Definition tuple.h:68
A variable introduced by a binder (mutable).
Definition def.h:700
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
Definition vector.h:18
Vector< Def * > mutate() const
Returns a copy of muts() in a Vector; this allows you modify the Externals while iterating.
Definition world.h:212
void internalize(Def *)
Definition world.cpp:34
Def * operator[](Sym name) const
Lookup by name.
Definition world.h:213
friend void swap(Externals &ex1, Externals &ex2) noexcept
Definition world.h:228
auto end() const
Definition world.h:225
auto muts() const
Definition world.h:209
auto begin() const
Definition world.h:224
const auto & sym2mut() const
Definition world.h:207
void externalize(Def *)
Definition world.cpp:27
auto syms() const
Definition world.h:208
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:31
const Lit * lit_idx(nat_t size, u64 val)
Constructs a Lit of type Idx of size size.
Definition world.h:468
const Lit * lit_idx(I val)
Definition world.h:471
const Def * arr(Defs shape, const Def *body)
Definition world.h:406
const Def * insert(const Def *d, const Def *i, const Def *val)
Definition world.cpp:430
const Def * type_i16()
Definition world.h:543
const Pi * fn(Defs dom, const Def *codom, bool implicit=false)
Definition world.h:310
const Def * meet(Defs ops)
Definition world.h:510
std::unique_ptr< World > inherit()
Inherits the State into the new World.
Definition world.h:76
const auto & flags2annex() const
Definition world.h:176
const Lam * con(Defs dom, Lam::Filter f, const Def *body)
Definition world.h:326
const Def * uinc(const Def *op, level_t offset=1)
Definition world.cpp:117
const Pi * cn()
Definition world.h:306
const Lit * lit(const Def *type, u64 val)
Definition world.cpp:513
const Def * arr(u64 n, const Def *body)
Definition world.h:408
u32 next_run()
Definition world.h:97
friend void swap(World &w1, World &w2) noexcept
Definition world.h:811
const Def * implicit_app(const Def *callee, nat_t arg)
Definition world.h:560
const Def * extract(const Def *d, u64 a, u64 i)
Definition world.h:433
auto & muts()
Definition world.h:581
const Def * type_i4()
Definition world.h:541
const Lit * lit_i8()
Definition world.h:462
Hole * mut_hole_infer_entity()
Either a value ?:?:Type ? or a type ?:Type ?:Type ?.
Definition world.h:266
const Pi * cn(const Def *dom, bool implicit=false)
Definition world.h:307
const Def * type_int(nat_t width)
Constructs a type Idx of size 2^width.
Definition world.h:536
World & operator=(World)=delete
const Lit * lit_i16(u16 val)
Definition world.h:483
void watchpoint(u32 gid)
Trigger breakpoint in your debugger when Def::setting a Def with this gid.
Definition world.cpp:699
const Lit * lit_i1(bool val)
Definition world.h:479
const Def * arr(View< u64 > shape, const Def *body)
Definition world.h:410
const Lit * lit_i32()
Definition world.h:464
Driver & driver()
Definition world.h:87
Zonker & zonker()
Definition world.h:88
Lam * mut_fun(const Def *dom, Defs codom)
Definition world.h:343
const Type * type(const Def *level)
Definition world.cpp:107
const Driver & driver() const
Definition world.h:86
const Lam * lam(const Def *dom, const Def *codom, Lam::Filter f, const Def *body)
Definition world.h:327
Externals & externals()
Definition world.h:238
Lam * mut_lam(const Def *dom, Defs codom)
Definition world.h:339
const Lit * lit_tt()
Definition world.h:495
const Def * filter(Lam::Filter filter)
Definition world.h:318
World(Driver *)
Definition world.cpp:74
const auto & watchpoints()
Definition world.h:162
const Def * type_idx(nat_t size)
Definition world.h:532
const Def * pack(const Def *arity, const Def *body)
Definition world.h:405
const Def * unit(bool term)
Definition world.h:388
void set(std::string_view name)
Definition world.h:92
const Def * app(const Def *callee, const Def *arg)
Definition world.cpp:199
u32 curr_gid() const
Manage global identifier - a unique number for each Def.
Definition world.h:95
const Def * seq(bool term, View< u64 > shape, const Def *body)
Definition world.h:394
const Axm * axm(const Def *type)
See above.
Definition world.h:288
const Def * match(Defs)
Definition world.cpp:600
const Def * type_bot()
Definition world.h:504
const Pi * pi(const Def *dom, const Def *codom, bool implicit=false)
Definition world.h:294
const Def * pack(u64 n, const Def *body)
Definition world.h:409
const Def * seq(bool term, const Def *arity, const Def *body)
Definition world.cpp:479
const Def * insert(const Def *d, u64 a, u64 i, const Def *val)
Definition world.h:445
const auto & muts() const
Definition world.h:583
const Univ * univ()
Definition world.h:243
const Lam * lam(Defs dom, const Def *codom, Lam::Filter f, const Def *body)
Definition world.h:328
const Def * seq_unsafe(bool term, const Def *body)
Definition world.h:395
const Lam * fun(Defs dom, const Def *codom, Lam::Filter f, const Def *body)
Definition world.h:332
Rule * mut_rule(const Reform *type)
Definition world.h:351
const Def * type_i8()
Definition world.h:542
World & verify()
Verifies that all externals() and annexes() are Def::is_closed(), if MIM_ENABLE_CHECKS.
Definition world.cpp:707
const Def * bot(const Def *type)
Definition world.h:502
const Lit * lit_i64()
Definition world.h:465
Arr * mut_arr(const Def *type)
Definition world.h:402
const Rule * rule(const Def *meta_type, const Def *lhs, const Def *rhs, const Def *guard)
Definition world.h:355
const Lam * lam(const Def *dom, Defs codom, Lam::Filter f, const Def *body)
Definition world.h:329
const Lit * lit_idx_mod(nat_t mod, u64 val)
Constructs a Lit of type Idx of size mod.
Definition world.h:491
const Idx * type_idx()
Definition world.h:528
const Def * type_i1()
Definition world.h:539
Pi * mut_pi(const Def *type, bool implicit=false)
Definition world.h:298
const Lit * lit_univ_0()
Definition world.h:453
const Def * annex(Id id)
Lookup annex by Axm::id.
Definition world.h:181
void set_loc(Loc loc={})
Definition world.h:117
const Def * implicit_app(const Def *callee, E arg)
Definition world.h:564
Sym name() const
Definition world.h:90
const Pi * cn(Defs dom, bool implicit=false)
Definition world.h:308
void dump()
Dump to std::cout.
Definition dump.cpp:491
void for_each(bool elide_empty, std::function< void(Def *)>)
Definition world.cpp:677
const Lam * fun(const Def *dom, Defs codom, Lam::Filter f, const Def *body)
Definition world.h:333
const Lit * lit_univ_1()
Definition world.h:454
const Reform * reform(const Def *meta_type)
Definition world.h:350
void write()
Same above but file name defaults to World::name.
Definition dump.cpp:502
const Lam * lam(Defs dom, Defs codom, Lam::Filter f, const Def *body)
Definition world.h:330
const Def * register_annex(plugin_t p, tag_t t, sub_t s, const Def *def)
Definition world.h:196
Lam * mut_fun(Defs dom, const Def *codom)
Definition world.h:342
Lam * mut_fun(const Def *dom, const Def *codom)
Definition world.h:341
const Lit * lit_i1()
Definition world.h:461
const Axm * axm(NormalizeFn n, u8 curry, u8 trip, const Def *type)
Builds a fresh Axm with descending Axm::sub.
Definition world.h:285
const Nat * type_nat()
Definition world.h:527
const Pi * fn(const Def *dom, Defs codom, bool implicit=false)
Definition world.h:311
Hole * mut_hole(const Def *type)
Definition world.h:261
const Axm * axm(const Def *type, plugin_t p, tag_t t, sub_t s)
Definition world.h:279
const Lam * lam(const Pi *pi, Lam::Filter f, const Def *body)
Definition world.h:322
void for_each(bool elide_empty, std::function< void(M *)> f)
Definition world.h:597
const Lam * fun(Defs dom, Defs codom, Lam::Filter f, const Def *body)
Definition world.h:334
Lam * mut_lam(const Def *dom, const Def *codom)
Definition world.h:337
const Def * gid2def(u32 gid)
Lookup Def by gid.
Definition world.cpp:701
Flags & flags()
Retrieve compile Flags.
Definition world.cpp:87
const Def * implicit_app(const Def *callee, const Def *arg)
Places Holes as demanded by Pi::is_implicit() and then apps arg.
Definition world.cpp:192
ScopedLoc push(Loc loc)
Definition world.h:118
const Def * implicit_app(const Def *callee, Defs args)
Definition world.h:556
void debug_dump()
Dump in Debug build if World::log::level is Log::Level::Debug.
Definition dump.cpp:493
u32 next_gid()
Definition world.h:96
const Def * extract(const Def *d, u64 i)
Definition world.h:434
Seq * mut_seq(bool term, const Def *type)
Definition world.h:390
const Lam * fun(const Def *dom, const Def *codom, Lam::Filter f, const Def *body)
Definition world.h:331
const Def * inj(const Def *type, const Def *value)
Definition world.cpp:585
const Def * call(Args &&... args)
Definition world.h:573
const auto & breakpoints()
Definition world.h:161
const Type * type()
Definition world.h:250
const Axm * axm(NormalizeFn n, u8 curry, u8 trip, const Def *type, plugin_t p, tag_t t, sub_t s)
Definition world.h:276
const Def * pack(Defs shape, const Def *body)
Definition world.h:407
const Def * seq(bool term, u64 n, const Def *body)
Definition world.h:393
void set(Sym name)
Definition world.h:91
const Def * app(const Def *callee, Defs args)
Definition world.h:365
const Def * pack(View< u64 > shape, const Def *body)
Definition world.h:411
Lam * mut_fun(Defs dom, Defs codom)
Definition world.h:344
const Lit * lit_i2(u8 val)
Definition world.h:480
const Def * extract(const Def *d, const Def *i)
Definition world.cpp:349
Pack * mut_pack(const Def *type)
Definition world.h:403
bool is_frozen() const
Definition world.h:137
const Lit * lit_idx_unsafe(u64 val)
Definition world.h:469
Loc get_loc() const
Definition world.h:116
const Lit * lit_nat_0()
Definition world.h:456
const Def * arr(const Def *arity, const Def *body)
Definition world.h:404
Sym sym(std::string_view)
Definition world.cpp:90
const Lit * lit_i16()
Definition world.h:463
Global * global(const Def *type, bool is_mutable=true)
Definition world.h:522
Lam * mut_lam(Defs dom, const Def *codom)
Definition world.h:338
const Lit * lit_ff()
Definition world.h:494
const Lit * lit_nat_max()
Definition world.h:458
const Def * raw_app(const Def *type, const Def *callee, Defs args)
Definition world.h:370
const Pi * pi(Defs dom, const Def *codom, bool implicit=false)
Definition world.h:295
const Def * bound(Defs ops)
Definition world.cpp:544
const Def * join(Defs ops)
Definition world.h:509
const Def * ext(const Def *type)
Definition world.cpp:534
Sym append_suffix(Sym name, std::string suffix)
Appends a suffix or an increasing number if the suffix already exists.
Definition world.cpp:641
const Proxy * proxy(const Def *type, Defs ops, u32 index, u32 tag)
Definition world.h:259
const Lit * lit_i32(u32 val)
Definition world.h:484
const Lit * lit_i8(u8 val)
Definition world.h:482
Lam * mut_lam(Defs dom, Defs codom)
Definition world.h:340
const Def * type_top()
Definition world.h:505
const Lit * lit_idx_1_0()
Definition world.h:459
Sigma * mut_sigma(size_t size)
A mutable Sigma of type level.
Definition world.h:378
const Lit * lit_univ(u64 level)
Definition world.h:452
const Pi * pi(Defs dom, Defs codom, bool implicit=false)
Definition world.h:297
const Def * var(Def *mut)
Definition world.cpp:179
const Tuple * tuple()
the unit value of type []
Definition world.h:425
const Type * type_infer_univ()
Definition world.h:248
const Def * call(Id id, Args &&... args)
Complete curried call of annexes obeying implicits.
Definition world.h:572
const Def * type_bool()
Definition world.h:538
const Def * uniq(const Def *inhabitant)
Definition world.cpp:636
const Def * raw_app(const Axm *axm, u8 curry, u8 trip, const Def *type, const Def *callee, const Def *arg)
Definition world.cpp:277
const Def * prod(bool term, Defs ops)
Definition world.h:415
const Externals & externals() const
Definition world.h:237
const Def * prod(bool term)
Definition world.h:416
const Def * umax(Defs)
Definition world.cpp:137
const Lam * con(const Def *dom, Lam::Filter f, const Def *body)
Definition world.h:325
const Def * type_i2()
Definition world.h:540
const Def * arr_unsafe(const Def *body)
Definition world.h:412
const Def * merge(const Def *type, Defs ops)
Definition world.cpp:567
const Lit * lit_i64(u64 val)
Definition world.h:485
const Def * type_i64()
Definition world.h:545
const Sigma * sigma()
The unit type within Type 0.
Definition world.h:382
const Lit * lit_nat(nat_t a)
Definition world.h:455
const State & state() const
Definition world.h:85
Hole * mut_hole_type()
Definition world.h:263
const Def * pack_unsafe(const Def *body)
Definition world.h:413
const auto & vars() const
Definition world.h:582
const Def * register_annex(flags_t f, const Def *)
Definition world.cpp:93
const Def * top(const Def *type)
Definition world.h:503
const Def * type_idx(const Def *size)
Definition world.h:530
Lam * mut_con(const Def *dom)
Definition world.h:335
Arr * mut_arr()
Definition world.h:398
auto & vars()
Definition world.h:580
Defs reduce(const Var *var, const Def *arg)
Yields the new body of [mut->var() -> arg]mut.
Definition world.cpp:661
const Lit * lit_int(nat_t width, u64 val)
Constructs a Lit of type Idx of size 2^width.
Definition world.h:478
const Lit * lit_bool(bool val)
Definition world.h:493
void breakpoint(u32 gid)
Trigger breakpoint in your debugger when creating a Def with this gid.
Definition world.cpp:698
const Def * select(const Def *cond, const Def *t, const Def *f)
Builds (f, t)#cond.
Definition world.h:438
Sigma * mut_sigma(const Def *type, size_t size)
Definition world.h:375
Hole * mut_hole_univ()
Definition world.h:262
Lam * mut_con(Defs dom)
Definition world.h:336
const Def * split(const Def *type, const Def *value)
Definition world.cpp:593
const Def * top_nat()
Definition world.h:506
const Pi * fn(const Def *dom, const Def *codom, bool implicit=false)
Definition world.h:309
const Rule * rule(const Reform *type, const Def *lhs, const Def *rhs, const Def *guard)
Definition world.h:352
auto annexes() const
Definition world.h:177
Log & log() const
Definition world.cpp:86
World(World &&other) noexcept
Definition world.h:68
const Lit * lit_nat_1()
Definition world.h:457
const Pi * pi(const Def *dom, Defs codom, bool implicit=false)
Definition world.h:296
bool freeze(bool on=true) const
Yields old frozen state.
Definition world.h:140
const Def * type_i32()
Definition world.h:544
const Lit * lit_i4(u8 val)
Definition world.h:481
void dot(std::ostream &os, bool annexes=false, bool types=false) const
Dumps DOT to os.
Definition dot.cpp:146
const Def * insert(const Def *d, u64 i, const Def *val)
Definition world.h:446
const Pi * fn(Defs dom, Defs codom, bool implicit=false)
Definition world.h:312
Lam * mut_lam(const Pi *pi)
Definition world.h:323
const Def * annex()
Get Axm from a plugin.
Definition world.h:191
#define M(S, D)
Definition ast.h:14
View< const Def * > Defs
Definition def.h:76
u64 nat_t
Definition types.h:43
Vector< const Def * > DefVec
Definition def.h:77
u8 sub_t
Definition types.h:48
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:118
u64 flags_t
Definition types.h:45
auto lookup(const C &container, const K &key)
Yields pointer to element (or the element itself if it is already a pointer), if found and nullptr ot...
Definition util.h:100
Span< const T, N > View
Definition span.h:98
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:125
u64 level_t
Definition types.h:42
TExt< true > Top
Definition lattice.h:172
std::ostream & outln(const char *fmt, Args &&... args)
Definition print.h:200
const Def *(*)(const Def *, const Def *, const Def *) NormalizeFn
Definition def.h:100
u64 plugin_t
Definition types.h:46
uint32_t u32
Definition types.h:34
TExt< false > Bot
Definition lattice.h:171
uint64_t u64
Definition types.h:34
u8 tag_t
Definition types.h:47
uint8_t u8
Definition types.h:34
@ Nat
Definition def.h:114
@ Univ
Definition def.h:114
@ Idx
Definition def.h:114
@ Sigma
Definition def.h:114
@ Type
Definition def.h:114
@ Tuple
Definition def.h:114
@ Lit
Definition def.h:114
uint16_t u16
Definition types.h:34
Compiler switches that must be saved and looked up in later phases of compilation.
Definition flags.h:11
static constexpr plugin_t Global_Plugin
Definition plugin.h:59
static Sym demangle(Driver &, plugin_t plugin)
Reverts an Axm::mangled string to a Sym.
Definition plugin.cpp:37
static consteval flags_t base()
Definition plugin.h:119
Freezer(const World &world)
Definition world.h:148
const World & world
Definition world.h:153
ScopedLoc(World &world, Loc old_loc)
Definition world.h:106
absl::flat_hash_set< uint32_t > watchpoints
Definition world.h:49
friend void swap(State &s1, State &s2) noexcept
Definition world.h:51
struct mim::World::State::POD pod
absl::flat_hash_set< uint32_t > breakpoints
Definition world.h:48
Plain Old Data
Definition world.h:39