Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
lattice.h
Go to the documentation of this file.
1#pragma once
2
3#include "thorin/def.h"
4
5namespace thorin {
6
7class Lam;
8class Sigma;
9
10/// Common base for TBound.
11class Bound : public Def {
12protected:
14 : Def(node, type, ops, 0) {} ///< Constructor for an *immutable* Bound.
15 Bound(node_t node, const Def* type, size_t size)
16 : Def(node, type, size, 0) {} ///< Constructor for a *mutable* Bound.
17
18public:
19 /// @name Get Element by Type
20 ///@{
21 size_t find(const Def* type) const;
22 const Def* get(const Def* type) const { return op(find(type)); }
23 ///@}
24};
25
26/// Specific [Bound](https://en.wikipedia.org/wiki/Join_and_meet) depending on @p Up.
27/// The name @p Up refers to the property that a [Join](@ref thorin::Join) **ascends** in the underlying
28/// [lattice](https://en.wikipedia.org/wiki/Lattice_(order)) while a [Meet](@ref thorin::Meet) descends.
29/// * @p Up = `true`: [Join](@ref thorin::Join) (aka least Upper bound/supremum/union)
30/// * @p Up = `false`: [Meet](@ref thorin::Meet) (aka greatest lower bound/infimum/intersection)
31template<bool Up> class TBound : public Bound {
32private:
33 TBound(const Def* type, Defs ops)
34 : Bound(Node, type, ops) {} ///< Constructor for an *immutable* Bound.
35 TBound(const Def* type, size_t size)
36 : Bound(Node, type, size) {} ///< Constructor for a *mutable* Bound.
37
38public:
40
41 TBound* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
42
43 static constexpr auto Node = Up ? Node::Join : Node::Meet;
44
45private:
46 Ref rebuild_(World&, Ref, Defs) const override;
47 TBound* stub_(World&, Ref) override;
48
49 friend class World;
50};
51
52/// Constructs a [Meet](@ref thorin::Meet) **value**.
53/// @remark [Ac](https://en.wikipedia.org/wiki/Wedge_(symbol)) is Latin and means *and*.
54class Ac : public Def {
55private:
56 Ac(const Def* type, Defs defs)
57 : Def(Node, type, defs, 0) {}
58
60};
61
62/// Constructs a [Join](@ref thorin::Join) **value**.
63/// @remark [Vel](https://en.wikipedia.org/wiki/Wedge_(symbol)) is Latin and means *or*.
64class Vel : public Def {
65private:
66 Vel(const Def* type, const Def* value)
67 : Def(Node, type, {value}, 0) {}
68
69public:
70 /// @name ops
71 ///@{
72 const Def* value() const { return op(0); }
73 ///@}
74
76};
77
78/// Picks the aspect of a Meet [value](Pick::value) by its [type](Def::type).
79class Pick : public Def {
80private:
81 Pick(const Def* type, const Def* value)
82 : Def(Node, type, {value}, 0) {}
83
84public:
85 /// @name ops
86 ///@{
87 const Def* value() const { return op(0); }
88 ///@}
89
91};
92
93/// Test whether Test::value currently holds **type** Test::probe:
94/// ```
95/// test value, probe, match, clash
96/// ```
97/// @note
98/// * Test::probe is a **type**!
99/// * This operation yields Test::match, if `tt`, and Test::clash otherwise.
100/// @invariant
101/// * Test::value must be of type Join.
102/// * Test::match must be of type `A -> B`.
103/// * Test::clash must be of type `[A, probe] -> C`.
104/// @remark This operation is usually known as `case` but named `Test` since `case` is a keyword in C++.
105class Test : public Def {
106private:
107 Test(const Def* type, const Def* value, const Def* probe, const Def* match, const Def* clash)
108 : Def(Node, type, {value, probe, match, clash}, 0) {}
109
110public:
111 /// @name ops
112 ///@{
113 const Def* value() const { return op(0); }
114 const Def* probe() const { return op(1); }
115 const Def* match() const { return op(2); }
116 const Def* clash() const { return op(3); }
117 ///@}
118
120};
121
122/// Common base for TExt%remum.
123class Ext : public Def {
124protected:
126 : Def(node, type, Defs{}, 0) {}
127};
128
129/// Ext%remum. Either Top (@p Up) or Bot%tom.
130template<bool Up> class TExt : public Ext {
131private:
132 TExt(const Def* type)
133 : Ext(Node, type) {}
134
135public:
137
138 TExt* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
139
140 static constexpr auto Node = Up ? Node::Top : Node::Bot;
141
142private:
143 Ref rebuild_(World&, Ref, Defs) const override;
144 TExt* stub_(World&, Ref) override;
145
146 friend class World;
147};
148
149/// @name Lattice
150///@{
155/// @}
156
157/// A singleton wraps a type into a higher order type.
158/// Therefore any type can be the only inhabitant of a singleton.
159/// Use in conjunction with @ref thorin::Join.
160class Singleton : public Def {
161private:
162 Singleton(const Def* type, const Def* inner_type)
163 : Def(Node, type, {inner_type}, 0) {}
164
165public:
166 /// @name ops
167 ///@{
168 const Def* inhabitant() const { return op(0); }
169 ///@}
170
172};
173
174} // namespace thorin
Constructs a Meet value.
Definition lattice.h:54
Common base for TBound.
Definition lattice.h:11
Bound(node_t node, const Def *type, Defs ops)
Constructor for an immutable Bound.
Definition lattice.h:13
Bound(node_t node, const Def *type, size_t size)
Constructor for a mutable Bound.
Definition lattice.h:15
const Def * get(const Def *type) const
Definition lattice.h:22
size_t find(const Def *type) const
Definition lattice.cpp:10
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
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
node_t node() const
Definition def.h:240
World & world() const
Definition def.cpp:421
Common base for TExtremum.
Definition lattice.h:123
Ext(node_t node, const Def *type)
Definition lattice.h:125
Picks the aspect of a Meet [value](Pick::value) by its [type](Def::type).
Definition lattice.h:79
const Def * value() const
Definition lattice.h:87
Helper class to retrieve Infer::arg if present.
Definition def.h:87
A singleton wraps a type into a higher order type.
Definition lattice.h:160
const Def * inhabitant() const
Definition lattice.h:168
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
Specific Bound depending on Up.
Definition lattice.h:31
Ref rebuild_(World &, Ref, Defs) const override
static constexpr auto Node
Definition lattice.h:43
TBound * stub(Ref type)
Definition lattice.h:41
TBound * stub_(World &, Ref) override
Definition def.cpp:144
Extremum. Either Top (Up) or Bottom.
Definition lattice.h:130
TExt * stub(Ref type)
Definition lattice.h:138
TExt * stub_(World &, Ref) override
Definition def.cpp:145
static constexpr auto Node
Definition lattice.h:140
Ref rebuild_(World &, Ref, Defs) const override
Test whether Test::value currently holds type Test::probe:
Definition lattice.h:105
const Def * probe() const
Definition lattice.h:114
const Def * value() const
Definition lattice.h:113
const Def * match() const
Definition lattice.h:115
const Def * clash() const
Definition lattice.h:116
Constructs a Join value.
Definition lattice.h:64
const Def * value() const
Definition lattice.h:72
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
#define THORIN_SETTERS(T)
Definition def.h:193
#define THORIN_DEF_MIXIN(T)
Definition def.h:198
@ Sigma
Definition def.h:41
Definition cfg.h:11
u8 node_t
Definition types.h:45