-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTermInner.ml
More file actions
1476 lines (1310 loc) · 44.4 KB
/
TermInner.ml
File metadata and controls
1476 lines (1310 loc) · 44.4 KB
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
(* This file is free software, part of nunchaku. See file "license" for more details. *)
(** {1 Main View for terms} *)
module ID = ID
module Var = Var
module MetaVar = MetaVar
type id = ID.t
type 'a var = 'a Var.t
type 'a or_error = [`Ok of 'a | `Error of string]
type 'a printer = Format.formatter -> 'a -> unit
let fpf = Format.fprintf
module Binder = struct
type t =
[ `Forall
| `Exists
| `Fun
| `TyForall
| `Mu (** fixpoint *)
]
let to_string : t -> string = function
| `Fun -> "fun"
| `Forall -> "forall"
| `Exists -> "exists"
| `TyForall -> "pi"
| `Mu -> "mu"
end
module TyBuiltin = struct
type t =
[ `Kind
| `Type
| `Prop
]
let equal = (=)
let to_string = function
| `Prop -> "prop"
| `Kind -> "kind"
| `Type -> "type"
end
module Builtin = struct
type +'a guard = {
assuming: 'a list; (* preconditions *)
asserting: 'a list; (* postconditions, to be enforced *)
}
let map_guard f g =
{ assuming = List.map f g.assuming;
asserting = List.map f g.asserting;
}
type builtin_fun =
[ `Choice (** Choice operator, "indefinite description operator", or ε *)
| `UChoice (** Unique choice, "definite description operator", or Hilbert's ι *)
]
type 'a t =
[ `True
| `False
| `Not
| `Or
| `And
| `Imply
| `Equiv of 'a * 'a
| `Ite of 'a * 'a * 'a
| `Eq of 'a * 'a
| `BFun of builtin_fun
| `DataTest of id (** Test whether [t : tau] starts with given constructor *)
| `DataSelect of id * int (** Select n-th argument of given constructor *)
| `Undefined of id * 'a (** Undefined case. argument=the undefined term *)
| `Guard of 'a * 'a guard (** term + some boolean conditions *)
]
let is_infix : _ t -> bool = function
| `Eq _ | `Or | `And | `Equiv _ | `Imply | `Guard _ -> true
| _ -> false
let pp pterm out : _ t -> unit = function
| `True -> CCFormat.string out "true"
| `False -> CCFormat.string out "false"
| `Not -> CCFormat.string out "~"
| `Or -> CCFormat.string out "||"
| `And -> CCFormat.string out "&&"
| `Imply -> CCFormat.string out "=>"
| `Equiv (a,b) | `Eq (a,b) ->
fpf out "@[<hv>%a@ @[<hv>=@ %a@]@]" pterm a pterm b
| `Ite (a,b,c) ->
fpf out "@[<hv>@[<2>if@ %a@]@ @[<2>then@ %a@]@ @[<2>else@ %a@]@]"
pterm a pterm b pterm c
| `BFun `Choice -> CCFormat.string out "ε"
| `BFun `UChoice -> CCFormat.string out "ι"
| `DataTest id -> fpf out "is-%s" (ID.name id)
| `DataSelect (id, n) ->
fpf out "select-%s-%d" (ID.name id) n
| `Undefined (id,t) -> fpf out "undefined_%d %a" (ID.id id) pterm t
| `Guard (t, o) ->
let pp_case name out l = match l with
| [] -> ()
| _::_ ->
fpf out "@ @[<2>%s@ @[<hv>%a@]@]" name
(CCFormat.list ~start:"" ~stop:"" ~sep:" && " pterm) l
in
assert (not (o.asserting=[] && o.assuming=[]));
fpf out "@[<hv>%a%a%a@]" pterm t
(pp_case "assuming") o.assuming
(pp_case "asserting") o.asserting
let equal
: ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
= fun eqterm a b -> match a, b with
| `True, `True
| `False, `False
| `Not, `Not
| `Or, `Or
| `And, `And
| `Imply, `Imply -> true
| `Ite(a1,b1,c1), `Ite(a2,b2,c2) ->
eqterm a1 a2 && eqterm b1 b2 && eqterm c1 c2
| `Equiv (a1,b1), `Equiv(a2,b2)
| `Eq(a1,b1), `Eq (a2,b2) -> eqterm a1 a2 && eqterm b1 b2
| `DataTest id, `DataTest id' -> ID.equal id id'
| `DataSelect (id, n), `DataSelect (id', n') -> n=n' && ID.equal id id'
| `BFun f1, `BFun f2 -> f1=f2
| `Undefined (a,t1), `Undefined (b,t2) -> ID.equal a b && eqterm t1 t2
| `Guard (t1, g1), `Guard (t2, g2) ->
List.length g1.assuming = List.length g2.assuming
&& List.length g1.asserting = List.length g2.asserting
&& eqterm t1 t2
&& List.for_all2 eqterm g1.assuming g2.assuming
&& List.for_all2 eqterm g1.asserting g2.asserting
| `Guard _, _ | `BFun _, _
| `True, _ | `False, _ | `Ite _, _ | `Not, _
| `Eq _, _ | `Or, _ | `And, _ | `Equiv _, _ | `Imply, _
| `DataSelect _, _ | `DataTest _, _ | `Undefined _, _ -> false
let map : f:('a -> 'b) -> 'a t -> 'b t
= fun ~f b -> match b with
| `True -> `True
| `False -> `False
| `And -> `And
| `Imply -> `Imply
| `Ite (a,b,c) -> `Ite (f a, f b, f c)
| `Eq (a,b) -> `Eq (f a, f b)
| `Equiv (a,b) -> `Equiv (f a, f b)
| `DataTest id -> `DataTest id
| `BFun f -> `BFun f
| `Or -> `Or
| `Not -> `Not
| `DataSelect (c,n) -> `DataSelect (c,n)
| `Undefined (id, t) -> `Undefined (id, f t)
| `Guard (t, g) ->
let g' = map_guard f g in
`Guard (f t, g')
let fold : f:('acc -> 'a -> 'acc) -> x:'acc -> 'a t -> 'acc
= fun ~f ~x:acc b -> match b with
| `True
| `And
| `Imply
| `False
| `Or
| `DataTest _
| `DataSelect _
| `BFun _
| `Not -> acc
| `Ite (a,b,c) -> f (f (f acc a) b) c
| `Eq (a,b)
| `Equiv (a,b) -> f (f acc a) b
| `Undefined (_,t) -> f acc t
| `Guard (t, g) ->
let acc = f acc t in
let acc = List.fold_left f acc g.assuming in
List.fold_left f acc g.asserting
let fold2 :
f:('acc -> 'a -> 'b -> 'acc) -> fail:(unit -> 'acc) ->
x:'acc -> 'a t -> 'b t -> 'acc
= fun ~f ~fail ~x:acc b1 b2 -> match b1, b2 with
| `True, `True
| `And, `And
| `Imply, `Imply
| `False, `False
| `Not, `Not
| `Or, `Or -> acc
| `BFun f1, `BFun f2 -> if f1=f2 then acc else fail()
| `DataTest i1, `DataTest i2 -> if ID.equal i1 i2 then acc else fail()
| `DataSelect (i1,n1), `DataSelect (i2,n2) ->
if n1=n2 && ID.equal i1 i2 then acc else fail()
| `Ite (a1,b1,c1), `Ite(a2,b2,c2) ->
let acc = f acc a1 a2 in
let acc = f acc b1 b2 in
f acc c1 c2
| `Eq (a1,b1), `Eq (a2,b2)
| `Equiv (a1,b1), `Equiv (a2,b2) -> let acc = f acc a1 a2 in f acc b1 b2
| `Undefined (i1,t1), `Undefined (i2,t2) ->
if ID.equal i1 i2 then f acc t1 t2 else fail()
| `Guard (t1, g1), `Guard (t2, g2)
when List.length g1.asserting=List.length g2.asserting
&& List.length g1.assuming = List.length g2.assuming ->
let acc = f acc t1 t2 in
let acc = List.fold_left2 f acc g1.assuming g2.assuming in
List.fold_left2 f acc g1.asserting g2.asserting
| `Guard _, _ | `BFun _, _
| `True, _ | `False, _ | `Ite _, _ | `Not, _
| `Eq _, _ | `Or, _ | `And, _ | `Equiv _, _ | `Imply, _
| `DataSelect _, _ | `DataTest _, _ | `Undefined _, _ -> fail()
let iter : ('a -> unit) -> 'a t -> unit
= fun f b -> match b with
| `True
| `And
| `Imply
| `False
| `Or
| `DataTest _
| `DataSelect _
| `BFun _
| `Not -> ()
| `Ite (a,b,c) -> f a; f b; f c
| `Eq (a,b)
| `Equiv (a,b) -> f a; f b
| `Undefined (_,t) -> f t
| `Guard (t,g) ->
f t;
List.iter f g.asserting;
List.iter f g.assuming
let to_seq b f = iter f b
end
type 'a case = 'a var list * 'a
(** A pattern match case for a given constructor [ vars, right-hand side ]
The pattern must be linear (variable list does not contain duplicates) *)
(** A list of cases (indexed by constructor) *)
type 'a cases = 'a case ID.Map.t
let cases_to_list = ID.Map.to_list
(* check that: each case is linear (all vars are different) *)
let cases_well_formed (type a) m =
let is_linear_ _ (vars,_) =
let module VarSet = Var.Set(struct type t = a end) in
VarSet.cardinal (VarSet.of_list vars) = List.length vars
in
ID.Map.for_all is_linear_ m
(** The main view of terms. Other representations will be refinements
(read: restrictions) of this view that enforce additional restrictions, such
as the absence of meta-variables or polymorphism *)
type 'a view =
| Const of id (** top-level symbol *)
| Var of 'a var (** bound variable *)
| App of 'a * 'a list
| Builtin of 'a Builtin.t (** built-in operation *)
| Bind of Binder.t * 'a var * 'a
| Let of 'a var * 'a * 'a
| Match of 'a * 'a cases (** shallow pattern-match *)
| TyBuiltin of TyBuiltin.t (** Builtin type *)
| TyArrow of 'a * 'a (** Arrow type *)
| TyMeta of 'a MetaVar.t
(* NOTE: Eq has its own case (in Builtin), because its type parameter is often hidden.
For instance, when we parse a model back from TPTP or SMT, equalities
are not annotated with their type parameter; we would have to compute or
infer types again for an unclear benefit (usually just for printing).
Instead, we just consider equality to be a specific "ad-hoc polymorphic"
predicate and do not require it to have a type argument.
*)
type 't repr = 't -> 't view
(** A concrete representation of terms by the type [t'] *)
type 't build = 't view -> 't
(** A builder for a concrete representation with type ['t]. *)
module type REPR = sig
type t
val repr : t repr
end
module type BUILD = sig
type t
val build : t build
end
module type S = sig
type t
include REPR with type t := t
include BUILD with type t := t
end
(** {2 Printing} *)
module type PRINT = sig
type t
val print : t printer
val print_in_app : t printer
val print_in_binder : t printer
end
module Print(T : REPR)
: PRINT with type t = T.t
= struct
type t = T.t
let pp_list_ ?(start="") ?(stop="") ~sep pp =
CCFormat.list ~start ~stop ~sep pp
let is_if_ t = match T.repr t with
| Builtin (`Ite _) -> true
| _ -> false
let rec unroll_if_ t = match T.repr t with
| Builtin (`Ite (a,b,c)) ->
let l, last = unroll_if_ c in
(a,b) :: l, last
| _ -> [], t
let rec print out t = match T.repr t with
| TyBuiltin b -> CCFormat.string out (TyBuiltin.to_string b)
| Const id -> ID.print out id
| TyMeta v -> MetaVar.print out v
| Var v -> Var.print_full out v
| Builtin (`Ite (a,b,c)) when is_if_ c ->
(* special case to avoid deep nesting of ifs *)
let pp_middle out (a,b) =
fpf out "@[<2>else if@ @[%a@]@]@ @[<2>then@ @[%a@]@]" print a print b
in
let middle, last = unroll_if_ c in
assert (not (is_if_ last));
fpf out "@[<hv>@[<2>if@ @[%a@]@]@ @[<2>then@ %a@]@ %a@ @[<2>else@ %a@]@]"
print a print b
(pp_list_ ~sep:"" pp_middle) middle
print last
| Builtin b -> Builtin.pp print_in_app out b
| App (f,l) ->
begin match T.repr f with
| Builtin b when Builtin.is_infix b ->
fpf out "@[<hv>%a@]" (print_infix_list b) l
| _ ->
fpf out "@[<2>%a@ %a@]" print_in_app f
(pp_list_ ~sep:" " print_in_app) l
end
| Let (v,t,u) ->
fpf out "@[<2>let %a :=@ %a in@ %a@]" Var.print_full v print t print u
| Match (t,l) ->
let pp_case out (id,(vars,t)) =
fpf out "@[<hv2>| @[<hv2>%a %a@] ->@ %a@]"
ID.print id (pp_list_ ~sep:" " Var.print_full) vars print t
in
fpf out "@[<hv>@[<hv2>match @[%a@] with@ %a@]@ end@]"
print t (pp_list_ ~sep:"" pp_case) (ID.Map.to_list l)
| Bind (b, v, t) ->
let s = Binder.to_string b in
fpf out "@[<2>%s %a:%a.@ %a@]" s Var.print_full v print_in_app (Var.ty v) print t
| TyArrow (a,b) ->
fpf out "@[<2>%a ->@ %a@]" print_in_binder a print b
and print_in_app out t = match T.repr t with
| Builtin b when not (Builtin.is_infix b) -> print out t
| TyBuiltin _ | Const _ | TyMeta _ | Var _ | Match _ -> print out t
| App (_,_) | Builtin _ | Let _ | Bind _ | TyArrow _ -> fpf out "(@[%a@])" print t
and print_in_binder out t = match T.repr t with
| TyBuiltin _ | Const _ | App _ | Builtin _ | Match _ ->
print out t
| Var _ -> print out t
| TyMeta _ -> print out t
| Bind _ -> fpf out "(@[%a@])" print t
| Let _ | TyArrow (_,_) -> fpf out "(@[%a@])" print t
and print_infix_list b out l = match l with
| [] -> assert false
| [t] -> print_in_app out t
| t :: l' ->
fpf out "@[%a@]@ @[%a@] %a"
print_in_app t (Builtin.pp print_in_app) b (print_infix_list b) l'
end
type 'a print = (module PRINT with type t = 'a)
(** {2 Utils} *)
module type UTIL_REPR = sig
type t_
val head_sym : t_ -> id
(** Search for a head symbol
@raise Not_found if not an application/const *)
val to_seq : t_ -> t_ Sequence.t
(** Iterate on sub-terms *)
val to_seq_vars : t_ -> t_ Var.t Sequence.t
(** Iterate on variables *)
val to_seq_free_vars : t_ -> t_ Var.t Sequence.t
(** Iterate on free variables. *)
val free_meta_vars : ?init:t_ MetaVar.t ID.Map.t -> t_ -> t_ MetaVar.t ID.Map.t
(** The free type meta-variables in [t] *)
val ty_unfold : t_ -> t_ Var.t list * t_ list * t_
(** [ty_unfold ty] decomposes [ty] into a list of quantified type variables,
a list of parameters, and a return type (which is not an arrow).
[ty_unfold (forall a b, a -> b -> c -> d)] will return
[([a;b], [a;b;c], d)]
*)
val ty_is_Type : t_ -> bool
(** t == Type? *)
val ty_returns_Type : t_ -> bool
(** t == forall ... -> ... -> ... -> Type? *)
val ty_returns_Prop : t_ -> bool
val ty_returns : t_ -> t_
(** follow forall/arrows to get return type. *)
val ty_is_Kind : t_ -> bool
(** type == Kind? *)
val ty_is_Prop : t_ -> bool
(** t == Prop? *)
val ty_num_param : t_ -> int
(** Number of type variables that must be bound in the type. *)
end
(** Utils that only require a {!REPR} *)
module UtilRepr(T : REPR)
: UTIL_REPR with type t_ = T.t
= struct
type t_ = T.t
let rec head_sym t = match T.repr t with
| App (f, _) -> head_sym f
| Const id -> id
| _ -> raise Not_found
let to_seq t yield =
let rec aux t =
yield t;
match T.repr t with
| TyMeta _ -> ()
| TyBuiltin _
| Const _ -> ()
| Var v -> aux_var v
| Match (t,l) ->
aux t;
ID.Map.iter (fun _ (vars,rhs) -> List.iter aux_var vars; aux rhs) l
| Builtin b -> Builtin.iter aux b
| App (f,l) -> aux f; List.iter aux l
| Bind (_,v,t) -> aux_var v; aux t
| Let (v,t,u) -> aux_var v; aux t; aux u
| TyArrow (a,b) -> aux a; aux b
and aux_var v = aux (Var.ty v)
in
aux t
let to_seq_free_vars t yield =
let module VarSet = Var.Set(T) in
let rec aux ~bound t = match T.repr t with
| Const _ -> ()
| Var v ->
if VarSet.mem v bound then () else yield v;
aux ~bound (Var.ty v)
| App (f,l) ->
aux ~bound f; List.iter (aux ~bound) l
| Match (t,l) ->
aux ~bound t;
ID.Map.iter
(fun _ (vars,rhs) ->
List.iter (fun v -> aux ~bound (Var.ty v)) vars;
let bound = List.fold_right VarSet.add vars bound in
aux ~bound rhs
) l
| Builtin b -> Builtin.iter (aux ~bound) b
| Bind (_,v,t) ->
aux ~bound (Var.ty v); aux ~bound:(VarSet.add v bound) t
| Let (v,t,u) ->
aux ~bound (Var.ty v); aux ~bound t; aux ~bound:(VarSet.add v bound) u
| TyBuiltin _ -> ()
| TyArrow (a,b) -> aux ~bound a; aux ~bound b
| TyMeta _ -> ()
in
aux ~bound:VarSet.empty t
let to_seq_vars t =
to_seq t
|> Sequence.flat_map
(fun t -> match T.repr t with
| Var v
| Bind (_,v,_)
| Let (v,_,_) -> Sequence.return v
| Match (_,l) ->
let open Sequence.Infix in
ID.Map.to_seq l >>= fun (_,(vars,_)) -> Sequence.of_list vars
| Builtin _
| Const _
| App _
| TyBuiltin _
| TyArrow (_,_)
| TyMeta _ -> Sequence.empty
)
let to_seq_meta_vars t =
to_seq t
|> Sequence.filter_map
(fun t -> match T.repr t with
| TyMeta v -> Some v
| Var _
| Bind _
| Builtin _
| Const _
| Let _
| Match _
| App _
| TyBuiltin _
| TyArrow (_,_) -> None
)
let free_meta_vars ?(init=ID.Map.empty) t =
to_seq_meta_vars t
|> Sequence.fold
(fun acc v -> ID.Map.add (MetaVar.id v) v acc)
init
let ty_unfold t =
let rec aux1 t = match T.repr t with
| TyArrow (l, r) ->
let args, ret = aux2 r in
[], l :: args, ret
| Bind (`TyForall, v, t') ->
let vs, args, ret = aux1 t' in
v :: vs, args, ret
| _ -> [], [], t
and aux2 t = match T.repr t with
| TyArrow (l, r) ->
let args, ret = aux2 r in
l :: args, ret
| _ -> [], t
in
aux1 t
let ty_is_Type t = match T.repr t with
| TyBuiltin `Type -> true
| _ -> false
let ty_is_Kind t = match T.repr t with
| TyBuiltin `Kind -> true
| _ -> false
let ty_is_Prop t = match T.repr t with
| TyBuiltin `Prop -> true
| _ -> false
let rec ty_returns t = match T.repr t with
| TyArrow (_, t') -> ty_returns t'
| Bind (`TyForall, _, t') -> ty_returns t'
| _ -> t
let ty_returns_Type t = match T.repr (ty_returns t) with
| TyBuiltin `Type -> true
| _ -> false
let ty_returns_Prop t = match T.repr (ty_returns t) with
| TyBuiltin `Prop -> true
| _ -> false
(* number of parameters of this (polymorphic?) T.t type *)
let rec ty_num_param ty = match T.repr ty with
| TyMeta _ -> 0
| Var _ -> 0
| Const _
| App _
| TyBuiltin _ -> 0
| TyArrow (a,t') ->
if ty_is_Type a
then 1 + ty_num_param t' (* [a] is a type parameter *)
else 0 (* asks for term parameters *)
| Bind (`TyForall, _,t) -> 1 + ty_num_param t
| _ -> assert false
end
exception Undefined of id
(** When a symbol is not defined *)
let () = Printexc.register_printer
(function
| Undefined id -> Some ("undefined ID: " ^ ID.to_string id)
| _ -> None
)
module type UTIL = sig
include UTIL_REPR
val const : id -> t_
val builtin : t_ Builtin.t -> t_
val app_builtin : t_ Builtin.t -> t_ list -> t_
val var : t_ var -> t_
val app : t_ -> t_ list -> t_
val fun_ : t_ var -> t_ -> t_
val mu : t_ var -> t_ -> t_
val let_ : t_ var -> t_ -> t_ -> t_
val match_with : t_ -> t_ cases -> t_
val ite : t_ -> t_ -> t_ -> t_
val forall : t_ var -> t_ -> t_
val exists : t_ var -> t_ -> t_
val eq : t_ -> t_ -> t_
val neq : t_ -> t_ -> t_
val equiv : t_ -> t_ -> t_
val imply : t_ -> t_ -> t_
val true_ : t_
val false_ : t_
val and_ : t_ list -> t_
val or_ : t_ list -> t_
val not_ : t_ -> t_
val undefined_ : t_ -> t_ (** fresh undefined term *)
val data_test : ID.t -> t_ -> t_
val data_select : ID.t -> int -> t_ -> t_
val asserting : t_ -> t_ list -> t_
val assuming : t_ -> t_ list -> t_
val guard : t_ -> t_ Builtin.guard -> t_
val mk_bind : Binder.t -> t_ var -> t_ -> t_
val ty_type : t_ (** Type of types *)
val ty_kind : t_ (** Type of ty_type *)
val ty_prop : t_ (** Propositions *)
val ty_builtin : TyBuiltin.t -> t_
val ty_const : id -> t_
val ty_app : t_ -> t_ list -> t_
val ty_arrow : t_ -> t_ -> t_
val ty_arrow_l : t_ list -> t_ -> t_
val ty_var : t_ var -> t_
val ty_meta : t_ MetaVar.t -> t_
val ty_forall : t_ var -> t_ -> t_
val fun_l : t_ var list -> t_ -> t_
val forall_l : t_ var list -> t_ -> t_
val exists_l : t_ var list -> t_ -> t_
val hash_fun : t_ CCHash.hash_fun
val hash : t_ -> int
(** Hash into a positive integer *)
val equal : t_ -> t_ -> bool
(** Syntactic equality *)
(** {6 Traversal} *)
val fold :
f:('acc -> 'b_acc -> t_ -> 'acc) ->
bind:('b_acc -> t_ Var.t -> 'b_acc) ->
'acc ->
'b_acc ->
t_ ->
'acc
(** Non recursive fold.
@param bind updates the binding accumulator with the bound variable
@param f used to update the regular accumulator (that is returned) *)
val iter :
f:('b_acc -> t_ -> unit) ->
bind:('b_acc -> t_ Var.t -> 'b_acc) ->
'b_acc ->
t_ ->
unit
(** Non recursive iter.
@param bind updates the binding accumulator with the bound variable
@param f called on immediate subterms and on the regular accumulator *)
val map :
f:('b_acc -> t_ -> t_) ->
bind:('b_acc -> t_ Var.t -> 'b_acc * t_ Var.t) ->
'b_acc ->
t_ ->
t_
(** Non recursive map.
@param f maps a term to a term
@param bind updates the binding accumulator and returns a new variable *)
val map_pol :
f:('b_acc -> Polarity.t -> t_ -> t_) ->
bind:('b_acc -> Polarity.t -> t_ Var.t -> 'b_acc * t_ Var.t) ->
'b_acc ->
Polarity.t ->
t_ ->
t_
(** Similar to {!map} but also keeping the subterm polarity *)
(** {6 Substitution Utils} *)
type subst = (t_, t_) Var.Subst.t
val equal_with : subst:subst -> t_ -> t_ -> bool
(** Equality modulo substitution *)
val deref : subst:subst -> t_ -> t_
(** [deref ~subst t] dereferences [t] as long as it is a variable
bound in [subst]. *)
exception ApplyError of string * t_ * t_ list
(** Raised when a type application fails *)
val eval : subst:subst -> t_ -> t_
(** Applying a substitution *)
val eval_renaming : subst:(t_, t_ Var.t) Var.Subst.t -> t_ -> t_
(** Applying a variable renaming *)
val ty_apply : t_ -> t_ list -> t_
(** [apply t l] computes the type of [f args] where [f : t] and [args : l].
@raise ApplyError if the arguments do not match *)
val ty_apply_full : t_ -> t_ list -> t_ * subst
(** [ty_apply_full ty l] is like [apply t l], but it returns a pair
[ty' , subst] such that [subst ty' = apply t l].
@raise ApplyError if the arguments do not match *)
type signature = id -> t_ option
(** A signature is a way to obtain the type of a variable *)
val ty : sigma:signature -> t_ -> t_ or_error
(** Compute the type of the given term in the given signature. *)
val ty_exn : sigma:signature -> t_ -> t_
(** Same as {!ty} but unsafe.
@raise Failure in case of error at an application
@raise Undefined in case some symbol is not defined *)
exception UnifError of string * t_ * t_
(** Raised for unification or matching errors *)
val match_exn : ?subst2:subst -> t_ -> t_ -> subst
(** [match_exn ~subst2 t1 t2] matches the pattern [t1] against [subst2 t2].
Variables in [subst2 t2] are not bound.
We assume [t1] and [subst2 t2] do not share variables, and we assume
that [t1] is a mostly first-order {b pattern} (no binders, but variables
in head position is accepted and will only match an application).
@raise UnifError if they dont_ match
@raise Invalid_argument if [t1] is not a valid pattern *)
val match_ : ?subst2:subst -> t_ -> t_ -> subst option
(** Safe version of {!match_exn}
@raise Invalid_argument if [t1] is not a valid pattern *)
(* TODO: unification *)
end
module Util(T : S)
: UTIL with type t_ = T.t
= struct
include UtilRepr(T)
let ty_type = T.build (TyBuiltin `Type)
let ty_kind = T.build (TyBuiltin `Kind)
let ty_prop = T.build (TyBuiltin `Prop)
let const id = T.build (Const id)
let var v = T.build (Var v)
let app t l = T.build (App(t,l))
let mk_bind b v t = T.build (Bind (b, v, t))
let fun_ v t = T.build (Bind (`Fun, v, t))
let mu v t = T.build (Bind (`Mu, v, t))
let let_ v t u = match T.repr u with
| Builtin `True
| Builtin `False -> u
| _ -> T.build (Let (v, t, u))
let match_with t l =
if ID.Map.is_empty l then invalid_arg "Term.case: empty list of cases";
T.build (Match (t,l))
let forall v t = T.build (Bind(`Forall,v, t))
let exists v t = T.build (Bind(`Exists,v, t))
exception FlattenExit of T.t
let flatten (b:[<`And | `Or]) l =
try
CCList.flat_map
(fun t -> match T.repr t with
| Builtin `True when b=`And -> []
| Builtin `True when b=`Or -> raise (FlattenExit t) (* shortcut *)
| Builtin `False when b=`Or -> []
| Builtin `False when b=`And -> raise (FlattenExit t)
| App (f, l') ->
begin match T.repr f with
| Builtin `Or when b=`Or -> l'
| Builtin `And when b=`And -> l'
| _ -> [t]
end
| _ -> [t])
l
with FlattenExit t ->
[t]
let builtin_ b = T.build (Builtin b)
let app_builtin_ b l = app (builtin_ b) l
let true_ = builtin_ `True
let false_ = builtin_ `False
let rec builtin arg = match arg with
| `Ite (a,b,c) ->
begin match T.repr a, T.repr b, T.repr c with
| Builtin `True, _, _ -> b
| Builtin `False, _, _ -> c
| _, Builtin `True, Builtin `False -> a
| _, Builtin `False, Builtin `True -> not_ a
| _, Builtin `True, Builtin `True -> true_
| _, Builtin `False, Builtin `False -> false_
| _, Builtin `True, _ -> imply (not_ a) c (* then branch: true *)
| _, Builtin `False, _ -> and_ [not_ a; c] (* then branch: false *)
| _, _, Builtin `True -> imply a b (* else branch: true *)
| _, _, Builtin `False -> and_ [a; b] (* else branch: false *)
| _ -> builtin_ arg
end
| `Equiv (a,b) ->
begin match T.repr a, T.repr b with
| Builtin `True, _ -> b
| _, Builtin `True -> a
| Builtin `False, _ -> not_ b
| _, Builtin `False -> not_ a
| _ -> builtin_ arg
end
| _ -> builtin_ arg
and app_builtin arg l = match arg, l with
| `And, _ ->
begin match flatten `And l with
| [] -> true_
| [x] -> x
| l -> app_builtin_ `And l
end
| `Or, _ ->
begin match flatten `Or l with
| [] -> false_
| [x] -> x
| l -> app_builtin_ `Or l
end
| `Not, [t] ->
begin match T.repr t with
| Builtin `True -> false_
| Builtin `False -> true_
| App (f, l) ->
begin match T.repr f, l with
| Builtin `And, _ -> or_ (List.map not_ l)
| Builtin `Or, _ -> and_ (List.map not_ l)
| Builtin `Not, [t] -> t
| _ -> app_builtin_ `Not [t]
end
| _ -> app_builtin_ `Not [t]
end
| `Imply, [a;b] ->
begin match T.repr a, T.repr b with
| Builtin `True, _ -> b
| Builtin `False, _ -> true_
| _, Builtin `True -> true_
| _, Builtin `False -> not_ a
| _ -> app_builtin_ arg l
end
| (`Ite _ | `Eq _ | `Equiv _), [] -> builtin arg
| _ -> app_builtin_ arg l
and not_ t = app_builtin `Not [t]
and and_ l = app_builtin `And l
and or_ l = app_builtin `Or l
and imply a b = app_builtin `Imply [a;b]
let eq a b = builtin (`Eq (a,b))
let neq a b = not_ (eq a b)
let equiv a b = builtin (`Equiv (a,b))
let ite a b c = app_builtin (`Ite (a,b,c)) []
let undefined_ t =
let id = ID.make "_" in
builtin (`Undefined (id,t))
let data_test c t = app_builtin (`DataTest c) [t]
let data_select c i t = app_builtin (`DataSelect (c,i)) [t]
let guard t g =
let open Builtin in
match T.repr t, g.asserting, g.assuming with
| _, [], [] -> t
| Builtin (`Guard (t', g')), _, _ ->
let g' = {
assuming = g.assuming @ g'.assuming;
asserting = g.asserting @ g'.asserting;
} in
builtin (`Guard (t', g'))
| _ ->
builtin (`Guard (t, g))
let asserting t p = guard t {Builtin. asserting=p; assuming=[]}
let assuming t p = guard t {Builtin. assuming=p; asserting=[]}
let ty_builtin b = T.build (TyBuiltin b)
let ty_const id = const id
let ty_app f l = if l=[] then f else app f l
let ty_arrow a b = T.build (TyArrow (a,b))
let ty_arrow_l args ret = List.fold_right ty_arrow args ret
let ty_forall v t = T.build (Bind (`TyForall,v,t))
let ty_var v = T.build (Var v)
let ty_meta v = T.build (TyMeta v)
let fun_l = List.fold_right fun_
let forall_l = List.fold_right forall
let exists_l = List.fold_right exists
let hash_fun t h =
let d = ref 30 in (* number of nodes to explore *)
let rec hash_ t h =
if !d = 0 then h
else match T.repr t with
| Const id -> decr d; ID.hash_fun id h
| Var v -> decr d; hash_var_ v h
| App (f,l) -> hash_ f h |> CCHash.list hash_ l
| Builtin b -> CCHash.seq hash_ (Builtin.to_seq b) h
| Let (v,t,u) -> decr d; hash_var_ v h |> hash_ t |> hash_ u
| Bind (_,v,t) -> decr d; hash_var_ v h |> hash_ t
| Match (t,l) ->
decr d;
hash_ t h
|> CCHash.seq
(fun (vars,rhs) h -> CCHash.list hash_var_ vars h |> hash_ rhs)
(ID.Map.to_seq l |> Sequence.map snd)
| TyArrow (a,b) -> decr d; hash_ a h |> hash_ b
| TyBuiltin _
| TyMeta _ -> h
and hash_var_ v h = ID.hash_fun (Var.id v) h
in
hash_ t h
let hash t = CCHash.apply hash_fun t
module Subst = Var.Subst
type subst = (T.t, T.t) Subst.t
let rec equal_with ~subst ty1 ty2 =
match T.repr ty1, T.repr ty2 with
| Const id1, Const id2 -> ID.equal id1 id2
| Var v1, _ when Subst.mem ~subst v1 ->
equal_with ~subst (Subst.find_exn ~subst v1) ty2
| _, Var v2 when Subst.mem ~subst v2 ->
equal_with ~subst ty1 (Subst.find_exn ~subst v2)
| Var v1, Var v2 -> Var.equal v1 v2
| Builtin b1, Builtin b2 -> Builtin.equal (equal_with ~subst) b1 b2
| TyBuiltin b1, TyBuiltin b2 -> TyBuiltin.equal b1 b2
| TyMeta v1, TyMeta v2 -> MetaVar.equal v1 v2
| App (f1,l1), App (f2, l2) ->
equal_with ~subst f1 f2
&& List.length l1 = List.length l2
&& List.for_all2 (equal_with ~subst) l1 l2
| TyArrow (a1,b1), TyArrow (a2,b2) ->
equal_with ~subst a1 a2 && equal_with ~subst b1 b2
| Bind (b1, v1, t1), Bind (b2, v2, t2) ->
b1 = b2 &&
( let v = Var.fresh_copy v1 in
let subst = Subst.add ~subst v1 (var v) in
let subst = Subst.add ~subst v2 (var v) in
equal_with ~subst t1 t2)
| Let (v1,t1,u1), Let (v2,t2,u2) ->
let subst = Subst.add ~subst v1 t1 in
let subst = Subst.add ~subst v2 t2 in
equal_with ~subst u1 u2
| Match (t1,l1), Match (t2,l2) ->
ID.Map.cardinal l1 = ID.Map.cardinal l2 &&
equal_with ~subst t1 t2 &&
List.for_all2
(fun (id1,(vars1,rhs1)) (id2,(vars2,rhs2)) ->
assert (List.length vars1=List.length vars2);
ID.equal id1 id2
&&
let subst = List.fold_right2
(fun v1 v2 subst ->
let v = Var.fresh_copy v1 in
let subst = Subst.add ~subst v1 (var v) in
let subst = Subst.add ~subst v2 (var v) in
subst
) vars1 vars2 subst
in
equal_with ~subst rhs1 rhs2