I'm experimenting with the following way of encoding extended naturals in SMT-LIB (I define a datatype analogous to Maybe Integer
):
; extended integers -- if first field is true, then the value is infinity
(declare-datatypes () ((IntX (mk-int-x (is-infty Bool) (not-infty Int)))))
; addition
(define-fun plus ((x IntX) (y IntX)) IntX
(ite (or (is-infty x) (is-infty y))
(mk-int-x true 0)
(mk-int-x false (+ (not-infty x) (not-infty y)))))
(declare-fun x () IntX)
(assert (= x (plus x (mk-int-x false 1))))
; x = x+1 when x |-> infty
(get-model)
(exit)
How would I go about to encode this in SBV? I tried the following, but that just crashed SBV. Also I somehow doubt that this would do what I want, but I'm not familiar enough with how SBV works.
!/usr/bin/env stack
{- stack script
--resolver nightly-2018-11-23
--package sbv
--package syb
-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Generics
import Data.SBV
data IntX = IntX (Maybe Integer) deriving (Eq, Ord, Data, Read, Show, SymWord, HasKind)
pretty :: IntX -> String
pretty = \case
IntX Nothing -> "∞"
IntX n -> show n
instance Num IntX where
(+) (IntX x) (IntX y) = IntX $ (+) <$> x <*> y
(*) (IntX x) (IntX y) = IntX $ (*) <$> x <*> y
fromInteger = IntX . Just
ex1 = sat $ do
x :: SBV IntX <- free "x"
return $ x .== x + 1
main :: IO ()
main = print =<< ex1
~/temp ✘ ./sbv.hs
sbv.hs: SBV.SMT.SMTLib2.cvtExp.sh: impossible happened; can't translate: s0 + s1
CallStack (from HasCallStack):
error, called at ./Data/SBV/SMT/SMTLib2.hs:681:13 in sbv-7.12-9AiNAYtrUhB8YA6mr6BTn4:Data.SBV.SMT.SMTLib2
The fundamental issue here is that your code is mixing Haskell's concrete
Maybe
type and trying to treat it as a symbolic object. But you're on the right track with how you implemented that in SMT-Lib2: You essentially need to write the corresponding code in SBV.I'd start with:
This is just boilerplate; and you don't need the
Data.SBV.Control
import unless you want to use the query mode, but it does come in handy as we shall see.The first thing to do is to encode your
IntX
type symbolically; just like you did in SMTLib:Nothing above should be surprising, except perhaps the deriving of
Generic
andMergeable
. It simply enables SBV to be able to useite
on your extended naturals. Also note how theShow
instance is careful in distinguishing concrete and symbolic values by usingunliteral
.Next, we add a few convenience functions, again nothing surprising:
Now we can make
IntX
a number:(Note that the semantics of this means
oo - oo = oo
, which is at best questionable. But that's besides the point. You might have to explicitly define-
and deal with that as you wish. Similar comments apply forsignum
.)Since you want to test for equality, we also have to define the symbolic version of that:
Similarly, if you want to compare, you'll have to define an
OrdSymbolic
instance; but the idea remains the same.We need a way to create symbolic extended naturals. The following function does it nicely:
Strictly speaking, you don't need to name the variables. (i.e., the
nm
parameter isn't needed.) But I find it helpful to always name my variables for obvious reasons.Now, we can code your example:
When I run this, I get:
Which is what you were looking for, I believe.
When you're dealing with larger programs, it's beneficial to be able to extract
IntX
values more directly and program further with them. This is when the query mode comes in handy. First, a helper:Now we can code:
And we get:
I hope this helps. I've put all this code in a gist: https://gist.github.com/LeventErkok/facfd067b813028390c89803b3a0e887