-
Notifications
You must be signed in to change notification settings - Fork 6
/
collect-var.sml
238 lines (217 loc) · 7.02 KB
/
collect-var.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
(* collect open variables *)
structure CollectVar = struct
open LongId
open Subst
infixr 0 $
(* fun collect_var_aux_i_ibind f d acc (Bind (_, b) : ('a * 'b) ibind) = f (d + 1) acc b *)
(* fun collect_var_long_id d (id : long_id) : long_id list = *)
(* case id of *)
(* QID _ => [id] *)
(* | ID (x, r) => *)
(* if x >= d then [ID (x - d, r)] *)
(* else [] *)
(* local *)
(* fun f d(*depth*) acc b = *)
(* case b of *)
(* VarI x => collect_var_long_id d x @ acc *)
(* | IConst _ => acc *)
(* | UnOpI (opr, i, r) => f d acc i *)
(* | BinOpI (opr, i1, i2) => *)
(* let *)
(* val acc = f d acc i1 *)
(* val acc = f d acc i2 *)
(* in *)
(* acc *)
(* end *)
(* | Ite (i1, i2, i3, r) => *)
(* let *)
(* val acc = f d acc i1 *)
(* val acc = f d acc i2 *)
(* val acc = f d acc i3 *)
(* in *)
(* acc *)
(* end *)
(* | IAbs (b, bind, r) => *)
(* collect_var_aux_i_ibind f d acc bind *)
(* | UVarI a => acc *)
(* in *)
(* val collect_var_aux_i_i = f *)
(* fun collect_var_i_i b = f 0 [] b *)
(* end *)
(* local *)
(* fun f d acc b = *)
(* case b of *)
(* PTrueFalse _ => acc *)
(* | Not (p, r) => f d acc p *)
(* | BinConn (opr,p1, p2) => *)
(* let *)
(* val acc = f d acc p1 *)
(* val acc = f d acc p2 *)
(* in *)
(* acc *)
(* end *)
(* | BinPred (opr, i1, i2) => *)
(* let *)
(* val acc = collect_var_aux_i_i d acc i1 *)
(* val acc = collect_var_aux_i_i d acc i2 *)
(* in *)
(* acc *)
(* end *)
(* | Quan (q, bs, bind, r) => collect_var_aux_i_ibind f d acc bind *)
(* in *)
(* val collect_var_aux_i_p = f *)
(* fun collect_var_i_p b = f 0 [] b *)
(* end *)
(* local *)
(* fun f d acc b = *)
(* case b of *)
(* Basic s => acc *)
(* | Subset (b, bind, r) => collect_var_aux_i_ibind collect_var_aux_i_p d acc bind *)
(* | UVarS a => acc *)
(* | SAbs (s, bind, r) => collect_var_aux_i_ibind f d acc bind *)
(* | SApp (s, i) => *)
(* let *)
(* val acc = f d acc s *)
(* val acc = collect_var_aux_i_i d acc i *)
(* in *)
(* acc *)
(* end *)
(* in *)
(* val collect_var_aux_i_s = f *)
(* fun collect_var_i_s b = f 0 [] b *)
(* end *)
(* fun collect_var_aux_i_k d acc (_, sorts) = *)
(* foldl (fn (s, acc) => collect_var_aux_i_s d acc s) acc sorts *)
(* fun collect_var_aux_t_ibind f d acc (Bind (_, b) : ('a * 'b) ibind) = f d acc b *)
(* fun collect_var_aux_i_tbind f d acc (Bind (_, b) : ('a * 'b) tbind) = f d acc b *)
(* fun collect_var_aux_t_tbind f d acc (Bind (_, b) : ('a * 'b) tbind) = f (d + 1) acc b *)
(* local *)
(* fun f d acc b = *)
(* case b of *)
(* Arrow (t1, i, t2) => *)
(* let *)
(* val acc = f d acc t1 *)
(* val acc = collect_var_aux_i_i d acc i *)
(* val acc = f d acc t2 *)
(* in *)
(* acc *)
(* end *)
(* | TyNat (i, _) => collect_var_aux_i_i d acc i *)
(* | TyArray (t, i) => *)
(* let *)
(* val acc = f d acc t *)
(* val acc = collect_var_aux_i_i d acc i *)
(* in *)
(* acc *)
(* end *)
(* | Unit _ => acc *)
(* | Prod (t1, t2) => *)
(* let *)
(* val acc = f d acc t1 *)
(* val acc = f d acc t2 *)
(* in *)
(* acc *)
(* end *)
(* | UniI (s, bind, _) => *)
(* let *)
(* val acc = collect_var_aux_i_s d acc s *)
(* val acc = collect_var_aux_i_ibind f d acc bind *)
(* in *)
(* acc *)
(* end *)
(* | MtVar _ => acc *)
(* | MtApp (t1, t2) => *)
(* let *)
(* val acc = f d acc t1 *)
(* val acc = f d acc t2 *)
(* in *)
(* acc *)
(* end *)
(* | MtAbs (k, bind, _) => collect_var_aux_i_tbind f d acc bind *)
(* | MtAppI (t, i) => *)
(* let *)
(* val acc = f d acc t *)
(* val acc = collect_var_aux_i_i d acc i *)
(* in *)
(* acc *)
(* end *)
(* | MtAbsI (b, bind, r) => collect_var_aux_i_ibind f d acc bind *)
(* | BaseType _ => acc *)
(* | UVar _ => acc *)
(* | TDatatype _ => raise Unimpl "collect_var_i_mt()/TDatatype" *)
(* in *)
(* val collect_var_aux_i_mt = f *)
(* fun collect_var_i_mt b = f 0 [] b *)
(* end *)
(* local *)
(* fun f d acc b = *)
(* case b of *)
(* Arrow (t1, i, t2) => *)
(* let *)
(* val acc = f d acc t1 *)
(* val acc = f d acc t2 *)
(* in *)
(* acc *)
(* end *)
(* | TyNat (i, _) => acc *)
(* | TyArray (t, i) => f d acc t *)
(* | Unit _ => acc *)
(* | Prod (t1, t2) => *)
(* let *)
(* val acc = f d acc t1 *)
(* val acc = f d acc t2 *)
(* in *)
(* acc *)
(* end *)
(* | UniI (s, bind, _) => collect_var_aux_t_ibind f d acc bind *)
(* | MtVar x => collect_var_long_id d x @ acc *)
(* | MtApp (t1, t2) => *)
(* let *)
(* val acc = f d acc t1 *)
(* val acc = f d acc t2 *)
(* in *)
(* acc *)
(* end *)
(* | MtAbs (k, bind, _) => collect_var_aux_t_tbind f d acc bind *)
(* | MtAppI (t, i) => f d acc t *)
(* | MtAbsI (s, bind, r) => collect_var_aux_t_ibind f d acc bind *)
(* | BaseType _ => acc *)
(* | UVar _ => acc *)
(* | TDatatype _ => raise Unimpl "collect_var_t_mt()/TDatatype" *)
(* in *)
(* val collect_var_aux_t_mt = f *)
(* fun collect_var_t_mt b = f 0 [] b *)
(* end *)
fun collect_var_aux_long_id output x id =
let
val () =
case id of
QID _ => output id
| ID (y, r) =>
if y >= x then output $ ID (y - x, r)
else ()
in
id
end
(* fun adapt f x _ b = f x b *)
(* fun unadapt f d b = f d 0 b *)
fun adapt f output x env = f output (x + env)
fun collect_var_aux_i_i output x =
IdxShiftVisitor.on_i_i $ adapt collect_var_aux_long_id output x
fun collect_var_aux_i_s output x =
IdxShiftVisitor.on_i_s $ adapt collect_var_aux_long_id output x
fun collect_var_aux_i_mt output x = TypeShiftVisitor.on_i_mt (adapt collect_var_aux_i_i output x, adapt collect_var_aux_i_s output x)
fun collect_var_aux_t_mt output x = TypeShiftVisitor.on_t_mt (adapt collect_var_aux_long_id output x)
fun collect_var_0 f b =
let
val r = ref []
fun output id = push_ref r id
val _ = f output 0 b
in
!r
end
fun collect_var_i_i b = collect_var_0 collect_var_aux_i_i b
fun collect_var_i_s b = collect_var_0 collect_var_aux_i_s b
fun collect_var_i_mt b = collect_var_0 collect_var_aux_i_mt b
fun collect_var_t_mt b = collect_var_0 collect_var_aux_t_mt b
end