- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读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/
我正在学习 Alex Aiken 的编译器设计。在研究解析器幻灯片时,Alex 说“正则语言是最弱的形式语言”。 From Youtube video 谁能澄清一下!提前致谢。 最佳答案 他的意思可能
我正在阅读Fun With Phantom Types 。第一个练习询问为什么需要为在幻像类型上运行的函数提供签名。虽然我无法找出一般原因,但我确实在以下示例中发现了一个问题: data Expr a
我是一名优秀的程序员,十分优秀!