-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.ott
345 lines (296 loc) · 14.2 KB
/
grammar.ott
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
%% Ott grammar for linear constraints work
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Metavariables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
metavar x,y,z,f ::= {{ com Term variables }}
metavar a,b ::= {{ com Type variables }}
metavar K ::= {{ com Data constructors }}
metavar T ::= {{ com Type constructors }}
indexvar i, j ::= {{ com Index variables }}
indexvar g, w, r, o, ext ::= {{ com Annotation variables }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Grammar %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
grammar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Top level %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
toplevel :: 'Toplevel_' ::= {{ com Entry point of the grammar }}
| { parsable } :: :: parsable {{ tex [[parsable]] }}
parsable :: 'Parsable_' ::= {{ com Toplevel productions allowed in Tex filtering }}
| QF :: :: qf
| q :: :: q {{ tex \constraintfont{[[q]]} }}
| LCtx :: :: L {{ tex \constraintfont{[[LCtx]]} }}
| UCtx :: :: U {{ tex \constraintfont{[[UCtx]]} }}
| CF :: :: cf
| t :: :: t
| ts :: :: ts
| piF :: :: pi
| theta :: :: theta
| e :: :: e
| xs :: :: xs
| as :: :: as
| binding :: :: binding
| G :: :: g
| Dup :: :: Dup
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Constraints %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
UCtx {{ tex \ottnt{U} }} :: 'SetCt_' ::= {{ com Set of atomic constraints }}
| emptyset :: :: Empty
| UCtx1 \u UCtx2 :: :: Union {{ tex [[UCtx1]] \cup [[UCtx2]] }}
| q :: :: One
| UCtx \u LCtx :: :: Forget {{ tex [[UCtx]] \cup [[LCtx]] }}
UCtxF :: 'SetCt_' ::= {{ com Formatted set of atomic constraints }}
| UCtx :: :: Formatted {{ tex \constraintfont{[[UCtx]]} }}
LCtx {{ tex \ottnt{L} }}, DCtx {{ tex \ottnt{D} }} :: 'MultisetCt_' ::= {{ com Multiset of atomic constraints }}
| emptyset :: :: Empty
| LCtx1 \u LCtx2 :: :: Union {{ tex [[LCtx1]] \uplus [[LCtx2]] }}
| LCtx \n Dup :: :: RestrictToDup {{ tex [[LCtx]] \cap [[Dup]] }}
| LCtx \m Dup :: :: RestrictToNonDup {{ tex [[LCtx]] \setminus [[Dup]] }}
| q :: :: One
| LCtx1 , LCtx2 :: :: Append
| ( LCtx ) :: S :: Paren
LCtxF, DCtxF :: 'MultisetCt_' ::= {{ com Formatted multiset of atomic constraints }}
| LCtx :: :: Formatted {{ tex \constraintfont{[[LCtx]]} }}
q :: ACt_ ::= {{ com Atomic constraints }}
| Linearly :: :: Linearly {{ tex \mathcal{L} }}
Q, Qdup {{ tex Q_{\mathcal{D} } }} :: 'Ct_' ::= {{ com Constraints }}
| ( UCtx , LCtx ) :: :: Sets
| piF . q :: :: Atom {{ tex [[piF]]\scale[[q]] }}
| Q1 * Q2 :: M :: Tensor
| Empty :: M :: Empty {{tex \mathbf{\varepsilon} }}
| ! Q :: M :: Unrestricted
| Q [ theta ] :: M :: Subst
| piF . Q :: M :: Scale {{tex [[piF]]\scale[[Q]]}}
| ( Q ) :: S :: Paren
% Empty case
| Top :: :: Top
qF :: 'ACt_' ::= {{ com Formatted atomic constraints }}
| q :: :: Formatted {{ tex \constraintfont{[[q]]} }}
QF :: 'Ct_' ::= {{ com Formatted constraints }}
| Q :: :: Formatted {{ tex \constraintfont{[[Q]]} }}
QQ {{ tex \mathcal{Q} }} :: 'G_' ::= {{ com Givens }}
| Q :: :: GivenConstraint
| QQ & Q :: :: With
| QQ1 * QQ2 :: :: Tensor
C :: 'Ctg_' ::= {{ tex \constraintfont{[[C]]} }} {{ com Generated constraints}}
| Q :: :: Simple
| C1 * C2 :: :: Tensor
| C1 & C2 :: :: With
| piF . ( Q => C ) :: :: Impl {{ tex [[piF]]\scale([[Q]] \Lolly [[C]]) }}
| ! C :: :: Unrestricted
| piF . C :: M :: Scale {{tex [[piF]]\scale[[C]]}}
| && C :: M :: BigWith
| C [ theta ] :: M :: Subst
| ( C ) :: S :: Paren
CF :: 'Ctg_' ::= {{ com Generated constraints}}
| C :: :: Formatted {{ tex \constraintfont{[[C]]} }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Types %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t {{ tex \tau }}, u {{ tex \upsilon }}, s {{ tex \sigma}} :: 'Type_' ::= {{ com Types }}
| a :: :: Var
| QF =o t :: :: Qual
| t o= QF :: :: Carry
| forall as . t :: :: ForallNested
{{ tex [[forall]] [[as]]. [[t]] }}
| exists as . t :: :: ExistsNested
{{ tex [[exists]] [[as]]. [[t]] }}
| ts1 -> t2 :: S :: NestedUrFunction
| ts1 ->_ pisF t2 :: S :: Function {{ tex [[ts1]] \to_{[[pisF]]} [[t2]] }}
| T ts :: :: TyConApp
| t [ theta ] :: M :: Subst
| ( t ) :: S :: Paren
% Desugared types (see also expressions)
| Ds ( t ) :: M :: Desugared
{{ tex \dstype{[[t]]} }}
| Ev ( q ) :: M :: Evidence
{{ tex \dsevidence{\constraintfont{[[q]]} } }}
| Ev ( Q ) :: M :: EvSimple
{{ tex \dsevidence{\constraintfont{[[Q]]} } }}
| Ev ( Hole ) :: S :: EvHole
{{ tex \dsevidence{\constraintfont{[[Hole]]} } }}
| Ur t :: :: Unrestricted
| unit :: :: Unit {{ tex \mathbf{1} }}
| t1 ** t2 :: :: Prod
| t1 o- t2 :: :: DsCarry
% used for talking about Ds(_) as a family
| Hole :: S :: Hole
ts {{ tex \overline{\tau} }}, us {{ tex \overline{\upsilon} }}, ss {{ tex \overline{\sigma} }}
:: 'Types_' ::= {{ com List of types }}
| empty :: :: None
| t :: :: One
| ts1 , .... , tsi :: :: Many
| as :: M :: Vars
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Multiplicities %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pi {{ tex \pi }}, rho {{ tex \rho }} :: 'Mult_' ::= {{ com Multiplicities }}
| 1 :: :: One
| omega :: :: Many
| pi1 . pi2 :: M :: Mult {{ tex [[pi1]]{⋅}[[pi2]] }}
| pi1 + pi2 :: M :: Add
| ( pi ) :: S :: Paren
% I'd like to avoid multiplicity variables if we can, as they are
% not very relevant to our discussion
piF :: 'Mult_' ::= {{ com Multiplicities formatted }}
| pi :: :: Formatted {{ tex \multiplicityfont{[[pi]]} }}
pis {{ tex \overline{\pi} }} :: 'Mults_' ::= {{ com Lists of multiplicities }}
| empty :: :: None
| pi :: :: One
| pis1 , .... , pisi :: :: Many
pisF :: 'Mults_' ::= {{ com Lists of multiplicities formatted }}
| pis :: :: Formatted {{ tex \multiplicityfont{[[pis]]} }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Substitutions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
theta {{ tex \theta }} :: 'Subst_' ::= {{ com Substitutions }}
| ts / as :: :: Mappings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Expressions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
e :: 'Expr_' ::= {{ com Expressions }}
| x :: :: Var
| K :: :: DataCon
| \ x . e :: :: Lam
| e1 e2 :: :: App
| pack :: :: Pack
| unpack x = e1 in e2 :: :: Unpack
{{ tex \klet\ \packbox [[x]] = [[e1]] \ [[in]] \ [[e2]] }}
| LET x = e1 in e2 :: :: Let
| LET x : s = e1 in e2 :: :: LetSig
| CASE e of { alts } :: :: Case
| ( e ) :: S :: Paren
% Cheating a little by adding desugared expressions to the same grammar below.
% This is not formally correct, but it should do.
| Ev ( Q1 ||- Q2 ) :: M :: Evidence
{{ tex \dsevidence{\constraintfont{[[Q1]]} [[||-]] \constraintfont{[[Q2]]} } }}
| Ds ( z ; Ty ) :: M :: Desugar
{{ tex \dsterm{[[z]]}{[[Ty]]} }}
| Ds ( z ; Hole ) :: S :: DesugarHole
{{ tex \dsterm{[[z]]}{[[Hole]]} }}
| Ur :: M :: UnrestrictedCon
| urify ( QF ; e ) :: M :: Urify {{ tex \underline{[[e]]}_{[[QF]]} }}
| urify ( e ) :: M :: UrifyShortened {{ tex \underline{[[e]]} }}
| (,) :: :: ProdCon
{{ tex ({,}) }}
| ( e1 , e2 ) :: M :: Prod
| () :: :: UnitCon
| unpack ( pat1 , pat2 ) = e1 in e2 :: :: UnpackProd
{{ tex \klet\ \packbox ( [[pat1]] , [[pat2]] ) = [[e1]] \ \kin \ [[e2]] }}
CASE :: 'CaseHead_' ::= {{ com Case head }}
| case_ piF :: :: Case {{ tex \kcase_[[piF]] }}
LET :: 'LetHead_' ::= {{ com Let head }}
| let_ piF :: :: Let {{ tex \klet_[[piF]] }}
alt :: 'Alt_' ::= {{ com Case alternative }}
| pat -> e :: :: Alt
pat :: 'Pat_' ::= {{ com Patterns }}
% We should consider making a category K, instead of using an expression everywhere a constructor is needed.
| x :: :: Var
| e0 xs :: :: Con
| () :: :: Unit
| ( pat1 , pat2 ) :: M :: Prod
| urified ( QF ; pat ) :: M :: Urified {{ tex \underline{[[pat]]}_{[[QF]]} }}
| urified ( pat ) :: M :: UrifiedShortened {{ tex \underline{[[pat]]} }}
alts {{ tex \overline{\ottmv{K}_i\ \overline{\ottmv{x}_i } \to \ottnt{e}_i } }}
:: 'Alts_' ::= {{ com Case alternatives }}
| empty :: :: None
| alt :: :: One
| < alt > :: :: Schema
| alts1 ; .... ; altsi :: :: Many
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Variables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xs {{ tex \overline{\ottmv{x} } }} :: 'Termvars_' ::=
| empty :: :: None
| x :: :: One
| xs1 , .... , xsi :: :: Many
as {{ tex \overline{\ottmv{a} } }} :: 'Tyvars_' ::=
| empty :: :: None
| a :: :: One
| as1 , .... , asi :: :: Many
| a1 .... ai :: :: ManyBinds
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Contexts %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
binding :: 'Binding_' ::= {{ com Context binding }}
| x :_ piF t :: :: Bnd
{{ tex [[x]]{:}_{[[piF]]}[[t]] }}
G {{ tex \Gamma }}, D {{ tex \Delta }} :: 'Ctx_' ::= {{ com Contexts }}
| empty :: :: None
| binding :: :: One
| { binding } :: :: OneWithBraces
| G1 , .... , Gi :: :: Many
| G1 + G2 :: M :: Add
| Sum G2 :: M :: BigSum
| piF . G :: M :: Scale {{ tex [[piF]]\scale[[G]]}}
| < binding > :: M :: Repeat {{ tex \overline{[[binding]]} }}
| ( G ) :: S :: Paren
% Desugared contexts (see also expressions)
| G1 \u G2 :: M :: Union
{{ tex [[G1]] \cup [[G2]] }}
| Ds ( G ) :: M :: DsCtx
{{ tex \dstype{[[G]]} }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Terminals %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
terminals :: 'terminals_' ::=
| & :: :: With {{ tex \aand }}
| && :: :: BigWith {{ tex \bigaand }}
| * :: :: Tensor {{ tex \qtensor }}
| ** :: :: TypeTensor {{ tex \otimes }}
| |- :: :: entails {{ tex \vdash }}
| |-> :: :: infer {{ tex \vdashi }}
| |-s :: :: solve {{ tex \vdashs }}
| |-simp :: :: solve_simples {{ tex \vdashsimp }}
| ||- :: :: Entails {{ tex \Vdash }}
| ~> :: :: generates {{ tex \leadsto }}
| \ :: :: Lambda {{ tex \lambda }}
| -> :: :: Arrow {{ tex \to }}
| forall :: :: Forall {{ tex \forall }}
| exists :: :: Exists {{ tex \exists }}
| =o :: :: Lolli {{ tex \Lolly }}
| o= :: :: illoL {{ tex \RLolly }}
| o- :: :: illol {{ tex \otimes }}
| \in :: :: In {{ tex \in }}
| \notin :: :: NotIn {{ tex \notin }}
| omega :: :: Many {{ tex \omega }}
| Sum :: :: BigSum {{ tex \sum }}
| empty :: :: emptyseq {{ tex ∙ }} % alternative: ϵ
| Top :: :: Top {{ tex \constraintfont{⊤} }}
| emptyset :: :: emptyset {{ tex \emptyset }}
| \subseteq :: :: subset {{ tex \subseteq }}
| Hole :: :: Hole {{ tex \_ }}
| Dup :: :: Dup {{ tex \constraintfont{\mathcal{D} } }}
| pack :: :: Pack {{ tex \packbox }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Formulae %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
formula :: formula_ ::=
| judgement :: :: judgement
| formula \\\\ :: :: lbh
{{ tex [[formula]] \ottlinebreakhack }}
% See ottalt, sec. 1.1
| formula1 .... formulai :: :: dots
| as fresh :: :: fresh
{{ tex [[as]] \text{ fresh} }}
| < formula > :: :: Repeat
{{ tex \overline{[[formula]]} }}
| e : t :: :: DataconTy
| x \notin G :: :: NotInCtx {{ tex [[x]] \notin [[G]] }}
| binding \in G :: :: binding_in {{ tex [[binding]] \notin [[G]] }}
| G1 = G2 :: :: ctx_equal
| QF \in Dup :: :: Q_in_Dup {{ tex [[QF]] \in [[Dup]] }}
| qF \in Dup :: :: q_in_Dup {{ tex [[qF]] \in [[Dup]] }}
| LCtxF1 \subseteq LCtxF2 :: :: subset
| qF \in UCtxF :: :: q_in_UCtx {{ tex [[qF]] \in [[UCtxF]] }}
| qF \notin UCtxF :: :: q_notin_UCtx {{ tex [[qF]] \notin [[UCtxF]] }}
| qF \notin LCtxF :: :: q_notin_LCtx {{ tex [[qF]] \notin [[LCtxF]] }}
| LCtxF1 = LCtxF2 :: :: LCtx_eq
| s1 = s2 :: :: s_eq
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Parsing rules %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parsing
Type_ExistsNested <= Type_Carry
Type_ExistsNested <= Type_DsCarry
Ct_Tensor <= Ct_Unrestricted
Ct_Tensor <= Ct_Scale
Ct_Tensor <= Ct_Subst
Ctg_Tensor <= Ctg_Unrestricted
Ctg_Tensor <= Ctg_Scale
Ctg_Tensor <= Ctg_Subst
Ctx_Add <= Ctx_Scale