MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lam.cpp
Go to the documentation of this file.
1#include "mim/lam.h"
2
3#include "mim/world.h"
4
5using namespace std::string_literals;
6
7namespace mim {
8
9/*
10 * Pi
11 */
12
13const Pi* Pi::ret_pi() const {
14 if (num_doms() > 0) {
15 auto ret = dom(num_doms() - 1);
16 if (auto pi = Pi::isa_basicblock(ret)) return pi;
17 }
18
19 return nullptr;
20}
21
22Pi* Pi::set_dom(Defs doms) { return Def::set(0, world().sigma(doms))->as<Pi>(); }
23
24/*
25 * Lam
26 */
27
28Lam* Lam::set_filter(Filter filter) { return Def::set(0, world().filter(filter))->as<Lam>(); }
29Lam* Lam::set(Filter filter, const Def* body) { return Def::set({world().filter(filter), body})->as<Lam>(); }
30Lam* Lam::app(Filter f, const Def* callee, const Def* arg) { return set_filter(f)->set_body(world().app(callee, arg)); }
31Lam* Lam::app(Filter filter, const Def* callee, Defs args) { return app(filter, callee, world().tuple(args)); }
32
33Lam* Lam::branch(Filter filter, const Def* cond, const Def* t, const Def* f, const Def* mem) {
34 return app(filter, world().select(cond, t, f), mem ? mem : world().tuple());
35}
36
37// TODO maybe we can eta-reduce immutable Lams in some edge casess like: lm _: [] = f ();
38
39const Def* Lam::eta_reduce() const {
40 if (auto var = has_var()) {
41 if (auto app = body()->isa<App>())
42 if (app->arg() == var && !app->callee()->free_vars().contains(var)) return app->callee();
43 }
44 return nullptr;
45}
46
47const Def* Lam::eta_expand(Filter filter, const Def* f) {
48 auto& w = f->world();
49 auto eta = w.mut_lam(f->type()->as<Pi>());
50 eta->debug_suffix("eta_"s + f->sym().str());
51 return eta->app(filter, f, eta->var());
52}
53
54/*
55 * Helpers
56 */
57
58const Def* compose_cn(const Def* f, const Def* g) {
59 auto& world = f->world();
60 world.DLOG("compose f (B->C): {} : {}", f, f->type());
61 world.DLOG("compose g (A->B): {} : {}", g, g->type());
62
63 auto F = f->type()->as<Pi>();
64 auto G = g->type()->as<Pi>();
65
66 assert(Pi::isa_returning(F));
67 assert(Pi::isa_returning(G));
68
69 auto A = G->dom(2, 0);
70 auto B = G->ret_dom();
71 auto C = F->ret_dom();
72 // The type check of codom G = dom F is better handled by the application type checking
73
74 world.DLOG("compose f (B->C): {} : {}", f, F);
75 world.DLOG("compose g (A->B): {} : {}", g, G);
76 world.DLOG(" A: {}", A);
77 world.DLOG(" B: {}", B);
78 world.DLOG(" C: {}", C);
79
80 auto h = world.mut_fun(A, C)->set("comp_"s + f->sym().str() + "_"s + g->sym().str());
81 auto hcont = world.mut_con(B)->set("comp_"s + f->sym().str() + "_"s + g->sym().str() + "_cont"s);
82
83 h->app(true, g, {h->var((nat_t)0), hcont});
84
85 auto hcont_var = hcont->var(); // Warning: not var(0) => only one var => normalization flattens tuples down here.
86 hcont->app(true, f, {hcont_var, h->var(1) /* ret_var */});
87
88 return h;
89}
90
91} // namespace mim
Base class for all Defs.
Definition def.h:251
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:266
World & world() const noexcept
Definition def.cpp:436
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:429
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Definition def.h:433
std::variant< bool, const Def * > Filter
Definition lam.h:117
const Def * filter() const
Definition lam.h:121
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:33
Lam * set(Filter filter, const Def *body)
Definition lam.cpp:29
Lam * set_filter(Filter)
Set filter first.
Definition lam.cpp:28
const Def * eta_reduce() const
Yields body(), if eta-convertible and nullptr otherwise.
Definition lam.cpp:39
Lam * app(Filter filter, const Def *callee, const Def *arg)
Set body to an App of callee and arg.
Definition lam.cpp:30
Lam * set_body(const Def *body)
Set body second.
Definition lam.h:170
const Def * body() const
Definition lam.h:122
static const Def * eta_expand(Filter, const Def *f)
Definition lam.cpp:47
A dependent function type.
Definition lam.h:13
const Def * ret_dom() const
Pi::domain of Pi::ret_pi.
Definition lam.h:68
Pi(const Def *type, const Def *dom, const Def *codom, bool implicit)
Constructor for an immutable Pi.
Definition lam.h:16
static const Pi * isa_basicblock(const Def *d)
Is this a continuation (Pi::isa_cn) that is not Pi::isa_returning?
Definition lam.h:50
const Def * dom() const
Definition lam.h:34
const Pi * ret_pi() const
Yields the last Pi::dom, if Pi::isa_basicblock.
Definition lam.cpp:13
Pi * set_dom(const Def *dom)
Definition lam.h:76
static const Pi * isa_returning(const Def *d)
Is this a continuation (Pi::isa_cn) which has a Pi::ret_pi?
Definition lam.h:48
const Def * filter(Lam::Filter filter)
Definition world.h:288
Definition ast.h:14
View< const Def * > Defs
Definition def.h:76
u64 nat_t
Definition types.h:43
const Def * compose_cn(const Def *f, const Def *g)
The high level view is:
Definition lam.cpp:58