MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
clos.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/world.h"
4
7
8namespace mim::plug::clos {
9
10/// @name Closures
11///@{
12
13/// Lightweight, non-owning view onto a closure literal `(env_type, fn, env)`; see isa_clos_lit.
14class ClosLit {
15public:
16 /// @name Getters
17 ///@{
18 const Sigma* type() const { return def_->type()->isa<Sigma>(); }
19
20 const Def* env() const;
21 const Def* env_type() const { return env()->type(); }
22
23 const Def* fnc() const;
24 const Pi* fnc_type() const { return fnc()->type()->isa<Pi>(); }
25 Lam* fnc_as_lam() const;
26
27 const Def* env_var() const;
28 const Def* ret_var() const { return fnc_as_lam()->ret_var(); }
29 ///@}
30
31 /// @name Predicates
32 ///@{
33 explicit operator bool() const { return def_ != nullptr; }
34 operator const Tuple*() const { return def_; }
35 const Tuple* operator->() const { return def_; }
36
37 bool is_returning() const { return Pi::isa_returning(fnc_type()); }
38 bool is_basicblock() const { return Pi::isa_basicblock(fnc_type()); }
39 ///@}
40
41private:
42 explicit ClosLit(const Tuple* def)
43 : def_(def) {}
44
45 const Tuple* def_;
46
47 friend ClosLit isa_clos_lit(const Def*, bool);
48};
49
50/// Tries to match a closure literal.
51/// If @p fn_isa_lam, additionally requires the code part to be a Lam.
52ClosLit isa_clos_lit(const Def* def, bool fn_isa_lam = true);
53
54/// Pack a typed closure.
55/// This assumes that @p fn expects the environment at its env_param()%th argument.
56const Def* clos_pack(const Def* env, const Def* fn, const Def* ct = nullptr);
57
58/// Deconstruct a closure into `(env_type, function, env)`.
59/// **Important**: use this or ClosLit to destruct closures, since typechecking dependent pairs is currently
60/// broken.
61std::tuple<const Def*, const Def*, const Def*> clos_unpack(const Def* c);
62
63/// Apply a closure to arguments.
64const Def* clos_apply(const Def* closure, const Def* args);
65inline const Def* apply_closure(const Def* closure, Defs args) {
66 return clos_apply(closure, closure->world().tuple(args));
67}
68
69/// If @p def is a projection `var#i` of the Var of some mutable of type @p N, returns `(projection, binder)`.
70/// Otherwise, returns `(nullptr, nullptr)`.
71template<class N>
72std::tuple<const Extract*, N*> isa_var_proj(const Def* def) {
73 if (auto proj = def->isa<Extract>())
74 if (auto var = proj->tuple()->isa<Var>(); var && var->binder()->isa<N>()) return {proj, var->binder()->as<N>()};
75 return {nullptr, nullptr};
76}
77///@}
78
79/// @name Closure Types
80///@{
81/// Returns @p def if @p def is a closure and @c nullptr otherwise
82const Sigma* isa_clos_type(const Def* def);
83
84/// Creates a typed closure type from @p pi.
85Sigma* clos_type(const Pi* pi);
86
87/// Convert a closure type to a Pi, where the environment type has been removed or replaced by @p new_env_type
88/// (if @p new_env_type != @c nullptr)
89const Pi* clos_type_to_pi(const Def* ct, const Def* new_env_type = nullptr);
90
91///@}
92
93/// @name Closure Environment
94///@{
95/// `tup_or_sig` should generally be a Tuple, Sigma or Var.
96
97/// Describes where the environment is placed in the argument list: right after a leading `%mem.M`, if @p doms
98/// starts with one, or in slot 0 otherwise. This way, closures built from mem-free (pure) functions don't gain
99/// a bogus mem-shaped layout, and don't get misaligned with their real parameters (see issue #126).
100inline size_t env_param(Defs doms) { return (!doms.empty() && Axm::isa<mem::M>(doms.front())) ? 1_u64 : 0_u64; }
101inline size_t env_param(const Pi* pi) { return env_param(pi->doms()); }
102
103/// Adjust the index of an argument to account for the env param.
104inline size_t shift_env(size_t ep, size_t i) { return (i < ep) ? i : i - 1_u64; }
105
106/// Same as shift_env, but skips the env param instead.
107inline size_t skip_env(size_t ep, size_t i) { return (i < ep) ? i : i + 1_u64; }
108
109/// Builds a closure type from the domains @p doms of a `Cn`.
110/// If @p env_type is `nullptr`, returns the recursive closure Sigma `[T: *, Cn [doms with T at env_param], T]`.
111/// Otherwise, returns the bare `Cn [doms with env_type at env_param]` (the code part of such a closure).
112const Def* ctype(World& w, Defs doms, const Def* env_type = nullptr);
113
114const Def* clos_insert_env(size_t ep, size_t i, const Def* env, std::function<const Def*(size_t)> f);
115inline const Def* clos_insert_env(size_t ep, size_t i, const Def* env, const Def* a) {
116 return clos_insert_env(ep, i, env, [&](auto i) { return a->proj(i); });
117}
118
119inline const Def* clos_insert_env(size_t ep, const Def* env, const Def* tup_or_sig) {
120 auto& w = tup_or_sig->world();
121 auto new_ops = DefVec(tup_or_sig->num_projs() + 1, [&](auto i) { return clos_insert_env(ep, i, env, tup_or_sig); });
122 return (tup_or_sig->isa<Sigma>()) ? w.sigma(new_ops) : w.tuple(new_ops);
123}
124
125const Def* clos_remove_env(size_t ep, size_t i, std::function<const Def*(size_t)> f);
126inline const Def* clos_remove_env(size_t ep, size_t i, const Def* def) {
127 return clos_remove_env(ep, i, [&](auto i) { return def->proj(i); });
128}
129inline const Def* clos_remove_env(size_t ep, const Def* tup_or_sig) {
130 auto& w = tup_or_sig->world();
131 auto new_ops = DefVec(tup_or_sig->num_projs() - 1, [&](auto i) { return clos_remove_env(ep, i, tup_or_sig); });
132 return (tup_or_sig->isa<Sigma>()) ? w.sigma(new_ops) : w.tuple(new_ops);
133}
134
135inline const Def* clos_sub_env(size_t ep, const Def* tup_or_sig, const Def* new_env) {
136 return tup_or_sig->refine(ep, new_env);
137}
138///@}
139
140} // namespace mim::plug::clos
static auto isa(const Def *def)
Definition axm.h:107
Base class for all Defs.
Definition def.h:261
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:635
World & world() const noexcept
Definition def.cpp:483
const Def * refine(size_t i, const Def *new_op) const
Definition def.cpp:242
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:491
nat_t num_projs() const
Yields Def::arity(), if it is a Lit, or 1 otherwise.
Definition def.cpp:628
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:210
A function.
Definition lam.h:110
const Def * ret_var()
Yields the Lam::var of the Lam::ret_pi.
Definition lam.h:156
A dependent function type.
Definition lam.h:14
static const Pi * isa_basicblock(const Def *d)
Is this a continuation (Pi::isa_cn) that is not Pi::isa_returning?
Definition lam.h:51
static const Pi * isa_returning(const Def *d)
Is this a continuation (Pi::isa_cn) which has a Pi::ret_pi?
Definition lam.h:49
A dependent tuple type.
Definition tuple.h:22
Data constructor for a Sigma.
Definition tuple.h:70
A variable introduced by a binder (mutable).
Definition def.h:756
Def * binder() const
The binder of this Var.
Definition def.h:766
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
const Def * tuple(Defs ops)
Definition world.cpp:308
Lightweight, non-owning view onto a closure literal (env_type, fn, env); see isa_clos_lit.
Definition clos.h:14
const Sigma * type() const
Definition clos.h:18
const Def * ret_var() const
Definition clos.h:28
const Def * env_type() const
Definition clos.h:21
const Def * fnc() const
Definition clos.cpp:37
const Pi * fnc_type() const
Definition clos.h:24
Lam * fnc_as_lam() const
Definition clos.cpp:39
const Tuple * operator->() const
Definition clos.h:35
bool is_basicblock() const
Definition clos.h:38
const Def * env_var() const
Definition clos.cpp:45
bool is_returning() const
Definition clos.h:37
friend ClosLit isa_clos_lit(const Def *, bool)
Tries to match a closure literal.
Definition clos.cpp:50
const Def * env() const
Definition clos.cpp:35
The clos Plugin
Definition clos.h:8
ClosLit isa_clos_lit(const Def *def, bool fn_isa_lam=true)
Tries to match a closure literal.
Definition clos.cpp:50
const Def * clos_sub_env(size_t ep, const Def *tup_or_sig, const Def *new_env)
Definition clos.h:135
const Def * clos_remove_env(size_t ep, size_t i, std::function< const Def *(size_t)> f)
Definition clos.cpp:122
Sigma * clos_type(const Pi *pi)
Creates a typed closure type from pi.
Definition clos.cpp:99
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:104
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:124
const Def * clos_insert_env(size_t ep, size_t i, const Def *env, std::function< const Def *(size_t)> f)
Definition clos.cpp:118
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:105
const Def * apply_closure(const Def *closure, Defs args)
Definition clos.h:65
std::tuple< const Def *, const Def *, const Def * > clos_unpack(const Def *c)
Deconstruct a closure into (env_type, function, env).
Definition clos.cpp:70
std::tuple< const Extract *, N * > isa_var_proj(const Def *def)
If def is a projection var#i of the Var of some mutable of type N, returns (projection,...
Definition clos.h:72
size_t skip_env(size_t ep, size_t i)
Same as shift_env, but skips the env param instead.
Definition clos.h:107
const Def * clos_pack(const Def *env, const Def *fn, const Def *ct=nullptr)
Pack a typed closure.
Definition clos.cpp:59
const Def * clos_apply(const Def *closure, const Def *args)
Apply a closure to arguments.
Definition clos.cpp:76
const Sigma * isa_clos_type(const Def *def)
Definition clos.cpp:88
size_t env_param(Defs doms)
Describes where the environment is placed in the argument list: right after a leading mem....
Definition clos.h:100
View< const Def * > Defs
Definition def.h:78
Vector< const Def * > DefVec
Definition def.h:79