[Haskell-cafe] Re: not possible with monad transformers ?

2004-11-30 Thread Pavel Zolnikov
You are absolutely right! I missed that âletâ.
And it does work, thanks.

On the other hand it is still not what I intended, so I decided
not to use  Monads in this particular case. I wanted to represent
values that can fail and that have complete history of errors.
So that if I calculate

c = (liftM2 (+)) a b

and if *both* a and b have failed, c would also fail plus it
would have history of errors form a and b (and potentially itâs own).
I tried to do  this by combining Maybe and Output monads.
Now I realize that as long as I am using liftM2 it is not possible
to have history of errors from a and b - only form a. And as I moved
to custom lift functions I realized I didnât need monads in the 
first place.

Anyway, thanks for your help, it is very much appreciated.

Regards,
Pavel.

I didnât know you can have âletâ without âinâ in do expressions, nice!


___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: not possible with monad transformers ?

2004-11-30 Thread Pavel Zolnikov
Thanks for response; unfortunately my problem was a bit deeper.
Note to self: try to verify all code snippets before posting ;-)

The problem is that code on line 4 is useless.  
If one of the arguments a or b is Nothing, computation will just return 
Nothing and will not bother to execute lines 4-5:

1   foo a b = do
2  output "before" 
3  let r = liftM2 (+) a b 
4  when (r == Nothing) $ output "error" 
5  return r -- ??? "lift r"

I guess it does make perfect sense. It just was not intuitive to me 
that as soon as I use Maybe monad in my monad transformation I will
loose ability to do output *after* error occurred. 
But, anyway, I think I was able to find a nice solution to that:

type M2 a = OuptutMonadT Maybe String a
whenError:: M2 a -> M2 a -> M2 a â

1   foo a b = do
2  output "before" 
3  let r = liftM2 (+) a b 
4   `whenError` $ reportError "error" 
5  return r

whenError combines two computations so that if first fails it will use
second instead.

Thanks,
Pavel.


___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] not possible with monad transformers ?

2004-11-29 Thread Pavel Zolnikov
Hi, I have been playing with Maybe and Output monad transformers 
and realized that I cannot make the following work nicely:

> foo a b = do
>   output "before"
>   r <- (liftM2(+)) a b
>   when r == Nothing $ output "error"
>   return r

As soon as computation produces Nothing, I am loosing ability to do any 
output. I think what I need is not wrapping one monad into another with 
transformer; I need some sort of monad combiner:

> newtype MC m1 m2 = MC (m1 a, m2 b)

So I could execute both of monads âin parallelâ. But I have no idea 
how to express this or even if this is doable at all. Any comments?

Thanks,
Pavel.

P.S. Here is summary of what I tried with transformers so far:

Letâs say I have following monad transformers:

> newtype MaybeMonadT m a = MMT  (m a)

> newtype OuptutMonadT m o a = OMT (m a, o)

And I am trying to use following monads:

> type M1 a = MaybeMonadT OutputM a
> type M2 a = OuptutMonadT Maybe String a

Now, they both wonât quite work in the following example:

> foo a b = do
>   output "before"
>   r <- (liftM2(+)) a b
>   when r == Nothing $ output "error"
>   return r


In case of M1, as soon as I get Nothing in r, computation will stop 
and return without any output gathered.

In case of M2, as soon as I get Nothing in r, computation will stop 
and return only with output gathered so far. That is 
> output "error" 
will never be called.


___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe