<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">
    <title type="text">Active questions tagged z3 - Stack Overflow</title>
    <link rel="self" href="https://stackoverflow.com/feeds/tag/z3" type="application/atom+xml" />
    <link rel="alternate" href="https://stackoverflow.com/questions/tagged?tagnames=z3&amp;sort=active" type="text/html" />
    <subtitle>most recent 30 from stackoverflow.com</subtitle>
    <updated>2026-06-10T10:53:23Z</updated>
    <id>https://stackoverflow.com/feeds/tag/z3</id>
    <creativeCommons:license>https://creativecommons.org/licenses/by-sa/4.0/rdf</creativeCommons:license> 
    <entry>
        <id>https://stackoverflow.com/q/16137005</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Set membership relation in z3</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
        <author>
            <name>Giacomo Tagliabue</name>
            <uri>https://stackoverflow.com/users/1117427</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/16137005/set-membership-relation-in-z3" />
        <published>2013-04-21T22:44:54Z</published>
        <updated>2026-06-05T06:08:11Z</updated>
        <summary type="html">
            &lt;p&gt;I wanted to define a membership relation in Z3 with C&#x2B;&#x2B; APIs. I thought of doing it in the following way:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code&gt;z3::context C;&#xA;z3::sort I = C.int_sort();&#xA;z3::sort B = C.bool_sort();&#xA;z3::func_decl InSet = C.function(&quot;f&quot;, I, B);&#xA;&#xA;z3::expr e1 = InSet(C.int_val(2)) == C.bool_val(true);&#xA;z3::expr e2 = InSet(C.int_val(3)) == C.bool_val(true);&#xA;z3::expr ite  = to_expr(C, Z3_mk_ite(C, e1, C.bool_val(true),&#xA;    Z3_mk_ite(C,e2,C.bool_val(true),C.bool_val(false))));&#xA;errs() &amp;lt;&amp;lt; Z3_ast_to_string(C,ite);&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&#xA;&lt;p&gt;In this example the set is composed by the integer 2 and 3. I am sure there is a better way to define a relation, in particular a set membership relation, but I am really a Z3 rookie. Does anyone know the best one?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/67632821</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">How to use tuples in SMT-lib?</title>
            <category scheme="https://stackoverflow.com/tags" term="tuples" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
            <category scheme="https://stackoverflow.com/tags" term="smt-lib" />
        <author>
            <name>radrow</name>
            <uri>https://stackoverflow.com/users/4400060</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/67632821/how-to-use-tuples-in-smt-lib" />
        <published>2021-05-21T07:53:12Z</published>
        <updated>2026-06-04T12:02:07Z</updated>
        <summary type="html">
            &lt;p&gt;I am quite sure that it should be possible to describe tuples using SMT-lib syntax, especially for the Z3 solver. However, I can&#x27;t really find a way of doing so. The only thing I found is &lt;a href=&quot;https://z3prover.github.io/api/html/ml/Z3.Tuple.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;this documentation page&lt;/a&gt;, but I don&#x27;t get how to use it in &lt;code&gt;z3 -in&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;My struggles so far:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-lisp prettyprint-override&quot;&gt;&lt;code&gt;(declare-const t (Prod Int Bool))&#xA;(error &amp;quot;line 1 column 19: unknown sort &#x27;Prod&#x27;&amp;quot;)&#xA;(declare-const t (Tuple Int Bool))&#xA;(error &amp;quot;line 2 column 18: unknown sort &#x27;Tuple&#x27;&amp;quot;)&#xA;(declare-const t (Pair Int Bool))&#xA;(error &amp;quot;line 3 column 18: unknown sort &#x27;Pair&#x27;&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79928792</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">What tactics/strategies to use for mixed complementarity problems?</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
            <category scheme="https://stackoverflow.com/tags" term="constraint-programming" />
            <category scheme="https://stackoverflow.com/tags" term="mathsat" />
            <category scheme="https://stackoverflow.com/tags" term="cvc5" />
        <author>
            <name>fvz185</name>
            <uri>https://stackoverflow.com/users/32641443</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79928792/what-tactics-strategies-to-use-for-mixed-complementarity-problems" />
        <published>2026-04-20T12:18:37Z</published>
        <updated>2026-05-28T14:47:21Z</updated>
        <summary type="html">
            &lt;p&gt;I would appreciate your input on a hypothesis I am currently working on: Exploiting the logical condition of mixed complementarity problems (MCP) by using (constraint) propagation techniques. I would like to solve the MCP as a system of equations which includes the complementarity condition as a propositional formula for which I am using Z3. How can I influence and direct the search for a feasible solution?&lt;/p&gt;&#xA;&lt;h1&gt;Setting&lt;/h1&gt;&#xA;&lt;p&gt;My test bed is a bimatrix game, which is a simultaneous game for two players in which each player has a finite number of possible actions. The payoffs resulting from actions are described by two matrices. The payoffs of player 1 are given by&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;A = \begin{bmatrix} 3 &amp;amp; 3 \\ 2 &amp;amp; 5 \\ 0 &amp;amp; 6 \end{bmatrix}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;with strategies &lt;code&gt;M = \set{1, 2, 3}&lt;/code&gt; (which are the rows of A). The payoffs of player 2 are given by&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;B = \begin{bmatrix} 3 &amp;amp; 2 \\ 2 &amp;amp; 6 \\ 3 &amp;amp; 1 \end{bmatrix}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;with strategies &lt;code&gt;N = \set{4, 5}&lt;/code&gt; (which are the columns of B).&lt;/p&gt;&#xA;&lt;p&gt;My chosen approach is to frame the bimatrix game as a feasibility problem, to find a solution in the form of a Nash equilibrium (that is, each player&#x27;s chosen action is the payoff maximising action given the other player&#x27;s chosen action). For the feasibility problem, I am using a set of variables for mixed strategies &lt;code&gt;x_{1}, x_{2}, x_{3} \in \mathbb{R}_{&#x2B;}&lt;/code&gt; for player 1 and &lt;code&gt;y_{4}, y_{5} \in \mathbb{R}_{&#x2B;}&lt;/code&gt; for player 2. Additionally, there are variables for the players&#x27; expected payoffs &lt;code&gt;u \in [0, \max(A)]&lt;/code&gt; for player 1 and &lt;code&gt;v \in [0, \max(B)]&lt;/code&gt; for player 2.&lt;/p&gt;&#xA;&lt;h1&gt;Problem&lt;/h1&gt;&#xA;&lt;p&gt;The payoff inequalities are given by&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;\begin{align*}&#xA;(Ay)_{i} \leq u \forall i \in M (1),\\&#xA;(B^{\top}x)_{j} \leq v \forall j \in N (2).&#xA;\end{align*}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Since the mixed strategies represent a probability distribution, we also have&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;\begin{align*}&#xA;\sum_{i \in M} x_{i} = 1 (3),\\&#xA;\sum_{j \in N} y_{j} = 1 (4).&#xA;\end{align*}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Lastly, the complementarity conditions which are the following bilinear terms:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;x_{i} (u - (Ay)\_{i}) = 0 \forall i \in M (5) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;for player 1. Similarly for player 2, we have&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;y_{j} (v - (B^{\top} x)_{j}) = 0 \forall j \in N (6).&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;These bilinear terms express that the player&#x27;s mixed strategy vector is orthogonal to their expected payoff vector. In other words, a player plays an action with positive probability, or the respective expected payoff equals the expected payoff by playing this strategy.&lt;/p&gt;&#xA;&lt;h2&gt;Hypothesis&lt;/h2&gt;&#xA;&lt;p&gt;I express the complementarity conditions (5) and (6) as logical disjunctions:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(x\_{i} = 0) \lor (u - (Ay)_{i} = 0) \forall i \in M (7) &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;for player 1 and&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(y_{j} = 0) \lor (v - (B^{\top} x)_{j} = 0) \forall j \in N (8)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;for player 2. Then I rewrite disjunctions (7) and (8) as implications&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;\begin{align*}&#xA;(x_{i} &amp;gt; 0) \Rightarrow (u - (Ay)_{i} = 0) \forall i \in M (9),\\&#xA;(y_{j} &amp;gt; 0) \Rightarrow (v - (B^{\top}x)_{j} = 0) \forall j \in N (10),&#xA;\end{align*} &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;respectively. Alternatively, implications (9) and (10) can be written with a double negation in the antecedence:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;\begin{align*}&#xA;(x_{i} != 0) \Rightarrow (u - (Ay)_{i} = 0) \forall i \in M (11),\\&#xA;(y_{j} != 0) \Rightarrow (v - (B^{\top}x)_{j} = 0) \forall j \in N (12),&#xA;\end{align*} &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;My idea behind it is that the complementarity problem can be solved by backtracking on binary cases of the mixed strategy variables: An action is played with either zero probability or positive probability. If it is played with positive probability, then the expected payoff of the associated action must equal the total expected payoff.&lt;br /&gt;&#xA;In other words, I want to avoid a search in the continuous space of mixed strategies to find a point where all equations (1)&#x2013;(6) (partly nonlinear) hold.&lt;/p&gt;&#xA;&lt;h1&gt;Solution Approach&lt;/h1&gt;&#xA;&lt;p&gt;It is worth noting that every bimatrix game has a Nash equilibrium in mixed strategies. This can be done by applying the Lemke-Howson algorithm, which is a pivoting technique on the Best Response polytopes of A and B.&lt;br /&gt;&#xA;Yet, I have chosen a SAT/CSP/SMT approach because I expect it to be a more flexible modelling framework to allow for additional side-constraints or optimising the mixed strategy vectors with respect to some objective.&lt;/p&gt;&#xA;&lt;p&gt;I ended up with using Z3 and Choco&#x2B;IBEX, since most other CSP solvers focus primarily on discrete domains (while I need continuous domains for x and y). However, I dropped Choco&#x2B;IBEX, as it does not scale as well as Z3 does for problems with matrices of size up to 150-by-150. This is probably because Choco&#x2B;IBEX finds sets of intervals whereas Z3 focuses on sets of points.&lt;/p&gt;&#xA;&lt;h2&gt;Experience&lt;/h2&gt;&#xA;&lt;p&gt;Z3, on default settings, allows me to solve the system of equations (1)&#x2013;(4) plus the complementarity conditions as random problems of size up to 150-by-150 matrices with integer payoffs [0,10]. Quick (naive) experiments (with different seeds) showed that it might matter for Z3 how the complementarity constraints are implemented using&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;disjunctions (7) and (8), Z3 terminated after between 0.66 and 10 seconds,&lt;/li&gt;&#xA;&lt;li&gt;implications (9) and (10), Z3 terminated after between 1.2 and 15 seconds, and&lt;/li&gt;&#xA;&lt;li&gt;implications (11) and (12), Z3 terminated after between 1.2 and 8 seconds.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;From MIP solvers, I also know that they can be tuned to decrease the computation time of some problems, like branching strategies, variable selection, add callbacks, etc. Is it possible to do similarly with Z3?&lt;br /&gt;&#xA;In particular, there is the Porter, Nudelman &amp;amp; Shoham (2006) article to solve n-player bimatrix games by searching the combinatorial space of actions. It does so by manually implementing a backtracking with arc-consistency-based pruning algorithm which wraps around a feasibility problem containing equations (1)&#x2013;(4). Using this constraint propagation technique enhances the computation time significantly compared to the Lemke-Howson algorithm.&lt;/p&gt;&#xA;&lt;h1&gt;Questions&lt;/h1&gt;&#xA;&lt;p&gt;I would like to know if it is possible to &#x27;internalise&#x27; what Porter, Nudelman &amp;amp; Shoham does into a configuration of Z3. Namely:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;p&gt;Can I instruct Z3 to prioritise small true assignments (i.e.,&#xA;&lt;code&gt;\min |\{ x_{i} : x_{i} &amp;gt; 0, \forall i \in M \cup N \}|&lt;/code&gt;)?&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;Can I instruct Z3 to prioritise balanced true assignments (i.e., &lt;code&gt;\min (|\{ x_{i} : x_{i} &amp;gt; 0, \forall i \in M \}| - |\{ y_{i} : y_{j} &amp;gt; 0 \forall j \in N \}|)&lt;/code&gt;)?&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;Does it make sense to adjust the arbitrary precision by, say, &lt;code&gt;set_option(precision=16)&lt;/code&gt; (Python API)?&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;Does it make sense to specify an engine by including &lt;code&gt;SolveFor(&#x27;QF_LRA&#x27;)&lt;/code&gt; (Python API)?&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;Should I choose a different SMT solver, such as cvc5 or MathSAT5?&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h1&gt;References&lt;/h1&gt;&#xA;&lt;p&gt;I have read through:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;a href=&quot;https://z3prover.github.io/papers/programmingz3.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;https://z3prover.github.io/papers/programmingz3.html&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/57654435/scalability-of-z3&quot;&gt;Scalability of z3&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/59439543/have-z3-prioritize-certain-boolean-variables-when-branching-searching-for-a-mode&quot;&gt;Have Z3 prioritize certain Boolean variables when branching/searching for a model&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;and tried to find answers to questions relating to performance, tuning, etc. under the z3 tag.&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;Porter, Nudelman, Shoham (2006): &lt;a href=&quot;https://doi.org/10.1016/j.geb.2006.03.015&quot; rel=&quot;nofollow noreferrer&quot;&gt;https://doi.org/10.1016/j.geb.2006.03.015&lt;/a&gt;&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79927676</id>
        <re:rank scheme="https://stackoverflow.com">-1</re:rank>
        <title type="text">How to determine symbolic relationship between different variables (rather than concrete values)</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>aquagarced</name>
            <uri>https://stackoverflow.com/users/32633733</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79927676/how-to-determine-symbolic-relationship-between-different-variables-rather-than" />
        <published>2026-04-17T22:24:01Z</published>
        <updated>2026-04-17T23:07:02Z</updated>
        <summary type="html">
            &lt;p&gt;I need a way to determine programmatically the symbolic relationship between variables in an equation&lt;/p&gt;&#xA;&lt;p&gt;For example, forall x, given 3ax - bx = 0, I want to determine that b must be equal to 3a. When I attempt to synthesize these values using something like Z3 I get concrete values rather than symbolic expressions, and the closest thing I&#x27;ve found thus far is &lt;a href=&quot;https://z3prover.github.io/api/html/group__capi.html#ga9e5061818a22dfefbb64f8a5f19ec478&quot; rel=&quot;nofollow noreferrer&quot;&gt;Z3&#x27;s get_implied_equalities&lt;/a&gt;. But that appears to require already knowing what the expected equality is, which I would not.&lt;/p&gt;&#xA;&lt;p&gt;I know this wasn&#x27;t possible in the past using Z3 (&lt;a href=&quot;https://stackoverflow.com/questions/11223929/symbolic-variables-in-z3&quot;&gt;See: Symbolic variables in z3&lt;/a&gt;), but I don&#x27;t know if that limitation is Z3 specific or a technical challenge across the board. I&#x27;m open to using any tool (although one with a C interface I could ffi to would be ideal)&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79890358</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Checking if a graph is connected with Z3</title>
            <category scheme="https://stackoverflow.com/tags" term="logic" />
            <category scheme="https://stackoverflow.com/tags" term="graph-theory" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
        <author>
            <name>pepper</name>
            <uri>https://stackoverflow.com/users/26704575</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79890358/checking-if-a-graph-is-connected-with-z3" />
        <published>2026-02-16T17:15:51Z</published>
        <updated>2026-02-17T10:50:20Z</updated>
        <summary type="html">
            &lt;p&gt;I&#x27;m trying to code a solver for the chinese postman problem using z3. I have to check two things to see if there is already a solution available:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;p&gt;if every node has even degree&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&lt;p&gt;if the graph is connected&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;The first is easy, but I don&#x27;t know how to check the second. I feel like I should use the Transitive closure but I don&#x27;t know how to implement the function in the code. I&#x27;m assuming that every edge is undirected.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;from z3 import *&#xA;&#xA;solver = Solver()&#xA;&#xA;nodes = [&amp;quot;A&amp;quot;, &amp;quot;B&amp;quot;, &amp;quot;C&amp;quot;, &amp;quot;D&amp;quot;, &amp;quot;E&amp;quot;, &amp;quot;F&amp;quot;, &amp;quot;G&amp;quot;]&#xA;numnodes = 7&#xA;&#xA;edges = [ [&amp;quot;A&amp;quot;, &amp;quot;B&amp;quot;, 5], [&amp;quot;B&amp;quot;, &amp;quot;C&amp;quot;, 1], [&amp;quot;C&amp;quot;, &amp;quot;D&amp;quot;, 7], [&amp;quot;D&amp;quot;, &amp;quot;A&amp;quot;, 9], [&amp;quot;E&amp;quot;, &amp;quot;F&amp;quot;, 3], [&amp;quot;F&amp;quot;, &amp;quot;G&amp;quot;, 2], [&amp;quot;G&amp;quot;, &amp;quot;E&amp;quot;, 3]]&#xA;&#xA;degree = [0] * numnodes&#xA;&#xA;for x, y, z in edges:&#xA;    degree[nodes.index(x)] &#x2B;=1&#xA;    degree[nodes.index(y)] &#x2B;=1&#xA;&#xA;&#xA;for i in range(numnodes):&#xA;    solver.add(degree[i] % 2 == 0)&#xA;&#xA;if solver.check() == sat:&#xA;    print(&amp;quot;Eulerian circuit exists&amp;quot;)&#xA;else:&#xA;    print(&amp;quot;No Eulerian circuit exists&amp;quot;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This is what I&#x27;v written so far. Edges are defined as &lt;code&gt;[start node, end node, cost]&lt;/code&gt; .&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79841051</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">What&#x27;s the meaning of uninterpreted sorts with non-zero arity and how do I get them in the Z3 C API?</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>Nicola Gigante</name>
            <uri>https://stackoverflow.com/users/3206471</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79841051/whats-the-meaning-of-uninterpreted-sorts-with-non-zero-arity-and-how-do-i-get-t" />
        <published>2025-12-08T14:14:49Z</published>
        <updated>2025-12-08T16:37:57Z</updated>
        <summary type="html">
            &lt;p&gt;SMT-LIB supports declaring uninterpreted sorts with an &amp;quot;arity&amp;quot; argument such as:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(declare-sort MySort 2)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Then one can declare functions and constants using that sort, which must be given some sort arguments.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(declare-const x (MySort Int Real))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I&#x27;m not really sure how I should use this feature. Is any instantiation of &lt;code&gt;(MySort X Y)&lt;/code&gt; a different uninterpreted sort altogether? Given that I cannot declare type-parametric functions, what&#x27;s the intended usage of this feature?&lt;/p&gt;&#xA;&lt;p&gt;Also, while Z3 supports it when parsing SMT-LIB files, it seems I cannot find the corresponding feature in the C API. The &lt;code&gt;Z3_mk_uninterpreted_sort()&lt;/code&gt; function does not take an &lt;code&gt;arity&lt;/code&gt; parameter and I cannot find other functions with this purpose.&lt;/p&gt;&#xA;&lt;p&gt;So how do I use parametric uninterpreted sorts in SMT-LIB and how do I get them from the Z3 C API?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/4615590</id>
        <re:rank scheme="https://stackoverflow.com">5</re:rank>
        <title type="text">Looking for practical examples of SMT Z3 usecases (like DbC) and open source alternative to Z3? [closed]</title>
            <category scheme="https://stackoverflow.com/tags" term=".net" />
            <category scheme="https://stackoverflow.com/tags" term="constraint-programming" />
            <category scheme="https://stackoverflow.com/tags" term="sat-solvers" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
        <author>
            <name>Yauhen Yakimovich</name>
            <uri>https://stackoverflow.com/users/544463</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/4615590/looking-for-practical-examples-of-smt-z3-usecases-like-dbc-and-open-source-alt" />
        <published>2011-01-06T13:52:34Z</published>
        <updated>2025-11-03T20:16:53Z</updated>
        <summary type="html">
            &lt;p&gt;I have got interested in and looking for practical examples of SMT Z3 usage (like DbC) with code and open source alternatives to this tool. So, in fact, I am interested in similar Z3 formal solving tools, but:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;it must be open source&lt;/li&gt;&#xA;&lt;li&gt;provide both low-level (API) and high-level (Text scripting) interaction&lt;/li&gt;&#xA;&lt;li&gt;support SMT-LIB&lt;/li&gt;&#xA;&lt;li&gt;suitable(usable) in tools/written in/for languages as Java, python, ruby, Vala, and &lt;em&gt;not&lt;/em&gt; Haskell&lt;/li&gt;&#xA;&lt;li&gt;has stable (usable in practice) tools based on it, like design by contract (DbC), static type verification, etc.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;According to SMT-LIB homepage (see bit.ly bundle for details) the list of 2010 SMT solvers is:&#xA;&amp;quot;Alt-Ergo, Barcelogic, Beaver, Boolector , CVC3, DPT, MathSAT, OpenSMT, SatEEn, Spear, STP, SWORD, UCLID, veriT, Yices, Z3.&amp;quot;&lt;/p&gt;&#xA;&lt;p&gt;Please give any feedback on using any of them in practice (code examples are the best)?&lt;/p&gt;&#xA;&lt;p&gt;Finally, any information on comparison of it with GHC possibilities would be useful, but only in case there is an implementation example (not a language &amp;quot;feature&amp;quot;).&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79681849</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Solving &quot;The Hardest Logic Puzzle Ever&quot; via z3</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
        <author>
            <name>nnarek</name>
            <uri>https://stackoverflow.com/users/11030311</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79681849/solving-the-hardest-logic-puzzle-ever-via-z3" />
        <published>2025-06-27T11:10:51Z</published>
        <updated>2025-10-07T11:24:46Z</updated>
        <summary type="html">
            &lt;p&gt;I am trying to solve &lt;a href=&quot;https://en.wikipedia.org/wiki/The_Hardest_Logic_Puzzle_Ever&quot; rel=&quot;nofollow noreferrer&quot;&gt;The Hardest Logic Puzzle Ever&lt;/a&gt;, about 3 gods. So far, I have written the following code, but it returns unsat. I am expressing questions as functions and trying to use z3 to find functions that satisfy the conditions of the problem. Answers from a random god are random, and I used an uninterpreted variable to formalize that behavior. Can someone help find the issue in my code or provide z3 based solution that returns the correct answer to the problem?&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-py prettyprint-override&quot;&gt;&lt;code&gt;from z3 import *&#xA;&#xA;Tr=0&#xA;Fa=1&#xA;Ra=2&#xA;&#xA;def GetArrayElement(arr,index):&#xA;    current_else = arr[0]&#xA;    for i in range(1,len(arr)):&#xA;        current_else = If(index==i,arr[i],current_else)      &#xA;    return current_else&#xA;&#xA;def PrintFunc(m,func,args,current_val=[]):&#xA;    if len(args)==0:&#xA;        print(current_val,m.eval(func(*current_val)))&#xA;        return&#xA;    for value in args[0]:&#xA;        PrintFunc(m,func,args[1:],current_val&#x2B;[value])&#xA;&#xA;s=Solver()&#xA;&#xA;gods = [ Int(&amp;quot;god_%s&amp;quot; % (j&#x2B;1)) for j in range(3)  ]&#xA;&#xA;&#xA;yes_answer=Bool(&amp;quot;yes&amp;quot;)&#xA;no_answer=Bool(&amp;quot;no&amp;quot;)&#xA;&#xA;&#xA;question1 = Function(&amp;quot;question_1&amp;quot;, IntSort(),IntSort(),IntSort(),BoolSort(),BoolSort(),                         BoolSort()) &#xA;question2 = Function(&amp;quot;question_2&amp;quot;, IntSort(),IntSort(),IntSort(),BoolSort(),BoolSort(),BoolSort(),              BoolSort()) &#xA;question3 = Function(&amp;quot;question_3&amp;quot;, IntSort(),IntSort(),IntSort(),BoolSort(),BoolSort(),BoolSort(),BoolSort(),   BoolSort()) &#xA;determine_god = Function(&amp;quot;determine_god&amp;quot;, BoolSort(),BoolSort(),IntSort(),                                      IntSort()) &#xA;&#xA;who_to_ask1 = Function(&amp;quot;who_to_ask1&amp;quot;,               IntSort()) &#xA;who_to_ask2 = Function(&amp;quot;who_to_ask2&amp;quot;,               IntSort()) &#xA;who_to_ask3 = Function(&amp;quot;who_to_ask3&amp;quot;, BoolSort(),   IntSort()) &#xA;&#xA;x=Bool(&amp;quot;x&amp;quot;)&#xA;s.add(0&amp;lt;=who_to_ask1(),who_to_ask1()&amp;lt;3)&#xA;s.add(0&amp;lt;=who_to_ask2(),who_to_ask2()&amp;lt;3)&#xA;s.add(ForAll([x],And(0&amp;lt;=who_to_ask3(x),who_to_ask3(x)&amp;lt;3)))&#xA;&#xA;answers_of_random_god = Bools(&amp;quot;unkn1 unkn2 unkn3&amp;quot;)&#xA;&#xA;def GetAnswer(question,question_id,god):&#xA;    return  If(god==Tr,&#xA;               If(question,yes_answer,no_answer),&#xA;            If(god==Fa,&#xA;               If(question,no_answer,yes_answer),&#xA;            answers_of_random_god[question_id]))&#xA;# def GetAnswer(question,question_id,god):&#xA;#     return  If(question,yes_answer,no_answer)&#xA;&#xA;answer1=GetAnswer(question1(gods[0],gods[1],gods[2],yes_answer,no_answer),0,GetArrayElement(gods,who_to_ask1()))&#xA;answer2=GetAnswer(question2(gods[0],gods[1],gods[2],yes_answer,no_answer,answer1),1,GetArrayElement(gods,who_to_ask2()))&#xA;answer3=GetAnswer(question3(gods[0],gods[1],gods[2],yes_answer,no_answer,answer1,answer2),2,GetArrayElement(gods,who_to_ask3(answer1==answer2)))&#xA;&#xA;&#xA;&#xA;z=Int(&amp;quot;z&amp;quot;)&#xA;s.add(ForAll(gods&#x2B;[no_answer,yes_answer,z]&#x2B;answers_of_random_god,Implies(And(And([And(0&amp;lt;=god,god&amp;lt;3) for god in gods]),Distinct(gods),Distinct(yes_answer,no_answer),0&amp;lt;=z,z&amp;lt;3), &#xA;                                                   &#xA;                                                   And(GetArrayElement(gods,z)==determine_god(answer1==answer2,answer1==answer3,z)))&#xA;))&#xA;#print(s)&#xA;if s.check() == sat:&#xA;    m = s.model()&#xA;    print(m)&#xA;&#xA;print(s.check())&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79777880</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Z3: unclear model</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
        <author>
            <name>Pietro Braione</name>
            <uri>https://stackoverflow.com/users/450589</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79777880/z3-unclear-model" />
        <published>2025-09-29T08:19:30Z</published>
        <updated>2025-09-29T15:11:54Z</updated>
        <summary type="html">
            &lt;p&gt;When I send Z3 the trivial:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(declare-fun M (Int) Int)&#xA;(check-sat)&#xA;(get-value (M))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Z3 answers:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;((M (_ as-array M)))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;How shall I interpret this answer?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/11927114</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Encoding of first order differential equation as First order formula</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>hafizul asad</name>
            <uri>https://stackoverflow.com/users/1577375</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/11927114/encoding-of-first-order-differential-equation-as-first-order-formula" />
        <published>2012-08-13T01:00:11Z</published>
        <updated>2025-09-13T13:27:02Z</updated>
        <summary type="html">
            &lt;h1&gt;Can somebody help me in pointing out what will be the best encoding of following equation using first order formula so as to give it as input to the SMT solver??&lt;/h1&gt;&#xA;&lt;pre&gt;&lt;code&gt;x`=Ax&#x2B;b&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79751883</id>
        <re:rank scheme="https://stackoverflow.com">3</re:rank>
        <title type="text">Problems proving trivial things involving shift operators using Frama-C WP</title>
            <category scheme="https://stackoverflow.com/tags" term="c" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="frama-c" />
            <category scheme="https://stackoverflow.com/tags" term="formal-verification" />
            <category scheme="https://stackoverflow.com/tags" term="why3" />
        <author>
            <name>Henrik Sommerland</name>
            <uri>https://stackoverflow.com/users/2826362</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79751883/problems-proving-trivial-things-involving-shift-operators-using-frama-c-wp" />
        <published>2025-08-31T16:22:21Z</published>
        <updated>2025-09-01T09:00:46Z</updated>
        <summary type="html">
            &lt;p&gt;I have this very simple example.&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-c prettyprint-override&quot;&gt;&lt;code&gt;/*@&#xA;  requires a &amp;gt;= 0 &amp;amp;&amp;amp; a &amp;lt;= 2;&#xA;  assigns \nothing;&#xA;  ensures \result &amp;lt; 10;&#xA;*/&#xA;int bob(int a) {&#xA;  return 1 &amp;lt;&amp;lt; a;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;When I try to prove this with both Alt-Ergo and z3 it times out&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;$ frama-c test.c -wp -wp-prover=&amp;quot;Alt-Ergo,z3&amp;quot; -wp-status&#xA;[kernel] Parsing test.c (with preprocessing)&#xA;[wp] Warning: Missing RTE guards&#xA;[wp] 2 goals scheduled&#xA;[wp] [Timeout] typed_bob_ensures (Qed 1ms) (Alt-Ergo) (Cached)&#xA;[wp] [Cache] found:2&#xA;[wp] Proved goals:    3 / 4&#xA;  Terminating:     1&#xA;  Unreachable:     1&#xA;  Qed:             1 (0.72ms)&#xA;  Timeout:         1&#xA;------------------------------------------------------------&#xA;  Function bob&#xA;------------------------------------------------------------&#xA;&#xA;Goal Post-condition (file test.c, line 110) in &#x27;bob&#x27;:&#xA;Let x = lsl(1, a).&#xA;Assume {&#xA;  Type: is_sint32(a) /\ is_sint32(x).&#xA;  (* Pre-condition *)&#xA;  Have: (0 &amp;lt;= a) /\ (a &amp;lt;= 2).&#xA;}&#xA;Prove: x &amp;lt;= 9.&#xA;Prover Alt-Ergo 2.6.2 returns Timeout (Qed:1ms) (2s) (cached)&#xA;Prover Z3 4.8.12 returns Timeout (Qed:1ms) (2s) (cached)&#xA;&#xA;------------------------------------------------------------&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;According to the WP manual there is a &lt;em&gt;shift&lt;/em&gt; strategy that should be able to transform  &lt;code&gt;lsl(a,k)&lt;/code&gt; to &lt;code&gt;a*2^k&lt;/code&gt; and with that transformation it feels like this should be automatically provable (Right??)&lt;/p&gt;&#xA;&lt;p&gt;I have tried defining a strategy with:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-c prettyprint-override&quot;&gt;&lt;code&gt;/*@&#xA;@strategy shift:&#xA;  \tactic(&amp;quot;Wp.shift&amp;quot;);&#xA;*/&#xA;&#xA;/*@&#xA;  requires a &amp;gt;= 0 &amp;amp;&amp;amp; a &amp;lt;= 2;&#xA;  assigns \nothing;&#xA;  ensures \result &amp;lt; 10;&#xA;*/&#xA;int bob(int a) {&#xA;  return 1 &amp;lt;&amp;lt; a;&#xA;}&#xA;&#xA;&#xA;//@proof shift : bob;&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;And I have tried applying the strategy with the &lt;code&gt;-wp-strategy=&amp;quot;shift&amp;quot;&lt;/code&gt; flag as well but i has had no effect.&lt;/p&gt;&#xA;&lt;p&gt;Is there any other explicit thing that I have to do in order to get any of the provers to handle the &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; operator?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/71718219</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">If clause in Z3py without the else</title>
            <category scheme="https://stackoverflow.com/tags" term="python" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
        <author>
            <name>Mario</name>
            <uri>https://stackoverflow.com/users/12385200</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/71718219/if-clause-in-z3py-without-the-else" />
        <published>2022-04-02T14:04:07Z</published>
        <updated>2025-08-12T22:49:21Z</updated>
        <summary type="html">
            &lt;p&gt;How do we write an If(&#x27;condition&#x27;, x, y) clause in Z3py if we don&#x27;t have the &#x27;else&#x27; part ?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79715293</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Z3 python simplify without monomials on bitvectors</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
            <category scheme="https://stackoverflow.com/tags" term="sat" />
        <author>
            <name>Jorayen</name>
            <uri>https://stackoverflow.com/users/5672025</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79715293/z3-python-simplify-without-monomials-on-bitvectors" />
        <published>2025-07-25T22:35:40Z</published>
        <updated>2025-07-25T22:35:40Z</updated>
        <summary type="html">
            &lt;p&gt;I have the following expression:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-py prettyprint-override&quot;&gt;&lt;code&gt;a = z3.BitVec(&#x27;a&#x27;, 32)&#xA;b = z3.BitVec(&#x27;b&#x27;, 32)&#xA;&#xA;e = a - b &amp;lt; z3.BitVecVal(0, 32)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;code&gt;z3.simplify&lt;/code&gt; returns:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-py prettyprint-override&quot;&gt;&lt;code&gt;Not(0 &amp;lt;= a &#x2B; 4294967295*b)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I even tried with options such as &lt;code&gt;som=False&lt;/code&gt; but to no avail, what can I do to prevent this rewrite with monomials?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/70250089</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">z3 sort mismatch for different bytes of the same variable [closed]</title>
            <category scheme="https://stackoverflow.com/tags" term="python" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="solver" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
        <author>
            <name>PaoloJ42</name>
            <uri>https://stackoverflow.com/users/13547857</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/70250089/z3-sort-mismatch-for-different-bytes-of-the-same-variable" />
        <published>2021-12-06T18:20:47Z</published>
        <updated>2025-07-14T04:27:01Z</updated>
        <summary type="html">
            &lt;p&gt;I&#x27;m simulating the behaviour of an algorithm using z3 python package, however I&#x27;m encountering problems using a bitwise XOR.&lt;/p&gt;&#xA;&lt;p&gt;At first I define&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;    class State:&#xA;        def __init__(self):&#xA;            self.state = [0] * 0x270&#xA;            self.index = 0&#xA;&#xA;    def mag(i):&#xA;        return z3.If(i == 0, 0x0, 0x9908b0df)&#xA;&#xA;seed = z3.BitVec(&#x27;seed&#x27;, 32)&#xA;s= State()&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;And the script goes on, however when I run the Solver I get a &lt;code&gt;z3.z3types.Z3Exception: sort mismatch&lt;/code&gt; caused by trying to execute the &lt;code&gt;__xor__&lt;/code&gt; in the line&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;s.state[i] = s.state[i &#x2B; 0x18d] ^ ((s.state[i &#x2B; 1] &amp;amp; 0x7fffffff | s.state[i] &amp;amp; 0x80000000) &amp;gt;&amp;gt; 1) ^ mag((s.state[1] &amp;amp; 1) *8)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Here every entry in s.state depends on the symbolic value of the seed.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79692271</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Indexing array by bv32 in Dafny causes timeout</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="dafny" />
            <category scheme="https://stackoverflow.com/tags" term="formal-verification" />
        <author>
            <name>AlumKal</name>
            <uri>https://stackoverflow.com/users/22908765</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79692271/indexing-array-by-bv32-in-dafny-causes-timeout" />
        <published>2025-07-07T03:24:26Z</published>
        <updated>2025-07-07T11:03:30Z</updated>
        <summary type="html">
            &lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;ghost predicate AllZero(a: array&amp;lt;int&amp;gt;, n: bv32)&#xA;    reads a {&#xA;    a.Length == (n as int) &amp;amp;&amp;amp;&#xA;    (forall i :: 0 &amp;lt;= i &amp;lt; n ==&amp;gt; a[i] == 0)&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I need to use the result of binary arithmetic to index an array. However, I found that even trivial array indexing expressions take forever to verify. Changing the type of &lt;code&gt;n&lt;/code&gt; to &lt;code&gt;int&lt;/code&gt; makes the code verify instantly.&lt;/p&gt;&#xA;&lt;p&gt;What auxiliary lemma should I add to make it work?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79690606</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Control recursive function evaluation depth in eval</title>
            <category scheme="https://stackoverflow.com/tags" term="recursion" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
        <author>
            <name>David Detlefs</name>
            <uri>https://stackoverflow.com/users/30967288</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79690606/control-recursive-function-evaluation-depth-in-eval" />
        <published>2025-07-04T19:24:51Z</published>
        <updated>2025-07-04T19:27:20Z</updated>
        <summary type="html">
            &lt;p&gt;As an exercise, to get a good idea of what Z3 can and can&#x27;t do, I was trying to solve a &amp;quot;coraline&amp;quot; puzzle.  This is a rectangular grid, where each square is a graph node of degree two: each edge is to an adjacent node.  So a square could be top-left, top-right, bottom-left, bottom-right, right-left, or top-bottom.  A coraline problem specifies the grid size, and these values for some subset of the squares.  The solution must be a path through all the squares -- a Hamiltonion path in the graph view.&lt;/p&gt;&#xA;&lt;p&gt;My thought was to encode the connectivity constraints, and then encode the &amp;quot;single path&amp;quot; idea by defining an order on coordinates, and defining a recursive function that find the minimum coordinate reachable from a given coordinate.  The single-path constraint would then be that this function yields (0, 0) for all squares.&lt;/p&gt;&#xA;&lt;p&gt;Well, that doesn&#x27;t work (at least not yet).  But I have some simpler questions before I ask why.&lt;/p&gt;&#xA;&lt;p&gt;For a 4x2 grid, with no initial conditions, if I comment out the single-path constraint, it finds a satisfying solution.  The one it finds happens to satisfy the single-path constraint.  The code is:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(declare-datatypes () ((SquareType TL TR BL BR TB RL)))&#xA;(define-fun square-to-string ((st SquareType)) String&#xA;        (ite (= st TL) &amp;quot;TL&amp;quot;&#xA;         (ite (= st TR) &amp;quot;TR&amp;quot;&#xA;              (ite (= st BL) &amp;quot;BL&amp;quot;&#xA;               (ite (= st BR) &amp;quot;BR&amp;quot;&#xA;                (ite (= st RL) &amp;quot;RL&amp;quot;&#xA;                     (ite (= st TB) &amp;quot;TB&amp;quot;&#xA;                      &amp;quot;unknown&amp;quot;)))))))&#xA;&#xA;(define-sort AISQ () (Array Int SquareType))&#xA;(define-sort Board () (Array Int AISQ))&#xA;&#xA;(declare-const Width Int)&#xA;(declare-const Height Int)&#xA;&#xA;(define-fun is-x-index ((x Int)) Bool (and (&amp;gt;= x 0) (&amp;lt; x Width)))&#xA;(define-fun is-y-index ((y Int)) Bool (and (&amp;gt;= y 0) (&amp;lt; y Height)))&#xA;&#xA;(define-fun get-square ((b Board) (x Int) (y Int)) SquareType&#xA;        (select (select b x) y))&#xA;&#xA;(define-fun square-constraint ((b Board) (x Int) (y Int)) Bool&#xA;        (and&#xA;         (implies (and (&amp;gt; x 0) (or (= (get-square b x y) TL)&#xA;                       (= (get-square b x y) BL)&#xA;                       (= (get-square b x y) RL)))&#xA;              (or (= (get-square b (- x 1) y) TR)&#xA;              (= (get-square b (- x 1) y) BR)&#xA;              (= (get-square b (- x 1) y) RL)))&#xA;         (implies (= x 0)&#xA;              (or (= (get-square b x y) TR)&#xA;              (= (get-square b x y) BR)&#xA;              (= (get-square b x y) TB)&#xA;              ))&#xA;&#xA;         (implies (and (&amp;lt; x (- Width 1)) (or (= (get-square b x y) TR)&#xA;                         (= (get-square b x y) BR)&#xA;                         (= (get-square b x y) RL)))&#xA;              (or (= (get-square b (&#x2B; x 1) y) TL)&#xA;              (= (get-square b (&#x2B; x 1) y) BL)&#xA;              (= (get-square b (&#x2B; x 1) y) RL)))&#xA;         (implies (= x (- Width 1))&#xA;              (or (= (get-square b x y) TL)&#xA;              (= (get-square b x y) BL)&#xA;              (= (get-square b x y) TB)&#xA;              ))&#xA;         &#xA;         (implies (and (&amp;gt; y 0) (or (= (get-square b x y) TR)&#xA;                       (= (get-square b x y) TL)&#xA;                       (= (get-square b x y) TB)))&#xA;              (or (= (get-square b x (- y 1)) BL)&#xA;              (= (get-square b x (- y 1)) BR)&#xA;              (= (get-square b x (- y 1)) TB)))&#xA;         (implies (= y 0)&#xA;              (or (= (get-square b x y) BR)&#xA;              (= (get-square b x y) BL)&#xA;              (= (get-square b x y) RL)))&#xA;&#xA;         (implies (and (&amp;lt; y (- Height 1)) (or (= (get-square b x y) BR)&#xA;                          (= (get-square b x y) BL)&#xA;                          (= (get-square b x y) TB)))&#xA;              (or (= (get-square b x (&#x2B; y 1)) TL)&#xA;              (= (get-square b x (&#x2B; y 1)) TR)&#xA;              (= (get-square b x (&#x2B; y 1)) TB)))&#xA;         (implies (= y (- Height 1))&#xA;              (or (= (get-square b x y) TR)&#xA;              (= (get-square b x y) TL)&#xA;              (= (get-square b x y) RL)&#xA;              ))&#xA;         ))&#xA;&#xA;;;; LeftTopMin&#xA;(declare-datatypes () ((Coord (mk-coord (getx Int) (gety Int)))))&#xA;&#xA;(declare-datatypes () ((Dir TDir BDir RDir LDir)))&#xA;&#xA;(define-fun dir-2-str ((dir Dir)) String&#xA;        (ite (= dir TDir) &amp;quot;T&amp;quot;&#xA;         (ite (= dir BDir) &amp;quot;B&amp;quot;&#xA;              (ite (= dir RDir) &amp;quot;R&amp;quot; &amp;quot;L&amp;quot;))))&#xA;&#xA;(define-fun coord-lt ((c0 Coord) (c1 Coord)) Bool&#xA;        (or (&amp;lt; (getx c0) (getx c1))&#xA;        (and (= (getx c0) (getx c1))&#xA;             (&amp;lt; (gety c0) (gety c1)))))&#xA;&#xA;(define-fun min-coord ((c0 Coord) (c1 Coord)) Coord&#xA;        (ite (coord-lt c0 c1) c0 c1))&#xA;&#xA;(define-fun out-dir ((st SquareType) (inDir Dir)) Dir&#xA;        (ite (= st TL)&#xA;         (ite (= inDir TDir) LDir TDir)&#xA;         (ite (= st TR)&#xA;              (ite (= inDir TDir) RDir TDir)&#xA;              (ite (= st BL)&#xA;               (ite (= inDir BDir) LDir BDir)&#xA;               (ite (= st BR)&#xA;                (ite (= inDir BDir) RDir BDir)&#xA;                (ite (= st RL)&#xA;                     (ite (= inDir RDir) LDir RDir)&#xA;                     (ite (= inDir TDir) BDir TDir)))))))&#xA;&#xA;;; Pick a direction that is one of the dirs of the SqureType&#xA;(define-fun pick-dir ((st SquareType)) Dir&#xA;        (ite (or (= st TL) (= st TR) (= st TB)) TDir&#xA;         (ite (or (= st BL) (= st BR)) BDir&#xA;              RDir)))&#xA;&#xA;;; The reverse of the dir.&#xA;(define-fun reverse-dir ((dir Dir)) Dir&#xA;  (ite (= dir TDir) BDir&#xA;       (ite (= dir BDir) TDir&#xA;        (ite (= dir RDir) LDir RDir))))&#xA;&#xA;;; The x value of the next square to consider (given x and, nextDir,&#xA;;; the direction in which to leave the current square).&#xA;(define-fun next-x ((x Int) (nextDir Dir)) Int&#xA;        (ite (= nextDir RDir)&#xA;         (&#x2B; x 1)&#xA;         (ite (= nextDir LDir)&#xA;              (- x 1)&#xA;              x)))&#xA;;; The y value of the next square to consider (given y and, nextDir,&#xA;;; the direction in which to leave the current square).&#xA;(define-fun next-y ((y Int) (nextDir Dir)) Int&#xA;        (ite (= nextDir TDir)&#xA;         (- y 1)&#xA;         (ite (= nextDir BDir)&#xA;              (&#x2B; y 1)&#xA;              y)))&#xA;&#xA;(define-fun-rec min-coord-in-cc-work&#xA;    ((b Board)&#xA;     (cur Coord) ;; The coordinate of the current square to consider.&#xA;     (in Dir)  ;; The direction -- on of the two dirs of the square&#x27;s value --&#xA;     ;; at which the path reaches the square.&#xA;     (start Coord) ;; The start of the path&#xA;     (min Coord)) ;; The min coord observed on the path so far.&#xA;  Coord&#xA;  (let ((x (getx cur)) (y (gety cur)))&#xA;    (let ((st (get-square b x y)))&#xA;      (let ((nextDir (out-dir st in)))&#xA;    (let ((nextX (next-x x nextDir))&#xA;          (nextY (next-y y nextDir)))&#xA;      (ite (= cur start)&#xA;           min&#xA;           (min-coord-in-cc-work b (mk-coord nextX nextY)&#xA;                     (reverse-dir nextDir)&#xA;                     start (min-coord min cur))))))))&#xA;&#xA;;; Do we reach all the coords?&#xA;(declare-datatypes ()&#xA;           ((NextInputs (mk-inputs (getCoord Coord) (getDir Dir)))))&#xA;&#xA;&#xA;&#xA;(define-fun min-coord-in-cc ((b Board) (c Coord))&#xA;  Coord&#xA;  (let ((x (getx c)) (y (gety c)))&#xA;    (let ((st (get-square b x y)))&#xA;      (let ((nextDir (out-dir st (pick-dir (get-square b (getx c) (gety c))))))&#xA;    (min-coord-in-cc-work&#xA;     b&#xA;     (mk-coord (next-x (getx c) nextDir)&#xA;           (next-y (gety c) nextDir))&#xA;     (reverse-dir nextDir)&#xA;     c c)))))&#xA;&#xA;(define-fun is-solution ((b Board))&#xA;  Bool&#xA;  (forall ((x Int) (y Int))&#xA;      (implies (and (is-x-index x)&#xA;            (is-y-index y))&#xA;           (and&#xA;            (square-constraint b x y)&#xA;            ; (= (min-coord-in-cc b (mk-coord x y)) (mk-coord 0 0))&#xA;            ))))&#xA;&#xA;&#xA;(declare-const b0 Board)&#xA;&#xA;(assert (= Width 4))&#xA;(assert (= Height 2))&#xA;(assert (is-solution b0))&#xA;(check-sat)&#xA;&#xA;(echo &amp;quot;row 0&amp;quot;)&#xA;(eval (square-to-string (get-square b0 0 0)))&#xA;(eval (square-to-string (get-square b0 1 0)))&#xA;(eval (square-to-string (get-square b0 2 0)))&#xA;(eval (square-to-string (get-square b0 3 0)))&#xA;&#xA;(echo &amp;quot;row 1&amp;quot;)&#xA;(eval (square-to-string (get-square b0 0 1)))&#xA;(eval (square-to-string (get-square b0 1 1)))&#xA;(eval (square-to-string (get-square b0 2 1)))&#xA;(eval (square-to-string (get-square b0 3 1)))&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;(This code makes the perhaps-unfortunate choice that the y coordinates grow &amp;quot;downwards&amp;quot; (wrt the Top/Bottom convention) -- apologies.)&#xA;When I run this I get:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;sat&#xA;row 0&#xA;&amp;quot;BR&amp;quot;&#xA;&amp;quot;RL&amp;quot;&#xA;&amp;quot;RL&amp;quot;&#xA;&amp;quot;BL&amp;quot;&#xA;row 1&#xA;&amp;quot;TR&amp;quot;&#xA;&amp;quot;RL&amp;quot;&#xA;&amp;quot;RL&amp;quot;&#xA;&amp;quot;TL&amp;quot;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;If I comment out the&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(assert (is-solution b0))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;constraint, and instead assume the solution found:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(assert (= (select (select b0 0) 0) BR))&#xA;(assert (= (select (select b0 1) 0) RL))&#xA;(assert (= (select (select b0 2) 0) RL))&#xA;(assert (= (select (select b0 3) 0) BL))&#xA;(assert (= (select (select b0 0) 1) TR))&#xA;(assert (= (select (select b0 1) 1) RL))&#xA;(assert (= (select (select b0 2) 1) RL))&#xA;(assert (= (select (select b0 3) 1) TL))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;then I can verify that my min-coord-in-cc function is working properly:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(eval (min-coord-in-cc b0 (mk-coord 0 0)))&#xA;(eval (min-coord-in-cc b0 (mk-coord 1 0)))&#xA;(eval (min-coord-in-cc b0 (mk-coord 2 0)))&#xA;(eval (min-coord-in-cc b0 (mk-coord 3 0)))&#xA;(eval (min-coord-in-cc b0 (mk-coord 0 1)))&#xA;(eval (min-coord-in-cc b0 (mk-coord 1 1)))&#xA;(eval (min-coord-in-cc b0 (mk-coord 2 1)))&#xA;(eval (min-coord-in-cc b0 (mk-coord 3 1)))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;yields what I expect:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(mk-coord 0 0)&#xA;(mk-coord 0 0)&#xA;(mk-coord 0 0)&#xA;(mk-coord 0 0)&#xA;(mk-coord 0 0)&#xA;(mk-coord 0 0)&#xA;(mk-coord 0 0)&#xA;(mk-coord 0 0)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;However, if do this printing of min-coord-in-cc values using the (same!) solution found via the is-solution constraint, the expression returned is much more complicated.  Here is the first one:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(let ((a!1 (lambda ((x!1 Int))&#xA;             (let ((a!1 (ite (and (&amp;lt;= 1 x!1) (not (&amp;lt;= 2 x!1)))&#xA;                             ((as const (Array Int SquareType)) RL)&#xA;                             (store ((as const (Array Int SquareType)) RL) 5 TL))))&#xA;             (let ((a!2 (ite (&amp;lt;= 1 x!1)&#xA;                             (ite (&amp;lt;= 3 x!1)&#xA;                                  (store ((as const (Array Int SquareType)) TL)&#xA;                                         0&#xA;                                         BL)&#xA;                                  a!1)&#xA;                             (store ((as const (Array Int SquareType)) TR) 0 BR))))&#xA;               (ite (and (not (&amp;lt;= 3 x!1)) (&amp;lt;= 2 x!1))&#xA;                    ((as const (Array Int SquareType)) RL)&#xA;                    a!2)))))&#xA;      (a!11 (and (= (getx (mk-coord 0 0)) (getx (mk-coord 1 0)))&#xA;                 (&amp;lt; (gety (mk-coord 0 0)) (gety (mk-coord 1 0))))))&#xA;(let ((a!2 (select (select a!1 (getx (mk-coord 1 0))) (gety (mk-coord 1 0))))&#xA;      (a!12 (or (&amp;lt; (getx (mk-coord 0 0)) (getx (mk-coord 1 0))) a!11)))&#xA;(let ((a!3 (ite (= a!2 BR)&#xA;                (ite (= LDir BDir) RDir BDir)&#xA;                (ite (= a!2 RL)&#xA;                     (ite (= LDir RDir) LDir RDir)&#xA;                     (ite (= LDir TDir) BDir TDir)))))&#xA;(let ((a!4 (ite (= a!2 TR)&#xA;                (ite (= LDir TDir) RDir TDir)&#xA;                (ite (= a!2 BL) (ite (= LDir BDir) LDir BDir) a!3))))&#xA;(let ((a!5 (ite (= a!2 TL) (ite (= LDir TDir) LDir TDir) a!4)))&#xA;(let ((a!6 (ite (= a!5 LDir) (- (getx (mk-coord 1 0)) 1) (getx (mk-coord 1 0))))&#xA;      (a!8 (ite (= a!5 BDir) (&#x2B; (gety (mk-coord 1 0)) 1) (gety (mk-coord 1 0))))&#xA;      (a!10 (ite (= a!5 TDir)&#xA;                 BDir&#xA;                 (ite (= a!5 BDir) TDir (ite (= a!5 RDir) LDir RDir)))))&#xA;(let ((a!7 (ite (= a!5 RDir) (&#x2B; (getx (mk-coord 1 0)) 1) a!6))&#xA;      (a!9 (ite (= a!5 TDir) (- (gety (mk-coord 1 0)) 1) a!8)))&#xA;  (ite (= (mk-coord 1 0) (mk-coord 0 0))&#xA;       (mk-coord 0 0)&#xA;       (min-coord-in-cc-work&#xA;         a!1&#xA;         (mk-coord a!7 a!9)&#xA;         a!10&#xA;         (mk-coord 0 0)&#xA;         (ite a!12 (mk-coord 0 0) (mk-coord 1 0)))))))))))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Given the fact that it found the right solution, it seems like it ought to be able to evalutate this.  Looking just at the end of the expression, shouldn&#x27;t it (for example) be able to decide the&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;  (ite (= (mk-coord 1 0) (mk-coord 0 0))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;It seems like deciding that would make this expression tail-recursive.  Am I hitting some sort of limit on recursion depth here?  Is there some way to control that?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79677807</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">How to use SMT tools to prove a simple theorem involving modulo [closed]</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
            <category scheme="https://stackoverflow.com/tags" term="cvc5" />
        <author>
            <name>Steve Siegel</name>
            <uri>https://stackoverflow.com/users/3672008</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79677807/how-to-use-smt-tools-to-prove-a-simple-theorem-involving-modulo" />
        <published>2025-06-24T15:03:05Z</published>
        <updated>2025-06-24T15:39:23Z</updated>
        <summary type="html">
            &lt;p&gt;I am trying to prove or refute the following:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;   ((pid&#x2B;1)%NP &#x2B; i)%NP = (pid &#x2B; i &#x2B; 1)%NP&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;assuming &lt;code&gt;pid&lt;/code&gt;, &lt;code&gt;i&lt;/code&gt;, and &lt;code&gt;NP&lt;/code&gt; are integers, &lt;code&gt;0&#x2264;pid&amp;lt;NP&lt;/code&gt;, and &lt;code&gt;0&#x2264;i&lt;/code&gt;.&#xA;I think it&#x27;s true, but in any case, I cannot get any SMT tool to prove or refute it.&lt;/p&gt;&#xA;&lt;p&gt;Here is how I expressed the problem in SMT2:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(declare-const i Int)&#xA;(declare-const pid Int)&#xA;(declare-const NP Int)&#xA;(assert (&amp;lt;= 0 pid))&#xA;(assert (&amp;lt; pid NP))&#xA;(assert (&amp;lt;= 0 i))&#xA;(assert (not&#xA;  (= (mod (&#x2B; (mod (&#x2B; pid 1) NP) i) NP)&#xA;     (mod (&#x2B; pid i 1) NP))))&#xA;(check-sat)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I think it should return nsat, but neither CVC5 nor Z3 ever returns on this input.&#xA;Any hints?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79668355</id>
        <re:rank scheme="https://stackoverflow.com">2</re:rank>
        <title type="text">Z3 Lookup sorts with Cpp API without Constructors Definitions</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>Karidus</name>
            <uri>https://stackoverflow.com/users/23081748</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79668355/z3-lookup-sorts-with-cpp-api-without-constructors-definitions" />
        <published>2025-06-17T00:26:47Z</published>
        <updated>2025-06-17T00:26:47Z</updated>
        <summary type="html">
            &lt;p&gt;I&#x27;m currently trying to get information in some way of all the sorts present in an smt2 file, via the Cpp API. As I want to retrieve the values that are assigned to the sorts in the formula and assuming that there are no constructors for them defined. While I was following this logic I found the &lt;code&gt;m.get_func_decl()&lt;/code&gt;. Which made it possible to retrieve the sorts of the argument functions. With no constructors which is what I need. &lt;code&gt;(declare-fun TestFunc (Int Bool Int) Int)&lt;/code&gt;. But when I use a &lt;code&gt;define-fun&lt;/code&gt;, this &lt;code&gt;(define-fun Main@main ((n Int)) (Int) ...&lt;/code&gt;. is not available to access with &lt;code&gt;m.get_func_decl()&lt;/code&gt;, I suppose some optimization happens that removes the declaration. So, my question is, are there any alternatives to the goal that I have in mind of getting a list of the sorts used in the smt file with no constructors?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/59137933</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Issues with real value calculation in z3</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>Kshitij Goyal</name>
            <uri>https://stackoverflow.com/users/6633601</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/59137933/issues-with-real-value-calculation-in-z3" />
        <published>2019-12-02T11:24:26Z</published>
        <updated>2025-06-15T03:15:42Z</updated>
        <summary type="html">
            &lt;p&gt;I am working on a problem where I want to learn real parameters based on certain constraints. Following is a snippet of the code:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code&gt;s = Solver()&#xA;def logistic_function(x, y):&#xA;   out = y[0]&#xA;   for i in range(len(x)):&#xA;      out = out &#x2B; x[i] * y[i &#x2B; 1]&#xA;   return out&#xA;&#xA;self.W = RealVector(&#x27;w&#x27;, X.shape[1]&#x2B;1)&#xA;self.F = RealVector(&#x27;f&#x27;, X.shape[0])&#xA;&#xA;for h in hard_instances:&#xA;  if y[h] == 1:&#xA;      self.model.add(Learner.logistic_function(list(X.iloc[h]), self.W) &amp;gt;= 0)         &#xA;  else:&#xA;      self.model.add(Learner.logistic_function(list(X.iloc[h]), self.W) &amp;lt; 0)         &#xA;  self.model.add(Learner.logistic_function(list(X.iloc[h]), self.W) == self.F[h])&#xA;&#xA;s.check()&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&#xA;&lt;p&gt;Where {X,y} is a dataset and hard_instances is a set of indexes that I am working with. After I get a model solution from the z3, I manually calculate the Learner.logistic_function(list(X.iloc[h]), self.W) value for each of the indices in the hard_instance by extracting the W and compare it with the F[h] values. Following are the results for both (with the size of hard_instances being 100):&lt;/p&gt;&#xA;&#xA;&lt;p&gt;Confidence value calculated using z3 output:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code&gt;[-27.378400928361582, -24.54479132404113, -24.307289651276747, -31.70713297755848, -30.762167609027458, -31.315939646075787, -33.00420507718851, -31.744112911331754, -26.23355531746848, -31.36228488104281, -30.427736819536484, -31.50527359981793, -42.88965873739677, -31.707129367228667, -31.56210015506779, -32.12409972766397, -31.70713297755848, -30.031558658483476, -18.358324137431104, -32.05022247782185, -41.531034659230095, -32.29000919967995, -31.75974435910986, -31.663303581555095, -31.492373296661544, -31.31746775645906, -31.707165983877054, -31.347401145915946, -9013.822052472326, -31.75724273178162, -38.284678733394, -290.3139883637738, -38.55432745005057, 186.6069890256151, -44.1131569461781, -3965.3468548458463, -30.19582424657528, -31.81069063619864, -30.619869067329255, -31.58128167212442, -31.822174319008383, -37.18356870531899, -33.442884835165096, -51.320302912234084, -267.50833857889654, -28.402318357266232, -31.62745176425138, 416.1353823972281, -40.42543492186646, -28.541400567435975, -94.80187721209138, -32.013861248574415, -29.42849153859601, -32.14935341971468, -31.20975052479889, 34.2925702396417, -52.91711293409269, -31.772331866138927, -28.05140296433753, -36.58557486365847, -31.83338866474074, -36.5299223283415, -31.327926505869392, -199.5517369747143, -32.08369384912356, -32.07316618164427, -98.62741827618949, -1003.470954079502, -31.240876251803435, 456.34073138747084, -64.27303567782826, -49.714357622299886, -31.905532688175438, 15.611397869700923, 518.5055614575923, -30.65519656405803, -72.45941570859743, -31.967928531880276, -30.55418177994407, -31.225101988980224, -395.9939788509901, -53.142500004465916, -29.61894900393206, -31.756397212326476, -32.51642103656665, -31.12483808710671, -30.768528286960102, -765.2299421009044, 240.09127856915606, 47.958346445463505, -30.42562757004379, -34.02946877293487, 245.48085838630791, -53.48190520867068, -11.398510468740053, -27.119576978335733, -1472.8001539856432, -7.909727924310422, -31.984175109074794, -1246.733548266756]&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&#xA;&lt;p&gt;Values of the F variable:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code&gt;[&#x27;6.63397?&#x27;, &#x27;48.31824?&#x27;, &#x27;9.84546?&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;0&#x27;, &#x27;-0.5&#x27;, &#x27;-5.38594?&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;2.67479?&#x27;, &#x27;-3.55787?&#x27;, &#x27;-10.59216?&#x27;, &#x27;-0.5&#x27;, &#x27;-2.87871?&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;21.51906?&#x27;, &#x27;-0.84313?&#x27;, &#x27;0&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;-3.33203?&#x27;, &#x27;0&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;-8980.16307?&#x27;, &#x27;-0.5&#x27;, &#x27;-7.06012?&#x27;, &#x27;-248.88109?&#x27;, &#x27;-6.90423?&#x27;, &#x27;-0.5&#x27;, &#x27;-12.90605?&#x27;, &#x27;-3945.30152?&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;2.09643?&#x27;, &#x27;0.57093?&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;-227.57308?&#x27;, &#x27;0&#x27;, &#x27;0&#x27;, &#x27;490.58834?&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;-20.46458?&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;75.43883?&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;3.39666?&#x27;, &#x27;-0.5&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;0&#x27;, &#x27;-91.32681?&#x27;, &#x27;-1.13005?&#x27;, &#x27;0&#x27;, &#x27;-41.40830?&#x27;, &#x27;-972.60267?&#x27;, &#x27;0&#x27;, &#x27;-0.5&#x27;, &#x27;0&#x27;, &#x27;0&#x27;, &#x27;-0.5&#x27;, &#x27;43.98384?&#x27;, &#x27;570.67543?&#x27;, &#x27;-0.5&#x27;, &#x27;-41.06592?&#x27;, &#x27;0&#x27;, &#x27;8.24429?&#x27;, &#x27;-3.43914?&#x27;, &#x27;-377.45813?&#x27;, &#x27;-21.85040?&#x27;, &#x27;0&#x27;, &#x27;-11.83317?&#x27;, &#x27;-4.00421?&#x27;, &#x27;-0.5&#x27;, &#x27;0.53349?&#x27;, &#x27;-691.19144?&#x27;, &#x27;264.52677?&#x27;, &#x27;68.84287?&#x27;, &#x27;9.83243?&#x27;, &#x27;3.52497?&#x27;, &#x27;269.78794?&#x27;, &#x27;-21.63869?&#x27;, &#x27;40.52079?&#x27;, &#x27;6.89110?&#x27;, &#x27;-1451.54107?&#x27;, &#x27;26.21240?&#x27;, &#x27;0&#x27;, &#x27;-1190.68525?&#x27;]&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&#xA;&lt;p&gt;There seems to be a huge difference in the values calculated within z3 (represented by F) and values I calculate based on the solution that I get from z3. These values should be same.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;Also, hard_instances are random samples from the full dataset. This kind of discrepancy happens only with some samples. For a lot of the samples, the value I calculate and the value I get from z3 are same. Also, there is no discrepancy if I use the Integer solver and learn integer parameters instead of real ones. &lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79665279</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Detecting an unbounded objective in Z3 OCaml API</title>
            <category scheme="https://stackoverflow.com/tags" term="ocaml" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
        <author>
            <name>David Monniaux</name>
            <uri>https://stackoverflow.com/users/2207776</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79665279/detecting-an-unbounded-objective-in-z3-ocaml-api" />
        <published>2025-06-13T19:08:54Z</published>
        <updated>2025-06-13T19:08:54Z</updated>
        <summary type="html">
            &lt;p&gt;Is there a way in the OCaml Z3 API to detect, after optimizing a problem and getting a model, to check if the objective was actually unbounded?&lt;/p&gt;&#xA;&lt;p&gt;One method I see is to add a constraint &amp;quot;I want something bigger than this value I obtained&amp;quot;, but that sounds a bit overkill..&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79663536</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Avoiding `unknown` for *, / over Reals?</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>Jason Kleban</name>
            <uri>https://stackoverflow.com/users/162273</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79663536/avoiding-unknown-for-over-reals" />
        <published>2025-06-12T12:25:54Z</published>
        <updated>2025-06-12T19:53:51Z</updated>
        <summary type="html">
            &lt;p&gt;I&#x27;m trying find a solution to a problem of dividing elements from a Sequence into N groups that are roughly evenly distributed.  I thought I was making good progress by using a &amp;quot;G-Test&amp;quot; and setting a goal of it being under some maximum value, but I keep getting &amp;quot;unknown&amp;quot; and I think I narrowed it down to this &lt;code&gt;(ln-approx x)&lt;/code&gt; function.  Since z3 can&#x27;t do &lt;code&gt;(ln x)&lt;/code&gt; I am hoping to use this approximation, but even when avoiding &lt;code&gt;x = -1&lt;/code&gt; I don&#x27;t see success.  I get &lt;code&gt;unknown&lt;/code&gt; and a reason of &lt;code&gt;canceled&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Is there a way to avoid this &lt;code&gt;unknown&lt;/code&gt;?&lt;/p&gt;&#xA;&lt;p&gt;Is there a better technique for evaluating a solutions &amp;quot;cardinal balance&amp;quot; among partitions, I might call it?&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;; approximation that looks close enough for me on the graph for [0 .. 1]&#xA;(define-fun ln-approx ((x Real)) Real (ite (&amp;lt;= 0.0 x 1.0) (* 2.0 (/ (- x 1.0) (&#x2B; x 1.0))) 1000.0))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;By the way, is there a way to write in native z3 to &lt;code&gt;(get-model)&lt;/code&gt; only if &lt;code&gt;(check-sat)&lt;/code&gt; &lt;code&gt;sat&lt;/code&gt; and ask for &lt;code&gt;reasons&lt;/code&gt; and other diagnostics if &lt;code&gt;unsat&lt;/code&gt;?&lt;/p&gt;&#xA;&lt;p&gt;Here is the function I&#x27;m using instead of &lt;code&gt;ln&lt;/code&gt; compared to &lt;code&gt;ln&lt;/code&gt; which I assume will be close enough for a useful &lt;a href=&quot;https://en.wikipedia.org/wiki/G-test&quot; rel=&quot;nofollow noreferrer&quot;&gt;g-test&lt;/a&gt; calculation.  Since &lt;code&gt;0 &amp;lt; O &amp;lt; E&lt;/code&gt;, then &lt;code&gt;0.0 &amp;lt; O/E &amp;lt;= 1.0&lt;/code&gt;.  The &lt;code&gt;E = 0&lt;/code&gt; case is &lt;code&gt;ite&lt;/code&gt;d in my g-test implementation.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&quot;https://www.desmos.com/calculator/1wh6g3glkx&quot; rel=&quot;nofollow noreferrer&quot;&gt;https://www.desmos.com/calculator/1wh6g3glkx&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&quot;https://i.sstatic.net/BLEIufzu.png&quot; rel=&quot;nofollow noreferrer&quot;&gt;&lt;img src=&quot;https://i.sstatic.net/BLEIufzu.png&quot; alt=&quot;graphs of ln(x) vs ln-approx(x)&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;For background, here&#x27;s my current g-test implementation, (missing some of the supporting types):&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;; g-test of an attribute&#xA;(define-fun attr-g-test ((attr-count-accessor (Array AttrCounts Int))) Real&#xA;    (* 2.0&#xA;    (seq.foldli&#xA;        (lambda ((group_0_based Int) (half-score Real) (ignored Bool))&#xA;            (let &#xA;                ((O (attr-count-accessor (attr-counts students (&#x2B; group_0_based 1))))&#xA;                (E (attr-count-accessor G_COUNTS)))&#xA;                (&#x2B; half-score (ite (&amp;lt; 0 O E) (* O (ln-approx (/ O E))) 0.0)))&#xA;                ;(&#x2B; half-score (* O 1.2))) ;; something like this instead is `sat` (but otherwise useless)&#xA;        )&#xA;        0 ; offset&#xA;        0.0 ; null-hypothesis initial value&#xA;        group-iterable-range))&#xA;)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/38605639</id>
        <re:rank scheme="https://stackoverflow.com">3</re:rank>
        <title type="text">How to represent logarithmic formula in z3py</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
        <author>
            <name>M Zhang</name>
            <uri>https://stackoverflow.com/users/6625168</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/38605639/how-to-represent-logarithmic-formula-in-z3py" />
        <published>2016-07-27T06:48:02Z</published>
        <updated>2025-06-11T12:41:14Z</updated>
        <summary type="html">
            &lt;p&gt;I am very new to z3py. I am trying to code the following logarithmic expressions in z3py .&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code&gt;log(x,y)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&#xA;&lt;p&gt;I did search stack overflow a lot and came across a similar question, but unfortunately I could not get a satisfactory enough answer. Please help me!&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/76159913</id>
        <re:rank scheme="https://stackoverflow.com">-1</re:rank>
        <title type="text">Maximizing nth largest element of grid</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
        <author>
            <name>Noxville</name>
            <uri>https://stackoverflow.com/users/889998</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/76159913/maximizing-nth-largest-element-of-grid" />
        <published>2023-05-03T01:39:07Z</published>
        <updated>2025-06-11T00:30:51Z</updated>
        <summary type="html">
            &lt;p&gt;I&#x27;ve got a numpy array (48 x 3) of IntVectors (&lt;code&gt;X&lt;/code&gt;) where various subsections of this array have constraints. I also have a (48 x 1) of pre-defined integer constants (&lt;code&gt;initial_scores&lt;/code&gt;).&lt;/p&gt;&#xA;&lt;p&gt;Given these constraints I&#x27;d like to maximize for the 13th largest possible row-sum; that is if we took &lt;code&gt;[initial_scores[k] &#x2B; X[k, 0] &#x2B; X[k, 1] &#x2B; X[k, 2] for k in range(48)]&lt;/code&gt;, sorted it (descending) and returned the 13th element.&lt;/p&gt;&#xA;&lt;p&gt;In some literature I came across the &lt;code&gt;nth&lt;/code&gt; and a &lt;code&gt;mk_seq_nth&lt;/code&gt; function but neither are defined functions so I&#x27;m likely not using them correctly, or perhaps they&#x27;re not available in z3py?&lt;/p&gt;&#xA;&lt;p&gt;Here&#x27;s a &lt;a href=&quot;https://gist.github.com/Noxville/a55c76c7631a3f31344802aec7143cd3&quot; rel=&quot;nofollow noreferrer&quot;&gt;gist of the code&lt;/a&gt; (69 LOC).&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79660625</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Record usage in Z3 especially with Arrays (Sets) and uninterpreted field constants</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>Jason Kleban</name>
            <uri>https://stackoverflow.com/users/162273</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79660625/record-usage-in-z3-especially-with-arrays-sets-and-uninterpreted-field-constan" />
        <published>2025-06-10T14:20:21Z</published>
        <updated>2025-06-10T20:27:03Z</updated>
        <summary type="html">
            &lt;p&gt;I have a few noob questions regarding usage and style in z3 (smt syntax directly).  I don&#x27;t have a theoretical background on solvers, but I would still like to be able to take advantage of the neatness of Z3 without needing to deeply understand the theory (that&#x27;s fair, no?).&lt;/p&gt;&#xA;&lt;p&gt;Sample problem: I want to assign Students to distinct Seats.  The Seats have letter names because I think that&#x27;s simpler to write in SMT than if they were numbered.  The Student type only has a &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;seat&lt;/code&gt; field so far, but please consider that the Students might get several more fields and additional seating constraints in the future.&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;(declare-datatypes () ((Seat A B C D E F G)))&#xA;&#xA;(declare-datatypes () ((Student (mkstudent (name String) (seat Seat)))))&#xA;&#xA;(declare-const S001 Student)&#xA;(declare-const S002 Student)&#xA;&#xA;(assert (= (name S001) &amp;quot;Alice&amp;quot;))&#xA;(assert (= (name S002) &amp;quot;Bob&amp;quot;))&#xA;&#xA;(assert (forall ((l Student) (r Student)) (xor (= l r) (distinct (seat l) (seat r)))))&#xA;; (assert (forall ((l Student) (r Student)) (implies (distinct l r) (distinct (seat l) (seat r))))) ; doesn&#x27;t work either&#xA;(check-sat)&#xA;;(get-model)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This is &lt;code&gt;Unsat&lt;/code&gt; currently.  Why?  I think I&#x27;m saying that &amp;quot;For all pairs of known Students that exist, either the student is paired with itself XOR the compared students are in distinct seats.&amp;quot;  But I might be saying &amp;quot;For all pairs of imaginable Students ..&amp;quot;&lt;/p&gt;&#xA;&lt;p&gt;If its the later, do I need to express the set of students as an array and forall over their indexes?  Or is there a more natural way to say it?&lt;/p&gt;&#xA;&lt;p&gt;It feels awkward to have to declare &lt;code&gt;S001&lt;/code&gt;, &lt;code&gt;S002&lt;/code&gt; and then assert each field independently.  If using an array is correct anyway, I&#x27;d rather say something like:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;(declare-datatypes () ((Seat A B C D E F G)))&#xA;&#xA;(declare-datatypes () ((Student (mkstudent (name String) (seat Seat)))))&#xA;&#xA;(declare-const students [&#xA;    (mkstudent &amp;quot;Alice&amp;quot; _)&#xA;    (mkstudent &amp;quot;Bob&amp;quot; _)&#xA;])&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;where there is some placeholder (an uninterpreted constraint?) for the seat field.  But I cannot figure out how to leave that free &lt;em&gt;anonymously&lt;/em&gt;.  And I don&#x27;t know if there&#x27;s such a syntax for arrays.&lt;/p&gt;&#xA;&lt;p&gt;The only way I can get close to this is by pre-declaring &lt;em&gt;a named Seat for each Student&lt;/em&gt; and using that in the constructor, but this feels really awful ergonomically - and therefore feels like I&#x27;m just doing it wrong.  Like, what would be the point of a Record if we have to declare unique identifiers for the instance fields anyway?? Especially presuming that I wouldn&#x27;t ever have to mention those identifiers again since there are other Record-accessors to get at them.  Sure, I could &lt;code&gt;assert (= (name (select students 0)) &amp;quot;Alice&amp;quot;)&lt;/code&gt; leaving the &lt;code&gt;seat&lt;/code&gt; field unconstrained, but as my Student type grows with new valued-fields, this would get ugly and then it makes constructors not so useful.&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;(declare-const SeatAlice Seat)&#xA;(declare-const SeatBob Seat)&#xA;&#xA;(declare-const students (Array Int Student))&#xA;(assert (= (select students 0) (mkstudent &amp;quot;Alice&amp;quot; SeatAlice)))&#xA;(assert (= (select students 1) (mkstudent &amp;quot;Bob&amp;quot; SeatBob)))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I&#x27;m not finding any documentation on how to use arrays and records and constructors and uninterpreted constraints all together.  Just minimal examples of them independently, at best.&lt;/p&gt;&#xA;&lt;p&gt;Many of my questions here are syntax-related - and maybe SMT/Z3 language just isn&#x27;t prioritizing ergonomics and that is important to know - but again I&#x27;m checking with you here because maybe the awkwardness is a signal that I&#x27;m just doing it wrong and that there&#x27;s a much more elegant way that is more readable and possibly even better for Z3 itself.&lt;/p&gt;&#xA;&lt;h1&gt;Updates&lt;/h1&gt;&#xA;&lt;p&gt;This feels a little better using sequence instead of array.&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;(declare-datatypes () ((Seat A B C D E F G)))&#xA;(declare-datatypes () ((Student (mk-student-ctor (fname String) (lname String) (seat Seat)))))&#xA;&#xA;(define-fun mk-student ((fname String) (lname String)) Student&#xA;    ((declare-const s Seat)&#xA;    (mk-student-ctor fname lname s))&#xA;)&#xA;&#xA;;; (define-fun mk-student ((fname String) (lname String)) Student&#xA;;;     ; doesn&#x27;t work, but how could (define-const s Seat) be used in an expression as a local?&#xA;;;     (let ((s Seat))&#xA;;;     (mk-student-ctor fname lname s))&#xA;;; )&#xA;&#xA;(declare-const students (Seq Student))&#xA;&#xA;(assert (= students (seq.&#x2B;&#x2B; &#xA;    (seq.unit (mk-student &amp;quot;Alice&amp;quot; &amp;quot;Smith&amp;quot;)) &#xA;    (seq.unit (mk-student &amp;quot;Bob&amp;quot; &amp;quot;Baker&amp;quot;))&#xA;    ; ...&#xA;)))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;But I still can&#x27;t figure out how to encapsulate the concept of an uninterpreted &lt;code&gt;seat&lt;/code&gt; field in a constructed &lt;code&gt;Student&lt;/code&gt;.  Because &lt;code&gt;declare-const&lt;/code&gt; is a command, I don&#x27;t know how it could be used in an expression or if there is an alternative syntax for declaring a const locally (or inline).  And in a define-fun, I don&#x27;t see any way to &lt;em&gt;make&lt;/em&gt; a Student without the constructor function which requires all arguments and thus I cannot even resign to constraining the fields one-at-a-time in my new &lt;code&gt;mk_student()&lt;/code&gt; factory.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/78146123</id>
        <re:rank scheme="https://stackoverflow.com">-1</re:rank>
        <title type="text">Z3-Solver (z3.z3types.Z3Exception: Z3 invalid substitution, expression pairs expected.)</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
        <author>
            <name>flessent</name>
            <uri>https://stackoverflow.com/users/10403050</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/78146123/z3-solver-z3-z3types-z3exception-z3-invalid-substitution-expression-pairs-exp" />
        <published>2024-03-12T09:54:59Z</published>
        <updated>2025-06-10T06:58:42Z</updated>
        <summary type="html">
            &lt;p&gt;I have a relative complex Z3 boolean formula and when trying to set the values of a particular variable , I get the following error :&lt;br /&gt;&#xA;raise Z3Exception(msg)&#xA;z3.z3types.Z3Exception: Z3 invalid substitution, expression pairs expected.&lt;/p&gt;&#xA;&lt;p&gt;I tried the following :&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;&#xA;def create_z3_formula_bdd(dimacs_file):&#xA;    variable_names = {&#xA;        1: &#x27;inflow1&#x27;, 2: &amp;quot;inflow1&#x27;&amp;quot;,&#xA;        3: &#x27;inflow2&#x27;, 4: &amp;quot;inflow2&#x27;&amp;quot;,&#xA;        5: &#x27;level@0.3.107&#x27;, 6: &amp;quot;level@0.3.107&#x27;&amp;quot;,&#xA;        7: &#x27;level@1&#x27;, 8: &amp;quot;level@1&#x27;&amp;quot;,&#xA;        9: &#x27;level@2&#x27;, 10: &amp;quot;level@2&#x27;&amp;quot;,&#xA;        11: &#x27;level@3&#x27;, 12: &amp;quot;level@3&#x27;&amp;quot;,&#xA;        13: &#x27;level@4&#x27;, 14: &amp;quot;level@4&#x27;&amp;quot;,&#xA;        15: &#x27;level@5&#x27;, 16: &amp;quot;level@5&#x27;&amp;quot;,&#xA;        17: &#x27;level@6&#x27;, 18: &amp;quot;level@6&#x27;&amp;quot;,&#xA;        19: &#x27;outflow&#x27;, 20: &amp;quot;outflow&#x27;&amp;quot;,&#xA;        21: &#x27;_jx_b0&#x27;&#xA;    }&#xA;    with open(dimacs_file, &#x27;r&#x27;) as f:&#xA;        dimacs_lines = f.readlines()&#xA;&#xA;    # Parse variables and clauses&#xA;    variables = set()&#xA;    clauses = []&#xA;    for line in dimacs_lines:&#xA;        tokens = line.split()&#xA;        if tokens[0] == &#x27;p&#x27;:&#xA;            num_vars = int(tokens[2])&#xA;        elif tokens[0] != &#x27;c&#x27; and tokens[0] != &#x27;0&#x27;:&#xA;            clause = [int(var) for var in tokens[:-1]]&#xA;            variables.update(map(abs, clause))&#xA;            clauses.append(clause)&#xA;&#xA;    z3_variables = {var: Bool(variable_names[abs(var)]) for var in variables}&#xA;&#xA;    # Create Z3 clauses&#xA;    z3_clauses = [Or([z3_variables[abs(literal)] if literal &amp;gt; 0 else Not(z3_variables[abs(literal)]) for literal in clause]) for clause in clauses]&#xA;&#xA;    return And(z3_clauses)&#xA;&#xA; formula_bdd = create_z3_formula_bdd(&#x27;neg_water_reservoir.cnf&#x27;)&#xA; substitution_map = {variable_names[1]: False}&#xA; formula_with_substitution = substitute(formula_bdd, substitution_map)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/66781172</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Modular arithmetic using z3</title>
            <category scheme="https://stackoverflow.com/tags" term="integer" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="modulo" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>bobismijnnaam</name>
            <uri>https://stackoverflow.com/users/2078414</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/66781172/modular-arithmetic-using-z3" />
        <published>2021-03-24T12:35:13Z</published>
        <updated>2025-05-31T17:06:35Z</updated>
        <summary type="html">
            &lt;p&gt;I&#x27;m trying to prove statements such as the following:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(assert (=&#xA;    (mod i n)&#xA;    (mod j n)))&#xA;&#xA;(assert (&amp;gt; n 0))&#xA;    &#xA;(assert (not (=&#xA;    (mod (&#x2B; i 1) n)&#xA;    (mod (&#x2B; j 1) n))))&#xA;&#xA;(check-sat)&#xA;(get-model)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Others are:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;(((i % n) &#x2B; j) % n) == ((i &#x2B; j) % n)&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;((i - n &#x2B; 1) % n) == ((i &#x2B; 1) % n)&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;((((a - 1) % n) &#x2B; 1) % n) == (a % n)&lt;/code&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;But z3 does not seem to terminate when proving these statements. Are they beyond the power of z3/smt solvers?&lt;/p&gt;&#xA;&lt;p&gt;If it is the only way I would not mind doing more explicit proof steps, but these rules seem so elementary I would not know where to begin. Even when using induction I quickly run into cases (like the initial example) that I would expect to be true, but which do not seem to be provable with z3.&lt;/p&gt;&#xA;&lt;p&gt;I am using z3 4.8.6, for what it&#x27;s worth. Can anyone shed some light on this why this is hard, or maybe point me in the direction of papers/z3 flags that make this feasible?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79644246</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Which version of Z3 does VeriEQL use?</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
        <author>
            <name>Stanislav Kikot</name>
            <uri>https://stackoverflow.com/users/4627124</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79644246/which-version-of-z3-does-verieql-use" />
        <published>2025-05-29T16:08:19Z</published>
        <updated>2025-05-30T08:37:40Z</updated>
        <summary type="html">
            &lt;p&gt;I am trying to set up VeriEQL on my computer (Ubunty, Python 3.10) from this repository: &lt;a href=&quot;https://github.com/VeriEQL/VeriEQL&quot; rel=&quot;nofollow noreferrer&quot;&gt;https://github.com/VeriEQL/VeriEQL&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;However, when I run python &lt;code&gt;__main__.py&lt;/code&gt; I get error message:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt; File &amp;quot;/mnt/compression/VeriEQL/constants.py&amp;quot;, line 33, in &amp;lt;lambda&amp;gt;&#xA;    And = lambda *args: Z3_And(*args, ctx=Z3_CONTEXT)&#xA;TypeError: And() got an unexpected keyword argument &#x27;ctx&#x27;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I tried installing the following versions of z3 via pip install:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;z3-solver-4.5.1.0&#xA;z3-solver-4.11.2.0&#xA;z3-solver-4.8.12.0&#xA;z3-solver-4.15.0.0&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;but the error persists.&lt;/p&gt;&#xA;&lt;p&gt;And I do not know to to print z3 version from &lt;code&gt;__main__.py&lt;/code&gt; to make sure that the installed version is indeed loaded.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/14080398</id>
        <re:rank scheme="https://stackoverflow.com">9</re:rank>
        <title type="text">Z3py: how to get the list of variables from a formula?</title>
            <category scheme="https://stackoverflow.com/tags" term="python" />
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="z3py" />
        <author>
            <name>user311703</name>
            <uri>https://stackoverflow.com/users/311703</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/14080398/z3py-how-to-get-the-list-of-variables-from-a-formula" />
        <published>2012-12-29T09:34:17Z</published>
        <updated>2025-05-13T15:16:43Z</updated>
        <summary type="html">
            &lt;p&gt;In Z3Py, I have a formula. How can I retrieve the list of variables using in the formula?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/23451388</id>
        <re:rank scheme="https://stackoverflow.com">4</re:rank>
        <title type="text">Z3 Sudoku Solver</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="sudoku" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
        <author>
            <name>user3600355</name>
            <uri>https://stackoverflow.com/users/3600355</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/23451388/z3-sudoku-solver" />
        <published>2014-05-04T00:47:15Z</published>
        <updated>2025-04-27T08:06:15Z</updated>
        <summary type="html">
            &lt;p&gt;I was on the site rise4fun a few weeks ago and they had a python code that converted a sudoku puzzle input file to z3. I checked again today and the file is gone, and was wondering if anyone had this code or could explain to me how to implement it. Thanks!!&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79555641</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">`div` versus `/` in SMTLIB</title>
            <category scheme="https://stackoverflow.com/tags" term="z3" />
            <category scheme="https://stackoverflow.com/tags" term="smt" />
            <category scheme="https://stackoverflow.com/tags" term="cvc4" />
            <category scheme="https://stackoverflow.com/tags" term="cvc5" />
        <author>
            <name>Ranjit Jhala</name>
            <uri>https://stackoverflow.com/users/729775</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79555641/div-versus-in-smtlib" />
        <published>2025-04-04T14:52:33Z</published>
        <updated>2025-04-09T16:57:05Z</updated>
        <summary type="html">
            &lt;p&gt;can someone explain the difference between &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;div&lt;/code&gt; in SMTLIB? My impression is that the former was for &lt;code&gt;Real&lt;/code&gt; division and the latter for &lt;code&gt;Int&lt;/code&gt; division. Concretely I find that if I write&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(define-fun SMTLIB_OP_DIV ((x Int) (y Int)) Int (/ x y))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Then both &lt;code&gt;z3&lt;/code&gt; and &lt;code&gt;cvc5&lt;/code&gt; will complain about the above.&lt;/p&gt;&#xA;&lt;p&gt;&lt;em&gt;However&lt;/em&gt; I also find that if I have the following query&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(declare-const a Int)&#xA;(declare-const b Int)&#xA;&#xA;(assert (&amp;gt;= b 32))&#xA;(assert (= a (/ b 8)))  &#xA;(assert (not (= (/ b a) 8))) &#xA;(check-sat)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Then both &lt;code&gt;cvc5&lt;/code&gt; and &lt;code&gt;z3&lt;/code&gt; will say &lt;code&gt;unsat&lt;/code&gt; &lt;em&gt;but&lt;/em&gt; if I replace the &lt;code&gt;/&lt;/code&gt; with &lt;code&gt;div&lt;/code&gt;&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;(declare-const a Int)&#xA;(declare-const b Int)&#xA;&#xA;(assert (&amp;gt;= b 32))&#xA;(assert (= a (div b 8)))  &#xA;(assert (not (= (div b a) 8))) &#xA;(check-sat)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Then they both say &lt;code&gt;sat&lt;/code&gt; (!)&lt;/p&gt;&#xA;&lt;p&gt;Can someone explain? My hunch is there is some internal coercion going on perhaps? But then why wouldn&#x27;t I just always use &lt;code&gt;/&lt;/code&gt; and why is the coercion disallowed in the &lt;code&gt;define-fun&lt;/code&gt;?&lt;/p&gt;&#xA;
        </summary>
    </entry>
</feed>