2012-04-30 107 views
9

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

{-# 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) 

有人可以澄清typechecker行为的原因吗?

+1

有人猜测,因为'v'类型与关联类型映射解析为什么没有关系? – ivanm

+2

可能相关:[ConstraintKinds和默认关联空约束](http://comments.gmane.org/gmane.comp.lang.haskell.glasgow.user/21058) – hammar

回答

4

这与7.4相关类型默认的错误0.1。几周前,我被告知#haskell这是一个已知的bug,但我在GHC trac上找不到它。

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) 

确实