summaryrefslogtreecommitdiff
path: root/src/Sequence/Formula.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Sequence/Formula.hs')
-rw-r--r--src/Sequence/Formula.hs23
1 files changed, 15 insertions, 8 deletions
diff --git a/src/Sequence/Formula.hs b/src/Sequence/Formula.hs
index c3e9e33..7fb4ccb 100644
--- a/src/Sequence/Formula.hs
+++ b/src/Sequence/Formula.hs
@@ -13,6 +13,8 @@ import Data.Data.Lens
13import Control.Monad 13import Control.Monad
14import Control.Monad.Except 14import Control.Monad.Except
15import Control.Monad.Reader 15import Control.Monad.Reader
16import Control.Monad.State
17import Control.Monad.Base
16import Numeric.Probability.Game.Event 18import Numeric.Probability.Game.Event
17import qualified Numeric.Probability.Game.Dice as D 19import qualified Numeric.Probability.Game.Dice as D
18 20
@@ -25,13 +27,17 @@ import Data.List
25import Data.Maybe 27import Data.Maybe
26import Data.Either 28import Data.Either
27 29
28import Debug.Trace 30import Data.Set (Set)
31import qualified Data.Set as Set
29 32
30 33
31type FormulaM input a = ReaderT input (ExceptT (Question input) EventM) a 34type FormulaM input a = StateT (Set String) (ReaderT input (ExceptT (Question input) EventM)) a
32 35
33type Formula input = FormulaM input Int 36type Formula input = FormulaM input Int
34 37
38instance MonadBase EventM EventM where
39 liftBase = id
40
35data Question input = Question 41data Question input = Question
36 { answer :: Traversal' input (Formula input) 42 { answer :: Traversal' input (Formula input)
37 , prompt :: String 43 , prompt :: String
@@ -66,15 +72,16 @@ askQuestion input q@(Question{..}) = flip (set answer) input . maybe (throwError
66evalFormula :: (MonadIO m, Ord a, Show a) => input -> FormulaM input a -> m (input, a) 72evalFormula :: (MonadIO m, Ord a, Show a) => input -> FormulaM input a -> m (input, a)
67evalFormula = evalFormula' [] 73evalFormula = evalFormula' []
68 where 74 where
69 evalFormula' finalChanges input formula = trace "evalFormula'" $ do 75 evalFormula' finalChanges input formula = do
70 result <- liftIO . enact . traceShowId . runExceptT . (runReaderT ?? input) $ formula 76 result <- liftIO . enact . runExceptT . (runReaderT ?? input) . (evalStateT ?? Set.empty) $ formula
71 liftIO . traceIO $ show (isLeft result, isRight result)
72 case result of 77 case result of
73 Left q@(Question{..}) -> askQuestion input q >>= flip (evalFormula' $ bool (pure . set answer $ throwError q) mempty keepResult ++ finalChanges) formula 78 Left q@(Question{..}) -> askQuestion input q >>= flip (evalFormula' $ bool (pure . set answer $ throwError q) mempty keepResult ++ finalChanges) formula
74 Right result -> return (foldr ($) input finalChanges, result) 79 Right result -> return (foldr ($) input finalChanges, result)
75 80
76val :: Integral a => Traversal' input (Formula input) -> String -> Bool -> Formula input 81val :: Integral a => Traversal' input (Formula input) -> String -> Bool -> Formula input
77val answer prompt keepResult = preview answer >>= maybe (throwError Question{..}) id . trace "val" 82val answer prompt keepResult = do
83 gets (Set.member prompt) >>= bool (modify $ Set.insert prompt) (throwError Question{..})
84 preview answer >>= maybe (throwError Question{..}) id
78 85
79-- viewL :: Lens' lInput sInput -> Prism' (FormulaM sInput a) (FormulaM lInput a) 86-- viewL :: Lens' lInput sInput -> Prism' (FormulaM sInput a) (FormulaM lInput a)
80-- viewL lTrav = iso asL asS 87-- viewL lTrav = iso asL asS
@@ -87,5 +94,5 @@ val answer prompt keepResult = preview answer >>= maybe (throwError Question{..}
87-- val' lTrav sTrav prompt keepResult = preview (lTrav . sTrav . viewL lTrav) >>= maybe (throwError Question{ answer = lTrav . sTrav . viewL lTrav, ..}) id 94-- val' lTrav sTrav prompt keepResult = preview (lTrav . sTrav . viewL lTrav) >>= maybe (throwError Question{ answer = lTrav . sTrav . viewL lTrav, ..}) id
88 95
89d, z :: Integral a => Int -> FormulaM input a 96d, z :: Integral a => Int -> FormulaM input a
90d n = lift . lift . fmap fromIntegral $ D.d n 97d n = liftBase . fmap fromIntegral $ D.d n
91z n = lift . lift . fmap fromIntegral $ D.z n 98z n = liftBase . fmap fromIntegral $ D.z n