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