Distinct types in a list

Hi all, I have a family of parsers that return either (Success t) or (Fail), using the following data type:
data ParserResult a = Success a | Fail String deriving (Read, Show, Eq, Ord)
isSuccess (Success _) = True isSuccess (Fail _) = False ...
I want to add the results of different parsers to a list. Such as:
m1 = parseFirstModel file1 -- it returns a ParserResult of t1 m2 = parseSecondModel file2 -- it returns a ParserResult of t2
ps = [m1, m2]
In such a way that I could write something like:
if and (map isSuccess ps) then process m1 m2 else ...
Actually, in the real program I have to check more than two input models. However, since Lists do only hold elements of a same type, I couldn't proceed in this way. Which improvements to the ParserResult data type should I wrote in order to proceed as I want to. Best regards, Rodrigo.

If I understand you correctly, what you want is very similar to catMaybes
isSuccess' (Success a) = Just a
isSuccess' _ = Nothing
result = catMaybes $ map isSuccess ps
This should do the trick.
2010/1/7 rodrigo.bonifacio
Hi all,
I have a family of parsers that return either (Success t) or (Fail), using the following data type:
data ParserResult a = Success a | Fail String deriving (Read, Show, Eq, Ord)
isSuccess (Success _) = True isSuccess (Fail _) = False ...
I want to add the results of different parsers to a list. Such as:
m1 = parseFirstModel file1 -- it returns a ParserResult of t1 m2 = parseSecondModel file2 -- it returns a ParserResult of t2
ps = [m1, m2]
In such a way that I could write something like:
if and (map isSuccess ps) then process m1 m2 else ...
Actually, in the real program I have to check more than two input models. However, since Lists do only hold elements of a same type, I couldn't proceed in this way. Which improvements to the ParserResult data type should I wrote in order to proceed as I want to.
Best regards,
Rodrigo.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ozgur Akgun

Hi all,
I have a family of parsers that return either (Success t) or (Fail), using
В сообщении от Четверг 07 января 2010 21:35:10 rodrigo.bonifacio написал: the following data type:
data ParserResult a = Success a | Fail String deriving (Read, Show, Eq, Ord)
isSuccess (Success _) = True isSuccess (Fail _) = False ...
I want to add the results of different parsers to a list. Such as:
m1 = parseFirstModel file1 -- it returns a ParserResult of t1 m2 = parseSecondModel file2 -- it returns a ParserResult of t2
ps = [m1, m2]
In such a way that I could write something like:
if and (map isSuccess ps) then process m1 m2 else ...
Actually, in the real program I have to check more than two input models. However, since Lists do only hold elements of a same type, I couldn't proceed in this way. Which improvements to the ParserResult data type should I wrote in order to proceed as I want to.
In short there is no easy way to this lists. But you can make ParserResult instance of Monad and/or Applicative. It's is error monad in fact. You can check Control.Monad.Error to see Monad instance for (Either String) With this you can write you program in applicative notation Here computation will stop after first error.
m1 = parseFirstModel file1 -- it returns a ParserResult of t1 m2 = parseSecondModel file2 -- it returns a ParserResult of t2
case process <$> m1 <*> m2 of (Success x) -> ... (Fail x) -> ...
Or use do notation with similar results
res = do m1 <- parseFirstModel file1 m2 <- parseSecondModel file2 return $ process m1 m2
foo = case res of (Success x) -> ... (Fail err) -> ...
P.S. I didn't run code but I think it should work if required instances are defined

You could cast your parser result "a" to Dynamic using Data.Dynamic.toDyn (and derive Typeable instances for all involved types). http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Data-Dyna... Using an existential types may be another alternative. Cheers Christian rodrigo.bonifacio schrieb:
Hi all,
I have a family of parsers that return either (Success t) or (Fail), using the following data type:
data ParserResult a = Success a | Fail String deriving (Read, Show, Eq, Ord)
isSuccess (Success _) = True isSuccess (Fail _) = False ...
I want to add the results of different parsers to a list. Such as:
m1 = parseFirstModel file1 -- it returns a ParserResult of t1 m2 = parseSecondModel file2 -- it returns a ParserResult of t2
ps = [m1, m2]
In such a way that I could write something like:
if and (map isSuccess ps) then process m1 m2 else ...
Actually, in the real program I have to check more than two input models. However, since Lists do only hold elements of a same type, I couldn't proceed in this way. Which improvements to the ParserResult data type should I wrote in order to proceed as I want to.
Best regards,
Rodrigo.

Hello, My impression is that using existential types where possible
will result in more complete type checking than Data.Dynamic but I'm
not sure since I haven't yet tried Data.Dynamic in my own code. Can
someone confirm if this is right?
Best
Keith
On Thu, Jan 7, 2010 at 2:02 PM, Christian Maeder
You could cast your parser result "a" to Dynamic using Data.Dynamic.toDyn (and derive Typeable instances for all involved types).
http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Data-Dyna...
Using an existential types may be another alternative.
Cheers Christian
rodrigo.bonifacio schrieb:
Hi all,
I have a family of parsers that return either (Success t) or (Fail), using the following data type:
data ParserResult a = Success a | Fail String deriving (Read, Show, Eq, Ord)
isSuccess (Success _) = True isSuccess (Fail _) = False ...
I want to add the results of different parsers to a list. Such as:
m1 = parseFirstModel file1 -- it returns a ParserResult of t1 m2 = parseSecondModel file2 -- it returns a ParserResult of t2
ps = [m1, m2]
In such a way that I could write something like:
if and (map isSuccess ps) then process m1 m2 else ...
Actually, in the real program I have to check more than two input models. However, since Lists do only hold elements of a same type, I couldn't proceed in this way. Which improvements to the ParserResult data type should I wrote in order to proceed as I want to.
Best regards,
Rodrigo.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- keithsheppard.name

Hello Keith, Thursday, January 7, 2010, 11:26:51 PM, you wrote: existential types has more limited usage compared to dynamics, but in the cases they work they allow to have compile-time type-checking instead of run-time one
Hello, My impression is that using existential types where possible will result in more complete type checking than Data.Dynamic but I'm not sure since I haven't yet tried Data.Dynamic in my own code. Can someone confirm if this is right?
Best Keith
On Thu, Jan 7, 2010 at 2:02 PM, Christian Maeder
wrote: You could cast your parser result "a" to Dynamic using Data.Dynamic.toDyn (and derive Typeable instances for all involved types).
http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Data-Dyna...
Using an existential types may be another alternative.
Cheers Christian
rodrigo.bonifacio schrieb:
Hi all,
I have a family of parsers that return either (Success t) or (Fail), using the following data type:
data ParserResult a = Success a | Fail String deriving (Read, Show, Eq, Ord)
isSuccess (Success _) = True isSuccess (Fail _) = False ...
I want to add the results of different parsers to a list. Such as:
m1 = parseFirstModel file1 -- it returns a ParserResult of t1 m2 = parseSecondModel file2 -- it returns a ParserResult of t2
ps = [m1, m2]
In such a way that I could write something like:
if and (map isSuccess ps) then process m1 m2 else ...
Actually, in the real program I have to check more than two input models. However, since Lists do only hold elements of a same type, I couldn't proceed in this way. Which improvements to the ParserResult data type should I wrote in order to proceed as I want to.
Best regards,
Rodrigo.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (6)
-
Bulat Ziganshin
-
Christian Maeder
-
Keith Sheppard
-
Khudyakov Alexey
-
Ozgur Akgun
-
rodrigo.bonifacio