(** Why3 driver for Z3 >= 4.3.2 *)

(* Do not set any logic, let z3 choose by itself
   prelude "(set-logic AUFNIRA)"
*)

(* Counterexamples: set model parser *)
model_parser "smtv2"

import "smt-libv2.gen"
printer "smtv2"
import "smt-libv2-bv.gen"
import "z3_bv.gen"
import "smt-libv2-floats.gen"
import "discrimination.gen"

(* Performed introductions depending on whether counterexamples are
   requested, as said by the meta "get_counterexmp". When this meta
   set, this transformation introduces the model variables that are
   still embedded in the goal. When it is not set, it removes all
   these unused-ce-related variables, even in the context.  *)
transformation "counterexamples_dependent_intros"

transformation "inline_trivial"
transformation "eliminate_builtin"
transformation "remove_unused_keep_constants"
transformation "detect_polymorphism"
transformation "eliminate_definition_conditionally"
transformation "eliminate_inductive"
transformation "eliminate_epsilon"
transformation "eliminate_algebraic_if_poly"
transformation "eliminate_literal"

transformation "simplify_formula"
(*transformation "simplify_trivial_quantification"*)

(* Prepare for counter-example query: get rid of some quantifiers (makes it
possible to query model values of the variables in premises) and introduce
counter-example projections  *)
transformation "prepare_for_counterexmp"

transformation "discriminate_if_poly"
transformation "encoding_smt_if_poly"


(** Error messages specific to Z3 *)

outofmemory "(error \".*out of memory\")"
outofmemory "Failed to verify: pthread_create"
outofmemory "std::bad_alloc"
outofmemory "Resource temporarily unavailable"
outofmemory "Cannot allocate memory"
(* not convenient: is more a problem of fork
outofmemory "error while loading shared libraries:.*failed to map segment from shared object"
*)
timeout "^timeout$"
timeout "interrupted by timeout"
steps ":rlimit-count +\\([0-9]+\\)" 1
steplimitexceeded "Maximal allocation counts [0-9]+ have been exceeded"

(** Z3 needs "(push)" to provide models *)
theory BuiltIn
  meta "counterexample_need_smtlib_push" ""
end

(** Extra theories supported by Z3 *)

(* div/mod of Z3 seems to be Euclidean Division *)
theory int.EuclideanDivision
  syntax function div "(div %1 %2)"
  syntax function mod "(mod %1 %2)"
  remove prop Mod_bound
  remove prop Div_mod
  remove prop Mod_1
  remove prop Div_1
end

theory int.MinMax
  remove allprops
end

theory real.FromInt
  syntax function from_int "(to_real %1)"
  remove prop Zero
  remove prop One
  remove prop Add
  remove prop Sub
  remove prop Mul
  remove prop Neg
end

(* does not work: Z3 segfaults
theory real.Trigonometry

  syntax function cos "(cos %1)"
  syntax function sin "(sin %1)"
  syntax function pi "pi"
  syntax function tan "(tan %1)"
  syntax function atan "(atan %1)"

end
*)

theory ieee_float.Float64
(* check the sign bit; if pos |%1| else |%1| - 2^1025 *)
   syntax function to_int
     "(ite (= ((_ extract 1024 1024) ((_ fp.to_sbv 1025) %1 %2)) #b0) (bv2int ((_ fp.to_sbv 1025) %1 %2)) (- (bv2int ((_ fp.to_sbv 1025) %1 %2)) (bv2int (bvshl (_ bv1 1026) (_ bv1025 1026)))))"
 (* we do not define of_int since z3 will not prove anything if it
    appears (05/01/2017) *)
end

theory ieee_float.Float32
  (* check the sign bit; if pos |%1| else |%1| - 2^129 *)
   syntax function to_int
     "(ite (= ((_ extract 128 128) ((_ fp.to_sbv 129) %1 %2)) #b0) (bv2int ((_ fp.to_sbv 129) %1 %2)) (- (bv2int ((_ fp.to_sbv 129) %1 %2)) (bv2int (bvshl (_ bv1 130) (_ bv129 130)))))"
end

theory real.Square
  remove allprops
end
