<p>The fundamental issue here is that you are mixing SMTLib's sequence logic and quantifiers. And the problem turns out to be too difficult for an SMT solver to handle. This sort of synthesis of functions is indeed possible if you restrict yourself to basic logics. (Bitvectors, Integers, Reals.) But adding sequences to the mix puts it into the undecidable fragment.</p>

<p>This doesn't mean z3 cannot synthesize your <code>add</code> function. Perhaps a future version might be able to handle it. But at this point you're at the mercy of heuristics. To see why, note that you're asking the solver to synthesize the following definition:</p>

<pre class="lang-hs prettyprint-override"><code>        add :: Stack -&gt; Stack
        add s = v .: s''
          where (a, s')  = L.uncons s
                (b, s'') = L.uncons s'
                v        = a + b
</code></pre>

<p>while this looks rather innocent and simple, it requires capabilities beyond the current abilities of z3. In general, z3 can currently synthesize functions that only make a finite number of choices on concrete elements. But it is unable to do so if the output depends on input for every choice of input. (Think of it as a case-analysis producing engine: It can conjure up a function that maps certain inputs to others, but cannot figure out if something should be incremented or two things must be added. This follows from the work in finite-model finding theory, and is way beyond the scope of this answer! See here for details: <a href="https://arxiv.org/abs/1706.00096" rel="nofollow noreferrer">https://arxiv.org/abs/1706.00096</a>)</p>

<p>A better use case for SBV and SMT solving for this sort of problem is to actually tell it what the <code>add</code> function is, and then prove some given program is correctly "compiled" using Hutton's strategy. Note that I'm explicitly saying a "given" program: It would also be very difficult to model and prove this for an arbitrary program, but you can do this rather easily for a given fixed program. If you are interested in proving the correspondence for arbitrary programs, you really should be looking at theorem provers such as Isabelle, Coq, ACL2, etc.; which can deal with induction, a proof technique you will no doubt need for this sort of problem. Note that SMT solvers cannot perform induction in general. (You can use e-matching to simulate some induction like proofs, but it's a kludge at best and in general unmaintainable.)</p>

<p>Here's your example, coded to prove the <code>\x -&gt; \y -&gt; x + y</code> program is "correctly" compiled and executed with respect to reference semantics:</p>

<pre class="lang-hs prettyprint-override"><code>{-# LANGUAGE ScopedTypeVariables #-}

import Data.SBV
import qualified Data.SBV.List as L
import           Data.SBV.List ((.:))

-- AST Definition
data Exp = Val SWord8
         | Sum Exp Exp

-- Our "Meaning" Function
eval :: Exp -&gt; SWord8
eval (Val x)   = x
eval (Sum x y) = eval x + eval y

-- Evaluation by "execution"
type Stack  = SList Word8

run :: Exp -&gt; SWord8
run e = L.head (eval' e L.nil)
  where eval' :: Exp -&gt; Stack -&gt; Stack
        eval' (Val n)   s = n .: s
        eval' (Sum x y) s = add (eval' y (eval' x s))

        add :: Stack -&gt; Stack
        add s = v .: s''
          where (a, s')  = L.uncons s
                (b, s'') = L.uncons s'
                v        = a + b

correct :: IO ThmResult
correct = prove $ do x :: SWord8 &lt;- forall "x"
                     y :: SWord8 &lt;- forall "y"


                     let pgm = Sum (Val x) (Val y)

                         spec    = eval pgm
                         machine = run  pgm

                     return $ spec .== machine
</code></pre>

<p>When I run this, I get:</p>

<pre><code>*Main&gt; correct
Q.E.D.
</code></pre>

<p>And the proof takes almost no time. You can easily extend this by adding other operators, binding forms, function calls, the whole works if you like. So long as you stick to a fixed "program" for verification, it should work out just fine.</p>

<p>If you make a <em>mistake</em>, let's say define <code>add</code> by subtraction (modify the last line of it to ready <code>v = a - b</code>), you get:</p>

<pre><code>*Main&gt; correct
Falsifiable. Counter-example:
  x = 32 :: Word8
  y =  0 :: Word8
</code></pre>

<p>I hope this gives an idea of what the current capabilities of SMT solvers are and how you can put them to use in Haskell via SBV. </p>

<p>Program synthesis is an active research area with many custom techniques and tools. An out of the box use of an SMT-solver will not get you there. But if you do build such a custom system in Haskell, you can use SBV to access an underlying SMT solver to solve many constraints you'll have to handle during the process.</p>

<p>(<em>Aside:</em> An extended example, similar in spirit but with different goals, is shipped with the SBV package: <a href="https://hackage.haskell.org/package/sbv-8.5/docs/Documentation-SBV-Examples-Strings-SQLInjection.html" rel="nofollow noreferrer">https://hackage.haskell.org/package/sbv-8.5/docs/Documentation-SBV-Examples-Strings-SQLInjection.html</a>. This program shows how to use SBV and SMT solvers to find SQL injection vulnerabilities in an idealized SQL implementation. That might be of some interest here, and would be more aligned with how SMT solvers are typically used in practice.)</p>
How to use Data.SBV to help derive correct stack machine implementation?
<p>Graham Hutton, in the 2nd edition of <em>Programming in Haskell</em>, spends the last 2 chapters on the topic of <em>stack machine</em> based implementation of an AST.
And he finishes by showing how to <em>derive</em> the correct implementation of that machine from the <em>semantic model</em> of the AST.</p>

<p><strong>I'm trying to enlist the help of <code>Data.SBV</code> in that derivation, and failing.</strong></p>

<p>And I'm hoping that someone can help me understand whether I'm:</p>

<ol>
<li>Asking for something that <code>Data.SBV</code> can't do, or</li>
<li>Asking <code>Data.SBV</code> for something it <em>can</em> do, but asking incorrectly.</li>
</ol>

<pre><code>-- test/sbv-stack.lhs - Data.SBV assisted stack machine implementation derivation.

{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE ScopedTypeVariables #-}

import Data.SBV
import qualified Data.SBV.List as L
import           Data.SBV.List ((.:), (.++))  -- Since they don't collide w/ any existing list functions.

-- AST Definition
data Exp = Val SWord8
         | Sum Exp Exp

-- Our "Meaning" Function
eval :: Exp -&gt; SWord8
eval (Val x)   = x
eval (Sum x y) = eval x + eval y

type Stack  = SList Word8

-- Our "Operational" Definition.
--
-- This function attempts to implement the *specification* provided by our
-- "meaning" function, above, in a way that is more conducive to
-- implementation in our available (and, perhaps, quite primitive)
-- computational machinery.
--
-- Note that we've (temporarily) assumed that this machinery will consist
-- of some form of *stack-based computation engine* (because we're
-- following Hutton's example).
--
-- Note that we give the *specification* of the function in the first
-- (commented out) line of the definition. The derivation of the actual
-- correct definition from this specification is detailed in Ch. 17 of
-- Hutton's book.
eval' :: Exp -&gt; Stack -&gt; Stack
-- eval' e s = eval e : s         -- our "specification"
eval' (Val n) s = push n s        -- We're defining this one manually.
 where
  push :: SWord8 -&gt; Stack -&gt; Stack
  push n s = n .: s
eval' (Sum x y) s = add (eval' y (eval' x s))
 where
  add :: Stack -&gt; Stack
  add = uninterpret "add" s       -- This is the function we're asking to be derived.

-- Now, let's just ask SBV to "solve" our specification of `eval'`:
spec :: Goal
spec = do x :: SWord8 &lt;- forall "x"
          y :: SWord8 &lt;- forall "y"
          -- Our spec., from above, specialized to the `Sum` case:
          constrain $ eval' (Sum (Val x) (Val y)) L.nil .== eval (Sum (Val x) (Val y)) .: L.nil
</code></pre>

<p>We get:</p>

<pre><code>λ&gt; :l test/sbv-stack.lhs
[1 of 1] Compiling Main             ( test/sbv-stack.lhs, interpreted )
Ok, one module loaded.
Collecting type info for 1 module(s) ... 
λ&gt; sat spec
Unknown.
  Reason: smt tactic failed to show goal to be sat/unsat (incomplete quantifiers)
</code></pre>

<p>What happened?!<br>
Well, maybe, asking SBV to solve for anything other than a <em>predicate</em> (i.e. - <code>a -&gt; Bool</code>) doesn't work?</p>