> observe $ flip runStateT 10 $ (put 0 >> mzero) <|> modify (+3)<br>
> ((),13)<br><br>If the only thing you need is backtracking, using LogicT might be a little overkill, using Maybe in the bottom of you monad stack suits just fine:<br><br>case flip runStateT 10 $ (put 0 >> mzero) <|> modify (+3) of<br>
Just x -> ....<br> Nothing -> ....<br><br><div class="gmail_quote">2012/5/27 Roman Cheplyaka <span dir="ltr"><<a href="mailto:roma@ro-che.info" target="_blank">roma@ro-che.info</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
* L Corbijn <<a href="mailto:aspergesoepje@gmail.com">aspergesoepje@gmail.com</a>> [2012-05-27 14:21:39+0200]<br>
<div class="im">> The solution I've in mind depends on the stack being pure. When the<br>
> monad stack is pure a rule can be applied, returning a maybe value (or<br>
> having a MaybeT wrapper) and when returning Nothing (failed rule)<br>
> reverting the stack to it's point before applying the rule.<br>
><br>
> As I'm not quite sure about the design (nor good at software design)<br>
> I've some questions about this approach.<br>
> 1. Is there a better approach then using a state monad for building<br>
> the 'products'?<br>
<br>
</div>If you need to interleave building your "products" and analyzing them,<br>
State seems a reasonable choice here.<br>
<div class="im"><br>
> 2. My solution with saving/reverting monad-stacks seems quite a<br>
> hassle/hack, so is it a good approach or is there something better?<br>
<br>
</div>You can use a backtracking monad here ([] or Logic).<br>
<br>
The key thing is to put the backtracking monad in the bottom of your<br>
stack (everything above it will be restored on mzero). On the other<br>
hand, if you want some "global" effects that should not be restored, you<br>
can put corresponding monads below LogicT in the stack.<br>
<br>
Example:<br>
<br>
> observe $ flip runStateT 10 $ (put 0 >> mzero) <|> modify (+3)<br>
((),13)<br>
<br>
Note that "put 0" had no effect here, because it was followed by mzero.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Roman I. Cheplyaka :: <a href="http://ro-che.info/" target="_blank">http://ro-che.info/</a><br>
</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>