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

Provide flipped ‘give’ #36

Open
Icelandjack opened this issue Aug 26, 2017 · 0 comments
Open

Provide flipped ‘give’ #36

Icelandjack opened this issue Aug 26, 2017 · 0 comments

Comments

@Icelandjack
Copy link
Contributor

Icelandjack commented Aug 26, 2017

forgive :: (Given a => result) -> (a -> result)
forgive f a = give a f

I find it useful for this kind of coding style:

type Pure xx a = Given (a -> IO xx)

newtype Managed a where
  Managed :: (forall xx. Pure xx a => IO xx) -> Managed a

implPure :: forall xx a. Pure xx a => a -> IO xx
implPure = given

instance Functor Managed where
  fmap :: forall a a'. (a -> a') -> (Managed a -> Managed a')
  fmap f (Managed ma) = Managed ma' where

    ma' :: forall xx. Pure xx a' => IO xx
    ma' =  ma `forgive` 
     \a -> implPure @xx (f a)

instance Applicative Managed where
  pure :: a -> Managed a
  pure x = Managed (given x)

  (<*>) :: forall a b. Managed (a -> b) -> Managed a -> Managed b
  Managed mf <*> Managed ma = Managed mb where

    mb :: forall zz. Pure zz b => IO zz
    mb = mf @zz `forgive` \f 
      -> ma @zz `forgive` \(a :: a) 
      -> implPure @zz @b (f a)
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

1 participant