eta-expansion and cost of extra thunk on ghc

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

eta-expansion and cost of extra thunk on ghc

Ruben Astudillo
Dear list

On a class, we recently discussed the join function for a (State s)
value (we where implementation Monad passing through Functor)

    join :: State s (State s a) -> State s a
    join outstate =
        State $ \s0 ->
            let (innerstate, s1) = runState outstate s0
            in  runState innerstate s1

On this, we eta-expand outstate to in a sense "pattern match" on what we
know about it. We were thinking on the cost of the extra wrapping we do
here for this "pattern matching". Because eta-expanding is one a of the
3 primitive operations on lambda calculus, we concluded it should be
free (modulo presence of seq). Is this true on ghc?

--
-- Ruben
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: eta-expansion and cost of extra thunk on ghc

Jeff Clites
I don't think it's always free; I believe you'll incur the cost at least of allocating the closure to capture outstate in that lambda in the canonical case. But, if you are thinking of the code:

    runState (join x) y

then it's likely that GHC will inline away everything such that it's free, but not so in the case in which you (say) store the result of join in some data structure and run it elsewhere.

So I'd say whether it's free depends on exactly what code you are comparing it to, and I think it's a question of whether GHC has the opportunity to inline everything, not a direct consequence of the similarity to a lambda calculus primitive; that doesn't inherently make anything free.

JEff

> On May 14, 2017, at 10:40 AM, Ruben Astudillo <[hidden email]> wrote:
>
> Dear list
>
> On a class, we recently discussed the join function for a (State s)
> value (we where implementation Monad passing through Functor)
>
>    join :: State s (State s a) -> State s a
>    join outstate =
>        State $ \s0 ->
>            let (innerstate, s1) = runState outstate s0
>            in  runState innerstate s1
>
> On this, we eta-expand outstate to in a sense "pattern match" on what we
> know about it. We were thinking on the cost of the extra wrapping we do
> here for this "pattern matching". Because eta-expanding is one a of the
> 3 primitive operations on lambda calculus, we concluded it should be
> free (modulo presence of seq). Is this true on ghc?
>
> --
> -- Ruben
> _______________________________________________
> 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.
_______________________________________________
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.