diff options
Diffstat (limited to 'src/Sequence/Formula.hs')
-rw-r--r-- | src/Sequence/Formula.hs | 23 |
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 | |||
13 | import Control.Monad | 13 | import Control.Monad |
14 | import Control.Monad.Except | 14 | import Control.Monad.Except |
15 | import Control.Monad.Reader | 15 | import Control.Monad.Reader |
16 | import Control.Monad.State | ||
17 | import Control.Monad.Base | ||
16 | import Numeric.Probability.Game.Event | 18 | import Numeric.Probability.Game.Event |
17 | import qualified Numeric.Probability.Game.Dice as D | 19 | import qualified Numeric.Probability.Game.Dice as D |
18 | 20 | ||
@@ -25,13 +27,17 @@ import Data.List | |||
25 | import Data.Maybe | 27 | import Data.Maybe |
26 | import Data.Either | 28 | import Data.Either |
27 | 29 | ||
28 | import Debug.Trace | 30 | import Data.Set (Set) |
31 | import qualified Data.Set as Set | ||
29 | 32 | ||
30 | 33 | ||
31 | type FormulaM input a = ReaderT input (ExceptT (Question input) EventM) a | 34 | type FormulaM input a = StateT (Set String) (ReaderT input (ExceptT (Question input) EventM)) a |
32 | 35 | ||
33 | type Formula input = FormulaM input Int | 36 | type Formula input = FormulaM input Int |
34 | 37 | ||
38 | instance MonadBase EventM EventM where | ||
39 | liftBase = id | ||
40 | |||
35 | data Question input = Question | 41 | data 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 | |||
66 | evalFormula :: (MonadIO m, Ord a, Show a) => input -> FormulaM input a -> m (input, a) | 72 | evalFormula :: (MonadIO m, Ord a, Show a) => input -> FormulaM input a -> m (input, a) |
67 | evalFormula = evalFormula' [] | 73 | evalFormula = 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 | ||
76 | val :: Integral a => Traversal' input (Formula input) -> String -> Bool -> Formula input | 81 | val :: Integral a => Traversal' input (Formula input) -> String -> Bool -> Formula input |
77 | val answer prompt keepResult = preview answer >>= maybe (throwError Question{..}) id . trace "val" | 82 | val 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 | ||
89 | d, z :: Integral a => Int -> FormulaM input a | 96 | d, z :: Integral a => Int -> FormulaM input a |
90 | d n = lift . lift . fmap fromIntegral $ D.d n | 97 | d n = liftBase . fmap fromIntegral $ D.d n |
91 | z n = lift . lift . fmap fromIntegral $ D.z n | 98 | z n = liftBase . fmap fromIntegral $ D.z n |