I'm working on a library where I want to define a recursive class that I've simplified here to:
{-# LANGUAGE MultiParamTypeClasses
, FlexibleInstances #-}
data Snoc st b c = Snoc (st b) (c -> b)
data Top a = Top
class StackTo a st c where
runStack :: st c -> (c -> a)
instance StackTo a Top a where
runStack _ = id
instance (StackTo a st b) => StackTo a (Snoc st b) c where
runStack (Snoc st' cb) = runStack st' . cb
This lets me do, e.g.
*Main Data.Label> let f = runStack $ Snoc (Snoc Top fst) head :: [(a,x)] -> a
*Main Data.Label> f [('a',undefined)]
'a'
But this seems to require careful use of type annotations, otherwise...
*Main Data.Label> let f = runStack $ Snoc (Snoc Top fst) head
<interactive>:1:1:
No instance for (StackTo a0 Top b0)
arising from a use of `runStack'
Possible fix: add an instance declaration for (StackTo a0 Top b0)
In the expression: runStack
In the expression: runStack $ Snoc (Snoc Top fst) head
In an equation for `it': it = runStack $ Snoc (Snoc Top fst) head
I think these are the same issues addressed in this question, but I'm having trouble adapting that solution here. Can I use type families or some other method to come up with a more user-friendly solution to my recursive continuation stack?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…