-
Notifications
You must be signed in to change notification settings - Fork 1
/
cvc5_stubs.cpp
1321 lines (1146 loc) · 36.3 KB
/
cvc5_stubs.cpp
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
/**************************************************************************
* Copyright (C) 2024 formalsec *
* *
* This file is part of ocaml-cvc5 *
* *
* ocaml-cvc5 is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation, either version 3 of the License, *
* or (at your option) any later version. *
* *
* ocaml-cvc5 is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with ocaml-cvc5. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/custom.h>
#include <atomic>
#include <google/dense_hash_map>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <tuple>
#include <cvc5/cvc5.h>
#define CVC5_TRY_CATCH_BEGIN \
try \
{
#define CVC5_TRY_CATCH_END \
} \
catch (cvc5::CVC5ApiException &e) { caml_invalid_argument(e.getMessage().c_str()); }
extern "C"
{
int compare_ptrs(void* pt1, void* pt2) {
if (pt1 == pt2)
return 0;
else if ((intnat)pt1 < (intnat)pt2)
return -1;
else
return +1;
}
/*============================================================================
* Solver
============================================================================*/
#define Solver_val(v) (*((cvc5::Solver**)Data_custom_val(v)))
static void solver_delete (value vt)
{
cvc5::Solver* solver = Solver_val(vt);
Solver_val(vt) = nullptr;
if (solver != nullptr) {
delete solver;
}
}
static struct custom_operations solver_operations =
{
"https://cvc5.github.io/",
&solver_delete,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
/*============================================================================
* Term Manager
============================================================================*/
class TermManager : public cvc5::TermManager {
public:
/* Used for API-level reference counting */
google::dense_hash_map<std::string, cvc5::Term*> *termMap;
std::atomic<unsigned long> rc;
TermManager() : cvc5::TermManager() {
rc = 1;
termMap = new google::dense_hash_map<std::string, cvc5::Term*>();
termMap->set_empty_key("");
termMap->set_deleted_key("DELETED");
}
~TermManager() {}
void * operator new(size_t size,
struct custom_operations *ops,
value *custom){
*custom = caml_alloc_custom(ops, size, 0, 1);
return Data_custom_val(*custom);
}
void operator delete(void *ptr) {}
void addRef() { rc.fetch_add(1, std::memory_order_release); }
void addTerm(const std::string &key, cvc5::Term* term) {
(*termMap)[key] = term;
}
cvc5::Term* getTerm(const std::string &key) const {
auto it = termMap->find(key);
if (it != termMap->end()) {
return it->second;
}
return nullptr;
}
};
#define TermManager_val(v) ((TermManager*)Data_custom_val(v))
int cvc5_tm_compare(value v1, value v2){
TermManager* tm1 = TermManager_val(v1);
TermManager* tm2 = TermManager_val(v2);
return compare_ptrs(tm1, tm2);
}
intnat cvc5_tm_hash(value v){
TermManager* tm = TermManager_val(v);
return (intnat)tm;
}
static void try_delete_tm(TermManager* tm){
if (tm->rc == 0) { delete tm; }
}
static void delete_tm(value v){
TermManager* tm = TermManager_val(v);
tm->rc--;
try_delete_tm(tm);
}
static struct custom_operations term_manager_operations =
{
"https://cvc5.github.io/",
&delete_tm,
cvc5_tm_compare,
cvc5_tm_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
/*============================================================================
* Term
============================================================================*/
class Term : public cvc5::Term {
public:
TermManager* _tm;
Term(cvc5::Term t, TermManager* tm) : cvc5::Term(t) {
if (tm != NULL) { _tm = tm; tm->addRef(); }
else { _tm = NULL; }
}
~Term() {}
void * operator new(size_t size,
struct custom_operations *ops,
value *custom){
*custom = caml_alloc_custom(ops, size, 0, 1);
return Data_custom_val(*custom);
}
void operator delete(void *ptr) {}
};
#define Term_val(v) ((Term*)Data_custom_val(v))
static void term_delete(value v){
Term* term = Term_val(v);
if (term->_tm != NULL) {
// decrement the reference count of the term manager
term->_tm->rc--;
try_delete_tm(term->_tm);
}
delete term;
}
static struct custom_operations term_operations =
{
"https://cvc5.github.io/",
&term_delete,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
/*============================================================================
* Sort
============================================================================*/
class Sort : public cvc5::Sort {
public:
TermManager* _tm;
Sort(cvc5::Sort t, TermManager* tm) : cvc5::Sort(t) {
if (tm != NULL) { _tm = tm; tm->addRef(); }
else { _tm = NULL; }
}
~Sort() {}
void * operator new(size_t size,
struct custom_operations *ops,
value *custom){
*custom = caml_alloc_custom(ops, size, 0, 1);
return Data_custom_val(*custom);
}
void operator delete(void *ptr) {}
};
#define Sort_val(v) ((Sort*)Data_custom_val(v))
static void sort_delete(value v){
Sort* sort = Sort_val(v);
if (sort->_tm != NULL) {
// decrement the reference count of the term manager
sort->_tm->rc--;
try_delete_tm(sort->_tm);
}
delete sort;
}
static struct custom_operations sort_operations =
{
"https://cvc5.github.io/",
&sort_delete,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
/*============================================================================
* Result
============================================================================*/
class Result : public cvc5::Result {
public:
Result(cvc5::Result t) : cvc5::Result(t) {}
~Result() {}
void * operator new(size_t size,
struct custom_operations *ops,
value *custom){
*custom = caml_alloc_custom(ops, size, 0, 1);
return Data_custom_val(*custom);
}
void operator delete(void *ptr) {}
};
#define Result_val(v) ((Result*)Data_custom_val(v))
static void result_delete(value v){
delete Result_val(v);
}
static struct custom_operations result_operations =
{
"https://cvc5.github.io/",
&result_delete,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
/*============================================================================
* Op
============================================================================*/
class Op : public cvc5::Op {
public:
Op(cvc5::Op t) : cvc5::Op(t) {}
~Op() {}
void * operator new(size_t size,
struct custom_operations *ops,
value *custom){
*custom = caml_alloc_custom(ops, size, 0, 1);
return Data_custom_val(*custom);
}
void operator delete(void *ptr) {}
};
#define OP_val(v) ((Op*)Data_custom_val(v))
static void op_delete(value v){
delete OP_val(v);
}
static struct custom_operations op_operations =
{
"https://cvc5.github.io/",
&op_delete,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
/*============================================================================
* Stubs
============================================================================*/
CAMLprim value ocaml_cvc5_stub_new_solver(value v){
CAMLparam1(v);
CAMLlocal1(r);
TermManager* tm = TermManager_val(v);
cvc5::Solver* solver = new cvc5::Solver(*tm);
r = caml_alloc_custom(&solver_operations, sizeof(cvc5::Solver*), 0, 1);
Solver_val(r) = solver;
CAMLreturn(r);
}
CAMLprim value ocaml_cvc5_stub_delete(value v){
CVC5_TRY_CATCH_BEGIN;
solver_delete(v);
return Val_unit;
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_new_term_manager(){
CAMLparam0();
CAMLlocal1(custom);
CVC5_TRY_CATCH_BEGIN;
new(&term_manager_operations, &custom) TermManager();
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_delete_term_manager(value v){
CVC5_TRY_CATCH_BEGIN;
delete_tm(v);
return Val_unit;
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_delete_term(value v){
CVC5_TRY_CATCH_BEGIN;
term_delete(v);
return Val_unit;
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_delete_sort(value v){
CVC5_TRY_CATCH_BEGIN;
sort_delete(v);
return Val_unit;
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_delete_result(value v){
CVC5_TRY_CATCH_BEGIN;
result_delete(v);
return Val_unit;
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_term_equal(value t1, value t2){
return Val_bool(*Term_val(t1) == *Term_val(t2));
}
CAMLprim value ocaml_cvc5_stub_sort_equal(value s1, value s2){
return Val_bool(*Sort_val(s1) == *Sort_val(s2));
}
CAMLprim value ocaml_cvc5_stub_result_equal(value r1, value r2){
return Val_bool(*Result_val(r1) == *Result_val(r2));
}
CAMLprim value ocaml_cvc5_stub_term_id(value v){
CVC5_TRY_CATCH_BEGIN;
return Val_int(Term_val(v)->getId());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_term_kind(value v){
CVC5_TRY_CATCH_BEGIN;
return Val_int(Term_val(v)->getKind());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_term_sort(value v){
CAMLparam1(v);
CAMLlocal1(custom);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(Term_val(v)->getSort(), NULL);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_true(value v){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(tm->mkTrue(), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_false(value v){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(tm->mkFalse(), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_bool(value v, value b){
CAMLparam2(v, b);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(tm->mkBoolean(Bool_val(b)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_int(value v, value i){
CAMLparam2(v, i);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(tm->mkInteger(Int_val(i)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_real_s(value v, value r){
CAMLparam2(v, r);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(tm->mkReal(String_val(r)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value native_cvc5_stub_mk_real_i(value v, int64_t i){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(tm->mkReal(i), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_real_i(value v, value i){
return native_cvc5_stub_mk_real_i(v, Int64_val(i));
}
CAMLprim value native_cvc5_stub_mk_real(value v, int64_t num, int64_t den){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(tm->mkReal(num, den), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_real(value v, value num, value den){
return native_cvc5_stub_mk_real(v, Int64_val(num), Int64_val(den));
}
CAMLprim value native_cvc5_stub_mk_bv(value v, uint32_t size, int64_t i){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom)
Term(tm->mkBitVector(size, i), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_bv(value v, value size, value i){
return native_cvc5_stub_mk_bv(v, Long_val(size), Int64_val(i));
}
CAMLprim value native_cvc5_stub_mk_bv_s(value v, uint32_t size, value s, uint32_t base){
CAMLparam2(v, s);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom)
Term(tm->mkBitVector(size, String_val(s), base), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_bv_s(value v, value size, value s, value base){
return native_cvc5_stub_mk_bv_s(v, Long_val(size), s, Long_val(base));
}
CAMLprim value ocaml_cvc5_stub_term_to_string(value v){
CAMLparam1(v);
CVC5_TRY_CATCH_BEGIN;
CAMLreturn(caml_copy_string(Term_val(v)->toString().c_str()));
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_string(value v, value s, value b){
CAMLparam3(v, s, b);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(tm->mkString(String_val(s), Bool_val(b)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_term(value v, value kind, value t){
CAMLparam3(v, kind, t);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
std::vector<cvc5::Term> args;
size_t arity = Wosize_val(t);
args.reserve(arity);
for (size_t i = 0; i < arity; i++)
args.emplace_back(*Term_val(Field(t, i)));
new(&term_operations, &custom)
Term(tm->mkTerm((cvc5::Kind)Int_val(kind), args), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_term_1(value v, value kind, value t){
CAMLparam3(v, kind, t);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
std::vector<cvc5::Term> args = {*Term_val(t)};
new(&term_operations, &custom)
Term(tm->mkTerm((cvc5::Kind)Int_val(kind), args), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_term_2(value v, value kind, value t1, value t2){
CAMLparam4(v, kind, t1, t2);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
std::vector<cvc5::Term> args = {*Term_val(t1), *Term_val(t2)};
new(&term_operations, &custom)
Term(tm->mkTerm((cvc5::Kind)Int_val(kind), args), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_term_3(value v, value kind, value t1, value t2, value t3){
CAMLparam5(v, kind, t1, t2, t3);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
std::vector<cvc5::Term> args = {*Term_val(t1), *Term_val(t2), *Term_val(t3)};
new(&term_operations, &custom)
Term(tm->mkTerm((cvc5::Kind)Int_val(kind), args), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_term_with_op(value v, value op, value t){
CAMLparam3(v, op, t);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
std::vector<cvc5::Term> args;
size_t arity = Wosize_val(t);
args.reserve(arity);
for (size_t i = 0; i < arity; i++)
args.emplace_back(*Term_val(Field(t, i)));
new(&term_operations, &custom)
Term(tm->mkTerm(*OP_val(op), args), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_declare_fun(value slv, value symbol, value sorts, value r) {
CAMLparam4(slv, symbol, sorts, r);
CAMLlocal1(custom);
cvc5::Solver* solver = Solver_val(slv);
CVC5_TRY_CATCH_BEGIN;
std::vector<cvc5::Sort> sort_vec;
size_t arity = Wosize_val(sorts);
sort_vec.reserve(arity);
for (size_t i = 0; i < arity; i++)
sort_vec.emplace_back(*Sort_val(Field(sorts, i)));
new(&term_operations, &custom)
Term(solver->declareFun(String_val(symbol), sort_vec, *Sort_val(r)), NULL);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_var_s(value v, value s, value n){
CAMLparam3(v, s, n);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
cvc5::Sort* sort = Sort_val(s);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom)
Term(tm->mkVar(*sort, String_val(n)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_var(value v, value s){
CAMLparam2(v, s);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
cvc5::Sort* sort = Sort_val(s);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom)
Term(tm->mkVar(*sort), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value
ocaml_cvc5_stub_define_fun(value slv, value symbol, value vars, value s, value body){
CAMLparam5(slv, symbol, vars, s, body);
CAMLlocal1(custom);
cvc5::Solver* solver = Solver_val(slv);
CVC5_TRY_CATCH_BEGIN;
std::vector<cvc5::Term> var_vec;
size_t arity = Wosize_val(vars);
var_vec.reserve(arity);
for (size_t i = 0; i < arity; i++)
var_vec.emplace_back(*Term_val(Field(vars, i)));
new(&term_operations, &custom)
Term(solver->defineFun(String_val(symbol), var_vec, *Sort_val(s), *Term_val(body)), NULL);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_int_value(value t){
CVC5_TRY_CATCH_BEGIN;
return caml_copy_string(Term_val(t)->getIntegerValue().c_str());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_int_value(value t){
CVC5_TRY_CATCH_BEGIN;
return Val_bool(Term_val(t)->isIntegerValue());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_real_value(value t){
CVC5_TRY_CATCH_BEGIN;
return caml_copy_string(Term_val(t)->getRealValue().c_str());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_real_value(value t){
return Val_bool(Term_val(t)->isRealValue());
}
CAMLprim value ocaml_cvc5_stub_is_string_value(value t){
return Val_bool(Term_val(t)->isStringValue());
}
CAMLprim value ocaml_cvc5_stub_get_string_value(value t){
CVC5_TRY_CATCH_BEGIN;
std::wstring ws = Term_val(t)->getStringValue();
std::string to_return;
std::transform(ws.begin(), ws.end(), std::back_inserter(to_return), [] (wchar_t c) {
return (char)c;
});
return caml_copy_string(to_return.c_str());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_int32_value(value t){
CVC5_TRY_CATCH_BEGIN;
return caml_copy_int32(Term_val(t)->getInt32Value());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_int32_value(value t){
return Val_bool(Term_val(t)->isInt32Value());
}
CAMLprim value ocaml_cvc5_stub_get_int64_value(value t){
CVC5_TRY_CATCH_BEGIN;
return caml_copy_int64(Term_val(t)->getInt64Value());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_int64_value(value t){
return Val_bool(Term_val(t)->isInt64Value());
}
CAMLprim value ocaml_cvc5_stub_get_uint32_value(value t){
CVC5_TRY_CATCH_BEGIN;
return Val_int(Term_val(t)->getUInt32Value());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_uint32_value(value t){
return Val_bool(Term_val(t)->isUInt32Value());
}
CAMLprim value ocaml_cvc5_stub_get_uint64_value(value t){
CVC5_TRY_CATCH_BEGIN;
return caml_copy_int64(Term_val(t)->getUInt64Value());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_uint64_value(value t){
return Val_bool(Term_val(t)->isUInt64Value());
}
CAMLprim value native_cvc5_stub_get_bv_value(value t, uint32_t base){
CVC5_TRY_CATCH_BEGIN;
return caml_copy_string(Term_val(t)->getBitVectorValue(base).c_str());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_bv_value(value t, value base){
return native_cvc5_stub_get_bv_value(t, Long_val(base));
}
CAMLprim value ocaml_cvc5_stub_is_bv_value(value t){
return Val_bool(Term_val(t)->isBitVectorValue());
}
CAMLprim value ocaml_cvc5_stub_get_rm_value(value t){
CVC5_TRY_CATCH_BEGIN;
return Val_int((int)(Term_val(t)->getRoundingModeValue()));
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_rm_value(value t){
CVC5_TRY_CATCH_BEGIN;
return Val_bool(Term_val(t)->isRoundingModeValue());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_fp_value(value t) {
CAMLparam1(t);
CVC5_TRY_CATCH_BEGIN;
CAMLreturn(Val_bool(Term_val(t)->isFloatingPointValue()));
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_fp_value(value t) {
CAMLparam1(t);
CAMLlocal2(custom, term);
TermManager *tm = TermManager_val(t);
const auto fp = Term_val(t)->getFloatingPointValue();
int ebits = std::get<0>(fp);
int sbits = std::get<1>(fp);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &term) Term(std::get<2>(fp), tm);
custom = caml_alloc_tuple(3);
Store_field (custom, 0, Val_int(ebits));
Store_field (custom, 1, Val_int(sbits));
Store_field (custom, 2, term);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_is_bool_value(value t){
CAMLparam1(t);
CVC5_TRY_CATCH_BEGIN;
CAMLreturn(Val_bool(Term_val(t)->isBooleanValue()));
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_bool_value(value t){
CVC5_TRY_CATCH_BEGIN;
return Val_bool(Term_val(t)->getBooleanValue());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_rounding_mode(value v, value rm){
CAMLparam2(v, rm);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom)
Term(tm->mkRoundingMode((cvc5::RoundingMode)Int_val(rm)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_boolean_sort(value v){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->getBooleanSort(), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_integer_sort(value v){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->getIntegerSort(), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_bitvector_sort(value v, value size){
CAMLparam2(v, size);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->mkBitVectorSort(Int_val(size)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_real_sort(value v){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->getRealSort(), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_string_sort(value v){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->getStringSort(), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_get_rm_sort(value v){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->getRoundingModeSort(), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_sort_get_bv_size(value v){
CVC5_TRY_CATCH_BEGIN;
return Val_int(Sort_val(v)->getBitVectorSize());
CVC5_TRY_CATCH_END;
}
CAMLprim value native_cvc5_stub_mk_fp_sort(value v, uint32_t exp, uint32_t sig){
CAMLparam1(v);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->mkFloatingPointSort(exp, sig), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_fp_sort(value v, value exp, value sig){
return native_cvc5_stub_mk_fp_sort(v, Long_val(exp), Long_val(sig));
}
CAMLprim value ocaml_cvc5_stub_mk_seq_sort(value v, value sort){
CAMLparam2(v, sort);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->mkSequenceSort(*Sort_val(sort)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_uninterpreted_sort(value v, value s){
CAMLparam2(v, s);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(tm->mkUninterpretedSort(String_val(s)), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_sort_to_string(value v){
CAMLparam1(v);
CVC5_TRY_CATCH_BEGIN;
return caml_copy_string(Sort_val(v)->toString().c_str());
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_mk_const_s(value v, value sort, value n) {
CAMLparam3(v, sort, n);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
cvc5::Sort* s = Sort_val(sort);
std::string termName = String_val(n);
auto existingTerm = tm->getTerm(termName);
if (existingTerm != nullptr) {
new(&term_operations, &custom) Term(*existingTerm, tm);
CAMLreturn(custom);
} else {
CVC5_TRY_CATCH_BEGIN;
cvc5::Term* newTerm = new cvc5::Term(tm->mkConst(*s, termName));
tm->addTerm(termName, newTerm);
new(&term_operations, &custom) Term(*newTerm, tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
}
CAMLprim value ocaml_cvc5_stub_mk_const(value v, value sort){
CAMLparam2(v, sort);
CAMLlocal1(custom);
TermManager* tm = TermManager_val(v);
cvc5::Sort* s = Sort_val(sort);
CVC5_TRY_CATCH_BEGIN;
new(&term_operations, &custom)
Term(tm->mkConst(*s), tm);
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_assert_formula(value v, value t){
CVC5_TRY_CATCH_BEGIN;
Solver_val(v)->assertFormula(*Term_val(t));
return Val_unit;
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_check_sat(value v){
CAMLparam1(v);
CAMLlocal1(custom);
cvc5::Solver* solver = Solver_val(v);
CVC5_TRY_CATCH_BEGIN;
new(&result_operations, &custom)
Result(solver->checkSat());
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_check_sat_assuming(value v, value t){
CAMLparam2(v, t);
CAMLlocal1(custom);
CVC5_TRY_CATCH_BEGIN;
std::vector<cvc5::Term> assumptions;
size_t arity = Wosize_val(t);
assumptions.reserve(arity);
for (size_t i = 0; i < arity; i++)
assumptions.emplace_back(*Term_val(Field(t, i)));
new(&result_operations, &custom)
Result(Solver_val(v)->checkSatAssuming(assumptions));
CAMLreturn(custom);
CVC5_TRY_CATCH_END;
}
CAMLprim value ocaml_cvc5_stub_result_is_sat(value v){
CVC5_TRY_CATCH_BEGIN;
return Val_bool(Result_val(v)->isSat());