17 Jan 2013 11:24
RULES map (\x -> x) = id
Joachim Breitner <mail <at> joachim-breitner.de>
2013-01-17 10:24:14 GMT
2013-01-17 10:24:14 GMT
Hi,
I am experimenting with rewrite rules, but found that they do not fire
as often as I wanted them. Here is a small example:
module MapId where
myMap f [] = []
myMap f (x:xs) = f x : myMap f xs
{-# RULES "map id" myMap id = id #-}
{-# RULES "map id2" myMap (\x -> x) = id #-}
mapId = myMap id
mapIdApp x = myMap id x
mapLamId = myMap (\x -> x)
mapLamIdApp x = myMap (\x -> x) x
This works as expected, i.e. the latter four functions become some kind
of identity. But I am a bit surprised that I do need two rules to catch
all cases, as I thought that the simplifier „looks through“ definitions
like "id". But when I remove the "map id" rule, the functions "mapId"
and "mapIdApp" are not optimized.
So I wonder: Why is rule "map id2" not enough here?
Thanks,
Joachim
(Continue reading)
RSS Feed