Daniel Wagner
Daniel Wagner

Reputation: 153132

Why does this function seem to hide its embedded exception?

While commenting on another question, I discovered an apparent break in referential transparency. In ghci:

> f g h = g `seq` h `seq` \x -> g (h x)
> seq (f undefined id) ()
()
> seq (undefined `seq` id `seq` \x -> undefined (id x)) ()
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
  error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
  undefined, called at <interactive>:3:14 in interactive:Ghci9

Why isn't the first one bottom? (GHC 8.2.2 in case it matters -- I don't have any other versions installed on this machine at the moment.)

Upvotes: 12

Views: 188

Answers (1)

K. A. Buhr
K. A. Buhr

Reputation: 51074

Looks like this is Trac #14002. If you do a:

> :set -fpedantic-bottoms

or define f in a file and load it into GHCi, then both expressions cause an exception.

The bug report claims it's difficult to fix without incurring a performance penalty.

Upvotes: 7

Related Questions