我有一個遞歸數據定義:遞歸實例定義
data Checked a = forall b. Checked (Either (Warning, Maybe (Checked b), a) a)
我需要定義顯示遞歸:
instance (Show a) => Show (Checked a) where
show (Right v) = show v
show (Left (w, Nothing, v) = show w ++ show v
show (Left (w, Just ch, v) = show w ++ show v ++ "caused by" ++ show ch --recursive here
GHC給
Could not deduce (Show b) arising from a use of `show'
from the context (Show a)
bound by the instance declaration at Checked.hs:29:10-35
Possible fix:
add (Show b) to the context of
the data constructor `Checked'
or the instance declaration
In the second argument of `(++)', namely `show ch'
如果我添加(顯示B)根據實例定義的限制,GHC給出:
Ambiguous constraint `Show b'
At least one of the forall'd type variables mentioned by the constraint
must be reachable from the type after the '=>'
In the instance declaration for `Show (Checked a)'
有沒有下一步我應該採取這個編譯?
請注意,@HaskellElephant已糾正你的代碼(這確實應該在回答中,而不是在你的問題上公佈)。 – AndrewC
@AndrewC我認爲這個錯誤與問題沒有任何關係,所以我糾正了它。我應該恢復它嗎?修復由Sjoerd Visschers解答覆蓋。 – HaskellElephant
@HaskellElephant。我自己幾乎恢復了它,但想讓你有機會將你的編輯作爲答案的一部分。如果你恢復它,在Sjoerd的答案中添加一行以解釋「Checked」應該在那裏。 (目前還不清楚Sjoerd是否看到了原始版本或編輯過的版本)。我認爲在答案中編輯簡單明顯的錯誤是可以的,但如果您不告訴提問者編輯問題,他們可能不會注意到它們將留在不必要的黑暗。如果您還原了,請再次回覆我,以便我可以刪除我的評論。 – AndrewC