Tue, 26 Sep 2000 13:29:39 +0200 (MET DST), John Hughes <[EMAIL PROTECTED]> pisze:

> Of course, in principle, an optimizer *could* replace x==x by
> x`seq`True (if x is known to be of base type), and the x`seq` might
> well be removed by later transformations (if x can be shown to be
> defined, something compilers do analyses to discover). Who knows,
> maybe this happens in the innards of ghc...

It gets optimized for Bool. It does not get optimized for Int, because
(==) on Int is defined in terms of a primitive operation (==#) and
the compiler does not know that x ==# x is always True.

But the programmer can teach it :-)

{-# RULES
"x == x" forall (x :: Int). (==) x x = x `seq` True
    #-}

Well, it's probably better to tell this on a lower level, because
some other operations might use (==#) directly instead of through (==):

import PrelGHC
{-# RULES
"x ==# x" forall (x :: Int#). (==#) x x = True
    #-}

Here it is not necessary to use seq because Int# values are always
evaluated.

Now a function like

f:: Int -> Int
f x = if x == x then x else 10

compiles to exactly the same code as id.

-- 
 __("<  Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK


Reply via email to