GADTs上的模式匹配

时间:2013-07-31 23:02:34

标签: haskell typeclass gadt

我为表情创建了一个GADT。当我对具有约束的构造函数进行模式匹配时,类型检查器无法推断出对构造函数约束中使用的类型变量的约束。我认为代码和错误信息更具说明性。

{-# LANGUAGE GADTs, MultiParamTypeClasses #-}
import Data.Word

data Expr a where
  Value :: a -> Expr a
  Cast :: (Castable a b) => Expr a -> Expr b

class Castable a b where
  cast :: a -> b

instance Castable Word64 Word32 where
  cast = fromIntegral

instance (Show a) => Show (Expr a) where
  show (Cast e) = "Cast " ++ show e -- ERROR

我得到的错误:

gadt.hs:16:30:
    Could not deduce (Show a1) arising from a use of `show'
    from the context (Show a)
      bound by the instance declaration at gadt.hs:15:10-34
    or from (Castable a1 a)
      bound by a pattern with constructor
                 Cast :: forall b a. Castable a b => Expr a -> Expr b,
               in an equation for `show'
      at gadt.hs:16:9-14
    Possible fix:
      add (Show a1) to the context of
        the data constructor `Cast'
        or the instance declaration
    In the second argument of `(++)', namely `show e'
    In the expression: "Cast " ++ show e
    In an equation for `show': show (Cast e) = "Cast " ++ show e

编辑:如果我注释掉Show (Expr a)实例并添加以下代码,它可以正常工作:

eval :: Expr a -> a
eval (Value a) = a
eval (Cast e) = cast $ eval e

main = do
  let bigvalue = maxBound `div` 2 + 5 :: Word64
      e = Cast (Value bigvalue) :: Expr Word32
      v = eval e
  putStrLn "typechecks."
  print (bigvalue, v)

我希望show实例基本上打印类似Cast (Value bigvalue)的内容。

1 个答案:

答案 0 :(得分:8)

Cast :: (Castable a b) => Expr a -> Expr b

所以这里:

instance (Show a) => Show (Expr a) where
  show (Cast e) = "Cast " ++ show e -- ERROR

Cast e的类型为Expr a。我们有一个Show a约束,这个实例意味着Show (Expr a),因此我们可以在show类型的内容上调用Expr a

但是e {<1}} Expr a接受任何类型Cast的参数,并为您提供Expr a1(重命名类型变量以与我们在实例中讨论的内容保持一致),Expr a的类型为e。我们Expr a1类型有Show约束,我们要求a1暗示Show a1,因此无法{ {1}}。

并且无法在Show (Expr a1)实例中添加此类约束,因为类型show e未出现在Show类型中。这似乎是在这里使用GADT的重点;你故意删掉有关a1所适用的事物类型的所有信息(除了Cast e持有的事实之外),并简单地宣告结果是Cast