
I am reading SPJ's seminal work "Implementing lazy functional languages on stock hardware: the Spinless Tagless G-machine" (1992). The paper is available here http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.53.3729 On p62 we see "The expression scrutinised by a case expression must eventually evaluate either to a primitive value or a constructor application.". This doesn't make sense to me. Isn't the following a valid STG program? What am I missing? Where is it specified that the scrutinee of a case expression cannot be of a function type? {x} \n {z} -> let f = \{x} \n {y} -> g x y in case f of f' -> f' z Thanks, Tom

Hi Tom, SPJ is surely more qualified to answer than I, but I'll take a stab. In general, it is computationally infeasible to compare functions. Granted, in your example, the function isn't being compared-- and therefore the case expr is extraneous. I don't think there exists a feasible, uncontrived example, so I imagine that's why STG forbids it (though I don't know where it's specified). Cheers, Will
On Jun 18, 2014, at 9:43 AM, Tom Ellis
wrote: I am reading SPJ's seminal work "Implementing lazy functional languages on stock hardware: the Spinless Tagless G-machine" (1992). The paper is available here
http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.53.3729
On p62 we see "The expression scrutinised by a case expression must eventually evaluate either to a primitive value or a constructor application.".
This doesn't make sense to me. Isn't the following a valid STG program? What am I missing? Where is it specified that the scrutinee of a case expression cannot be of a function type?
{x} \n {z} -> let f = \{x} \n {y} -> g x y in case f of f' -> f' z
Thanks,
Tom _______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

I've forgotten what I intended in the STG paper, but GHC's Core language certainly allows case on a function; all it does is to force the function to head normal form.
Simon
| -----Original Message-----
| From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of William
| Knop
| Sent: 18 June 2014 23:54
| To: ghc-devs@haskell.org
| Subject: Re: Case expressions in STG
|
| Hi Tom,
|
| SPJ is surely more qualified to answer than I, but I'll take a stab.
|
| In general, it is computationally infeasible to compare functions.
| Granted, in your example, the function isn't being compared-- and
| therefore the case expr is extraneous. I don't think there exists a
| feasible, uncontrived example, so I imagine that's why STG forbids it
| (though I don't know where it's specified).
|
| Cheers,
| Will
|
|
| > On Jun 18, 2014, at 9:43 AM, Tom Ellis

Whoops, looks like my phone dropped Tom from the CC (fixed). Simon, what about the following: f = \x -> x g = \x -> (x,1) h = \x -> fst (g x) i = \x -> case f of f -> True _ -> False i f => True i h => ? If g isn't inlined into h and fst optimized out, wouldn't the head normal form of f and h be different, and the comparison fail even though it shouldn't? Or should it? I've taken function equality to be "f and g are equal iff f x == g x, for all x." Will
On Jun 18, 2014, at 7:04 PM, Simon Peyton Jones
wrote: I've forgotten what I intended in the STG paper, but GHC's Core language certainly allows case on a function; all it does is to force the function to head normal form.
Simon
| -----Original Message----- | From: ghc-devs [mailto:ghc-devs-bounces@haskell.org] On Behalf Of William | Knop | Sent: 18 June 2014 23:54 | To: ghc-devs@haskell.org | Subject: Re: Case expressions in STG | | Hi Tom, | | SPJ is surely more qualified to answer than I, but I'll take a stab. | | In general, it is computationally infeasible to compare functions. | Granted, in your example, the function isn't being compared-- and | therefore the case expr is extraneous. I don't think there exists a | feasible, uncontrived example, so I imagine that's why STG forbids it | (though I don't know where it's specified). | | Cheers, | Will | | | > On Jun 18, 2014, at 9:43 AM, Tom Ellis
wrote: | > | > I am reading SPJ's seminal work "Implementing lazy functional languages | on | > stock hardware: the Spinless Tagless G-machine" (1992). The paper is | > available here | > | > http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.53.3729 | > | > On p62 we see "The expression scrutinised by a case expression must | > eventually evaluate either to a primitive value or a constructor | > application.". | > | > This doesn't make sense to me. Isn't the following a valid STG | program? | > What am I missing? Where is it specified that the scrutinee of a case | > expression cannot be of a function type? | > | > {x} \n {z} -> let f = \{x} \n {y} -> g x y | > in case f of f' -> f' z | > | > Thanks, | > | > Tom | > _______________________________________________ | > ghc-devs mailing list | > ghc-devs@haskell.org | > http://www.haskell.org/mailman/listinfo/ghc-devs | _______________________________________________ | ghc-devs mailing list | ghc-devs@haskell.org | http://www.haskell.org/mailman/listinfo/ghc-devs

On Wed, Jun 18, 2014 at 07:40:26PM -0400, William Knop wrote:
f = \x -> x g = \x -> (x,1) h = \x -> fst (g x) i = \x -> case f of f -> True _ -> False
i f => True i h => ?
If g isn't inlined into h and fst optimized out, wouldn't the head normal form of f and h be different, and the comparison fail even though it shouldn't? Or should it? I've taken function equality to be "f and g are equal iff f x == g x, for all x."
You mean 'i = \x -> case x of' ... Anyway, I'm not suggesting using case to *compare* functions, simply force their thunk before proceeding with a single default alternative. Tom

Ah, yes, that's what I meant. I see your point about your example, now. Will
On Jun 19, 2014, at 3:27 AM, Tom Ellis
wrote: On Wed, Jun 18, 2014 at 07:40:26PM -0400, William Knop wrote: f = \x -> x g = \x -> (x,1) h = \x -> fst (g x) i = \x -> case f of f -> True _ -> False
i f => True i h => ?
If g isn't inlined into h and fst optimized out, wouldn't the head normal form of f and h be different, and the comparison fail even though it shouldn't? Or should it? I've taken function equality to be "f and g are equal iff f x == g x, for all x."
You mean 'i = \x -> case x of' ...
Anyway, I'm not suggesting using case to *compare* functions, simply force their thunk before proceeding with a single default alternative.
Tom _______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://www.haskell.org/mailman/listinfo/ghc-devs

| i = \x -> case f of
| f -> True
| _ -> False
This case expression always succeeds with the first pattern. The second is entirely overlapped. Variable patterns always match.
Simon
| -----Original Message-----
| From: William Knop [mailto:william.knop.nospam@gmail.com]
| Sent: 19 June 2014 00:40
| To: Simon Peyton Jones
| Cc: ghc-devs@haskell.org; tom-lists-ghc-devs@jaguarpaw.co.uk
| Subject: Re: Case expressions in STG
|
| Whoops, looks like my phone dropped Tom from the CC (fixed).
|
| Simon, what about the following:
|
| f = \x -> x
| g = \x -> (x,1)
| h = \x -> fst (g x)
| i = \x -> case f of
| f -> True
| _ -> False
|
| i f => True
| i h => ?
|
| If g isn't inlined into h and fst optimized out, wouldn't the head
| normal form of f and h be different, and the comparison fail even though
| it shouldn't? Or should it? I've taken function equality to be "f and g
| are equal iff f x == g x, for all x."
|
| Will
|
|
| > On Jun 18, 2014, at 7:04 PM, Simon Peyton Jones
|

On Wed, Jun 18, 2014 at 11:04:22PM +0000, Simon Peyton Jones wrote:
I've forgotten what I intended in the STG paper, but GHC's Core language certainly allows case on a function; all it does is to force the function to head normal form.
Right, that's what I imagined. I will have a look for some more up-to-date references to see how this works in modern GHC. Tom
participants (3)
-
Simon Peyton Jones
-
Tom Ellis
-
William Knop