smt https://www.e-learn.cn/tag/smt zh-hans CVC4 minimize/maximize model optimization https://www.e-learn.cn/topic/4101773 <span>CVC4 minimize/maximize model optimization</span> <span><span lang="" about="/user/82" typeof="schema:Person" property="schema:name" datatype="">不想你离开。</span></span> <span>2021-02-11 18:26:11</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Does CVC4 an option to maximize or minimize the result model for bitvectors as Z3 does?</p> <p>Thanks.</p> <br /><h3>回答1:</h3><br /><p>Unfortunately, CVC4 does not (yet) support optimization. For bitvectors, you can always do it yourself using multiple queries and binary search, but it's not built-in.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/37304885/cvc4-minimize-maximize-model-optimization</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/optimization" hreflang="zh-hans">optimization</a></div> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> <div class="field--item"><a href="/tag/cvc4" hreflang="zh-hans">cvc4</a></div> </div> </div> Thu, 11 Feb 2021 10:26:11 +0000 不想你离开。 4101773 at https://www.e-learn.cn CVC4 minimize/maximize model optimization https://www.e-learn.cn/topic/4101771 <span>CVC4 minimize/maximize model optimization</span> <span><span lang="" about="/user/240" typeof="schema:Person" property="schema:name" datatype="">北慕城南</span></span> <span>2021-02-11 18:26:07</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Does CVC4 an option to maximize or minimize the result model for bitvectors as Z3 does?</p> <p>Thanks.</p> <br /><h3>回答1:</h3><br /><p>Unfortunately, CVC4 does not (yet) support optimization. For bitvectors, you can always do it yourself using multiple queries and binary search, but it's not built-in.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/37304885/cvc4-minimize-maximize-model-optimization</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/optimization" hreflang="zh-hans">optimization</a></div> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> <div class="field--item"><a href="/tag/cvc4" hreflang="zh-hans">cvc4</a></div> </div> </div> Thu, 11 Feb 2021 10:26:07 +0000 北慕城南 4101771 at https://www.e-learn.cn Z3 Solver Java API: Unexpected behaviour https://www.e-learn.cn/topic/4082753 <span>Z3 Solver Java API: Unexpected behaviour</span> <span><span lang="" about="/user/139" typeof="schema:Person" property="schema:name" datatype="">删除回忆录丶</span></span> <span>2021-02-09 07:54:34</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>By adding conditions to the solver, I want to check with "solver.check()", whether there exists a solution or not. Therefore, I created a simple example to find a solution for t1. I know that there is a solution for t1, namely t1 = 0. Nevertheless, the solver has not the status "SATISFIABLE". </p> <pre><code>public static void main(String[] args) { int h_max = 7; HashMap&lt;String, String&gt; cfg = new HashMap&lt;String, String&gt;(); cfg.put("model", "true"); Context ctx = new Context(cfg); FPSort s = ctx.mkFPSort(5, 20); Solver solver = ctx.mkSolver(); Model model = null; // Initialize constants RealExpr half = ctx.mkFPToReal(ctx.mkFP(0.5, s)); RealExpr g = ctx.mkFPToReal(ctx.mkFP(9.81, s)); RealExpr hmax = ctx.mkInt2Real(ctx.mkInt(h_max)); RealExpr v = ctx.mkFPToReal(ctx.mkFP((Math.sqrt(2*h_max*9.81)), s)); // Create query Information RealExpr q2 = ctx.mkReal(1); RealExpr q2Min = (RealExpr) ctx.mkSub(q2, half); RealExpr q2Max = (RealExpr) ctx.mkAdd(q2, half); // Initialize constraints RealExpr tmax = ctx.mkFPToReal(ctx.mkFP((Math.sqrt(2*h_max/9.81)), s)); RealExpr t0 = ctx.mkReal(0); // Initialize sampling interval RealExpr ts = ctx.mkFPToReal(ctx.mkFP(Math.sqrt(2*h_max/9.81)+0.1, s)); // Variable t1 RealExpr t1 = ctx.mkRealConst("t1"); // 0 &lt;= t1 &lt;= tmax BoolExpr c1 = ctx.mkGe(t1, t0); BoolExpr c2 = ctx.mkLe(t1,tmax); // Elapsed Times RealExpr tE = (RealExpr) ctx.mkAdd(ts, t1); // Add conditions to solver solver.add(c1); solver.add(c2); // Calculating tE2 % tmax, since tE2 &gt; tmax RealExpr quotient = (RealExpr) ctx.mkDiv(tE, tmax); IntExpr factor = ctx.mkReal2Int(quotient); RealExpr t2 = (RealExpr) ctx.mkSub(tE, ctx.mkMul(factor, tmax)); // Calculating the observation h2 with t2. RealExpr h2 = (RealExpr) ctx.mkSub(ctx.mkMul(v,t2), ctx.mkMul(half, t2, t2, g)); // Defining constraint q2Min &lt;= h2 &lt; q2Max BoolExpr c3 = ctx.mkAnd(ctx.mkGe(h2, q2Min),ctx.mkLt(h2, q2Max)); solver.add(c3); //System.out.println("solver c1: " + solver.check(c1)); //System.out.println("solver c2: " + solver.check(c2)); //System.out.println("solver c3: " + solver.check(c3)); if (solver.check() == Status.SATISFIABLE) { model = solver.getModel(); System.out.println("System is Satisfiable"); } else { System.out.println("Unsatisfiable"); } ctx.close(); } </code></pre> <p>I discovered some unexpected behaviour. If I try to check conditions before I do "solver.check()", for example</p> <pre><code>System.out.println("solver c2: " + solver.check(c2)); System.out.println("solver c3: " + solver.check(c3)); </code></pre> <p>it outputs: </p> <pre><code>solver c2: UNKNOWN solver c3: UNKNOWN </code></pre> <p>and suddenly, the solver's status is "SATISFIABLE". But if I only check one condition beforehand, the status is still "UNSATISFIABLE".</p> <p>Other than that, if I change from</p> <pre><code>t1 = ctx.mkRealConst("t1"); </code></pre> <p>to </p> <pre><code>t1 = ctx.mkReal(0); </code></pre> <p>the solver also finds a solution and the solver status is "SATISFIABLE".</p> <p>Why does the solver have this behaviour and how could I possibly make the solver find a solution? Are there any alternative ways that I could try?</p> <br /><h3>回答1:</h3><br /><p>In general, when you write:</p> <pre><code>solver.check(c1) </code></pre> <p>you are <em>not</em> asking z3 to check that <code>c1</code> is satisfiable. What you are asking z3 to do is to check that all the assertions you put in are satisfiable assuming <code>c1</code> is true. This is called "check under assumptions" and is documented here: https://z3prover.github.io/api/html/classcom_1_1microsoft_1_1z3_1_1_solver.html#a71882930969215081ef020ec8fec45f3</p> <p>This can be rather confusing at first, but it allows checking satisfiability under assumptions without having to assert those assumptions globally.</p> <p>Regarding why you get <code>UNKNOWN</code>. You are using floating-point arithmetic, and mixing and matching it with real's. That will create a lot of non-linear constraints, something that z3 doesn't really deal with all that well. Try to keep the logics separated: Don't mix reals with floats if you can. (Ask a separate question if you have questions regarding how to model things.)</p> <p>Finally, writing <code>t1 = ctx.mkReal(0)</code> is very different than writing <code>t1 = ctx.mkRealConst("t1")</code>. The first one is much simpler to deal with: <code>t1</code> is just 0. In the second case it's a variable. So, it isn't surprising at all that the former leads to much easier problems to handle for z3. Again, there's no silver bullet but start with not-mixing the logics this way: If you want to work on floating-point, keep everything in that land. If you want to work with real values, keep everything real valued. You'll get much more mileage that way. If you have to mix the two, then you'll most likely see <code>UNKNOWN</code> results.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/61009461/z3-solver-java-api-unexpected-behaviour</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/solver" hreflang="zh-hans">solver</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> </div> </div> Mon, 08 Feb 2021 23:54:34 +0000 删除回忆录丶 4082753 at https://www.e-learn.cn Modeling a small programming language and analysis in SMT-LIB using datatypes and forall https://www.e-learn.cn/topic/4056350 <span>Modeling a small programming language and analysis in SMT-LIB using datatypes and forall</span> <span><span lang="" about="/user/172" typeof="schema:Person" property="schema:name" datatype="">无人久伴</span></span> <span>2021-02-05 11:58:20</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am trying to model a small programming language in SMT-LIB 2. My intent is to express some program analysis problems and solve them with Z3. I think I am misunderstanding the <code>forall</code> statement though. Here is a snippet of my code.</p> <pre><code>; barriers.smt2 (declare-datatype Barrier ((barrier (proc Int) (rank Int) (group Int) (complete-time Int)))) ; barriers in the same group complete at the same time (assert (forall ((b1 Barrier) (b2 Barrier)) (=&gt; (= (group b1) (group b2)) (= (complete-time b1) (complete-time b2))))) (check-sat) </code></pre> <p>When I run <code>z3 -smt2 barriers.smt2</code> I get <code>unsat</code> as the result. I am thinking that an instance of my analysis problem would be a series of <code>forall</code> assertions like the above and a series of const declarations with assertions that describe the input program.</p> <pre><code>(declare-const b00 Barrier) (assert (= (proc b00) 0)) (assert (= (rank b00) 0)) ... </code></pre> <p>But apparently I am using the <code>forall</code> expression incorrectly because I expected z3 to decide that there was a satisfying model for that assertion. What am I missing?</p> <br /><h3>回答1:</h3><br /><p>When you declare a <code>datatype</code> like this:</p> <pre><code>(declare-datatype Barrier ((barrier (proc Int) (rank Int) (group Int) (complete-time Int)))) </code></pre> <p>you are generating a universe that is "freely" generated. That's just a fancy word for saying there is a value for <code>Barrier</code> for each possible element in the cartesian product <code>Int x Int x Int x Int</code>.</p> <p>Later on, when you say:</p> <pre><code>(assert (forall ((b1 Barrier) (b2 Barrier)) (=&gt; (= (group b1) (group b2)) (= (complete-time b1) (complete-time b2))))) </code></pre> <p>you are making an assertion about all possible values of <code>b1</code> and <code>b2</code>, and you are saying that if groups are the same then completion times must be the same. But remember that datatypes are freely generated so z3 tells you <code>unsat</code>, meaning that your assertion is clearly violated by picking up proper values of <code>b1</code> and <code>b2</code> from that cartesian product, which have plenty of inhabitant pairs that violate this assertion.</p> <p>What you were trying to say, of course, was: <em>"I just want you to pay attention to those elements that satisfy this property. I don't care about the others."</em> But that's not what you said. To do so, simply turn your assertion to a function:</p> <pre><code>(define-fun groupCompletesTogether ((b1 Barrier) (b2 Barrier)) Bool (=&gt; (= (group b1) (group b2)) (= (complete-time b1) (complete-time b2)))) </code></pre> <p>then, use it as the hypothesis of your implications. Here's a silly example:</p> <pre><code>(declare-const b00 Barrier) (declare-const b01 Barrier) (assert (=&gt; (groupCompletesTogether b00 b01) (&gt; (rank b00) (rank b01)))) (check-sat) (get-model) </code></pre> <p>This prints:</p> <pre><code>sat (model (define-fun b01 () Barrier (barrier 3 0 2437 1797)) (define-fun b00 () Barrier (barrier 2 1 1236 1796)) ) </code></pre> <p>This isn't a particularly interesting model, but it is correct nonetheless. I hope this explains the issue and sets you on the right path to model. You can use that predicate in conjunction with other facts as well, and I suspect in a <code>sat</code> scenario, that's really what you want. So, you can say:</p> <pre><code>(assert (distinct b00 b01)) (assert (and (= (group b00) (group b01)) (groupCompletesTogether b00 b01) (&gt; (rank b00) (rank b01)))) </code></pre> <p>and you'd get the following model:</p> <pre><code>sat (model (define-fun b01 () Barrier (barrier 3 2436 0 1236)) (define-fun b00 () Barrier (barrier 2 2437 0 1236)) ) </code></pre> <p>which is now getting more interesting!</p> <p>In general, while SMTLib does support quantifiers, you should try to stay away from them as much as possible as it renders the logic semi-decidable. And in general, you only want to write quantified axioms like you did for uninterpreted constants. (That is, introduce a new function/constant, let it go uninterpreted, but do assert a universally quantified axiom that it should satisfy.) This can let you model a bunch of interesting functions, though quantifiers can make the solver respond <code>unknown</code>, so they are best avoided if you can.</p> <p>[Side note: As a rule of thumb, When you write a quantified axiom over a freely-generated datatype (like your Barrier), it'll either be trivially true or will never be satisfied because the universe literally will contain everything that can be constructed in that way. Think of it like a datatype in Haskell/ML etc.; where it's nothing but a container of all possible values.]</p> <br /><br /><br /><h3>回答2:</h3><br /><p>For what it is worth I was able to move forward by using sorts and uninterpreted functions instead of data types.</p> <pre><code>(declare-sort Barrier 0) (declare-fun proc (Barrier) Int) (declare-fun rank (Barrier) Int) (declare-fun group (Barrier) Int) (declare-fun complete-time (Barrier) Int) </code></pre> <p>Then the <code>forall</code> assertion is sat. I would still appreciate an explanation of why this change made a difference.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/60997115/modeling-a-small-programming-language-and-analysis-in-smt-lib-using-datatypes-an</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> </div> </div> Fri, 05 Feb 2021 03:58:20 +0000 无人久伴 4056350 at https://www.e-learn.cn Modeling a small programming language and analysis in SMT-LIB using datatypes and forall https://www.e-learn.cn/topic/4056347 <span>Modeling a small programming language and analysis in SMT-LIB using datatypes and forall</span> <span><span lang="" about="/user/233" typeof="schema:Person" property="schema:name" datatype="">五迷三道</span></span> <span>2021-02-05 11:58:02</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am trying to model a small programming language in SMT-LIB 2. My intent is to express some program analysis problems and solve them with Z3. I think I am misunderstanding the <code>forall</code> statement though. Here is a snippet of my code.</p> <pre><code>; barriers.smt2 (declare-datatype Barrier ((barrier (proc Int) (rank Int) (group Int) (complete-time Int)))) ; barriers in the same group complete at the same time (assert (forall ((b1 Barrier) (b2 Barrier)) (=&gt; (= (group b1) (group b2)) (= (complete-time b1) (complete-time b2))))) (check-sat) </code></pre> <p>When I run <code>z3 -smt2 barriers.smt2</code> I get <code>unsat</code> as the result. I am thinking that an instance of my analysis problem would be a series of <code>forall</code> assertions like the above and a series of const declarations with assertions that describe the input program.</p> <pre><code>(declare-const b00 Barrier) (assert (= (proc b00) 0)) (assert (= (rank b00) 0)) ... </code></pre> <p>But apparently I am using the <code>forall</code> expression incorrectly because I expected z3 to decide that there was a satisfying model for that assertion. What am I missing?</p> <br /><h3>回答1:</h3><br /><p>When you declare a <code>datatype</code> like this:</p> <pre><code>(declare-datatype Barrier ((barrier (proc Int) (rank Int) (group Int) (complete-time Int)))) </code></pre> <p>you are generating a universe that is "freely" generated. That's just a fancy word for saying there is a value for <code>Barrier</code> for each possible element in the cartesian product <code>Int x Int x Int x Int</code>.</p> <p>Later on, when you say:</p> <pre><code>(assert (forall ((b1 Barrier) (b2 Barrier)) (=&gt; (= (group b1) (group b2)) (= (complete-time b1) (complete-time b2))))) </code></pre> <p>you are making an assertion about all possible values of <code>b1</code> and <code>b2</code>, and you are saying that if groups are the same then completion times must be the same. But remember that datatypes are freely generated so z3 tells you <code>unsat</code>, meaning that your assertion is clearly violated by picking up proper values of <code>b1</code> and <code>b2</code> from that cartesian product, which have plenty of inhabitant pairs that violate this assertion.</p> <p>What you were trying to say, of course, was: <em>"I just want you to pay attention to those elements that satisfy this property. I don't care about the others."</em> But that's not what you said. To do so, simply turn your assertion to a function:</p> <pre><code>(define-fun groupCompletesTogether ((b1 Barrier) (b2 Barrier)) Bool (=&gt; (= (group b1) (group b2)) (= (complete-time b1) (complete-time b2)))) </code></pre> <p>then, use it as the hypothesis of your implications. Here's a silly example:</p> <pre><code>(declare-const b00 Barrier) (declare-const b01 Barrier) (assert (=&gt; (groupCompletesTogether b00 b01) (&gt; (rank b00) (rank b01)))) (check-sat) (get-model) </code></pre> <p>This prints:</p> <pre><code>sat (model (define-fun b01 () Barrier (barrier 3 0 2437 1797)) (define-fun b00 () Barrier (barrier 2 1 1236 1796)) ) </code></pre> <p>This isn't a particularly interesting model, but it is correct nonetheless. I hope this explains the issue and sets you on the right path to model. You can use that predicate in conjunction with other facts as well, and I suspect in a <code>sat</code> scenario, that's really what you want. So, you can say:</p> <pre><code>(assert (distinct b00 b01)) (assert (and (= (group b00) (group b01)) (groupCompletesTogether b00 b01) (&gt; (rank b00) (rank b01)))) </code></pre> <p>and you'd get the following model:</p> <pre><code>sat (model (define-fun b01 () Barrier (barrier 3 2436 0 1236)) (define-fun b00 () Barrier (barrier 2 2437 0 1236)) ) </code></pre> <p>which is now getting more interesting!</p> <p>In general, while SMTLib does support quantifiers, you should try to stay away from them as much as possible as it renders the logic semi-decidable. And in general, you only want to write quantified axioms like you did for uninterpreted constants. (That is, introduce a new function/constant, let it go uninterpreted, but do assert a universally quantified axiom that it should satisfy.) This can let you model a bunch of interesting functions, though quantifiers can make the solver respond <code>unknown</code>, so they are best avoided if you can.</p> <p>[Side note: As a rule of thumb, When you write a quantified axiom over a freely-generated datatype (like your Barrier), it'll either be trivially true or will never be satisfied because the universe literally will contain everything that can be constructed in that way. Think of it like a datatype in Haskell/ML etc.; where it's nothing but a container of all possible values.]</p> <br /><br /><br /><h3>回答2:</h3><br /><p>For what it is worth I was able to move forward by using sorts and uninterpreted functions instead of data types.</p> <pre><code>(declare-sort Barrier 0) (declare-fun proc (Barrier) Int) (declare-fun rank (Barrier) Int) (declare-fun group (Barrier) Int) (declare-fun complete-time (Barrier) Int) </code></pre> <p>Then the <code>forall</code> assertion is sat. I would still appreciate an explanation of why this change made a difference.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/60997115/modeling-a-small-programming-language-and-analysis-in-smt-lib-using-datatypes-an</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> </div> </div> Fri, 05 Feb 2021 03:58:02 +0000 五迷三道 4056347 at https://www.e-learn.cn Floor and Ceiling Function implementation in Z3 https://www.e-learn.cn/topic/4041364 <span>Floor and Ceiling Function implementation in Z3</span> <span><span lang="" about="/user/200" typeof="schema:Person" property="schema:name" datatype="">余生长醉</span></span> <span>2021-01-29 16:20:33</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have tried to implement Floor and Ceiling Function as defined in the following link</p> <p>https://math.stackexchange.com/questions/3619044/floor-or-ceiling-function-encoding-in-first-order-logic/3619320#3619320</p> <p>But Z3 query returning counterexample.</p> <p>Floor Function</p> <pre><code>_X=Real('_X') _Y=Int('_Y') _W=Int('_W') _n=Int('_n') _Floor=Function('_Floor',RealSort(),IntSort()) .. _s.add(_X&gt;=0) _s.add(_Y&gt;=0) _s.add(Implies(_Floor(_X)==_Y,And(Or(_Y==_X,_Y&lt;_X),ForAll(_W,Implies(And(_W&gt;=0,_W&lt;_X),And(_W ==_Y,_W&lt;_Y)))))) _s.add(Implies(And(Or(_Y==_X,_Y&lt;_X),ForAll(_W,Implies(And(_W&gt;=0,_W&lt;_X),And(_W==_Y,_W&lt;_Y))),_Floor(_X)==_Y)) _s.add(Not(_Floor(0.5)==0)) </code></pre> <p>Expected Result - Unsat</p> <p>Actual Result - Sat</p> <p>Ceiling Function</p> <pre><code>_X=Real('_X') _Y=Int('_Y') _W=Int('_W') _Ceiling=Function('_Ceiling',RealSort(),IntSort()) .. .. _s.add(_X&gt;=0) _s.add(_Y&gt;=0) _s.add(Implies(_Ceiling(_X)==_Y,And(Or(_Y==_X,_Y&lt;_X),ForAll(_W,Implies(And(_W&gt;=0,_W&lt;_X),And(_W ==_Y,_Y&lt;_W)))))) _s.add(Implies(And(Or(_Y==_X,_Y&lt;_X),ForAll(_W,Implies(And(_W&gt;=0,_W&lt;_X),And(_W==_Y,_Y&lt;_W)))),_Ceiling(_X)==_Y)) _s.add(Not(_Ceilng(0.5)==1)) </code></pre> <p>Expected Result - Unsat</p> <p>Actual Result - Sat</p> <br /><h3>回答1:</h3><br /><p>[Your encoding doesn't load to z3, it gives a syntax error even after eliminating the '..', as your call to <code>Implies</code> needs an extra argument. But I'll ignore all that.]</p> <p>The short answer is, you can't really do this sort of thing in an SMT-Solver. If you could, then you can solve arbitrary Diophantine equations. Simply cast it in terms of Reals, solve it (there is a decision procedure for Reals), and then add the extra constraint that the result is an integer by saying <code>Floor(solution) = solution</code>. So, by this argument, you can see that modeling such functions will be beyond the capabilities of an SMT solver.</p> <p>See this answer for details: Get fractional part of real in QF_UFNRA</p> <p>Having said that, this does <em>not</em> mean you cannot code this up in Z3. It just means that it will be more or less useless. Here's how I would go about it:</p> <pre class="lang-py prettyprint-override"><code>from z3 import * s = Solver() Floor = Function('Floor',RealSort(),IntSort()) r = Real('R') f = Int('f') s.add(ForAll([r, f], Implies(And(f &lt;= r, r &lt; f+1), Floor(r) == f))) </code></pre> <p>Now, if I do this:</p> <pre class="lang-py prettyprint-override"><code>s.add(Not(Floor(0.5) == 0)) print(s.check()) </code></pre> <p>you'll get <code>unsat</code>, which is correct. If you do this instead:</p> <pre class="lang-py prettyprint-override"><code>s.add(Not(Floor(0.5) == 1)) print(s.check()) </code></pre> <p>you'll see that z3 simply loops forever. To make this usefull, you'd want the following to work as well:</p> <pre class="lang-py prettyprint-override"><code>test = Real('test') s.add(test == 2.4) result = Int('result') s.add(Floor(test) == result) print(s.check()) </code></pre> <p>but again, you'll see that z3 simply loops forever.</p> <p>So, bottom line: Yes, you can model such constructs, and z3 will correctly answer the simplest of queries. But with anything interesting, it'll simply loop forever. (Essentially whenever you'd expect <code>sat</code> and most of the <code>unsat</code> scenarios unless they can be constant-folded away, I'd expect z3 to simply loop.) And there's a very good reason for that, as I mentioned: Such theories are just not decidable and fall well out of the range of what an SMT solver can do.</p> <p>If you are interested in modeling such functions, your best bet is to use a more traditional theorem prover, like Isabelle, Coq, ACL2, HOL, HOL-Light, amongst others. They are much more suited for working on these sorts of problems. And also, give a read to Get fractional part of real in QF_UFNRA as it goes into some of the other details of how you can go about modeling such functions using non-linear real arithmetic.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/61388141/floor-and-ceiling-function-implementation-in-z3</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> <div class="field--item"><a href="/tag/z3py" hreflang="zh-hans">z3py</a></div> <div class="field--item"><a href="/tag/floor" hreflang="zh-hans">floor</a></div> <div class="field--item"><a href="/tag/ceil" hreflang="zh-hans">ceil</a></div> </div> </div> Fri, 29 Jan 2021 08:20:33 +0000 余生长醉 4041364 at https://www.e-learn.cn Z3 support for exponentials https://www.e-learn.cn/topic/3912454 <span>Z3 support for exponentials</span> <span><span lang="" about="/user/155" typeof="schema:Person" property="schema:name" datatype="">和自甴很熟</span></span> <span>2020-11-13 04:22:36</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>来源:<code>https://stackoverflow.com/questions/49430434/z3-support-for-exponentials</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> </div> </div> Thu, 12 Nov 2020 20:22:36 +0000 和自甴很熟 3912454 at https://www.e-learn.cn Z3 support for exponentials https://www.e-learn.cn/topic/3912452 <span>Z3 support for exponentials</span> <span><span lang="" about="/user/136" typeof="schema:Person" property="schema:name" datatype="">扶醉桌前</span></span> <span>2020-11-13 04:22:28</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>来源:<code>https://stackoverflow.com/questions/49430434/z3-support-for-exponentials</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> </div> </div> Thu, 12 Nov 2020 20:22:28 +0000 扶醉桌前 3912452 at https://www.e-learn.cn Z3 support for exponentials https://www.e-learn.cn/topic/3912449 <span>Z3 support for exponentials</span> <span><span lang="" about="/user/57" typeof="schema:Person" property="schema:name" datatype="">瘦欲@</span></span> <span>2020-11-13 04:22:04</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>来源:<code>https://stackoverflow.com/questions/49430434/z3-support-for-exponentials</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> </div> </div> Thu, 12 Nov 2020 20:22:04 +0000 瘦欲@ 3912449 at https://www.e-learn.cn z3: solve the Eight Queens puzzle https://www.e-learn.cn/topic/3867497 <span>z3: solve the Eight Queens puzzle</span> <span><span lang="" about="/user/127" typeof="schema:Person" property="schema:name" datatype="">我怕爱的太早我们不能终老</span></span> <span>2020-10-21 05:48:33</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>来源:<code>https://stackoverflow.com/questions/48031462/z3-solve-the-eight-queens-puzzle</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/z3" hreflang="zh-hans">Z3</a></div> <div class="field--item"><a href="/tag/smt" hreflang="zh-hans">smt</a></div> <div class="field--item"><a href="/tag/z3py" hreflang="zh-hans">z3py</a></div> </div> </div> Tue, 20 Oct 2020 21:48:33 +0000 我怕爱的太早我们不能终老 3867497 at https://www.e-learn.cn