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

First-order effects admit an automatic definition of handle #54

Closed
robrix opened this issue Oct 30, 2018 · 4 comments · Fixed by #170
Closed

First-order effects admit an automatic definition of handle #54

robrix opened this issue Oct 30, 2018 · 4 comments · Fixed by #170
Labels
help wanted Extra attention is needed

Comments

@robrix
Copy link
Collaborator

robrix commented Oct 30, 2018

To wit, handle state handler = coerce . fmap (handler . (<$ state)).

We should use this in the README for Teletype.

@robrix robrix added the help wanted Extra attention is needed label Oct 30, 2018
@mitchellwrosen
Copy link
Contributor

mitchellwrosen commented Mar 31, 2019

This is possible to derive with DerivingVia:

newtype FirstOrderEffect
          (sig :: (Type -> Type) -> Type -> Type)
          (m :: Type -> Type)
          (k :: Type)
  = FirstOrderEffect (sig m k)
  deriving stock Functor

instance
     ( forall m. Functor (sig m)
     , forall m n a. Coercible (sig m a) (sig n a)
     )
  => HFunctor (FirstOrderEffect sig) where

  hmap _ =
    coerce

instance
     ( forall m n a. Coercible (sig m a) (sig n a)
     , forall m. Functor (sig m)
     )
  => Effect (FirstOrderEffect sig) where

  handle state handler =
    coerce . fmap (handler . (<$ state))

@mitchellwrosen
Copy link
Contributor

mitchellwrosen commented Mar 31, 2019

data MyEffect a b c m k = ...
  deriving (HFunctor, Effect) via (FirstOrderEffect (MyEffect a b c))

@mitchellwrosen
Copy link
Contributor

mitchellwrosen commented Mar 31, 2019

Whoops, ignore that, it's just an infinite loop... 😅 Ok, I fixed the original snippet, it works

@robrix
Copy link
Collaborator Author

robrix commented Apr 1, 2019

Nice one! Unfortunately, the dependency on -XDerivingVia and -XQuantifiedConstraints puts it out of reach of some of our supported versions of ghc for a while. (Tho we could always use -XCPP or w/e to do this conditionally in 8.6+.)

Seems like we could also do this with -XDefaultSignatures, tho that would probably require -XQuantifiedConstraints too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants