5 import qualified Data.Map.Strict as M
6 import Data.Map.Strict ((!))
9 import Control.Monad.State.Strict
10 -- import Control.Monad.Reader
11 -- import Control.Monad.Writer
12 -- import Control.Monad.RWS.Strict
14 import Control.Parallel.Strategies
17 type Memo = M.Map (Int, Int) Int
19 type MemoF = State Memo
23 do let resultPairs = fmap evalAck [0 .. (2 ^ 15 - 1)] `using` parList rdeepseq
24 let result = filter ((== 6) . snd) resultPairs
27 r7Gives6 :: Int -> Bool
28 r7Gives6 r7 = result == 6
29 where result = evalState (ackermann 4 1 r7) M.empty
31 evalAck :: Int -> (Int, Int)
32 evalAck r7 = (r7, evalState (ackermann 4 1 r7) M.empty)
35 ackermann :: Int -> Int -> Int -> MemoF Int
36 ackermann r0 r1 r7 = do
37 memoResult <- memoLookup r0 r1
42 then do let res = ((r1 + 1) `mod` (2 ^ 15))
46 then do res <- ackermann (r0 - 1) r7 r7
49 else do subResult <- ackermann r0 (r1 - 1) r7
50 res <- ackermann (r0 - 1) subResult r7
54 memoLookup :: Int -> Int -> MemoF (Maybe Int)
57 return $ M.lookup (r0, r1) table
59 memoStore :: Int -> Int -> Int -> MemoF ()
60 memoStore r0 r1 result =
62 let table' = M.insert (r0, r1) result table