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

Oneshot eta-expanding of Sem #285

Open
isovector opened this issue Nov 25, 2019 · 1 comment
Open

Oneshot eta-expanding of Sem #285

isovector opened this issue Nov 25, 2019 · 1 comment
Labels
enhancement New feature or request polish make the libary joyful t use question Further information is requested
Milestone

Comments

@isovector
Copy link
Member

I was reading this https://www.joachim-breitner.de/blog/763-Faster_Winter_5__Eta-Expanding_ReaderT, and it mentions using oneShot to convince GHC to float lambdas out. I suspect this is why the common pattern in polysemy of foo (Sem m) = Sem $ \k -> m $ \u -> blah performs better than the naive foo = runSem $ \u -> blah.

We can probably clean up a lot of this roundabout code by doing the same thing, and putting oneShot into the common combinators.

@KingoftheHomeless
Copy link
Collaborator

KingoftheHomeless commented Nov 25, 2019

Impredicative types complicates implementing an etaSem à la etaReaderT. We have to work through a newtype to use oneShot.
If we want etaSem, we have one of two options: using coerce, or eta-expanding the argument to oneShot:

newtype Trans f g = Trans { runTrans :: forall x. f x -> g x}

etaSem :: forall r a. Sem r a -> Sem r a
etaSem (Sem m) = Sem $ \(k :: forall x. Union r (Sem r) x -> m x) ->
  oneShot
    (coerce (m :: (forall x. Union r (Sem r) x -> m x) -> m a))
    (Trans k)

etaSem' :: Sem r a -> Sem r a
etaSem' (Sem m) = Sem $ \k -> oneShot (\(Trans k') -> m k') (Trans k)

I don't know enough about the simplifier or oneShot to tell if either of these work. etaSem uses coerce, which, if the simplifier treats it as it does unsafeCoerce, causes it not to touch m anyway. etaSem''s eta-expansion on the argument could prevent lambda-floating if oneShot only looks one lambda deep.

@isovector isovector added enhancement New feature or request polish make the libary joyful t use question Further information is requested labels Nov 26, 2019
@KingoftheHomeless KingoftheHomeless mentioned this issue Jan 18, 2020
10 tasks
@TheMatten TheMatten added this to the v2 milestone Jan 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request polish make the libary joyful t use question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants