
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) -- 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? -- Question 2 -- In Haskell can commutativity be specified as a property of infix operations? This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

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

On 31/10/2010 16:55, Brent Yorgey wrote:
Note that the first parameter to commutative shadows the previous definition of com, I don't know if that's what you intended.
Does the following avoid the shadowing? infixl 5 `op` op :: Int -> Int -> Int x `op` y = (x + y) commutative op1 = \a b -> (a `op` b) == (b `op` a) This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

Yes, that would prevent the shadowing. But now you are ignoring the
argument op1. Choosing a name that is more different from 'op' might
be helpful. You can even invent your own operator as an argument to
your commutative function:
commutative (⊕) = \a b -> (a ⊕ b) == (b ⊕ a)
On Mon, Nov 1, 2010 at 11:15 AM, Patrick Browne
Does the following avoid the shadowing?
infixl 5 `op` op :: Int -> Int -> Int x `op` y = (x + y) commutative op1 = \a b -> (a `op` b) == (b `op` a)

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 10/30/10 06:55 , Patrick Browne wrote:
-- Question 1 -- commutative com 1 3 -- This also gives true. Is it because of commutative equation or because of the plus operation?
Haskell doesn't know about commutativity; you got true because (+) happens to be commutative. There was a discussion about this recently on the list. - -- brandon s. allbery [linux,solaris,freebsd,perl] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.10 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkzO8MMACgkQIn7hlCsL25XC2ACgulNd5A+Gf33lplW3HOmhPLlZ u3EAoMsrbEkMQuU1yR/VaG5XqvNdhS5+ =jMQe -----END PGP SIGNATURE-----
participants (4)
-
Brandon S Allbery KF8NH
-
Brent Yorgey
-
Patrick Browne
-
Roel van Dijk