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

Declare loses input state #47

Open
dylex opened this issue Apr 28, 2022 · 2 comments
Open

Declare loses input state #47

dylex opened this issue Apr 28, 2022 · 2 comments

Comments

@dylex
Copy link

dylex commented Apr 28, 2022

I would expect this:

> execDeclare (declare "World") "Hello"
"World"

to produce "HelloWorld". To make this more interesting, the initial state is visible during execution, but is lost afterwards:

> runDeclareT (declare "World" >> look) "Hello"
("World","HelloWorld")

Is this intentional? If not, I think the fix is just to have the bind operators include d (mappend (mappend d d') d'' combining the input and output of the second one), and I can make a PR.

@dylex
Copy link
Author

dylex commented Apr 28, 2022

Actually that's not the right fix because it would keep doubling the input...

@maksbotan
Copy link
Collaborator

Looking at the code, you'd get expected answer even with execDeclare (declare "World" >> return ()) "Hello"

That's because declare explicitly drops previous state:

instance (Applicative m, Monad m, Monoid d) => MonadDeclare d (DeclareT d m) where
declare d = DeclareT (\_ -> return (d, ()))
look = DeclareT (\d -> return (mempty, d))

It is then combined between different declare calls in >>=:

DeclareT dx >>= f = DeclareT $ \d -> do
~(d', x) <- dx d
~(d'', y) <- runDeclareT (f x) (mappend d d')
return (mappend d' d'', y)

I'm not sure how to fix this at the moment 🤔

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