简体   繁体   English

Haskell 中运算符的中缀属性

[英]infix properties of operators in Haskell

I've done a Haskell library allowing to manipulate multivariate polynomials.我已经完成了一个Haskell 库,允许操纵多元多项式。 It uses some instances defined by the numeric-prelude library, eg an additive group instance.它使用由numeric-prelude库定义的一些实例,例如加法组实例。 The addition for this instance is denoted + in this library.此实例的添加在此库中表示为+ I find this a bit annoying because there's already the usual + for numbers, so I defined:我觉得这有点烦人,因为数字已经有了常用的+ ,所以我定义了:

import Algebra.Additive as AlgAdd

(^+^) :: Polynomial a -> Polynomial a -> Polynomial a
(^+^) p q = p AlgAdd.+ q

But it seems to me that by doing that, the "infix properties" of + are lost.但在我看来,这样做会丢失+的“中缀属性”。 I never dealt with the infix properties, and I have a couple of questions.我从来没有处理过中缀属性,我有几个问题。 I want to keep the same infix properties.我想保留相同的中缀属性。

  1. Looking at the code I see this line:查看代码,我看到了这一行:

     infixl 6 +, -

This line is isolated, at the beginning of the code.此行是孤立的,位于代码的开头。 So should I similarly include the line那么我是否应该同样包含该行

infixl 6 ^+^, ^-^

at the beginning of my code?在我的代码的开头? In this blog post the author defines the infix property of a function just before defining this function.这篇博文中,作者在定义 function 之前定义了 function 的中缀属性。 Is it another, equivalent way to proceed?这是另一种等效的继续方式吗? And is it not possible to do something like (^+^) = (AlgAdd.+) in such a way that the infix properties are automatically copied?是否不可能以自动复制中缀属性的方式执行(^+^) = (AlgAdd.+)之类的操作?

  1. Still looking at the code, I see:仍在查看代码,我看到:

     {-# MINIMAL zero, (+), ((-) | negate) #-}

What does that mean?这意味着什么?

  1. I'm also wondering how to define the opposite of a polynomial.我也想知道如何定义多项式的反面。 I have the substraction p ^-^ q but how to define ^-^ p ?我有减法p ^-^ q但如何定义^-^ p

  2. Finally, when the package author defines the instance eg for Double , he/she writes:最后,当 package 作者为Double定义实例时,他/她写道:

     instance C Double where {-# INLINE zero #-} {-# INLINE negate #-} {-# INLINE (+) #-} {-# INLINE (-) #-} zero = P.fromInteger 0 negate = P.negate (+) = (P.+) (-) = (P.-)

What is the purpose of INLINE ? INLINE的目的是什么?

  1. You can write the infix declaration above the definition, but it's not a requirement.您可以在定义上方编写中缀声明,但这不是必需的。

     infixl 6 ^+^, ^-^ (^+^):: Polynomial a -> Polynomial a -> Polynomial a (^+^) = (AlgAdd.+)

    The infix properties cannot be copied.不能复制中缀属性。

  2. The Minimal pragma refers to the "minimal set of methods that must be defined" for a class definition to be total. Minimal pragma指的是“必须定义的最小方法集”,以使 class 定义成为总定义。 In your case it means you can either write a definition for the methods " zero and (+) and (-) " or " zero and (+) and negate " since you can define negate in terms of the first group在您的情况下,这意味着您可以为方法“ zero and (+) and (-) ”或“ zero and (+) and negate ”编写定义,因为您可以根据第一组定义negate

     negate a = zero - a

    and (-) in terms of the second.(-)就第二个而言。 So it doesn't matter which one you define.所以你定义哪一个并不重要。

     n - m = n + negate m
  3. Not sure.没有把握。

  4. The Inline pragma forces ghc to inline (unfold) the definition. Inline pragma强制 ghc 内联(展开)定义。 Here is the answer to Is there ever a reason to not mark a monadic bind operator (>>=) as inline?这是对是否有理由不将一元绑定运算符(>>=)标记为内联的答案?

Without a fixity declaration, your operators ^+^ and ^-^ will behave as if declared with如果没有固定性声明,您的运营商^+^^-^将表现得就像声明了

infixl 9 ^+^, ^-^

that is, left-associative with extremely high precedence.也就是说,具有极高优先级的左关联。 If you want them to behave more or less like "regular" addition and subtraction, then you should explicitly declare them to have precedence level 6 with如果您希望它们的行为或多或少像“常规”加法和减法,那么您应该明确声明它们具有优先级 6

infixl 6 ^+^, ^-^

The associativity and precedence levels are not "transferable" from another operator;关联性和优先级不能从另一个运算符“转移”; you'll just have to look up the behavior of the operator you want to be similar to and declare yours appropriately.您只需要查找您想要与之相似的操作员的行为并适当地声明您的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM