Source file build.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
(** A core internal module to support term building. Do not use outside of core
directly.
Basic Idea
==========
There is a stack of to be built terms. When the construction of term starts,
the term can get some type requirement.
Invariant
=========
- Only terms which are welltyped in the context are constructed.
- All terms on the stack are welltyped and satisfy their requirements or
have still a chance to satisfy their requirement. The satisfaction of the
requirements is checked by putting a term to the stack.
Used Typing Rules
=================
Product
-------
Gamma |- A: sA
Gamma, x:A |- B: sB
Gamma |- pi_sort(sA,sB) <= s
-----------------------------------
Gamma |- (all (x: A). B) : s
sA sR pi_sort(sA,sR)
----------------------------------------------
* Proposition Proposition
Proposition Any(i) Any(i)
Any(i) Any(j) Any(max(i,j))
At the start there is on top of the stack an empty term with an optional
requirement of its return type. The required type, if present, must be a
sort.
Depending of the required type we push a new item onto the stack. If the
required type is a proposition, then the required type of the argument
type is Any. If the required type of the whole product is [Any(i)], then
the required type of the argument type is [Any(i)].
We build the argument type and pop it and push is as a bound variable.
Then we push a new item for the result type with the same required type
as the requirement of the product term onto the stack and build the
result type.
Finally we pop the result type and the binder and put the product term
onto the top of the stack.
Application
-----------
Gamma |- f: all (x: A). B
Gamma |- a: A'
Gamma |- A' <= A -- subtype
----------------------------------------
Gamma |- f a: B[x:=a]
We start to build [f a] with a possible requirement which has to be
satisfied after being applied to [nargs] arguments.
[start_application] increments nargs by one. Then it builds [f]. The
function term can only be built if its type is a product type.
Then we start a new term with the required type [A] from the type of
[f].
Lambda
------
Gamma |- (all (x: A): B) : s
Gamma, x:A |- e: B
--------------------------------------------
Gamma |- (\ (x: A) := e): all (x: A): B
*)
open Fmlib
open Module_types
open Common
module Make (Info: ANY) =
struct
module Algo = Gamma_algo.Make (Gamma_holes)
module Uni = Unifier.Make (Gamma_holes)
type name =
Info.t * string
type requirement =
| No_requirement
| Some_type
| Result_type of Term.typ * int
type item = {
requirement: requirement;
satisfied: bool;
nargs: int;
term: (Info.t
* Term.t
* Term.typ
* int
) option;
}
type binder = {
info: Info.t;
sort: Sort.t;
binder_level: int;
name_map_previous: Name_map.t;
}
type t = {
name_map: Name_map.t;
gh: Gamma_holes.t;
stack: item list;
binders: binder list;
}
type problem = Info.t * Type_error.t
type 'a res = ('a, problem) result
let make (context: Context.t): t =
let gamma = Context.gamma context
and name_map = Context.name_map context
in
{
name_map;
gh = Gamma_holes.make gamma;
stack = [];
binders = [];
}
let empty: t =
make Context.empty
let count (bc: t): int =
Gamma_holes.count bc.gh
let count_base (bc: t): int =
Gamma_holes.count_base bc.gh
let string_of_term (term: Term.t) (bc: t): string =
Term_printer.string_of_term
term
(Gamma_holes.context bc.gh)
let term_to_context (term: Term.t) (n: int) (bc: t): Term.t =
assert (n <= count bc);
Term.up (count bc - n) term
let into_new_binder (idx: int) (term: Term.t): Term.t
=
Term.map
(fun i ->
if i = idx then
0
else
i + 1)
term
let sort_of_term (term: Term.typ): Sort.t =
match term with
| Sort sort ->
sort
| _ ->
assert false
let make_pi
(arg_name: string)
(arg_idx: int)
(arg_typ: Term.typ)
(arg_sort: Sort.t)
(res_typ: Term.typ)
(res_sort: Term.typ)
(bc: t)
: Term.typ * Term.typ
=
Term.make_product
arg_name
true
(Algo.is_kind arg_typ bc.gh)
arg_typ
(into_new_binder arg_idx res_typ)
,
(Term.Sort
(Sort.pi_sort
arg_sort
(sort_of_term res_sort)))
let push_item
(requirement: requirement)
(nargs: int)
(bc: t)
: t
=
{bc with
stack =
{requirement; nargs; satisfied = false; term = None}
::
bc.stack}
let split_stack (bc: t): item * item list =
List.split_head_tail bc.stack
let split_binders (bc: t): binder * binder list =
List.split_head_tail bc.binders
let map_top (f: item -> item res): t -> t res =
fun bc ->
let top, stack = split_stack bc in
Result.map
(fun item -> {bc with stack = item :: stack})
(f top)
let map_top0 (f: item -> item): t -> t =
fun bc ->
match map_top (fun item -> Ok (f item)) bc with
| Ok bc ->
bc
| Error _ ->
assert false
let get_top (bc: t): Term.t * Term.typ * item list =
let item, stack = split_stack bc
in
match item.term with
| None ->
assert false
| Some (_, term, typ, n) ->
term_to_context term n bc,
term_to_context typ n bc,
stack
let pop_top (bc: t): Term.t * Term.typ * t =
let term, typ, stack = get_top bc
in
term, typ, {bc with stack}
let pop_binder (bc: t): string * int * Term.typ * Sort.t * t =
let binder, binders = split_binders bc
in
Gamma_holes.name_at_level binder.binder_level bc.gh,
Gamma_holes.index_of_level binder.binder_level bc.gh,
Gamma_holes.type_at_level binder.binder_level bc.gh,
binder.sort,
{bc with
gh = Gamma_holes.pop_bound bc.gh;
binders;
name_map = binder.name_map_previous}
let get_final (bc: t): Term.t * Term.typ =
let term, typ, stack = get_top bc
in
assert (stack = []);
let cnt = count bc
and cnt0 = count_base bc
in
match
Term.down (cnt - cnt0) term,
Term.down (cnt - cnt0) typ
with
| Some term, Some typ ->
term, typ
| _, _ ->
assert false
let set_term
(info: Info.t) (term: Term.t) (typ: Term.typ)
: t -> t
=
fun bc ->
let item, stack = split_stack bc in
{bc with
stack =
{item with
term =
Some (info, term, typ, count bc)}
::
stack}
let put_term
(info: Info.t) (term: Term.t) (typ: Term.typ)
: t -> t res
=
fun bc ->
let item, _ = split_stack bc
in
let put bc =
Ok (set_term info term typ bc)
in
if item.nargs = 0 then
match item.requirement with
| No_requirement ->
put bc
| Some_type ->
if Term.is_sort typ then
put bc
else
Error (info, Type_error.Not_a_type)
| Result_type (req_typ, n) ->
(
let req_typ = term_to_context req_typ n bc
in
match Uni.unify typ req_typ true bc.gh with
| None ->
Error (
info,
Type_error.Wrong_type (
req_typ,
typ,
Gamma_holes.context bc.gh
))
| Some gh ->
put {bc with gh}
)
else
let typ_n = Algo.key_normal typ bc.gh
and cnt = count bc in
match typ_n with
| Term.Pi (arg_tp, _, _) ->
Ok (
set_term info term typ_n bc
|> push_item (Result_type (arg_tp, cnt)) 0
)
| _ ->
Error (info,
Type_error.Not_a_function (
typ, Gamma_holes.context bc.gh)
)
let check_naming_convention
(info: Info.t) (name: string) (typ: Term.typ) (bc: t)
: t res
=
let is_lower, is_upper =
if String.length name > 0 then
let c = name.[0] in
Char.is_lower c, Char.is_upper c
else
false, false
and tv fail =
if not fail then
Ok bc
else
Error (info, Type_error.Naming_type_variable)
and no_tv fail =
if not fail then
Ok bc
else
Error (info, Type_error.Naming_no_type_variable)
in
match Algo.sort_of_kind typ bc.gh with
| None ->
no_tv is_upper
| Some Sort.Proposition ->
no_tv is_upper
| Some Sort.Any _ ->
tv is_lower
let make_sort (info: Info.t) (s: Sort.t) (bc: t): t res =
let add =
put_term
info
(Term.Sort s)
(Term.Sort (Sort.type_of s))
in
match s with
| Sort.Proposition ->
add bc
| Sort.Any i ->
if i > 0 then
Error (info, Type_error.Higher_universe i)
else
add bc
let make_identifier (info: Info.t) (name: string): t -> t res =
fun bc ->
if name = "Proposition" then
make_sort info Sort.Proposition bc
else if name = "Any" then
make_sort info (Sort.Any 0) bc
else
match Name_map.find name bc.name_map with
| [] ->
Error (info, Type_error.Name_not_found name)
| [level] ->
let level = Gamma_holes.adapted_level level bc.gh in
let typ = Gamma_holes.type_at_level level bc.gh
and idx = Gamma_holes.index_of_level level bc.gh
in
put_term info (Term.Variable idx) typ bc
| _ ->
Error (info,
Type_error.Not_yet_implemented
"<name overloading>")
let start_term: t -> t =
push_item No_requirement 0
let start_typed_term (bc: t): t =
let typ, _, _ = get_top bc in
push_item (Result_type (typ, count bc)) 0 bc
let start_type: t -> t =
push_item Some_type 0
let start_application: t -> t =
map_top0
(fun item ->
assert (item.term = None);
{item with nargs = item.nargs + 1})
let end_application (info: Info.t) (bc: t): t res =
let arg, _, bc = pop_top bc in
let f, typ, _ = get_top bc in
let bc =
map_top0
(fun item ->
assert (item.nargs > 0);
{item with nargs = item.nargs - 1})
bc
in
match typ with
| Pi (_, res_tp, _) ->
put_term
info
(Term.application f arg)
(Term.apply res_tp arg)
bc
| _ ->
assert false
let start_binder ((info, name): name): t -> t res =
fun bc ->
let open Result in
let typ, sort, stack = get_top bc in
let sort = sort_of_term sort in
check_naming_convention info name typ bc
>>= fun _ ->
Ok
{name_map =
Name_map.add_local name bc.name_map;
stack;
gh =
Gamma_holes.push_bound name true typ bc.gh;
binders =
{info;
sort;
name_map_previous = bc.name_map;
binder_level = count bc
}
::
bc.binders
}
let start_pi (info: Info.t): t -> t res =
fun bc ->
let item, _ = split_stack bc
and cnt = count bc in
assert (item.term = None);
match item.requirement with
| No_requirement | Some_type ->
Ok (push_item Some_type 0 bc)
| Result_type (Term.(Sort sort), _) ->
(
match sort with
| Sort.Proposition ->
Ok (push_item
(Result_type (Term.Sort (Sort.Any 1), cnt))
0
bc)
| _ ->
Ok (push_item
(Result_type (Term.Sort sort, cnt))
0
bc)
)
| Result_type (req_typ,n) ->
Error (info,
Type_error.No_type_allowed (
term_to_context req_typ n bc,
Gamma_holes.context bc.gh))
let start_pi_result: t -> t res =
fun bc ->
let item, _ = split_stack bc in
assert (item.term = None);
match item.requirement with
| No_requirement | Some_type ->
Ok (push_item Some_type 0 bc)
| Result_type (req_typ, n) ->
Ok (push_item
(Result_type (req_typ, n))
0
bc)
let end_pi (info: Info.t): t -> t res =
fun bc ->
let res_typ, res_sort, bc = pop_top bc
in
let arg_name, arg_idx, arg_typ, arg_sort, bc = pop_binder bc
in
let pi_term, pi_type =
make_pi arg_name arg_idx arg_typ arg_sort res_typ res_sort bc
in
put_term info pi_term pi_type bc
end