How would you make a monad instance of this coroutine?

xiaolingxiao

I am rolling a Coroutine package for education purposes, here it is:

data Step a b r 
  = Stop
  | Yield b r      
  | Await (a -> r) 

instance Functor (Step a b) where
   fmap g s = case s of 
   Stop       -> Stop
   Yield b r  -> Yield b $ g r
   Await f    -> Await (g . f) 

data CoroutineT m a b = CoT { resume :: m (Step a b (CoroutineT m a b)) }

run_ :: Monad m => CoroutineT m a b -> [a] -> m [b]
run_ (CoT m) as = m >>= \step -> case step of
     Stop      -> return []
     Yield o r -> liftM (o:) $ run_ r as 
     Await k   -> case is of 
       []     -> return [] 
       (x:xs) -> run_ (k x) xs

instance Monad m => Functor (CoroutineT m a) where
  fmap g (CoT m) = CoT $ liftM ap m where 
    ap Stop        = Stop
    ap (Yield b r) = Yield (g b) (fmap g r)
    ap (Await k)   = Await $ (fmap g) . k


instance Monad m => Monad (CoroutineT m a) where
   return b      = CoT . return . Yield b $ return b    
   (CoT m) >>= g = CoT $ liftM go m where 
        go Stop        = Stop
        go (Yield b r) = undefined      -- * This line I am having trouble with
        go (Await k)   = Await $ (>>=g) . k

As you could see in the comments above, the only line I am having issue with the Yield case, I can see that

(>>=)     :: CoroutineT m a b -> (b -> CoroutineT m a c) -> CoroutineT m a c
(g b)     :: CoroutineT m a c
r         :: CoroutineT m a b
(r >>= g) :: CoroutineT m a c

But I am not sure of

  1. How to put them together so it type checks
  2. What the semantics of bind is in the case of Yield
xiaolingxiao

Per Gabriel's suggestion, the alternate implementation.

data Step a b x r 
  = Done x 
  | Yield b r
  | Await (a -> r)
  | Fail

instance Functor (Step a b x) where
    fmap g s = case s of 
        Done x    -> Done x
        Yield b r -> Yield b (g r)
        Await k   -> Await $ g . k
        Fail      -> Fail


-- | note the monad type needs to parameterize over type of `x` in `Done x`
data CoroutineT a b m x = CoT { resume :: m (Step a b x (CoroutineT a b m x)) }

instance Monad m => Functor (CoroutineT a b m) where
  fmap g (CoT m) = CoT $ liftM ap m where 
    ap (Done x)    = Done $ g x
    ap (Yield b r) = Yield b (fmap g r) 
    ap (Await k)   = Await $ (fmap g) . k
    ap Fail        = Fail


instance Monad m => Monad (CoroutineT a b m) where
  return = CoT . return . Done
  (CoT m) >>= g = CoT $ m >>= \step -> case step of 
    Done x    -> resume $ g x
    Yield b r -> return . Yield b $ r >>= g
    Await k   -> return . Await $ (>>=g) . k   
    Fail      -> return Fail

Note the implementation of return makes more sense now as well.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How would you express this in Haskell?

分類Dev

I would like my logo to make one full rotation as you scroll to the bottom of the page. How?

分類Dev

how would you make it so that it grabs a random number and uses it to grab objects from an array

分類Dev

Selenium @FindBy - when would you use How?

分類Dev

How would you lerp the positions of two matrices?

分類Dev

How would you explain this disassembly listing?

分類Dev

How would you characterize the term "this" in java?

分類Dev

How would I make a Bukkit webserver?

分類Dev

How to make Koen Claessen's concurrency monad with pure variables?

分類Dev

How would you define a pool of goroutines to be executed at once?

分類Dev

In Python, how would you check if a number is one of the integer types?

分類Dev

How would you iterate this 2D linked list?

分類Dev

How would you compare a dictionary containing unhashable ojects?

分類Dev

RESTservice, resource with two different outputs - how would you do it?

分類Dev

How do you convert an instance of generic T into a concrete instance in Rust?

分類Dev

How would I make a content within and the image to be responsive

分類Dev

How would I make a general case accessor for an object in js?

分類Dev

how would i make this work with nokogiri xml builder

分類Dev

How to make reverse URL to current instance of application?

分類Dev

How to properly exec() a coroutine

分類Dev

How do you make an intro screen for a game

分類Dev

How do you make an automatic "slideshow" of text?

分類Dev

How to zoom a monad transformer?

分類Dev

In JavaScript, how can you access a property of a superclass' instance?

分類Dev

How do you find the source location for an instance variable?

分類Dev

How do you check the class of a Python csv.writer instance?

分類Dev

How would you resolve "error: method does not override or implement a method from a supertype"?

分類Dev

How would you hand a member of a class implementing an interface to another class using the interface?

分類Dev

How would you hand a member of a class implementing an interface to another class using the interface?

Related 関連記事

  1. 1

    How would you express this in Haskell?

  2. 2

    I would like my logo to make one full rotation as you scroll to the bottom of the page. How?

  3. 3

    how would you make it so that it grabs a random number and uses it to grab objects from an array

  4. 4

    Selenium @FindBy - when would you use How?

  5. 5

    How would you lerp the positions of two matrices?

  6. 6

    How would you explain this disassembly listing?

  7. 7

    How would you characterize the term "this" in java?

  8. 8

    How would I make a Bukkit webserver?

  9. 9

    How to make Koen Claessen's concurrency monad with pure variables?

  10. 10

    How would you define a pool of goroutines to be executed at once?

  11. 11

    In Python, how would you check if a number is one of the integer types?

  12. 12

    How would you iterate this 2D linked list?

  13. 13

    How would you compare a dictionary containing unhashable ojects?

  14. 14

    RESTservice, resource with two different outputs - how would you do it?

  15. 15

    How do you convert an instance of generic T into a concrete instance in Rust?

  16. 16

    How would I make a content within and the image to be responsive

  17. 17

    How would I make a general case accessor for an object in js?

  18. 18

    how would i make this work with nokogiri xml builder

  19. 19

    How to make reverse URL to current instance of application?

  20. 20

    How to properly exec() a coroutine

  21. 21

    How do you make an intro screen for a game

  22. 22

    How do you make an automatic "slideshow" of text?

  23. 23

    How to zoom a monad transformer?

  24. 24

    In JavaScript, how can you access a property of a superclass' instance?

  25. 25

    How do you find the source location for an instance variable?

  26. 26

    How do you check the class of a Python csv.writer instance?

  27. 27

    How would you resolve "error: method does not override or implement a method from a supertype"?

  28. 28

    How would you hand a member of a class implementing an interface to another class using the interface?

  29. 29

    How would you hand a member of a class implementing an interface to another class using the interface?

ホットタグ

アーカイブ