case in monadic function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

case in monadic function

Kovacs David
Hello!

I have a function like this: http://rafb.net/p/8E66FI29.html, starting
with evalState. The problem is with checkPredName cause it's return type
is SemanticError what's not a monadic value but case waiting for m a (as
the error message sais), but if I use return to checkPredName then ofc
the pattern match will fail. How can I fix this?

Regards, David
Reply | Threaded
Open this post in threaded view
|

case in monadic function

Thomas Davie

On 23 Jan 2009, at 15:40, Kovacs David wrote:

> Hello!
>
> I have a function like this: http://rafb.net/p/8E66FI29.html,  
> starting with evalState. The problem is with checkPredName cause  
> it's return type is SemanticError what's not a monadic value but  
> case waiting for m a (as the error message sais), but if I use  
> return to checkPredName then ofc the pattern match will fail. How  
> can I fix this?

This looks a lot to me like you need to look at the Error e => Either  
e applicative.

Bob
Reply | Threaded
Open this post in threaded view
|

case in monadic function

Thomas Davie
In reply to this post by Kovacs David

On 23 Jan 2009, at 15:40, Kovacs David wrote:

> Hello!
>
> I have a function like this: http://rafb.net/p/8E66FI29.html,  
> starting with evalState. The problem is with checkPredName cause  
> it's return type is SemanticError what's not a monadic value but  
> case waiting for m a (as the error message sais), but if I use  
> return to checkPredName then ofc the pattern match will fail. How  
> can I fix this?

Oops, sorry, I meant monad, because this monad unfortunately doesn't  
have it's associated Applicative instance (which is much nicer to work  
with).  You can ofc create the standard applicative instance:

instance Error e => Applicative (Either e) where
   pure = return
   (<*>) = ap

Bob
Reply | Threaded
Open this post in threaded view
|

case in monadic function

Daniel Schoepe-3
In reply to this post by Kovacs David
Kovacs David wrote:
> Hello!
>
> I have a function like this: http://rafb.net/p/8E66FI29.html, starting
> with evalState. The problem is with checkPredName cause it's return type
> is SemanticError what's not a monadic value but case waiting for m a (as
> the error message sais), but if I use return to checkPredName then ofc
> the pattern match will fail. How can I fix this?
>
> Regards, David

You can use liftM/fmap to lift a function into a monad:
case (checkPredName pn . snd) `fmap` get of ...
Reply | Threaded
Open this post in threaded view
|

case in monadic function

Daniel Schoepe-3
Daniel Schoepe wrote:

> You can use liftM/fmap to lift a function into a monad:
> case (checkPredName pn . snd) `fmap` get of ...

I'm sorry, I misunderstood the problem, so this suggestion wouldn't work
either. Could you give some more information on what SemanticResult is?

Reply | Threaded
Open this post in threaded view
|

case in monadic function

Kovacs David
Daniel Schoepe wrote:

> Daniel Schoepe wrote:
>
>  
>> You can use liftM/fmap to lift a function into a monad:
>> case (checkPredName pn . snd) `fmap` get of ...
>>    
>
> I'm sorry, I misunderstood the problem, so this suggestion wouldn't work
> either. Could you give some more information on what SemanticResult is?
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>  
It looks like this:
data SemanticResult = Ok | Error String;