[Haskell-cafe] Haskell from SML - referrential Transparency?!

Gregory Guthrie guthrie at mum.edu
Tue Apr 19 21:10:09 CEST 2011


I am pretty new to Haskell, so need some clarification.
I am porting some code from SML, and getting a result that surprises me.

I basically have some functions which work like this:
    f1 =  fa fb fc
    test1 = do print "test1:"
                         f1

But I ran a few tests, and got odd results - so I ran the same  test function twice, and got different results - that was my surprise.
I did this:
    f1 =  fa fb fc
    f2 =  fa fb fc
    test2 = do print "test1:"
                         f1
                         f2

and I get different results from the two executions (f1,f2), even though they have exactly the same definition. Reversing their order, gives the exact same results (i.e. the results are still different, and in the same original order as f2;f1). Even doing   (f1;f1) gives two different results.

Seems to me that by referential transparency, I should always get the same result from the function(s).

So, I added some Debug.trace to the argument functions which are used, and I get a trace from the first call(s), but none from the second one(s), although I do get the result from each.

It is as if because of the laziness, it someone cached some of the intermediate results, so did not re-invoke the functions.

Anyway, totally confused. I must be missing something significant here. Thanks for any clarification!
(The original code is a bit long, so I did not include here...)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110419/38172867/attachment.htm>


More information about the Haskell-Cafe mailing list