[Haskell-cafe] Building pattern and trying monads

Yves Parès yves.pares at gmail.com
Mon May 28 11:28:22 CEST 2012


> observe $ flip runStateT 10 $ (put 0 >> mzero) <|> modify (+3)
>   ((),13)

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:

case flip runStateT 10 $ (put 0 >> mzero) <|> modify (+3) of
    Just x -> ....
    Nothing -> ....

2012/5/27 Roman Cheplyaka <roma at ro-che.info>

> * L Corbijn <aspergesoepje at gmail.com> [2012-05-27 14:21:39+0200]
> > The solution I've in mind depends on the stack being pure. When the
> > monad stack is pure a rule can be applied, returning a maybe value (or
> > having a MaybeT wrapper) and when returning Nothing (failed rule)
> > reverting the stack to it's point before applying the rule.
> >
> > As I'm not quite sure about the design (nor good at software design)
> > I've some questions about this approach.
> > 1. Is there a better approach then using a state monad for building
> > the 'products'?
>
> If you need to interleave building your "products" and analyzing them,
> State seems a reasonable choice here.
>
> > 2. My solution with saving/reverting monad-stacks seems quite a
> > hassle/hack, so is it a good approach or is there something better?
>
> You can use a backtracking monad here ([] or Logic).
>
> The key thing is to put the backtracking monad in the bottom of your
> stack (everything above it will be restored on mzero). On the other
> hand, if you want some "global" effects that should not be restored, you
> can put corresponding monads below LogicT in the stack.
>
> Example:
>
>    > observe $ flip runStateT 10 $ (put 0 >> mzero) <|> modify (+3)
>    ((),13)
>
> Note that "put 0" had no effect here, because it was followed by mzero.
>
> --
> Roman I. Cheplyaka :: http://ro-che.info/
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120528/5fa3625f/attachment.htm>


More information about the Haskell-Cafe mailing list