Francois-Nicola Demers wrote:
| I have noticed that most monads <M, return, then> are constructed by
| abstraction on the feature they manage.
| [...]
| Does anyone know monads that could not been express as an
| abstraction?
I think you can "encode", or "mimick" every monad by the following type,
which is the monad of continuations:
type M a = (a -> Action) -> Action
unit :: a -> M a
unit a = \cont -> cont a
bind :: M a -> (a -> M b) -> M b
m `bind` k = \cont -> m (\a -> k a cont)
As you see, the type Action is not mentioned in these definitions, so we
can choose any structure on Action we want, to encode the "features" of
the monad.
Examples:
Writer Monad in Strings:
type Action = String
write :: String -> M ()
write s = \cont -> s ++ cont ()
Reader Monad in Env:
-- for some suitable type Action'
type Action = Env -> Action'
read :: M Env
read = \cont -> (\env -> cont env env)
State Monad in State (this is the same type as above...):
-- for some suitable type Action'
type Action = State -> Action'
update :: State -> M State
update new = \cont -> (\old -> cont old new)
Error monad:
type Action = Maybe String
raise :: String -> M ()
raise err = \cont -> Just err -- disable cont
handle :: (a -> M b) -> (String -> M b) -> M a -> M b
handle succ fail m = \cont ->
case m (\a -> succ a cont) of
Nothing -> Nothing
Just err -> fail err cont
Unfortunately the Haskell type system is often too restrictive to encode
the wanted features. I have for example no idea how to do lists in this
setting, without doing dirty type hacks in Haskell (but it _is_
possible... :-).
Regards,
Koen.
--
Koen Claessen,
[EMAIL PROTECTED],
http://www.cs.chalmers.se/~koen,
Chalmers University of Technology.