Closed gergoerdi closed 4 years ago
Managed to make it just a tiny bit smaller at 62 lines now:
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
module Main (main) where
import Control.Monad.State
import Data.Int
import Data.SBV
import Data.SBV.Control
data Game = Game
{ gameItems :: [Int16]
, gameRooms :: [[Int16]]
}
deriving (Show)
main :: IO ()
main = do
let theGame = Game
{ gameItems = [1]
, gameRooms = [[0],[1]]
}
runSMTWith z3{ verbose = True} $ query $ do
let round s = do
word <- freshVar_
return $ runState (stepPlayer theGame word) s
s <- return [0]
(finished, s) <- round s
(finished, s) <- round s
constrain finished
checkSat
return ()
instance (Mergeable s, Mergeable a) => Mergeable (State s a) where
symbolicMerge force cond thn els = state $ symbolicMerge force cond (runState thn) (runState els)
type Engine = State [SInt16]
stepPlayer :: Game -> SInt16 -> Engine SBool
stepPlayer Game{..} word = do
ite (word .== 1) builtin_go builtin_get
locs <- get
return $ map (.== 1) locs `pbExactly` 1
where
builtin_go = do
~[here] <- get
let rooms@(room:_) = map (map literal) gameRooms
let exits = select rooms room here
let newRoom = select exits 0 word
ite (newRoom .== 0) (return ()) $ put [1]
builtin_get = do
locs <- get
let item = literal . head $ gameItems
ite (select locs (-1) item ./= 0) (return ()) $ put [255]
The SMTLib output is also considerably smaller:
** Calling: z3 -nw -in -smt2
[GOOD] ; Automatically generated by SBV. Do not edit.
[GOOD] (set-option :print-success true)
[GOOD] (set-option :global-declarations true)
[GOOD] (set-option :smtlib2_compliant true)
[GOOD] (set-option :diagnostic-output-channel "stdout")
[GOOD] (set-option :produce-models true)
[GOOD] (set-logic ALL) ; external query, using all logics.
[GOOD] ; --- uninterpreted sorts ---
[GOOD] ; --- tuples ---
[GOOD] ; --- sums ---
[GOOD] ; --- literal constants ---
[GOOD] ; --- skolem constants ---
[GOOD] ; --- constant tables ---
[GOOD] ; --- skolemized tables ---
[GOOD] ; --- arrays ---
[GOOD] ; --- uninterpreted constants ---
[GOOD] ; --- user given axioms ---
[GOOD] ; --- formula ---
[GOOD] (declare-fun s0 () (_ BitVec 16))
[GOOD] (declare-fun s1 () (_ BitVec 16))
[GOOD] (define-fun s2 () (_ BitVec 16) #x0001)
[GOOD] (define-fun s5 () (_ BitVec 16) #x0000)
[GOOD] (declare-fun table0 ((_ BitVec 16)) (_ BitVec 16))
[FAIL] (define-fun table0_initializer_0 () Bool (= (table0 #x0000) s8))
[SYNC] Attempting to synchronize with tag: "terminating upon unexpected response (at: 2020-07-06 11:52:17.00269423 +08)"
[FIRE] (echo "terminating upon unexpected response (at: 2020-07-06 11:52:17.00269423 +08)")
[SYNC] Synchronization achieved using tag: "terminating upon unexpected response (at: 2020-07-06 11:52:17.00269423 +08)"
*** Exception:
*** Data.SBV: Unexpected response from the solver, context: define-fun:
***
*** Sent : (define-fun table0_initializer_0 () Bool (= (table0 #x0000) s8))
*** Expected : success
*** Received : (error "line 12 column 60: unknown constant s8")
***
*** Executable: z3
*** Options : -nw -in -smt2
Thanks for the report. This seems to be a bug in SBV; some unexpected interaction. I'll investigate.
@gergoerdi
I pushed in a fix that should address this issue. But it's rather finicky, so please do some testing and let me know if it solves the problem.
Note that I ran into a z3 bug while working on this, and reported here: https://github.com/Z3Prover/z3/issues/4565
If you run into that, I recommend downloading a version of z3 that's pre-Jun-24th of this year; this bug seems to have crept after that date into z3.
@gergoerdi
Looking at the code again, this isn't quite fixed yet. Please delay testing. It'll need some more work.
@gergoerdi
I pushed in changes that should address this issue. (And the z3 bug is fixed as well.)
Can you give it a try and let me know if it all works fine?
Holy moly, the code assembles SMTLib output as a string?!
I was hoping it has an AST representation which gets coparsed into a string at the edges only...
That rendering is precisely what I'd call the "edges." Is this causing you problems?
No, it's just surprising.
I can confirm that both my cut-down test case and my original program works with the fix in 036ce4e.
Thanks for the report.
Sure, please file a new issue. There could always be bugs/performance issues.
I am getting the following output from SBV:
Looking at the SMTLib output with
verbose = True
, I see that indeeds57
is not part of the output:Unfortunately, the only way I have of reproducing it at the moment is a 150-line module; bear in mind that this is already the result of a significant reduction effort, as the original code was much more complicated. Here is the minimised version with no external dependencies other than MTL/Transformers:
Tested with SBV 8.8 running on GHC 8.8.3.