| 1 |
{- | Implementation of monads that allow the computation |
| 2 |
to 'Control.Monad.Prompt.prompt' for further input. |
| 3 |
|
| 4 |
(c) 2008 Bertram Felgenhauer & Ryan Ingram |
| 5 |
Released as open source under a 3 clause BSD license. See the LICENSE |
| 6 |
file in the source code distribution for further information. |
| 7 |
|
| 8 |
RecPromptT added by Cale Gibbard, contributed under the same license. |
| 9 |
|
| 10 |
MonadPrompt monads allow you to pass some object of the prompt |
| 11 |
type in, and get a result of the prompt's answer type out. |
| 12 |
-} |
| 13 |
|
| 14 |
{-# LANGUAGE RankNTypes #-} |
| 15 |
{-# LANGUAGE FlexibleInstances #-} |
| 16 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 17 |
{-# LANGUAGE FunctionalDependencies #-} |
| 18 |
module Control.Monad.Prompt ( |
| 19 |
MonadPrompt(..), |
| 20 |
Prompt, |
| 21 |
runPromptC, |
| 22 |
runPrompt, |
| 23 |
runPromptM, |
| 24 |
RecPrompt, |
| 25 |
unRecPrompt, |
| 26 |
runRecPromptC, |
| 27 |
runRecPrompt, |
| 28 |
runRecPromptM, |
| 29 |
PromptT, |
| 30 |
runPromptT, |
| 31 |
runPromptTM, |
| 32 |
runPromptTM', |
| 33 |
Lift(..), |
| 34 |
unPromptT, |
| 35 |
liftP, |
| 36 |
RecPromptT, |
| 37 |
unRecPromptT, |
| 38 |
runRecPromptT |
| 39 |
|
| 40 |
) where |
| 41 |
import Control.Applicative (Applicative(..)) |
| 42 |
import Control.Monad (ap, liftM) |
| 43 |
import Control.Monad.Trans (MonadTrans(..)) |
| 44 |
|
| 45 |
{- |You can construct a monad very simply with prompt, by putting |
| 46 |
all of its effects as terms in a GADT, like the following example: |
| 47 |
|
| 48 |
@ |
| 49 |
data PromptState s a where |
| 50 |
Put :: s -> PromptState s () |
| 51 |
Get :: PromptState s s |
| 52 |
@ |
| 53 |
|
| 54 |
You then use 'prompt' to access effects: |
| 55 |
|
| 56 |
@ |
| 57 |
postIncrement :: MonadPrompt (PromptState Int) m => m Int |
| 58 |
postIncrement = |
| 59 |
do x <- prompt Get |
| 60 |
prompt (Put (x+1)) |
| 61 |
return x |
| 62 |
@ |
| 63 |
|
| 64 |
The advantage of Prompt over implementing effects directly: |
| 65 |
|
| 66 |
1. Prompt is pure; it is only through the observation function |
| 67 |
runPromptC that you can cause effects. |
| 68 |
|
| 69 |
2. You don't have to worry about the monad laws; they are |
| 70 |
correct by construction and you cannot break them. |
| 71 |
|
| 72 |
3. You can implement several observation functions for the same |
| 73 |
type. See, for example, <http://paste.lisp.org/display/53766> |
| 74 |
where a guessing game is implemented with an IO observation |
| 75 |
function for the user, and an AI observation function that |
| 76 |
plays the game automatically. |
| 77 |
|
| 78 |
In these ways Prompt is similar to Unimo, but bind and return |
| 79 |
are inlined into the computation, whereas in Unimo they are |
| 80 |
handled as a term calculus. |
| 81 |
See <http://sneezy.cs.nott.ac.uk/fplunch/weblog/?p=89> |
| 82 |
-} |
| 83 |
class Monad m => MonadPrompt p m | m -> p where |
| 84 |
prompt :: p a -> m a |
| 85 |
{- |
| 86 |
For any prompt p, Prompt p is an instance of MonadPrompt p. |
| 87 |
-} |
| 88 |
newtype Prompt p r = Prompt { |
| 89 |
runP :: forall b . (r -> b) -> (forall a . p a -> (a -> b) -> b) -> b |
| 90 |
} |
| 91 |
|
| 92 |
instance Monad (Prompt p) where |
| 93 |
return a = Prompt $ \done _ -> done a |
| 94 |
f >>= g = Prompt $ \done prm -> runP f (\x -> runP (g x) done prm) prm |
| 95 |
|
| 96 |
instance Functor (Prompt p) where |
| 97 |
fmap = liftM |
| 98 |
|
| 99 |
instance Applicative (Prompt p) where |
| 100 |
pure = return |
| 101 |
(<*>) = ap |
| 102 |
|
| 103 |
instance MonadPrompt p (Prompt p) where |
| 104 |
prompt p = Prompt $ \done prm -> prm p done |
| 105 |
|
| 106 |
{- |'runPromptC' is the observation function for prompts. It takes |
| 107 |
two functions as arguments: |
| 108 |
|
| 109 |
1. @ret@ will be called with the final result of the computation, |
| 110 |
to convert it to the answer type. |
| 111 |
|
| 112 |
2. @prm@ will be called if there are any effects; it is passed |
| 113 |
a prompt and a continuation function. prm can apply |
| 114 |
the effect requested by the prompt and call the continuation. |
| 115 |
|
| 116 |
In some cases prm can return the answer type directly; it |
| 117 |
may be useful to abort the remainder of the computation, or |
| 118 |
save off the continuation to be called later. There is a great |
| 119 |
example of using this to implement a UI for peg solitaire in Bertram |
| 120 |
Felgenhauer's post to Haskell-Cafe at |
| 121 |
<http://www.haskell.org/pipermail/haskell-cafe/2008-January/038301.html> |
| 122 |
-} |
| 123 |
|
| 124 |
runPromptC :: forall p r b. -- prompt, computation result, answer type |
| 125 |
(r -> b) -- ^ handler when there is no further computation |
| 126 |
-> (forall a . p a -> (a -> b) -> b) |
| 127 |
-- ^ handler for prompts |
| 128 |
-> Prompt p r -- ^ a prompt-based computation |
| 129 |
-> b -- ^ answer |
| 130 |
runPromptC ret prm p = runP p ret prm |
| 131 |
|
| 132 |
{- |'runPrompt' takes a way of converting prompts to an element in a pure |
| 133 |
fashion and calculates the result of the prompt -} |
| 134 |
|
| 135 |
runPrompt :: (forall a. p a -> a) -> Prompt p r -> r |
| 136 |
runPrompt prm = runPromptC id (\p cont -> cont $ prm p) |
| 137 |
|
| 138 |
{- |'runPromptM' is similar to 'runPrompt' but allows the computation to happen in any monad. -} |
| 139 |
|
| 140 |
runPromptM :: Monad m => (forall a . p a -> m a) -> Prompt p r -> m r |
| 141 |
runPromptM prm = runPromptC return (\p cont -> prm p >>= cont) |
| 142 |
|
| 143 |
{- | 'RecPrompt' is for prompts which are dependent on the prompt monad. |
| 144 |
|
| 145 |
For example, a 'MonadPlus' prompt: |
| 146 |
|
| 147 |
@ |
| 148 |
data PromptPlus m a where |
| 149 |
PromptZero :: PromptPlus m a |
| 150 |
PromptPlus :: m a -> m a -> PromptPlus m a |
| 151 |
|
| 152 |
instance MonadPlus (RecPrompt PromptPlus) where |
| 153 |
mzero = prompt PromptZero |
| 154 |
mplus x y = prompt (PromptPlus x y) |
| 155 |
@ |
| 156 |
-} |
| 157 |
newtype RecPrompt p r = RecPrompt { unRecPrompt :: Prompt (p (RecPrompt p)) r } |
| 158 |
|
| 159 |
instance Monad (RecPrompt p) where |
| 160 |
return = RecPrompt . return |
| 161 |
m >>= f = RecPrompt $ unRecPrompt m >>= (unRecPrompt . f) |
| 162 |
|
| 163 |
instance Functor (RecPrompt p) where |
| 164 |
fmap = liftM |
| 165 |
|
| 166 |
instance Applicative (RecPrompt p) where |
| 167 |
pure = return |
| 168 |
(<*>) = ap |
| 169 |
|
| 170 |
instance MonadPrompt (p (RecPrompt p)) (RecPrompt p) where |
| 171 |
prompt = RecPrompt . prompt |
| 172 |
|
| 173 |
{- | Runs a recursive prompt computation. This is similar to 'runPromptC', but for recursive prompt types. -} |
| 174 |
runRecPromptC :: forall p r b. -- prompt, computation result, answer type |
| 175 |
(r -> b) -- ^ handler when there is no further computation |
| 176 |
-> (forall a . p (RecPrompt p) a -> (a -> b) -> b) |
| 177 |
-- ^ handler for prompts |
| 178 |
-> RecPrompt p r -- ^ a prompt-based computation |
| 179 |
-> b -- ^ answer |
| 180 |
runRecPromptC ret prm = runPromptC ret prm . unRecPrompt |
| 181 |
|
| 182 |
{- | Run a recursive prompt computation in a pure fashion, similar to 'runPrompt'. -} |
| 183 |
runRecPrompt :: (forall a. p (RecPrompt p) a -> a) -> RecPrompt p r -> r |
| 184 |
runRecPrompt prm = runPrompt prm . unRecPrompt |
| 185 |
|
| 186 |
{- | Run a recursive prompt computation in an arbitrary monad, similar to 'runPromptM'. -} |
| 187 |
runRecPromptM :: Monad m => (forall a . p (RecPrompt p) a -> m a) -> RecPrompt p r -> m r |
| 188 |
runRecPromptM prm = runPromptM prm . unRecPrompt |
| 189 |
|
| 190 |
{- | Prompt can also be used to define monad transformers. |
| 191 |
|
| 192 |
You will notice the lack of a @Monad m@ constraint; this is allowed |
| 193 |
because Prompt doesn't use the underlying monad at all; instead |
| 194 |
the observation function (generally implemented via 'runPromptT') |
| 195 |
will have the constraint. |
| 196 |
|
| 197 |
-} |
| 198 |
|
| 199 |
newtype PromptT p m a = PromptT { unPromptT :: Prompt (Lift p m) a } |
| 200 |
|
| 201 |
{- | A higher-kinded Either, used in defining 'PromptT'. -} |
| 202 |
data Lift p m a = Effect (p a) | Lift (m a) |
| 203 |
|
| 204 |
instance Monad (PromptT p m) where |
| 205 |
return = PromptT . return |
| 206 |
m >>= f = PromptT $ unPromptT m >>= (unPromptT . f) |
| 207 |
|
| 208 |
instance Functor (PromptT p m) where |
| 209 |
fmap = liftM |
| 210 |
|
| 211 |
instance Applicative (PromptT p m) where |
| 212 |
pure = return |
| 213 |
(<*>) = ap |
| 214 |
|
| 215 |
instance MonadPrompt p (PromptT p m) where |
| 216 |
prompt = PromptT . prompt . Effect |
| 217 |
|
| 218 |
instance MonadTrans (PromptT p) where |
| 219 |
lift = PromptT . prompt . Lift |
| 220 |
|
| 221 |
{- | 'runPromptT' runs a prompt monad transformer. -} |
| 222 |
runPromptT :: forall p m r b. |
| 223 |
(r -> b) -- ^ handler when there is no further computation |
| 224 |
-> (forall a . p a -> (a -> b) -> b) |
| 225 |
-- ^ handler for prompts |
| 226 |
-> (forall a . m a -> (a -> b) -> b) |
| 227 |
-- ^ handler for lifted computations |
| 228 |
-> PromptT p m r -- ^ a prompt-based computation |
| 229 |
-> b -- ^ answer |
| 230 |
runPromptT ret prm lft = runPromptC ret prm' . unPromptT where |
| 231 |
prm' (Effect e) = prm e |
| 232 |
prm' (Lift a) = lft a |
| 233 |
|
| 234 |
{- | 'runPromptTM' is a useful variant of runPromptT when interpreting into another monad -} |
| 235 |
runPromptTM :: forall p m r n. (Monad n) |
| 236 |
=> (forall a. p a -> n a) -- ^ interpretation for prompts |
| 237 |
-> (forall a. m a -> n a) -- ^ interpretation for lifted computations |
| 238 |
-> PromptT p m r -- ^ a prompt-based computation |
| 239 |
-> n r -- ^ resulting interpretation |
| 240 |
runPromptTM prm lft = runPromptT return (\p k -> prm p >>= k) (\l k -> lft l >>= k) |
| 241 |
|
| 242 |
{- | 'runPromptTM'' specialises runPromptTM further for the case that you're interpreting to the base monad by supplying the identity function as the interpretation |
| 243 |
for lifted computations -} |
| 244 |
runPromptTM' :: forall p m r. (Monad m) |
| 245 |
=> (forall a. p a -> m a) -- ^ interpretation for prompts |
| 246 |
-> PromptT p m r -- ^ a prompt-based computation |
| 247 |
-> m r -- ^ resulting interpretation |
| 248 |
runPromptTM' prm = runPromptTM prm id |
| 249 |
|
| 250 |
{- | You can also lift any Prompt computation into a PromptT (or more generally, any appropriate MonadPrompt instance). This is the kind of place where the advantage of being able to use multiple observation functions on Prompt really shows. -} |
| 251 |
|
| 252 |
liftP :: (MonadPrompt p m) => Prompt p r -> m r |
| 253 |
liftP = runPromptM prompt |
| 254 |
|
| 255 |
{- | A recursive variant of the prompt monad transformer. -} |
| 256 |
newtype RecPromptT p m a = |
| 257 |
RecPromptT { unRecPromptT :: Prompt (Lift (p (RecPromptT p m)) m) a } |
| 258 |
|
| 259 |
instance Monad (RecPromptT p m) where |
| 260 |
return = RecPromptT . return |
| 261 |
m >>= f = RecPromptT $ unRecPromptT m >>= (unRecPromptT . f) |
| 262 |
|
| 263 |
instance Functor (RecPromptT p m) where |
| 264 |
fmap = liftM |
| 265 |
|
| 266 |
instance Applicative (RecPromptT p m) where |
| 267 |
pure = return |
| 268 |
(<*>) = ap |
| 269 |
|
| 270 |
instance MonadPrompt (p (RecPromptT p m)) (RecPromptT p m) where |
| 271 |
prompt = RecPromptT . prompt . Effect |
| 272 |
|
| 273 |
instance MonadTrans (RecPromptT p) where |
| 274 |
lift = RecPromptT . prompt . Lift |
| 275 |
|
| 276 |
{- | Run a recursive prompt monad transformer. -} |
| 277 |
runRecPromptT :: forall p r b m. |
| 278 |
(r -> b) -- ^ handler when there is no further computation |
| 279 |
-> (forall a . p (RecPromptT p m) a -> (a -> b) -> b) |
| 280 |
-- ^ handler for prompts |
| 281 |
-> (forall a . m a -> (a -> b) -> b) |
| 282 |
-- ^ handler for lifted computations |
| 283 |
-> RecPromptT p m r -- ^ a prompt-based computation |
| 284 |
-> b -- ^ answer |
| 285 |
runRecPromptT ret prm lft = runPromptC ret prm' . unRecPromptT where |
| 286 |
prm' (Effect e) = prm e |
| 287 |
prm' (Lift a) = lft a |
| 288 |
|