簡體   English   中英

Haskell GADT 和派生秀

[英]Haskell GADT and Deriving Show

我怎樣才能讓Bind數據構造函數的show工作? 前兩個( ReturnPut )似乎工作正常。 由於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)’

我認為您可能混淆了您的類型變量。 aProgram 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM