123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257(**************************************************************************)(* This file is part of the Codex semantics library. *)(* *)(* Copyright (C) 2013-2025 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It 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 Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file LICENSE). *)(* *)(**************************************************************************)typecounter_example=unittypesat=|Satofcounter_example|Unsat|UnknownmoduletypeCOMMON_S=sig(* An SMTLIB printer, with an higher-order abstract syntax
interface, that does not require any buffering (for fast
output). *)typelogictypecommand(**************** Logics ****************)valqf_uf:logic(* uninterpreted functions. *)valqf_lia:logic(* linear arithmetic. *)valqf_nia:logic(* general arithmetic. *)valqf_lra:logic(* linear real arithmetic. *)valqf_auflia:logic(* lia + uf + array. *)valauflia:logicvalauflira:logicvalaufnira:logicvallra:logicvalqf_idl:logicvalqf_rdl:logicvalqf_ufidl:logicvalqf_bv:logic(* bitvectors *)valqf_ax:logic(* arrays *)valqf_abv:logic(* array + bitvectors *)valqf_aubv:logic(* array + bitvectors + uninterpreted functions *)valhorn:logic(* Extension *)(**************** Commands ****************)typesatisfiable=Sat|Unsat|Unknown;;valcheck_sat:unit->satisfiablevalset_logic:logic->unitvalset_option:string->unitendmoduletypeTYPED_S=sigincludeCOMMON_Stype'asorttype'avalue(**************** Commands ****************)valassert_:boolvalue->unit(* command t *)(**************** Bool and generic functions ****************)valbool:boolsortvallet_:?name:string->'avalue->('avalue->'bvalue)->'bvaluevalforall:?name:string->'asort->('avalue->boolvalue)->boolvaluevalexists:?name:string->'asort->('avalue->boolvalue)->boolvalueval(=):'avalue->'avalue->boolvalueval(||):boolvalue->boolvalue->boolvalue(* Int theory *)valint:intsortvalnumeral:int->intvalueval(<):intvalue->intvalue->boolvalue(* Array theory, with extensibility: arrays are equal iff all elements are equal. *)type('a,'b)array(* Array from 'a sort to 'b sort. *)valarray:'asort->'bsort->('a,'b)arraysortvalselect:('a,'b)arrayvalue->'avalue->'bvaluevalstore:('a,'b)arrayvalue->'avalue->'bvalue->('a,'b)arrayvalue(* Fixed_Size_Bitvectors theory *)typebitvector(* Bitvector of a given size. TODO: compute sizes statically with GADTs? Works with extract?*)valbitvec:int->bitvectorsort(* Smallest bits in the integer *)valbvlit:size:int->Z.t->bitvectorvaluevalconcat:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvnot:bitvectorvalue->bitvectorvaluevalbvneg:bitvectorvalue->bitvectorvaluevalbvand:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvor:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvadd:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvmul:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvudiv:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvurem:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvshl:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvlshr:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvult:bitvectorvalue->bitvectorvalue->boolvalue(* Extensionso Fixed_Size_Bitvectors, but described in
http://smtlib.cs.uiowa.edu/logics-all.shtml. *)valbvxor:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvsdiv:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvsrem:bitvectorvalue->bitvectorvalue->bitvectorvaluevalbvule:bitvectorvalue->bitvectorvalue->boolvaluevalbvslt:bitvectorvalue->bitvectorvalue->boolvaluevalbvsle:bitvectorvalue->bitvectorvalue->boolvalueendmoduletypeUNTYPED_S=sigincludeCOMMON_Stypesorttypevalue(**************** Commands ****************)(* Execute the command. *)valassert_:value->unitvalget_assignment:unit->unit(* TODO: Difficult to make declare fun generic in terms of arity. *)(* MAYBE: A monadic interface would make explicit the fact that
declare_var has a side-effect. *)valdeclare_var:?name:string->sort->valuevaldefine_var:?name:string->sort->value->value(**************** Bool and generic functions ****************)valbool:sortvallet_:?name:string->value->(value->value)->valuevalforall:?name:string->sort->(value->value)->valuevalexists:?name:string->sort->(value->value)->valueval(=):value->value->valueval(||):value->value->valueval(&&):value->value->valueval(=>):value->value->valuevaland_list:valuelist->valuevalor_list:valuelist->valuevalnot:value->valuevaltrue_:valuevalfalse_:value(* Int theory *)valint:sortvalnumeral:int->valuevalnumeralz:Z.t->valueval(<):value->value->valueval(<=):value->value->valueval(-):value->value->valuevalneg:value->valueval(+):value->value->valueval(*):value->value->valuevaldiv:value->value->valuevalmodu:value->value->value(* Array theory, with extensibility: arrays are equal iff all elements are equal. *)valarray:sort->sort->sortvalselect:value->value->valuevalstore:value->value->value->value(* Fixed_Size_Bitvectors theory *)valbitvec:int->sort(* Smallest bits in the integer *)valbvlit:size:int->Z.t->valuevalconcat:value->value->valuevalbvnot:value->valuevalbvneg:value->valuevalbvand:value->value->valuevalbvor:value->value->valuevalbvadd:value->value->valuevalbvmul:value->value->valuevalbvudiv:value->value->valuevalbvurem:value->value->valuevalbvshl:value->value->valuevalbvlshr:value->value->valuevalbvult:value->value->valuevalextract:first:int->last:int->value->value(* Extensions to Fixed_Size_Bitvectors, but described in
http://smtlib.cs.uiowa.edu/logics-all.shtml. *)valbvxor:value->value->valuevalbvsdiv:value->value->valuevalbvsrem:value->value->valuevalbvsmod:value->value->valuevalbvule:value->value->valuevalbvslt:value->value->valuevalbvsle:value->value->valuevalbvashr:value->value->value(* The int indicates how much bit to extend. *)valsign_extend:int->value->valuevalzero_extend:int->value->valueend(**************** Mu-z extensions, described in http://rise4fun.com/Z3/tutorialcontent/fixedpoints ****************)moduletypeUNTYPED_MUZ=sigincludeUNTYPED_Styperelation=valuelist->valuevaldeclare_rel:?name:string->sortlist->relation*string(* A horn rule, with premices and a conclusion. *)valrule:valuelist->value->unitvalfact:value->unit(* val query: relation t -> value list -> unit *)valquery:value->satisfiable(* Note: query is used differently on different versions of z3. *)valquery2:string->satisfiablevaldeclare_muz_var:?name:string->sort->valueendmoduletypePARAM_S=sigvalprint:string->unitvalinc:Stdlib.in_channelvalflush:unit->unitend