I would like to have a State Monad, where I keep a list of modes.
data State = S {
modes :: [Mode]
}
However, I have two requirements:
- Mode needs to be a parametrized with a type.
- Mode can be built on run time.
Here is how I tried to convince GHC:
Attempt 1, with a data type
data Mode a = Mode {
complF :: String -> IO [String]
action :: State -> String -> X a
}
data State a = S {
modes :: [Mode a]
}
initState :: (String -> IO [String]) -> (State -> String -> X a) -> State a
initState c a = {
modes = [buildMode c a]
}
buildMode :: (String -> IO [String]) -> (State -> String -> X a) -> Mode a
buildMode c a = {
complF = c
, action = a
}
Good.. however, this is not useful for me, since every mode has to be of type Mode a
inside a State a
. Meaning that no Mode String
and Mode ()
can co-exist within a State.
Attempt 2:, type class
class Mode a where
complF :: String -> IO [String]
action :: State -> String -> X a
data State = S {
modes :: (Mode a) => [a]
}
But, now I don't know how to build a mode in run time given complF
and action
. It seems that instances of type classes must be defined at compile time.
instance Mode DefaultMode where
complF :: ..
action :: ..
Is there a workaround?