我正在开发一个库,我想在其中定义一个递归类,在这里我将其简化为:
{-# 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
这让我做,例如
*Main Data.Label> let f = runStack $ Snoc (Snoc Top fst) head :: [(a,x)] -> a
*Main Data.Label> f [('a',undefined)]
'a'
但这似乎需要仔细使用类型注释,否则......
*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
我认为这些问题与此问题中解决的问题相同,但我无法在这里调整该解决方案。我可以使用类型族或其他方法来为我的递归延续堆栈提供更用户友好的解决方案吗?