diff options
author | Gregor Kleen <pngwjpgh@users.noreply.github.com> | 2016-06-25 17:50:52 +0200 |
---|---|---|
committer | Gregor Kleen <pngwjpgh@users.noreply.github.com> | 2016-06-25 17:50:52 +0200 |
commit | eea3a546370ed95321dcc21b4db739ad0d893dfb (patch) | |
tree | 6e7686fcd84dccf83d85b4245022ec61e1a5f8d2 /src/Sequence/Formula.hs | |
parent | 7a80cf63d55c62a2e5fbf4c937fae4e33f5629c2 (diff) | |
download | 2017-01-16_17:13:37-eea3a546370ed95321dcc21b4db739ad0d893dfb.tar 2017-01-16_17:13:37-eea3a546370ed95321dcc21b4db739ad0d893dfb.tar.gz 2017-01-16_17:13:37-eea3a546370ed95321dcc21b4db739ad0d893dfb.tar.bz2 2017-01-16_17:13:37-eea3a546370ed95321dcc21b4db739ad0d893dfb.tar.xz 2017-01-16_17:13:37-eea3a546370ed95321dcc21b4db739ad0d893dfb.zip |
Inspect entities
Diffstat (limited to 'src/Sequence/Formula.hs')
-rw-r--r-- | src/Sequence/Formula.hs | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/Sequence/Formula.hs b/src/Sequence/Formula.hs index a3675c6..2ac1210 100644 --- a/src/Sequence/Formula.hs +++ b/src/Sequence/Formula.hs | |||
@@ -4,6 +4,7 @@ module Sequence.Formula | |||
4 | ( FormulaM, Formula, quot' | 4 | ( FormulaM, Formula, quot' |
5 | , (:<:)(..), Context(..), ctx | 5 | , (:<:)(..), Context(..), ctx |
6 | , evalFormula, evalFormula' | 6 | , evalFormula, evalFormula' |
7 | , findDistribution, findDistribution' | ||
7 | , val | 8 | , val |
8 | , d, z | 9 | , d, z |
9 | , Table, table | 10 | , Table, table |
@@ -109,18 +110,33 @@ askQuestion promptPref input q@(Question{..}) = flip (if keepResult then set $ _ | |||
109 | sep = "»" | 110 | sep = "»" |
110 | 111 | ||
111 | evalFormula :: (MonadIO m, sInput :<: lInput) => [String] -> lInput -> FormulaM sInput a -> m (lInput, a) | 112 | evalFormula :: (MonadIO m, sInput :<: lInput) => [String] -> lInput -> FormulaM sInput a -> m (lInput, a) |
112 | evalFormula promptPref input = evalFormula' [] promptPref (input, Nothing) | 113 | evalFormula = primEvalFormula $ liftIO . enact |
114 | |||
115 | findDistribution :: (MonadIO m, sInput :<: lInput, Ord a) => [String] -> lInput -> FormulaM sInput a -> m (lInput, (Map a Rational)) | ||
116 | findDistribution = primEvalFormula $ return . fmap Map.fromList . seqEither . outcomes | ||
113 | where | 117 | where |
114 | evalFormula' :: (MonadIO m, sInput :<: lInput) => [lInput -> lInput] -> [String] -> (lInput, Maybe (Formula sInput)) -> FormulaM sInput a -> m (lInput, a) | 118 | seqEither :: [(Either q a, Rational)] -> Either q [(a, Rational)] |
115 | evalFormula' finalChanges promptPref (input, fSt) formula = do | 119 | seqEither = mapM seqEither' |
116 | result <- liftIO . enact . runExceptT . (runReaderT ?? (Context input fSt)) . (evalStateT ?? Set.empty) $ formula | 120 | |
121 | seqEither' (Left a, _) = Left a | ||
122 | seqEither' (Right b, c) = Right (b, c) | ||
123 | |||
124 | primEvalFormula :: (MonadIO m, sInput :<: lInput) => (EventM (Either (Question sInput) a) -> m (Either (Question sInput) r)) -> [String] -> lInput -> FormulaM sInput a -> m (lInput, r) | ||
125 | primEvalFormula fromOutcomes promptPref input = evalFormula' fromOutcomes [] promptPref (input, Nothing) | ||
126 | where | ||
127 | evalFormula' :: (MonadIO m, sInput :<: lInput) => (EventM (Either (Question sInput) a) -> m (Either (Question sInput) r)) -> [lInput -> lInput] -> [String] -> (lInput, Maybe (Formula sInput)) -> FormulaM sInput a -> m (lInput, r) | ||
128 | evalFormula' fromOutcomes finalChanges promptPref (input, fSt) formula = do | ||
129 | result <- fromOutcomes . runExceptT . (runReaderT ?? (Context input fSt)) . (evalStateT ?? Set.empty) $ formula | ||
117 | case result of | 130 | case result of |
118 | Left q@(Question{..}) -> askQuestion promptPref (input, fSt) q >>= flip (flip evalFormula' promptPref $ set (ctx' . answer) (throwError q) : finalChanges) formula | 131 | Left q@(Question{..}) -> askQuestion promptPref (input, fSt) q >>= flip (flip (evalFormula' fromOutcomes) promptPref $ set (ctx' . answer) (throwError q) : finalChanges) formula |
119 | Right result -> return (foldr ($) input finalChanges, result) | 132 | Right result -> return (foldr ($) input finalChanges, result) |
120 | 133 | ||
121 | evalFormula' :: (MonadIO m, sInput :<: lInput, MonadState lInput m) => [String] -> FormulaM sInput a -> m a | 134 | evalFormula' :: (MonadIO m, sInput :<: lInput, MonadState lInput m) => [String] -> FormulaM sInput a -> m a |
122 | evalFormula' promptPref formula = uncurry (<$) . over _2 put . swap =<< flip (evalFormula promptPref) formula =<< get | 135 | evalFormula' promptPref formula = uncurry (<$) . over _2 put . swap =<< flip (evalFormula promptPref) formula =<< get |
123 | 136 | ||
137 | findDistribution' :: (MonadIO m, sInput :<: lInput, MonadState lInput m, Ord a) => [String] -> FormulaM sInput a -> m (Map a Rational) | ||
138 | findDistribution' promptPref formula = uncurry (<$) . over _2 put . swap =<< flip (findDistribution promptPref) formula =<< get | ||
139 | |||
124 | val :: Traversal' input (Formula input) -> [String] -> Bool -> Formula input | 140 | val :: Traversal' input (Formula input) -> [String] -> Bool -> Formula input |
125 | val answer prompt keepResult = do | 141 | val answer prompt keepResult = do |
126 | gets (Set.member prompt) >>= bool (modify $ Set.insert prompt) (modify (Set.delete prompt) >> throwError Question{..}) | 142 | gets (Set.member prompt) >>= bool (modify $ Set.insert prompt) (modify (Set.delete prompt) >> throwError Question{..}) |