Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we get inlining with a skolem? #31

Open
treeowl opened this issue Jul 17, 2016 · 5 comments
Open

Can we get inlining with a skolem? #31

treeowl opened this issue Jul 17, 2016 · 5 comments

Comments

@treeowl
Copy link

treeowl commented Jul 17, 2016

Currently, reflection clogs up the inliner pretty badly. The following modification seems to fix this, but there could be some gotcha somewhere.

data Skolem = Skolem
newtype Magic a r = Magic (Reifies Skolem a => Proxy Skolem -> r)

The function passed to reify, of type forall (s :: *). Reifies s a => Proxy s -> r, can certainly be passed to Magic (automatically instantiating s to Skolem).

For example, using this definition,

foo :: Int -> Int
foo x = reify (5 :: Int) $ \(p :: Proxy s) -> reflect p + x

yields

foo :: Int -> Int
foo =
  \ (x_a4WB :: Int) ->
    case ((\ _ -> I# 5) `cast` ...) ((Proxy) `cast` ...)
    of _ { I# x1_a9qY ->
    case x_a4WB of _ { I# y_a9r2 -> I# (+# x1_a9qY y_a9r2) }
    }

The only remaining traces of reflection are the Proxy and the failure to unbox 5. Note that Tagged-based reflection manages to get rid of absolutely all traces even when Proxy-based reflection is built on top of it:

class Reifies s a | s -> a where
  reflect' :: Tagged s a

data Skolem = Skolem

newtype Magic a r = Magic (Reifies Skolem a => Tagged Skolem r)

reify' :: forall a r . a -> (forall (s :: *) . Reifies s a => Tagged s r) -> r
reify' a k = unsafeCoerce (Magic k :: Magic a r) a
{-# INLINE reify' #-}

reflect :: Reifies s a => proxy s -> a
reflect = proxy reflect'
{-# INLINE reflect #-}

reify :: forall a r . a -> (forall (s :: *) . Reifies s a => Proxy s -> r) -> r
reify a k = reify' a (unproxy k)
{-# INLINE reify #-}

With these definitions, foo compiles to

foo :: Int -> Int
foo =
  \ (x_aDk :: Int) ->
    case x_aDk of _ { I# y_a12D -> I# (+# 5 y_a12D) }
@treeowl
Copy link
Author

treeowl commented Jul 17, 2016

Hrmmm.... It's possible that GHC specialization will screw this up. I'll do some more tests.

@treeowl
Copy link
Author

treeowl commented Jul 18, 2016

However, I haven't been able to make it fail yet, at least...

@cartazio
Copy link
Collaborator

Do we have any examples where it also is a perf win according to
bechmarks? That seems like an awfully fragile trick

On Monday, July 18, 2016, David Feuer notifications@github.com wrote:

However, I haven't been able to make it fail yet, at least...


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#31 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAQwu-dHz-altGQbjs90AOyJTXB2Q4sks5qW-mJgaJpZM4JOVcN
.

@cartazio
Copy link
Collaborator

On the other had this is pretty darn cool

On Tuesday, July 19, 2016, Carter Schonwald carter.schonwald@gmail.com
wrote:

Do we have any examples where it also is a perf win according to
bechmarks? That seems like an awfully fragile trick

On Monday, July 18, 2016, David Feuer <notifications@github.com
javascript:_e(%7B%7D,'cvml','notifications@github.com');> wrote:

However, I haven't been able to make it fail yet, at least...


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#31 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAQwu-dHz-altGQbjs90AOyJTXB2Q4sks5qW-mJgaJpZM4JOVcN
.

@ocharles
Copy link
Collaborator

ocharles commented Jul 8, 2019

Are there any more thoughts on this? The Tagged + Skolem approach is the only way I've ever seen reflection not end up in the core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants