8

I'm trying to better understand the effect/meaning of seq on ->-typed values, or rather what WHNF means for ->-values.

The Haskell Report defines seq as

seq ⊥ b  =  ⊥
seq a b  =  b,  if a ≠ ⊥

The report also notes that due to those definitions above

⊥ is not the same as \x -> ⊥, since seq can be used to distinguish them

If I have the following definitions

f, g :: () -> ()
g = ⊥
f = \x -> g x

Then f should be syntactically equivalent to g (shouldn't it?), but what should the expressions

seq f ()
seq g ()

according to the Haskell Report evaluate to?

hvr
  • 7,775
  • 3
  • 33
  • 47
  • 3
    Re: "Then `f` should be syntactically equivalent to `g`, shouldn't it?": no, they are not syntactically equivalent. In fact, what you did is called η-expansion (the opposite of η-reduction), and deciding whether or not to include η-reduction when designing a language is a somewhat deep semantic question with some surprising and profound consequences. – Daniel Wagner Sep 10 '11 at 14:48
  • @Daniel Wagner: Btw, where does the Haskell Report actually state whether η-conversion is available for reducing expressions? – hvr Sep 10 '11 at 16:32
  • 2
    I don't believe there is a formal semantics of Haskell anywhere. (Surprise!) – Daniel Wagner Sep 10 '11 at 18:22
  • Doesn't it kind of state (or was it my teatcher that said so) that you can choose any reduction scheme you want, as long as it's consistent. But then they are almost different languages. – nulvinge Sep 11 '11 at 09:31
  • 1
    Very similar to a question I asked a couple months back: http://stackoverflow.com/questions/6442019/how-does-seq-force-functions – acfoltzer Sep 12 '11 at 20:12
  • @acfoltzer: interesting question, it is somewhat related but with a different emphasis -- I was interested in what the formal language specification really requires (regardless of any actual implementation of the language) – hvr Sep 13 '11 at 19:23

1 Answers1

4

According to the rules given:

seq f ()
seq (\x -> g x) ()
()

seq g ()
seq ⊥ ()
⊥

This is because (\x -> g x) is a closure, and cannot be evaluated until it's has something to be evaluated on.

An alternative evaluation order:

seq f ()
seq (\x -> g x) ()
seq (\x -> ⊥) ()
()

Still gives the same result.

nulvinge
  • 1,600
  • 8
  • 17
  • How do You justify based on the Haskell Report that `\x -> g x` can't be evaluated to be equal to `⊥` as far as `seq` is concerned? I couldn't find any reference (maybe I just missed it) which lambda-calculus conversions are permissible. – hvr Sep 10 '11 at 16:42
  • If we suppose we can use η-conversion in this instance, we get inconsistent results, thus it's not permissible (or rather, we must always use it in this situation, or never, we cannot choose as is the case everywhere else). – nulvinge Sep 10 '11 at 17:02