-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjudgments.jl
More file actions
234 lines (172 loc) · 6.12 KB
/
Copy pathjudgments.jl
File metadata and controls
234 lines (172 loc) · 6.12 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# GAT Judgments
###############
"""
`TypeScope`
A scope where variables are assigned to `AlgType`s. We use a wrapper
here so that it pretty prints as `[a::B]` instead of `{a => AlgType(B)}`
"""
@struct_hash_equal struct TypeScope <: HasScope{AlgType}
scope::Scope{AlgType}
end
TypeScope() = TypeScope(Scope{AlgType}())
TypeScope(t::TypeScope) = t
TypeScope(bindings::Vector{Binding{AlgType}}; tag=newscopetag()) = TypeScope(Scope(bindings; tag))
TypeScope(bindings::Pair{Symbol, AlgType}...) = TypeScope(Scope{AlgType}(bindings...))
Scopes.getscope(ts::TypeScope) = ts.scope
Scopes.unsafe_pushbinding!(ts::TypeScope, b) =
Scopes.unsafe_pushbinding!(ts.scope, b)
reident(d::Dict{Ident, Ident}, ts::TypeScope) =
TypeScope(reident(d, getscope(ts)))
function Base.show(io::IO, ts::TypeScope)
print(io, toexpr(EmptyContext{AlgType}(), ts))
end
"""
A GAT is conceptually a bunch of `Judgment`s strung together.
"""
abstract type Judgment <: HasContext{AlgType} end
abstract type TrmTypConstructor <: Judgment end
argsof(t::TrmTypConstructor) = t[t.args]
Scopes.getscope(t::TrmTypConstructor) = t.localcontext
Base.getindex(tc::TrmTypConstructor, lid::LID) = getindex(tc.localcontext, lid)
Base.getindex(tc::TrmTypConstructor, lids::AbstractVector{LID}) = getindex(tc.localcontext, lids)
getdecl(tc::TrmTypConstructor) = tc.declaration
"""
`AlgDeclaration`
A declaration of a constructor; constructor methods in the form of
`AlgTermConstructors` or the accessors for `AlgTypeConstructors` follow later in
the theory.
"""
@struct_hash_equal struct AlgDeclaration <: Judgment
end
Scopes.getcontext(::AlgDeclaration) = EmptyContext{AlgType}()
"""
`AlgTypeConstructor`
A declaration of a type constructor.
"""
@struct_hash_equal struct AlgTypeConstructor <: TrmTypConstructor
declaration::Ident
localcontext::TypeScope
args::Vector{LID}
end
Scopes.getcontext(tc::TrmTypConstructor) = tc.localcontext
reident(d::Dict{Ident,Ident}, tc::AlgTypeConstructor) = AlgTypeConstructor(
get(d, tc.declaration, tc.declaration),
reident(d, Scopes.getcontext(tc)),
tc.args,
)
abstract type AccessorField <: Judgment end
"""
`AlgAccessor`
The arguments to a term constructor serve a dual function as both arguments and
also methods to extract the value of those arguments.
I.e., declaring `Hom(dom::Ob, codom::Ob)::TYPE` implicitly overloads a previous
declaration for `dom` and `codom`, or creates declarations if none previously
exist.
"""
@struct_hash_equal struct AlgAccessor <: AccessorField
declaration::Ident
typecondecl::Ident
typecon::Ident
arg::Int
end
getdecl(acc::AccessorField) = acc.declaration
sortsignature(acc::AccessorField) = [AlgSort(acc.typecondecl, acc.typecon)]
"""
`AlgTermConstructor`
A declaration of a term constructor as a method of an `AlgFunction`.
"""
@struct_hash_equal struct AlgTermConstructor <: TrmTypConstructor
declaration::Ident
localcontext::TypeScope
args::Vector{LID}
type::Union{TypeScope,AlgType}
end
sortsignature(tc::TrmTypConstructor) = AlgSort.(getvalue.(argsof(tc)))
reident(d::Dict{Ident,Ident}, tc::AlgTermConstructor) = AlgTermConstructor(
get(d, tc.declaration, tc.declaration),
reident(d, Scopes.getcontext(tc)),
tc.args,
reident(d, tc.type)
)
function reident(d::Dict{Ident, Ident}, s::Scope{AlgType})
Scope(Binding{AlgType}[setvalue(b, reident(d, getvalue(b))) for b in s.bindings];
tag = gettag(s))
end
reident(d::Dict{Ident, Ident}, s::AlgSort) =
AlgSort(reident(d, headof(s)), reident(d, methodof(s)))
reident(d::Dict{Ident, Ident}, s::T) where T<:AlgAST =
T(reident(d, bodyof(s)))
reident(d::Dict{Ident, Ident}, x::Ident) = get(d, x, x)
reident(d::Dict{Ident, Ident}, m::MethodApp{T}) where T =
MethodApp{T}(reident(d, headof(m)), reident(d, methodof(m)),
reident.(Ref(d), argsof(m)))
reident(d::Dict{Ident,Ident}, b::Binding{Judgment}) =
setvalue(b, reident(d, getvalue(b)))
"""
`AlgAxiom`
A declaration of an axiom
"""
@struct_hash_equal struct AlgAxiom <: Judgment
localcontext::TypeScope
sort::AlgSort
equands::Vector{AlgTerm}
end
Scopes.getcontext(ax::AlgAxiom) = ax.localcontext
reident(d::Dict{Ident,Ident}, ax::AlgAxiom) = AlgAxiom(
reident(d, Scopes.getcontext(ax)),
reident(d, ax.sort),
reident.(Ref(d), ax.equands)
)
"""
`AlgSorts`
A description of the argument sorts for a term constructor, used to disambiguate
multiple term constructors of the same name.
"""
const AlgSorts = Vector{<:AbstractAlgSort}
"""
`AlgStruct`
A declaration which is sugar for an AlgTypeConstructor, an AlgTermConstructor
which constructs an element of that type, and projection term constructors. E.g.
struct Cospan(dom, codom) ⊣ [dom:Ob, codom::Ob]
apex::Ob
i1::dom->apex
i2::codom->apex
end
Is tantamount to (in a vanilla GAT):
Cospan(dom::Ob, codom::Ob)::TYPE
cospan(apex, i1, i2)::Cospan(dom, codom)
⊣ [(dom, codom, apex)::Ob, i1::dom->apex, i2::codom->apex]
apex(csp::Cospan(d::Ob, c::Ob))::Ob
i1(csp::Cospan(d::Ob, c::Ob))::(d->apex(csp))
i2(csp::Cospan(d::Ob, c::Ob))::(c->apex(csp))
apex(cospan(a, i_1, i_2)) == a
⊣ [(dom, codom, apex)::Ob, i_1::dom->apex, i_2::codom->apex]
i1(cospan(a, i_1, i_2)) == i_1
⊣ [(dom, codom, apex)::Ob, i_1::dom->apex, i_2::codom->apex]
i2(cospan(a, i_1, i_2)) == i_2
⊣ [(dom, codom, apex)::Ob, i_1::dom->apex, i_2::codom->apex]
cospan(apex(csp), i1(csp), i2(csp)) == csp
⊣ [(dom, codom)::Ob, csp::Cospan(dom, codom)]
"""
@struct_hash_equal struct AlgStruct <: TrmTypConstructor
declaration::Ident
localcontext::TypeScope
typeargs::Vector{LID}
fields::TypeScope
end
Base.nameof(t::AlgStruct) = nameof(t.declaration)
typeargsof(t::AlgStruct) = t[t.typeargs]
typesortsignature(tc::AlgStruct) =
AlgSort.(getvalue.(typeargsof(tc)))
argsof(t::AlgStruct) = getbindings(t.fields)
"""
A shorthand for a function, such as "square(x) := x * x". It is relevant for
models but can be ignored by theory maps, as it is fully determined by other
judgments in the theory.
"""
@struct_hash_equal struct AlgFunction <: TrmTypConstructor
declaration::Ident
localcontext::TypeScope
args::Vector{LID}
value::AlgTerm
end