Skip to content

Commit

Permalink
Add Expr constructors for Reals and Reals_Ints
Browse files Browse the repository at this point in the history
This patch adds constructors for the `Reals` and `Reals_Ints` SMT-LIB
theories.
  • Loading branch information
bclement-ocp committed Oct 12, 2023
1 parent ea79161 commit a62159c
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/lib/structures/expr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2886,6 +2886,76 @@ module Ints = struct
let ( > ) x y = y < x
end


(** Constructors from the smtlib theory of real numbers.
https://smtlib.cs.uiowa.edu/theories-Reals.shtml *)
module Reals = struct
let of_int n = real (string_of_int n)

let ( ~$ ) = of_int

let of_Z n = real (Z.to_string n)

let ( ~$$ ) = of_Z

let of_Q q = real (Q.to_string q)

let ( ~$$$ ) = of_Q

let ( - ) x y = mk_term (Op Minus) [ x; y ] Treal

let ( ~- ) x = ~$0 - x

let ( + ) x y = mk_term (Op Plus) [ x; y ] Treal

let ( * ) x y = mk_term (Op Mult) [ x; y ] Treal

let ( / ) x y = mk_term (Op Div) [ x; y ] Treal

let ( ** ) x y = mk_term (Op Pow) [ x; y ] Treal

let ( <= ) x y = mk_builtin ~is_pos:true LE [ x; y ]

let ( >= ) x y = y <= x

let ( < ) x y = mk_builtin ~is_pos:true LT [x; y ]

let ( > ) x y = y < x
end

(** Constructors from the smtlib theories of real numbers and integers.
Constants and comparisons are not provided to avoid type confusion bugs. Use
the constants and comparisons from [Reals] or [Ints] as appropriate.
https://smtlib.cs.uiowa.edu/theories-Reals_Ints.shtml *)
module Reals_Ints = struct
(* Integer operators *)
let ( - ) = Ints.( - )
let ( ~- ) = Ints.( ~- )
let ( + ) = Ints.( + )
let ( * ) = Ints.( * )
let ( / ) = Ints.( / )
let ( mod ) = Ints.( mod )
let abs = Ints.abs
let ( ** ) = Ints.( ** )

(* Real operators *)
let ( -. ) = Reals.( - )
let ( ~-. ) = Reals.( ~- )
let ( +. ) = Reals.( + )
let ( *. ) = Reals.( * )
let ( /. ) = Reals.( / )
let ( **. ) = Reals.( ** )

(* Conversion operators *)
let to_real n = mk_term (Op Real_of_int) [ n ] Treal

let to_int n = mk_term (Op Int_floor) [ n ] Tint

let is_int n = mk_term (Op Real_is_int) [ n ] Tbool
end

(** Constructors from the smtlib theory of fixed-size bit-vectors and the QF_BV
logic.
Expand Down
62 changes: 62 additions & 0 deletions src/lib/structures/expr.mli
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,68 @@ module Ints : sig
val ( > ) : t -> t -> t
end

(** Constructors from the smtlib theory of reals.
https://smtlib.cs.uiowa.edu/theories-Reals.shtml *)
module Reals : sig
(* Conversion from int *)
val of_int : int -> t
val ( ~$ ) : int -> t

(* Conversion from ZArith *)
val of_Z : Z.t -> t
val ( ~$$ ) : Z.t -> t

val of_Q : Q.t -> t
val ( ~$$$ ) : Q.t -> t

(* Arithmetic operations *)
val ( + ) : t -> t -> t
val ( - ) : t -> t -> t
val ( ~- ) : t -> t
val ( * ) : t -> t -> t
val ( / ) : t -> t -> t

(* Exponentiation *)
val ( ** ) : t -> t -> t

(* Comparisons *)
val ( <= ) : t -> t -> t
val ( >= ) : t -> t -> t
val ( < ) : t -> t -> t
val ( > ) : t -> t -> t
end

(** Constructors from the smtlib theories of real numbers and integers.
Constants and comparisons are not provided to avoid type confusion bugs. Use
the constants and comparisons from [Reals] or [Ints] as appropriate.
https://smtlib.cs.uiowa.edu/theories-Reals_Ints.shtml *)
module Reals_Ints : sig
(* Integer operations *)
val ( + ) : t -> t -> t
val ( - ) : t -> t -> t
val ( ~- ) : t -> t
val ( * ) : t -> t -> t
val ( / ) : t -> t -> t
val ( mod ) : t -> t -> t
val abs : t -> t
val ( ** ) : t -> t -> t

(* Real operations *)
val ( +. ) : t -> t -> t
val ( -. ) : t -> t -> t
val ( ~-. ) : t -> t
val ( *. ) : t -> t -> t
val ( /. ) : t -> t -> t
val ( **. ) : t -> t -> t

(* Conversions *)
val to_real : t -> t
val to_int : t -> t
val is_int : t -> t
end

(** Constructors from the smtlib theory of fixed-size bit-vectors and the QF_BV
logic.
Expand Down

0 comments on commit a62159c

Please sign in to comment.