Skip to content

Embed? #36

@treeowl

Description

@treeowl

Control.Monad.Morph has a class

class MMonad t where
  embed :: Monad n => (forall a. m a -> t n a) -> t m a -> t n a

We can write a similar function with a stronger constraint:

embedSeq :: (Monad m, Monad n) => (forall a. m a -> SeqT n a) -> SeqT m a -> SeqT n a
embedSeq f s = f (toView s) >>= \r -> case r of
  Empty -> empty
  a :< s' -> pure a <|> embedSeq f s'

Is it lawful? I haven't checked yet, but I wouldn't be surprised.

The documentation doesn't indicate any restrictions on the function passed to embed, but I wonder if it's supposed to be a monad morphism (see Gabriella439/Haskell-MMorph-Library#68). If it is, we might be able to write an instance something like this inscrutable monstrosity:

instance MMonad SeqT where
  embed (f :: forall a. m a -> SeqT n a) (SeqT m0) = SeqT $ fmap go m0
    where
      go :: forall b. m (View m b) -> n (View n b)
      go m = toView (f m) >>= \r -> case r of
        Empty -> pure Empty
        Empty :< s -> toView (step s)
        (x :< q) :< s -> pure $ x :< (embed f q <|> step s)

      step :: forall b. SeqT n (View m b) -> SeqT n b
      step s = s >>= \r -> case r of
        Empty -> empty
        a :< s' -> pure a <|> embed f s'

Lawful? Sensible? No idea whatsoever.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions