Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
clos.h
Go to the documentation of this file.
1#pragma once
2
3#include "thorin/world.h"
4
6
8
9/// @name Closures
10///@{
11
12/// Wrapper around a Def that can be used to match closures (see isa_clos_lit).
13class ClosLit {
14public:
15 /// @name Getters
16 ///@{
17 const Sigma* type() {
18 assert(def_);
19 return def_->type()->isa<Sigma>();
20 }
21
22 Ref env();
23 Ref env_type() { return env()->type(); }
24
25 Ref fnc();
26 const Pi* fnc_type() { return fnc()->type()->isa<Pi>(); }
27 Lam* fnc_as_lam();
28
29 Ref env_var();
30 Ref ret_var() { return fnc_as_lam()->ret_var(); }
31 ///@}
32
33 explicit operator bool() const { return def_ != nullptr; }
34 operator const Tuple*() { return def_; }
35
36 const Tuple* operator->() {
37 assert(def_);
38 return def_;
39 }
40
41 /// @name Properties
42 ///@{
45 attr get() { return attr_; } ///< Clos annotation. These should appear in front of the code-part.
46 ///@}
47
48private:
49 ClosLit(const Tuple* def, attr a = attr::bot)
50 : def_(def)
51 , attr_(a) {}
52
53 const Tuple* def_;
54 const attr attr_;
55
56 friend ClosLit isa_clos_lit(Ref, bool);
57};
58
59/// Tries to match a closure literal.
60ClosLit isa_clos_lit(Ref def, bool fn_isa_lam = true);
61
62/// Pack a typed closure. This assumes that @p fn expects the environment as its Clos_Env_Param%th argument.
63Ref clos_pack(Ref env, Ref fn, Ref ct = nullptr);
64
65/// Deconstruct a closure into `(env_type, function, env)`.
66/// **Important**: use this or ClosLit to destruct closures, since typechecking dependent pairs is currently
67/// broken.
68std::tuple<Ref, Ref, Ref> clos_unpack(Ref c);
69
70/// Apply a closure to arguments.
71Ref clos_apply(Ref closure, Ref args);
72inline Ref apply_closure(Ref closure, Defs args) {
73 auto& w = closure->world();
74 return clos_apply(closure, w.tuple(args));
75}
76
77// TODO: rename this
78/// Checks is def is the variable of a mut of type N.
79template<class N> std::tuple<const Extract*, N*> ca_isa_var(Ref def) {
80 if (auto proj = def->isa<Extract>()) {
81 if (auto var = proj->tuple()->isa<Var>(); var && var->mut()->isa<N>())
82 return std::tuple(proj, var->mut()->as<N>());
83 }
84 return {nullptr, nullptr};
85}
86///@}
87
88/// @name Closure Types
89///@{
90
91/// Returns @p def if @p def is a closure and @c nullptr otherwise
92const Sigma* isa_clos_type(Ref def);
93
94/// Creates a typed closure type from @p pi.
95Sigma* clos_type(const Pi* pi);
96
97/// Convert a closure type to a Pi, where the environment type has been removed or replaced by @p new_env_type
98/// (if @p new_env_type != @c nullptr)
99const Pi* clos_type_to_pi(Ref ct, Ref new_env_type = nullptr);
100
101///@}
102
103/// @name Closure Environment
104///@{
105/// `tup_or_sig` should generally be a Tuple, Sigma or Var.
106
107/// Describes where the environment is placed in the argument list.
108static constexpr size_t Clos_Env_Param = 1_u64;
109
110// Adjust the index of an argument to account for the env param
111inline size_t shift_env(size_t i) { return (i < Clos_Env_Param) ? i : i - 1_u64; }
112
113// Same but skip the env param
114inline size_t skip_env(size_t i) { return (i < Clos_Env_Param) ? i : i + 1_u64; }
115
116// TODO what does this do exactly?
117Ref ctype(World& w, Defs doms, Ref env_type = nullptr);
118
119Ref clos_insert_env(size_t i, Ref env, std::function<Ref(size_t)> f);
120inline Ref clos_insert_env(size_t i, Ref env, Ref a) {
121 return clos_insert_env(i, env, [&](auto i) { return a->proj(i); });
122}
123
124inline Ref clos_insert_env(Ref env, Ref tup_or_sig) {
125 auto& w = tup_or_sig->world();
126 auto new_ops = DefVec(tup_or_sig->num_projs() + 1, [&](auto i) { return clos_insert_env(i, env, tup_or_sig); });
127 return (tup_or_sig->isa<Sigma>()) ? w.sigma(new_ops) : w.tuple(new_ops);
128}
129
130Ref clos_remove_env(size_t i, std::function<Ref(size_t)> f);
131inline Ref clos_remove_env(size_t i, Ref def) {
132 return clos_remove_env(i, [&](auto i) { return def->proj(i); });
133}
134inline Ref clos_remove_env(Ref tup_or_sig) {
135 auto& w = tup_or_sig->world();
136 auto new_ops = DefVec(tup_or_sig->num_projs() - 1, [&](auto i) { return clos_remove_env(i, tup_or_sig); });
137 return (tup_or_sig->isa<Sigma>()) ? w.sigma(new_ops) : w.tuple(new_ops);
138}
139
140inline Ref clos_sub_env(Ref tup_or_sig, Ref new_env) { return tup_or_sig->refine(Clos_Env_Param, new_env); }
141///@}
142
143} // namespace thorin::plug::clos
const Def * refine(size_t i, const Def *new_op) const
Definition def.cpp:226
const Def * type() const
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:248
nat_t num_projs() const
Yields Def::as_lit_arity(), if it is in fact a Lit, or 1 otherwise.
Definition def.h:358
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:531
World & world() const
Definition def.cpp:421
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:118
A function.
Definition lam.h:97
Ref ret_var()
Yields the Lam::var of the Lam::ret_pi.
Definition lam.h:147
A dependent function type.
Definition lam.h:11
static const Pi * isa_returning(Ref d)
Is this a continuation (Pi::isa_cn) which has a Pi::ret_pi?
Definition lam.h:52
static const Pi * isa_basicblock(Ref d)
Is this a continuation (Pi::isa_cn) that is not Pi::isa_returning?
Definition lam.h:54
Helper class to retrieve Infer::arg if present.
Definition def.h:87
A dependent tuple type.
Definition tuple.h:9
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
Data constructor for a Sigma.
Definition tuple.h:39
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
Wrapper around a Def that can be used to match closures (see isa_clos_lit).
Definition clos.h:13
attr get()
Clos annotation. These should appear in front of the code-part.
Definition clos.h:45
const Pi * fnc_type()
Definition clos.h:26
const Tuple * operator->()
Definition clos.h:36
const Sigma * type()
Definition clos.h:17
friend ClosLit isa_clos_lit(Ref, bool)
Tries to match a closure literal.
Definition clos.cpp:64
The clos Plugin
Definition clos.h:7
Ref clos_insert_env(size_t i, Ref env, std::function< Ref(size_t)> f)
Definition clos.cpp:135
Ref clos_pack(Ref env, Ref fn, Ref ct=nullptr)
Pack a typed closure. This assumes that fn expects the environment as its Clos_Env_Paramth argument.
Definition clos.cpp:78
std::tuple< const Extract *, N * > ca_isa_var(Ref def)
Checks is def is the variable of a mut of type N.
Definition clos.h:79
static constexpr size_t Clos_Env_Param
Describes where the environment is placed in the argument list.
Definition clos.h:108
Ref clos_apply(Ref closure, Ref args)
Apply a closure to arguments.
Definition clos.cpp:100
const Sigma * isa_clos_type(Ref def)
Returns def if def is a closure and nullptr otherwise.
Definition clos.cpp:111
Sigma * clos_type(const Pi *pi)
Creates a typed closure type from pi.
Definition clos.cpp:121
ClosLit isa_clos_lit(Ref def, bool fn_isa_lam=true)
Tries to match a closure literal.
Definition clos.cpp:64
size_t skip_env(size_t i)
Definition clos.h:114
Ref clos_remove_env(size_t i, std::function< Ref(size_t)> f)
Definition clos.cpp:139
std::tuple< Ref, Ref, Ref > clos_unpack(Ref c)
Deconstruct a closure into (env_type, function, env).
Definition clos.cpp:88
const Pi * clos_type_to_pi(Ref ct, Ref 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:123
Ref clos_sub_env(Ref tup_or_sig, Ref new_env)
Definition clos.h:140
Ref ctype(World &w, Defs doms, Ref env_type=nullptr)
Definition clos.cpp:141
Ref apply_closure(Ref closure, Defs args)
Definition clos.h:72
size_t shift_env(size_t i)
Definition clos.h:111
Vector< const Def * > DefVec
Definition def.h:63