haskell - GADT 的 : Is there a reason why the weakest or strongest type is not chosen

标签 haskell types type-inference gadt

我正在阅读Fun With Phantom Types 。第一个练习询问为什么需要为在幻像类型上运行的函数提供签名。虽然我无法找出一般原因,但我确实在以下示例中发现了一个问题:

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

Expr Int -> 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 没有最通用的类​​型,拒绝选择类型并强制用户决定这次他们想要的多种可能类型中的哪一种是有意义的。

关于haskell - GADT 的 : Is there a reason why the weakest or strongest type is not chosen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41431216/

相关文章:

c++ - 哪个值更好用? boolean 真还是整数 1?

recursion - 为什么我的 'a list * ' 类型的函数是 a list -> 'b list?

Java泛型不可能赋值?

c++ - 为什么 auto 的推论不同?

haskell - 如何删除reactive-banana中的重复事件

haskell - 有哪些可能的 Haskell 优化键?

go - 是否可以将 Type 存储在 map 中,稍后使用它在 Go lang 中实例化对象?

scala - 更多关于通用 Scala 函数的信息

parsing - 如何正确地脱糖语法树? - 减少数据结构定义/构造函数禁用

c++ - 新分配的先前数据会发生什么变化?