GADT:是否有理由不选择最弱或最强的类型

Dam*_*les 3 haskell types type-inference gadt

我正在阅读有趣的幽灵类型.第一个练习询问为什么有必要为在幻影类型上运行的函数提供签名.虽然我无法提出一般原因,但我确实在以下示例中看到了一个问题:

data Expr a where
  I :: Int -> Expr Int
  B :: Bool -> Expr Bool
  Add :: Expr Int -> Expr Int -> Expr Int
  Eq :: (Eq a) => Expr a -> Expr a -> Expr Bool


whatIs (I _) = "an integer expression"
whatIs (Add _ _) = "an adition operation"
Run Code Online (Sandbox Code Playgroud)

现在我明白whatIs上面有两种可能的类型,即:

Expr a -> String
Run Code Online (Sandbox Code Playgroud)

Expr Int -> String
Run Code Online (Sandbox Code Playgroud)

但是,编译器会给出错误:

• Couldn't match expected type ‘t’ with actual type ‘[Char]’
    ‘t’ is untouchable
      inside the constraints: t1 ~ Int
      bound by a pattern with constructor: I :: Int -> Expr Int,
               in an equation for ‘whatIs’
      at ti.hs:9:9-11
  ‘t’ is a rigid type variable bound by
    the inferred type of whatIs :: Expr t1 -> t at ti.hs:9:1
  Possible fix: add a type signature for ‘whatIs’
• In the expression: "someString"
  In an equation for ‘whatIs’: whatIs (I _) = "someString"
• Relevant bindings include
    whatIs :: Expr t1 -> t (bound at ti.hs:9:1)
Run Code Online (Sandbox Code Playgroud)

我想知道为什么编译器不会选择这两者中的任何一个.

Dan*_*ner 8

对于你的例子,Expr a -> String是一个严格更好的类型Expr Int -> String:在任何Expr Int -> String可以使用的地方,Expr a -> String肯定会做.但有时候没有 "最弱"或"最强"的类型.

让我们进一步简化您的示例:

data SoSimple a where
    SoSimple :: SoSimple Int

eval SoSimple = 3 :: Int
Run Code Online (Sandbox Code Playgroud)

现在这里有两个非常好的类型eval:

eval :: SoSimple a -> a
eval :: SoSimple a -> Int
Run Code Online (Sandbox Code Playgroud)

这些类型不可互换!每种都适用于不同的情况.相比:

{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE GADTs #-}
import Data.Void

data SomeSimple where
    SomeSimple :: SoSimple a -> SomeSimple

-- typechecks if eval :: SoSimple a -> Int,
--    but not if eval :: SoSimple a -> a
evalSome :: SomeSimple -> Int
evalSome (SomeSimple x) = eval x

-- typechecks if eval :: SoSimple a -> a,
--    but not if eval :: SoSimple a -> Int
evalNone :: SoSimple Void -> Void
evalNone = eval
Run Code Online (Sandbox Code Playgroud)

所以这些都不比另一个更通用(事实证明,没有类型比两者更通用,同时仍然让eval自己进行类型检查).由于没有最通用的类​​型eval,因此拒绝选择类型并迫使用户决定他们想要的许多可能类型中的哪一种是有意义的.