-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmicro-timl-util.sml
291 lines (244 loc) · 7.22 KB
/
micro-timl-util.sml
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
functor MicroTiMLUtilFun(MicroTiMLDef : SIG_MICRO_TIML_DEF) : SIG_MICRO_TIML_UTIL =
struct
open Util
structure MicroTiMLDef = MicroTiMLDef
open MicroTiMLDef
fun extract_judge_kdeq ke =
case ke of
KdEqKType j => j
| KdEqKArrow (j, _, _) => j
| KdEqBaseSort j => j
| KdEqSubset (j, _, _) => j
| KdEqSubsetElimLeft (j, _) => j
| KdEqSubsetElimRight (j, _) => j
fun extract_judge_proping pr =
case pr of
PrAdmit j => j
fun extract_judge_kinding kd =
case kd of
KdVar j => j
| KdConst j => j
| KdBinOp (j, _, _) => j
| KdIte (j, _, _, _) => j
| KdArrow (j, _, _, _) => j
| KdAbs (j, _, _) => j
| KdApp (j, _, _) => j
| KdTimeAbs (j, _) => j
| KdTimeApp (j, _, _) => j
| KdQuan (j, _, _) => j
| KdRec (j, _, _) => j
| KdTypeNat (j, _) => j
| KdTypeArr (j, _, _) => j
| KdEq (j, _, _) => j
| KdUnOp (j, _) => j
| KdAdmit j => j
fun extract_judge_wfkind wk =
case wk of
WfKdType j => j
| WfKdArrow (j, _, _) => j
| WfKdBaseSort j => j
| WfKdSubset (j, _, _) => j
| WfKdAdmit j => j
fun extract_judge_wfprop wp =
case wp of
WfPropTrue j => j
| WfPropFalse j => j
| WfPropBinConn (j, _, _) => j
| WfPropNot (j, _) => j
| WfPropBinPred (j, _, _) => j
| WfPropQuan (j, _) => j
fun extract_judge_tyeq te =
case te of
TyEqVar j => j
| TyEqConst j => j
| TyEqBinOp (j, _, _) => j
| TyEqIte (j, _, _, _) => j
| TyEqArrow (j, _, _, _) => j
| TyEqApp (j, _, _) => j
| TyEqBeta j => j
| TyEqBetaRev j => j
| TyEqQuan (j, _, _) => j
| TyEqRec (j, _, _) => j
| TyEqAbs j => j
| TyEqTimeAbs j => j
| TyEqTimeApp j => j
| TyEqTrans (j, _, _) => j
| TyEqUnOp (j, _) => j
| TyEqTypeNat (j, _) => j
| TyEqTypeArr (j, _, _) => j
| TyEqNat (j, _) => j
| TyEqTime (j, _) => j
fun extract_expr_value v =
case v of
VConst e => e
| VPair (e, _, _) => e
| VInj (e, _) => e
| VAbs e => e
| VAbsC e => e
| VPack (e, _) => e
| VFold (e, _) => e
fun extract_judge_typing ty =
case ty of
TyVar j => j
| TyApp (j, _, _) => j
| TyAbs (j, _, _) => j
| TyAppC (j, _, _) => j
| TyAbsC (j, _, _, _) => j
| TyRec (j, _, _) => j
| TyFold (j, _, _) => j
| TyUnfold (j, _) => j
| TyPack (j, _, _, _) => j
| TyUnpack (j, _, _) => j
| TyConst j => j
| TyPair (j, _, _) => j
| TyProj (j, _) => j
| TyInj (j, _, _) => j
| TyCase (j, _, _, _) => j
| TyNew (j, _, _) => j
| TyRead (j, _, _, _) => j
| TyWrite (j, _, _, _, _) => j
| TySubTy (j, _, _) => j
| TySubTi (j, _, _) => j
| TyHalt (j, _) => j
| TyLet (j, _, _) => j
| TyFix (j, _, _) => j
| TyPrimBinOp (j, _, _) => j
fun extract_p_bin_conn (PBinConn a) = a
| extract_p_bin_conn _ = raise (Impossible "extract_p_bin_conn")
fun extract_p_quan (PQuan a) = a
| extract_p_quan _ = raise (Impossible "extract_p_quan")
fun extract_p_bin_pred (PBinPred a) = a
| extract_p_bin_pred _ = raise (Impossible "extract_p_bin_pred")
fun extract_c_quan (CQuan a) = a
| extract_c_quan _ = raise (Impossible "extract_c_quan")
fun extract_c_bin_op (CBinOp a) = a
| extract_c_bin_op _ = raise (Impossible "extract_c_bin_op")
fun extract_c_un_op (CUnOp a) = a
| extract_c_un_op _ = raise (Impossible "extract_c_un_op")
fun extract_c_time_app (CTimeApp a) = a
| extract_c_time_app _ = raise (Impossible "extract_c_time_app")
fun extract_c_arrow (CArrow a) = a
| extract_c_arrow _ = raise (Impossible "extract_c_arrow")
fun extract_c_sum (CBinOp (CBTypeSum, c1, c2)) = (c1, c2)
| extract_c_sum _ = raise (Impossible "extract_c_sum")
fun extract_c_prod (CBinOp (CBTypeProd, c1, c2)) = (c1, c2)
| extract_c_prod _ = raise (Impossible "extract_c_prod")
fun extract_c_type_nat (CTypeNat a) = a
| extract_c_type_nat _ = raise (Impossible "extract_c_type_nat")
fun extract_c_type_arr (CTypeArr a) = a
| extract_c_type_arr _ = raise (Impossible "extract_c_type_arr")
fun extract_c_rec (CRec a) = a
| extract_c_rec _ = raise (Impossible "extract_c_rec")
fun extract_c_abs (CAbs a) = a
| extract_c_abs _ = raise (Impossible "extract_c_abs")
fun extract_c_const (CConst a) = a
| extract_c_const _ = raise (Impossible "extract_c_const")
fun extract_k_time_fun (KBaseSort (BSTimeFun a)) = a
| extract_k_time_fun _ = raise (Impossible "extract_k_time_fun")
fun extract_k_arrow (KArrow a) = a
| extract_k_arrow _ = raise (Impossible "extract_k_arrow")
fun extract_e_inj (EUnOp (EUInj c, e)) = (c, e)
| extract_e_inj _ = raise (Impossible "extract_e_inj")
fun extract_e_proj (EUnOp (EUProj p, e)) = (p, e)
| extract_e_proj _ = raise (Impossible "extract_e_proj")
fun extract_e_abs (EAbs a) = a
| extract_e_abs _ = raise (Impossible "extract_e_abs")
fun extract_e_abs_c (EAbsC a) = a
| extract_e_abs_c _ = raise (Impossible "extract_e_abs_c")
fun extract_e_prim_bin_op (EBinOp (EBPrim opr, e1, e2)) = (opr, e1, e2)
| extract_e_prim_bin_op _ = raise (Impossible "extract_e_prim_bin_op")
fun extract_e_pack (EPack a) = a
| extract_e_pack _ = raise (Impossible "extract_e_pack")
val str_time = Time.str_time
val str_nat = Nat.str_nat
fun str_cstr_const cn =
case cn of
CCIdxTT => "tt"
| CCIdxTrue => "true"
| CCIdxFalse => "false"
| CCIdxNat n => str_nat n
| CCTime r => str_time r
| CCTypeUnit => "Unit"
| CCTypeInt => "Int"
fun str_cstr_bin_op opr =
case opr of
CBTimeAdd => "+r"
| CBTimeMinus => "-r"
| CBTimeMult => "*r"
| CBTimeMax => "max"
| CBTimeMin => "min"
| CBTypeProd => "*"
| CBTypeSum => "+"
| CBNatAdd => "+n"
| CBNatMinus => "-n"
| CBNatMult => "*n"
fun str_cstr_un_op opr =
case opr of
CUNat2Time => "nat2time"
| CUBool2Nat => "bool2nat"
| CUCeil => "ceil"
| CUFloor => "floor"
| CULog n => "log_" ^ str_int n
| CUDiv n => "/" ^ str_int n
fun str_quan q =
case q of
QuanForall => "forall"
| QuanExists => "exists"
fun str_sort b =
case b of
BSNat => "nat"
| BSUnit => "unit"
| BSBool => "bool"
| BSTimeFun arity => "time_fun(" ^ str_int arity ^ ")"
fun str_prop_bin_conn opr =
case opr of
PBCAnd => "/\\"
| PBCOr => "\\/"
| PBCImply => "->"
| PBCIff => "<->"
fun str_prop_bin_pred opr =
case opr of
PBTimeLe => "<=r"
| PBTimeLt => "<r"
| PBTimeEq => "=r"
| PBTimeGe => ">=r"
| PBTimeGt => ">r"
| PBBigO arity => "BigO"
| PBNatEq => "=n"
| PBNatLe => "<=n"
| PBNatLt => "<n"
| PBNatGe => ">=n"
| PBNatGt => ">n"
fun str_expr_const cn =
case cn of
ECTT => "()"
| ECInt i => str_int i
| ECNat n => str_nat n
fun str_projector p =
case p of
ProjFst => "fst"
| ProjSnd => "snd"
fun str_injector inj =
case inj of
InjInl => "inl"
| InjInr => "inr"
fun str_expr_un_op opr =
case opr of
EUProj p => str_projector p
| EUInj inj => str_injector inj
| EUFold => "fold"
| EUUnfold => "unfold"
fun str_prim_expr_bin_op opr =
case opr of
PEBIntAdd => "+"
fun str_expr_bin_op opr =
case opr of
EBPrim opr => str_prim_expr_bin_op opr
| EBApp => ""
| EBPair => ","
| EBNew => "new"
| EBRead => "read"
fun str_expr_tri_op opr =
case opr of
ETWrite => "write"
end