gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:12:54 25 4
gpt4 key购买 nike

我正在阅读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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com