Source file arith.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
module Id = Dolmen.Std.Id
module Term = Dolmen.Std.Term
module Ae = struct
module Tff
(Type : Tff_intf.S)
(Ty : Dolmen.Intf.Ty.Ae_Arith with type t := Type.Ty.t)
(T : Dolmen.Intf.Term.Ae_Arith with type t := Type.T.t
and type ty := Type.Ty.t) = struct
type _ Type.err +=
| Expected_arith_type : Type.Ty.t -> Term.t Type.err
let dispatch1 env (mk_int, mk_real) ast t =
let ty = T.ty t in
match Ty.view ty with
| `Int -> mk_int t
| `Real -> mk_real t
| _ -> Type._error env (Ast ast) (Expected_arith_type ty)
let dispatch2 env (mk_int, mk_real) ast a b =
let tya = T.ty a in
match Ty.view tya with
| `Int -> mk_int a b
| `Real -> mk_real a b
| _ -> Type._error env (Ast ast) (Expected_arith_type tya)
let promote_to_real t =
let ty = T.ty t in
match Ty.view ty with
| `Int -> T.Int.to_real t
| `Real -> t
| _ -> t
let parse env s =
match s with
| Type.Builtin Term.Int ->
`Ty (Base.app0 (module Type) env s Ty.int)
| Type.Builtin Term.Real ->
`Ty (Base.app0 (module Type) env s Ty.real)
| Type.Id { Id.ns = Value Integer; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.int name))
| Type.Id { Id.ns = Value Real; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.real name))
| Type.Builtin Term.Minus ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.minus, T.Real.minus)))
| Type.Builtin Term.Add ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.add, T.Real.add)))
| Type.Builtin Term.Sub ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.sub, T.Real.sub)))
| Type.Builtin Term.Mult ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.mul, T.Real.mul)))
| Type.Builtin Term.Div ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.div_e, T.Real.div)))
| Type.Builtin Term.Mod ->
`Term (Base.term_app2 (module Type) env s T.Int.rem_e)
| Type.Builtin Term.Int_pow ->
`Term (Base.term_app2 (module Type) env s T.Int.pow)
| Type.Builtin Term.Real_pow ->
`Term (fun ast args ->
Base.term_app2 (module Type) env s
(fun a b ->
let a' = promote_to_real a in
let b' = promote_to_real b in
T.Real.pow a' b') ast args)
| Type.Builtin Term.Lt ->
`Term (fun ast args ->
match args with
| [Term.{
term = App (
{ term = Builtin (Lt | Leq); _ }, [_; lr_st]
); _
} as l_st; r_st] ->
Base.term_app_list (module Type) env s
T._and ast [l_st; Term.lt lr_st r_st]
| _ ->
Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.lt, T.Real.lt)) ast args
)
| Type.Builtin Term.Leq ->
`Term (fun ast args ->
match args with
| [Term.{
term = App (
{ term = Builtin (Lt | Leq); _ }, [_; lr_st]
); _
} as l_st; r_st] ->
Base.term_app_list (module Type) env s
T._and ast [l_st; Term.leq lr_st r_st]
| _ ->
Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.le, T.Real.le)) ast args
)
| Type.Builtin Term.Gt ->
`Term (fun ast args ->
match args with
| [Term.{
term = App (
{ term = Builtin (Gt | Geq); _ }, [_; lr_st]
); _
} as l_st; r_st] ->
Base.term_app_list (module Type) env s
T._and ast [l_st; Term.gt lr_st r_st]
| _ ->
Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.gt, T.Real.gt)) ast args
)
| Type.Builtin Term.Geq ->
`Term (fun ast args ->
match args with
| [Term.{
term = App (
{ term = Builtin (Gt | Geq); _ }, [_; lr_st]
); _
} as l_st; r_st] ->
Base.term_app_list (module Type) env s
T._and ast [l_st; Term.geq lr_st r_st]
| _ ->
Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.ge, T.Real.ge)) ast args
)
| _ -> `Not_found
end
end
module Smtlib2 = struct
type arith =
| Regular
| Linear of [ `Large | `Strict ]
| Difference of [ `IDL | `RDL | `UFIDL ]
type view =
| Numeral of string
| Decimal of string
| Negation of Term.t
| Addition of Term.t list
| Subtraction of Term.t list
| Division of Term.t * Term.t
| Complex_arith
| Variable of Id.t
| Constant of Id.t
| Top_symbol_not_in_arith
type filter_res =
| Ok
| Warn of string
| Error of string
module View(Type : Tff_intf.S) = struct
type t = view
let print fmt (t : t) =
match t with
| Numeral s -> Format.fprintf fmt "the numeral '%s'" s
| Decimal s -> Format.fprintf fmt "the decimal '%s'" s
| Negation _ -> Format.fprintf fmt "a negation"
| Addition _ -> Format.fprintf fmt "an addition"
| Subtraction _ -> Format.fprintf fmt "a subtraction"
| Division _ -> Format.fprintf fmt "a division"
| Complex_arith -> Format.fprintf fmt "a complex arithmetic expression"
| Variable v -> Format.fprintf fmt "the quantified variable %a" Id.print v
| Constant c -> Format.fprintf fmt "the constant symbol %a" Id.print c
| Top_symbol_not_in_arith ->
Format.fprintf fmt "an arbitrary (not arithmetic) expression"
let expect_error v v' expected =
Format.asprintf "expects %s but was given:\n- %a\n- %a"
expected print v print v'
let rec view ~parse version env (t : Term.t) =
match t.term with
| Symbol { ns = Value Integer; name = Simple name } -> Numeral name
| Symbol { ns = Value Real; name = Simple name } -> Decimal name
| App ({ term = Symbol { Id.ns = Term; name = Simple "-"; }; _ }, [e])
-> Negation e
| App ({ term = Symbol { Id.ns = Term; name = Simple "+"; }; _ }, ((_ :: _) as args))
-> Addition args
| App ({ term = Symbol { Id.ns = Term; name = Simple "-"; }; _ }, ((_ :: _) as args))
-> Subtraction args
| App ({ term = Symbol { Id.ns = Term; name = Simple "/"; }; _ }, [a; b])
-> Division (a, b)
| Symbol id -> view_id ~parse version env id []
| App ({ term = Symbol id; _}, args) -> view_id ~parse version env id args
| Builtin b | App ({ term = Builtin b; _ }, _) ->
begin match parse version env (Type.Builtin b) with
| #Type.builtin_res -> Complex_arith
| #Type.not_found -> Top_symbol_not_in_arith
end
| _ -> Top_symbol_not_in_arith
and view_id ~parse version env id args =
match Type.find_var env id with
| `Letin (env, e, _, _) -> view ~parse version env e
| #Type.var ->
begin match args with
| [] -> Variable id
| _ -> Top_symbol_not_in_arith
end
| #Type.not_found ->
begin match Type.find_global env id with
| #Type.cst ->
begin match args with
| [] -> Constant id
| _ -> Top_symbol_not_in_arith
end
| #Type.not_found ->
begin match parse version env (Type.Id id) with
| #Type.builtin_res -> Complex_arith
| #Type.not_found -> Top_symbol_not_in_arith
end
end
let rec difference_count view t :
[ `Ok of Id.t * int | `Error of string ] =
match (view t : view) with
| Variable v -> `Ok (v, 1)
| Constant c -> `Ok (c, 1)
| Addition l -> difference_count_list view l
| v -> `Error (
Format.asprintf "addition in real difference logic expects \
either variables/constants or an addition of \
variables/constants, but was here given %a" print v)
and difference_count_list view = function
| h :: r ->
begin match difference_count view h with
| (`Error _) as res -> res
| `Ok (symb, n) ->
difference_count_list_aux view n symb r
end
| [] -> assert false
and difference_count_list_aux view n s = function
| [] -> `Ok (s, n)
| t :: r ->
begin match difference_count view t with
| (`Error _) as res -> res
| `Ok (s', n') ->
if Id.equal s s' then
difference_count_list_aux view (n + n') s r
else
`Error (
Format.asprintf "addition in real difference logic expects
n-th times the same variable/constant, but was
here applied to %a and %a which are different"
Id.print s Id.print s')
end
end
module Classification = struct
type t =
| Int_coefficient
| Rat_coefficient
| Complex_arith
| Variable_or_constant
| Top_symbol_not_in_arith
let print fmt (t: t) =
match t with
| Int_coefficient -> Format.fprintf fmt "an integer coefficient"
| Rat_coefficient -> Format.fprintf fmt "a rational coefficient"
| Complex_arith -> Format.fprintf fmt "a complex arithmetic expression"
| Variable_or_constant -> Format.fprintf fmt "a symbol (or quantified variable)"
| Top_symbol_not_in_arith ->
Format.fprintf fmt "an arbitrary expression with top symbol not in Arithmetic"
let expect_error c c' expected =
Format.asprintf "expects %s but was given:\n- %a\n- %a"
expected print c print c'
let rec classify view t =
match (view t: view) with
| Numeral _ -> Int_coefficient
| Decimal _ -> Rat_coefficient
| Addition _ -> Complex_arith
| Subtraction _ -> Complex_arith
| Negation t' ->
begin match (view t': view) with
| Numeral _ -> Int_coefficient
| Decimal _ -> Rat_coefficient
| _ -> Complex_arith
end
| Division (numerator, denominator) ->
begin match (classify view numerator, view denominator) with
| Int_coefficient, Numeral s when s <> "0"
-> Rat_coefficient
| _ -> Complex_arith
end
| Complex_arith -> Complex_arith
| Variable _ | Constant _ -> Variable_or_constant
| Top_symbol_not_in_arith -> Top_symbol_not_in_arith
end
module Filter(Type : Tff_intf.S) = struct
module V = View(Type)
let bad_arity operator logic n =
Error (Format.asprintf
"%s in %s must have exactly %d arguments" operator logic n)
let forbidden operator logic =
Error (Format.asprintf "%s is not allowed in %s" operator logic)
let minus_dl parse version env = function
| [ a ] ->
begin match V.view ~parse version env a with
| Numeral _
| Decimal _ -> Ok
| v ->
Error (Format.asprintf
"unary subtraction in difference logic expects \
a literal, but was given %a"
V.print v)
end
| _ -> bad_arity "unary subtraction" "difference logic" 1
let sub_idl parse version env = function
| [ a; b ] ->
begin match Classification.classify (V.view ~parse version env) a,
Classification.classify (V.view ~parse version env) b with
| Variable_or_constant, Variable_or_constant -> Ok
| v, v' ->
Error (Format.asprintf "subtraction in difference logic %s"
(Classification.expect_error v v' "two constants/variables"))
end
| _ -> bad_arity "subtraction" "integer difference logic" 2
let sub_rdl parse version env = function
| [ a; b ] ->
begin match V.difference_count (V.view ~parse version env) a,
V.difference_count (V.view ~parse version env) b with
| `Error msg, _
| _, `Error msg
-> Error msg
| `Ok (_, n), `Ok (_, n') ->
if n = n' then Ok
else Error (
Format.asprintf "subtraction in real difference logic \
expects both sides to be sums of the same \
length, but here the sums have lengths \
%d and %d" n n')
end
| _ -> bad_arity "subtraction" "real difference logic" 2
let op_ufidl parse version env args =
let rec aux non_numeral_seen = function
| [] -> Ok
| t :: r ->
begin match V.view ~parse version env t with
| Numeral _ -> aux non_numeral_seen r
| _ ->
if non_numeral_seen then
Error (Format.asprintf "subtraction in difference logic (QF_UFIDL version) \
expects all but at most one of its arguments to be \
numerals")
else
aux true r
end
in
aux false args
let add_rdl parse version env args =
match V.difference_count_list (V.view ~parse version env) args with
| `Ok _ -> Ok
| `Error msg -> Error msg
let div_linear parse version env = function
| [ a; b ] ->
begin match Classification.classify (V.view ~parse version env) a with
| Int_coefficient ->
begin match V.view ~parse version env b with
| Numeral "0" ->
Error (Format.asprintf "division in linear arithmetic \
expects a non-zero denominator")
| Numeral _ -> Ok
| v ->
Error (Format.asprintf "division in linear arithmetic \
expects a constant positive \
integer literal as denominator, \
but was given %a"
V.print v)
end
| _ ->
let v = V.view ~parse version env a in
let msg =
Format.asprintf "division in linear arithmetic \
expects as first argument an integer \
coeficient, i.e. either an integer \
numeral or the negation of one, but \
here was given %a" V.print v
in
begin match v with
| Decimal s when Misc.Strings.is_suffix s ~suffix:".0" -> Warn msg
| _ -> Error msg
end
end
| _ -> bad_arity "division" "linear arithmetic" 2
let mul_linear_msg ~strict c c' =
Format.asprintf "multiplication in %slinear arithmetic %s"
(if strict then "strict " else "")
(Classification.expect_error c c'
(if strict
then "an integer or rational literal and a symbol (variable or constant)"
else "an integer or rational literal and a non-arithmetic expression"))
let mul_linear ~strict parse version env = function
| [ a; b ] ->
begin match Classification.classify (V.view ~parse version env) a,
Classification.classify (V.view ~parse version env) b with
| (Int_coefficient | Rat_coefficient), Variable_or_constant
| Variable_or_constant, (Int_coefficient | Rat_coefficient)
-> Ok
| ((Int_coefficient | Rat_coefficient) as c), (Top_symbol_not_in_arith as c')
| (Top_symbol_not_in_arith as c), ((Int_coefficient | Rat_coefficient) as c')
->
if strict then Warn (mul_linear_msg ~strict c c') else Ok
| ((Int_coefficient | Rat_coefficient) as c),
((Int_coefficient | Rat_coefficient | Complex_arith) as c')
| (Complex_arith as c'), ((Int_coefficient | Rat_coefficient) as c) ->
Warn (mul_linear_msg ~strict c c')
| c, c' ->
Error (mul_linear_msg ~strict c c')
end
| _ -> bad_arity "multiplication" "linear arithmetic" 2
let comp_idl parse version env = function
| [ a; b ] ->
begin match V.view ~parse version env a,
V.view ~parse version env b with
| (Variable _ | Constant _),
(Variable _ | Constant _) -> Ok
| Subtraction _, Numeral _ -> Ok
| Subtraction _, Negation _ -> Ok
| v, v' ->
Error (Format.asprintf "comparison in integer difference logic %s"
(V.expect_error v v'
"a substraction on the left and a (possibly negated) \
integer literal on the right"))
end
| _ -> bad_arity "comparison" "integer difference logic" 2
let comp_rdl parse version env = function
| [ a; b ] ->
begin match V.view ~parse version env a,
V.view ~parse version env b with
| (Variable _ | Constant _),
(Variable _ | Constant _) -> Ok
| Subtraction _, Numeral _ -> Ok
| Subtraction _, Negation _ -> Ok
| Subtraction [x; y], Division _ ->
begin match V.difference_count (V.view ~parse version env) x,
V.difference_count (V.view ~parse version env) y with
| `Error msg, _
| _, `Error msg -> Error msg
| `Ok (_, n), `Ok (_, n') ->
if n = 1 && n' = 1 then Ok
else
Error (
Format.asprintf "in real difference logic, when comparing \
the result of a subtraction with a rational \
number, each side of the subtraction can only \
contain a single variable/constant, but here there
was %d" n)
end
| v, v' ->
Error (
Format.asprintf "comparison in difference logic %s"
(V.expect_error v v'
"a subtraction on the left and a (possibly negated) \
integer literal on the right"))
end
| _ -> bad_arity "comparison" "real difference logic" 2
let minus arith parse version env args =
match arith with
| Regular -> Ok
| Linear _ -> Ok
| Difference (`IDL | `RDL) ->
minus_dl parse version env args
| Difference `UFIDL ->
forbidden "unary subtraction" "difference logic (QF_UFIDL variant)"
let sub arith parse version env args =
match arith with
| Regular -> Ok
| Linear _ -> Ok
| Difference `IDL -> sub_idl parse version env args
| Difference `RDL -> sub_rdl parse version env args
| Difference `UFIDL -> op_ufidl parse version env args
let add arith parse version env args =
match arith with
| Regular -> Ok
| Linear _ -> Ok
| Difference `IDL -> forbidden "addition" "integer difference logic"
| Difference `RDL -> add_rdl parse version env args
| Difference `UFIDL -> op_ufidl parse version env args
let mul arith parse version env args =
match arith with
| Regular -> Ok
| Linear `Strict -> mul_linear ~strict:true parse version env args
| Linear `Large -> mul_linear ~strict:false parse version env args
| Difference `IDL -> forbidden "multiplication" "integer difference logic"
| Difference `RDL -> forbidden "multiplication" "real difference logic"
| Difference `UFIDL -> forbidden "multiplication" "difference logic (QF_UFIDL variant)"
let div arith parse version env args =
match arith with
| Regular -> Ok
| Linear _ -> div_linear parse version env args
| Difference `IDL -> forbidden "division" "integer difference logic"
| Difference `RDL -> div_linear parse version env args
| Difference `UFIDL -> forbidden "division" "difference logic (QF_UFIDL variant)"
let ediv arith _args =
match arith with
| Regular -> Ok
| Linear _ -> forbidden "euclidean division" "linear arithmetic"
| Difference _ -> forbidden "euclidean division" "difference logic"
let mod_ arith _args =
match arith with
| Regular -> Ok
| Linear _ -> forbidden "mod" "linear arithmetic"
| Difference _ -> forbidden "mod" "difference logic"
let abs arith _args =
match arith with
| Regular -> Ok
| Linear _ -> forbidden "abs" "linear arithmetic"
| Difference _ -> forbidden "abs" "difference logic"
let comp arith parse version env args =
match arith with
| Regular -> Ok
| Linear _ -> Ok
| Difference `IDL -> comp_idl parse version env args
| Difference `RDL -> comp_rdl parse version env args
| Difference `UFIDL -> Ok
let divisible arith _args =
match arith with
| Regular -> Ok
| Linear _ -> forbidden "divisible" "linear arithmetic"
| Difference _ -> forbidden "divisible" "difference logic"
end
module Int = struct
module Tff
(Type : Tff_intf.S)
(Ty : Dolmen.Intf.Ty.Smtlib_Int with type t := Type.Ty.t)
(T : Dolmen.Intf.Term.Smtlib_Int with type t := Type.T.t) = struct
module F = Filter(Type)
type _ Type.warn +=
| Restriction : string -> Term.t Type.warn
type _ Type.err +=
| Forbidden : string -> Term.t Type.err
let check env filter ast args =
match filter args with
| Ok -> ()
| Warn msg -> Type._warn env (Ast ast) (Restriction msg)
| Error msg -> Type._error env (Ast ast) (Forbidden msg)
let check1 env filter ast a = check env filter ast [a]
let check2 env filter ast a b = check env filter ast [a; b]
let rec parse ~arith version env s =
match s with
| Type.Id { Id.ns = Sort; name = Simple "Int"; } ->
`Ty (Base.app0 (module Type) env s Ty.int)
| Type.Id { Id.ns = Value Integer; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.mk name))
| Type.Id { Id.ns = Term; name = Simple name; } ->
begin match name with
| "-" ->
`Term (fun ast args -> match args with
| [x] ->
check env (F.minus arith (parse ~arith) version env) ast args;
T.minus (Type.parse_term env x)
| _ ->
Base.term_app_left (module Type) env s T.sub ast args
~check:(check env (F.sub arith (parse ~arith) version env))
)
| "+" ->
`Term (Base.term_app_left (module Type) env s T.add
~check:(check env (F.add arith (parse ~arith) version env)))
| "*" ->
`Term (Base.term_app_left (module Type) env s T.mul
~check:(check env (F.mul arith (parse ~arith) version env)))
| "div" ->
`Term (Base.term_app_left (module Type) env s T.div
~check:(check env (F.ediv arith)))
| "mod" ->
`Term (Base.term_app2 (module Type) env s T.rem
~check:(check2 env (F.mod_ arith)))
| "abs" ->
`Term (Base.term_app1 (module Type) env s T.abs
~check:(check1 env (F.abs arith)))
| "<=" ->
`Term (Base.term_app_chain (module Type) env s T.le
~check:(check env (F.comp arith (parse ~arith) version env)))
| "<" ->
`Term (Base.term_app_chain (module Type) env s T.lt
~check:(check env (F.comp arith (parse ~arith) version env)))
| ">=" ->
`Term (Base.term_app_chain (module Type) env s T.ge
~check:(check env (F.comp arith (parse ~arith) version env)))
| ">" ->
`Term (Base.term_app_chain (module Type) env s T.gt
~check:(check env (F.comp arith (parse ~arith) version env)))
| _ -> `Not_found
end
| Type.Id { Id.ns = Term; name = Indexed { basename; indexes; }; } ->
Base.parse_indexed basename indexes
~k:(function () -> `Not_found)
~err:(Base.bad_ty_index_arity (module Type) env) (function
| "divisible" ->
`Unary (function n ->
`Term (Base.term_app1 (module Type) env s (T.divisible n)
~check:(check1 env (F.divisible arith)))
)
| _ -> `Not_indexed
)
| _ -> `Not_found
end
end
module Real = struct
module Tff
(Type : Tff_intf.S)
(Ty : Dolmen.Intf.Ty.Smtlib_Real with type t := Type.Ty.t)
(T : Dolmen.Intf.Term.Smtlib_Real with type t := Type.T.t) = struct
module F = Filter(Type)
type _ Type.warn +=
| Restriction : string -> Term.t Type.warn
type _ Type.err +=
| Forbidden : string -> Term.t Type.err
let check env filter ast args =
match filter args with
| Ok -> ()
| Warn msg -> Type._warn env (Ast ast) (Restriction msg)
| Error msg -> Type._error env (Ast ast) (Forbidden msg)
let rec parse ~arith version env s =
match s with
| Type.Id { Id.ns = Sort; name = Simple "Real"; } ->
`Ty (Base.app0 (module Type) env s Ty.real)
| Type.Id { Id.ns = Value (Integer | Real); name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.mk name))
| Type.Id { Id.ns = Term; name = Simple name; } ->
begin match name with
| "-" -> `Term (fun ast args ->
match args with
| [x] ->
check env (F.minus arith (parse ~arith) version env) ast args;
T.minus (Type.parse_term env x)
| _ ->
Base.term_app_left (module Type) env s T.sub ast args
~check:(check env (F.sub arith (parse ~arith) version env))
)
| "+" ->
`Term (Base.term_app_left (module Type) env s T.add
~check:(check env (F.add arith (parse ~arith) version env)))
| "*" ->
`Term (Base.term_app_left (module Type) env s T.mul
~check:(check env (F.mul arith (parse ~arith) version env)))
| "/" ->
`Term (Base.term_app_left (module Type) env s T.div
~check:(check env (F.div arith (parse ~arith) version env)))
| "<=" ->
`Term (Base.term_app_chain (module Type) env s T.le
~check:(check env (F.comp arith (parse ~arith) version env)))
| "<" ->
`Term (Base.term_app_chain (module Type) env s T.lt
~check:(check env (F.comp arith (parse ~arith) version env)))
| ">=" ->
`Term (Base.term_app_chain (module Type) env s T.ge
~check:(check env (F.comp arith (parse ~arith) version env)))
| ">" ->
`Term (Base.term_app_chain (module Type) env s T.gt
~check:(check env (F.comp arith (parse ~arith) version env)))
| _ -> `Not_found
end
| _ -> `Not_found
end
end
module Real_Int = struct
module Tff
(Type : Tff_intf.S)
(Ty : Dolmen.Intf.Ty.Smtlib_Real_Int with type t := Type.Ty.t)
(T : Dolmen.Intf.Term.Smtlib_Real_Int with type t := Type.T.t
and type ty := Type.Ty.t) = struct
module F = Filter(Type)
type _ Type.warn +=
| Restriction : string -> Term.t Type.warn
type _ Type.err +=
| Forbidden : string -> Term.t Type.err
| Expected_arith_type : Type.Ty.t -> Term.t Type.err
let check env filter ast args =
match filter args with
| Ok -> ()
| Warn msg -> Type._warn env (Ast ast) (Restriction msg)
| Error msg -> Type._error env (Ast ast) (Forbidden msg)
let check1 env filter ast a = check env filter ast [a]
let check2 env filter ast a b = check env filter ast [a; b]
let dispatch1 env (mk_int, mk_real) ast t =
match Ty.view @@ T.ty t with
| `Int -> mk_int t
| `Real -> mk_real t
| _ -> Type._error env (Ast ast) (Expected_arith_type (T.ty t))
let dispatch2 env (mk_int, mk_real) ast a b =
match Ty.view @@ T.ty a, Ty.view @@ T.ty b with
| `Int, `Int -> mk_int a b
| `Int, `Real -> mk_real (T.Int.to_real a) b
| `Real, `Int -> mk_real a (T.Int.to_real b)
| `Real, `Real -> mk_real a b
| (`Int | `Real), _ -> Type._error env (Ast ast) (Expected_arith_type (T.ty b))
| _, (`Int | `Real) -> Type._error env (Ast ast) (Expected_arith_type (T.ty a))
| _, _ -> Type._error env (Ast ast) (Expected_arith_type (T.ty a))
let promote_int_to_real _env mk_real _ast a b =
match Ty.view @@ T.ty a, Ty.view @@ T.ty b with
| `Int, `Int -> mk_real (T.Int.to_real a) (T.Int.to_real b)
| _ -> mk_real a b
let rec parse ~arith version env s =
match s with
| Type.Id { Id.ns = Sort; name = Simple "Int"; } ->
`Ty (Base.app0 (module Type) env s Ty.int)
| Type.Id { Id.ns = Sort; name = Simple "Real"; } ->
`Ty (Base.app0 (module Type) env s Ty.real)
| Type.Id { Id.ns = Value Integer; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.Int.mk name))
| Type.Id { Id.ns = Value Real; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.Real.mk name))
| Type.Id { Id.ns = Term; name = Simple name; } ->
begin match name with
| "-" -> `Term (fun ast args ->
match args with
| [_] ->
Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.minus, T.Real.minus)) ast args
~check:(check1 env (F.minus arith (parse ~arith) version env))
| _ ->
Base.term_app_left_ast (module Type) env s
(dispatch2 env (T.Int.sub, T.Real.sub)) ast args
~check:(check env (F.sub arith (parse ~arith) version env))
)
| "+" ->
`Term (Base.term_app_left_ast (module Type) env s
(dispatch2 env (T.Int.add, T.Real.add))
~check:(check env (F.add arith (parse ~arith) version env)))
| "*" ->
`Term (Base.term_app_left_ast (module Type) env s
(dispatch2 env (T.Int.mul, T.Real.mul))
~check:(check env (F.mul arith (parse ~arith) version env)))
| "div" ->
`Term (Base.term_app_left (module Type) env s T.Int.div
~check:(check env (F.ediv arith)))
| "mod" ->
`Term (Base.term_app2 (module Type) env s T.Int.rem
~check:(check2 env (F.mod_ arith)))
| "abs" ->
`Term (Base.term_app1 (module Type) env s T.Int.abs
~check:(check1 env (F.abs arith)))
| "/" ->
`Term (Base.term_app_left_ast (module Type) env s
(promote_int_to_real env T.Real.div)
~check:(check env (F.div arith (parse ~arith) version env)))
| "<=" ->
`Term (Base.term_app_chain_ast (module Type) env s
(dispatch2 env (T.Int.le, T.Real.le))
~check:(check env (F.comp arith (parse ~arith) version env)))
| "<" ->
`Term (Base.term_app_chain_ast (module Type) env s
(dispatch2 env (T.Int.lt, T.Real.lt))
~check:(check env (F.comp arith (parse ~arith) version env)))
| ">=" ->
`Term (Base.term_app_chain_ast (module Type) env s
(dispatch2 env (T.Int.ge, T.Real.ge))
~check:(check env (F.comp arith (parse ~arith) version env)))
| ">" ->
`Term (Base.term_app_chain_ast (module Type) env s
(dispatch2 env (T.Int.gt, T.Real.gt))
~check:(check env (F.comp arith (parse ~arith) version env)))
| "to_real" ->
`Term (Base.term_app1 (module Type) env s T.Int.to_real)
| "to_int" ->
`Term (Base.term_app1 (module Type) env s T.Real.floor_to_int)
| "is_int" ->
`Term (Base.term_app1 (module Type) env s T.Real.is_int)
| _ -> `Not_found
end
| Type.Id { Id.ns = Term; name = Indexed { basename; indexes; }; } ->
Base.parse_indexed basename indexes
~k:(function () -> `Not_found)
~err:(Base.bad_ty_index_arity (module Type) env) (function
| "divisible" ->
`Unary (function n ->
`Term (Base.term_app1 (module Type) env s (T.Int.divisible n)
~check:(check1 env (F.divisible arith))))
| _ -> `Not_indexed
)
| _ -> `Not_found
end
end
end
module Tptp = struct
module Tff
(Type : Tff_intf.S)
(Ty : Dolmen.Intf.Ty.Tptp_Arith with type t := Type.Ty.t)
(T : Dolmen.Intf.Term.Tptp_Tff_Arith with type t := Type.T.t
and type ty := Type.Ty.t) = struct
type _ Type.err +=
| Expected_arith_type : Type.Ty.t -> Term.t Type.err
| Cannot_apply_to : Type.Ty.t -> Term.t Type.err
let _invalid env ast ty _ =
Type._error env (Ast ast) (Cannot_apply_to ty)
let dispatch1 env (mk_int, mk_rat, mk_real) ast t =
let ty = T.ty t in
if Ty.(equal int) ty then mk_int t
else if Ty.(equal rat) ty then mk_rat t
else if Ty.(equal real) ty then mk_real t
else begin
Type._error env (Ast ast) (Expected_arith_type ty)
end
let dispatch2 env (mk_int, mk_rat, mk_real) ast a b =
let ty = T.ty a in
if Ty.(equal int) ty then mk_int a b
else if Ty.(equal rat) ty then mk_rat a b
else if Ty.(equal real) ty then mk_real a b
else begin
Type._error env (Ast ast) (Expected_arith_type ty)
end
let parse _version env s =
match s with
| Type.Id { Id.ns = Term; name = Simple "$int"; } ->
`Ty (Base.app0 (module Type) env s Ty.int)
| Type.Id { Id.ns = Term; name = Simple "$rat"; } ->
`Ty (Base.app0 (module Type) env s Ty.rat)
| Type.Id { Id.ns = Term; name = Simple "$real"; } ->
`Ty (Base.app0 (module Type) env s Ty.real)
| Type.Id { Id.ns = Value Integer; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.int name))
| Type.Id { Id.ns = Value Rational; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.rat name))
| Type.Id { Id.ns = Value Real; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.real name))
| Type.Id { Id.ns = Term; name = Simple "$less"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.lt, T.Rat.lt, T.Real.lt)))
| Type.Id { Id.ns = Term; name = Simple "$lesseq"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.le, T.Rat.le, T.Real.le)))
| Type.Id { Id.ns = Term; name = Simple "$greater"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.gt, T.Rat.gt, T.Real.gt)))
| Type.Id { Id.ns = Term; name = Simple "$greatereq"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.ge, T.Rat.ge, T.Real.ge)))
| Type.Id { Id.ns = Term; name = Simple "$uminus"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.minus, T.Rat.minus, T.Real.minus)))
| Type.Id { Id.ns = Term; name = Simple "$sum"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.add, T.Rat.add, T.Real.add)))
| Type.Id { Id.ns = Term; name = Simple "$difference"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.sub, T.Rat.sub, T.Real.sub)))
| Type.Id { Id.ns = Term; name = Simple "$product"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.mul, T.Rat.mul, T.Real.mul)))
| Type.Id { Id.ns = Term; name = Simple "$quotient"; } ->
`Term (Base.term_app2_ast (module Type) env s (fun ast a b ->
(dispatch2 env (_invalid env ast Ty.int, T.Rat.div, T.Real.div)) ast a b
))
| Type.Id { Id.ns = Term; name = Simple "$quotient_e"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.div_e, T.Rat.div_e, T.Real.div_e)))
| Type.Id { Id.ns = Term; name = Simple "$remainder_e"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.rem_e, T.Rat.rem_e, T.Real.rem_e)))
| Type.Id { Id.ns = Term; name = Simple "$quotient_t"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.div_t, T.Rat.div_t, T.Real.div_t)))
| Type.Id { Id.ns = Term; name = Simple "$remainder_t"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.rem_t, T.Rat.rem_t, T.Real.rem_t)))
| Type.Id { Id.ns = Term; name = Simple "$quotient_f"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.div_f, T.Rat.div_f, T.Real.div_f)))
| Type.Id { Id.ns = Term; name = Simple "$remainder_f"; } ->
`Term (Base.term_app2_ast (module Type) env s
(dispatch2 env (T.Int.rem_f, T.Rat.rem_f, T.Real.rem_f)))
| Type.Id { Id.ns = Term; name = Simple "$floor"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.floor, T.Rat.floor, T.Real.floor)))
| Type.Id { Id.ns = Term; name = Simple "$ceiling"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.ceiling, T.Rat.ceiling, T.Real.ceiling)))
| Type.Id { Id.ns = Term; name = Simple "$truncate"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.truncate, T.Rat.truncate, T.Real.truncate)))
| Type.Id { Id.ns = Term; name = Simple "$round"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.round, T.Rat.round, T.Real.round)))
| Type.Id { Id.ns = Term; name = Simple "$is_int"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.is_int, T.Rat.is_int, T.Real.is_int)))
| Type.Id { Id.ns = Term; name = Simple "$is_rat"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.is_rat, T.Rat.is_rat, T.Real.is_rat)))
| Type.Id { Id.ns = Term; name = Simple "$to_int"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.to_int, T.Rat.to_int, T.Real.to_int)))
| Type.Id { Id.ns = Term; name = Simple "$to_rat"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.to_rat, T.Rat.to_rat, T.Real.to_rat)))
| Type.Id { Id.ns = Term; name = Simple "$to_real"; } ->
`Term (Base.term_app1_ast (module Type) env s
(dispatch1 env (T.Int.to_real, T.Rat.to_real, T.Real.to_real)))
| _ -> `Not_found
end
end
module Zf = struct
module Thf
(Type : Thf_intf.S)
(Ty : Dolmen.Intf.Ty.Zf_Arith with type t := Type.Ty.t)
(T : Dolmen.Intf.Term.Zf_Arith with type t := Type.T.t) = struct
let parse env s =
match s with
| Type.Builtin Term.Int ->
`Ty (Base.app0 (module Type) env s Ty.int)
| Type.Id { Id.ns = Value Integer; name = Simple name; } ->
`Term (Base.app0 (module Type) env s (T.int name))
| Type.Builtin Term.Minus ->
`Term (Base.term_app1 (module Type) env s T.Int.minus)
| Type.Builtin Term.Add ->
`Term (Base.term_app2 (module Type) env s T.Int.add)
| Type.Builtin Term.Sub ->
`Term (Base.term_app2 (module Type) env s T.Int.sub)
| Type.Builtin Term.Mult ->
`Term (Base.term_app2 (module Type) env s T.Int.mul)
| Type.Builtin Term.Lt ->
`Term (Base.term_app2 (module Type) env s T.Int.lt)
| Type.Builtin Term.Leq ->
`Term (Base.term_app2 (module Type) env s T.Int.le)
| Type.Builtin Term.Gt ->
`Term (Base.term_app2 (module Type) env s T.Int.gt)
| Type.Builtin Term.Geq ->
`Term (Base.term_app2 (module Type) env s T.Int.ge)
| _ -> `Not_found
end
end