2014-09-27 76 views
0

下面的代碼片段這個實例聲明如何導致模糊的情況?

data Tree k v = ETree | Node { leftTreeOf :: Tree k v, 
           rightTreeOf :: Tree k v, 
           tKey  :: k, 
           tVal  :: v 
           } 

instance Show s => Show (Tree s s) where 
    show = showTree 0 

產量

Illegal instance declaration for `Show (Tree s s)' 
    (All instance types must be of the form (T a1 ... an) 
    where a1 ... an are *distinct type variables*, 
    and each type variable appears at most once in the instance head. 
    Use -XFlexibleInstances if you want to disable this.) 
In the instance declaration for `Show (Tree s s)' 

我看着它,那-XFlexibleInstances電梯到位的限制,以防止模棱兩可的情況下被宣佈。 如何讓兩個類型變量允許模糊的情況呢?

instance Show s => Show (Tree s) where 
    show = showTree 0 

工作的很好,當我只需要一個類型變量。

+0

'FlexibleInstances'與模棱兩可的實例無關。你不能用'FlexibleInstances'單獨編寫一個不明確的實例。類型類實例必須具有由[Haskell 98](http://www.haskell.org/onlinereport/decls.html#undecidable-delete.html)指定的非常具體的形式(即錯誤告訴你必須具有的形式)實例)報告。 FlexibleInstances刪除了這個需求,因此使你的代碼不符合Haskell 98標準。 – user2407038 2014-09-28 08:25:14

回答

4

對不起,我沒有想到它。

其他人是否有這個問題,它需要2個不同類型的變量將被提供,以允許2個不同的顯示-能夠類型:

instance (Show sk, Show sv) => Show (Tree sk sv) where 
    show = showTree 0 

然後包含的任何函數(在這種情況下showTree),需要一個類似的簽名。