Haskell GADT&x27;学生:有没有理由不选择最弱或最强的类型

Haskell GADT&x27;学生:有没有理由不选择最弱或最强的类型,haskell,types,type-inference,gadt,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 whatI

我在看书。第一个练习询问为什么有必要为在幻影类型上操作的函数提供签名。虽然我无法提出一般原因,但我确实在以下示例中看到了一个问题:

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"
现在我知道上面的
whatIs
有两种可能的类型,即:

Expr a -> String

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

• 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)

我想知道为什么编译器不选择这两种类型中的任何一种。

例如,
Expr a->String
是比
Expr Int->String
好得多的类型:只要可以使用
Expr Int->String
,那么
Expr a->String
就一定可以了。但有时没有“最弱”或“最强”类型

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

data SoSimple a where
    SoSimple :: SoSimple Int

eval SoSimple = 3 :: Int
现在,这里有两种非常好的类型可以给出
eval

eval :: SoSimple a -> a
eval :: SoSimple a -> Int
这些类型不能互换!每种方法在不同的情况下都很有用。比较:

{-# 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

因此,这两种类型都不比另一种更通用(事实证明,在仍然允许
eval
本身进行类型检查的情况下,没有一种类型比这两种类型更通用)。由于没有最通用的类型用于
eval
,因此拒绝选择一种类型并强制用户决定这一次他们想要的许多可能类型中的哪一种是有意义的。

任何消歧规则有时都会选择“错误”类型;也就是说,它将选择用户不想要的类型。这个决定是快速失败并告诉用户模糊性,而不是有时做错事,我同意这个决定。根据我的经验,提前失败通常是正确的做法,而不是试图找出真正的意图,然后做出一个潜在的错误选择。是的,我会将其归类为ghc,试图在我试图编译此文件时根据最少意外的原则行事,我得到一个错误,因为返回值对于推断的
Expr t1->t
类型来说太具体了。除了可能的有效类型之间的歧义之外,这似乎是一个问题。如果您添加上面的任何类型,它都应该编译。@Damianadales是的,这使得(对我来说)更加令人困惑的是,
Expr t1->t
就是推断出来的。
{-# 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