-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtranslate.ml
More file actions
2079 lines (1872 loc) · 66.8 KB
/
Copy pathtranslate.ml
File metadata and controls
2079 lines (1872 loc) · 66.8 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
(**************************************************************************)
(* *)
(* Alt-Ergo: The SMT Solver For Software Verification *)
(* Copyright (C) --- OCamlPro SAS *)
(* *)
(* This file is distributed under the terms of OCamlPro *)
(* Non-Commercial Purpose License, version 1. *)
(* *)
(* As an exception, Alt-Ergo Club members at the Gold level can *)
(* use this file under the terms of the Apache Software License *)
(* version 2.0. *)
(* *)
(* --------------------------------------------------------------- *)
(* *)
(* More details can be found in the directory licenses/ *)
(* *)
(**************************************************************************)
open D_loop
module E = Expr
module SE = E.Set
module C = Commands
module Sy = Symbols
module SM = Sy.Map
module DE = DStd.Expr
module DT = DE.Ty
module B = DStd.Builtin
let unsupported msg =
Fmt.kstr
(fun str -> Errors.(run_error (Unsupported_feature str)))
msg
type id = Id : 'a DE.id -> id[@@unboxed]
module HT =
Hashtbl.Make (struct
type t = id
let equal (Id id1) (Id id2) = DE.Id.equal id1 id2
let hash (Id i)= DE.Id.hash i
end)
module Cache = struct
let ae_sy_ht: Sy.t HT.t = HT.create 100
let ae_ty_ht: Ty.t HT.t = HT.create 100
let store_sy id sy =
HT.add ae_sy_ht (Id id) sy
let store_ty id ty =
HT.add ae_ty_ht (Id id) ty
let find_sy id =
HT.find ae_sy_ht (Id id)
let find_var ind =
match find_sy ind with
| Sy.Var v -> v
| sym ->
Fmt.failwith
"Internal error: Expected to find a variable symbol,\
instead found (%a)"
Sy.print sym
let store_var ind v =
store_sy ind (Sy.var v)
let find_ty id =
HT.find ae_ty_ht (Id id)
let fresh_ty ?(is_var = true) () =
if is_var
then Ty.fresh_tvar ()
else Ty.fresh_empty_text ()
let update_ty_store_ret ?(is_var = true) id =
let ty = fresh_ty ~is_var () in
store_ty id ty;
ty
let find_update_ty ?(is_var = true) id =
match HT.find_opt ae_ty_ht (Id id) with
| Some ty -> ty
| None ->
update_ty_store_ret ~is_var id
let store_tyv ?(is_var = true) t_v =
let ty = fresh_ty ~is_var () in
store_ty t_v ty
let store_tyvl ?(is_var = true) (tyvl: DE.ty_var list) =
List.iter (store_tyv ~is_var) tyvl
let store_tyv_ret ?(is_var = true) t_v =
update_ty_store_ret ~is_var t_v
let store_tyvl_ret ?(is_var = true) (tyvl: DE.ty_var list) =
List.map (store_tyv_ret ~is_var) tyvl
let store_sy_vl_names (tvl: DE.term_var list) =
List.iter (
fun ({ DE.path; _ } as tv) ->
let name = Util.get_basename path in
(* TODO : Check this line! *)
store_sy tv (Sy.name @@ Id.of_string ~ns:Internal name)
) tvl
let store_ty_vars ?(is_var = true) ty =
match DT.view ty with
| `Var ty_v ->
store_tyv ~is_var ty_v
| `Pi (tyvl, _) ->
store_tyvl ~is_var tyvl
| _-> ()
let store_ty_vars_ret ?(is_var = true) ty =
match DT.view ty with
| `Var ty_v ->
[store_tyv_ret ~is_var ty_v]
| `Pi (tyvl, _) ->
store_tyvl_ret ~is_var tyvl
| _-> []
(* Assumes that the two cases are the only cases in which type variables are
introduced *)
let clear () =
HT.clear ae_sy_ht;
HT.clear ae_ty_ht
end
(** Builtins *)
type _ DStd.Builtin.t +=
| Float
| AERound of int * int
(** Equivalent of Float for the SMT2 format. *)
| Integer_round
| Abs_real
| Sqrt_real
| Sqrt_real_default
| Sqrt_real_excess
| Ceiling_to_int of [ `Real ]
| Max_real
| Min_real
| Max_int
| Min_int
| Integer_log2
(* Internal use for semantic triggers -- do not expose outside of theories *)
| Not_theory_constant | Is_theory_constant | Linear_dependency
let builtin_term t = Dl.Typer.T.builtin_term t
let builtin_ty t = Dl.Typer.T.builtin_ty t
let ty (ty_cst : DE.ty_cst) ty =
let name = Util.get_basename ty_cst.path in
DStd.Id.Map.add { name = DStd.Name.simple name; ns = Sort } @@
fun env s ->
builtin_ty @@
Dolmen_type.Base.app0 (module Dl.Typer.T) env s ty
let fpa_rounding_mode, rounding_modes, add_rounding_modes =
match DT.view Fpa_rounding.fpa_rounding_mode_dty with
| `App ((`Generic ty_cst), []) ->
let constrs = Fpa_rounding.d_constrs in
let add_constrs map =
List.fold_left (fun map (c : DE.term_cst) ->
let name = Util.get_basename c.path in
DStd.Id.Map.add { name = DStd.Name.simple name; ns = Term }
(fun env _ ->
builtin_term @@
Dolmen_type.Base.term_app_cst
(module Dl.Typer.T) env c) map)
map constrs
in
Cache.store_ty ty_cst Fpa_rounding.fpa_rounding_mode;
Fpa_rounding.fpa_rounding_mode_dty,
constrs,
fun map ->
map
|> ty ty_cst Fpa_rounding.fpa_rounding_mode_dty
|> add_constrs
| _ -> assert false
module Const = struct
open DE
let smt_round =
with_cache (fun (n, m) ->
let name = "ae.round" in
DE.Id.mk
~name
~builtin:(AERound (n, m))
(DStd.Path.global name)
Ty.(arrow [fpa_rounding_mode; real] real))
end
let smt_round n m rm t =
DE.Term.apply_cst (Const.smt_round (n, m)) [] [rm; t]
(** Takes a dolmen identifier [id] and injects it in Alt-Ergo's registered
identifiers.
It transforms "fpa_rounding_mode", the Alt-Ergo builtin type into the SMT2
rounding type "RoundingMode". Also injects each constructor into their SMT2
equivalent *)
let inject_ae_to_smt2 id =
match id with
| DStd.Id.{name = Simple n; _} ->
begin
if String.equal n Fpa_rounding.fpa_rounding_mode_ae_type_name then
(* Injecting the type name as the SMT2 Type name. *)
let name =
Dolmen_std.Name.simple Fpa_rounding.fpa_rounding_mode_type_name
in
{id with name}
else
match Fpa_rounding.rounding_mode_of_ae n with
| rm ->
let name =
Dolmen_std.Name.simple (Fpa_rounding.string_of_rounding_mode rm)
in
{id with name}
| exception (Failure _) ->
id
end
| id -> id
let ae_fpa_builtins =
let (->.) args ret = (args, ret) in
let dterm name f =
DStd.Id.Map.add { name = DStd.Name.simple name; ns = Term } @@
fun env s ->
builtin_term @@
Dolmen_type.Base.term_app1 (module Dl.Typer.T) env s f
in
let op ?(tyvars = []) name builtin (args, ret) =
let ty = DT.pi tyvars @@ DT.arrow args ret in
let cst = DE.Id.mk ~name ~builtin (DStd.Path.global name) ty in
DStd.Id.Map.add { name = DStd.Name.simple name; ns = Term } @@
fun env _ ->
builtin_term @@
Dolmen_type.Base.term_app_cst
(module Dl.Typer.T) env cst
in
let float_cst =
let ty = DT.(arrow [int; int; fpa_rounding_mode; real] real) in
DE.Id.mk ~name:"float" ~builtin:Float (DStd.Path.global "float") ty
in
let float prec exp mode x =
DE.Term.apply_cst float_cst [] [prec; exp; mode; x]
in
let mode m =
let cst =
List.find (fun cst ->
match cst.DE.path with
| Absolute { name; _ } -> String.equal name m
| Local _ -> false)
rounding_modes
in
DE.Term.apply_cst cst [] []
in
let float32 = float (DE.Term.int "24") (DE.Term.int "149") in
let float32d x = float32 (mode "NearestTiesToEven") x in
let float64 = float (DE.Term.int "53") (DE.Term.int "1074") in
let float64d x = float64 (mode "NearestTiesToEven") x in
let partial1 name f =
DStd.Id.Map.add { name = DStd.Name.simple name; ns = Term } @@
fun env s ->
builtin_term @@
Dolmen_type.Base.term_app1 (module Dl.Typer.T) env s f
in
let partial2 name f =
DStd.Id.Map.add { name = DStd.Name.simple name; ns = Term } @@
fun env s ->
builtin_term @@
Dolmen_type.Base.term_app2 (module Dl.Typer.T) env s f
in
let is_theory_constant =
let open DT in
let a = Var.mk "alpha" in
op
~tyvars:[a]
"is_theory_constant"
Is_theory_constant
([of_var a] ->. prop)
in
let fpa_builtins =
let open DT in
DStd.Id.Map.empty
|> add_rounding_modes
(* the first argument is mantissas' size (including the implicit bit),
the second one is the exp of the min representable normalized number,
the third one is the rounding mode, and the last one is the real to
be rounded *)
|> op "float" Float ([int; int; fpa_rounding_mode; real] ->. real)
|> partial2 "float32" float32
|> partial1 "float32d" float32d
|> partial2 "float64" float64
|> partial1 "float64d" float64d
(* rounds to nearest integer *)
|> op "integer_round" Integer_round ([fpa_rounding_mode; real] ->. int)
(* type cast: from int to real *)
|> dterm "real_of_int" DE.Term.Int.to_real
(* type check: integers *)
|> dterm "real_is_int" DE.Term.Real.is_int
(* abs value of a real *)
|> op "abs_real" Abs_real ([real] ->. real)
(* sqrt value of a real *)
|> op "sqrt_real" Sqrt_real ([real] ->. real)
(* sqrt value of a real by default *)
|> op "sqrt_real_default" Sqrt_real_default ([real] ->. real)
(* sqrt value of a real by excess *)
|> op "sqrt_real_excess" Sqrt_real_excess ([real] ->. real)
(* abs value of an int *)
|> dterm "abs_int" DE.Term.Int.abs
(* (integer) floor of a rational *)
|> dterm "int_floor" DE.Term.Real.floor_to_int
(* (integer) ceiling of a ratoinal *)
|> op "int_ceil" (Ceiling_to_int `Real) ([real] ->. int)
(* The functions below are only interpreted when applied on constants.
Aximatization for the general case are not currently imlemented *)
(* maximum of two reals *)
|> op "max_real" Max_real ([real; real] ->. real)
(* minimum of two reals *)
|> op "min_real" Min_real ([real; real] ->. real)
(* maximum of two ints *)
|> op "max_int" Max_int ([int; int] ->. int)
(* minimum of two ints *)
|> op "min_int" Min_int ([int; int] ->. int)
(* computes an integer log2 of a real. The function is only
interpreted on (non-zero) positive real constants. When applied on a
real 'm', the result 'res' of the function is such that: 2^res <= m <
2^(res+1) *)
|> op "integer_log2" Integer_log2 ([real] ->. int)
(* only used for arithmetic. It should not be used for x in float(x)
to enable computations modulo equality *)
|> op "not_theory_constant" Not_theory_constant ([real] ->. prop)
|> is_theory_constant
|> op "linear_dependency" Linear_dependency ([real; real] ->. prop)
in
fun env s ->
let search_id id =
try
DStd.Id.Map.find_exn id fpa_builtins env s
with Not_found -> `Not_found
in
match s with
| Dl.Typer.T.Id id ->
let new_id = inject_ae_to_smt2 id in
search_id new_id
| Builtin _ -> `Not_found
let smt_fpa_builtins =
let term_app env s f =
Dl.Typer.T.builtin_term @@
Dolmen_type.Base.term_app2 (module Dl.Typer.T) env s f
in
let other_builtins =
DStd.Id.Map.empty
|> add_rounding_modes
in
fun env s ->
match s with
| Dl.Typer.T.Id {
ns = Term ;
name = Indexed {
basename = "ae.round" ;
indexes = [ i; j ] } } ->
begin match
int_of_string i,
int_of_string j
with
| n, m -> term_app env s (smt_round n m)
| exception Failure _ -> `Not_found
end
| Id { ns = Term ; name = Simple "ae.float16" } ->
term_app env s (smt_round 11 24)
| Id { ns = Term ; name = Simple "ae.float32" } ->
term_app env s (smt_round 24 149)
| Id { ns = Term ; name = Simple "ae.float64" } ->
term_app env s (smt_round 53 1074)
| Id { ns = Term ; name = Simple "ae.float128" } ->
term_app env s (smt_round 113 16494)
| Dl.Typer.T.Id id -> begin
match DStd.Id.Map.find_exn id other_builtins env s with
| e -> e
| exception Not_found -> `Not_found
end
| _ -> `Not_found
(* Custom attribute used to name lemmas. *)
let lemma_name_attr : string DStd.Tag.t = DStd.Tag.create ()
type _ Dl.Typer.T.err +=
| Invalid_lemma_name
let smt_tag_builtins =
let module Type = Dl.Typer.T in
let make_op env s f =
Dl.Typer.T.builtin_tags @@
Dolmen_type.Base.make_op1 (module Dl.Typer.T) env s f
in
fun env s ->
match s with
| Type.Id { ns = Attr ; name = Simple ":lemma" } ->
make_op env s (fun ast t ->
match t with
| { term = Symbol { name = Simple name; _ }; _ } ->
[Type.Set (lemma_name_attr, name)]
| _ ->
(* TODO: add a custom printer as soon as the issue
https://github.com/Gbury/dolmen/issues/218 is solved. *)
Type._error env (Ast ast) Invalid_lemma_name
)
| _ -> `Not_found
let builtins =
fun _st (lang : Typer.lang) ->
match lang with
| `Logic Alt_ergo -> ae_fpa_builtins
| `Logic (Smtlib2 _) ->
(fun env s ->
match smt_fpa_builtins env s with
| `Not_found -> smt_tag_builtins env s
| r -> r)
| _ -> fun _ _ -> `Not_found
(** Translates dolmen locs to Alt-Ergo's locs *)
let dl_to_ael dloc_file (compact_loc: DStd.Loc.t) =
DStd.Loc.(lexing_positions (loc dloc_file compact_loc))
(** clears the cache in the [Cache] module. *)
let clear_cache () = Cache.clear ()
(** [dty_to_ty update is_var subst tyv_substs dty]
Converts a Dolmen type to an Alt-Ergo type.
- If [update] is [true] then for each type variable of type [DE.Ty.Var.t],
if it was not encountered before, a new type variable of type [Ty.t] is
created and added to the cache.
- If [dty] is a type application, or an arrow type, only its return type
is converted since those have no counterpart in AE's [Ty] module. The
function arguments' types or the type paramters ought to be converted
individually.
*)
let rec dty_to_ty ?(update = false) ?(is_var = false) dty =
let aux = dty_to_ty ~update ~is_var in
match DT.view dty with
| `Prop | `App (`Builtin B.Prop, []) -> Ty.Tbool
| `Int | `App (`Builtin B.Int, []) -> Ty.Tint
| `Real | `App (`Builtin B.Real, []) -> Ty.Treal
| `Array (ity, vty) ->
let ity = aux ity in
let vty = aux vty in
Ty.Tfarray (ity, vty)
| `Bitv n ->
if n <= 0 then Errors.typing_error (NonPositiveBitvType n) Loc.dummy;
Ty.Tbitv n
| `App (`Builtin B.Unit, []) -> Ty.tunit
| `App (`Builtin _, [ty]) -> aux ty
| `App (`Generic c, l) -> handle_ty_app ~update c l
| `Var ty_v when update ->
Cache.find_update_ty ty_v
| `Var ty_v ->
Cache.find_ty ty_v
| `Arrow (_, ty) -> aux ty
| `Pi (tyvl, ty) ->
if update then
Cache.store_tyvl ~is_var tyvl;
aux ty
| _ -> unsupported "Type %a" DE.Ty.print dty
and handle_ty_app ?(update = false) ty_c l =
(* Applies the substitutions in [tysubsts] to each encountered type
variable. *)
let rec apply_ty_substs tysubsts ty =
match ty with
| Ty.Tvar v ->
Ty.TvMap.find v tysubsts
| Text (tyl, hs) ->
Ty.Text (List.map (apply_ty_substs tysubsts) tyl, hs)
| Tfarray (ti, tv) ->
Tfarray (
apply_ty_substs tysubsts ti,
apply_ty_substs tysubsts tv
)
| Tadt (hs, tyl) ->
Tadt (hs, List.map (apply_ty_substs tysubsts) tyl)
| Trecord ({ args; lbs; _ } as rcrd) ->
Trecord {
rcrd with
args = List.map (apply_ty_substs tysubsts) args;
lbs = List.map (
fun (hs, t) ->
hs, apply_ty_substs tysubsts t
) lbs;
}
| _ -> ty
in
let tyl = List.map (dty_to_ty ~update) l in
(* Recover the initial versions of the types and apply them on the provided
type arguments stored in [tyl]. *)
match Cache.find_ty ty_c with
| Tadt (hs, _) -> Tadt (hs, tyl)
| Trecord { args; _ } as ty ->
let tysubsts =
List.fold_left2 (
fun acc tv ty ->
match tv with
| Ty.Tvar v -> Ty.TvMap.add v ty acc
| _ -> assert false
) Ty.TvMap.empty args tyl
in
apply_ty_substs tysubsts ty
| Text (_, s) -> Text (tyl, s)
| _ -> assert false
(** Handles a simple type declaration. *)
let mk_ty_decl (ty_c: DE.ty_cst) =
match DT.definition ty_c with
| Some (
(Adt
{ cases = [| { cstr = { id_ty; _ } as cstr; dstrs; _ } |]; _ } as adt)
) ->
(* Records and adts that only have one case are treated in the same way,
and considered as records. *)
Nest.attach_orders [adt];
let tyvl = Cache.store_ty_vars_ret id_ty in
let lbs =
Array.fold_right (
fun c acc ->
match c with
| Some (DE.{ id_ty; _ } as id) ->
let pty = dty_to_ty id_ty in
(id, pty) :: acc
| _ ->
Fmt.failwith
"Unexpected null label for some field of the record type %a"
DE.Ty.Const.print ty_c
) dstrs []
in
let ty = Ty.trecord ~record_constr:cstr tyvl ty_c lbs in
Cache.store_ty ty_c ty
| Some (Adt { cases; _ } as adt) ->
Nest.attach_orders [adt];
let tyvl = Cache.store_ty_vars_ret cases.(0).cstr.id_ty in
Cache.store_ty ty_c (Ty.t_adt ty_c tyvl);
let cs =
Array.fold_right (
fun DE.{ cstr; dstrs; _ } accl ->
let fields =
Array.fold_right (
fun tc_o acc ->
match tc_o with
| Some (DE.{ id_ty; _ } as field) ->
(field, dty_to_ty id_ty) :: acc
| None -> assert false
) dstrs []
in
(cstr, fields) :: accl
) cases []
in
let ty = Ty.t_adt ~body:(Some cs) ty_c tyvl in
Cache.store_ty ty_c ty
| None | Some Abstract ->
let ty_params = []
(* List.init ty_c.id_ty.arity (fun _ -> Ty.fresh_tvar ()) *)
in
let ty = Ty.text ty_params ty_c in
Cache.store_ty ty_c ty
(** Handles term declaration by storing the eventual present type variables
in the cache as well as the symbol associated to the term. *)
let mk_term_decl ({ id_ty; tags; _ } as tcst: DE.term_cst) =
let sy =
let id = Id.of_term_cst tcst in
begin match DStd.Tag.get tags DE.Tags.ac with
| Some () -> Sy.name ~kind:Sy.Ac id
| _ -> Sy.name id
end
in
Cache.store_sy tcst sy;
(* Adding polymorphic types to the cache. *)
Cache.store_ty_vars id_ty;
let arg_tys, ret_ty =
match DT.view id_ty with
| `Arrow (arg_tys, ret_ty) ->
List.map dty_to_ty arg_tys, dty_to_ty ret_ty
| _ -> [], dty_to_ty id_ty
in
(tcst, arg_tys, ret_ty)
(** Handles the definitions of a list of mutually recursive types.
- If one of the types is an ADT, the ADTs that have only one case are
considered as ADTs as well and not as records. *)
let mk_mr_ty_decls (tdl: DE.ty_cst list) =
let handle_ty_decl (ty: Ty.t) (tdef: DE.Ty.def option) =
match ty, tdef with
| Trecord { args; name; record_constr; _ },
Some (
Adt { cases = [| { dstrs; _ } |]; ty = ty_c; _ }
) ->
let lbs =
Array.fold_right (
fun c acc ->
match c with
| Some (DE.{ id_ty; _ } as id) ->
let pty = dty_to_ty id_ty in
(id, pty) :: acc
| _ ->
Fmt.failwith
"Unexpected null label for some field of the record type %a"
DE.Ty.Const.print ty_c
) dstrs []
in
let ty =
Ty.trecord ~record_constr args name lbs
in
Cache.store_ty ty_c ty
| Tadt (hs, tyl), Some (Adt { cases; ty = ty_c; _ }) ->
let cs =
Array.fold_right (
fun DE.{ cstr; dstrs; _ } accl ->
let fields =
Array.fold_right (
fun tc_o acc ->
match tc_o with
| Some (DE.{ id_ty; _ } as id) ->
(id, dty_to_ty id_ty) :: acc
| None -> assert false
) dstrs []
in
(cstr, fields) :: accl
) cases []
in
let ty = Ty.t_adt ~body:(Some cs) hs tyl in
Cache.store_ty ty_c ty
| _ -> assert false
in
(* If there are adts in the list of type declarations then records are
converted to adts, because that's how it's done in the legacy typechecker.
But it might be more efficient not to do that. *)
let rev_tdefs, contains_adts =
List.fold_left (
fun (acc, ca) ty_c ->
match DT.definition ty_c with
| Some (Adt { record; cases; _ } as df)
when not record && Array.length cases > 1 ->
df :: acc, true
| Some (Adt _ as df) ->
df :: acc, ca
| Some Abstract | None ->
assert false
) ([], false) tdl
in
Nest.attach_orders rev_tdefs;
let rev_l =
List.fold_left (
fun acc tdef ->
match tdef with
| DE.Adt { cases; record; ty = ty_c; } as adt ->
let tyvl = Cache.store_ty_vars_ret cases.(0).cstr.id_ty in
let record_constr = cases.(0).cstr in
let ty =
if (record || Array.length cases = 1) && not contains_adts
then
Ty.trecord ~record_constr tyvl ty_c []
else
Ty.t_adt ty_c tyvl
in
Cache.store_ty ty_c ty;
(ty, Some adt) :: acc
| Abstract ->
assert false (* unreachable in the second iteration *)
) [] (List.rev rev_tdefs)
in
List.iter (
fun (t, d) -> handle_ty_decl t d
) (List.rev rev_l)
(** Helper function hadle variables that are encoutered in patterns. *)
let handle_patt_var id (DE.{ term_descr; _ } as term) =
match term_descr with
| Cst ({ builtin = B.Base; id_ty; _ } as ty_c) ->
let ty = dty_to_ty id_ty in
let v = Var.of_id @@ Id.of_term_cst ty_c in
let sy = Sy.var v in
Cache.store_sy ty_c sy;
v, id, ty
| Var ({ builtin = B.Base; id_ty; _ } as ty_v) ->
let ty = dty_to_ty id_ty in
let v = Var.of_id @@ Id.of_term_cst ty_v in
let sy = Sy.var v in
Cache.store_sy ty_v sy;
v, id, ty
| _ ->
Fmt.failwith
"Expected a variable in a case match but got %a"
DE.Term.print term
module Match : sig
type pat
(** Type used as an intermediate description of patterns during the
match compilation. *)
val mk_pat : DE.term -> pat
(** Convert Dolmen pattern into the intermediate description. *)
val make : Expr.t -> (pat * Expr.t) list -> Expr.t
(** [make e l] compiles into ite expressions the match of the expression [e]
against the patterns of the branches [l]. *)
end = struct
type pat =
| Var of Var.t
| Constr of DE.term_cst * (Var.t * DE.term_cst * Ty.t) list
(** Helper function to translate patterns in a pattern-matching from a Dolmen
Term.t to an Alt-Ergo Expr.t *)
let mk_pat DE.{ term_descr; _ } =
match term_descr with
| App (
{ term_descr =
Cst ({ builtin = B.Constructor { adt; case; }; _ } as cst); _
}, _, pargs
) ->
let vnames =
begin match DT.definition adt with
| Some (Adt { cases; _ }) ->
let { DE.dstrs; _ } = cases.(case) in
Array.fold_right (
fun v acc ->
match v with
| Some dstr -> dstr :: acc
| _ -> assert false
) dstrs []
| _ ->
Fmt.failwith
"Expected a constructor for an algebraic data type but got\
something else for the definition of: %a"
DE.Ty.Const.print adt
end
in
let rev_args =
List.fold_left2 (
fun acc rn arg ->
let v, n, ty = handle_patt_var rn arg in
(v, n, ty) :: acc
) [] vnames pargs
in
Constr (cst, List.rev rev_args)
| Cst ({ builtin = B.Constructor _; _ } as cst) ->
Constr (cst, [])
| Var ({ builtin = B.Base; _ } as t_v) ->
(* Should the type be passed as an argument
instead of re-evaluating it here? *)
let v = Var.of_id @@ Id.of_term_cst t_v in
let sy = Sy.var v in
Cache.store_sy t_v sy;
(* Adding the matched variable to the store *)
Var v
| _ -> assert false
let rec compile mk_destr mk_tester e cases accu =
match cases with
| [] -> accu
| (Var x, p) :: _ ->
E.apply_subst (Var.Map.singleton x e, Ty.esubst) p
| (Constr (name, args), p) :: l ->
let _then =
List.fold_left
(fun acc (var, destr, ty) ->
let destr = mk_destr destr in
let d = E.mk_term destr [e] ty in
E.mk_let var d acc
) p args
in
match l with
[] -> _then
| _ ->
let _else = compile mk_destr mk_tester e l accu in
let cond = mk_tester name e in
E.mk_ite cond _then _else
let make e cases =
let ty = E.type_info e in
let mk_destr =
match ty with
| Ty.Tadt _ -> (fun hs -> Sy.destruct hs)
| Ty.Trecord _ -> (fun hs -> Sy.Op (Sy.Access hs))
| _ -> assert false
in
let mk_tester =
match ty with
| Ty.Tadt _ -> E.mk_tester
| Ty.Trecord _ ->
(* no need to test for records *)
(fun _e _name -> assert false)
| _ -> assert false
in
let res = compile mk_destr mk_tester e cases e in
(* debug_compile_match e cases res; *)
res
[@ocaml.ppwarning "TODO: introduce a let if e is a big expr"]
[@ocaml.ppwarning "TODO: add other elim schemes"]
[@ocaml.ppwarning "TODO: add a match construct in expr"]
end
let arith_ty = function
| `Int -> Ty.Tint
| `Real -> Ty.Treal
| `Rat -> unsupported "rationals"
(* Parse a semantic bound [x `b` y] and returns a tuple [(sort, lb, ub)] where:
- One of [x] or [y] *MUST* be the variable [var]
- [sort] is the sort of the bound ([Ty.Tint] or [Ty.Treal])
- [lb] is the (optional) lower bound for the variable [var]
- [ub] is the (optional) upper bound for the variable [var]
*)
let parse_semantic_bound ?(loc = Loc.dummy) ~var b x y =
let is_main_var { DE.term_descr; _ } =
match term_descr with
| DE.Var v -> DE.Id.equal v var
| _ -> false
in
assert (is_main_var x || is_main_var y);
let op, t =
match b with
| B.Lt t -> `Lt, t
| B.Leq t -> `Le, t
| B.Gt t -> `Gt, t
| B.Geq t -> `Ge, t
| _ ->
Fmt.failwith
"%aInternal error: invalid semantic bound"
Loc.report loc
in
let sort = arith_ty t in
let parse_bound_kind { DE.term_descr; _ } =
match term_descr with
| Cst { builtin = (B.Integer s | B.Rational s | B.Decimal s); _ } ->
Sy.ValBnd (Numbers.Q.from_string s)
| Var v -> Sy.VarBnd (Cache.find_var v)
| _ ->
Fmt.failwith
"%aInternal error: invalid semantic bound"
Loc.report loc
in
(* Parse [main_var `op` b] *)
let parse_bound ?(flip = false) b =
let b = parse_bound_kind b in
let is_open =
match op with
| `Lt | `Gt -> true
| `Le | `Ge -> false
and is_lower =
match op with
| `Lt | `Le -> flip
| `Gt | `Ge -> not flip
in
Sy.mk_bound b sort ~is_open ~is_lower
in
let bnd =
if is_main_var x then
parse_bound y
else
parse_bound ~flip:true x
in
if bnd.is_lower then sort, Some bnd, None else sort, None, Some bnd
let destruct_let e =
match e.DE.term_descr with
| Binder (Let_seq ls, body) ->
Some (ls, body)
| _ -> None
let destruct_app e =
match e.DE.term_descr with
| App ({ term_descr = Cst cst; _ }, _, args) ->
Some (cst.builtin, args)
| _ -> None
(* Helper functions *)
let mk_lt translate ty x y =
if ty == `Int then
let e3 =
E.mk_term (Sy.Op Sy.Minus) [translate y; E.int "1"] Ty.Tint
in
let e1 = translate x in
E.mk_builtin ~is_pos:true Sy.LE [e1; e3]
else
E.mk_builtin ~is_pos:true Sy.LT [translate x; translate y]
let mk_gt translate ty x y =
if ty == `Int then
let e3 =
E.mk_term (Sy.Op Sy.Minus) [translate x; E.int "1"] Ty.Tint
in
let e2 = translate y in
E.mk_builtin ~is_pos:true Sy.LE [e2; e3]
else
E.mk_builtin ~is_pos:true Sy.LT [translate y; translate x]
let mk_add translate sy ty l =
let rec aux_mk_add l =
match l with
| h :: t ->
let args = aux_mk_add t in
translate h :: args
| [] -> []
in
let args = aux_mk_add l in
E.mk_term sy args ty
let mk_rounding fpar =
let tcst = Fpa_rounding.term_cst_of_rounding_mode fpar in
let ty = Fpa_rounding.fpa_rounding_mode in
E.mk_constr tcst [] ty
(** [mk_expr ~loc ~name_base ~toplevel ~decl_kind term]
Builds an Alt-Ergo hashconsed expression from a dolmen term
*)
let rec mk_expr
?(loc = Loc.dummy) ?(name_base = "") ?(toplevel = false)
~decl_kind dt =
let name_tag = ref 0 in
let rec aux_mk_expr ?(toplevel = false)
(DE.{ term_descr; term_ty; term_tags = root_tags; _ } as term) =
let mk = aux_mk_expr in
let res =
match term_descr with
| Cst ({ builtin; _ } as tcst) ->
begin match builtin with
| B.True -> E.vrai
| B.False -> E.faux
| B.Integer s -> E.int s
| B.Decimal s -> E.real s
| B.Bitvec s ->
let ty = dty_to_ty term_ty in
E.bitv s ty
| B.Base ->
let sy = Cache.find_sy tcst in
let ty = dty_to_ty term_ty in
E.mk_term sy [] ty