Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
tuple.h
Go to the documentation of this file.
1#pragma once
2
3#include "thorin/def.h"
4
5namespace thorin {
6
7/// A [dependent tuple type](https://en.wikipedia.org/wiki/Dependent_type#%CE%A3_type).
8/// @see Tuple, Arr, Pack
9class Sigma : public Def {
10private:
11 Sigma(const Def* type, Defs ops)
12 : Def(Node, type, ops, 0) {} ///< Constructor for an *immutable* Sigma.
13 Sigma(const Def* type, size_t size)
14 : Def(Node, type, size, 0) {} ///< Constructor for a *mutable* Sigma.
15
16public:
17 /// @name Setters
18 ///@{
19 /// @see @ref set_ops "Setting Ops"
20 Sigma* set(size_t i, const Def* def) { return Def::set(i, def)->as<Sigma>(); }
21 Sigma* set(Defs ops) { return Def::set(ops)->as<Sigma>(); }
22 Sigma* unset() { return Def::unset()->as<Sigma>(); }
23 ///@}
24
25 const Def* immutabilize() override;
26 Sigma* stub_(World&, Ref) override;
27 Sigma* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
28
29 /// @name Type Checking
30 ///@{
31 void check() override;
32 ///@}
33
35};
36
37/// Data constructor for a Sigma.
38/// @see Sigma, Arr, Pack
39class Tuple : public Def {
40private:
41 Tuple(const Def* type, Defs args)
42 : Def(Node, type, args, 0) {}
43
45};
46
47/// A (possibly paramterized) Arr%ay.
48/// Arr%ays are usually homogenous but they can be *inhomogenous* as well: `«i: N; T#i»`
49/// @see Sigma, Tuple, Pack
50class Arr : public Def {
51private:
52 Arr(const Def* type, const Def* shape, const Def* body)
53 : Def(Node, type, {shape, body}, 0) {} ///< Constructor for an *immutable* Arr.
54 Arr(const Def* type)
55 : Def(Node, type, 2, 0) {} ///< Constructor for a *mut*able Arr.
56
57public:
58 /// @name ops
59 ///@{
60 const Def* shape() const { return op(0); }
61 const Def* body() const { return op(1); }
62 ///@}
63
64 /// @name Setters
65 ///@{
66 /// @see @ref set_ops "Setting Ops"
67 Arr* set_shape(const Def* shape) { return Def::set(0, shape)->as<Arr>(); }
68 Arr* set_body(const Def* body) { return Def::set(1, body)->as<Arr>(); }
69 Arr* unset() { return Def::unset()->as<Arr>(); }
70 ///@}
71
72 const Def* immutabilize() override;
73 Arr* stub_(World&, Ref) override;
74 Arr* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
75 const Def* reduce(const Def* arg) const;
76
77 /// @name Type Checking
78 ///@{
79 void check() override;
80 ///@}
81
83};
84
85/// A (possibly paramterized) Tuple.
86/// @see Sigma, Tuple, Arr
87class Pack : public Def {
88private:
89 Pack(const Def* type, const Def* body)
90 : Def(Node, type, {body}, 0) {} ///< Constructor for an *immutable* Pack.
91 Pack(const Def* type)
92 : Def(Node, type, 1, 0) {} ///< Constructor for a *mut*ablel Pack.
93
94public:
95 /// @name ops
96 ///@{
97 const Def* body() const { return op(0); }
98 const Def* shape() const;
99 ///@}
100
101 /// @name Setters
102 ///@{
103 /// @see @ref set_ops "Setting Ops"
104 Pack* set(const Def* body) { return Def::set(0, body)->as<Pack>(); }
105 Pack* reset(const Def* body) { return unset()->set(body); }
106 Pack* unset() { return Def::unset()->as<Pack>(); }
107 ///@}
108
109 const Def* immutabilize() override;
110 Pack* stub_(World&, Ref) override;
111 Pack* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
112 const Def* reduce(const Def* arg) const;
113
115};
116
117/// Extracts from a Sigma or Arr%ay-typed Extract::tuple the element at position Extract::index.
118class Extract : public Def {
119private:
120 Extract(const Def* type, const Def* tuple, const Def* index)
121 : Def(Node, type, {tuple, index}, 0) {}
122
123public:
124 /// @name ops
125 ///@{
126 const Def* tuple() const { return op(0); }
127 const Def* index() const { return op(1); }
128 ///@}
129
131};
132
133/// Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
134/// @attention This is a *functional* Insert.
135/// The Insert::tuple itself remains untouched.
136/// The Insert itself is a *new* Tuple / Pack which contains the inserted Insert::value.
137class Insert : public Def {
138private:
139 Insert(const Def* tuple, const Def* index, const Def* value)
140 : Def(Node, tuple->type(), {tuple, index, value}, 0) {}
141
142public:
143 /// @name ops
144 ///@{
145 const Def* tuple() const { return op(0); }
146 const Def* index() const { return op(1); }
147 const Def* value() const { return op(2); }
148 ///@}
149
151};
152
153/// @name Helpers to work with Tulpes/Sigmas/Arrays/Packs
154///@{
155bool is_unit(const Def*);
156std::string tuple2str(const Def*);
157
158/// Flattens a sigma/array/pack/tuple.
159const Def* flatten(const Def* def);
160/// Same as unflatten, but uses the operands of a flattened pack/tuple directly.
161size_t flatten(DefVec& ops, const Def* def, bool flatten_sigmas = true);
162
163/// Applies the reverse transformation on a pack/tuple, given the original type.
164const Def* unflatten(const Def* def, const Def* type);
165/// Same as unflatten, but uses the operands of a flattened pack/tuple directly.
166const Def* unflatten(Defs ops, const Def* type, bool flatten_muts = true);
167
169DefVec merge(const Def* def, Defs defs);
170const Def* merge_sigma(const Def* def, Defs defs);
171const Def* merge_tuple(const Def* def, Defs defs);
172
173Ref tuple_of_types(Ref t);
174///@}
175
176} // namespace thorin
A (possibly paramterized) Array.
Definition tuple.h:50
const Def * body() const
Definition tuple.h:61
Arr * set_body(const Def *body)
Definition tuple.h:68
const Def * reduce(const Def *arg) const
Definition def.cpp:204
const Def * shape() const
Definition tuple.h:60
Arr * set_shape(const Def *shape)
Definition tuple.h:67
Arr * stub(Ref type)
Definition tuple.h:74
void check() override
Definition check.cpp:225
Arr * stub_(World &, Ref) override
Definition def.cpp:136
Arr * unset()
Definition tuple.h:69
const Def * immutabilize() override
Tries to make an immutable from a mutable.
Definition def.cpp:180
Base class for all Defs.
Definition def.h:222
auto ops() const
Definition def.h:268
const Def * op(size_t i) const
Definition def.h:269
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
Definition def.cpp:272
const Def * type() const
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:248
Dbg dbg() const
Definition def.h:468
Def * set(size_t i, const Def *def)
Successively set from left to right.
Definition def.cpp:254
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
const Def * index() const
Definition tuple.h:127
const Def * tuple() const
Definition tuple.h:126
Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
Definition tuple.h:137
const Def * tuple() const
Definition tuple.h:145
const Def * value() const
Definition tuple.h:147
const Def * index() const
Definition tuple.h:146
A (possibly paramterized) Tuple.
Definition tuple.h:87
Pack * stub(Ref type)
Definition tuple.h:111
const Def * body() const
Definition tuple.h:97
Pack * set(const Def *body)
Definition tuple.h:104
Pack * reset(const Def *body)
Definition tuple.h:105
Pack * unset()
Definition tuple.h:106
Pack * stub_(World &, Ref) override
Definition def.cpp:140
const Def * shape() const
Definition tuple.cpp:41
const Def * immutabilize() override
Tries to make an immutable from a mutable.
Definition def.cpp:190
const Def * reduce(const Def *arg) const
Definition def.cpp:209
Helper class to retrieve Infer::arg if present.
Definition def.h:87
A dependent tuple type.
Definition tuple.h:9
Sigma * set(size_t i, const Def *def)
Definition tuple.h:20
void check() override
Definition check.cpp:231
Sigma * stub(Ref type)
Definition tuple.h:27
Sigma * unset()
Definition tuple.h:22
const Def * immutabilize() override
Tries to make an immutable from a mutable.
Definition def.cpp:174
Sigma * stub_(World &, Ref) override
Definition def.cpp:142
Sigma * set(Defs ops)
Definition tuple.h:21
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
#define THORIN_DEF_MIXIN(T)
Definition def.h:198
Definition cfg.h:11
Ref tuple_of_types(Ref t)
Definition tuple.cpp:110
const Def * flatten(const Def *def)
Flattens a sigma/array/pack/tuple.
Definition tuple.cpp:67
const Def * merge_tuple(const Def *def, Defs defs)
Definition tuple.cpp:99
View< const Def * > Defs
Definition def.h:62
bool is_unit(const Def *)
Definition tuple.cpp:47
const Def * unflatten(const Def *def, const Def *type)
Applies the reverse transformation on a pack/tuple, given the original type.
Definition tuple.cpp:81
DefVec merge(Defs, Defs)
Definition tuple.cpp:87
const Def * merge_sigma(const Def *def, Defs defs)
Definition tuple.cpp:94
Vector< const Def * > DefVec
Definition def.h:63
std::string tuple2str(const Def *)
Definition tuple.cpp:49