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

MonadPlus instance for Logic has a bug #6

Closed
dagit opened this issue Jul 20, 2021 · 3 comments
Closed

MonadPlus instance for Logic has a bug #6

dagit opened this issue Jul 20, 2021 · 3 comments

Comments

@dagit
Copy link

dagit commented Jul 20, 2021

I noticed that runIdentity (observeAllT (mzero `mplus` pure 1)) was producing []. I then noticed this happened anytime mzero was the left argument to mplus.

The MonadPlus instance currently looks like this:

instance Monad m => MonadPlus (ML m) where
  mzero = ML empty
  mplus (toView -> m) n = fromView $ m >>= return . \case
       Nothing    -> Nothing
       Just (h,t) -> Just (h, cat t n) 
    where cat (ML l) (ML r) = ML $ l .>< r 

However, that first case means that mzero stops the rest of the computation. I changed the code to this:

instance Monad m => MonadPlus (ML m) where
  mzero = ML empty
  mplus (toView -> m) n = fromView $ m >>= \case
      Nothing -> toView n
      Just (h,t) -> return (Just (h, cat t n))
    where cat (ML l) (ML r) = ML $ l .>< r

And that seems to fix the issue. In particular, I have some local test code that compares breadth-first search and iterative deepening depth-first search and they now return equivalent results. And I was also able to confirm it now returns the same results as LogicT from Control.Monad.Logic. So I think the change I made is correct.

@dagit
Copy link
Author

dagit commented Jul 20, 2021

BTW, I uploaded a these ideas to hackage as logict-sequence so that I could use them more easily. I tried to give you and Oleg credit wherever I could. So I hope you don't mind: https://hackage.haskell.org/package/logict-sequence

@atzeus
Copy link
Owner

atzeus commented Oct 4, 2021

Whoops, that is indeed a bug. I'll fix it now.

@atzeus
Copy link
Owner

atzeus commented Oct 4, 2021

Fixed by 44e94ec

@atzeus atzeus closed this as completed Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants