Skip to content

Commit

Permalink
Fixes for real lits
Browse files Browse the repository at this point in the history
  • Loading branch information
Gbury committed Sep 12, 2024
1 parent 9419ec5 commit e74fc4d
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 13 deletions.
30 changes: 23 additions & 7 deletions src/languages/smtlib2/poly/print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ let num fmt s =
let dec ~k fmt s =
(* smtlib requires at least one digit before or after a `.` *)
let s =
if String.length s >= 2 then
if not (String.contains s '.') then
s ^ ".0"
else if String.length s >= 2 then
if s.[0] = '.'
then "0" ^ s
else if s.[String.length s - 1] = '.'
Expand Down Expand Up @@ -387,6 +389,15 @@ module Make
| Var v -> Env.Term_var.bind env v
| Constructor (_, l) -> List.fold_left Env.Term_var.bind env l

let extract_int_lit t =
match V.Term.view t with
| App (head, [], []) ->
begin match V.Term.Cst.builtin head with
| B.Integer s -> Some s
| _ -> None
end
| _ -> None

let term_cst_chainable _env c =
(* WARNING: this `blt` function should only be called with builtins that
do not have payload (such as terms), since the polymorphic comparison
Expand Down Expand Up @@ -541,13 +552,18 @@ module Make
| App (ah, []), App (bh, [])
when (match V.Ty.Cst.builtin ah with B.Int -> true | _ -> false) &&
(match V.Ty.Cst.builtin bh with B.Real -> true | _ -> false) ->
if can_omit_to_real env then
match args with
begin match args with
| [t] when can_omit_to_real env -> term env fmt t
| [t] ->
term env fmt t
| _ -> _cannot_print "bad application of coercion"
else
simple "to_real"
if can_omit_to_real env then
term env fmt t
else
begin match extract_int_lit t with
| Some n -> aux (Dolmen_std.Id.create (Value Real) (N.simple n)) []
| None -> simple "to_real"
end
| _ -> simple "to_real"
end

(* fallback *)
| _ -> _cannot_print "unhandled builtin"
Expand Down
26 changes: 20 additions & 6 deletions src/languages/smtlib2/v2.6/script/print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@ module Make
| Var v -> Env.Term_var.bind env v
| Constructor (_, l) -> List.fold_left Env.Term_var.bind env l

let extract_int_lit t =
match V.Term.view t with
| App (head, [], []) ->
begin match V.Term.Cst.builtin head with
| B.Integer s -> Some s
| _ -> None
end
| _ -> None

let term_cst_chainable _env c =
(* WARNING: this `blt` function should only be called with builtins that
do not have payload (such as terms), since the polymorphic comparison
Expand Down Expand Up @@ -546,13 +555,18 @@ module Make
| App (ah, []), App (bh, [])
when (match V.Ty.Cst.builtin ah with B.Int -> true | _ -> false) &&
(match V.Ty.Cst.builtin bh with B.Real -> true | _ -> false) ->
if can_omit_to_real env then
match args with
begin match args with
| [t] when can_omit_to_real env -> term env fmt t
| [t] ->
term env fmt t
| _ -> _cannot_print "bad application of coercion"
else
simple "to_real"
if can_omit_to_real env then
term env fmt t
else
begin match extract_int_lit t with
| Some n -> aux (Dolmen_std.Id.create (Value Real) (N.simple n)) []
| None -> simple "to_real"
end
| _ -> simple "to_real"
end

(* fallback *)
| _ -> _cannot_print "unhandled builtin"
Expand Down
32 changes: 32 additions & 0 deletions tests/print/smtlib/poly/dune
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@
(action (diff poly.expected poly.full)))


; Test for real_lit.smt2
; Incremental test

(rule
(target real_lit.incremental)
(deps (:input real_lit.smt2))
(package dolmen_bin)
(action (chdir %{workspace_root}
(with-outputs-to %{target}
(with-accepted-exit-codes (or 0 (not 0))
(run dolmen --mode=incremental --color=never %{input} %{read-lines:flags.dune}))))))
(rule
(alias runtest)
(package dolmen_bin)
(action (diff real_lit.expected real_lit.incremental)))

; Full mode test

(rule
(target real_lit.full)
(deps (:input real_lit.smt2))
(package dolmen_bin)
(action (chdir %{workspace_root}
(with-outputs-to %{target}
(with-accepted-exit-codes (or 0 (not 0))
(run dolmen --mode=full --color=never %{input} %{read-lines:flags.dune}))))))
(rule
(alias runtest)
(package dolmen_bin)
(action (diff real_lit.expected real_lit.full)))


; Test for unit_and_dec.ae
; Full mode test

Expand Down
6 changes: 6 additions & 0 deletions tests/print/smtlib/poly/real_lit.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Warning The logic used to typecheck the problem (ALL) is not minimal. The minimal logic actually is: QF_UFLRA
(set-logic QF_UFLRA)
(declare-fun p (Real) Bool)
(assert (p 42.0))
(check-sat)
(exit)
5 changes: 5 additions & 0 deletions tests/print/smtlib/poly/real_lit.smt2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(set-logic ALL)
(declare-fun p (Real) Bool)
(assert (p (to_real 42)))
(check-sat)
(exit)
32 changes: 32 additions & 0 deletions tests/print/smtlib/v2.6/dune
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,36 @@
(action (diff poly.expected poly.full)))


; Test for real_lit.smt2
; Incremental test

(rule
(target real_lit.incremental)
(deps (:input real_lit.smt2))
(package dolmen_bin)
(action (chdir %{workspace_root}
(with-outputs-to %{target}
(with-accepted-exit-codes (or 0 (not 0))
(run dolmen --mode=incremental --color=never %{input} %{read-lines:flags.dune}))))))
(rule
(alias runtest)
(package dolmen_bin)
(action (diff real_lit.expected real_lit.incremental)))

; Full mode test

(rule
(target real_lit.full)
(deps (:input real_lit.smt2))
(package dolmen_bin)
(action (chdir %{workspace_root}
(with-outputs-to %{target}
(with-accepted-exit-codes (or 0 (not 0))
(run dolmen --mode=full --color=never %{input} %{read-lines:flags.dune}))))))
(rule
(alias runtest)
(package dolmen_bin)
(action (diff real_lit.expected real_lit.full)))


; Auto-generated part end
6 changes: 6 additions & 0 deletions tests/print/smtlib/v2.6/real_lit.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Warning The logic used to typecheck the problem (ALL) is not minimal. The minimal logic actually is: QF_UFLRA
(set-logic QF_UFLRA)
(declare-fun p (Real) Bool)
(assert (p 42.0))
(check-sat)
(exit)
5 changes: 5 additions & 0 deletions tests/print/smtlib/v2.6/real_lit.smt2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(set-logic ALL)
(declare-fun p (Real) Bool)
(assert (p (to_real 42)))
(check-sat)
(exit)

0 comments on commit e74fc4d

Please sign in to comment.