Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

haskell - Understanding the different behavior of thunks when GHCi let bindings are involved

I've been playing with some examples from Simon Marlow's book about parallel and concurrent programming in Haskell and stumbled across an interesting behavior that I don't really understand. This is really about me trying to understand some of the inner workings of GHC.

Let's say I do the following in the REPL:

λ? let x = 1 + 2 :: Int
λ? let z = (x,x)
λ? :sprint x
x = _
λ? :sprint z
z = (_,_)
λ? seq x ()
()
λ? :sprint z
z = (3,3)

Ok, this is pretty much what I expected except that z gets evaluated to WHNF already. Let's write a similar program and put it in a file:

module Thunk where

import Debug.Trace

x :: Int
x = trace "add" $ 1 + 2

z :: (Int,Int)
z = (x,x)

And fiddle around with it in GHCi:

λ? :sprint x
x = _
λ? :sprint z
z = _
λ? seq x ()
add
()
λ? :sprint z
z = _
λ? seq z ()
()
λ? z
(3,3)

So this behaves a little different: z is not evaluated to WHNF in advance. My question is:

Why is z evaluated to WHNF in the REPL when doing let z = (x,x) but not when loading the definition from a file. My suspicion is that it has something to do with pattern binding but I don't know where to look that up for clarification (maybe I'm just completely utterly wrong). I would have expected it to somehow behave like the example in the file.

Any pointers or a brief explanation why this happens?

question from:https://stackoverflow.com/questions/24755672/understanding-the-different-behavior-of-thunks-when-ghci-let-bindings-are-involv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because (,) is a constructor, the difference makes no difference to Haskell's semantics (:sprint gives access to internal thunk implementation details so doesn't count.) So this is a question of which optimizations and trade-offs GHC does when compiling (x,x) in different positions. Someone else may know the precise reason in these cases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...