{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wall -Werror -Wno-incomplete-uni-patterns #-}
module Documentation.SBV.Examples.Puzzles.U2Bridge where
import Control.Monad (unless)
import Control.Monad.State (State, runState, put, get, gets, modify, evalState)
import Data.List(sortOn)
import GHC.Generics (Generic)
import Data.SBV
data U2Member = Bono | Edge | Adam | Larry
mkSymbolicEnumeration ''U2Member
type Time = Word32
type STime = SBV Time
crossTime :: U2Member -> Time
crossTime :: U2Member -> Time
crossTime U2Member
Bono = Time
1
crossTime U2Member
Edge = Time
2
crossTime U2Member
Adam = Time
5
crossTime U2Member
Larry = Time
10
sCrossTime :: SU2Member -> STime
sCrossTime :: SU2Member -> STime
sCrossTime SU2Member
m = forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
m forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sBono) (forall a. SymVal a => a -> SBV a
literal (U2Member -> Time
crossTime U2Member
Bono))
forall a b. (a -> b) -> a -> b
$ forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
m forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sEdge) (forall a. SymVal a => a -> SBV a
literal (U2Member -> Time
crossTime U2Member
Edge))
forall a b. (a -> b) -> a -> b
$ forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
m forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sAdam) (forall a. SymVal a => a -> SBV a
literal (U2Member -> Time
crossTime U2Member
Adam))
(forall a. SymVal a => a -> SBV a
literal (U2Member -> Time
crossTime U2Member
Larry))
data Location = Here | There
mkSymbolicEnumeration ''Location
data Status = Status { Status -> STime
time :: STime
, Status -> SLocation
flash :: SLocation
, Status -> SLocation
lBono :: SLocation
, Status -> SLocation
lEdge :: SLocation
, Status -> SLocation
lAdam :: SLocation
, Status -> SLocation
lLarry :: SLocation
} deriving (forall x. Rep Status x -> Status
forall x. Status -> Rep Status x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Status x -> Status
$cfrom :: forall x. Status -> Rep Status x
Generic, Bool -> SBool -> Status -> Status -> Status
forall b.
(Ord b, SymVal b, Num b) =>
[Status] -> Status -> SBV b -> Status
forall a.
(Bool -> SBool -> a -> a -> a)
-> (forall b. (Ord b, SymVal b, Num b) => [a] -> a -> SBV b -> a)
-> Mergeable a
select :: forall b.
(Ord b, SymVal b, Num b) =>
[Status] -> Status -> SBV b -> Status
$cselect :: forall b.
(Ord b, SymVal b, Num b) =>
[Status] -> Status -> SBV b -> Status
symbolicMerge :: Bool -> SBool -> Status -> Status -> Status
$csymbolicMerge :: Bool -> SBool -> Status -> Status -> Status
Mergeable)
start :: Status
start :: Status
start = Status { time :: STime
time = STime
0
, flash :: SLocation
flash = SLocation
sHere
, lBono :: SLocation
lBono = SLocation
sHere
, lEdge :: SLocation
lEdge = SLocation
sHere
, lAdam :: SLocation
lAdam = SLocation
sHere
, lLarry :: SLocation
lLarry = SLocation
sHere
}
type Move a = State Status a
instance Mergeable a => Mergeable (Move a) where
symbolicMerge :: Bool -> SBool -> Move a -> Move a -> Move a
symbolicMerge Bool
f SBool
t Move a
a Move a
b
= do Status
s <- forall s (m :: * -> *). MonadState s m => m s
get
let (a
ar, Status
s1) = forall s a. State s a -> s -> (a, s)
runState Move a
a Status
s
(a
br, Status
s2) = forall s a. State s a -> s -> (a, s)
runState Move a
b Status
s
forall s (m :: * -> *). MonadState s m => s -> m ()
put forall a b. (a -> b) -> a -> b
$ forall a. Mergeable a => Bool -> SBool -> a -> a -> a
symbolicMerge Bool
f SBool
t Status
s1 Status
s2
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Mergeable a => Bool -> SBool -> a -> a -> a
symbolicMerge Bool
f SBool
t a
ar a
br
peek :: (Status -> a) -> Move a
peek :: forall a. (Status -> a) -> Move a
peek = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets
whereIs :: SU2Member -> Move SLocation
whereIs :: SU2Member -> Move SLocation
whereIs SU2Member
p = forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
p forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sBono) (forall a. (Status -> a) -> Move a
peek Status -> SLocation
lBono)
forall a b. (a -> b) -> a -> b
$ forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
p forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sEdge) (forall a. (Status -> a) -> Move a
peek Status -> SLocation
lEdge)
forall a b. (a -> b) -> a -> b
$ forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
p forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sAdam) (forall a. (Status -> a) -> Move a
peek Status -> SLocation
lAdam)
(forall a. (Status -> a) -> Move a
peek Status -> SLocation
lLarry)
xferFlash :: Move ()
xferFlash :: Move ()
xferFlash = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \Status
s -> Status
s{flash :: SLocation
flash = forall a. Mergeable a => SBool -> a -> a -> a
ite (Status -> SLocation
flash Status
s forall a. EqSymbolic a => a -> a -> SBool
.== SLocation
sHere) SLocation
sThere SLocation
sHere}
xferPerson :: SU2Member -> Move ()
xferPerson :: SU2Member -> Move ()
xferPerson SU2Member
p = do ~[SLocation
lb, SLocation
le, SLocation
la, SLocation
ll] <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall a. (Status -> a) -> Move a
peek [Status -> SLocation
lBono, Status -> SLocation
lEdge, Status -> SLocation
lAdam, Status -> SLocation
lLarry]
let move :: SLocation -> SLocation
move SLocation
l = forall a. Mergeable a => SBool -> a -> a -> a
ite (SLocation
l forall a. EqSymbolic a => a -> a -> SBool
.== SLocation
sHere) SLocation
sThere SLocation
sHere
lb' :: SLocation
lb' = forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
p forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sBono) (SLocation -> SLocation
move SLocation
lb) SLocation
lb
le' :: SLocation
le' = forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
p forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sEdge) (SLocation -> SLocation
move SLocation
le) SLocation
le
la' :: SLocation
la' = forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
p forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sAdam) (SLocation -> SLocation
move SLocation
la) SLocation
la
ll' :: SLocation
ll' = forall a. Mergeable a => SBool -> a -> a -> a
ite (SU2Member
p forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sLarry) (SLocation -> SLocation
move SLocation
ll) SLocation
ll
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \Status
s -> Status
s{lBono :: SLocation
lBono = SLocation
lb', lEdge :: SLocation
lEdge = SLocation
le', lAdam :: SLocation
lAdam = SLocation
la', lLarry :: SLocation
lLarry = SLocation
ll'}
bumpTime1 :: SU2Member -> Move ()
bumpTime1 :: SU2Member -> Move ()
bumpTime1 SU2Member
p = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \Status
s -> Status
s{time :: STime
time = Status -> STime
time Status
s forall a. Num a => a -> a -> a
+ SU2Member -> STime
sCrossTime SU2Member
p}
bumpTime2 :: SU2Member -> SU2Member -> Move ()
bumpTime2 :: SU2Member -> SU2Member -> Move ()
bumpTime2 SU2Member
p1 SU2Member
p2 = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \Status
s -> Status
s{time :: STime
time = Status -> STime
time Status
s forall a. Num a => a -> a -> a
+ SU2Member -> STime
sCrossTime SU2Member
p1 forall a. OrdSymbolic a => a -> a -> a
`smax` SU2Member -> STime
sCrossTime SU2Member
p2}
whenS :: SBool -> Move () -> Move ()
whenS :: SBool -> Move () -> Move ()
whenS SBool
t Move ()
a = forall a. Mergeable a => SBool -> a -> a -> a
ite SBool
t Move ()
a (forall (m :: * -> *) a. Monad m => a -> m a
return ())
move1 :: SU2Member -> Move ()
move1 :: SU2Member -> Move ()
move1 SU2Member
p = do SLocation
f <- forall a. (Status -> a) -> Move a
peek Status -> SLocation
flash
SLocation
l <- SU2Member -> Move SLocation
whereIs SU2Member
p
SBool -> Move () -> Move ()
whenS (SLocation
f forall a. EqSymbolic a => a -> a -> SBool
.== SLocation
l) forall a b. (a -> b) -> a -> b
$ do SU2Member -> Move ()
bumpTime1 SU2Member
p
Move ()
xferFlash
SU2Member -> Move ()
xferPerson SU2Member
p
move2 :: SU2Member -> SU2Member -> Move ()
move2 :: SU2Member -> SU2Member -> Move ()
move2 SU2Member
p1 SU2Member
p2 = do SLocation
f <- forall a. (Status -> a) -> Move a
peek Status -> SLocation
flash
SLocation
l1 <- SU2Member -> Move SLocation
whereIs SU2Member
p1
SLocation
l2 <- SU2Member -> Move SLocation
whereIs SU2Member
p2
SBool -> Move () -> Move ()
whenS (SLocation
f forall a. EqSymbolic a => a -> a -> SBool
.== SLocation
l1 SBool -> SBool -> SBool
.&& SLocation
f forall a. EqSymbolic a => a -> a -> SBool
.== SLocation
l2) forall a b. (a -> b) -> a -> b
$ do SU2Member -> SU2Member -> Move ()
bumpTime2 SU2Member
p1 SU2Member
p2
Move ()
xferFlash
SU2Member -> Move ()
xferPerson SU2Member
p1
SU2Member -> Move ()
xferPerson SU2Member
p2
type Actions = [(SBool, SU2Member, SU2Member)]
run :: Actions -> Move [Status]
run :: Actions -> Move [Status]
run = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (SBool, SU2Member, SU2Member) -> StateT Status Identity Status
step
where step :: (SBool, SU2Member, SU2Member) -> StateT Status Identity Status
step (SBool
b, SU2Member
p1, SU2Member
p2) = forall a. Mergeable a => SBool -> a -> a -> a
ite SBool
b (SU2Member -> Move ()
move1 SU2Member
p1) (SU2Member -> SU2Member -> Move ()
move2 SU2Member
p1 SU2Member
p2) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s (m :: * -> *). MonadState s m => m s
get
isValid :: Actions -> SBool
isValid :: Actions -> SBool
isValid Actions
as = Status -> STime
time Status
end forall a. OrdSymbolic a => a -> a -> SBool
.<= STime
17 SBool -> SBool -> SBool
.&& forall a. (a -> SBool) -> [a] -> SBool
sAll (SBool, SU2Member, SU2Member) -> SBool
check Actions
as SBool -> SBool -> SBool
.&& forall {b}. EqSymbolic b => [b] -> [b] -> SBool
zigZag (forall a. [a] -> [a]
cycle [SLocation
sThere, SLocation
sHere]) (forall a b. (a -> b) -> [a] -> [b]
map Status -> SLocation
flash [Status]
states) SBool -> SBool -> SBool
.&& forall a. (a -> SBool) -> [a] -> SBool
sAll (forall a. EqSymbolic a => a -> a -> SBool
.== SLocation
sThere) [Status -> SLocation
lBono Status
end, Status -> SLocation
lEdge Status
end, Status -> SLocation
lAdam Status
end, Status -> SLocation
lLarry Status
end]
where check :: (SBool, SU2Member, SU2Member) -> SBool
check (SBool
s, SU2Member
p1, SU2Member
p2) = (SBool -> SBool
sNot SBool
s SBool -> SBool -> SBool
.=> SU2Member
p1 forall a. OrdSymbolic a => a -> a -> SBool
.> SU2Member
p2)
SBool -> SBool -> SBool
.&& (SBool
s SBool -> SBool -> SBool
.=> SU2Member
p2 forall a. EqSymbolic a => a -> a -> SBool
.== SU2Member
sBono)
states :: [Status]
states = forall s a. State s a -> s -> a
evalState (Actions -> Move [Status]
run Actions
as) Status
start
end :: Status
end = forall a. [a] -> a
last [Status]
states
zigZag :: [b] -> [b] -> SBool
zigZag [b]
reqs [b]
locs = [SBool] -> SBool
sAnd forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a. EqSymbolic a => a -> a -> SBool
(.==) [b]
locs [b]
reqs
solveN :: Int -> IO Bool
solveN :: Int -> IO Bool
solveN Int
n = do String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Checking for solutions with " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
" move" forall a. [a] -> [a] -> [a]
++ forall {a}. (Eq a, Num a) => a -> String
plu Int
n forall a. [a] -> [a] -> [a]
++ String
"."
let genAct :: SymbolicT IO (SBool, SU2Member, SU2Member)
genAct = do SBool
b <- forall a. SymVal a => Symbolic (SBV a)
sbvExists_
SU2Member
p1 <- forall a. SymVal a => Symbolic (SBV a)
sbvExists_
SU2Member
p2 <- forall a. SymVal a => Symbolic (SBV a)
sbvExists_
forall (m :: * -> *) a. Monad m => a -> m a
return (SBool
b, SU2Member
p1, SU2Member
p2)
AllSatResult
res <- forall a. Provable a => a -> IO AllSatResult
allSat forall a b. (a -> b) -> a -> b
$ Actions -> SBool
isValid forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall a b. a -> b -> a
const SymbolicT IO (SBool, SU2Member, SU2Member)
genAct) [Int
1..Int
n]
Int
cnt <- forall a.
SatModel a =>
([(Bool, a)] -> [(Bool, a)])
-> (Int -> (Bool, a) -> IO ()) -> AllSatResult -> IO Int
displayModels (forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn forall a. Show a => a -> String
show) Int -> (Bool, [(Bool, U2Member, U2Member)]) -> IO ()
disp AllSatResult
res
if Int
cnt forall a. Eq a => a -> a -> Bool
== Int
0 then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else do String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Found: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
cnt forall a. [a] -> [a] -> [a]
++ String
" solution" forall a. [a] -> [a] -> [a]
++ forall {a}. (Eq a, Num a) => a -> String
plu Int
cnt forall a. [a] -> [a] -> [a]
++ String
" with " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
" move" forall a. [a] -> [a] -> [a]
++ forall {a}. (Eq a, Num a) => a -> String
plu Int
n forall a. [a] -> [a] -> [a]
++ String
"."
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
where plu :: a -> String
plu a
v = if a
v forall a. Eq a => a -> a -> Bool
== a
1 then String
"" else String
"s"
disp :: Int -> (Bool, [(Bool, U2Member, U2Member)]) -> IO ()
disp :: Int -> (Bool, [(Bool, U2Member, U2Member)]) -> IO ()
disp Int
i (Bool
_, [(Bool, U2Member, U2Member)]
ss)
| Int
lss forall a. Eq a => a -> a -> Bool
/= Int
n = forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Expected " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
" results; got: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
lss
| Bool
True = do String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Solution #" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i forall a. [a] -> [a] -> [a]
++ String
": "
Bool -> Time -> [(Bool, U2Member, U2Member)] -> IO ()
go Bool
False Time
0 [(Bool, U2Member, U2Member)]
ss
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where lss :: Int
lss = forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Bool, U2Member, U2Member)]
ss
go :: Bool -> Time -> [(Bool, U2Member, U2Member)] -> IO ()
go Bool
_ Time
t [] = String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Total time: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Time
t
go Bool
l Time
t ((Bool
True, U2Member
a, U2Member
_):[(Bool, U2Member, U2Member)]
rest) = do String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
sh2 Time
t forall a. [a] -> [a] -> [a]
++ Bool -> String
shL Bool
l forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show U2Member
a
Bool -> Time -> [(Bool, U2Member, U2Member)] -> IO ()
go (Bool -> Bool
not Bool
l) (Time
t forall a. Num a => a -> a -> a
+ U2Member -> Time
crossTime U2Member
a) [(Bool, U2Member, U2Member)]
rest
go Bool
l Time
t ((Bool
False, U2Member
a, U2Member
b):[(Bool, U2Member, U2Member)]
rest) = do String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
sh2 Time
t forall a. [a] -> [a] -> [a]
++ Bool -> String
shL Bool
l forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show U2Member
a forall a. [a] -> [a] -> [a]
++ String
", " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show U2Member
b
Bool -> Time -> [(Bool, U2Member, U2Member)] -> IO ()
go (Bool -> Bool
not Bool
l) (Time
t forall a. Num a => a -> a -> a
+ U2Member -> Time
crossTime U2Member
a forall a. Ord a => a -> a -> a
`max` U2Member -> Time
crossTime U2Member
b) [(Bool, U2Member, U2Member)]
rest
sh2 :: p -> String
sh2 p
t = let s :: String
s = forall a. Show a => a -> String
show p
t in if forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s forall a. Ord a => a -> a -> Bool
< Int
2 then Char
' ' forall a. a -> [a] -> [a]
: String
s else String
s
shL :: Bool -> String
shL Bool
False = String
" --> "
shL Bool
True = String
" <-- "
solveU2 :: IO ()
solveU2 :: IO ()
solveU2 = Int -> IO ()
go Int
1
where go :: Int -> IO ()
go Int
i = do Bool
p <- Int -> IO Bool
solveN Int
i
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
p forall a b. (a -> b) -> a -> b
$ Int -> IO ()
go (Int
iforall a. Num a => a -> a -> a
+Int
1)