forked from AnyDSL/MimIR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlattice.h
191 lines (159 loc) · 5.55 KB
/
lattice.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#pragma once
#include "thorin/def.h"
namespace thorin {
class Lam;
class Sigma;
/// Common base for TBound.
class Bound : public Def {
protected:
/// Constructor for a *structural* Bound.
Bound(node_t node, const Def* type, Defs ops, const Def* dbg)
: Def(node, type, ops, 0, dbg) {}
/// Constructor for a *nom*inal Bound.
Bound(node_t node, const Def* type, size_t size, const Def* dbg)
: Def(node, type, size, 0, dbg) {}
public:
size_t find(const Def* type) const;
const Def* get(const Def* type) const { return op(find(type)); }
};
/// Specific [Bound](https://en.wikipedia.org/wiki/Join_and_meet) depending on @p up.
/// The name @p up refers to the property that a [Join](@ref thorin::Join) **ascends** in the underlying
/// [lattice](https://en.wikipedia.org/wiki/Lattice_(order)) while a [Meet](@ref thorin::Meet) descends.
/// * @p up = `true`: [Join](@ref thorin::Join) (aka least upper bound/supremum/union)
/// * @p up = `false`: [Meet](@ref thorin::Meet) (aka greatest lower bound/infimum/intersection)
template<bool up>
class TBound : public Bound {
private:
/// Constructor for a *structural* Bound.
TBound(const Def* type, Defs ops, const Def* dbg)
: Bound(Node, type, ops, dbg) {}
/// Constructor for a *nom*inal Bound.
TBound(const Def* type, size_t size, const Def* dbg)
: Bound(Node, type, size, dbg) {}
public:
/// @name virtual methods
///@{
const Def* rebuild(World&, const Def*, Defs, const Def*) const override;
TBound* stub(World&, const Def*, const Def*) override;
///@}
static constexpr auto Node = up ? Node::Join : Node::Meet;
friend class World;
};
/// Constructs a [Meet](@ref thorin::Meet) **value**.
/// @remark [Ac](https://en.wikipedia.org/wiki/Wedge_(symbol)) is Latin and means *and*.
class Ac : public Def {
private:
Ac(const Def* type, Defs defs, const Def* dbg)
: Def(Node, type, defs, 0, dbg) {}
public:
/// @name virtual methods
///@{
const Def* rebuild(World&, const Def*, Defs, const Def*) const override;
///@}
static constexpr auto Node = Node::Ac;
friend class World;
};
/// Constructs a [Join](@ref thorin::Join) **value**.
/// @remark [Vel](https://en.wikipedia.org/wiki/Wedge_(symbol)) is Latin and means *or*.
class Vel : public Def {
private:
Vel(const Def* type, const Def* value, const Def* dbg)
: Def(Node, type, {value}, 0, dbg) {}
public:
/// @name ops
///@{
const Def* value() const { return op(0); }
///@}
/// @name virtual methods
///@{
const Def* rebuild(World&, const Def*, Defs, const Def*) const override;
///@}
static constexpr auto Node = Node::Vel;
friend class World;
};
/// Picks the aspect of a Meet [value](Pick::value) by its [type](Def::type).
class Pick : public Def {
private:
Pick(const Def* type, const Def* value, const Def* dbg)
: Def(Node, type, {value}, 0, dbg) {}
public:
/// @name ops
///@{
const Def* value() const { return op(0); }
///@}
/// @name virtual methods
///@{
const Def* rebuild(World&, const Def*, Defs, const Def*) const override;
///@}
static constexpr auto Node = Node::Pick;
friend class World;
};
/// `test value, probe, match, clash` tests whether Test::value currently holds **type** Test::probe.
/// @note
/// * Test::probe is a **type**!
/// * This operation yields Test::match, if `tt`, and Test::clash otherwise.
/// @invariant
/// * Test::value must be of type Join.
/// * Test::match must be of type `A -> B`.
/// * Test::clash must be of type `[A, probe] -> C`.
/// @remark This operation is usually known as `case` but named `Test` since `case` is a keyword in C++.
class Test : public Def {
private:
Test(const Def* type, const Def* value, const Def* probe, const Def* match, const Def* clash, const Def* dbg)
: Def(Node, type, {value, probe, match, clash}, 0, dbg) {}
public:
/// @name ops
///@{
const Def* value() const { return op(0); }
const Def* probe() const { return op(1); }
const Def* match() const { return op(2); }
const Def* clash() const { return op(3); }
///@}
/// @name virtual methods
///@{
const Def* rebuild(World&, const Def*, Defs, const Def*) const override;
///@}
static constexpr auto Node = Node::Test;
friend class World;
};
/// Common base for TExt%remum.
class Ext : public Def {
protected:
Ext(node_t node, const Def* type, const Def* dbg)
: Def(node, type, Defs{}, 0, dbg) {}
};
/// Ext%remum. Either Top (@p up) or Bot%tom.
template<bool up>
class TExt : public Ext {
private:
TExt(const Def* type, const Def* dbg)
: Ext(Node, type, dbg) {}
public:
/// @name virtual methods
///@{
const Def* rebuild(World&, const Def*, Defs, const Def*) const override;
///@}
static constexpr auto Node = up ? Node::Top : Node::Bot;
friend class World;
};
using Bot = TExt<false>;
using Top = TExt<true>;
using Meet = TBound<false>;
using Join = TBound<true>;
/// A singleton wraps a type into a higher order type.
/// Therefore any type can be the only inhabitant of a singleton.
/// Use in conjunction with @ref thorin::Join.
class Singleton : public Def {
private:
Singleton(const Def* type, const Def* inner_type, const Def* dbg)
: Def(Node, type, {inner_type}, 0, dbg) {}
public:
const Def* inhabitant() const { return op(0); }
/// @name virtual methods
///@{
const Def* rebuild(World&, const Def*, Defs, const Def*) const override;
///@}
static constexpr auto Node = Node::Singleton;
friend class World;
};
} // namespace thorin