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