[Haskell-beginners] Help with expressing polymorphic return type
Marco Yuen
marcoy at gmail.com
Fri Dec 11 15:38:14 UTC 2015
Hi all,
{-# LANGUAGE TypeFamilies #-}
data CB = CB deriving (Show)
data CO = CO deriving (Show)
data CBParseResult = CBParseResult deriving (Show)
data COParseResult = COParseResult deriving (Show)
class ParseResult a where
type EntityType a :: *
toEntity :: a -> EntityType a
instance ParseResult CBParseResult where
type EntityType CBParseResult = CB
toEntity result = CB
instance ParseResult COParseResult where
type EntityType COParseResult = CO
toEntity result = CO
getParseResult :: (ParseResult r) => String -> IO r
getParseResult i = do
if someCond
then return CBParseResult
else return COParseResult
where
someCond = null i
Couldn't match expected type ‘r’ with actual type ‘COParseResult’
‘r’ is a rigid type variable bound by
the type signature for
getParseResult :: ParseResult r => String -> IO r
at Parse.hs:19:19
Relevant bindings include
getParseResult :: String -> IO r (bound at Parse.hs:20:1)
In the first argument of ‘return’, namely ‘COParseResult’
In the expression: return COParseResult
What I want to express is getParseResult to be able to return any instances
of ParseResult type class. But what I understand from the error is that r
has been specialized to CBParseResult, but in the next expression I'm
returning COParseResult. Is my understanding correct? And, How would I
express what I described?
Thanks,
Marco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151211/1cade41d/attachment.html>
More information about the Beginners
mailing list