The dreaded M-R

Cale Gibbard cgibbard at gmail.com
Sat Jan 28 18:30:50 EST 2006


On 28/01/06, Taral <taralx at gmail.com> wrote:
> On 1/28/06, Cale Gibbard <cgibbard at gmail.com> wrote:
> > Do you have an example of such a program handy?
>
> b = (x, x) where { x :: Num a => a; x = fromInteger 1 }
>
> fromInteger is called twice.
>

--- mr.hs ---
{-# OPTIONS_GHC -fno-monomorphism-restriction #-}

import Debug.Trace

b :: Num a => (a,a)
b = (x,x)
    where x :: Num a => a
          x = (trace "x" . fromInteger) 1

main = print b
------------
cale at zaphod[~]$ ghci -fno-monomorphism-restriction mr.hs
Loading package base-1.0 ... linking ... done.
Compiling Main             ( mr.hs, interpreted )
Ok, modules loaded: Main.
*Main> :t b
b :: (Num a) => (a, a)
*Main> b
(x
1,1)
------------
cale at zaphod[~]$ ghc -fno-monomorphism-restriction -o mr mr.hs && ./mr
x
(1,1)
-----------

If x isn't being shared, then Debug.Trace at least seems incapable of
resolving that fact.

Let's try profiling:

--- mr.hs, revised -----
{-# OPTIONS_GHC -fno-monomorphism-restriction #-}

b :: Num a => (a,a)
b = (x,x)
    where x :: Num a => a
          x = {-# SCC "x" #-} fromInteger 1

main = print b
--------------
cale at zaphod[~]$ ghc -fno-monomorphism-restriction -prof -auto-all -o
mr mr.hs && ./mr +RTS -p
(1,1)
cale at zaphod[~]$ cat mr.prof
        Sat Jan 28 18:21 2006 Time and Allocation Profiling Report  (Final)

           mr +RTS -p -RTS

        total time  =        0.00 secs   (0 ticks @ 20 ms)
        total alloc =      17,972 bytes  (excludes profiling overheads)

COST CENTRE                    MODULE               %time %alloc

CAF                            GHC.Handle             0.0   48.2
CAF                            System.IO              0.0    1.4
CAF                            Main                   0.0   50.3


                                                                      
                        individual    inherited
COST CENTRE              MODULE                                       
       no.    entries  %time %alloc   %time %alloc

MAIN                     MAIN                                         
         1           0   0.0    0.0     0.0  100.0
 CAF                     Main                                         
       150           6   0.0   50.3     0.0   50.5
  b                      Main                                         
       157           1   0.0    0.1     0.0    0.2
   x                     Main                                         
       158           1   0.0    0.1     0.0    0.1
  main                   Main                                         
       156           1   0.0    0.0     0.0    0.0
 CAF                     System.IO                                    
       105           1   0.0    1.4     0.0    1.4
 CAF                     GHC.Handle                                   
       103           3   0.0   48.2     0.0   48.2

-------

One entry to x. So where is this lack of sharing I keep hearing about?
Even if I repeat these tests with -fno-cse, the results are the same.

 - Cale


More information about the Haskell-prime mailing list