MCPcopy Create free account
hub / github.com/Z3Prover/z3 / treeExample

Method treeExample

examples/java/JavaGenericExample.java:1461–1517  ·  view source on GitHub ↗
(Context ctx)

Source from the content-addressed store, hash-verified

1459 // / Create a binary tree datatype.
1460
1461 @SuppressWarnings("unchecked")
1462 public <Tree> void treeExample(Context ctx) throws TestFailedException
1463 {
1464 System.out.println("TreeExample");
1465 Log.append("TreeExample");
1466
1467 String[] head_tail = new String[] { "car", "cdr" };
1468 Sort[] sorts = new Sort[] { null, null };
1469 int[] sort_refs = new int[] { 0, 0 };
1470 Constructor<Tree> nil_con, cons_con;
1471
1472 nil_con = ctx.mkConstructor("nil", "is_nil", null, null, null);
1473 cons_con = ctx.mkConstructor("cons", "is_cons", head_tail, sorts,
1474 sort_refs);
1475 Constructor<Tree>[] constructors = new Constructor[] { nil_con, cons_con };
1476
1477 DatatypeSort<Tree> cell = ctx.mkDatatypeSort("cell", constructors);
1478
1479 FuncDecl<DatatypeSort<Tree>> nil_decl = nil_con.ConstructorDecl();
1480 FuncDecl<BoolSort> is_nil_decl = nil_con.getTesterDecl();
1481 FuncDecl<DatatypeSort<Tree>> cons_decl = cons_con.ConstructorDecl();
1482 FuncDecl<BoolSort> is_cons_decl = cons_con.getTesterDecl();
1483 FuncDecl<?>[] cons_accessors = cons_con.getAccessorDecls();
1484 FuncDecl<?> car_decl = cons_accessors[0];
1485 FuncDecl<?> cdr_decl = cons_accessors[1];
1486
1487 Expr<DatatypeSort<Tree>> nil = ctx.mkConst(nil_decl);
1488 Expr<DatatypeSort<Tree>> l1 = ctx.mkApp(cons_decl, nil, nil);
1489 Expr<DatatypeSort<Tree>> l2 = ctx.mkApp(cons_decl, l1, nil);
1490
1491 /* nil != cons(nil, nil) */
1492 prove(ctx, ctx.mkNot(ctx.mkEq(nil, l1)), false);
1493
1494 /* cons(x,u) = cons(x, v) => u = v */
1495 Expr<DatatypeSort<Tree>> u = ctx.mkConst("u", cell);
1496 Expr<DatatypeSort<Tree>> v = ctx.mkConst("v", cell);
1497 Expr<DatatypeSort<Tree>> x = ctx.mkConst("x", cell);
1498 Expr<DatatypeSort<Tree>> y = ctx.mkConst("y", cell);
1499 l1 = ctx.mkApp(cons_decl, x, u);
1500 l2 = ctx.mkApp(cons_decl, y, v);
1501 prove(ctx, ctx.mkImplies(ctx.mkEq(l1, l2), ctx.mkEq(u, v)), false);
1502 prove(ctx, ctx.mkImplies(ctx.mkEq(l1, l2), ctx.mkEq(x, y)), false);
1503
1504 /* is_nil(u) or is_cons(u) */
1505 prove(ctx, ctx.mkOr(ctx.mkApp(is_nil_decl, u), ctx.mkApp(is_cons_decl, u)), false);
1506
1507 /* occurs check u != cons(x,u) */
1508 prove(ctx, ctx.mkNot(ctx.mkEq(u, l1)), false);
1509
1510 /* destructors: is_cons(u) => u = cons(car(u),cdr(u)) */
1511 BoolExpr fml1 = ctx.mkEq(u, ctx.mkApp(cons_decl, ctx.mkApp(car_decl, u), ctx.mkApp(cdr_decl, u)));
1512 BoolExpr fml = ctx.mkImplies(ctx.mkApp(is_cons_decl, u), fml1);
1513 System.out.printf("Formula %s%n", fml);
1514 prove(ctx, fml, false);
1515
1516 disprove(ctx, fml1, false);
1517 }
1518

Callers 1

mainMethod · 0.95

Calls 14

appendMethod · 0.95
proveMethod · 0.95
disproveMethod · 0.95
mkConstructorMethod · 0.80
mkDatatypeSortMethod · 0.80
ConstructorDeclMethod · 0.80
getAccessorDeclsMethod · 0.80
mkConstMethod · 0.80
mkAppMethod · 0.80
mkNotMethod · 0.80
mkEqMethod · 0.80
mkImpliesMethod · 0.80

Tested by

no test coverage detected