MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lattice.h
Go to the documentation of this file.
1#pragma once
2
3#include <span>
4
5#include "mim/def.h"
6
7namespace mim {
8
9class Lam;
10class Sigma;
11
12/// Common base for TBound.
13class Bound : public Def {
14protected:
15 Bound(Node node, const Def* type, Defs ops)
16 : Def(node, type, ops, 0) {}
17
18 constexpr size_t reduction_offset() const noexcept final { return 0; }
19
20public:
21 ~Bound() override;
22
23 /// @name Get Element by Type
24 ///@{
25 size_t find(const Def* type) const;
26 const Def* get(const Def* type) const { return op(find(type)); }
27 ///@}
28};
29
30/// Specific [Bound](https://en.wikipedia.org/wiki/Join_and_meet) depending on @p Up.
31/// The name @p Up refers to the property that a [Join](@ref mim::Join) **ascends** in the underlying
32/// [lattice](https://en.wikipedia.org/wiki/Lattice_(order)) while a [Meet](@ref mim::Meet) descends.
33/// * @p Up = `true`: [Join](@ref mim::Join) (aka least Upper bound/supremum/union)
34/// * @p Up = `false`: [Meet](@ref mim::Meet) (aka greatest lower bound/infimum/intersection)
35template<bool Up>
36class TBound : public Bound, public Setters<TBound<Up>> {
37private:
38 TBound(const Def* type, Defs ops)
39 : Bound(Node, type, ops) {}
40
41public:
42 using Setters<TBound<Up>>::set;
43
44 static constexpr auto Node = Up ? mim::Node::Join : mim::Node::Meet;
45 static constexpr size_t Num_Ops = std::dynamic_extent;
46
47private:
48 const Def* rebuild_(World&, const Def*, Defs) const final;
49
50 friend class World;
51};
52
53/// Constructs a [Meet](@ref mim::Meet) **value**.
54/// @remark [Ac](https://en.wikipedia.org/wiki/Wedge_(symbol)) is Latin and means *and*.
55class Merge : public Def, public Setters<Merge> {
56public:
57 using Setters<Merge>::set;
58 static constexpr auto Node = mim::Node::Merge;
59 static constexpr size_t Num_Ops = std::dynamic_extent;
60
61private:
62 Merge(const Def* type, Defs defs)
63 : Def(Node, type, defs, 0) {}
64
65 const Def* rebuild_(World&, const Def*, Defs) const final;
66
67 friend class World;
68};
69
70/// Constructs a [Join](@ref mim::Join) **value**.
71/// @remark [Inj](https://en.wikipedia.org/wiki/Wedge_(symbol)) is Latin and means *or*.
72class Inj : public Def, public Setters<Inj> {
73private:
74 Inj(const Def* type, const Def* value)
75 : Def(Node, type, {value}, 0) {}
76
77public:
78 using Setters<Inj>::set;
79
80 /// @name ops
81 ///@{
82 const Def* value() const { return op(0); }
83 ///@}
84
85 static constexpr auto Node = mim::Node::Inj;
86 static constexpr size_t Num_Ops = 1;
87
88private:
89 const Def* rebuild_(World&, const Def*, Defs) const final;
90
91 friend class World;
92};
93
94/// Picks the aspect of a Meet [value](Pick::value) by its [type](Def::type).
95class Split : public Def, public Setters<Split> {
96private:
97 Split(const Def* type, const Def* value)
98 : Def(Node, type, {value}, 0) {}
99
100public:
101 using Setters<Split>::set;
102
103 /// @name ops
104 ///@{
105 const Def* value() const { return op(0); }
106 ///@}
107
108 static constexpr auto Node = mim::Node::Split;
109 static constexpr size_t Num_Ops = 1;
110
111private:
112 const Def* rebuild_(World&, const Def*, Defs) const final;
113
114 friend class World;
115};
116
117/// Scrutinize Match::scrutinee() and dispatch to Match::arms.
118class Match : public Def, public Setters<Match> {
119private:
120 Match(const Def* type, Defs ops)
121 : Def(Node, type, ops, 0) {}
122
123public:
124 using Setters<Match>::set;
125 static constexpr auto Node = mim::Node::Match;
126 static constexpr size_t Num_Ops = std::dynamic_extent;
127
128 /// @name ops
129 ///@{
130 const Def* scrutinee() const { return op(0); }
131 template<size_t N = std::dynamic_extent>
132 constexpr auto arms() const noexcept {
133 return ops().subspan<1, N>();
134 }
135 const Def* arm(size_t i) const { return arms()[i]; }
136 size_t num_arms() const { return arms().size(); }
137 ///@}
138
139private:
140 const Def* rebuild_(World&, const Def*, Defs) const final;
141
142 friend class World;
143};
144
145/// Common base for TExt%remum.
146class Ext : public Def {
147protected:
148 Ext(Node node, const Def* type)
149 : Def(node, type, Defs{}, 0) {}
150
151public:
152 ~Ext() override;
153};
154
155/// Ext%remum. Either Top (@p Up) or Bot%tom.
156template<bool Up>
157class TExt : public Ext, public Setters<TExt<Up>> {
158private:
159 TExt(const Def* type)
160 : Ext(Node, type) {}
161
162public:
163 using Setters<TExt<Up>>::set;
164
165 static constexpr auto Node = Up ? mim::Node::Top : mim::Node::Bot;
166 static constexpr size_t Num_Ops = 0;
167
168private:
169 const Def* rebuild_(World&, const Def*, Defs) const final;
170
171 friend class World;
172};
173
174/// @name Lattice
175///@{
178using Meet = TBound<false>; ///< AKA intersection.
179using Join = TBound<true>; ///< AKA union.
180/// @}
181
182/// A singleton wraps a type into a higher order type.
183/// Therefore any type can be the only inhabitant of a singleton.
184/// Use in conjunction with @ref mim::Join.
185class Uniq : public Def, public Setters<Uniq> {
186private:
187 Uniq(const Def* type, const Def* inner_type)
188 : Def(Node, type, {inner_type}, 0) {}
189
190public:
191 using Setters<Uniq>::set;
192
193 /// @name ops
194 ///@{
195 const Def* op() const { return Def::op(0); }
196 ///@}
197
198 static constexpr auto Node = mim::Node::Uniq;
199 static constexpr size_t Num_Ops = 1;
200
201private:
202 const Def* rebuild_(World&, const Def*, Defs) const final;
203
204 friend class World;
205};
206
207} // namespace mim
~Bound() override
Bound(Node node, const Def *type, Defs ops)
Definition lattice.h:15
size_t find(const Def *type) const
Definition lattice.cpp:10
const Def * get(const Def *type) const
Definition lattice.h:26
constexpr size_t reduction_offset() const noexcept final
First Def::op that needs to be dealt with during reduction; e.g.
Definition lattice.h:18
Base class for all Defs.
Definition def.h:261
constexpr Node node() const noexcept
Definition def.h:286
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:276
constexpr auto ops() const noexcept
Definition def.h:317
const Def * op(size_t i) const noexcept
Definition def.h:320
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:491
~Ext() override
Ext(Node node, const Def *type)
Definition lattice.h:148
static constexpr auto Node
Definition lattice.h:85
const Def * value() const
Definition lattice.h:82
friend class World
Definition lattice.h:91
static constexpr size_t Num_Ops
Definition lattice.h:86
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:127
A function.
Definition lam.h:110
const Def * scrutinee() const
Definition lattice.h:130
size_t num_arms() const
Definition lattice.h:136
friend class World
Definition lattice.h:142
static constexpr size_t Num_Ops
Definition lattice.h:126
static constexpr auto Node
Definition lattice.h:125
const Def * arm(size_t i) const
Definition lattice.h:135
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:139
constexpr auto arms() const noexcept
Definition lattice.h:132
static constexpr size_t Num_Ops
Definition lattice.h:59
friend class World
Definition lattice.h:67
static constexpr auto Node
Definition lattice.h:58
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:131
CRTP-based mixin to declare setters for Def::loc & Def::name using a covariant return type.
Definition def.h:193
const TBound< Up > * set(Loc l) const
Definition def.h:200
A dependent tuple type.
Definition tuple.h:22
friend class World
Definition lattice.h:114
static constexpr auto Node
Definition lattice.h:108
const Def * value() const
Definition lattice.h:105
static constexpr size_t Num_Ops
Definition lattice.h:109
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:138
Specific Bound depending on Up.
Definition lattice.h:36
friend class World
Definition lattice.h:50
static constexpr size_t Num_Ops
Definition lattice.h:45
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:154
static constexpr auto Node
Definition lattice.h:44
Extremum. Either Top (Up) or Bottom.
Definition lattice.h:157
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:153
static constexpr auto Node
Definition lattice.h:165
friend class World
Definition lattice.h:171
static constexpr size_t Num_Ops
Definition lattice.h:166
const Def * op() const
Definition lattice.h:195
static constexpr size_t Num_Ops
Definition lattice.h:199
friend class World
Definition lattice.h:204
static constexpr auto Node
Definition lattice.h:198
const Def * rebuild_(World &, const Def *, Defs) const final
Definition def.cpp:144
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
Definition ast.h:14
View< const Def * > Defs
Definition def.h:78
TBound< true > Join
AKA union.
Definition lattice.h:179
TExt< true > Top
Definition lattice.h:177
TExt< false > Bot
Definition lattice.h:176
TBound< false > Meet
AKA intersection.
Definition lattice.h:178
Node
Definition def.h:107
@ Bot
Definition def.h:109
@ Meet
Definition def.h:109
@ Inj
Definition def.h:109
@ Merge
Definition def.h:109
@ Match
Definition def.h:109
@ Split
Definition def.h:109
@ Join
Definition def.h:109
@ Top
Definition def.h:109
@ Uniq
Definition def.h:109