
After I load the following into ghci and call main I expect to see "norm" in the output 3 times as I have called it 3 times but it only shows once. Why? traceBug.hs --------------- {-# OPTIONS_GHC -Wall #-} import Debug.Trace (trace) norm :: [Int] -> [Int] norm = trace ("norm") id main :: IO () main = do print $ norm [2] print $ norm [2] print $ norm [3] --------------------- ghci session % ghci traceBug.hs Loaded package environment from /Users/gcolpitts/.ghc/x86_64-darwin-9.8.2/environments/default GHCi, version 9.8.2: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( traceBug.hs, interpreted ) Ok, one module loaded. ghci> main norm [2] [2] [3] ghci>

On Fri, 17 May 2024, George Colpitts wrote:
After I load the following into ghci and call main I expect to see "norm" in the output 3 times as I have called it 3 times but it only shows once. Why?
traceBug.hs --------------- {-# OPTIONS_GHC -Wall #-}
import Debug.Trace (trace)
norm :: [Int] -> [Int] norm = trace ("norm") id
main :: IO () main = do print $ norm [2] print $ norm [2] print $ norm [3] ---------------------
ghci session
% ghci traceBug.hs Loaded package environment from /Users/gcolpitts/.ghc/x86_64-darwin-9.8.2/environments/default GHCi, version 9.8.2: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( traceBug.hs, interpreted ) Ok, one module loaded. ghci> main norm [2] [2] [3]
I think this is correct. 'norm' is evaluated only once, because it is the same variable (for a function) in every call. It would be different, if you would define: norm xs = trace "norm" xs

Thanks Henning! I suspected some kind of optimization but I didn't think ghci would do any. I guess it is just a subtle consequence of eta reduction. It was very confusing to me. Cheers George On Fri, May 17, 2024 at 6:43 PM Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Fri, 17 May 2024, George Colpitts wrote:
After I load the following into ghci and call main I expect to see "norm" in the output 3 times as I have called it 3 times but it only shows once. Why?
traceBug.hs --------------- {-# OPTIONS_GHC -Wall #-}
import Debug.Trace (trace)
norm :: [Int] -> [Int] norm = trace ("norm") id
main :: IO () main = do print $ norm [2] print $ norm [2] print $ norm [3] ---------------------
ghci session
% ghci traceBug.hs Loaded package environment from /Users/gcolpitts/.ghc/x86_64-darwin-9.8.2/environments/default GHCi, version 9.8.2: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( traceBug.hs, interpreted ) Ok, one module loaded. ghci> main norm [2] [2] [3]
I think this is correct. 'norm' is evaluated only once, because it is the same variable (for a function) in every call. It would be different, if you would define:
norm xs = trace "norm" xs

It's a consequence of lazy evaluation. Which means `trace` is very useful
to see when things are actually evaluated, but not useful as a general
output mechanism.
On Fri, May 17, 2024 at 7:27 PM George Colpitts
Thanks Henning! I suspected some kind of optimization but I didn't think ghci would do any. I guess it is just a subtle consequence of eta reduction. It was very confusing to me.
Cheers George
On Fri, May 17, 2024 at 6:43 PM Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Fri, 17 May 2024, George Colpitts wrote:
After I load the following into ghci and call main I expect to see "norm" in the output 3 times as I have called it 3 times but it only shows once. Why?
traceBug.hs --------------- {-# OPTIONS_GHC -Wall #-}
import Debug.Trace (trace)
norm :: [Int] -> [Int] norm = trace ("norm") id
main :: IO () main = do print $ norm [2] print $ norm [2] print $ norm [3] ---------------------
ghci session
% ghci traceBug.hs Loaded package environment from /Users/gcolpitts/.ghc/x86_64-darwin-9.8.2/environments/default GHCi, version 9.8.2: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( traceBug.hs, interpreted ) Ok, one module loaded. ghci> main norm [2] [2] [3]
I think this is correct. 'norm' is evaluated only once, because it is the same variable (for a function) in every call. It would be different, if you would define:
norm xs = trace "norm" xs
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- brandon s allbery kf8nh allbery.b@gmail.com

I don't think optimization plays any role. At the start of the program 'norm' is a thunk which points to the code 'trace "norm" id'. The first time it is evaluated the 'trace' prints "norm" and 'norm' becomes bound to the value 'id'. On Fri, May 17, 2024 at 08:26:44PM -0300, George Colpitts wrote:
Thanks Henning! I suspected some kind of optimization but I didn't think ghci would do any. I guess it is just a subtle consequence of eta reduction. It was very confusing to me.
On Fri, May 17, 2024 at 6:43 PM Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Fri, 17 May 2024, George Colpitts wrote:
After I load the following into ghci and call main I expect to see "norm" in the output 3 times as I have called it 3 times but it only shows once. Why?
traceBug.hs --------------- {-# OPTIONS_GHC -Wall #-}
import Debug.Trace (trace)
norm :: [Int] -> [Int] norm = trace ("norm") id
main :: IO () main = do print $ norm [2] print $ norm [2] print $ norm [3] ---------------------
ghci session
% ghci traceBug.hs Loaded package environment from /Users/gcolpitts/.ghc/x86_64-darwin-9.8.2/environments/default GHCi, version 9.8.2: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( traceBug.hs, interpreted ) Ok, one module loaded. ghci> main norm [2] [2] [3]
I think this is correct. 'norm' is evaluated only once, because it is the same variable (for a function) in every call. It would be different, if you would define:
norm xs = trace "norm" xs

Agreed! Thanks On Sat, May 18, 2024 at 3:43 AM Tom Ellis < tom-lists-haskell-cafe-2023@jaguarpaw.co.uk> wrote:
I don't think optimization plays any role. At the start of the program 'norm' is a thunk which points to the code 'trace "norm" id'. The first time it is evaluated the 'trace' prints "norm" and 'norm' becomes bound to the value 'id'.
Thanks Henning! I suspected some kind of optimization but I didn't think ghci would do any. I guess it is just a subtle consequence of eta reduction. It was very confusing to me.
On Fri, May 17, 2024 at 6:43 PM Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Fri, 17 May 2024, George Colpitts wrote:
After I load the following into ghci and call main I expect to see "norm" in the output 3 times as I have called it 3 times but it only shows once. Why?
traceBug.hs --------------- {-# OPTIONS_GHC -Wall #-}
import Debug.Trace (trace)
norm :: [Int] -> [Int] norm = trace ("norm") id
main :: IO () main = do print $ norm [2] print $ norm [2] print $ norm [3] ---------------------
ghci session
% ghci traceBug.hs Loaded package environment from /Users/gcolpitts/.ghc/x86_64-darwin-9.8.2/environments/default GHCi, version 9.8.2: https://www.haskell.org/ghc/ :? for help [1 of 2] Compiling Main ( traceBug.hs, interpreted ) Ok, one module loaded. ghci> main norm [2] [2] [3]
I think this is correct. 'norm' is evaluated only once, because it is
On Fri, May 17, 2024 at 08:26:44PM -0300, George Colpitts wrote: the
same variable (for a function) in every call. It would be different, if you would define:
norm xs = trace "norm" xs
Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
participants (4)
-
Brandon Allbery
-
George Colpitts
-
Henning Thielemann
-
Tom Ellis