HXT/Practical/Simple1: Difference between revisions
a simple example |
Use block markup for multiline code |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 42: | Line 42: | ||
First, a quick overview of the necessary imports and compiler declarations: | First, a quick overview of the necessary imports and compiler declarations: | ||
The <code> | The <code>LANGUAGE</code> pragma allows us to specifically enable | ||
and the <code> | certain language extensions. The <code>Arrows</code> option provides the special Arrow syntax, and the <code>NoMonomorphismRestriction</code> option eliminates the need for explicit type signatures on our filters. | ||
the need for explicit type signatures on our filters. | |||
< | <haskell> | ||
{-# LANGUAGE Arrows, NoMonomorphismRestriction #-} | |||
import Text.XML.HXT.Core | |||
</haskell> | |||
</ | |||
The XML will be parsed directly into this data-structure: | The XML will be parsed directly into this data-structure: | ||
< | <haskell> | ||
data Guest = Guest { firstName, lastName :: String } | |||
deriving (Show, Eq) | |||
</haskell> | |||
</ | |||
I find it helpful to get a feel for the combinators at the GHCi | I find it helpful to get a feel for the combinators at the GHCi | ||
Line 66: | Line 61: | ||
the code so far. Then you can use commands like: | the code so far. Then you can use commands like: | ||
< | <haskell> | ||
Main> runX (readDocument [ | Main> runX (readDocument [ withValidate no] "simple1.xml" | ||
>>> deep (isElem >>> hasName "guest")) | >>> deep (isElem >>> hasName "guest")) | ||
</ | </haskell> | ||
to see the XML structures inside the guest tags. The <hask>deep</hask> | to see the XML structures inside the guest tags. The <hask>deep</hask> | ||
filter traverses the XML structures recursively and applies the filter | filter traverses the XML structures recursively and applies the filter | ||
in its parameter to all the underlying structures. <hask> | in its parameter to all the underlying structures. <hask>withValidate no</hask> | ||
turns off validation -- since the XML technically isn't well formed. | turns off validation -- since the XML technically isn't well formed. | ||
Line 81: | Line 76: | ||
be done conveniently. | be done conveniently. | ||
< | <haskell> | ||
getGuest = deep (isElem >>> hasName "guest") >>> | |||
proc x -> do | |||
fname <- getText <<< getChildren <<< deep (hasName "fname") -< x | |||
lname <- getText <<< getChildren <<< deep (hasName "lname") -< x | |||
returnA -< Guest { firstName = fname, lastName = lname } | |||
</haskell> | |||
</ | |||
If you are familiar with monadic do-syntax, you've probably noticed some similarities already. | If you are familiar with monadic do-syntax, you've probably noticed some similarities already. | ||
Line 97: | Line 90: | ||
If you squint a bit you'll notice that there seems to be a bit of an "arrow" feel to the syntax: | If you squint a bit you'll notice that there seems to be a bit of an "arrow" feel to the syntax: | ||
< | <haskell> | ||
... <- ... <<< ... <<< ... -< ... | ... <- ... <<< ... <<< ... -< ... | ||
</ | </haskell> | ||
That's done purposefully, you can think of the XML structures flowing through the combinators | That's done purposefully, you can think of the XML structures flowing through the combinators | ||
Line 107: | Line 100: | ||
Test it out in GHCi: | Test it out in GHCi: | ||
< | <haskell> | ||
Main> runX (readDocument [ | Main> runX (readDocument [withValidate no] "simple1.xml" >>> getGuest) | ||
</ | </haskell> | ||
There is some repetition in the above code. Let's factor it out into useful combinators. | There is some repetition in the above code. Let's factor it out into useful combinators. | ||
< | <haskell> | ||
atTag tag = deep (isElem >>> hasName tag) | |||
text = getChildren >>> getText | |||
</haskell> | |||
</ | |||
And rewrite the example, much cleaner. | And rewrite the example, much cleaner. | ||
< | <haskell> | ||
getGuest2 = atTag "guest" >>> | |||
proc x -> do | |||
fname <- text <<< atTag "fname" -< x | |||
lname <- text <<< atTag "lname" -< x | |||
returnA -< Guest { firstName = fname, lastName = lname } | |||
</haskell> | |||
</ | |||
Hopefully, at this point it should be easy to follow the code, with the more appropriately named | Hopefully, at this point it should be easy to follow the code, with the more appropriately named | ||
functions. | functions. | ||
< | <haskell> | ||
main = do | |||
guests <- runX (readDocument [withValidate no] "simple1.xml" | |||
>>> getGuest2) | |||
print guests | |||
</haskell> | |||
</ |
Latest revision as of 16:11, 11 October 2011
The Data
Save this data to "simple1.xml"
<guestbook> <guest> <fname>John</fname> <lname>Steinbeck</lname> </guest> <guest> <fname>Henry</fname> <lname>Ford</lname> </guest> <guest> <fname>Andrew</fname> <lname>Carnegie</lname> </guest> <guest> <fname>Anton</fname> <lname>Chekhov</lname> </guest> <guest> <fname>George</fname> <lname>Washington</lname> </guest> <guest> <fname>William</fname> <lname>Shakespeare</lname> </guest> <guest> <fname>Nathaniel</fname> <lname>Hawthorne</lname> </guest> </guestbook>
An unlikely list, but it will suffice for our purposes.
The Code
First, a quick overview of the necessary imports and compiler declarations:
The LANGUAGE
pragma allows us to specifically enable
certain language extensions. The Arrows
option provides the special Arrow syntax, and the NoMonomorphismRestriction
option eliminates the need for explicit type signatures on our filters.
{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
import Text.XML.HXT.Core
The XML will be parsed directly into this data-structure:
data Guest = Guest { firstName, lastName :: String }
deriving (Show, Eq)
I find it helpful to get a feel for the combinators at the GHCi prompt. At this point, you may want to start GHCi and load the code so far. Then you can use commands like:
Main> runX (readDocument [ withValidate no] "simple1.xml"
>>> deep (isElem >>> hasName "guest"))
to see the XML structures inside the guest tags. The deep
filter traverses the XML structures recursively and applies the filter
in its parameter to all the underlying structures. withValidate no
turns off validation -- since the XML technically isn't well formed.
With the "guest" structures in hand, the filter can be refined to
pick out the first and last name text and construct a Guest value.
The proc
syntax is introduced here to show how it can
be done conveniently.
getGuest = deep (isElem >>> hasName "guest") >>>
proc x -> do
fname <- getText <<< getChildren <<< deep (hasName "fname") -< x
lname <- getText <<< getChildren <<< deep (hasName "lname") -< x
returnA -< Guest { firstName = fname, lastName = lname }
If you are familiar with monadic do-syntax, you've probably noticed some similarities already.
There is a do
, and there is a <-
. But there are some new operators
too: -<
, returnA
, and this introduced variable x
.
If you squint a bit you'll notice that there seems to be a bit of an "arrow" feel to the syntax:
... <- ... <<< ... <<< ... -< ...
That's done purposefully, you can think of the XML structures flowing through the combinators
in the direction the arrow is pointing. Binding is still done with <-
. returnA
returns the value to the Arrow much like return
does to a monad.
Test it out in GHCi:
Main> runX (readDocument [withValidate no] "simple1.xml" >>> getGuest)
There is some repetition in the above code. Let's factor it out into useful combinators.
atTag tag = deep (isElem >>> hasName tag)
text = getChildren >>> getText
And rewrite the example, much cleaner.
getGuest2 = atTag "guest" >>>
proc x -> do
fname <- text <<< atTag "fname" -< x
lname <- text <<< atTag "lname" -< x
returnA -< Guest { firstName = fname, lastName = lname }
Hopefully, at this point it should be easy to follow the code, with the more appropriately named functions.
main = do
guests <- runX (readDocument [withValidate no] "simple1.xml"
>>> getGuest2)
print guests