Annotation for unfolding wanted

Stefan O'Rear stefanor at cox.net
Wed Aug 1 03:56:04 EDT 2007


On Wed, Aug 01, 2007 at 09:42:54AM +0200, Georg Martius wrote:
> Hi,
> 
> I am sorry for using the wrong terminology here. Let me ask again:
> Does it sound reasonable to extend the compiler with a pragma that specifies 
> that a certain function should be compiled to a loop? And if the compiler can 
> not do it, it helps with some error message. 

Unfortunately, that's still almost useless in the presence of laziness.
Take the following (contrived) example:

fib :: Int -> (Int,Int) -> (Int,Int)
fib 0 p = p
fib k p = fib (k-1) (snd p, fst p + snd p)

That compiles to a perfectly reasonable loop:  (minor code rearrangement
and bounds-checks deleted for clarity)

   X_zdszdwfib_info:
        Hp = Hp + 16;
        _sjD = I32[Sp + 8];
        if (_sjD == 0) goto clk;
        _sjF = _sjD - 1;
        I32[Hp - 12] = sjC_info;
        I32[Hp - 4] = I32[Sp + 4];
        I32[Hp] = I32[Sp];
        I32[Sp + 8] = _sjF;
        I32[Sp + 4] = I32[Sp];
        I32[Sp] = Hp - 12;
        jump X_zdszdwfib_info ();
    clk:
        I32[Hp - 12] = base_DataziTuple_Z2T_con_info;
        I32[Hp - 8] = I32[Sp + 4];
        I32[Hp - 4] = I32[Sp];
        R1 = Hp - 12;
        Sp = Sp + 12;
        Hp = Hp - 4;
        jump I32[Sp] ();

Yet still:

Prelude X> fib 10000000 (1,1)
(*** Exception: stack overflow

Since while *your function* may be a loop, the thunks it builds are
evaluated in a way that isn't.

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20070801/626fa7c8/attachment.bin


More information about the Glasgow-haskell-users mailing list