diff options
Diffstat (limited to 'src/Sequence/Formula.hs')
| -rw-r--r-- | src/Sequence/Formula.hs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/Sequence/Formula.hs b/src/Sequence/Formula.hs index 66672a2..c3e9e33 100644 --- a/src/Sequence/Formula.hs +++ b/src/Sequence/Formula.hs | |||
| @@ -23,6 +23,9 @@ import Text.Read (readMaybe) | |||
| 23 | import Data.Bool | 23 | import Data.Bool |
| 24 | import Data.List | 24 | import Data.List |
| 25 | import Data.Maybe | 25 | import Data.Maybe |
| 26 | import Data.Either | ||
| 27 | |||
| 28 | import Debug.Trace | ||
| 26 | 29 | ||
| 27 | 30 | ||
| 28 | type FormulaM input a = ReaderT input (ExceptT (Question input) EventM) a | 31 | type FormulaM input a = ReaderT input (ExceptT (Question input) EventM) a |
| @@ -35,6 +38,15 @@ data Question input = Question | |||
| 35 | , keepResult :: Bool | 38 | , keepResult :: Bool |
| 36 | } | 39 | } |
| 37 | 40 | ||
| 41 | instance Eq (Question a) where | ||
| 42 | (==) _ _ = True | ||
| 43 | |||
| 44 | instance Ord (Question a) where | ||
| 45 | (<=) _ _ = True | ||
| 46 | |||
| 47 | instance Show (Question a) where | ||
| 48 | show Question{..} = show prompt | ||
| 49 | |||
| 38 | instance Integral a => Num (FormulaM input a) where | 50 | instance Integral a => Num (FormulaM input a) where |
| 39 | (+) x y = (+) <$> x <*> y | 51 | (+) x y = (+) <$> x <*> y |
| 40 | (-) x y = (-) <$> x <*> y | 52 | (-) x y = (-) <$> x <*> y |
| @@ -51,17 +63,18 @@ quot' = liftM2 quot | |||
| 51 | askQuestion :: MonadIO m => input -> (Question input) -> m input | 63 | askQuestion :: MonadIO m => input -> (Question input) -> m input |
| 52 | askQuestion input q@(Question{..}) = flip (set answer) input . maybe (throwError q) return <$> askQ prompt (join . fmap readMaybe) | 64 | askQuestion input q@(Question{..}) = flip (set answer) input . maybe (throwError q) return <$> askQ prompt (join . fmap readMaybe) |
| 53 | 65 | ||
| 54 | evalFormula :: MonadIO m => input -> FormulaM input a -> m (input, a) | 66 | evalFormula :: (MonadIO m, Ord a, Show a) => input -> FormulaM input a -> m (input, a) |
| 55 | evalFormula = evalFormula' [] | 67 | evalFormula = evalFormula' [] |
| 56 | where | 68 | where |
| 57 | evalFormula' finalChanges input formula = do | 69 | evalFormula' finalChanges input formula = trace "evalFormula'" $ do |
| 58 | result <- liftIO . enact . runExceptT . (runReaderT ?? input) $ formula | 70 | result <- liftIO . enact . traceShowId . runExceptT . (runReaderT ?? input) $ formula |
| 71 | liftIO . traceIO $ show (isLeft result, isRight result) | ||
| 59 | case result of | 72 | case result of |
| 60 | Left q@(Question{..}) -> askQuestion input q >>= flip (evalFormula' $ bool (pure . set answer $ throwError q) mempty keepResult ++ finalChanges) formula | 73 | Left q@(Question{..}) -> askQuestion input q >>= flip (evalFormula' $ bool (pure . set answer $ throwError q) mempty keepResult ++ finalChanges) formula |
| 61 | Right result -> return (foldr ($) input finalChanges, result) | 74 | Right result -> return (foldr ($) input finalChanges, result) |
| 62 | 75 | ||
| 63 | val :: Integral a => Traversal' input (Formula input) -> String -> Bool -> Formula input | 76 | val :: Integral a => Traversal' input (Formula input) -> String -> Bool -> Formula input |
| 64 | val answer prompt keepResult = preview answer >>= maybe (throwError Question{..}) id | 77 | val answer prompt keepResult = preview answer >>= maybe (throwError Question{..}) id . trace "val" |
| 65 | 78 | ||
| 66 | -- viewL :: Lens' lInput sInput -> Prism' (FormulaM sInput a) (FormulaM lInput a) | 79 | -- viewL :: Lens' lInput sInput -> Prism' (FormulaM sInput a) (FormulaM lInput a) |
| 67 | -- viewL lTrav = iso asL asS | 80 | -- viewL lTrav = iso asL asS |
