- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对haskell编译器有时会推断出类型较少的类型感到困惑
比我预期的多态,例如在使用无点定义时。
似乎问题出在“单态限制”上,默认情况下,“单态限制”处于启用状态
较旧版本的编译器。
考虑以下haskell程序:
{-# LANGUAGE MonomorphismRestriction #-}
import Data.List(sortBy)
plus = (+)
plus' x = (+ x)
sort = sortBy compare
main = do
print $ plus' 1.0 2.0
print $ plus 1.0 2.0
print $ sort [3, 1, 2]
ghc
进行编译,则不会获得错误,并且可执行文件的输出为:
3.0
3.0
[1,2,3]
main
正文更改为:
main = do
print $ plus' 1.0 2.0
print $ plus (1 :: Int) 2
print $ sort [3, 1, 2]
3.0
3
[1,2,3]
main = do
print $ plus' 1.0 2.0
print $ plus (1 :: Int) 2
print $ plus 1.0 2.0
print $ sort [3, 1, 2]
test.hs:13:16:
No instance for (Fractional Int) arising from the literal ‘1.0’
In the first argument of ‘plus’, namely ‘1.0’
In the second argument of ‘($)’, namely ‘plus 1.0 2.0’
In a stmt of a 'do' block: print $ plus 1.0 2.0
sort
时,也会发生相同的情况:
main = do
print $ plus' 1.0 2.0
print $ plus 1.0 2.0
print $ sort [3, 1, 2]
print $ sort "cba"
test.hs:14:17:
No instance for (Num Char) arising from the literal ‘3’
In the expression: 3
In the first argument of ‘sort’, namely ‘[3, 1, 2]’
In the second argument of ‘($)’, namely ‘sort [3, 1, 2]’
ghc
突然认为plus
不是多态的,需要Int
自变量?Int
的唯一引用是在plus
的应用程序中,那怎么回事ghc
突然认为sort
需要Num Char
实例? {-# LANGUAGE MonomorphismRestriction #-}
module TestMono where
import Data.List(sortBy)
plus = (+)
plus' x = (+ x)
sort = sortBy compare
TestMono.hs:10:15:
No instance for (Ord a0) arising from a use of ‘compare’
The type variable ‘a0’ is ambiguous
Relevant bindings include
sort :: [a0] -> [a0] (bound at TestMono.hs:10:1)
Note: there are several potential instances:
instance Integral a => Ord (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
instance Ord () -- Defined in ‘GHC.Classes’
instance (Ord a, Ord b) => Ord (a, b) -- Defined in ‘GHC.Classes’
...plus 23 others
In the first argument of ‘sortBy’, namely ‘compare’
In the expression: sortBy compare
In an equation for ‘sort’: sort = sortBy compare
ghc
不能为Ord a => [a] -> [a]
使用多态类型sort
? ghc
对plus
和plus'
的对待不同? plus
应该具有Num a => a -> a -> a
,我真的看不出这有什么不同sort
的类型开始,但只有sort
会引发错误。 sort
的定义,则文件会编译。然而
ghci
中并检查我得到的类型:
*TestMono> :t plus
plus :: Integer -> Integer -> Integer
*TestMono> :t plus'
plus' :: Num a => a -> a -> a
plus
的类型不是多态的?
最佳答案
什么是单态性限制?
Haskell Wiki声明的monomorphism restriction为:
a counter-intuitive rule in Haskell type inference.If you forget to provide a type signature, sometimes this rule will fillthe free type variables with specific types using "type defaulting" rules.
plus :: Num a => a -> a -> a
plus = (+) -- Okay!
-- Runs as:
Prelude> plus 1.0 1
2.0
另外,如果您要定义一个函数,则可以避免
point-free style,
plus x y = x + y
关掉它
MonomorphismRestriction
将启用它(这是默认设置),同时
NoMonomorphismRestriction
将禁用它。
{-# LANGUAGE NoMonomorphismRestriction #-}
如果您正在使用GHCi,则可以使用
:set
命令启用扩展:
Prelude> :set -XNoMonomorphismRestriction
您还可以从命令行告诉
ghc
启用扩展:
ghc ... -XNoMonomorphismRestriction
注意:您真的应该首选第一个选项,而不是通过命令行选项选择扩展名。
plus = (+)
您认为可以用
+
替换每次出现的
plus
。特别是由于
(+) :: Num a => a -> a -> a
,您希望也有
plus :: Num a => a -> a -> a
。
Prelude> let plus = (+)
Prelude> plus 1.0 1
我们得到以下输出:
<interactive>:4:6:
No instance for (Fractional Integer) arising from the literal ‘1.0’
In the first argument of ‘plus’, namely ‘1.0’
In the expression: plus 1.0 1
In an equation for ‘it’: it = plus 1.0 1
您可能需要在较新的GHCi版本中使用
:set -XMonomorphismRestriction
。
plus
的类型不是我们期望的:
Prelude> :t plus
plus :: Integer -> Integer -> Integer
发生的是,编译器看到
plus
的类型为
Num a => a -> a -> a
,这是一种多态类型。
a
来使类型为单态。
Integer
。
ghc
编译上述代码,则不会出现任何错误。
ghci
处理(并且必须处理)交互式定义的方式。
ghci
中输入的每个语句都必须在进行完整的类型检查之前
f1 x = show x
f2 = \x -> show x
f3 :: (Show a) => a -> String
f3 = \x -> show x
f4 = show
f5 :: (Show a) => a -> String
f5 = show
我们希望所有这些函数的行为均相同,并且具有相同的类型,
show
的类型:
Show a => a -> String
。
test.hs:3:12:
No instance for (Show a1) arising from a use of ‘show’
The type variable ‘a1’ is ambiguous
Relevant bindings include
x :: a1 (bound at blah.hs:3:7)
f2 :: a1 -> String (bound at blah.hs:3:1)
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus 24 others
In the expression: show x
In the expression: \ x -> show x
In an equation for ‘f2’: f2 = \ x -> show x
test.hs:8:6:
No instance for (Show a0) arising from a use of ‘show’
The type variable ‘a0’ is ambiguous
Relevant bindings include f4 :: a0 -> String (bound at blah.hs:8:1)
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus 24 others
In the expression: show
In an equation for ‘f4’: f4 = show
因此
f2
和
f4
无法编译。此外,当尝试定义这些功能时
f2
和
f4
的类型是
() -> String
!
f2
和
f4
需要单态性的原因
ghc
和
ghci
之间的行为不同是由于不同
f x = x + 1
请注意,它们的语法为:
<identifier> arg1 arg2 ... argn = expr
模态卫队和
where
声明。但是它们并不重要。
<pattern> = expr
同样,模数卫队。
plus = (+)
是模式绑定(bind)。它将模式
plus
(一个变量)绑定(bind)到表达式
(+)
。
A declaration group is a minimal set of mutually dependent bindings.
a given declaration group is unrestricted if and only if:
every variable in the group is bound by a function binding (e.g.
f x = x
)or a simple pattern binding (e.g.plus = (+)
; Section 4.4.3.2 ), andan explicit type signature is given for every variable in the group thatis bound by simple pattern binding. (e.g.
plus :: Num a => a -> a -> a; plus = (+)
).
(x:xs) = f something
或
(f, g) = ((+), (-))
)或
plus = (+)
)。
The usual Hindley-Milner restriction on polymorphism is that only typevariables that do not occur free in the environment may be generalized.In addition, the constrained type variables of a restricted declarationgroup may not be generalized in the generalization step for that group.(Recall that a type variable is constrained if it must belong to sometype class; see Section 4.5.2 .)
Num a => a -> a -> a
类型是多态的,因为它包含
a
和
a
对其具有约束
Num
。)
plus
函数的可能会更改其类型。
plus = (+)
x :: Integer
x = plus 1 2
y :: Double
y = plus 1.0 2
那么您将得到一个类型错误。因为当编译器看到
plus
是
Integer
声明中的
x
上调用它将统一类型
a
的
Integer
变量,因此
plus
的类型变为:
Integer -> Integer -> Integer
但是然后,当它输入检查
y
的定义时,它将看到
plus
应用于
Double
参数,并且类型不匹配。
plus
而不会出现错误:
plus = (+)
x = plus 1.0 2
在这种情况下,首先将
plus
的类型推断为
Num a => a -> a -> a
但随后将其用于
x
的定义中,其中
1.0
需要
Fractional
约束,将其更改为
Fractional a => a -> a -> a
。
Rule 1 is required for two reasons, both of which are fairly subtle.
Rule 1 prevents computations from being unexpectedly repeated.For example,
genericLength
is a standard function (in libraryData.List
)whose type is given bygenericLength :: Num a => [b] -> a
Now consider the following expression:
let len = genericLength xs
in (len, len)It looks as if
len
should be computed only once, but without Rule 1 itmight be computed twice, once at each of two different overloadings.If the programmer does actually wish the computation to be repeated,an explicit type signature may be added:let len :: Num a => a
len = genericLength xs
in (len, len)
f xs = (len, len)
where
len = genericLength xs
如果
len
是多态的,则
f
的类型为:
f :: Num a, Num b => [c] -> (a, b)
因此,元组
(len, len)
的两个元素实际上可能是
genericLength
完成了计算
f
的类型变为:
f :: Num a => [b] -> (a, a)
这样,无需多次执行计算。
Rule 1 prevents ambiguity. For example, consider the declaration group
[(n,s)] = reads t
Recall that
reads
is a standard function whose type is given by the signaturereads :: (Read a) => String -> [(a,String)]
Without Rule 1,
n
would be assigned the type∀ a. Read a ⇒ a
ands
the type∀ a. Read a ⇒ String
.The latter is an invalid type, because it is inherently ambiguous.It is not possible to determine at what overloading to uses
,nor can this be solved by adding a type signature fors
.Hence, when non-simple pattern bindings are used (Section 4.4.3.2 ),the types inferred are always monomorphic in their constrained type variables,irrespective of whether a type signature is provided.In this case, bothn
ands
are monomorphic ina
.
read
时必须以某种方式告诉编译器
- Any monomorphic type variables that remain when type inference for anentire module is complete, are considered ambiguous, and are resolvedto particular types using the defaulting rules (Section 4.3.4 ).
plus = (+)
这将具有
Num a => a -> a -> a
类型,其中
a
是
a
的类型
plus :: Integer -> Integer -> Integer
。
plus = (+)
x = plus 1.0 2.0
在模块内部,在键入默认值之前,
plus
的类型为:
Fractional a => a -> a -> a
(有关发生这种情况的原因,请参见规则1)。
a
将替换为
Double
所以我们将有
plus :: Double -> Double -> Double
和
x :: Double
。
let x = read "<something>" in show x
这里的表达式是模棱两可的,因为
show
和
read
的类型是:
show :: Show a => a -> String
read :: Read a => String -> a
因此,
x
的类型为
Read a => a
。但是许多类型都满足此约束:
Int
,
Double
或
()
。选择哪一个?没有什么可以告诉我们的。
let x = read "<something>" :: Int in show x
现在的问题是:由于Haskell使用
Num
类型类来处理数字,
show 1
结果应该是什么?
1
具有
Num a => a
类型,可以使用多种类型的数字。
default
声明。通过指定
default (T1, T2, T3)
,我们可以更改
v
是默认的:
v
仅出现在C v
是类的情况下,才出现在C
类型的约束中。Monad (m v)
,则默认为,而不是)。 Num
或Num
的子类。 default
列表中的第一个类型替换
default
声明为
default (Integer, Double)
。
plus = (+)
minus = (-)
x = plus 1.0 1
y = minus 2 1
推断的类型为:
plus :: Fractional a => a -> a -> a
minus :: Num a => a -> a -> a
根据默认规则,该值变为:
plus :: Double -> Double -> Double
minus :: Integer -> Integer -> Integer
请注意,这解释了为什么在问题示例中仅使用
sort
定义引发错误。
Ord a => [a] -> [a]
类型不能为默认值
Ord
不是数字类。
ExtendedDefaultRules
扩展名在文件中启用该功能。
Eq
,
Ord
,
Show
或
Num
及其子类。
default
声明是
default ((), Integer, Double)
。
Prelude> :set -XMonomorphismRestriction
Prelude> import Data.List(sortBy)
Prelude Data.List> let sort = sortBy compare
Prelude Data.List> :t sort
sort :: [()] -> [()]
在ghci中,我们没有收到类型错误,但是
Ord a
约束导致
()
几乎没有用。
关于haskell - 什么是单态性限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49832804/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!