{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.Puzzles.Coins where
import Data.SBV
type Coin = SWord16
mkCoin :: Int -> Symbolic Coin
mkCoin :: Int -> Symbolic Coin
mkCoin Int
i = do c <- String -> Symbolic Coin
forall a. SymVal a => String -> Symbolic (SBV a)
free (String -> Symbolic Coin) -> String -> Symbolic Coin
forall a b. (a -> b) -> a -> b
$ Char
'c' Char -> String -> String
forall a. a -> [a] -> [a]
: Int -> String
forall a. Show a => a -> String
show Int
i
constrain $ sAny (.== c) [1, 5, 10, 25, 50, 100]
return c
combinations :: [a] -> [[a]]
combinations :: forall a. [a] -> [[a]]
combinations [a]
coins = [[[a]]] -> [[a]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [Int -> [a] -> [[a]]
forall {t} {a}. (Eq t, Num t) => t -> [a] -> [[a]]
combs Int
i [a]
coins | Int
i <- [Int
1 .. [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
coins]]
where combs :: t -> [a] -> [[a]]
combs t
0 [a]
_ = [[]]
combs t
_ [] = []
combs t
k (a
x:[a]
xs) = ([a] -> [a]) -> [[a]] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (t -> [a] -> [[a]]
combs (t
kt -> t -> t
forall a. Num a => a -> a -> a
-t
1) [a]
xs) [[a]] -> [[a]] -> [[a]]
forall a. [a] -> [a] -> [a]
++ t -> [a] -> [[a]]
combs t
k [a]
xs
c1 :: [Coin] -> SBool
c1 :: [Coin] -> SBool
c1 [Coin]
xs = [Coin] -> Coin
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Coin]
xs Coin -> Coin -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= Coin
100
c2 :: [Coin] -> SBool
c2 :: [Coin] -> SBool
c2 [Coin]
xs = [Coin] -> Coin
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Coin]
xs Coin -> Coin -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= Coin
50
c3 :: [Coin] -> SBool
c3 :: [Coin] -> SBool
c3 [Coin]
xs = [Coin] -> Coin
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Coin]
xs Coin -> Coin -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= Coin
25
c4 :: [Coin] -> SBool
c4 :: [Coin] -> SBool
c4 [Coin]
xs = [Coin] -> Coin
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Coin]
xs Coin -> Coin -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= Coin
10
c5 :: [Coin] -> SBool
c5 :: [Coin] -> SBool
c5 [Coin]
xs = [Coin] -> Coin
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Coin]
xs Coin -> Coin -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= Coin
5
c6 :: [Coin] -> SBool
c6 :: [Coin] -> SBool
c6 [Coin]
xs = [Coin] -> Coin
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Coin -> Coin) -> [Coin] -> [Coin]
forall a b. (a -> b) -> [a] -> [b]
map Coin -> Coin
forall {a}. (Mergeable a, EqSymbolic a, Num a) => a -> a
val [Coin]
xs) Coin -> Coin -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= Coin
95
where val :: a -> a
val a
x = SBool -> a -> a -> a
forall a. Mergeable a => SBool -> a -> a -> a
ite (a
x a -> a -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== a
50) a
0 a
x
puzzle :: IO SatResult
puzzle :: IO SatResult
puzzle = SymbolicT IO SBool -> IO SatResult
forall a. Satisfiable a => a -> IO SatResult
sat (SymbolicT IO SBool -> IO SatResult)
-> SymbolicT IO SBool -> IO SatResult
forall a b. (a -> b) -> a -> b
$ do
cs <- (Int -> Symbolic Coin) -> [Int] -> SymbolicT IO [Coin]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Int -> Symbolic Coin
mkCoin [Int
1..Int
6]
mapM_ constrain [c s | s <- combinations cs, length s >= 2, c <- [c1, c2, c3, c4, c5, c6]]
constrain $ sAnd $ zipWith (.>=) cs (drop 1 cs)
return $ sum cs .== 115