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

Instanes for ExceptT? #7

Open
safareli opened this issue May 15, 2018 · 0 comments
Open

Instanes for ExceptT? #7

safareli opened this issue May 15, 2018 · 0 comments

Comments

@safareli
Copy link

I tried to implement some of this classes with ExceptT

MonadFork with Compose (Aff.Fiber eff) (Either MyError) as fiber could be implemented just fine

data MyError = MyCustomError

newtype M eff a = M (ExceptT MyError (Aff.Aff eff) a)

runM :: forall eff a. M eff a -> Aff.Aff eff (Either MyError a)
runM (M (ExceptT x)) = x

derive newtype instance functorM :: Functor (M e)
derive newtype instance applyM :: Apply (M e)
derive newtype instance applicativeM :: Applicative (M e)
derive newtype instance bindM :: Bind (M e)
derive newtype instance monadM :: Monad (M e)
derive newtype instance monadEffM :: MonadEff e (M e)
derive newtype instance monadAffMMonadAff e (M e)

instance monadForkMMonadFork (Compose (Aff.Fiber eff) (Either MyError)) (M eff) where
  suspend x = map Compose $ liftAff $ suspend $ runM x
  fork x = map Compose $ liftAff $ fork $ runM x
  join (Compose f) = M $ ExceptT $ join f

for MonadKill you need MonadThrow, i tried to implement it with Either MyError Aff.Error as error type, but i still have to do kill (Aff.error "kill") as in that case there is no Error around:

instance monadThrowM :: MonadThrow (Either MyError Aff.Error) (M e) where
  throwError (Left e) = M $ ExceptT $ pure $ Left e
  throwError (Right e) = M $ ExceptT $ throwError e

instance monadKillMMonadKill (Either MyError Aff.Error) (Compose (Aff.Fiber eff) (Either MyError)) (M eff) where
  kill (Left e) (Compose f) = M $ ExceptT $ kill (Aff.error "kill") f $> Left e
  kill (Right e) (Compose f) = M $ ExceptT $ map Right $ kill e f

for MonadBracket we need MonadError:

instance monadErrorM :: MonadError (Either MyError Aff.Error) (M e) where
  catchError x f = do
    res <- M $ ExceptT $ map Right $ Aff.try (runM x)
    case res of
      Left e -> f $ Right e
      Right (Left e) -> f $ Left e
      Right (Right a) -> pure a

But I got stack with bracket function of MonadBracket (uninterruptible and never are trivial to implement).

is it possible to implement correct bracket instance for ExceptT at all?

What should you do if you want some error type in your monad other then Error, is there some way which makes it possible to implement MonadBracket but still have custom error type?

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

No branches or pull requests

1 participant