Code Monkey home page Code Monkey logo

Comments (10)

bfops avatar bfops commented on August 17, 2024

What features of bracket do you need? If you just need the basic acquire, perform, release, then it should be straightforward monadically. If you need the exception-handling features, then that would involve another effect.

from extensible-effects.

ibotty avatar ibotty commented on August 17, 2024

yes, i wanted the exception handling part. how can that be implemented in extensible-effects? my short tinkering did not yield any results. i don't know how to construct something like liftControlIO :: SetMember Lift (Lift IO) => (Eff r a -> IO a) -> Eff r a which would, i guess, let me implement all the bracket's and mask's i'd like.

can you kindly point me in the right direction?

edit: playing around with it, that does not make any sense. i'll have to capture the monadic state and restore it. i don't feel qualified to adapt monad-control to extensible-effects...

from extensible-effects.

bfops avatar bfops commented on August 17, 2024

This is as far as I got before giving up:

bracket :: (Monad m, Typeable1 m, SetMember Lift (Lift m) r, Typeable e, Member (Exc e) r)
        => Eff r a
        -> (a -> Eff r b)
        -> (a -> Eff r c)
        -> Eff r c
bracket acquire act release = do
    a <- acquire
    e <- catchExc (Nothing <$ act a) (return . Just)
    c <- release a
    () <- maybe (return ()) throwExc e
    return c

The remaining compiler error is:

src/Control/Eff/Exception.hs:116:10:
    Could not deduce (Member (* -> *) * (Exc a0) r)
      arising from a use of `catchExc'
    from the context (Monad m,
                      Typeable1 m,
                      SetMember ((* -> *) -> * -> *) * Lift (Lift m) r,
                      Typeable e,
                      Member (* -> *) * (Exc e) r)
      bound by the type signature for
                 bracket :: (Monad m, Typeable1 m,
                             SetMember ((* -> *) -> * -> *) * Lift (Lift m) r, Typeable e,
                             Member (* -> *) * (Exc e) r) =>
                            Eff r a -> (a -> Eff r b) -> (a -> Eff r c) -> Eff r c
      at src/Control/Eff/Exception.hs:(109,12)-(113,18)
    The type variable `a0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance [overlap ok] Member (* -> *) * t ((:>) k t r)
        -- Defined in `Data.OpenUnion1'
      instance [overlap ok] Member k k1 t r =>
                            Member k * t ((:>) k1 t' r)
        -- Defined in `Data.OpenUnion1'
    Possible fix:
      add an instance declaration for (Member (* -> *) * (Exc a0) r)
    In a stmt of a 'do' block:
      e <- catchExc (Nothing <$ act a) (return . Just)
    In the expression:
      do { a <- acquire;
           e <- catchExc (Nothing <$ act a) (return . Just);
           c <- release a;
           () <- maybe (return ()) throwExc e;
           .... }
    In an equation for `bracket':
        bracket acquire act release
          = do { a <- acquire;
                 e <- catchExc (Nothing <$ act a) (return . Just);
                 c <- release a;
                 .... }

The type of exception is ambiguous, because the type signature doesn't specify it in any way. Looking into the definition of bracket and onException in Control.Exception.Base, I'm still a little confused on how they manage this. I'll think more on this later; it seems like we'll need to do an implementation of something like onException using extensible-effects internals.

from extensible-effects.

ibotty avatar ibotty commented on August 17, 2024

thank you for looking into it.

I know that's not the main problem, but you ordered release and act wrong. it's the other way around.

i'm also not sure, that function will do what i want. i will need a way to catch native io exceptions. for that i guess a way to run a -> Eff r a within an IO context would be needed. i played around extending Lift but that did not work out so far.

Sorry for the incomplete comment earlier, i was on my phone...

from extensible-effects.

suhailshergill avatar suhailshergill commented on August 17, 2024

@ibotty any updates on this?

from extensible-effects.

ibotty avatar ibotty commented on August 17, 2024

unfortunately not. i won't have time this year for it either. sorry.

btw: thanks for giving momentum to extensible-effects.

from extensible-effects.

suhailshergill avatar suhailshergill commented on August 17, 2024

onException in Control.Exception.Base seems to use SomeException to raise exception and the Exception e => constraint (implicitly via catch) to handle them

from extensible-effects.

suhailshergill avatar suhailshergill commented on August 17, 2024

related:

from extensible-effects.

suhailshergill avatar suhailshergill commented on August 17, 2024

@ibotty does the recent addition of catchDynE let you do what you'd originally wanted here? see 60850dd for relevant usage examples

from extensible-effects.

ibotty avatar ibotty commented on August 17, 2024

Sorry, I missed the comment. Yes, that captures (pun intended) my intentions.

from extensible-effects.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.