Learning Haskell and I am not sure why I don't get the expected result, given these definitions:
instance Ring Integer where
addId = 0
addInv = negate
mulId = 1
add = (+)
mul = (*)
class Ring a where
addId :: a -- additive identity
addInv :: a -> a -- additive inverse
mulId :: a -- multiplicative identity
add :: a -> a -> a -- addition
mul :: a -> a -> a -- multiplication
I wrote this function
squashMul :: (Ring a) => RingExpr a -> RingExpr a -> RingExpr a
squashMul x y
| (Lit mulId) <- x = y
| (Lit mulId) <- y = x
squashMul x y = Mul x y
However:
*HW05> squashMul (Lit 5) (Lit 1)
Lit 1
If I write one version specifically for Integer:
squashMulInt :: RingExpr Integer -> RingExpr Integer -> RingExpr Integer
squashMulInt x y
| (Lit 1) <- x = y
| (Lit 1) <- y = x
squashMulInt x y = Mul x y
Then I get the expected result.
Why does (Lit mulId) <- x
match even when x is not (Lit 1) ?
Variables used in pattern matching are considered to be local variables. Consider this definition for computing the length of a list:
Variables
x
andxs
are local variables to this definition. In particular, if we add a definition for a top-level variable, as inthis does not affect the meaning for
len
. More in detail, the first pattern(x:xs)
is not equivalent to(10:xs)
. If it were interpreted in that way, we would now havelen [5,6] == 0
, breaking the previous code! Fortunately, the semantics of pattern matching is robust to such new declarations asx=10
.Your code
actually means
which is wrong, since
w
can be arbitrary. What you want is probably:(The
Eq a
constraint may depend on the definition ofRingExpr
, which was not posted)You can also simplify everything to:
or even to:
This version does not even use pattern guards, since there's no need to.