[英]In what cases does NegativeLiterals change behavior?
While describing the NegativeLiterals School of Haskell shows an example of how using the language extension might change the performance of some code and then says 虽然描述NegaskLiterals Haskell学校展示了如何使用语言扩展可能会改变某些代码的性能然后说
Other examples might actually change behavior rather than simply be less efficient
其他示例实际上可能会改变行为,而不是简单地降低效率
After fooling around with the extension a bit I was not able to find any of these instances of behavior changes. 在使用扩展程序后,我无法找到任何这些行为更改实例。 I was only able to find the performance changes they were talking about and a few programs that will error with and without the extension.
我只能找到他们正在谈论的性能变化以及一些在有和没有扩展名时会出错的程序。
What are these programs that change their behavior when the NegativeLiterals extension is enabled? 在启用NegativeLiterals扩展时,这些程序会改变其行为?
The difference of negative literals is the difference of if we negate the integer then call fromInteger
or call fromInteger
then negate the type of the codomain. 负文字的差异是我们否定整数然后调用
fromInteger
或调用fromInteger
然后否定codomain的类型的差异。 That is, fromInteger . negate
也就是说,来自
fromInteger . negate
fromInteger . negate
is not the same as negate . fromInteger
fromInteger . negate
与fromInteger . negate
是不一样的negate . fromInteger
negate . fromInteger
. negate . fromInteger
。 I would expect this to happen when you are on the bounds of some type - one might saturate while another wraps. 我希望当你处于某种类型的界限时会发生这种情况 - 一种可能会在另一种情况下饱和。
For example I have a pretty trivially bad type: 例如,我有一个非常简单的坏类型:
data Bad = Bad Integer
deriving (Show)
instance Num Bad where
negate (Bad a) = Bad (a + 1)
fromInteger = Bad
And the result: 结果如下:
*Main> (-1) :: Bad
Bad 2
*Main> :set -XNegativeLiterals
*Main> (-1) :: Bad
Bad (-1)
*Main>
NegativeLiterals
also changes how some expressions are parsed. NegativeLiterals
还会更改某些表达式的解析方式。 Unary -
is interpreted as an operator with the same precedence as binary -
(ie 6), but with NegativeLiterals
a -
that is directly (without whitespace) followed by a number will be part of the literal. 一元
-
被解释为具有与二元相同的优先级的运算符-
(即6),但是使用NegativeLiterals
a -
直接(没有空格)后面跟一个数字将是文字的一部分。 Here is an example of how this can make a difference: 以下是一个如何产生影响的示例:
>>> :set -XNoNegativeLiterals
>>> -1 `mod` 2
-1
>>> :set -XNegativeLiterals
>>> -1 `mod` 2
1
>>> - 1 `mod` 2
-1
I have got this from this StackOverflow question. 我从这个 StackOverflow问题得到了这个。
Consider a fixed-size type, such as Int8
from Data.Int
. 考虑固定大小的类型,例如
Int8
的Data.Int
。 This is a signed integer stored in 8
bits. 这是一个以
8
位存储的有符号整数。 Its possible values range from -128 to 127. So, with the default syntax, the literal -128
will try to store 128
in an Int8
and then negate it, which results in overflow. 它的可能值范围从-128到127.因此,使用默认语法,文字
-128
将尝试在Int8
存储128
然后否定它,这会导致溢出。 With NegativeLiterals
, it will directly construct -128
as an Int8
, avoiding this possible error. 使用
NegativeLiterals
,它将直接构造-128
作为Int8
,避免这种可能的错误。
As it turns out, on ghci
, the literal 128
procures a warning and then produces -128
as a result of the overflow, so as it happens -128
produces the correct value. 事实证明,在
ghci
,文字128
一个警告,然后由于溢出而产生-128
,因此它发生-128
产生正确的值。 However, I don't believe this behavior is standardized since Int8
is a signed type, so it's best not to rely on overflow behaving in this way. 但是,我不认为这种行为是标准化的,因为
Int8
是一个带符号的类型,因此最好不要依赖于溢出行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.