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

Embed? #36

Open
treeowl opened this issue Jul 30, 2021 · 0 comments
Open

Embed? #36

treeowl opened this issue Jul 30, 2021 · 0 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@treeowl
Copy link
Collaborator

treeowl commented Jul 30, 2021

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.

@treeowl treeowl added duplicate This issue or pull request already exists enhancement New feature or request question Further information is requested and removed duplicate This issue or pull request already exists labels Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant