[英]Haskell GADT and Deriving Show
我怎樣才能讓Bind
數據構造函數的show
工作? 前兩個( Return
和Put
)似乎工作正常。 由於Bind
將一個函數作為參數,我認為它需要一些特殊的“處理”嗎?
data Program a where
Put :: Char -> Program ()
Get :: Program (Maybe Char)
Return :: a -> Program a
Bind :: Program a -> (a -> Program b) -> Program b
instance Show a => Show (Program a) where
show (Return a) = show a
show (Put a) = show a
show (Bind pr f) = show pr -- incorrect
• Could not deduce (Show a1) arising from a use of ‘show’
from the context: Show a
bound by the instance declaration at EDSL_Deep1.hs:18:10-35
Possible fix:
add (Show a1) to the context of the data constructor ‘Bind’
• In the expression: show pr
In an equation for ‘show’: show (Bind pr f) = show pr
In the instance declaration for ‘Show (Program a)’
我認為您可能混淆了您的類型變量。 在a
在Program a
沒有任何關系的a
在類型構造函數秒。 它只是用來指定程序的類型。 實際上,使用KindSignatures
您也可以說data Program :: Type -> Type
。 當我們在它的時候,我們也可以在 Bind 中重命名變量,因為我們可以:
data Program :: Type -> Type where
Put :: Char -> Program ()
Get :: Program (Maybe Char)
Return :: a -> Program a
Bind :: Program x -> (x -> Program y) -> Program y
這仍然是相同的定義。 希望這清楚地表明您正在嘗試顯示Program x
只有Show y
,這就是您收到錯誤的原因。 正如評論中所說,我認為這里沒有希望。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.