mirror of
https://github.com/Z3Prover/z3
synced 2025-06-24 23:03:41 +00:00
Merge branch 'fpa-api' of https://git01.codeplex.com/z3 into unstable
Signed-off-by: Christoph M. Wintersteiger <cwinter@microsoft.com> Conflicts: src/tactic/portfolio/default_tactic.cpp
This commit is contained in:
commit
d56d63e3e8
110 changed files with 11348 additions and 4084 deletions
|
@ -2595,6 +2595,100 @@ void substitute_vars_example() {
|
|||
Z3_del_context(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Demonstrates some basic features of the FloatingPoint theory.
|
||||
*/
|
||||
|
||||
void fpa_example() {
|
||||
Z3_config cfg;
|
||||
Z3_context ctx;
|
||||
Z3_sort double_sort, rm_sort;
|
||||
Z3_symbol s_rm, s_x, s_y, s_x_plus_y;
|
||||
Z3_ast rm, x, y, n, x_plus_y, c1, c2, c3, c4, c5, c6;
|
||||
|
||||
printf("\nFPA-example\n");
|
||||
LOG_MSG("FPA-example");
|
||||
|
||||
cfg = Z3_mk_config();
|
||||
ctx = Z3_mk_context(cfg);
|
||||
Z3_del_config(cfg);
|
||||
|
||||
double_sort = Z3_mk_fpa_sort(ctx, 11, 53);
|
||||
rm_sort = Z3_mk_fpa_rounding_mode_sort(ctx);
|
||||
|
||||
{
|
||||
// Show that there are x, y s.t. (x + y) = 42.0 (with rounding mode).
|
||||
s_rm = Z3_mk_string_symbol(ctx, "rm");
|
||||
rm = Z3_mk_const(ctx, s_rm, rm_sort);
|
||||
s_x = Z3_mk_string_symbol(ctx, "x");
|
||||
s_y = Z3_mk_string_symbol(ctx, "y");
|
||||
x = Z3_mk_const(ctx, s_x, double_sort);
|
||||
y = Z3_mk_const(ctx, s_y, double_sort);
|
||||
n = Z3_mk_fpa_numeral_double(ctx, 42.0, double_sort);
|
||||
|
||||
s_x_plus_y = Z3_mk_string_symbol(ctx, "x_plus_y");
|
||||
x_plus_y = Z3_mk_const(ctx, s_x_plus_y, double_sort);
|
||||
c1 = Z3_mk_eq(ctx, x_plus_y, Z3_mk_fpa_add(ctx, rm, x, y));
|
||||
|
||||
Z3_ast args[2] = { c1, Z3_mk_eq(ctx, x_plus_y, n) };
|
||||
c2 = Z3_mk_and(ctx, 2, (Z3_ast*)&args);
|
||||
|
||||
Z3_ast args2[2] = { c2, Z3_mk_not(ctx, Z3_mk_eq(ctx, rm, Z3_mk_fpa_rtz(ctx))) };
|
||||
c3 = Z3_mk_and(ctx, 2, (Z3_ast*)&args2);
|
||||
|
||||
Z3_ast and_args[3] = {
|
||||
Z3_mk_not(ctx, Z3_mk_fpa_is_zero(ctx, y)),
|
||||
Z3_mk_not(ctx, Z3_mk_fpa_is_nan(ctx, y)),
|
||||
Z3_mk_not(ctx, Z3_mk_fpa_is_infinite(ctx, y)) };
|
||||
Z3_ast args3[2] = { c3, Z3_mk_and(ctx, 3, and_args) };
|
||||
c4 = Z3_mk_and(ctx, 2, (Z3_ast*)&args3);
|
||||
|
||||
printf("c4: %s\n", Z3_ast_to_string(ctx, c4));
|
||||
Z3_push(ctx);
|
||||
Z3_assert_cnstr(ctx, c4);
|
||||
check(ctx, Z3_L_TRUE);
|
||||
Z3_pop(ctx, 1);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
// Show that the following are equal:
|
||||
// (fp #b0 #b10000000001 #xc000000000000)
|
||||
// ((_ to_fp 11 53) #x401c000000000000))
|
||||
// ((_ to_fp 11 53) RTZ 1.75 2)))
|
||||
// ((_ to_fp 11 53) RTZ 7.0)))
|
||||
Z3_ast args3[3];
|
||||
|
||||
Z3_push(ctx);
|
||||
c1 = Z3_mk_fpa_fp(ctx,
|
||||
Z3_mk_numeral(ctx, "0", Z3_mk_bv_sort(ctx, 1)),
|
||||
Z3_mk_numeral(ctx, "1025", Z3_mk_bv_sort(ctx, 11)),
|
||||
Z3_mk_numeral(ctx, "3377699720527872", Z3_mk_bv_sort(ctx, 52)));
|
||||
c2 = Z3_mk_fpa_to_fp_bv(ctx,
|
||||
Z3_mk_numeral(ctx, "4619567317775286272", Z3_mk_bv_sort(ctx, 64)),
|
||||
Z3_mk_fpa_sort(ctx, 11, 53));
|
||||
c3 = Z3_mk_fpa_to_fp_real_int(ctx,
|
||||
Z3_mk_fpa_rtz(ctx),
|
||||
Z3_mk_numeral(ctx, "1.75", Z3_mk_real_sort(ctx)),
|
||||
Z3_mk_numeral(ctx, "2", Z3_mk_int_sort(ctx)),
|
||||
Z3_mk_fpa_sort(ctx, 11, 53));
|
||||
c4 = Z3_mk_fpa_to_fp_real(ctx,
|
||||
Z3_mk_fpa_rtz(ctx),
|
||||
Z3_mk_numeral(ctx, "7.0", Z3_mk_real_sort(ctx)),
|
||||
Z3_mk_fpa_sort(ctx, 11, 53));
|
||||
args3[0] = Z3_mk_eq(ctx, c1, c2);
|
||||
args3[1] = Z3_mk_eq(ctx, c1, c3);
|
||||
args3[2] = Z3_mk_eq(ctx, c1, c4);
|
||||
c5 = Z3_mk_and(ctx, 3, args3);
|
||||
|
||||
printf("c5: %s\n", Z3_ast_to_string(ctx, c5));
|
||||
Z3_assert_cnstr(ctx, c5);
|
||||
check(ctx, Z3_L_TRUE);
|
||||
Z3_pop(ctx, 1);
|
||||
}
|
||||
|
||||
Z3_del_context(ctx);
|
||||
}
|
||||
|
||||
/*@}*/
|
||||
/*@}*/
|
||||
|
@ -2640,5 +2734,6 @@ int main() {
|
|||
smt2parser_example();
|
||||
substitute_example();
|
||||
substitute_vars_example();
|
||||
fpa_example();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -2028,6 +2028,84 @@ namespace test_mapi
|
|||
// Console.WriteLine("{0}", ctx.MkEq(s1, t1));
|
||||
}
|
||||
|
||||
public static void FloatingPointExample1(Context ctx)
|
||||
{
|
||||
Console.WriteLine("FloatingPointExample1");
|
||||
|
||||
FPSort s = ctx.MkFPSort(11, 53);
|
||||
Console.WriteLine("Sort: {0}", s);
|
||||
|
||||
FPNum x = (FPNum)ctx.MkNumeral("-1e1", s); /* -1 * 10^1 = -10 */
|
||||
FPNum y = (FPNum)ctx.MkNumeral("-10", s); /* -10 */
|
||||
FPNum z = (FPNum)ctx.MkNumeral("-1.25p3", s); /* -1.25 * 2^3 = -1.25 * 8 = -10 */
|
||||
Console.WriteLine("x={0}; y={1}; z={2}", x.ToString(), y.ToString(), z.ToString());
|
||||
|
||||
BoolExpr a = ctx.MkAnd(ctx.MkFPEq(x, y), ctx.MkFPEq(y, z));
|
||||
Check(ctx, ctx.MkNot(a), Status.UNSATISFIABLE);
|
||||
|
||||
/* nothing is equal to NaN according to floating-point
|
||||
* equality, so NaN == k should be unsatisfiable. */
|
||||
FPExpr k = (FPExpr)ctx.MkConst("x", s);
|
||||
FPExpr nan = ctx.MkFPNaN(s);
|
||||
|
||||
/* solver that runs the default tactic for QF_FP. */
|
||||
Solver slvr = ctx.MkSolver("QF_FP");
|
||||
slvr.Add(ctx.MkFPEq(nan, k));
|
||||
if (slvr.Check() != Status.UNSATISFIABLE)
|
||||
throw new TestFailedException();
|
||||
Console.WriteLine("OK, unsat:" + Environment.NewLine + slvr);
|
||||
|
||||
/* NaN is equal to NaN according to normal equality. */
|
||||
slvr = ctx.MkSolver("QF_FP");
|
||||
slvr.Add(ctx.MkEq(nan, nan));
|
||||
if (slvr.Check() != Status.SATISFIABLE)
|
||||
throw new TestFailedException();
|
||||
Console.WriteLine("OK, sat:" + Environment.NewLine + slvr);
|
||||
|
||||
/* Let's prove -1e1 * -1.25e3 == +100 */
|
||||
x = (FPNum)ctx.MkNumeral("-1e1", s);
|
||||
y = (FPNum)ctx.MkNumeral("-1.25p3", s);
|
||||
FPExpr x_plus_y = (FPExpr)ctx.MkConst("x_plus_y", s);
|
||||
FPNum r = (FPNum)ctx.MkNumeral("100", s);
|
||||
slvr = ctx.MkSolver("QF_FP");
|
||||
|
||||
slvr.Add(ctx.MkEq(x_plus_y, ctx.MkFPMul(ctx.MkFPRoundNearestTiesToAway(), x, y)));
|
||||
slvr.Add(ctx.MkNot(ctx.MkFPEq(x_plus_y, r)));
|
||||
if (slvr.Check() != Status.UNSATISFIABLE)
|
||||
throw new TestFailedException();
|
||||
Console.WriteLine("OK, unsat:" + Environment.NewLine + slvr);
|
||||
}
|
||||
|
||||
public static void FloatingPointExample2(Context ctx)
|
||||
{
|
||||
Console.WriteLine("FloatingPointExample2");
|
||||
FPSort double_sort = ctx.MkFPSort(11, 53);
|
||||
FPRMSort rm_sort = ctx.MkFPRoundingModeSort();
|
||||
|
||||
FPRMExpr rm = (FPRMExpr)ctx.MkConst(ctx.MkSymbol("rm"), rm_sort);
|
||||
BitVecExpr x = (BitVecExpr)ctx.MkConst(ctx.MkSymbol("x"), ctx.MkBitVecSort(64));
|
||||
FPExpr y = (FPExpr)ctx.MkConst(ctx.MkSymbol("y"), double_sort);
|
||||
FPExpr fp_val = ctx.MkFP(42, double_sort);
|
||||
|
||||
BoolExpr c1 = ctx.MkEq(y, fp_val);
|
||||
BoolExpr c2 = ctx.MkEq(x, ctx.MkFPToBV(rm, y, 64, false));
|
||||
BoolExpr c3 = ctx.MkEq(x, ctx.MkBV(42, 64));
|
||||
BoolExpr c4 = ctx.MkEq(ctx.MkNumeral(42, ctx.RealSort), ctx.MkFPToReal(fp_val));
|
||||
BoolExpr c5 = ctx.MkAnd(c1, c2, c3, c4);
|
||||
Console.WriteLine("c5 = " + c5);
|
||||
|
||||
/* Generic solver */
|
||||
Solver s = ctx.MkSolver();
|
||||
s.Assert(c5);
|
||||
|
||||
Console.WriteLine(s);
|
||||
|
||||
if (s.Check() != Status.SATISFIABLE)
|
||||
throw new TestFailedException();
|
||||
|
||||
Console.WriteLine("OK, model: {0}", s.Model.ToString());
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
try
|
||||
|
@ -2069,6 +2147,8 @@ namespace test_mapi
|
|||
FindSmallModelExample(ctx);
|
||||
SimplifierExample(ctx);
|
||||
FiniteDomainExample(ctx);
|
||||
FloatingPointExample1(ctx);
|
||||
FloatingPointExample2(ctx);
|
||||
}
|
||||
|
||||
// These examples need proof generation turned on.
|
||||
|
|
|
@ -1095,7 +1095,7 @@ class JavaExample
|
|||
|
||||
// / Shows how to use Solver(logic)
|
||||
|
||||
// / <param name="ctx"></param>
|
||||
// / @param ctx
|
||||
void logicExample(Context ctx) throws Z3Exception, TestFailedException
|
||||
{
|
||||
System.out.println("LogicTest");
|
||||
|
@ -2157,6 +2157,86 @@ class JavaExample
|
|||
// System.out.println(ctx.mkEq(s1, t1));
|
||||
}
|
||||
|
||||
public void floatingPointExample1(Context ctx) throws Z3Exception, TestFailedException
|
||||
{
|
||||
System.out.println("FloatingPointExample1");
|
||||
Log.append("FloatingPointExample1");
|
||||
|
||||
FPSort s = ctx.mkFPSort(11, 53);
|
||||
System.out.println("Sort: " + s);
|
||||
|
||||
FPNum x = (FPNum)ctx.mkNumeral("-1e1", s); /* -1 * 10^1 = -10 */
|
||||
FPNum y = (FPNum)ctx.mkNumeral("-10", s); /* -10 */
|
||||
FPNum z = (FPNum)ctx.mkNumeral("-1.25p3", s); /* -1.25 * 2^3 = -1.25 * 8 = -10 */
|
||||
System.out.println("x=" + x.toString() +
|
||||
"; y=" + y.toString() +
|
||||
"; z=" + z.toString());
|
||||
|
||||
BoolExpr a = ctx.mkAnd(ctx.mkFPEq(x, y), ctx.mkFPEq(y, z));
|
||||
check(ctx, ctx.mkNot(a), Status.UNSATISFIABLE);
|
||||
|
||||
/* nothing is equal to NaN according to floating-point
|
||||
* equality, so NaN == k should be unsatisfiable. */
|
||||
FPExpr k = (FPExpr)ctx.mkConst("x", s);
|
||||
FPExpr nan = ctx.mkFPNaN(s);
|
||||
|
||||
/* solver that runs the default tactic for QF_FP. */
|
||||
Solver slvr = ctx.mkSolver("QF_FP");
|
||||
slvr.add(ctx.mkFPEq(nan, k));
|
||||
if (slvr.check() != Status.UNSATISFIABLE)
|
||||
throw new TestFailedException();
|
||||
System.out.println("OK, unsat:" + System.getProperty("line.separator") + slvr);
|
||||
|
||||
/* NaN is equal to NaN according to normal equality. */
|
||||
slvr = ctx.mkSolver("QF_FP");
|
||||
slvr.add(ctx.mkEq(nan, nan));
|
||||
if (slvr.check() != Status.SATISFIABLE)
|
||||
throw new TestFailedException();
|
||||
System.out.println("OK, sat:" + System.getProperty("line.separator") + slvr);
|
||||
|
||||
/* Let's prove -1e1 * -1.25e3 == +100 */
|
||||
x = (FPNum)ctx.mkNumeral("-1e1", s);
|
||||
y = (FPNum)ctx.mkNumeral("-1.25p3", s);
|
||||
FPExpr x_plus_y = (FPExpr)ctx.mkConst("x_plus_y", s);
|
||||
FPNum r = (FPNum)ctx.mkNumeral("100", s);
|
||||
slvr = ctx.mkSolver("QF_FP");
|
||||
|
||||
slvr.add(ctx.mkEq(x_plus_y, ctx.mkFPMul(ctx.mkFPRoundNearestTiesToAway(), x, y)));
|
||||
slvr.add(ctx.mkNot(ctx.mkFPEq(x_plus_y, r)));
|
||||
if (slvr.check() != Status.UNSATISFIABLE)
|
||||
throw new TestFailedException();
|
||||
System.out.println("OK, unsat:" + System.getProperty("line.separator") + slvr);
|
||||
}
|
||||
|
||||
public void floatingPointExample2(Context ctx) throws Z3Exception, TestFailedException
|
||||
{
|
||||
System.out.println("FloatingPointExample2");
|
||||
Log.append("FloatingPointExample2");
|
||||
FPSort double_sort = ctx.mkFPSort(11, 53);
|
||||
FPRMSort rm_sort = ctx.mkFPRoundingModeSort();
|
||||
|
||||
FPRMExpr rm = (FPRMExpr)ctx.mkConst(ctx.mkSymbol("rm"), rm_sort);
|
||||
BitVecExpr x = (BitVecExpr)ctx.mkConst(ctx.mkSymbol("x"), ctx.mkBitVecSort(64));
|
||||
FPExpr y = (FPExpr)ctx.mkConst(ctx.mkSymbol("y"), double_sort);
|
||||
FPExpr fp_val = ctx.mkFP(42, double_sort);
|
||||
|
||||
BoolExpr c1 = ctx.mkEq(y, fp_val);
|
||||
BoolExpr c2 = ctx.mkEq(x, ctx.mkFPToBV(rm, y, 64, false));
|
||||
BoolExpr c3 = ctx.mkEq(x, ctx.mkBV(42, 64));
|
||||
BoolExpr c4 = ctx.mkEq(ctx.mkNumeral(42, ctx.getRealSort()), ctx.mkFPToReal(fp_val));
|
||||
BoolExpr c5 = ctx.mkAnd(c1, c2, c3, c4);
|
||||
System.out.println("c5 = " + c5);
|
||||
|
||||
/* Generic solver */
|
||||
Solver s = ctx.mkSolver();
|
||||
s.add(c5);
|
||||
|
||||
if (s.check() != Status.SATISFIABLE)
|
||||
throw new TestFailedException();
|
||||
|
||||
System.out.println("OK, model: " + s.getModel().toString());
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
JavaExample p = new JavaExample();
|
||||
|
@ -2200,6 +2280,8 @@ class JavaExample
|
|||
p.findSmallModelExample(ctx);
|
||||
p.simplifierExample(ctx);
|
||||
p.finiteDomainExample(ctx);
|
||||
p.floatingPointExample1(ctx);
|
||||
p.floatingPointExample2(ctx);
|
||||
}
|
||||
|
||||
{ // These examples need proof generation turned on.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue