package psmt2-frontend

  1. Overview
  2. Docs

Source file smtlib_typed_logic.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
open Smtlib_typed_env
open Smtlib_ty
open Options

type th_def = {
  sorts :
    (string *
     ((int * int) *
      (string -> (ty list * int list)  -> desc))) list;
  funs :
    (string * fun_def) list;
  par_funs :
    (string *
     ((string list) -> fun_def)) list;
}

type theory =
  | Core
  | Ints
  | Reals
  | Reals_Ints
  | FloatingPoint
  | Arrays
  | BitVectors

let new_fun params return assoc =
  {params =
     Smtlib_ty.new_type (Smtlib_ty.TFun (params,return));
   assoc}

let core = {
  sorts = [ "Bool",((0,0),(fun _s (l1,l2) ->
      assert (l1 == [] && l2 == []); TBool))];
  funs = [
    "true", new_fun [] (new_type TBool) None;
    "false", new_fun [] (new_type TBool) None;
    "not", new_fun
      [(new_type TBool)]
      (new_type TBool) None;
    "=>", new_fun
      [(new_type TBool);
       (new_type TBool)]
      (new_type TBool) (Some Right);
    "and", new_fun
      [(new_type TBool);
       (new_type TBool)]
      (new_type TBool) (Some Left);
    "or", new_fun
      [(new_type TBool);
       (new_type TBool)]
      (new_type TBool) (Some Left);
    "xor", new_fun
      [(new_type TBool);
       (new_type TBool)]
      (new_type TBool) (Some Left);
    (let a = new_type(TVar("A")) in
     "=", new_fun [a;a]
       (new_type TBool) (Some Chainable));
    (let a = new_type(TVar("A")) in
     "distinct", new_fun [a;a]
       (new_type TBool) (Some Pairwise));
    (let a = new_type(TVar("A")) in
     "ite", new_fun
       [(new_type TBool); a; a] a None);
  ];
  par_funs = []
}

let ints = {
  sorts = ["Int",((0,0),(fun _s (l1,l2) ->
      assert (l1 == [] && l2 == []); TInt))];
  funs = [
    "-", new_fun
      [(new_type TInt)]
      (new_type TInt) None;
    "-", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TInt) (Some Left);
    "+", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TInt) (Some Left);
    "*", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TInt) (Some Left);
    "div", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TInt) (Some Left);
    "mod", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TInt) None;
    "abs", new_fun
      [(new_type TInt)]
      (new_type TInt) None;
    "<=", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TBool) (Some Chainable);
    "<", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TBool) (Some Chainable);
    ">=", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TBool) (Some Chainable);
    ">", new_fun
      [(new_type TInt);
       (new_type TInt)]
      (new_type TBool) (Some Chainable);
  ];
  par_funs = []

}

let reals = {
  sorts = ["Real",((0,0),(fun _s (l1,l2) ->
      assert (l1 == [] && l2 == []); TReal))];
  funs = [
    "-", new_fun
      [(new_type TReal)]
      (new_type TReal) None;
    "-", new_fun
      [(new_type TReal);
       (new_type TReal)]
      (new_type TReal) (Some Left);
    "+", new_fun
      [(new_type TReal);
       (new_type TReal)]
      (new_type TReal) (Some Left);
    "*", new_fun
      [(new_type TReal);
       (new_type TReal)]
      (new_type TReal) (Some Left);
    "/", new_fun
      [(new_type TReal);
       (new_type TReal)]
      (new_type TReal) (Some Left);
    "<=", new_fun
      [(new_type TReal);
       (new_type TReal)]
      (new_type TBool) (Some Chainable);
    "<", new_fun
      [(new_type TReal);
       (new_type TReal)]
      (new_type TBool) (Some Chainable);
    ">=", new_fun
      [(new_type TReal);
       (new_type TReal)]
      (new_type TBool) (Some Chainable);
    ">", new_fun
      [(new_type TReal);
       (new_type TReal)]
      (new_type TBool) (Some Chainable);
  ];
  par_funs = []
}
let reals_ints = {
  sorts = List.rev_append ints.sorts reals.sorts;
  funs = List.rev_append (List.rev_append ints.funs reals.funs) [
      "to_real", new_fun
        [(new_type TInt)]
        (new_type TReal) None;
      "to_int", new_fun
        [(new_type TReal)]
        (new_type TInt) None;
      "is_int", new_fun
        [(new_type TReal)]
        (new_type TBool) None;
    ];
  par_funs = []
}

let arrays =
  {
    sorts = ["Array",((2,0),
                      (fun _s (l1,l2) ->
                         let t1,t2 = List.hd l1, List.hd (List.tl l1) in
                         assert (List.length l1 = 2 && l2 == []);
                         TArray (t1,t2)))];
    funs = [
      (let x = new_type(TVar("X")) in
       let y = new_type(TVar("Y")) in
       "select", new_fun
         [new_type (TArray (x,y));x] y None);
      (let x = new_type(TVar("X")) in
       let y = new_type(TVar("Y")) in
       "store", new_fun
         [new_type (TArray (x,y));x;y]
         (new_type (TArray (x,y))) None);
    ];
    par_funs = []
  }


let floating_point = {
  sorts = [
    "RoundingMode",((0,0), (fun _s (l1,l2) ->
        assert (l1 == [] && l2 == []); TRoundingMode));
    "FloatingPoint",((0,2),
                     (fun _s (l1,l2) ->
                        match l1,l2 with
                        | [], [n1;n2] -> TFloatingPoint(n1,n2)
                        | _, _ -> assert false
                     ));
    "Float16",((0,0), (fun _s (l1,l2) ->
        assert (l1 == [] && l2 == []); TFloatingPoint(5,11)));
    "Float32",((0,0), (fun _s (l1,l2) ->
        assert (l1 == [] && l2 == []); TFloatingPoint(8,24)));
    "Float64",((0,0), (fun _s (l1,l2) ->
        assert (l1 == [] && l2 == []); TFloatingPoint(11,53)));
    "Float128",((0,0), (fun _s (l1,l2) ->
        assert (l1 == [] && l2 == []); TFloatingPoint(15,113)));

  ];
  funs = [
    "roundNearestTiesToEven", new_fun
      [] (new_type TRoundingMode) None;
    "RNE", new_fun
      [] (new_type TRoundingMode) None;
    "roundNearestTiesToAway", new_fun
      [] (new_type TRoundingMode) None;
    "RNA", new_fun
      [] (new_type TRoundingMode) None;
    "roundTowardPositive", new_fun
      [] (new_type TRoundingMode) None;
    "RTP", new_fun
      [] (new_type TRoundingMode) None;
    "roundTowardNegative", new_fun
      [] (new_type TRoundingMode) None;
    "RTN", new_fun
      [] (new_type TRoundingMode) None;
    "roundTowardZero", new_fun
      [] (new_type TRoundingMode) None;
    "RTZ", new_fun
      [] (new_type TRoundingMode) None;

    "fp", new_fun
      [(new_type (TBitVec 0));
       (new_type (TBitVec 0));
       (new_type (TBitVec 0))]
      (new_type (TFloatingPoint (0,0))) None;

    "fp", new_fun
      [(new_type TInt);
       (new_type TInt);
       (new_type TInt)]
      (new_type (TFloatingPoint (0,0))) None;

    "fp.to_real", new_fun
      [new_type (TFloatingPoint (0,0))]
      (new_type TReal) None;

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.abs", new_fun
       [x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.neg", new_fun
       [x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.add", new_fun
       [(new_type TRoundingMode);x;x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.sub", new_fun
       [(new_type TRoundingMode);x;x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.mul", new_fun
       [(new_type TRoundingMode);x;x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.div", new_fun
       [(new_type TRoundingMode);x;x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.fma", new_fun
       [(new_type TRoundingMode);x;x;x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.sqrt", new_fun
       [(new_type TRoundingMode);x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.rem", new_fun
       [x;x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.roundToIntegral", new_fun
       [(new_type TRoundingMode);x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.min", new_fun
       [x;x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.max", new_fun
       [x;x] x None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.leq", new_fun
       [x;x]
       (new_type TBool) (Some Chainable));

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.lt", new_fun
       [x;x]
       (new_type TBool) (Some Chainable));

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.geq", new_fun
       [x;x]
       (new_type TBool) (Some Chainable));

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.gt", new_fun
       [x;x]
       (new_type TBool) (Some Chainable));

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.eq", new_fun
       [x;x]
       (new_type TBool) (Some Chainable));

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.isNormal", new_fun
       [x] (new_type TBool) None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.isSubnormal", new_fun
       [x] (new_type TBool) None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.isZero", new_fun
       [x] (new_type TBool) None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.isInfinite", new_fun
       [x] (new_type TBool) None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.isNaN", new_fun
       [x] (new_type TBool) None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.isNegative", new_fun
       [x] (new_type TBool) None);

    (let x = new_type (TFloatingPoint (0,0)) in
     "fp.isPositive", new_fun
       [x] (new_type TBool) None);
  ];
  par_funs = [

    ("to_fp", (fun l ->
         match l with
         | [a;b] ->
           let a = int_of_string a in
           let b = int_of_string b in
           new_fun [(new_type (TBitVec (a+b)))]
             (new_type (TFloatingPoint (a,b))) None;
         | _ -> assert false
       )
    );

    ("to_fp", (fun l ->
         match l with
         | [a;b] ->
           let a = int_of_string a in
           let b = int_of_string b in
           new_fun [(new_type TRoundingMode);
                    (new_type (TFloatingPoint (0,0)))]
             (new_type (TFloatingPoint (a,b))) None;
         | _ -> assert false
       )
    );

    ("to_fp", (fun l ->
         match l with
         | [a;b] ->
           let a = int_of_string a in
           let b = int_of_string b in
           new_fun [(new_type TRoundingMode);
                    (new_type TReal)]
             (new_type (TFloatingPoint (a,b))) None;
         | _ -> assert false
       )
    );

    ("to_fp", (fun l ->
         match l with
         | [a;b] ->
           let a = int_of_string a in
           let b = int_of_string b in
           new_fun [(new_type TRoundingMode);
                    (new_type (TBitVec 0))]
             (new_type (TFloatingPoint (a,b))) None;
         | _ -> assert false
       )
    );

    ("to_fp_unsigned", (fun l ->
         match l with
         | [a;b] ->
           let a = int_of_string a in
           let b = int_of_string b in
           new_fun [(new_type TRoundingMode);
                    (new_type (TBitVec 0))]
             (new_type (TFloatingPoint (a,b))) None;
         | _ -> assert false
       )
    );

    ("fp.to_ubv", (fun l ->
         match l with
         | [m] ->
           let m = int_of_string m in
           new_fun [(new_type TRoundingMode);
                    (new_type (TFloatingPoint (0,0)))]
             (new_type (TBitVec m))
             None;
         | _ -> assert false
       )
    );

    ("fp.to_sbv", (fun l ->
         match l with
         | [m] ->
           let m = int_of_string m in
           new_fun [(new_type TRoundingMode);
                    (new_type (TFloatingPoint (0,0)))]
             (new_type (TBitVec m))
             None;
         | _ -> assert false
       )
    );

    ("+oo", fun l ->
        match l with
        | [a;b] ->
          let a = int_of_string a in
          let b = int_of_string b in
          new_fun [] (new_type (TFloatingPoint (a,b))) None;
        | _ -> assert false
    );

    ("-oo", fun l ->
        match l with
        | [a;b] ->
          let a = int_of_string a in
          let b = int_of_string b in
          new_fun [] (new_type (TFloatingPoint (a,b))) None;
        | _ -> assert false
    );

    ("+zero", fun l ->
        match l with
        | [a;b] ->
          let a = int_of_string a in
          let b = int_of_string b in
          new_fun [] (new_type (TFloatingPoint (a,b))) None;
        | _ -> assert false
    );

    ("-zero", fun l ->
        match l with
        | [a;b] ->
          let a = int_of_string a in
          let b = int_of_string b in
          new_fun [] (new_type (TFloatingPoint (a,b))) None;
        | _ -> assert false
    );

    ("NaN", fun l ->
        match l with
        | [a;b] ->
          let a = int_of_string a in
          let b = int_of_string b in
          new_fun [] (new_type (TFloatingPoint (a,b))) None;
        | _ -> assert false
    );
  ] }

let bit_vectors = {
  sorts = ["BitVec",((0,1),
                     (fun _s (l1,l2) ->
                        assert (List.length l2 = 1 && l1 == []);
                        TBitVec(List.hd l2)
                     ))];
  funs = [];
  par_funs = []
}


let add_theories env ths =
  let aux env th =
    let th_def =
      match th with
      | Core -> core
      | Ints -> ints
      | Reals ->
        (*let sorts = if get_is_real () then
            ("Int",((0,0),(fun s (l1,l2) ->
                 assert (l1 == [] && l2 == []); TReal)))
            :: reals.sorts
          else reals.sorts
          in sorts*)
        reals
      | Reals_Ints -> reals_ints
      | FloatingPoint -> floating_point
      | Arrays -> arrays
      | BitVectors -> bit_vectors
    in
    let env = Smtlib_typed_env.add_sorts env th_def.sorts in
    let env = Smtlib_typed_env.add_funs env th_def.funs in
    Smtlib_typed_env.add_par_funs env th_def.par_funs
  in
  List.fold_left (fun env th -> aux env th) env ths


let contains s1 s2 =
  try
    let len = String.length s2 in
    for i = 0 to String.length s1 - len do
      if String.sub s1 i len = s2 then raise Exit
    done;
    false
  with Exit -> true

let set_logic env s =
  let logic = s.Smtlib_syntax.c in
  let theories = ref [Core] in
  let all = contains logic "ALL" in
  if contains logic "QF" then
    set_is_qf true;
  if all || contains logic "UF" then
    set_is_uf true;

  if contains logic "BV" then
    check_command "Bitvector";
  if contains logic "FP" then begin
    theories := FloatingPoint :: !theories;
    set_is_fp true;
  end;

  if all || contains logic "AX" || contains logic "A" then
    theories := Arrays :: !theories;

  if all || contains logic "IRA" then begin
    set_is_int_real true;
    theories := Reals_Ints :: !theories
  end
  else if contains logic "IA" || contains logic "IDL" then
    theories := Ints :: !theories
  else if contains logic "RA" || contains logic "RDL" then begin
    set_is_real true;
    theories := Reals :: !theories
  end;

  if all || contains logic "LIRA" || contains logic "LIA" ||
     contains logic "LRA" then
    set_is_linear true;
  if contains logic "NIRA" || contains logic "NIA" || contains logic "NRA" then
    set_is_non_linear true;

  if contains logic "DT" then
    set_is_dt true;

  add_theories env !theories