简体   繁体   English

NegativeLiterals在什么情况下会改变行为?

[英]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 . negatefromInteger . 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 . 考虑固定大小的类型,例如Int8Data.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.

相关问题 解决方法GHC 7.6缺少NegativeLiterals - Workaround ghc 7.6 lack of NegativeLiterals 如何知道是什么改变了行为? - How to know what made a behavior change? `~`(代字号)在实例上下文中意味着什么,为什么在某些情况下需要解决重叠? - What does `~` (tilde) mean in an instance context, and why is it necessary to resolve overlap in some cases? 为什么bind(>> =)存在? 没有绑定的解决方案难以处理的典型情况是什么? - Why does bind (>>=) exist? What are typical cases where a solution without bind is ugly? 什么是无限数据结构的引人注目的用例? - What are some compelling use cases of infinite data structures? 在线程中调用'yield'有什么好的用例? - What are some good use cases for calling 'yield' in a thread? 如果存在子类型,Scala中存在的用例有哪些? - what are the use cases of existentials in Scala if there is subtyping? GHC发生检查识别哪些情况? - What cases do the GHC occurs check identify? RxScala中FRP的事件和行为的相应概念是什么? - What are the corresponding concepts of Event and Behavior from FRP in RxScala? symdiff是做什么的(在haskell中) - What does symdiff does (in haskell)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM