-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaturate.ml
1520 lines (1370 loc) · 46.4 KB
/
saturate.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
open Flags;;
open Utilities;;
open Syntax;;
open Grammar;;
open Automaton;;
open Stype;;
open Type;;
type tstate = ity * Stype.st
type delta = (tstate * tr) list
and tr = TrNT of nameNT | TrT of nameT
| TrApp of tstate * tstate list
let flag_updated_nt = ref false
let flag_updated_termid = ref false
let flag_overwritten = ref false
let merge_ty ty1 ty2 =
merge_and_unify compare_ity ty1 ty2
let add_ity ity ty =
if List.exists (fun ity1 -> subtype ity1 ity) ty
then ty
else
merge_ty [ity] (List.filter (fun ity1 -> not(subtype ity ity1)) ty)
let compare_tys tys1 tys2 =
compare (List.map (List.map id_of_ity) tys1) (List.map (List.map id_of_ity) tys2)
let merge_tyss tyss1 tyss2 =
merge_and_unify compare_tys tyss1 tyss2
let rec eq_ty ty1 ty2 =
match (ty1,ty2) with
([],[]) -> true
| (ity1::ty1',ity2::ty2') ->
(id_of_ity ity1 = id_of_ity ity2)
&& eq_ty ty1' ty2'
| _ -> false
let rec eq_tys tys1 tys2 =
match (tys1,tys2) with
([],[]) -> true
| (ty1::tys1',ty2::tys2') ->
eq_ty ty1 ty2 && eq_tys tys1' tys2'
| _ -> false
let add_tyss tys tyss =
if List.exists (fun tys1 -> eq_tys tys tys1) tyss then
tyss
else tys::tyss
let rec eq_tyarray_mask j mask tys1 tys2 =
List.for_all (fun i ->eq_ty tys1.(i-j) tys2.(i-j)) mask
let emptyTE = []
let nteref = ref [||]
let rec register_linearity ity a i =
match ity with
ItyQ _ -> ()
| ItyFun(_,ty,ity1) ->
(if List.length ty>1 then a.(i) <- false (* non-linear *));
register_linearity ity1 a (i+1)
let mk_linearity_tab() =
Hashtbl.iter (fun c tyarray ->
let arity = Grammar.arity_of_t c in
let a = Array.make arity true in
Array.iter (fun ty ->
List.iter (fun ity ->
register_linearity ity a 0) ty) tyarray;
Hashtbl.add Ai.tab_linearity c a
)
cte
(** convert a transition q->q1...qn(=qs) to a negated type **)
let rec tr2ty_sub q qs =
match qs with
[] -> (ItyQ(Ai.state2id q), [])
| q1::qs' ->
let (top,ty) = tr2ty_sub q qs' in
let ty'= List.map (fun ity -> mkItyFun([],ity)) ty in
if q1="top" then
(mkItyFun([],top), ty')
else
(mkItyFun([],top), (mkItyFun([ItyQ(Ai.state2id q1)],top))::ty')
and tr2ty q qs =
let (_,ty) = tr2ty_sub q qs in
ty
let arity_of a m =
List.assoc a m.alpha
let rec add_topty n ity =
if n=0 then ity
else add_topty (n-1) (mkItyFun([],ity))
let build_ity q n vs =
let rec go = function
| 0 -> ItyQ (Ai.state2id q)
| k ->
let vs = List.filter (fun (i,_) -> n - k + 1 = i) vs in
let vs = List.map (fun (_,q) -> ItyQ (Ai.state2id q)) vs in
let t1 = go (k-1) in
mkItyFun (List.sort compare_ity vs,t1) in
go n;;
let init_cte terminals st =
let n = List.length st in
List.iter (fun (a,_) ->
Hashtbl.add cte a (Array.make n [])) terminals
let register_cte_ity a ity =
let tyarray = lookup_cte a in
let x = codom_of_ity ity in
let ty = tyarray.(x) in
tyarray.(x) <- merge_and_unify compare [ity] ty
let register_cte_ty (a, ty) =
List.iter (register_cte_ity a) ty
let ata2cte m =
(* let open AlternatingAutomaton in *)
init_cte m.AlternatingAutomaton.alpha m.AlternatingAutomaton.st;
List.iter (fun (a,i) ->
let l = List.concat (List.map (fun q ->
let fml = List.assoc (q,a) m.AlternatingAutomaton.delta in
let pis = AlternatingAutomaton.prime_implicants fml in
List.map (build_ity q i) pis) m.AlternatingAutomaton.st) in
register_cte_ty (a,l)) m.AlternatingAutomaton.alpha
let automaton2cte m =
let delta = m.delta in
init_cte m.alpha m.st;
let _ = List.iter
(fun ((q, a), qs) ->
let ty = tr2ty q qs in
register_cte_ty (a, ty))
delta
in
let qs = m.st in
let terminals = List.map fst m.alpha in
List.iter
(fun a ->
let qs1 = (* the set of q s.t. delta(q,a) is undefined *)
List.filter
(fun q-> not(List.exists (fun ((q',a'),_)->q=q'&&a=a') delta))
qs
in register_cte_ty (a, List.map (fun q->add_topty (arity_of a m) (ItyQ(Ai.state2id q))) qs1))
terminals
let rec print_ity ity =
match ity with
ItyQ x -> print_string ("~"^(Ai.id2state x))
| ItyFun(_,ty,ity) ->
print_string "(";
print_ty ty;
print_string "->";
print_ity ity;
print_string ")"
and print_ty ty =
match ty with
[] -> print_string "top"
| [ity] -> print_ity ity
| ity::ty' ->
print_ity ity;
print_string "^";
print_ty ty'
let print_tys tys =
Array.iter (fun ty-> print_ty ty; print_string " * ") tys
let print_itylist ty =
List.iter (fun ity ->
print_ity ity; print_string "\n") ty
let print_nte() =
print_string "types for nt:\n===========\n";
for nt=0 to (Array.length (!nteref))-1 do
print_string ((Grammar.name_of_nt nt)^":\n");
for q=0 to (Array.length (!nteref).(nt))-1 do
print_itylist (!nteref).(nt).(q)
done
done
let print_cte() =
print_string "Constant types:\n=============\n";
Hashtbl.iter
(fun a tyarray ->
print_string (a^":\n");
Array.iter (fun ty -> List.iter (fun ity->print_ity ity;print_string "\n") ty) tyarray)
cte
(* terms_id -> list of types of terms *)
type tyseq = TySeq of (Grammar.ty * (tyseq ref)) list | TySeqNil
type tyseqref = tyseq ref
let terms_te: (tyseqref array ref) = ref (Array.make 0 (ref TySeqNil))
let terms_tyss = ref (Array.make 0 (None))
let rec tys2tyseq_singleton tys =
match tys with
[] -> TySeqNil
| ty::tys' ->
TySeq([(ty, ref (tys2tyseq_singleton tys'))])
let rec tyseq_mem tys tyseqref =
match tys with
[] -> true
| ty::tys' ->
(match !tyseqref with
TySeqNil -> assert false (* size of the type sequence does not match *)
| TySeq(tyseqlist) ->
try
let tyseqref1 = Utilities.assoc_eq eq_ty ty tyseqlist in
tyseq_mem tys' tyseqref1
with Not_found -> false
)
let rec tyseq_subsumed tys tyseqref =
match tys with
[] -> true
| ty::tys' ->
(match !tyseqref with
TySeqNil -> assert false (* size of the type sequence does not match *)
| TySeq(tyseqlist) ->
List.exists (fun (ty1,tyseqref1) ->
subtype_ty ty1 ty
&& tyseq_subsumed tys' tyseqref1
) tyseqlist
)
let rec tyseq_add_wo_subtyping tys tyseqref =
match tys with
[] ->
(match !tyseqref with
TySeqNil -> false
| _ -> assert false)
| ty::tys' ->
(match !tyseqref with
TySeqNil -> assert false (* size of the type sequence does not match *)
| TySeq(tyseqlist) ->
try
let tyseqref1 = Utilities.assoc_eq eq_ty ty tyseqlist in
tyseq_add_wo_subtyping tys' tyseqref1
with Not_found ->
(tyseqref := TySeq((ty, ref (tys2tyseq_singleton tys'))::tyseqlist); true)
)
exception TySeqEmptied
let rec tyseq_rem_subtyping_aux tys tyseqref =
match tys with
[] -> raise TySeqEmptied
| ty::tys' ->
(match !tyseqref with
TySeqNil -> assert false
| TySeq(tyseqlist) ->
let (tyseqlist_subsumed,tyseqlist_not_subsumed) =
List.partition (fun (ty1,_) -> subtype_ty ty ty1) tyseqlist
in
let removed = ref false in
let updated = ref false in
let tyseqlist1 =
List.fold_left
(fun tyseqlist1' (ty1,tyseqref1) ->
try
updated := tyseq_rem_subtyping_aux tys' tyseqref1;
(ty1,tyseqref1)::tyseqlist1'
with TySeqEmptied ->
(removed := true; tyseqlist1')
)
[] tyseqlist_subsumed
in if !removed
then if tyseqlist1=[] && tyseqlist_not_subsumed=[] then raise TySeqEmptied
else (tyseqref := TySeq(List.rev_append tyseqlist1 tyseqlist_not_subsumed); true)
else !updated
)
let tyseq_rem_subtyping tys tyseqref =
try tyseq_rem_subtyping_aux tys tyseqref
with TySeqEmptied -> (tyseqref := TySeq []; true)
let rec tyseq_add_with_subtyping tys tyseqref =
(* print_string "adding:"; print_tys tys;print_string "\n";*)
let overwritten = tyseq_rem_subtyping tys tyseqref in
let _ = tyseq_add_wo_subtyping tys tyseqref in
overwritten
let merged_vte_updated = ref false
let rec tyseq_merge_tys tys tyseqref =
match tys with
[] ->
(match !tyseqref with
TySeqNil -> ()
| _ -> assert false)
| ty::tys' ->
(match !tyseqref with
TySeqNil -> assert false (* size of the type sequence does not match *)
| TySeq(tyseqlist) ->
match tyseqlist with
[] ->
merged_vte_updated := true;
tyseqref := TySeq((ty, ref (tys2tyseq_singleton tys'))::tyseqlist)
| (ty1,tyseqref')::tyseqlist' ->
assert(tyseqlist'=[]);
tyseq_merge_tys tys' tyseqref';
let ty2 = merge_and_unify compare_ity ty ty1 in
if List.length ty1=List.length ty2 then ()
else (merged_vte_updated:= true;
tyseqref := TySeq([(ty2, tyseqref')]))
)
(*
let rec tyseq2tyss tyseq len =
match tyseq with
TySeqNil -> [[]]
| TySeq(tyseqlist) ->
List.fold_left
(fun tyss (ty,tyseqref) ->
let tyss1 = tyseq2tyss (!tyseqref) in
List.fold_left (fun tyss2 tys1 ->
(ty::tys1)::tyss2) tyss tyss1)
[] tyseqlist
*)
let rec tyseq2tyss tyseq len =
match tyseq with
TySeqNil -> [Array.make len []]
| TySeq(tyseqlist) ->
List.fold_left
(fun tyss (ty,tyseqref) ->
let tyss1 = tyseq2tyss (!tyseqref) (len+1) in
let _ = List.iter (fun tys -> tys.(len) <- ty) tyss1 in
List.rev_append tyss1 tyss)
[] tyseqlist
let lookup_terms_te id =
match (!terms_tyss).(id) with
Some(tyss) -> tyss
| None ->
let tyss = tyseq2tyss(!((!terms_te).(id))) 0 in
(!terms_tyss).(id) <- Some(tyss); tyss
let print_terms_te() =
print_string "Types_of_terms:\n=========\n";
for id=0 to (Array.length !terms_te)-1 do
if (!Ai.termid_isarg).(id) then
let terms = Ai.id2terms id in
List.iter (fun t-> print_term t; print_string ", ") terms;
print_string "\n";
let tyss = lookup_terms_te id in
List.iter (fun tys -> print_tys tys;
print_string "\n") tyss
else ()
done
let rec subtype_tys tys1 tys2 =
match (tys1,tys2) with
([], []) -> true
| (ty1::tys1', ty2::tys2') ->
subtype_ty ty1 ty2
&& subtype_tys tys1' tys2'
| _ -> assert false
let worklist_var_ty = ref ([], Array.make 1 [])
let worklist_var_ty_wo_overwrite = ref ([], Array.make 1 [])
let updated_nt_ty = ref ([], Array.make 1 [])
let init_worklist_var_ty maxid =
worklist_var_ty := ([], Array.make maxid []);
worklist_var_ty_wo_overwrite := ([], Array.make maxid [])
let worklist_var = ref (Setqueue.make 1)
let worklist_var_overwritten = ref (Setqueue.make 1)
let worklist_nt = ref (Setqueue.make 1)
let updated_nts = ref (Setqueue.make 1)
let enqueue_var_ty termid tys =
let _ = (!terms_tyss).(termid) <- None in (* invalidate the previous type table for id *)
let (ids, a) = !worklist_var_ty in
let x = a.(termid) in
a.(termid) <- tys::x;
if x=[] then worklist_var_ty := (termid::ids, a)
else ()
let enqueue_var_ty_wo_overwrite termid tys =
let _ = (!terms_tyss).(termid) <- None in (* invalidate the previous type table for id *)
let (ids, a) = !worklist_var_ty_wo_overwrite in
let x = a.(termid) in
a.(termid) <- tys::x;
if x=[] then worklist_var_ty_wo_overwrite := (termid::ids, a)
else ()
let enqueue_nt_ty f ity =
let (nts,a) = !updated_nt_ty in
let x = a.(f) in
a.(f) <- ity::x;
if x=[] then updated_nt_ty := (f::nts, a)
else ()
exception WorklistVarTyEmpty
let rec dequeue_var_ty_gen worklist =
let (ids, a) = !worklist in
match ids with
[] -> raise WorklistVarTyEmpty
| id::ids' ->
match a.(id) with
[] -> (worklist := (ids',a);
dequeue_var_ty_gen worklist)
| tys::tyss ->
a.(id) <- tyss;
(if tyss=[] then worklist := (ids',a));
(id, tys)
let rec dequeue_var_ty() =
dequeue_var_ty_gen worklist_var_ty
let rec dequeue_var_ty_wo_overwrite() =
dequeue_var_ty_gen worklist_var_ty_wo_overwrite
let dequeue_nt_ty() =
let (nts, a) = !updated_nt_ty in
match nts with
[] -> raise WorklistVarTyEmpty
| f::nts' ->
let ty = a.(f) in
(updated_nt_ty := (nts',a); a.(f) <- []; (f,ty))
let register_terms_te id ty overwrite_flag =
(* assert (not(ty=[]));*)
if !Flags.merge_vte then
let tyseqref = (!terms_te).(id) in
(merged_vte_updated := false;
tyseq_merge_tys ty tyseqref;
if !merged_vte_updated then
(Utilities.debug ("type of id "^(string_of_int id)^" has been updated\n");
let tys = List.hd (tyseq2tyss !tyseqref 0) in
enqueue_var_ty id tys)
else ())
else
let tyseqref = (!terms_te).(id) in
if overwrite_flag && !Flags.overwrite then
if tyseq_mem ty tyseqref then ()
else if tyseq_subsumed ty tyseqref
then (flag_overwritten := true;
Setqueue.enqueue !worklist_var_overwritten id)
else
(let overwritten = tyseq_add_with_subtyping ty tyseqref in
flag_updated_termid := true;
(* let _ = Utilities.debug ("updated type of id "^(string_of_int id)^"\n") in*)
let ty' = Array.of_list ty in
enqueue_var_ty id ty';
if overwritten then
(flag_overwritten := true ; Setqueue.enqueue !worklist_var_overwritten id)
else ()
)
else
let changed = tyseq_add_wo_subtyping ty tyseqref in
if changed then
(let ty' = Array.of_list ty in
enqueue_var_ty_wo_overwrite id ty';
flag_updated_termid := true)
else ()
let rec mk_prod_venvs (i,j,tys) venvs acc =
match tys with
[] -> acc
| ty::tys' ->
let acc' =
List.fold_left
(fun venvs1 venv1 ->
((i,j,ty)::venv1)::venvs1 ) acc venvs
in mk_prod_venvs (i,j,tys') venvs acc'
(* env is a list of elements of the form (i,j,id),
which means that variables i to j are bound to ti to tj,
where id is the identifier of [ti;...;tj] *)
let rec mk_venvs env =
match env with
[] -> [[]]
| [(i,j,id)] -> let tys = lookup_terms_te(id) in
List.map (fun ty -> [(i,j,ty)]) tys
| (i,j,id)::env' ->
let tys = lookup_terms_te(id) in
if tys=[] then []
else
let venvs = mk_venvs env' in
if venvs=[] then []
else
(* this may blow up the number of type environments *)
mk_prod_venvs (i,j,tys) venvs []
let rec mk_venvs_incremental env (id,tys) =
match env with
[] -> [[]]
| (i,j,id1)::env' ->
if id1=id then
let venvs = mk_venvs env' in
List.map (fun venv-> (i,j,tys)::venv) venvs
else
let tyss = lookup_terms_te(id1) in
if tyss=[] then []
else
let venvs = mk_venvs_incremental env' (id,tys) in
if venvs=[] then []
else
(* this may blow up the number of type environments *)
mk_prod_venvs (i,j,tyss) venvs []
let rec mask_ty i mask tys =
List.iter (fun j ->
tys.(j-i) <- []) mask
(*
match (mask,tys) with
([],_) -> tys
| (j::mask',ty::tys') ->
if i=j then ty::(mask_ty (i+1) mask' tys')
else []::(mask_ty (i+1) mask tys')
| _ -> assert false
*)
let rec mask_tys i mask tys r =
match tys with
[] -> r
| ty::tys' ->
if List.exists (eq_tyarray_mask i mask ty) r then
mask_tys i mask tys' r
else
mask_tys i mask tys' (ty::r)
let rec mk_venvs_mask env =
match env with
[] -> [[]]
| [(i,j,mask,id)] ->
let tys = lookup_terms_te(id) in
let tys' = if List.length mask=j+1-i then tys
else mask_tys i mask tys []
in List.map (fun ty -> [(i,j,ty)]) tys'
| (i,j,mask,id)::env' ->
let tys = lookup_terms_te(id) in
if tys=[] then []
else
let venvs = mk_venvs_mask env' in
if venvs=[] then []
else
(* this may blow up the number of type environments *)
let tys' = if List.length mask=j+1-i then tys
else mask_tys i mask tys []
in mk_prod_venvs (i,j,tys') venvs []
let rec mk_venvs_mask_incremental env (id,tys) =
match env with
[] -> [[]]
| (i,j,mask,id1)::env' ->
let tyss = if id=id1 then [tys] else lookup_terms_te(id1) in
if tyss=[] then []
else
let venvs = if id=id1 then mk_venvs_mask env'
else mk_venvs_mask_incremental env' (id,tys) in
if venvs=[] then []
else
(* this may blow up the number of type environments *)
let tyss' = if j-i+1=List.length mask then tyss
else mask_tys i mask tyss []
in mk_prod_venvs (i,j,tyss') venvs []
let rec range_types ty1 ty2 =
List.fold_left
(fun ty ity1 ->
match ity1 with
ItyFun(_,ty3,ity)->
if (* not(List.exists (fun ity0 -> subtype ity0 ity) ty)
&& *)
List.for_all
(fun ity3-> List.exists (fun ity2-> subtype ity2 ity3) ty2)
ty3
then add_ity ity ty
else ty
| _ -> assert false
) [] ty1
exception Untypable
let rec size_of_vte vte =
match vte with
[] -> 0
| (_,ty)::vte' -> List.length ty + size_of_vte vte'
let rec pick_smallest_vte ity_vte_list =
match ity_vte_list with
[] -> raise Untypable
| (_,vte)::ity_vte_list' ->
let n = size_of_vte vte in
pick_smallest_vte_aux ity_vte_list' n vte
and pick_smallest_vte_aux ity_vte_list n vte =
match ity_vte_list with
[] -> vte
| (_,vte')::ity_vte_list' ->
let n' = size_of_vte vte in
if n'<n then
pick_smallest_vte_aux ity_vte_list' n' vte'
else
pick_smallest_vte_aux ity_vte_list' n vte
let pick_vte ity ity_vte_list =
try
snd(List.find (fun (ity',vte)-> subtype ity' ity) ity_vte_list )
with Not_found -> raise Untypable
(*
let ity_vte_list' = List.filter (fun (ity',vte)-> subtype ity' ity) ity_vte_list in
pick_smallest_vte ity_vte_list'
*)
let rec merge_vtes vtes =
match vtes with
[] -> []
| vte::vtes' ->
merge_two_vtes vte (merge_vtes vtes')
and merge_two_vtes vte1 vte2 =
match (vte1,vte2) with
([], _) -> vte2
| (_,[]) -> vte1
| ((v1,ty1)::vte1', (v2,ty2)::vte2') ->
let n = compare v1 v2 in
if n<0 then (v1,ty1)::(merge_two_vtes vte1' vte2)
else if n>0 then (v2,ty2)::(merge_two_vtes vte1 vte2')
else (v1,merge_ty ty1 ty2)::(merge_two_vtes vte1' vte2')
let rec insert_ty_with_vte (ity,vte) ty =
match ty with
[] -> [(ity,vte)]
| (ity1,vte1)::ty' ->
let c= compare_ity ity ity1 in
if c<0 then (ity,vte)::ty
else if c>0 then (ity1,vte1)::(insert_ty_with_vte (ity,vte) ty')
else if size_of_vte vte < size_of_vte vte1
then (ity,vte)::ty'
else ty
let rec range_types_with_vte ty1 ty2 =
List.fold_left
(fun ty (ity1,vte1) ->
match ity1 with
ItyFun(_,ty3,ity)->
( try
let vtes = List.map (fun ity3 -> pick_vte ity3 ty2) ty3 in
let vte' = merge_vtes (vte1::vtes) in
insert_ty_with_vte (ity,vte') ty
with Untypable -> ty)
| _ -> assert false
) [] ty1
let ty_of_nt f =
Array.fold_left (@) [] (!nteref).(f)
let ty_of_nt_q f q =
(!nteref).(f).(q)
let ty_of_nt_qs f qs =
let tyarray = (!nteref).(f) in
List.fold_left (fun ty q -> List.rev_append tyarray.(q) ty) [] qs
let ty_of_t_qs a qs =
let tyarray = lookup_cte a in
List.fold_left (fun ty q -> List.rev_append tyarray.(q) ty) [] qs
let proj_tys f i tys = tys.(i)
let rec ty_of_var venv (f,i) =
match venv with
[] -> assert false
| (j1,j2,tys)::venv' ->
if j1<=i && i<=j2 then
proj_tys f (i-j1) tys
else ty_of_var venv' (f,i)
let rec ty_of_term venv term =
match term with
NT(f) -> ty_of_nt f
| T(a) -> ty_of_t a
| Var(v) -> ty_of_var venv v
| App(t1,t2) ->
let ty1 = ty_of_term venv t1 in
let ty2 = ty_of_term venv t2 in
range_types ty1 ty2
let rec ty_of_term_with_vte venv term =
match term with
NT(f) -> List.map (fun ity -> (ity,[])) (ty_of_nt f)
| T(a) -> List.map (fun ity -> ity,[]) (ty_of_t a)
| Var(v) -> let ty = ty_of_var venv v in
List.map (fun ity->(ity,[(v,[ity])])) ty
| App(t1,t2) ->
let ty1 = ty_of_term_with_vte venv t1 in
let ty2 = ty_of_term_with_vte venv t2 in
range_types_with_vte ty1 ty2
let rec ty_of_term_with_vte_qs venv term qs =
match term with
NT(f) -> let ty = ty_of_nt_qs f qs in
List.map (fun ity -> (ity,[])) ty
| T(a) -> let ty=ty_of_t_qs a qs in
List.map (fun ity -> (ity,[])) ty
| Var(v) -> let ty = ty_of_var venv v in
let ty' = List.filter (fun ity->List.mem (codom_of_ity ity) qs) ty in
List.map (fun ity->(ity,[(v,[ity])])) ty'
| App(t1,t2) ->
let ty1 = ty_of_term_with_vte_qs venv t1 qs in
let ty2 = ty_of_term_with_vte venv t2 in
range_types_with_vte ty1 ty2
let rec split_ity arity ity =
if arity=0 then ([],ity)
else match ity with
ItyFun(_,ty,ity1)->
let (tys,ity') = split_ity (arity-1) ity1 in
(ty::tys, ity')
| _ -> assert false
let rec get_range ity arity =
if arity=0 then ity
else
match ity with
ItyFun(_,_,ity1) -> get_range ity1 (arity-1)
| _ -> assert false
let rec ty_of_head h venv =
match h with
NT(f) -> (ty_of_nt f)
| T(a) -> (ty_of_t a)
| Var(v) -> ty_of_var venv v
| _ -> assert false
let rec ty_of_head_q h venv q =
match h with
NT(f) -> List.map (fun ity -> (ity,[])) (ty_of_nt_q f q)
| T(a) -> List.map (fun ity -> ity,[]) (ty_of_t_q a q)
| Var(v) -> let ty = ty_of_var venv v in
List.map (fun ity->(ity,[(v,[ity])])) ty
| _ -> assert false
let rec ty_of_head_q2 h venv q =
match h with
NT(f) -> (ty_of_nt_q f q)
| T(a) -> (ty_of_t_q a q)
| Var(v) -> ty_of_var venv v
| _ -> assert false
let rec get_argtys arity ity =
if arity=0 then []
else
match ity with
ItyFun(_,ty,ity1) -> ty::(get_argtys (arity-1) ity1)
| _ -> assert false
let match_head_ity h venv arity ity =
match ity with
ItyQ(q) ->
(match h with
Var(v) ->
if !num_of_states=1 then
let ty = (ty_of_var venv v) in
List.map (fun ity1 -> get_argtys arity ity1) ty
else
let ty = List.filter (fun ity1->codom_of_ity ity1=q) (ty_of_var venv v) in
List.map (fun ity1 -> get_argtys arity ity1) ty
| _ ->
let ty = ty_of_head_q2 h venv q in
List.map (fun ity1 -> get_argtys arity ity1) ty
)
| _ ->
let q = codom_of_ity ity in
let ty = List.filter (fun ity1 ->
subtype (get_range ity1 arity) ity) (ty_of_head_q2 h venv q) in
List.map (fun ity -> get_argtys arity ity) ty
let match_head_types h venv arity ity =
match ity with
ItyQ(q) ->
(match h with
Var(v) ->
let ty = (ty_of_var venv v) in
let ty' = if !num_of_states=1 then
ty
else List.filter (fun ity1->codom_of_ity ity1=q) ty
in
List.map (fun ity1 -> (get_argtys arity ity1, [(v,[ity1])])) ty'
| _ ->
let ty = ty_of_head_q2 h venv q in
List.map (fun ity1 -> (get_argtys arity ity1, [])) ty
)
| _ ->
let ty = List.filter (fun (ity1,_) ->
subtype (get_range ity1 arity) ity) (ty_of_head_q h venv (codom_of_ity ity)) in
List.map (fun (ity,vte) -> (get_argtys arity ity, vte)) ty
let rec check_args tys_ity_list terms venv ty =
match tys_ity_list with
[] -> ty
| (tys,ity)::tys_ity_list' ->
if check_args_aux tys terms venv
then
(if !Flags.merge_vte then
let ty' = List.filter (fun ity1->not(eq_ity ity ity1)) ty in
let tys_ity_list'' =
List.filter (fun (_,ity1)->not(eq_ity ity ity1)) tys_ity_list'
in
check_args tys_ity_list'' terms venv (ity::ty')
else
let ty' = List.filter (fun ity1->not(subtype ity ity1)) ty in
let tys_ity_list'' =
List.filter (fun (_,ity1)->not(subtype ity ity1)) tys_ity_list'
in
check_args tys_ity_list'' terms venv (ity::ty')
)
else
check_args tys_ity_list' terms venv ty
and check_args_aux tys terms venv =
match (tys,terms) with
([], []) -> true
| (ty::tys', t::terms') ->
List.for_all (fun ity-> check_term t ity venv) ty
&& check_args_aux tys' terms' venv
| _ -> assert false
and check_term term ity venv =
match term with
App(_,_) ->
let (h,terms) = Grammar.decompose_term term in
let tyss = match_head_ity h venv (List.length terms) ity in
List.exists (fun tys->check_args_aux tys terms venv) tyss
| Var(v) -> List.exists (fun ity1 -> subtype ity1 ity) (ty_of_var venv v)
| T(a) -> let q = codom_of_ity ity in
List.exists (fun ity1 -> subtype ity1 ity) (ty_of_t_q a q)
| NT(f) -> let q = codom_of_ity ity in
List.exists (fun ity1 -> subtype ity1 ity) (ty_of_nt_q f q)
(* alternative implementation of ty_of_term *)
let ty_of_term2 venv term =
let (h,terms) = Grammar.decompose_term term in
let ty = ty_of_head h venv in
let arity = List.length terms in
let tys_ity_list = List.map (split_ity arity) ty in
check_args tys_ity_list terms venv []
(* returns ty list *)
let ty_of_terms venv terms =
if !(Flags.ty_of_term) then
List.map (fun term ->
List.sort compare_ity (ty_of_term venv term)) terms
else
List.map (fun term ->
match term with
Var(v) -> ty_of_var venv v
| T(a) -> List.sort compare_ity (ty_of_t a)
| NT(f) -> List.sort compare_ity (ty_of_nt f)
| App(_,_) -> List.sort compare_ity (ty_of_term2 venv term)) terms
(*
module X = struct type t = Grammar.term * Grammar.ity;;
let equal (t1,ity1) (t2,ity2) = t1==t2 && (id_of_ity ity1)=(id_of_ity ity2);;
let hash = Hashtbl.hash end;;
module TabCheckTy = Hashtbl.Make(X)
let tab_checkty = TabCheckTy.create 10000
let reset_ty_hash() = TabCheckTy.clear tab_checkty
*)
let reset_ty_hash() = ()
let rec check_ty_of_term venv term ity =
(*
try
TabCheckTy.find tab_checkty (term,ity)
with
Not_found ->
*)
match term with
App(_,_) ->
let (h,terms) = Grammar.decompose_term term in
let tyss = match_head_types h venv (List.length terms) ity in
let vte = check_argtypes venv terms tyss in vte
(* (TabCheckTy.replace tab_checkty (term,ity) vte; vte)
*)
| Var(v) ->
( try
let ity1 = List.find (fun ity1 -> subtype ity1 ity) (ty_of_var venv v) in
[(v, [ity1])]
with Not_found -> raise Untypable)
| T(a) ->
let q = codom_of_ity ity in
if List.exists (fun ity1 -> subtype ity1 ity) (ty_of_t_q a q)
then []
else raise Untypable
| NT(f) ->
let q = codom_of_ity ity in
if List.exists (fun ity1 -> subtype ity1 ity) (ty_of_nt_q f q)
then []
else raise Untypable
and check_argtypes venv terms tyss =
match tyss with
[] -> raise Untypable
| (tys,vte0)::tyss' ->
( try
merge_two_vtes vte0 (check_argtypes_aux venv terms tys)
with Untypable ->
check_argtypes venv terms tyss')
and check_argtypes_aux venv terms tys =
match (terms,tys) with
([], []) -> []
| (t::terms',ty::tys') ->
let vtes = List.map (fun ity -> check_ty_of_term venv t ity) ty in
let vte = check_argtypes_aux venv terms' tys' in
merge_vtes (vte::vtes)
| _ -> assert false
(* incremental version of check_ty_of_term *)
let rec check_ty_of_term_inc venv term ity f tyf =
let (h,terms) = Grammar.decompose_term term in
let arity = List.length terms in
let tyss =
if h=NT(f) then
let ty1 = List.filter (fun ity1 -> subtype (get_range ity1 arity) ity) tyf in
if ty1=[] then raise Untypable
else List.map (fun ity -> (get_argtys arity ity, [])) ty1
else match_head_types h venv arity ity
in
let vte = check_argtypes_inc venv terms tyss f tyf in vte
and check_argtypes_inc venv terms tyss f tyf =
match tyss with
[] -> raise Untypable
| (tys,vte0)::tyss' ->
( try
merge_two_vtes vte0 (check_argtypes_inc_aux venv terms tys f tyf)
with Untypable ->
check_argtypes_inc venv terms tyss' f tyf)
and check_argtypes_inc_aux venv terms tys f tyf =
match (terms,tys) with
([], []) -> []
| (t::terms',ty::tys') ->
let vtes = List.map (fun ity -> check_ty_of_term_inc venv t ity f tyf) ty in
let vte = check_argtypes_inc_aux venv terms' tys' f tyf in
merge_vtes (vte::vtes)
| _ -> assert false
let update_ty_of_id_aux id venvs overwrite_flag =
let terms = Ai.id2terms id in
List.iter
(fun venv ->
let ty = ty_of_terms venv terms in
register_terms_te id ty overwrite_flag)
venvs
(* update the set of types for each term list [t1;...;tk] *)
let update_ty_of_id id env overwrite_flag =
(* (if !Flags.debugging then
(print_string ("updating the type for "^(string_of_int id)^"\n");
Ai.print_binding env));
*)
let venvs = mk_venvs_mask env in
update_ty_of_id_aux id venvs overwrite_flag
let rec mk_funty venv ity =
match venv with
[] -> ity
| (i,j,ty)::venv' ->
(* here, we assume that venv=[(i1,j1);...;(ik,jk)] where
i_{x+1} = j_x+1
*)
let ity' = mk_funty venv' ity in
mk_funty_aux ty ity'
and mk_funty_aux tys ity =
match tys with
[] -> ity
| ty::tys' -> mkItyFun(ty,mk_funty_aux tys' ity)
let rec mk_funty_with_vte vte ity arity =
mk_funty_with_vte_aux vte ity 0 arity
and mk_funty_with_vte_aux vte ity i arity =
if i>=arity then ity
else
match vte with
[] -> mkItyFun([], mk_funty_with_vte_aux vte (ity) (i+1) arity)
| ((_,j),ty)::vte' ->
if i=j then mkItyFun(ty, mk_funty_with_vte_aux vte' ity (i+1) arity)
else mkItyFun([], mk_funty_with_vte_aux vte (ity) (i+1) arity)
exception REFUTED