Source file gamma_holes.ml
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
open Fmlib
open Common
type term_n = Term.t * int
module Entry =
struct
type t =
| Hole of
term_n option
* Int_set.t
| Bound of int
let hole: t =
Hole (None, Int_set.empty)
let make_bound (n: int): t =
Bound n
let is_hole (loc: t): bool =
match loc with
| Hole _ ->
true
| Bound _ ->
false
let is_unfilled (loc: t): bool =
match loc with
| Hole (None, _ ) ->
true
| _ ->
false
let is_bound (loc: t): bool =
not (is_hole loc)
let value (loc: t): term_n option =
match loc with
| Hole (value, _) ->
value
| _ ->
None
let users (loc: t): Int_set.t =
match loc with
| Hole (_, users) ->
users
| _ ->
assert false
let add_users (user: int) (users: Int_set.t) (loc: t): t =
match loc with
| Hole (value, users0) ->
let set = Int_set.add user (Int_set.union users0 users) in
Hole (value, set)
| _ ->
assert false
let set_value (term_n: term_n) (loc: t): t =
match loc with
| Hole (_, users) ->
Hole (Some term_n, users)
| _ ->
assert false
let bound_number (loc: t): int =
match loc with
| Bound n ->
n
| _ ->
assert false
end
type t = {
base0: Gamma.t;
base: Gamma.t;
entries: Entry.t array;
bounds: (int * bool) array;
nholes: int;
}
let make (base: Gamma.t): t =
{
base0 = base;
base;
entries = [||];
bounds = [||];
nholes = 0;
}
let string_of_term (term: Term.t) (gh: t): string =
Term_printer.string_of_term term gh.base
let _ = string_of_term
let count (gh: t): int =
Gamma.count gh.base
let count_base (gh: t): int =
Gamma.count gh.base0
let count_bounds (gh: t): int =
Array.length gh.bounds
let count_entries (gh: t): int =
Array.length gh.entries
let context (gh: t): Gamma.t =
gh.base
let base_context (gh: t): Gamma.t =
gh.base0
let is_valid_index (idx: int) (gh: t): bool =
Gamma.is_valid_index idx gh.base
let name_of_index (idx: int) (gh: t): string =
Gamma.name_of_index idx gh.base
let index_of_level (level: int) (gh: t): int =
Gamma.index_of_level level gh.base
let level_of_index (idx: int) (gh: t): int =
Gamma.level_of_index idx gh.base
let adapted_level (level: int) (gh: t): int =
let cnt0 = count_base gh in
if level < cnt0 then
level
else (
let i_bound = level - cnt0 in
assert (i_bound < count_bounds gh);
fst gh.bounds.(i_bound)
)
let is_entry (idx: int) (gh: t): bool =
idx < count_entries gh
let entry_of_index (idx: int) (gh: t): Entry.t =
assert (is_entry idx gh);
let level = level_of_index idx gh in
gh.entries.(level - count_base gh)
let is_hole (idx: int) (gh: t): bool =
is_entry idx gh
&& Entry.is_hole (entry_of_index idx gh)
let is_unfilled (idx: int) (gh: t): bool =
is_entry idx gh
&&
Entry.is_unfilled (entry_of_index idx gh)
let is_bound (idx: int) (gh: t): bool =
is_entry idx gh
&&
Entry.is_bound (entry_of_index idx gh)
let bound_number (idx: int) (gh: t): int =
assert (is_bound idx gh);
Entry.bound_number (entry_of_index idx gh)
let variable_of_bound (i: int) (gh: t): Term.t =
assert (i < count_bounds gh);
Term.Variable
(index_of_level
(fst gh.bounds.(i))
gh)
let value (idx: int) (gh: t): Term.t option =
if is_entry idx gh then
Option.map
(fun (term, n) ->
assert (n <= count gh);
Term.up (count gh - n) term)
(Entry.value (entry_of_index idx gh))
else
None
let has_value (idx: int) (gh: t): bool =
Option.has (value idx gh)
let collect_holes
(cnt0: int)
(filled: bool)
(term: Term.t)
(gh: t)
: Int_set.t
=
let cntbase =count_base gh
in
let delta = max (cnt0 - cntbase) 0
in
let idx_beyond = count_entries gh - delta
in
Term.fold_free
(fun idx set ->
if idx < idx_beyond then
let entry = entry_of_index idx gh
in
if
Entry.is_hole entry
&& ((Entry.value entry <> None) = filled)
then
Int_set.add
(Gamma.level_of_index idx gh.base)
set
else
set
else
set)
term
Int_set.empty
let unfilled_holes (cnt0: int) (term: Term.t) (gh: t): Int_set.t =
collect_holes cnt0 false term gh
let expand (term: Term.t) (gh: t): Term.t =
Term.substitute_with_beta
(fun i ->
match value i gh with
| None ->
Variable i
| Some term ->
term)
term
let is_expanded (term: Term.t) (gh: t): bool =
Int_set.is_empty
(collect_holes 0 true term gh)
let term_of_term_n ((term,n): Term.t_n) (gh: t): Term.t =
expand (Term.up (count gh - n) term) gh
let name_at_level (level: int) (gh: t): string =
Gamma.name_at_level level gh.base
let type_at_level (level: int) (gh: t): Term.typ =
let typ = Gamma.type_at_level level gh.base in
if count_base gh <= level then
expand typ gh
else
typ
let type_of_variable (idx: int) (gh: t): Term.typ =
type_at_level (Gamma.level_of_index idx gh.base) gh
let type_of_literal (value: Value.t) (gh: t): Term.typ =
Gamma.type_of_literal value gh.base
let definition_term (idx: int) (gh: t): Term.t option =
Gamma.definition_term idx gh.base
let fold_entries
(f: int -> int -> string -> Term.typ -> bool -> Term.t option -> 'a -> 'a)
(gh: t)
(a: 'a):
'a
=
let cnt0 = count_base gh
in
Array.foldi_left
(fun a k entry ->
let level = cnt0 + k in
let idx = index_of_level level gh in
f
level
idx
(name_at_level level gh)
(type_at_level level gh)
(Entry.is_hole entry)
(value idx gh)
a
)
a
gh.entries
let push_bound (name: string) (typed: bool) (typ: Term.typ) (gh: t): t =
{gh with
base = Gamma.push_local name typ gh.base;
entries =
Array.push
(Entry.make_bound (Array.length gh.bounds))
gh.entries;
bounds = Array.push (count gh, typed) gh.bounds;
}
let remove_bounds (n: int) (gh: t): t =
assert (n <= count_bounds gh);
{gh with bounds = Array.remove_last n gh.bounds}
let pop_bound: t -> t =
remove_bounds 1
let push_local (name: string) (typ: Term.typ) (gh: t): t =
push_bound name true typ gh
let push_named_hole (name: string) (typ: Term.typ) (gh: t): t =
{gh with
base = Gamma.push_local name typ gh.base;
entries = Array.push Entry.hole gh.entries;
nholes = gh.nholes + 1;
}
let push_hole (typ: Term.typ) (gh: t): t =
push_named_hole
("<" ^ string_of_int gh.nholes ^ ">")
typ
gh
let fill_hole0 (idx: int) (value: Term.t) (beta_reduce: bool) (gh: t): t =
assert (is_unfilled idx gh);
assert (not (Term.has_variable idx value));
let value = expand value gh
and cnt = count gh
and nentries = count_entries gh
and entries = Array.copy gh.entries
in
let cnt0 = cnt - nentries
and loc_level = Term.bruijn_convert idx nentries
in
let gh_new = {gh with entries}
in
entries.(loc_level) <-
Entry.set_value (value, cnt) entries.(loc_level);
let users = Entry.users entries.(loc_level) in
Int_set.iter
(fun unfilled ->
let iloc = unfilled - cnt0 in
entries.(iloc) <-
Entry.add_users (cnt0 + loc_level) users entries.(iloc))
(unfilled_holes cnt0 value gh);
Int_set.iter
(fun user ->
let i = user - cnt0 in
match Entry.value entries.(i) with
| Some (term,n) ->
let term = Term.up (count gh - n) term in
let term =
Term.substitute0
(fun k ->
if k = idx then
value
else
Term.Variable k)
beta_reduce
term
in
entries.(i) <- Entry.set_value (term, cnt) entries.(i)
| _ ->
assert false )
users;
gh_new
let fill_hole (idx: int) (value: Term.t) (gh: t): t =
fill_hole0 idx value false gh
let into_binder
(bnd0: int)
(nb: int)
(term: Term.t)
(gh: t)
: Term.typ
=
assert (bnd0 <= count_bounds gh);
let nentries = count_entries gh
in
Term.substitute
(fun idx ->
if nentries <= idx then
Variable (idx + nb)
else
let entry = entry_of_index idx gh in
if Entry.is_bound entry then
let i = Entry.bound_number entry in
if bnd0 <= i then
(
assert (i < bnd0 + nb);
Variable (Term.bruijn_convert (i - bnd0) nb)
)
else
Variable (idx + nb)
else
Variable (idx + nb))
term
let pi_lambda
(mk: string -> bool -> Term.typ -> Term.t -> Term.t)
(nbounds: int)
(inner: Term.t)
(gh: t)
: Term.t
=
assert (nbounds <= count_bounds gh);
let bnd0 = count_bounds gh - nbounds in
let into = into_binder bnd0 in
let rec make i exp =
if i = 0 then
exp
else
let i = i - 1 in
let name, typed, arg_tp =
let level, typed = gh.bounds.(bnd0 + i) in
name_at_level level gh,
typed,
into i (type_at_level level gh) gh
in
make i (mk name typed arg_tp exp)
in
make nbounds (into nbounds inner gh)
let pi (nargs: int) (res_tp: Term.typ) (gh: t): Term.typ =
assert (0 < nargs);
assert (nargs <= count_bounds gh);
pi_lambda Term.product0 nargs res_tp gh
let lambda (nargs: int) (exp: Term.t) (gh: t): Term.t =
assert (0 < nargs);
assert (nargs <= count_bounds gh);
pi_lambda Term.lambda0 nargs exp gh