3

I am trying to learn Z3 using the java API, since there is not documentation I've been looking at the C API documentation but until now I can't find a clear example of how to use some basic functions.

I am trying to encode this Z3 code (which works in the online version)

;general options for getting values when sat
(set-option :produce-models true)
(set-option :produce-assignments true)

;declaring new sorts
(declare-sort Task)
(declare-sort User)

;function for assign an specific user
(declare-fun assignUser (Task) User)
;creating a relation between a task and a usert
(declare-fun TaskUser (Task User) Bool)

;stablishing order
(declare-fun mustPrecede (Task Task) Bool)

(assert(forall((t Task)) (not (mustPrecede t t))))
(assert(forall((t1 Task)(t2 Task)(t3 Task)) (implies (and (mustPrecede t1 t2)(mustPrecede t2 t3)) (mustPrecede t1 t3))))        

;asserting that all task must have one assigned user
(assert(forall((t Task)(u User)) (TaskUser t u)))
;asserting that all task must have one assigned user
;(assert(forall((t1 Task)(t2 Task)) (not(= (assignUser t1) (assignUser t2))))) 

Until now I just manage to declare the uninterpreted sorts and to declare my functions as follows

      HashMap<String, String> cfg = new HashMap<String, String>();
      cfg.put("proof", "true");
      cfg.put("auto-config", "false");
      Context ctx = new Context(cfg);

    //cfg.put("model", "true");
    Sort USER = ctx.mkUninterpretedSort("USER");
    Sort TASK = ctx.mkUninterpretedSort("TASK");

    FuncDecl assignUser = ctx.mkFuncDecl("assignUser", TASK, USER);
    FuncDecl TaskUser = ctx.mkFuncDecl("TaskUser", new Sort[] { TASK, USER }, ctx.mkBoolSort());
    FuncDecl mustPrecede = ctx.mkFuncDecl("mustPrecede", new Sort[]{TASK,TASK}, ctx.mkBoolSort());

but I can't find an example to express

(assert(forall((t Task)) (not (mustPrecede t t))))
(assert(forall((t1 Task)(t2 Task)(t3 Task)) (implies (and (mustPrecede t1 t2) (mustPrecede t2 t3)) (mustPrecede t1 t3))))

;asserting that all task must have one assigned user
(assert(forall((t Task)(u User)) (TaskUser t u)))
;asserting that all task must have one assigned user
;(assert(forall((t1 Task)(t2 Task)) (not(= (assignUser t1) (assignUser t2))))) 

Can somebody help my with this please? which is the way to express this asserts-foralls with the java API?

NissimL
  • 148
  • 8
user3723800
  • 145
  • 7

2 Answers2

2

There is a set of samples with various Java calls as part of the examples in the Z3 code:

https://z3.codeplex.com/SourceControl/latest#examples/java/JavaExample.java

They include building and checking quantified formulas.

Nikolaj Bjorner
  • 8,229
  • 14
  • 15
  • Thank you for the link, actually I have those examples, but in them I couldn´t find how to use the returning value of a function inside a for all.. for instance.. For expressing (assert(forall((t Task)) (not (mustPrecede t t)))) I am creating Expr Task1 = ctx.mkConst("TAKS1",TASK); Expr[] bounds = new Expr[] { Task1 }; (as shown in the examples) and then I need to create a expression for the body.. but I don't know how to use the mustPrece function with a the Task1 parameter .. can you help me with one example? – user3723800 Jul 30 '14 at 08:52
  • The FuncDecl object has a function called Apply, which takes a set of Expr's as argument; those arguments can of course be quantified variables. The quantifierExample2 function illustrates the use of either constants ("x") as bound variables, or indexed variables (mkBound(...)). – Christoph Wintersteiger Jul 30 '14 at 13:06
1
//creating uninterpreted sorts for function declaration
Sort User = ctx.mkUninterpretedSort("User");
Sort Task = ctx.mkUninterpretedSort("Task");
//function declaration
FuncDecl assignUser = ctx.mkFuncDecl("assignUser", Task, User);
FuncDecl TaskUser = ctx.mkFuncDecl("TaskUser", new Sort[] { Task, User }, ctx.mkBoolSort());
FuncDecl mustPrecede = ctx.mkFuncDecl("mustPrecede", new Sort[]{Task,Task}, ctx.mkBoolSort());

//task for using in quatifiers
Expr task = ctx.mkConst("t", ctx.mkUninterpretedSort(ctx.mkSymbol("Task")));

// creating (assert(forall((t Task)) (not (mustPrecede t t))))
//just one task is needed
Sort[] Tasks = new Sort[1];
Tasks[0] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));
//setting the name for the task
Symbol[] namess = new Symbol[1];
namess[0] =  ctx.mkSymbol("t");
//Creating a map between mustPrecede and  its two parameters
Expr mtt = ctx.mkApp(mustPrecede, task,task);
//acreating not
Expr body = ctx.mkNot((BoolExpr)mtt);

Expr mustPrecedett = ctx.mkForall(Tasks, namess, body, 1, null, null,
ctx.mkSymbol("Q1"), ctx.mkSymbol("skid1"));

System.out.println("Quantifier mustPrecedett: " + mustPrecedett.toString());

//creating (assert(forall((t1 Task)(t2 Task)(t3 Task)) (implies (and (mustPrecede t1 t2)(mustPrecede t2 t3)) (mustPrecede t1 t3))))

//tree taks will be neede
Sort[] tTask = new Sort[3];
tTask[0] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));
tTask[1] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));
tTask[2] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));

//setting the names for the tasks
Symbol[] Tnames = new Symbol[3];
Tnames[0] =  ctx.mkSymbol("t1");
Tnames[1] =  ctx.mkSymbol("t2");
Tnames[2] =  ctx.mkSymbol("t3");

//creating tree diferent tasks for the relations
Expr t1 = ctx.mkConst("t1", ctx.mkUninterpretedSort(ctx.mkSymbol("Task")));
Expr t2 = ctx.mkConst("t2", ctx.mkUninterpretedSort(ctx.mkSymbol("Task")));
Expr t3 = ctx.mkConst("t3", ctx.mkUninterpretedSort(ctx.mkSymbol("Task")));
//creating mappins
Expr mt1t2 = ctx.mkApp(mustPrecede, t1,t2);
Expr mt2t3 = ctx.mkApp(mustPrecede, t2,t3);
Expr mt1t3 = ctx.mkApp(mustPrecede, t1,t3);
//Creating the relation between them        
Expr tbody2= ctx.mkImplies(ctx.mkAnd((BoolExpr)mt1t2,(BoolExpr) mt2t3), (BoolExpr) mt1t3);  
//building quatifier
Expr tra = ctx.mkForall(tTask, Tnames, tbody2, 1, null, null,ctx.mkSymbol("Q1"), ctx.mkSymbol("skid1"));

System.out.println("Quantifier tra: " + tra.toString());



//creating (assert(forall((t Task)(u User)) (TaskUser t u)))
//one user and one task
Sort[] userTask = new Sort[2];
userTask[0] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));
userTask[1] =ctx.mkUninterpretedSort(ctx.mkSymbol("User"));
//setting names
Symbol[] userNames = new Symbol[2];
userNames[0] =  ctx.mkSymbol("t");
userNames[1] =  ctx.mkSymbol("u");
//creating one cost for the user        
Expr user = ctx.mkConst("u", ctx.mkUninterpretedSort(ctx.mkSymbol("User")));
//creating the relation between them and TaskUser funct
Expr uTask = ctx.mkApp(TaskUser, task,user);
//building quatificar
Expr userTaskExpr = ctx.mkForall(userTask, userNames, uTask, 1, null, null,ctx.mkSymbol("Q1"), ctx.mkSymbol("skid1"));

System.out.println("Quantifier userTaskExpr: " + userTaskExpr.toString());
user3723800
  • 145
  • 7