-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExp.ott
270 lines (215 loc) · 7.02 KB
/
Exp.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
embed {{ coq-preamble
Require Export Metalib.LibLNgen.
}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% METAVARIABLES
metavar tmvar {{ tex \text{Term variables} }}, x, y, z ::=
{{ repr-locally-nameless }}
{{ com variables }}
{{ lex alphanum }}
metavar tyvar {{ tex \text{Type variables} }}, a, b ::=
{{ repr-locally-nameless }}
{{ com type variables }}
{{ lex alphanum }}
metavar integer {{ tex \text{Integer} }}, i ::=
{{ coq nat }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% GRAMMAR
grammar
ty_mono {{ tex \text{Monotypes} }}, tau {{ tex \tau }} :: 'ty_mono_' ::= {{ com Monotypes }}
| Int :: :: base
{{ tex \texttt{Int} }}
| a :: :: var
| tau1 -> tau2 :: :: func
{{ tex [[tau1]] [[->]] [[tau2]] }}
ty_rho {{ tex \text{Rho-types} }}, rho {{ tex \rho }} :: 'ty_rho_' ::= {{ com Rho-types }}
| tau :: :: tau
% | sig -> sig' :: :: func
% {{ tex [[sig]] [[->]] [[sig']] }}
| ( rho ) :: S :: paren
{{ tex ([[rho]]) }}
{{ coq ([[rho]]) }}
ty_poly {{ tex \text{Polytypes} }}, sig {{ tex \sigma }} :: 'ty_poly_' ::=
| rho :: :: rho
| forall a . sig :: :: poly_gen
(+ bind a in sig +)
{{ tex \forall [[a]] . [[sig]] }}
| [ a |-> tau ] sig :: M :: subst
{{ tex \left[ [[a]] \mapsto [[tau]] \right] [[sig]] }}
{{ coq (open_ty_poly_wrt_ty_mono[[a sig]][[tau]])}}
tm {{ tex \text{Terms} }}, t, u :: 'exp_' ::=
| i :: :: lit
{{ coq (exp_lit [[i]]) }}
{{ com Literal }}
| x :: :: var
{{ com Variable }}
| \ x . t :: :: abs
(+ bind x in t +)
{{ tex \lambda [[x]] . [[t]] }}
{{ com Abstraction }}
| t u :: :: app
{{ tex [[t]] \; [[u]] }}
{{ com Application }}
| \ ( annot x sig ) . t :: :: typed_abs
(+ bind x in t +)
{{ tex \lambda ([[x]] :: [[sig]]). [[t]] }}
{{ com Typed abstraction }}
| let x = u in t :: :: let
(+ bind x in t +)
{{ tex \texttt{let}\; [[x]] = [[u]] \;\texttt{in}\; [[t]] }}
{{ com Local binding }}
| annot t sig :: :: type_anno
{{ tex [[t]] :: [[sig]] }}
{{ com Type annotation }}
| ( t ) :: S :: paren
{{ tex ([[t]]) }}
{{ coq ([[t]]) }}
| t [ x ~> t' ] :: M :: subst
{{ tex [[t]] \left[ [[x]] \leadsto [[t']] \right] }}
{{ coq (open_tm_wrt_tm[[x t]][[t']])}}
substitutions
single t x :: subst
single sig a :: subst_ty_poly
single rho a :: subst_ty_rho
single tau a :: subst_ty_mono
% Need free type variables (not sure how to generate it for contexts)
freevars
t x :: fv
sig a :: ftv_poly
rho a :: ftv_rho
tau a :: ftv_mono
%% values %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
grammar
value, v :: 'val_' ::=
| i :: :: nat
| \ x . t :: :: abs
(+ bind x in t +)
{{ tex \lambda [[x]] . [[t]] }}
| \ ( annot x sig ) . t :: :: typed_abs
(+ bind x in t +)
{{ tex \lambda ([[x]] :: [[sig]]). [[t]] }}
ctx {{ tex \text{Typing contexts} }}, G {{ tex \Gamma }} :: '' ::= {{ coq list ( atom * ty_poly ) }}
| empty :: :: Empty {{ coq nil }}
{{ tex \epsilon }}
{{ com empty context }}
| G , x : sig :: :: Cons {{ coq (([[x]]~[[sig]])++[[G]]) }}
{{ com assumption }}
embed
{{ coq
Fixpoint ftv_mono_ctx (G:ctx) : vars :=
match G with
| nil => {}
| (_, sig) :: tl => (ftv_mono_ty_poly sig) \u (ftv_mono_ctx tl)
end.
}}
grammar
fv :: 'fv_' ::=
| fv ( t ) :: M :: tm
{{ coq fv_tm [[t]] }}
ftvany :: 'ftv_' ::=
| ftv ( tau ) :: M :: mono
{{ coq ftv_mono_ty_mono[[tau]] }}
{{ tex [[ftv]]([[tau]])}}
| ftv ( rho ) :: M :: rho
{{ coq ftv_mono_ty_rho[[rho]] }}
{{ tex [[ftv]]([[rho]])}}
| ftv ( sig ) :: M :: poly
{{ coq ftv_mono_ty_poly[[sig]] }}
{{ tex [[ftv]]([[sig]])}}
| ftv ( G ) :: M :: ctx
{{ coq ftv_mono_ctx[[G]] }}
{{ tex [[ftv]]([[G]])}}
subrules
v <:: t
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Terminals %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
grammar
terminals :: 'terminals_' ::=
| --> :: :: step {{ tex \longrightarrow }}
| -> :: :: arrow {{ tex \to }}
| annot :: :: annot {{ tex :: }}
| fv :: :: fv {{ tex \text{fv} }}
| ftv :: :: ftv {{ tex \text{ftv} }}
| in :: :: in {{ tex \in }}
| notin :: :: notin {{ tex \not\in }}
| |- :: :: entails {{ tex \vdash }}
formula :: 'formula_' ::=
| judgement :: :: judgement
| is_value v :: :: is_value
{{ coq is_value_of_tm [[v]] }}
{{ tex \mathsf{value}\,[[v]] }}
| uniq G :: :: uniqG
{{ coq uniq [[G]] }}
{{ tex \mathsf{uniq}\,[[G]] }}
| closed sig :: :: closed_sig
{{ coq ftv_mono_ty_poly[[sig]][=]{} }}
{{ tex \mathsf{closed}\,[[sig]] }}
| a notin ftvany :: :: in_ftv
% (+ bind a in ftvany +)
{{ coq [[a]]`notin`[[ftvany]] }}
{{ tex [[a]][[notin]][[ftvany]] }}
| x : sig in G :: :: inG
{{ coq binds[[x]][[sig]][[G]] }}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Rules
defns
JTyping :: '' ::=
defn
G |- t : sig :: :: typing :: 'typ_'
{{ tex [[G]][[|-]][[t]]:[[sig]] }}
by
-------------- :: int
G |- i : Int
uniq G
x : sig in G
-------------- :: var
G |- i : sig
G, x : tau1 |- t : tau2
------------------------------- :: abs
G |- (\x . t) : (tau1 -> tau2)
G |- t : tau1 -> tau2
G |- u : tau1
--------------------- :: app
G |- t u : tau2
G |- u : sig
G, x : sig |- t : rho
--------------------------- :: let
G |- let x = u in t : rho
closed sig
G |- t : sig
--------------------------- :: annot
G |- (annot t sig) : sig
% b notin ftv (G)
G |- t : rho
------------------------- :: gen
G |- t : forall a . rho
G |- t : forall a . rho
------------------------------ :: inst
G |- t : [ a |-> tau ] rho
defns
JStep :: '' ::=
defn
t --> u :: :: step :: 'step_'
{{ tex [[t]] [[-->]] [[u]] }}
by
u --> u'
------------------------------------ :: let1
let x = u in t --> let x = u' in t
--------------------------------- :: let
let x = v in t --> t [ x ~> v ]
t --> t'
------------------------------ :: app1
t u --> t' u
u --> u'
------------------------------ :: app2
(\ x . t) u --> (\ x . t) u'
------------------------------ :: app
(\ x . t) v --> t [ x ~> v ]
u --> u'
------------------------------------------------------ :: annot_app2
(\ (annot x sig) . t) u --> (\ (annot x sig) . t) u'
------------------------------------------------------ :: annot_app
(\ (annot x sig) . t) v --> t [ x ~> v ]
------------------- :: erase
annot t sig --> t