MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
clos.cpp
Go to the documentation of this file.
2
3#include <mim/config.h>
4
5#include <mim/pass/eta_exp.h>
6#include <mim/pass/eta_red.h>
7#include <mim/pass/pass.h>
10
17
18using namespace mim;
19using namespace mim::plug;
20
22 return {"clos", [](Normalizers& normalizers) { clos::register_normalizers(normalizers); },
23 [](Passes& passes) {
30 // TODO:; remove after ho_codegen merge
32 = [&](World&, PipelineBuilder& builder, const Def* app) {
33 auto bb = app->as<App>()->arg();
34 auto bb_only = bb->as<Lit>()->get<u64>();
35 builder.add_pass<EtaRed>(app, bb_only);
36 };
37 },
38 nullptr};
39}
40
41namespace mim::plug::clos {
42
43/*
44 * ClosLit
45 */
46
47const Def* ClosLit::env() {
48 assert(def_);
49 return std::get<2_u64>(clos_unpack(def_));
50}
51
52const Def* ClosLit::fnc() {
53 assert(def_);
54 return std::get<1_u64>(clos_unpack(def_));
55}
56
58 auto f = fnc();
59 if (auto a = match<attr>(f)) f = a->arg();
60 return f->isa_mut<Lam>();
61}
62
64
65ClosLit isa_clos_lit(const Def* def, bool lambda_or_branch) {
66 auto tpl = def->isa<Tuple>();
67 if (tpl && isa_clos_type(def->type())) {
68 auto a = attr::bottom;
69 auto fnc = std::get<1_u64>(clos_unpack(tpl));
70 if (auto fa = match<attr>(fnc)) {
71 fnc = fa->arg();
72 a = fa.id();
73 }
74 if (!lambda_or_branch || fnc->isa<Lam>()) return ClosLit(tpl, a);
75 }
76 return ClosLit(nullptr, attr::bottom);
77}
78
79const Def* clos_pack(const Def* env, const Def* lam, const Def* ct) {
80 assert(env && lam);
81 assert(!ct || isa_clos_type(ct));
82 auto& w = env->world();
83 auto pi = lam->type()->as<Pi>();
84 assert(env->type() == pi->dom(Clos_Env_Param));
85 ct = (ct) ? ct : clos_type(w.cn(clos_remove_env(pi->dom())));
86 return w.tuple(ct, {env->type(), lam, env})->isa<Tuple>();
87}
88
89std::tuple<const Def*, const Def*, const Def*> clos_unpack(const Def* c) {
90 assert(c && isa_clos_type(c->type()));
91 // auto& w = c->world();
92 // auto env_type = c->proj(0_u64);
93 // // auto pi = clos_type_to_pi(c->type(), env_type);
94 // auto fn = w.extract(c, w.lit_idx(3, 1));
95 // auto env = w.extract(c, w.lit_idx(3, 2));
96 // return {env_type, fn, env};
97 auto [ty, pi, env] = c->projs<3>();
98 return {ty, pi, env};
99}
100
101const Def* clos_apply(const Def* closure, const Def* args) {
102 auto& w = closure->world();
103 auto [_, fn, env] = clos_unpack(closure);
104 auto pi = fn->type()->as<Pi>();
105 return w.app(fn, DefVec(pi->num_doms(), [&](auto i) { return clos_insert_env(i, env, args); }));
106}
107
108/*
109 * closure types
110 */
111
112const Sigma* isa_clos_type(const Def* def) {
113 auto& w = def->world();
114 auto sig = def->isa_mut<Sigma>();
115 if (!sig || sig->num_ops() < 3 || sig->op(0_u64) != w.type()) return nullptr;
116 auto var = sig->var(0_u64);
117 if (sig->op(2_u64) != var) return nullptr;
118 auto pi = sig->op(1_u64)->isa<Pi>();
119 return (pi && Pi::isa_cn(pi) && pi->num_ops() > 1_u64 && pi->dom(Clos_Env_Param) == var) ? sig : nullptr;
120}
121
122Sigma* clos_type(const Pi* pi) {
123 auto& w = pi->world();
124 auto doms = pi->doms();
125 return ctype(w, doms, nullptr)->as_mut<Sigma>();
126}
127
128const Pi* clos_type_to_pi(const Def* ct, const Def* new_env_type) {
129 assert(isa_clos_type(ct));
130 auto& w = ct->world();
131 auto pi = ct->op(1_u64)->as<Pi>();
132 auto new_dom = new_env_type ? clos_sub_env(pi->dom(), new_env_type) : clos_remove_env(pi->dom());
133 return w.cn(new_dom);
134}
135
136/*
137 * closure environments
138 */
139
140const Def* clos_insert_env(size_t i, const Def* env, std::function<const Def*(size_t)> f) {
141 return (i == Clos_Env_Param) ? env : f(shift_env(i));
142}
143
144const Def* clos_remove_env(size_t i, std::function<const Def*(size_t)> f) { return f(skip_env(i)); }
145
146const Def* ctype(World& w, Defs doms, const Def* env_type) {
147 if (!env_type) {
148 auto sigma = w.mut_sigma(w.type(), 3_u64)->set("Clos");
149 sigma->set(0_u64, w.type());
150 sigma->set(1_u64, ctype(w, doms, sigma->var(0_u64)));
151 sigma->set(2_u64, sigma->var(0_u64));
152 return sigma;
153 }
154 return w.cn(
155 DefVec(doms.size() + 1, [&](auto i) { return clos_insert_env(i, env_type, [&](auto j) { return doms[j]; }); }));
156}
157
158} // namespace mim::plug::clos
Base class for all Defs.
Definition def.h:198
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition def.h:438
World & world() const noexcept
Definition def.cpp:413
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:430
const Def * op(size_t i) const noexcept
Definition def.h:264
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:379
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:242
Performs η-reduction.
Definition eta_red.h:9
A function.
Definition lam.h:105
A dependent function type.
Definition lam.h:11
static const Pi * isa_cn(const Def *d)
Is this a continuation - i.e. is the Pi::codom mim::Bottom?
Definition lam.h:44
A dependent tuple type.
Definition tuple.h:9
Data constructor for a Sigma.
Definition tuple.h:50
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
const Def * fnc()
Definition clos.cpp:52
const Def * env_var()
Definition clos.cpp:63
const Def * env()
Definition clos.cpp:47
#define MIM_EXPORT
Definition config.h:16
The clos Plugin
Definition clos.h:7
ClosLit isa_clos_lit(const Def *def, bool fn_isa_lam=true)
Tries to match a closure literal.
Definition clos.cpp:65
void register_normalizers(Normalizers &normalizers)
Sigma * clos_type(const Pi *pi)
Creates a typed closure type from pi.
Definition clos.cpp:122
size_t shift_env(size_t i)
Definition clos.h:110
const Def * clos_remove_env(size_t i, std::function< const Def *(size_t)> f)
Definition clos.cpp:144
const Def * clos_insert_env(size_t i, const Def *env, std::function< const Def *(size_t)> f)
Definition clos.cpp:140
const Def * ctype(World &w, Defs doms, const Def *env_type=nullptr)
Definition clos.cpp:146
static constexpr size_t Clos_Env_Param
Describes where the environment is placed in the argument list.
Definition clos.h:107
const Pi * clos_type_to_pi(const Def *ct, const Def *new_env_type=nullptr)
Convert a closure type to a Pi, where the environment type has been removed or replaced by new_env_ty...
Definition clos.cpp:128
std::tuple< const Def *, const Def *, const Def * > clos_unpack(const Def *c)
Deconstruct a closure into (env_type, function, env).
Definition clos.cpp:89
const Def * clos_sub_env(const Def *tup_or_sig, const Def *new_env)
Definition clos.h:139
size_t skip_env(size_t i)
Definition clos.h:113
const Def * clos_pack(const Def *env, const Def *fn, const Def *ct=nullptr)
Pack a typed closure. This assumes that fn expects the environment as its Clos_Env_Paramth argument.
Definition clos.cpp:79
const Def * clos_apply(const Def *closure, const Def *args)
Apply a closure to arguments.
Definition clos.cpp:101
const Sigma * isa_clos_type(const Def *def)
Definition clos.cpp:112
Definition ast.h:14
View< const Def * > Defs
Definition def.h:49
Vector< const Def * > DefVec
Definition def.h:50
u64 flags_t
Definition types.h:45
absl::flat_hash_map< flags_t, std::function< void(World &, PipelineBuilder &, const Def *)> > Passes
axiom ↦ (pipeline part) × (axiom application) → () The function should inspect Application to const...
Definition plugin.h:22
mim::Plugin mim_get_plugin()
void register_pass(Passes &passes, CArgs &&... args)
constexpr decltype(auto) get(Span< T, N > span) noexcept
Definition span.h:102
void register_phase(Passes &passes, CArgs &&... args)
auto match(const Def *def)
Definition axiom.h:112
absl::flat_hash_map< flags_t, NormalizeFn > Normalizers
Definition plugin.h:19
static constexpr flags_t Base
Definition plugin.h:118
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:29