MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lam.h
Go to the documentation of this file.
1#pragma once
2
3#include <variant>
4
5#include "mim/def.h"
6
7namespace mim {
8
9/// A [dependent function type](https://en.wikipedia.org/wiki/Dependent_type#%CE%A0_type).
10/// @see Lam
11class Pi : public Def {
12protected:
13 /// Constructor for an *immutable* Pi.
14 Pi(const Def* type, const Def* dom, const Def* codom, bool implicit)
15 : Def(Node, type, {dom, codom}, (flags_t)implicit) {}
16 /// Constructor for a *mut*able Pi.
17 Pi(const Def* type, bool implicit)
18 : Def(Node, type, 2, implicit ? 1 : 0) {}
19
20public:
21 /// @name Get/Set implicit
22 ///@{
23 bool is_implicit() const { return flags(); }
24 Pi* make_implicit() { return flags_ = (flags_t) true, this; }
25 Pi* make_explicit() { return flags_ = (flags_t) false, this; }
26 ///@}
27
28 /// @name dom
29 /// @anchor pi_dom
30 /// @see @ref proj
31 ///@{
32 Ref dom() const { return op(0); }
33 MIM_PROJ(dom, const)
34 ///@}
35
36 /// @name codom
37 /// @anchor pi_codom
38 /// @see @ref proj
39 ///@{
40 Ref codom() const { return op(1); }
41 MIM_PROJ(codom, const)
42 ///@}
43
44 /// @name Continuations
45 /// @anchor continuations
46 /// Checks certain properties of this Pi regarding continuations.
47 ///@{
48 // clang-format off
49 /// Is this a continuation - i.e. is the Pi::codom mim::Bot%tom?
50 static const Pi* isa_cn(Ref d) { return d->isa<Pi>() && d->as<Pi>()->codom()->node() == Node::Bot ? d->as<Pi>() : nullptr; }
51 /// Is this a continuation (Pi::isa_cn) which has a Pi::ret_pi?
52 static const Pi* isa_returning(Ref d) { return isa_cn(d) && d->as<Pi>()->ret_pi() ? d->as<Pi>() : nullptr; }
53 /// Is this a continuation (Pi::isa_cn) that is **not** Pi::isa_returning?
54 static const Pi* isa_basicblock(Ref d) { return isa_cn(d) && !d->as<Pi>()->ret_pi() ? d->as<Pi>() : nullptr; }
55 // clang-format on
56 ///@}
57
58 /// @name Return Continuation
59 /// @anchor return_continuation
60 ///@{
61 /// Yields the Pi::ret_pi() of @p d, if it is in fact a Pi.
62 static const Pi* ret_pi(Ref d) { return d->isa<Pi>() ? d->as<Pi>()->ret_pi() : nullptr; }
63 /// Yields the last Pi::dom, if it Pi::isa_basicblock.
64 const Pi* ret_pi() const;
65 /// Pi::dom%ain of Pi::ret_pi.
66 Ref ret_dom() const { return ret_pi()->dom(); }
67 ///@}
68
69 /// @name Setters
70 /// @see @ref set_ops "Setting Ops"
71 ///@{
73 Pi* set_dom(Ref dom) { return Def::set(0, dom)->as<Pi>(); }
74 Pi* set_dom(Defs doms);
75 Pi* set_codom(Ref codom) { return Def::set(1, codom)->as<Pi>(); }
76 Pi* unset() { return Def::unset()->as<Pi>(); }
77 ///@}
78
79 /// @name Type Checking
80 ///@{
81 void check() override;
82 static Ref infer(Ref dom, Ref codom);
83 ///@}
84
85 const Pi* immutabilize() override;
86 Pi* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
87
89
90private:
91 Pi* stub_(World&, Ref) override;
92};
93
94/// A function.
95/// @see Pi
96class Lam : public Def {
97private:
98 Lam(const Pi* pi, const Def* filter, const Def* body)
99 : Def(Node, pi, {filter, body}, 0) {}
100 Lam(const Pi* pi)
101 : Def(Node, pi, 2, 0) {}
102
103public:
104 /// @name ops & type
105 ///@{
106 Ref filter() const { return op(0); }
107 Ref body() const { return op(1); }
108 const Pi* type() const { return Def::type()->as<Pi>(); }
109 ///@}
110
111 /// @name dom
112 /// @anchor lam_dom
113 /// @see @ref proj
114 ///@{
115 Ref dom() const { return type()->dom(); }
116 MIM_PROJ(dom, const)
117 ///@}
118
119 /// @name codom
120 /// @anchor lam_codom
121 /// @see @ref proj
122 ///@{
123 Ref codom() const { return type()->codom(); }
124 MIM_PROJ(codom, const)
125 ///@}
126
127 /// @name Continuations
128 /// @see @ref continuations "Pi: Continuations"
129 ///@{
130 // clang-format off
131 static const Lam* isa_cn(Ref d) { return Pi::isa_cn(d->type()) ? d->isa<Lam>() : nullptr; }
132 static const Lam* isa_basicblock(Ref d) { return Pi::isa_basicblock(d->type()) ? d->isa<Lam>() : nullptr; }
133 static const Lam* isa_returning(Ref d) { return Pi::isa_returning (d->type()) ? d->isa<Lam>() : nullptr; }
134 static Lam* isa_mut_cn(Ref d) { return isa_cn(d) ? d->isa_mut<Lam>() : nullptr; } ///< Only for mutables.
135 static Lam* isa_mut_basicblock(Ref d) { return isa_basicblock(d) ? d->isa_mut<Lam>(): nullptr; } ///< Only for mutables.
136 static Lam* isa_mut_returning(Ref d) { return isa_returning (d) ? d->isa_mut<Lam>(): nullptr; } ///< Only for mutables.
137 // clang-format on
138 ///@}
139
140 /// @name Return Continuation
141 /// @see @ref return_continuation "Pi: Return Continuation"
142 ///@{
143 const Pi* ret_pi() const { return type()->ret_pi(); }
144 Ref ret_dom() const { return ret_pi()->dom(); }
145 /// Yields the Lam::var of the Lam::ret_pi.
146 Ref ret_var() { return type()->ret_pi() ? var(num_vars() - 1) : nullptr; }
147 ///@}
148
149 /// @name Setters
150 /// Lam::Filter is a `std::variant<bool, const Def*>` that lets you set the Lam::filter() like this:
151 /// ```cpp
152 /// lam1->app(true, f, arg);
153 /// lam2->app(my_filter_def, f, arg);
154 /// ```
155 /// @see @ref set_ops "Setting Ops"
156 ///@{
157 using Filter = std::variant<bool, const Def*>;
159 Lam* set_filter(Filter); ///< Set filter first.
160 Lam* set_body(const Def* body) { return Def::set(1, body)->as<Lam>(); } ///< Set body second.
161 /// Set body to an App of @p callee and @p arg.
162 Lam* app(Filter filter, const Def* callee, const Def* arg);
163 /// Set body to an App of @p callee and @p args.
164 Lam* app(Filter filter, const Def* callee, Defs args);
165 /// Set body to an App of `(f, t)#cond mem` or `(f, t)#cond ()` if @p mem is `nullptr`.
166 Lam* branch(Filter filter, const Def* cond, const Def* t, const Def* f, const Def* mem = nullptr);
167 Lam* test(Filter filter, const Def* val, const Def* idx, const Def* match, const Def* clash, const Def* mem);
168 Lam* set(Defs ops) { return Def::set(ops)->as<Lam>(); }
169 Lam* unset() { return Def::unset()->as<Lam>(); }
170 ///@}
171
172 Lam* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
173
174 /// @name Type Checking
175 ///@{
176 void check() override;
177 ///@}
178
180
181private:
182 Lam* stub_(World&, Ref) override;
183};
184
185/// @name Lam
186/// GIDSet / GIDMap keyed by Lam::gid of `Lam*`.
187///@{
188template<class To> using LamMap = GIDMap<Lam*, To>;
191///@}
192
193class App : public Def {
194private:
195 App(const Axiom* axiom, u8 curry, u8 trip, const Def* type, const Def* callee, const Def* arg)
196 : Def(Node, type, {callee, arg}, 0) {
197 axiom_ = axiom;
198 curry_ = curry;
199 trip_ = trip;
200 }
201
202public:
203 /// @name callee
204 ///@{
205 const Def* callee() const { return op(0); }
206 const App* decurry() const { return callee()->as<App>(); } ///< Returns App::callee again as App.
207 const Pi* callee_type() const { return callee()->type()->as<Pi>(); }
208 ///@}
209
210 /// @name arg
211 /// @anchor app_arg
212 /// @see @ref proj
213 ///@{
214 const Def* arg() const { return op(1); }
215 MIM_PROJ(arg, const)
216 ///@}
217
218 /// @name Get axiom, current curry counter and trip count
219 ///@{
220 const Axiom* axiom() const { return axiom_; }
221 u8 curry() const { return curry_; }
222 u8 trip() const { return trip_; }
223 ///@}
224
226};
227
228/// @name Helpers to work with Functions
229///@{
230inline const App* isa_callee(const Def* def, size_t i) { return i == 0 ? def->isa<App>() : nullptr; }
231
232/// These are Lam%s that are neither `nullptr`, nor Lam::is_external, nor Lam::is_unset.
233inline Lam* isa_workable(Lam* lam) {
234 if (!lam || lam->is_external() || !lam->is_set()) return nullptr;
235 return lam;
236}
237
238inline std::pair<const App*, Lam*> isa_apped_mut_lam(const Def* def) {
239 if (auto app = def->isa<App>()) return {app, app->callee()->isa_mut<Lam>()};
240 return {nullptr, nullptr};
241}
242
243/// Yields curried App%s in a flat `std::deque<const App*>`.
244std::deque<const App*> decurry(const Def*);
245
246/// The high level view is:
247/// ```
248/// f: B -> C
249/// g: A -> B
250/// f o g := λ x. f(g(x)) : A -> C
251/// ```
252/// In CPS the types look like:
253/// ```
254/// f: .Cn[B, .Cn C]
255/// g: .Cn[A, .Cn B]
256/// h = f o g
257/// h: .Cn[A, cn C]
258/// h = λ (a ret_h) = g (a, h')
259/// h': .Cn B
260/// h'= λ b = f (b, ret_h)
261/// ```
262const Def* compose_cn(const Def* f, const Def* g);
263
264/// Helper function to cope with the fact that normalizers take all arguments and not only its axiom arguments.
265std::pair<const Def*, std::vector<const Def*>> collect_args(const Def* def);
266///@}
267
268} // namespace mim
const Pi * callee_type() const
Definition lam.h:207
u8 curry() const
Definition lam.h:221
const Axiom * axiom() const
Definition lam.h:220
const App * decurry() const
Returns App::callee again as App.
Definition lam.h:206
const Def * callee() const
Definition lam.h:205
const Def * arg() const
Definition lam.h:214
u8 trip() const
Definition lam.h:222
Base class for all Defs.
Definition def.h:220
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:311
Def * set(size_t i, const Def *def)
Successively set from left to right.
Definition def.cpp:248
Ref var()
Not necessarily a Var: E.g., if the return type is [], this will yield ().
Definition def.cpp:456
const Def * op(size_t i) const
Definition def.h:266
u8 trip_
Definition def.h:574
nat_t num_vars()
Definition def.h:398
World & world() const
Definition def.cpp:417
u8 curry_
Definition def.h:573
bool is_external() const
Definition def.h:428
auto ops() const
Definition def.h:265
const Def * type() const
Definition def.h:245
node_t node() const
Definition def.h:238
flags_t flags() const
Definition def.h:235
flags_t flags_
Definition def.h:572
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
Definition def.cpp:266
Dbg dbg() const
Definition def.h:463
A function.
Definition lam.h:96
std::variant< bool, const Def * > Filter
Definition lam.h:157
Lam * unset()
Definition lam.h:169
Ref filter() const
Definition lam.h:106
Lam * branch(Filter filter, const Def *cond, const Def *t, const Def *f, const Def *mem=nullptr)
Set body to an App of (f, t)#cond mem or (f, t)#cond () if mem is nullptr.
Definition lam.cpp:32
Lam * set(Filter filter, const Def *body)
Definition lam.h:158
void check() override
Definition check.cpp:252
Lam * stub(Ref type)
Definition lam.h:172
Lam * set_filter(Filter)
Set filter first.
Definition lam.cpp:28
Ref codom() const
Definition lam.h:123
static Lam * isa_mut_basicblock(Ref d)
Only for mutables.
Definition lam.h:135
Lam * stub_(World &, Ref) override
Definition def.cpp:133
const Pi * ret_pi() const
Definition lam.h:143
const Pi * type() const
Definition lam.h:108
Lam * set(Defs ops)
Definition lam.h:168
static Lam * isa_mut_returning(Ref d)
Only for mutables.
Definition lam.h:136
Lam * test(Filter filter, const Def *val, const Def *idx, const Def *match, const Def *clash, const Def *mem)
Definition lam.cpp:36
Ref body() const
Definition lam.h:107
Ref dom() const
Definition lam.h:115
static Lam * isa_mut_cn(Ref d)
Only for mutables.
Definition lam.h:134
Lam * app(Filter filter, const Def *callee, const Def *arg)
Set body to an App of callee and arg.
Definition lam.cpp:29
Lam * set_body(const Def *body)
Set body second.
Definition lam.h:160
static const Lam * isa_cn(Ref d)
Definition lam.h:131
static const Lam * isa_basicblock(Ref d)
Definition lam.h:132
Ref ret_dom() const
Definition lam.h:144
Ref ret_var()
Yields the Lam::var of the Lam::ret_pi.
Definition lam.h:146
static const Lam * isa_returning(Ref d)
Definition lam.h:133
A dependent function type.
Definition lam.h:11
static const Pi * isa_returning(Ref d)
Is this a continuation (Pi::isa_cn) which has a Pi::ret_pi?
Definition lam.h:52
Ref codom() const
Definition lam.h:40
static Ref infer(Ref dom, Ref codom)
Definition check.cpp:266
Pi(const Def *type, bool implicit)
Constructor for a *mut*able Pi.
Definition lam.h:17
Pi * stub(Ref type)
Definition lam.h:86
Pi * unset()
Definition lam.h:76
Pi * set_codom(Ref codom)
Definition lam.h:75
Pi * make_implicit()
Definition lam.h:24
Pi(const Def *type, const Def *dom, const Def *codom, bool implicit)
Constructor for an immutable Pi.
Definition lam.h:14
void check() override
Definition check.cpp:271
bool is_implicit() const
Definition lam.h:23
Pi * make_explicit()
Definition lam.h:25
Pi * set(Ref dom, Ref codom)
Definition lam.h:72
Pi * stub_(World &, Ref) override
Definition def.cpp:135
const Pi * immutabilize() override
Tries to make an immutable from a mutable.
Definition def.cpp:163
static const Pi * isa_cn(Ref d)
Is this a continuation - i.e. is the Pi::codom mim::Bottom?
Definition lam.h:50
static const Pi * isa_basicblock(Ref d)
Is this a continuation (Pi::isa_cn) that is not Pi::isa_returning?
Definition lam.h:54
static const Pi * ret_pi(Ref d)
Definition lam.h:62
const Pi * ret_pi() const
Yields the last Pi::dom, if it Pi::isa_basicblock.
Definition lam.cpp:13
Ref dom() const
Definition lam.h:32
Pi * set_dom(Ref dom)
Definition lam.h:73
Ref ret_dom() const
Pi::domain of Pi::ret_pi.
Definition lam.h:66
Helper class to retrieve Infer::arg if present.
Definition def.h:85
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
#define MIM_DEF_MIXIN(T)
Definition def.h:196
#define MIM_PROJ(NAME, CONST)
Use as mixin to wrap all kind of Def::proj and Def::projs variants.
Definition def.h:157
@ Bot
Definition def.h:39
The mem Plugin
Definition mem.h:10
Definition cfg.h:11
GIDSet< Lam * > LamSet
Definition lam.h:189
u64 flags_t
Definition types.h:45
std::pair< const App *, Lam * > isa_apped_mut_lam(const Def *def)
Definition lam.h:238
auto match(Ref def)
Definition axiom.h:105
absl::flat_hash_set< K, GIDHash< K >, GIDEq< K > > GIDSet
Definition util.h:180
LamMap< Lam * > Lam2Lam
Definition lam.h:190
GIDMap< Lam *, To > LamMap
Definition lam.h:188
Lam * isa_workable(Lam *lam)
These are Lams that are neither nullptr, nor Lam::is_external, nor Lam::is_unset.
Definition lam.h:233
std::deque< const App * > decurry(const Def *)
Yields curried Apps in a flat std::deque<const App*>.
Definition lam.cpp:44
absl::flat_hash_map< K, V, GIDHash< K >, GIDEq< K > > GIDMap
Definition util.h:179
const App * isa_callee(const Def *def, size_t i)
Definition lam.h:230
const Def * compose_cn(const Def *f, const Def *g)
The high level view is:
Definition lam.cpp:53
std::pair< const Def *, std::vector< const Def * > > collect_args(const Def *def)
Helper function to cope with the fact that normalizers take all arguments and not only its axiom argu...
Definition lam.cpp:86
uint8_t u8
Definition types.h:34