2012-04-30 103 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) 

確實