Reputation: 15967
I have been trying to parse an XML-datastructure with xml-conduit, which seems to work after a bit of learning. But what I am now stuck on is the following.
Given a list of tag-parsers how do I use choose
to combine them
data SumType = A Text | B Text | C Text
parseSumTypeList :: MonadThrow m => ConduitM Event o m (Maybe SumType)
parseSumTypeList = choose $
[ (tagIgnoreAttrs "A"
$ do result <- content
return (A $ result))
, (tagIgnoreAttrs "B"
$ do result <- content
return (A $ result))
, (tagIgnoreAttrs "C"
$ do result <- content
return (A $ result))]
While the above works fine - if I refactor the common pattern to
parseSumTypeList :: MonadThrow m => ConduitM Event o m (Maybe SumType)
parseSumTypeList = choose $ map f [("A",A),("B",B),("C",C)]
where f (str,constr) = tagIgnoreAttrs str
$ do result <- content
return (constr $ result)
I get the following error
Couldn't match type ‘ConduitM Event o0 m0 (Maybe SumType)’
with ‘forall o1. ConduitM Event o1 m (Maybe SumType)’
Expected type: (Name, Text -> SumType)
-> Consumer Event m (Maybe SumType)
Actual type: (Name, Text -> SumType)
-> ConduitM Event o0 m0 (Maybe SumType)
Relevant bindings include
parseSumType :: Consumer Event m (Maybe SumType)
(bound at ...)
In the first argument of ‘map’, namely ‘f’
In the first argument of ‘choose’, namely
‘(map f [("A", A), ("B", B), ("C", C)])’
It seems to me that the forall o
part gets specialized - and therefore it is not a forall
anymore, but that is just a guess.
from the documentation of xml-conduit
choose :: Monad m
=> [Consumer Event m (Maybe a)] -- List of parsers that will be tried in order.
-> Consumer Event m (Maybe a) -- Result of the first parser to succeed, or Nothing if no parser succeeded
and knowing that Consumer is just a type synonym
type Consumer i m r = forall o. ConduitM i o m r
I tried with f
as a lambda function - which also did not work.
parseSumTypeList = choose $ flip map [("A",A),("B",B),("C",C)]
$\(str,constr) -> tagIgnoreAttrs str
$ do result <- content
return (constr $ result)
ConduitErr.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Rank2Types #-}
module ConduitErr where
import Control.Monad.Trans.Resource (MonadThrow)
import Text.XML.Stream.Parse
import Data.Text (Text)
import Data.XML.Types (Event)
import Data.Conduit (ConduitM)
-- import Control.Lens ((&),(.~))
data SumType = A Text | B Text | C Text
parseSumTypeList :: MonadThrow m => ConduitM Event o m (Maybe SumType)
parseSumTypeList = choose $
[ (tagIgnoreAttrs "A"
$ do result <- content
return (A $ result))
, (tagIgnoreAttrs "B"
$ do result <- content
return (B $ result))
, (tagIgnoreAttrs "C"
$ do result <- content
return (C $ result))]
parseSumTypeList' :: MonadThrow m => ConduitM Event o m (Maybe SumType)
parseSumTypeList' = choose $ map f [("A",A),("B",B),("C",C)]
where f (str,constr) = tagIgnoreAttrs str
$ do result <- content
return (constr $ result)
foo.cabal
[...]
build-depends: base >=4.8 && <4.9
, conduit
, resourcet
, text
, xml-conduit
, xml-types
[...]
Upvotes: 0
Views: 138
Reputation: 52029
You can't use map
, but you can do this:
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad.Trans.Resource
import Data.Conduit
import Data.Text (Text, unpack)
import Data.XML.Types
import Text.XML.Stream.Parse
data SumType = A Text | B Text | C Text
parseList :: MonadThrow m => ConduitM Event o m (Maybe SumType)
parseList = choose [ mkCond "A" A , mkCond "B" B , mkCond "C" C ]
where
mkCond x xc = tagIgnoreAttrs x (content >>= (return . xc))
Upvotes: 1