相关疑难解决方法(0)

实例签名:对方法的约束

使用InstanceSignatures,我可以为实例 decl 中的方法签名。

实例声明中的类型签名必须比类声明中的类型签名更多(或相同),使用实例类型实例化。

我可以从这个答案中看到sig 的类型部分不能更具体;但是为什么不能添加额外的约束呢?毕竟,您可以对实例 decl 施加约束,这将使实例比“使用实例类型实例化的类声明”更具体。

Addit:(响应前几条评论。)为了解释“您可以对实例 decl 进行约束……更具体的OVERLAPPABLE实例”:这里的实例头声称它提供addNat了所有类型a, b, c。但它没有:它只提供 if ais of the form SNat a'cis of the formSNat c'等。我在问为什么我不能在方法级别同样限制类型?

例如[改编自 Hughes 1999](更现实地说,这可能是 BST/玫瑰树等):

data AscList a = AscList [a]                          -- ascending list

instance Functor AscList  where                       -- constructor class
  fmap f (AscList xs) = AscList $ sort $ fmap f xs
  -- :: Ord …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass

2
推荐指数
1
解决办法
95
查看次数

标签 统计

haskell ×1

typeclass ×1