- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这似乎适用于 GHCi 和 GHC。我将首先展示一个使用 GHCi 的示例。
给定的 i
类型已推断如下:
Prelude> i = 1
Prelude> :t i
i :: Num p => p
鉴于 succ
是在 Enum
上定义的函数:
Prelude> :i Enum
class Enum a where
succ :: a -> a
pred :: a -> a
-- …OMITTED…
并且 Num
不是 Enum
的“子类”(如果我可以使用这个术语的话):
class Num a where
(+) :: a -> a -> a
(-) :: a -> a -> a
-- …OMITTED…
为什么succ i
不返回错误?
Prelude> succ i
2 -- works, no error
我希望 :type i
被推断为:
Prelude> i = 1
Prelude> :type i
i :: (Enum p, Num p) => p
(我正在使用“GHC v. 8.6.3”)
添加:
阅读 @RobinZigmond 评论和 @AlexeyRomanov 答案后,我注意到 1
可以解释为多种类型之一和多种类之一。感谢@AlexeyRomanov 的回答,我对用于决定用于不明确表达式的类型的默认规则有了更多了解。
但是我不认为 Alexey 的回答完全解决了我的问题。我的问题是关于 i 的类型。这与 succ i
的类型无关。
这是关于 succ
参数类型(Enum a
)和 i
的表观类型(Num a
)。
我现在开始意识到我的问题一定源于一个错误的假设:“一旦 i
被推断为 i::Num a => a
,那么 i
可以是没有别的'。因此,我很困惑地看到 succ i
的评估没有错误。
除了显式声明的内容之外,GHC 似乎还推断了 Enum a
。
x :: Num a => a
x = 1
y = succ x -- works
但是,当类型变量作为函数出现时,它不会添加 Enum a
:
my_succ :: Num a => a -> a
my_succ z = succ z -- fails compilation
对我来说,附加到函数的类型约束似乎比应用于变量的类型约束更严格。
GHC 正在说 my_succ::forall a。 Num a => a -> a
并给出forall a
没有出现在 i
和 x
的类型签名中我认为这意味着 GHC 不会再推断任何类对于 my_succ
类型。
但这似乎又是错误的:我已经用以下内容检查了这个想法(我第一次输入RankNTypes),显然GHC仍然推断Enum a
:
{-# LANGUAGE RankNTypes #-}
x :: forall a. Num a => a
x = 1
y = succ x
看来函数的推理规则比变量的推理规则更严格?
最佳答案
是的,succ i
的类型按照您的预期推断出来:
Prelude> :t succ i
succ i :: (Enum a, Num a) => a
该类型不明确,但满足 the defaulting rules 中的条件对于 GHCi:
Find all the unsolved constraints. Then:
- Find those that are of form
(C a)
wherea
is a type variable, and partition those constraints into groups that share a common type variablea
.
在本例中,只有一组:(Enum a, Num a)
.
- Keep only the groups in which at least one of the classes is an interactive class (defined below).
保留该组,因为 Num
是一个互动类(class)。
Now, for each remaining group G, try each type
ty
from the default-type list in turn; if settinga = ty
would allow the constraints in G to be completely solved. If so, defaulta
toty
.The unit type
()
and the list type[]
are added to the start of the standard list of types which are tried when doing type defaulting.
默认的默认类型列表(原文如此)是(带有最后一个子句的添加内容)default ((), [], Integer, Double)
。
所以当你这样做时Prelude> succ i
要实际计算此表达式(注意 :t
不会计算它得到的表达式), a
设置为Integer
(此列表中的第一个满足约束条件),结果打印为 2
.
您可以通过更改默认值来查看原因:
Prelude> default (Double)
Prelude> succ 1
2.0
对于更新的问题:
I'm now starting to realise that my question must stem from a wrong assumption: 'that once
i
is inferred to bei :: Num a => a
, theni
can be nothing else'. Hence I was puzzled to seesucc i
was evaluated without errors.
i
可以是其他(即没有不适合此类型的内容),但它可以与不太通用(更具体)的类型一起使用:Integer
, Int
。即使其中许多同时出现在一个表达式中:
Prelude> (i :: Double) ^ (i :: Integer)
1.0
这些用途不会影响 i
的类型本身:它已经定义并且类型固定。到目前为止还好吗?
嗯,添加约束也会使类型更加具体,所以(Num a, Enum a) => a
比 (Num a) => a
更具体:
Prelude> i :: (Num a, Enum a) => a
1
因为当然任何类型 a
满足 (Num a, Enum a)
中的两个约束满足 Num a
.
However it is not adding
Enum a
when the type variable appears as a function:
那是因为您指定了不允许的签名。如果你不签名,就没有理由推断Num
约束。但例如
Prelude> f x = succ x + 1
将推断具有两个约束的类型:
Prelude> :t f
f :: (Num a, Enum a) => a -> a
So it seems that inference rules for functions are stricter than the ones for variables?
实际上是相反的,因为 monomorphism restriction (默认情况下不在 GHCi 中)。实际上你很幸运没有在这里遇到它,但答案已经足够长了。搜索该术语应该会给您解释。
GHC is saying
my_succ :: forall a. Num a => a -> a
and givenforall a
doesn't appear in the type-signature of neitheri
norx
.
这是一条红鲱鱼。我不确定为什么它显示在一种情况下而不是另一种情况下,但所有情况下都有 forall a
幕后花絮:
Haskell type signatures are implicitly quantified. When the language option
ExplicitForAll
is used, the keywordforall
allows us to say exactly what this means. For example:g :: b -> b
means this:
g :: forall b. (b -> b)
(此外,您只需要
ExplicitForAll
而不是RankNTypes
来写下forall a. Num a => a
。)
关于haskell - 为什么 `succ i` 在 `i::Num a => a` 处有效(而不是 `Enum a` )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308208/
在 Haskell 中,类型声明使用双冒号,即 (::),如 not::Bool -> Bool。 但是在许多语法与 Haskell 类似的语言中,例如榆树、 Agda 、他们使用单个冒号(:)来声明
insertST :: StateDecoder -> SomeState -> Update SomeState SomeThing insertST stDecoder st = ... Stat
如果这个问题有点含糊,请提前道歉。这是一些周末白日梦的结果。 借助 Haskell 出色的类型系统,将数学(尤其是代数)结构表达为类型类是非常令人愉快的。我的意思是,看看 numeric-prelud
我有需要每 5 分钟执行一次的小程序。 目前,我有执行该任务的 shell 脚本,但我想通过 CLI 中的键为用户提供无需其他脚本即可运行它的能力。 实现这一目标的最佳方法是什么? 最佳答案 我想你会
RWH 面世已经有一段时间了(将近 3 年)。在在线跟踪这本书的渐进式写作之后,我渴望获得我的副本(我认为这是写书的最佳方式之一。)在所有相当学术性的论文中,作为一个 haskell 学生,读起来多么
一个经典的编程练习是用 Lisp/Scheme 编写一个 Lisp/Scheme 解释器。可以利用完整语言的力量来为该语言的子集生成解释器。 Haskell 有类似的练习吗?我想使用 Haskell
以下摘自' Learn You a Haskell ' 表示 f 在函数中用作“值的类型”。 这是什么意思?即“值的类型”是什么意思? Int 是“值的类型”,对吗?但是 Maybe 不是“值的类型”
现在我正在尝试创建一个基本函数,用于删除句子中的所有空格或逗号。 stringToIntList :: [Char] -> [Char] stringToIntList inpt = [ a | a
我是 Haskell 的新手,对模式匹配有疑问。这是代码的高度简化版本: data Value = MyBool Bool | MyInt Integer codeDuplicate1 :: Valu
如何解释这个表达式? :t (+) (+3) (*100) 自 和 具有相同的优先级并且是左结合的。我认为这与 ((+) (+3)) (*100) 相同.但是,我不知道它的作用。在 Learn
这怎么行 > (* 30) 4 120 但这不是 > * 30 40 error: parse error on input ‘*’ 最佳答案 (* 30) 是一个 section,它仍然将 * 视为
我想创建一个函数,删除满足第二个参数中给定谓词的第一个元素。像这样: removeFirst "abab" ( 'b') = "abab" removeFirst [1,2,3,4] even =
Context : def fib(n): if n aand returns a memoized version of the same function. The trick is t
我明白惰性求值是什么,它是如何工作的以及它有什么优势,但是你能解释一下 Haskell 中什么是严格求值吗?我似乎找不到太多关于它的信息,因为惰性评估是最著名的。 他们各自的优势是什么。什么时候真正使
digits :: Int -> [Int] digits n = reverse (x) where x | n digits 1234 = [3,1,2,4]
我在 F# 中有以下代码(来自一本书) open System.Collections.Generic type Table = abstract Item : 'T -> 'U with ge
我对 Haskell 比较陌生,过去几周一直在尝试学习它,但一直停留在过滤器和谓词上,我希望能得到帮助以帮助理解。 我遇到了一个问题,我有一个元组列表。每个元组包含一个 (songName, song
我是 haskell 的初学者,我试图为埃拉托色尼筛法定义一个简单的函数,但它说错误: • Couldn't match expected type ‘Bool -> Bool’
我是 Haskell 语言的新手,我在使用 read 函数时遇到了一些问题。准确地说,我的理解是: read "8.2" + 3.8 应该返回 12.0,因为我们希望返回与第二个成员相同的类型。我真正
当我尝试使用真实项目来驱动它来学习 Haskell 时,我遇到了以下定义。我不明白每个参数前面的感叹号是什么意思,我的书上好像也没有提到。 data MidiMessage = MidiMessage
我是一名优秀的程序员,十分优秀!