- loose-fish
- Apr 1, 2005
-
|
While playing around with generic execution models I ran into the following, a typeclass with a function that returns another instance of that typeclass:
code:class Processor a where
process :: Processor b => a -> Int -> b
data P = P Int
instance Processor P where
process (P x) y = P y
-- can't return P because it's no Processor
-- more precise, but pointless
instance (Processor P) => Processor P where
process (P x) y = P y
Is there a way to achieve something like this in Haskell? Type families maybe? I'm not up to speed on all extensions...
|
#
¿
May 20, 2017 12:33
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 14, 2024 04:44
|
|
- loose-fish
- Apr 1, 2005
-
|
Thanks for clearing that up! I had a fundamental misconception of type classes, guess the 'forall' should've tipped me off...
I'm trying to find a solution for processors with arbitrary local state, and a function to apply an event to the global state according to the current processor. (I say 'State' but I don't care if it's in the State Monad, it's just what I'm using here).
This is the best I've come up with so far:
code:import Control.Monad.State (State, put)
type Model = String
type Event = Int
data AppState = AppState { _model :: Model
, _processor :: Event -> State AppState ()
}
class Processor a where
process :: a -> Event -> State AppState ()
data P = P Int
instance Processor P where
process (P 0) x = put $ AppState "foo" (process $ Q x (x * 2)) -- change processor to Q
process (P _) x = put $ AppState (show x) (process $ P x)
data Q = Q Int Int
instance Processor Q where
process (Q 0 0) x = put $ AppState "bar" (process $ P x) -- change processor to P
process (Q _ _) x = put $ AppState (show x) (process $ Q x (x * 2))
I know the simple way to do it would be something like this:
code:data Processor = P Int | Q Int Int
process :: Processor Model -> Event -> (Processor, Model)
process (P p) m = processP p m
process (Q q) m = processQ q m
But this would make it impossible to add new Processors in separate modules...
|
#
¿
May 21, 2017 11:53
|
|