[Haskell-cafe] Why does this blow the stack?

Don Stewart dons at galois.com
Sat Dec 22 15:34:06 EST 2007


dons:
> dbenbenn:
> > On 12/21/07, Stefan O'Rear <stefanor at cox.net> wrote:
> > > Because they simply aren't the same.
> > 
> > Good point; thanks.  That means that Don's patch could theoretically
> > break some existing Haskell program:
> > 
> > Prelude> length $ take 10 ([undefined ..] :: [Integer])
> > 10
> 
> That's right. It makes Integer behave like the Int instance.

And we see again that strictness properties are very ill-defined,
here, the Enum types in base:

    Prelude> length $ take 10 ([undefined ..] :: [Int])
    *** Exception: Prelude.undefined

    Prelude> length $ take 10 ([undefined ..] :: [()])
    *** Exception: Prelude.undefined

    Prelude> length $ take 10 ([undefined ..] :: [Ordering])
    *** Exception: Prelude.undefined

    Prelude> length $ take 10 ([undefined ..] :: [Bool])
    *** Exception: Prelude.undefined

But,

    Prelude> length $ take 10 ([undefined ..] :: [Float])
    10

    Prelude> length $ take 10 ([undefined ..] :: [Double])
    10

And,
    Prelude> length $ take 10 ([undefined ..] :: [Integer])
    10

Now,
    Prelude> length $ take 10 ([undefined ..] :: [Integer])
    *** Exception: Prelude.undefined

So we see that Float and Double also have this problem,

    Prelude> head (drop 10000000 [1 .. ]) :: Float
    *** Exception: stack overflow

    Prelude> head (drop 10000000 [1 .. ]) :: Double
    *** Exception: stack overflow

People shouldn't be writing code that depends on this!

-- Don


More information about the Haskell-Cafe mailing list