MimIR
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#include <mim/phase.h>
5
12
13using namespace mim;
14using namespace mim::plug;
15
26
28 plugin.register_normalizers = clos::register_normalizers;
29 plugin.register_phases = reg_phases;
30}
31
32namespace mim::plug::clos {
33
34/*
35 * ClosLit
36 */
37
38const Def* ClosLit::env() const { return std::get<2>(clos_unpack(def_)); }
39
40const Def* ClosLit::fnc() const { return std::get<1>(clos_unpack(def_)); }
41
43
44const Def* ClosLit::env_var() const {
45 auto lam = fnc_as_lam();
46 return lam->var(lam->num_vars(), env_param(lam->type()->as<Pi>()));
47}
48
49ClosLit isa_clos_lit(const Def* def, bool fn_isa_lam) {
50 if (auto tpl = def->isa<Tuple>(); tpl && isa_clos_type(def->type())) {
51 auto fnc = Axm::peel<anno>(std::get<1>(clos_unpack(tpl)));
52 if (!fn_isa_lam || fnc->isa<Lam>()) return ClosLit(tpl);
53 }
54 return ClosLit(nullptr);
55}
56
57const Def* clos_pack(const Def* env, const Def* fn, const Def* ct) {
58 assert(env && fn);
59 assert(!ct || isa_clos_type(ct));
60 auto& w = env->world();
61 auto pi = fn->type()->as<Pi>();
62 auto ep = env_param(pi);
63 assert(env->type() == pi->dom(pi->num_doms(), ep));
64 ct = ct ? ct : clos_type(w.cn(clos_remove_env(ep, pi->dom())));
65 return w.tuple(ct, {env->type(), fn, env})->as<Tuple>();
66}
67
68std::tuple<const Def*, const Def*, const Def*> clos_unpack(const Def* c) {
69 assert(c && isa_clos_type(c->type()));
70 auto [env_type, fn, env] = c->projs<3>();
71 return {env_type, fn, env};
72}
73
74const Def* clos_apply(const Def* closure, const Def* args) {
75 auto& w = closure->world();
76 auto [_, fn, env] = clos_unpack(closure);
77 auto pi = fn->type()->as<Pi>();
78 auto ep = env_param(pi);
79 return w.app(fn, DefVec(pi->num_doms(), [&](auto i) { return clos_insert_env(ep, i, env, args); }));
80}
81
82/*
83 * closure types
84 */
85
86const Sigma* isa_clos_type(const Def* def) {
87 auto& w = def->world();
88 auto sig = def->isa_mut<Sigma>();
89 if (!sig || sig->num_ops() < 3 || sig->op(0_u64) != w.type()) return nullptr;
90 auto var = sig->var(sig->num_ops(), 0);
91 if (sig->op(2_u64) != var) return nullptr;
92 auto pi = sig->op(1_u64)->isa<Pi>();
93 if (!pi || !Pi::isa_cn(pi) || pi->num_ops() <= 1_u64) return nullptr;
94 return (pi->dom(pi->num_doms(), env_param(pi)) == var) ? sig : nullptr;
95}
96
97Sigma* clos_type(const Pi* pi) {
98 auto& w = pi->world();
99 auto doms = pi->doms();
100 return ctype(w, doms, nullptr)->as_mut<Sigma>();
101}
102
103const Pi* clos_type_to_pi(const Def* ct, const Def* new_env_type) {
104 assert(isa_clos_type(ct));
105 auto& w = ct->world();
106 auto pi = ct->op(1_u64)->as<Pi>();
107 auto ep = env_param(pi);
108 auto new_dom = new_env_type ? clos_sub_env(ep, pi->dom(), new_env_type) : clos_remove_env(ep, pi->dom());
109 return w.cn(new_dom);
110}
111
112/*
113 * closure environments
114 */
115
116const Def* clos_insert_env(size_t ep, size_t i, const Def* env, std::function<const Def*(size_t)> f) {
117 return (i == ep) ? env : f(shift_env(ep, i));
118}
119
120const Def* clos_remove_env(size_t ep, size_t i, std::function<const Def*(size_t)> f) { return f(skip_env(ep, i)); }
121
122const Def* ctype(World& w, Defs doms, const Def* env_type) {
123 auto ep = env_param(doms);
124 if (!env_type) {
125 auto sigma = w.mut_sigma(w.type(), 3_u64)->set("Clos");
126 sigma->set(0_u64, w.type());
127 sigma->set(1_u64, ctype(w, doms, sigma->var(3, 0)));
128 sigma->set(2_u64, sigma->var(3, 0));
129 return sigma;
130 }
131 return w.cn(DefVec(doms.size() + 1,
132 [&](auto i) { return clos_insert_env(ep, i, env_type, [&](auto j) { return doms[j]; }); }));
133}
134
135} // namespace mim::plug::clos
static const Def * peel(const Def *def)
Peel off all Id Axms from def.
Definition axm.h:149
Base class for all Defs.
Definition def.h:289
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition def.h:597
World & world() const noexcept
Definition def.h:1105
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:588
const Def * op(size_t i) const noexcept
Definition def.h:377
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:492
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1119
A function.
Definition lam.h:91
static void hook(Flags2Phases &phases)
Definition phase.h:51
A dependent function type.
Definition lam.h:14
auto doms(Projector auto f) const noexcept
Definition lam.h:40
static const Pi * isa_cn(const Def *d)
Definition lam.h:50
const Def * dom() const
Definition lam.h:38
A dependent tuple type.
Definition tuple.h:39
Data constructor for a Sigma.
Definition tuple.h:84
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
const Def * fnc() const
Definition clos.cpp:40
Lam * fnc_as_lam() const
Definition clos.cpp:42
const Def * env_var() const
Definition clos.cpp:44
friend ClosLit isa_clos_lit(const Def *, bool)
Tries to match a closure literal.
Definition clos.cpp:49
const Def * env() const
Definition clos.cpp:38
static void reg_phases(Flags2Phases &phases)
Definition clos.cpp:16
The clos Plugin
Definition clos.h:9
void register_normalizers(Normalizers &normalizers)
const Def * clos_sub_env(size_t ep, const Def *tup_or_sig, const Def *new_env)
Definition clos.h:142
const Def * clos_remove_env(size_t ep, size_t i, std::function< const Def *(size_t)> f)
Definition clos.cpp:120
Sigma * clos_type(const Pi *pi)
Creates a typed closure type from pi.
Definition clos.cpp:97
size_t shift_env(size_t ep, size_t i)
Adjust the index of an argument to account for the env param.
Definition clos.h:111
const Def * ctype(World &w, Defs doms, const Def *env_type=nullptr)
Builds a closure type from the domains doms of a Cn.
Definition clos.cpp:122
const Def * clos_insert_env(size_t ep, size_t i, const Def *env, std::function< const Def *(size_t)> f)
Definition clos.cpp:116
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:103
std::tuple< const Def *, const Def *, const Def * > clos_unpack(const Def *c)
Deconstruct a closure into (env_type, function, env).
Definition clos.cpp:68
size_t skip_env(size_t ep, size_t i)
Same as shift_env, but skips the env param instead.
Definition clos.h:114
const Def * clos_pack(const Def *env, const Def *fn, const Def *ct=nullptr)
Pack a typed closure.
Definition clos.cpp:57
const Def * clos_apply(const Def *closure, const Def *args)
Apply a closure to arguments.
Definition clos.cpp:74
const Sigma * isa_clos_type(const Def *def)
Definition clos.cpp:86
size_t env_param(Defs doms)
Describes where the environment is placed in the argument list: right after a leading mem....
Definition clos.h:107
Definition ast.h:16
fe::View< const Def * > Defs
Definition def.h:96
fe::Vector< const Def * > DefVec
Definition def.h:98
ankerl::unordered_dense::map< flags_t, std::function< std::unique_ptr< Phase >(World &)> > Flags2Phases
Maps an axiom of a Phase to a function that creates one.
Definition plugin.h:30
#define MIM_PLUGIN_ENTRY(p)
Defines a Plugin's entry point; the body fills in the plugin handed to it, as in MIM_PLUGIN_ENTRY(dem...
Definition plugin.h:196