默认约束种类被忽略

时间:2012-04-30 09:24:26

标签: haskell default type-constraints

在定义默认约束时遇到了一个奇怪的问题。如果约束是单位,则不选择默认实例。在所有其他情况下,它按预期工作。

{-# LANGUAGE TypeFamilies, ConstraintKinds #-}
import qualified GHC.Exts as E

class Expression a where
  type Constr a v :: E.Constraint
  --type Constr a v = ()         -- with this line compilation fails
  --type Constr a v = v ~ v      -- compiles
  wrap :: Constr a v => a -> Maybe v

instance Expression () where
  wrap () = Just undefined

main = print (wrap () :: Maybe Int)

有人可以澄清类型检查员行为的原因吗?

2 个答案:

答案 0 :(得分:4)

不是真的答案,但这不是ConstraintKinds

class Expression a where
   type Type a v
   type Type a v = ()
   wrap :: (Type a v) ~ () => a -> Maybe v

instance Expression () where
   wrap () = Just undefined

main = print (wrap () :: Maybe Int)

无法编译,但

class Expression a where
   type Type a v
   type Type a v = v
   wrap :: (Type a v) ~ v => a -> Maybe v

instance Expression () where
   wrap () = Just undefined

main = print (wrap () :: Maybe Int)

确实

答案 1 :(得分:4)

这是7.4.1中关联类型默认值的错误。几个星期前,我被告知#haskell这是一个已经修复的已知错误,但我在GHC trac上找不到它。