我的工作,我希望定义我在这里已经简化为一个递归类库:类型线程异构列表和缺省(?)类型族?
{-# 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
我觉得这些都是在this question解决同样的问题,但是我无法在这里适应这种解决方案。我可以使用类型族还是其他方法为我的递归延续堆栈提供更加用户友好的解决方案?
你可能想要考虑一个'class Stack st',其中包含像'type Domain st a'和'type Codomain st a'这样的关联类型'或者类似的东西,但是我还没有仔细阅读以便知道这个会为你工作。 –