Skip to content

Observing all in MonadLogic #32

@treeowl

Description

@treeowl

For any MonadLogic instance, we can write

gather :: MonadLogic m => m a -> m [a]
gather m = msplit m >>= \case
  Nothing -> pure []
  Just (a, m') -> (a:) <$> gather m'

However, this is inefficient for LogicT when producing many results, due to the recursive use of msplit. For LogicT, we can simply write

gather = lift . observeAllT

This can also be implemented easily to ReaderT. I haven't looked into other transformers because I didn't need them for my work.

One option would be to add gather as a LogicT method. The fact that it always accumulates a list doesn't seem ideal, however. Is there some nice way to generalize it that would allow both lazy and strict accumulation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions