我在图书馆工作 我想给一个循环类下定义 我简化了这个分类如下:
{-# 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
我认为这些都是在 < a href=> https://stackoverflow.com/ questions/8031288/variadic-list-list- constructor-how-to-default-to-the- 更正- type- and-get- type- safety- safett> 中处理的同样问题,但我在这里无法调整这个解决方案。我可以使用打字家庭或其他方法来为我的循环续存堆找到更方便用户的解决方案吗?