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

Daniel Fischer daniel.is.fischer at googlemail.com
Tue Apr 19 21:16:16 CEST 2011


On Tuesday 19 April 2011 21:10:09, Gregory Guthrie wrote:
> 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

So f1 :: IO something

Being an IO-action, f1 can return different things in different invocations 
since the world in which it runs has changed (it might read a file which 
was modified between the first and the second invocation, for example).

> 
> 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.

Depending on what f1 does, that may be perfectly normal or a serious bug.
We'd need to see more of the code to determine which.

> 
> 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.

Did you do it in the form

fa = trace ("fa") realFa

?

Then the trace is only evaluated the first time fa is evaluated, even if fa 
is called later again.

> 
> 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...)

http://hpaste.org/

perhaps?



More information about the Haskell-Cafe mailing list