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.hs21
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)
23import Data.Bool 23import Data.Bool
24import Data.List 24import Data.List
25import Data.Maybe 25import Data.Maybe
26import Data.Either
27
28import Debug.Trace
26 29
27 30
28type FormulaM input a = ReaderT input (ExceptT (Question input) EventM) a 31type 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
41instance Eq (Question a) where
42 (==) _ _ = True
43
44instance Ord (Question a) where
45 (<=) _ _ = True
46
47instance Show (Question a) where
48 show Question{..} = show prompt
49
38instance Integral a => Num (FormulaM input a) where 50instance 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
51askQuestion :: MonadIO m => input -> (Question input) -> m input 63askQuestion :: MonadIO m => input -> (Question input) -> m input
52askQuestion input q@(Question{..}) = flip (set answer) input . maybe (throwError q) return <$> askQ prompt (join . fmap readMaybe) 64askQuestion input q@(Question{..}) = flip (set answer) input . maybe (throwError q) return <$> askQ prompt (join . fmap readMaybe)
53 65
54evalFormula :: MonadIO m => input -> FormulaM input a -> m (input, a) 66evalFormula :: (MonadIO m, Ord a, Show a) => input -> FormulaM input a -> m (input, a)
55evalFormula = evalFormula' [] 67evalFormula = 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
63val :: Integral a => Traversal' input (Formula input) -> String -> Bool -> Formula input 76val :: Integral a => Traversal' input (Formula input) -> String -> Bool -> Formula input
64val answer prompt keepResult = preview answer >>= maybe (throwError Question{..}) id 77val 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