-----------------------------------------------------------------------------
-- |
-- Module    : Documentation.SBV.Examples.Puzzles.Birthday
-- Copyright : (c) Levent Erkok
-- License   : BSD3
-- Maintainer: erkokl@gmail.com
-- Stability : experimental
--
-- This is a formalization of the Cheryl's birthday problem, which went viral in April 2015.
-- (See <http://www.nytimes.com/2015/04/15/science/a-math-problem-from-singapore-goes-viral-when-is-cheryls-birthday.html>.)
--
-- Here's the puzzle:
--
-- @
-- Albert and Bernard just met Cheryl. “When’s your birthday?” Albert asked Cheryl.
--
-- Cheryl thought a second and said, “I’m not going to tell you, but I’ll give you some clues.” She wrote down a list of 10 dates:
--
--   May 15, May 16, May 19
--   June 17, June 18
--   July 14, July 16
--   August 14, August 15, August 17
--
-- “My birthday is one of these,” she said.
--
-- Then Cheryl whispered in Albert’s ear the month — and only the month — of her birthday. To Bernard, she whispered the day, and only the day. 
-- “Can you figure it out now?” she asked Albert.
--
-- Albert: I don’t know when your birthday is, but I know Bernard doesn’t know, either.
-- Bernard: I didn’t know originally, but now I do.
-- Albert: Well, now I know, too!
--
-- When is Cheryl’s birthday?
-- @
--
-- NB. Thanks to Amit Goel for suggesting the formalization strategy used in here.
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wall -Werror -Wno-incomplete-uni-patterns #-}

module Documentation.SBV.Examples.Puzzles.Birthday where

import Data.SBV

-----------------------------------------------------------------------------------------------
-- * Types and values
-----------------------------------------------------------------------------------------------

-- | Represent month by 8-bit words; We can also use an uninterpreted type, but numbers work well here.
type Month = SWord8

-- | Represent day by 8-bit words; Again, an uninterpreted type would work as well.
type Day = SWord8

-- | Months referenced in the problem.
may, june, july, august :: SWord8
[Day
may, Day
june, Day
july, Day
august] = [Day
5, Day
6, Day
7, Day
8]

-----------------------------------------------------------------------------------------------
-- * Helper predicates
-----------------------------------------------------------------------------------------------

-- | Check that a given month/day combo is a possible birth-date.
valid :: Month -> Day -> SBool
valid :: Day -> Day -> SBool
valid Day
month Day
day = (Day
month, Day
day) forall a. EqSymbolic a => a -> [a] -> SBool
`sElem` [(Day, Day)]
candidates
  where candidates :: [(Month, Day)]
        candidates :: [(Day, Day)]
candidates = [ (   Day
may, Day
15), (   Day
may, Day
16), (   Day
may, Day
19)
                     , (  Day
june, Day
17), (  Day
june, Day
18)
                     , (  Day
july, Day
14), (  Day
july, Day
16)
                     , (Day
august, Day
14), (Day
august, Day
15), (Day
august, Day
17)
                     ]

-- | Assert that the given function holds for one of the possible days.
existsDay :: (Day -> SBool) -> SBool
existsDay :: (Day -> SBool) -> SBool
existsDay Day -> SBool
f = forall a. (a -> SBool) -> [a] -> SBool
sAny (Day -> SBool
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SymVal a => a -> SBV a
literal) [Word8
14 .. Word8
19]

-- | Assert that the given function holds for all of the possible days.
forallDay :: (Day -> SBool) -> SBool
forallDay :: (Day -> SBool) -> SBool
forallDay Day -> SBool
f = forall a. (a -> SBool) -> [a] -> SBool
sAll (Day -> SBool
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SymVal a => a -> SBV a
literal) [Word8
14 .. Word8
19]

-- | Assert that the given function holds for one of the possible months.
existsMonth :: (Month -> SBool) -> SBool
existsMonth :: (Day -> SBool) -> SBool
existsMonth Day -> SBool
f = forall a. (a -> SBool) -> [a] -> SBool
sAny Day -> SBool
f [Day
may .. Day
august]

-- | Assert that the given function holds for all of the possible months.
forallMonth :: (Month -> SBool) -> SBool
forallMonth :: (Day -> SBool) -> SBool
forallMonth Day -> SBool
f = forall a. (a -> SBool) -> [a] -> SBool
sAll Day -> SBool
f [Day
may .. Day
august]

-----------------------------------------------------------------------------------------------
-- * The puzzle
-----------------------------------------------------------------------------------------------

-- | Encode the conversation as given in the puzzle.
--
-- NB. Lee Pike pointed out that not all the constraints are actually necessary! (Private
-- communication.) The puzzle still has a unique solution if the statements @a1@ and @b1@
-- (i.e., Albert and Bernard saying they themselves do not know the answer) are removed.
-- To experiment you can simply comment out those statements and observe that there still
-- is a unique solution. Thanks to Lee for pointing this out! In fact, it is instructive to
-- assert the conversation line-by-line, and see how the search-space gets reduced in each
-- step.
puzzle :: Predicate
puzzle :: Predicate
puzzle = do Day
birthDay   <- forall a. SymVal a => String -> Symbolic (SBV a)
sbvExists String
"birthDay"
            Day
birthMonth <- forall a. SymVal a => String -> Symbolic (SBV a)
sbvExists String
"birthMonth"

            -- Albert: I do not know
            let a1 :: Day -> SBool
a1 Day
m = (Day -> SBool) -> SBool
existsDay forall a b. (a -> b) -> a -> b
$ \Day
d1 -> (Day -> SBool) -> SBool
existsDay forall a b. (a -> b) -> a -> b
$ \Day
d2 ->
                           Day
d1 forall a. EqSymbolic a => a -> a -> SBool
./= Day
d2 SBool -> SBool -> SBool
.&& Day -> Day -> SBool
valid Day
m Day
d1 SBool -> SBool -> SBool
.&& Day -> Day -> SBool
valid Day
m Day
d2

            -- Albert: I know that Bernard doesn't know
            let a2 :: Day -> SBool
a2 Day
m = (Day -> SBool) -> SBool
forallDay forall a b. (a -> b) -> a -> b
$ \Day
d -> Day -> Day -> SBool
valid Day
m Day
d SBool -> SBool -> SBool
.=>
                          (Day -> SBool) -> SBool
existsMonth (\Day
m1 -> (Day -> SBool) -> SBool
existsMonth forall a b. (a -> b) -> a -> b
$ \Day
m2 ->
                                Day
m1 forall a. EqSymbolic a => a -> a -> SBool
./= Day
m2 SBool -> SBool -> SBool
.&& Day -> Day -> SBool
valid Day
m1 Day
d SBool -> SBool -> SBool
.&& Day -> Day -> SBool
valid Day
m2 Day
d)

            -- Bernard: I did not know
            let b1 :: Day -> SBool
b1 Day
d = (Day -> SBool) -> SBool
existsMonth forall a b. (a -> b) -> a -> b
$ \Day
m1 -> (Day -> SBool) -> SBool
existsMonth forall a b. (a -> b) -> a -> b
$ \Day
m2 ->
                           Day
m1 forall a. EqSymbolic a => a -> a -> SBool
./= Day
m2 SBool -> SBool -> SBool
.&& Day -> Day -> SBool
valid Day
m1 Day
d SBool -> SBool -> SBool
.&& Day -> Day -> SBool
valid Day
m2 Day
d

            -- Bernard: But now I know
            let b2p :: Day -> Day -> SBool
b2p Day
m Day
d = Day -> Day -> SBool
valid Day
m Day
d SBool -> SBool -> SBool
.&& Day -> SBool
a1 Day
m SBool -> SBool -> SBool
.&& Day -> SBool
a2 Day
m
                b2 :: Day -> SBool
b2  Day
d   = (Day -> SBool) -> SBool
forallMonth forall a b. (a -> b) -> a -> b
$ \Day
m1 -> (Day -> SBool) -> SBool
forallMonth forall a b. (a -> b) -> a -> b
$ \Day
m2 ->
                                (Day -> Day -> SBool
b2p Day
m1 Day
d SBool -> SBool -> SBool
.&& Day -> Day -> SBool
b2p Day
m2 Day
d) SBool -> SBool -> SBool
.=> Day
m1 forall a. EqSymbolic a => a -> a -> SBool
.== Day
m2

            -- Albert: Now I know too
            let a3p :: Day -> Day -> SBool
a3p Day
m Day
d = Day -> Day -> SBool
valid Day
m Day
d SBool -> SBool -> SBool
.&& Day -> SBool
a1 Day
m SBool -> SBool -> SBool
.&& Day -> SBool
a2 Day
m SBool -> SBool -> SBool
.&& Day -> SBool
b1 Day
d SBool -> SBool -> SBool
.&& Day -> SBool
b2 Day
d
                a3 :: Day -> SBool
a3 Day
m    = (Day -> SBool) -> SBool
forallDay forall a b. (a -> b) -> a -> b
$ \Day
d1 -> (Day -> SBool) -> SBool
forallDay forall a b. (a -> b) -> a -> b
$ \Day
d2 ->
                                (Day -> Day -> SBool
a3p Day
m Day
d1 SBool -> SBool -> SBool
.&& Day -> Day -> SBool
a3p Day
m Day
d2) SBool -> SBool -> SBool
.=> Day
d1 forall a. EqSymbolic a => a -> a -> SBool
.== Day
d2

            -- Assert all the statements made:
            forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ Day -> SBool
a1 Day
birthMonth
            forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ Day -> SBool
a2 Day
birthMonth
            forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ Day -> SBool
b1 Day
birthDay
            forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ Day -> SBool
b2 Day
birthDay
            forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ Day -> SBool
a3 Day
birthMonth

            -- Find a valid birth-day that satisfies the above constraints:
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Day -> Day -> SBool
valid Day
birthMonth Day
birthDay

-- | Find all solutions to the birthday problem. We have:
--
-- >>> cheryl
-- Solution #1:
--   birthDay   = 16 :: Word8
--   birthMonth =  7 :: Word8
-- This is the only solution.
cheryl :: IO ()
cheryl :: IO ()
cheryl = forall a. Show a => a -> IO ()
print forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. Provable a => a -> IO AllSatResult
allSat Predicate
puzzle