[Haskell-cafe] commutativity

Brent Yorgey byorgey at seas.upenn.edu
Sun Oct 31 12:55:43 EDT 2010


On Sat, Oct 30, 2010 at 11:55:48AM +0100, Patrick Browne wrote:
> Hi,
> Below are two questions on commutative operations in Haskell.
> 
> infixl 5 `com`
> com :: Int -> Int -> Int
> x `com` y  = (x + y)

> commutative com a b = (a `com` b) == (b`com`a)

Note that the first parameter to commutative shadows the previous
definition of com, I don't know if that's what you intended.

> 
> -- 1 + 3 == 3 + 1
> -- This gives true by virtue of the value of LHS and RHS being equal
> after the plus operation
> 
> -- Question 1
> -- commutative com 1 3
> -- This also gives true. Is it because of commutative equation or
> because of the plus operation?

I'm not quite sure I understand your question.  In any case:

commutative com 1 3
  
  = (1 `com` 3) == (3 `com` 1)   {- definition of 'commutative' -}

  = (1 + 3) == (3 + 1)           {- definition of 'com' -}

  = True

> 
> -- Question 2
> -- In Haskell can commutativity be specified as a property of infix
> operations?

No, it can't.

-Brent


More information about the Haskell-Cafe mailing list