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

GetInspectorT #71

Merged
merged 4 commits into from
May 31, 2019
Merged

GetInspectorT #71

merged 4 commits into from
May 31, 2019

Conversation

isovector
Copy link
Member

Some of the ideas in #69

@isovector
Copy link
Member Author

Look at the following program:

{-# LANGUAGE TemplateHaskell #-}

import Control.Monad
import Polysemy
import Polysemy.State
import Polysemy.Error
import Polysemy.Internal.Tactics


doCB :: IO String -> IO ()
doCB m = m >>= putStrLn


data Callback m a where
  Callback :: m String -> Callback m ()

makeSem ''Callback


runCallback
    :: Member (Lift IO) r
    => (forall x. Sem r x -> IO x)
    -> Sem (Callback ': r) a
    -> Sem r a
runCallback lower = interpretH $ \case
  Callback cb -> do
    cb' <- runT cb
    ev <- runEvacuator <$> getEvacuatorT
    sendM $ doCB $ do
      v <- lower .@ runCallback $ cb'
      pure $ maybe ":(" id $ ev v
    getInitialStateT


main :: IO ()
main = do
  void . (runM .@ runCallback)
       . runState False
       $ do
    sendM $ putStrLn "hello world"
    callback $ show <$> get @Bool
    modify not
    callback $ show <$> get @Bool

  putStrLn "\n"

  void . (runM .@ runCallback)
       . runError @()
       $ do
    sendM $ putStrLn "hello world"
    callback $ throw ()

which prints

hello world
False
True


hello world
:(

@isovector
Copy link
Member Author

@jkachmar this would let you do your retry thing

@isovector
Copy link
Member Author

I don't see any reason not to merge this. The fancier stuff in #69 can be done in some sort of polysemy-2 release.

TIME TO BIKESHED:

What's a better name for this function: f a -> Maybe a, where f is some existential bastard?

@jhenahan
Copy link
Contributor

jhenahan commented May 30, 2019

I might bikeshed hush to rightMay or rightMaybe or eitherMaybe. All those names come up in Hoogle, but hush is the least obvious unless you go find the type signature.

As for f a -> Maybe a, the closest analog I can find is something like gcopoint (generic copoint), but that's not exactly informative. That said, taking a lesson from Control.Functor.Copointed's extract :: f a -> a , maybe you could call it extractMay or extractMaybe.

EDIT: Also, do you sleep? 😆

@NickSeagull
Copy link

+1 to extractMaybe. In the end editors autocomplete the function names, so having long names that state what they do is awesome 😄

@isovector
Copy link
Member Author

I'm keen on neither hush nor extractMaybe, since both have existing intuitions in the ecosystem, which don't apply here:

  • hush: from the errors package. A natural transformation Either e ~> Maybe, which silences the errors. I agree that the type looks similar here, but the concern is the difference in intentions. We don't want to silence the errors, we just want to get our hands on the insides!
  • extractMaybe: Extracting is the right idea; but the name is already taken by comonads.

Something else to keep in mind is that the thing we're naming isn't pure. Its' type is more this:

  :: Sem (WithTactics e f m r) (forall x. f x -> Maybe x)

@jhenahan
Copy link
Contributor

Non-serious: extractMaybe -> extractics. 😆

From the docs

-- The @f@ type here is existential and corresponds to "whatever
-- state the other effects want to keep track of." @f@ is always
-- a 'Functor'.

Maybe something like fromExternalState or even fromState, though it's not necessarily State, so that could be misleading. fromExternal or fromEnv, maybe?

If you prefer punny names, restate might be cute.

@NickSeagull
Copy link

I like fromExternal and restate 😁

@isovector isovector marked this pull request as ready for review May 31, 2019 03:36
@isovector isovector changed the title Evacuation wip GetInspectorT May 31, 2019
@isovector isovector merged commit 63b1f42 into master May 31, 2019
------------------------------------------------------------------------------
-- | Get a natural transformation capable of potentially inspecting values
-- inside of @f@. Binding the result of 'getInspectorT' produces a function that
-- can sometimes peek inside values returned by 'bindT'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the cases when it cannot inspect the values? Perhaps it'd be nice to document them?

@isovector isovector deleted the evacuation branch June 10, 2019 15:29
infinisil added a commit to infinisil/polysemy that referenced this pull request Jun 11, 2019
The test suite is made for 0.3 and doesn't compile with earlier versions
due to Yo now taking another argument since
polysemy-research#71
isovector pushed a commit that referenced this pull request Jun 11, 2019
The test suite is made for 0.3 and doesn't compile with earlier versions
due to Yo now taking another argument since
#71
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

Successfully merging this pull request may close these issues.

3 participants