Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  cabal sdist and Setup.hs (Guy)
   2. Re:  cabal sdist and Setup.hs (Daniel Fischer)
   3.  Function definition order and performance (Zhi-Qiang Lei)
   4. Re:  Confused by type constraints (Daniel Schoepe)
   5. Re:  I don't understand mapM in mapM id (Just 1, Nothing,
      Just 3) (Brent Yorgey)
   6.  xml light problem or pebkac (Manfred Lotz)
   7. Re:  xml light problem or pebkac (Daniel Fischer)


----------------------------------------------------------------------

Message: 1
Date: Mon, 6 Jun 2011 12:55:16 +0100 (BST)
From: Guy <[email protected]>
Subject: [Haskell-beginners] cabal sdist and Setup.hs
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

I created a package with Build-Type: Simple, and when I run cabal sdist, I get

Distribution quality errors:

The package is missing a Setup.hs or Setup.lhs script.

Is there some problem with the simple build type?




------------------------------

Message: 2
Date: Mon, 6 Jun 2011 14:17:48 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] cabal sdist and Setup.hs
To: [email protected], Guy <[email protected]>
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="us-ascii"

On Montag, 6. Juni 2011, 13:55, Guy wrote:
> I created a package with Build-Type: Simple, and when I run cabal sdist,
> I get
> 
> Distribution quality errors:
> 
> The package is missing a Setup.hs or Setup.lhs script.
> 
> Is there some problem with the simple build type?

No, but while cabal-install is able to build and install a [build-type: 
simple] package without a Setup.[l]hs, assuming the defaultMain, the 
runhaskell ./Setup.hs configure ... way requires a Setup.[l]hs, so cabal 
sdist insists on the presence of such a file.



------------------------------

Message: 3
Date: Mon, 6 Jun 2011 21:58:19 +0800
From: Zhi-Qiang Lei <[email protected]>
Subject: [Haskell-beginners] Function definition order and performance
To: Haskell Beginer <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi,

If the matching proceeds from top to bottom, will the same functions defined in 
different order have different performance strictly? Thanks.

Quote from Real World Haskell:

"As we have already mentioned, a Haskell implementation checks patterns for 
matches in the order in which we specify them in our equations. Matching 
proceeds from top to bottom, and stops at the first success. Equations below a 
successful match have no effect."

Best regards,
Zhi-Qiang Lei
[email protected]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110606/e3196e8d/attachment-0001.htm>

------------------------------

Message: 4
Date: Mon, 06 Jun 2011 16:31:40 +0200
From: Daniel Schoepe <[email protected]>
Subject: Re: [Haskell-beginners] Confused by type constraints
To: Guy <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Sun, 05 Jun 2011 19:53:50 +0300, Guy <[email protected]> wrote:
> So in my original example, why isn't "instance Foo (Bar f)" destructing Bar - 
> and making the constraint available?

The "Bar" in "instance Foo (Bar f)" is a type constructor, not a data
constructor like the X in "s (X x)". Hence "instance Foo (Bar f)" isn't
really deconstructing anything, but applying the type constructor Bar to
f, whereas "s (X x)" matches a _value_ of type X.

This might be a bit clearer if you name type and data constructors
differently, e.g.:

data Bar f a = Foo f => MkBar { bar :: f a }

Cheers,
Daniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110606/e6eca183/attachment-0001.pgp>

------------------------------

Message: 5
Date: Mon, 6 Jun 2011 11:54:53 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] I don't understand mapM in mapM id
        (Just 1, Nothing, Just 3)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Mon, Jun 06, 2011 at 09:08:40AM +0800, Haisheng Wu wrote:
> Thanks Senastian!
> 
> I would refine the equality as below:
> 
>   sequence [Just 2, Nothing]
> = do x <- Just 2
>      y <- sequence [Nothing]
>   return (x:y)
> = Just 2 >>= \x -> sequence [Nothing] >>= \y -> return (x:y)
> = Nothing >>= \y -> return (2:y)
> = Nothing

Yes, exactly.

-Brent



------------------------------

Message: 6
Date: Mon, 6 Jun 2011 20:19:49 +0200
From: Manfred Lotz <[email protected]>
Subject: [Haskell-beginners] xml light problem or pebkac
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Hi all,
I'm a bit lost. I'm having a problem with xml light but as a Haskell
newbie it could be of course that the problem is sitting between my
keyboard and chair.


I tried hard to create a minimal example.

Here is a minimal xmltest.hs:

<-----------------------------snip--------------------------------> 
module Main where

import System.Environment.UTF8
import qualified System.IO.UTF8 as U
import System.IO
import Text.XML.Light


data CTest = CTest {
    ctName :: String
  , ctLocation :: String
  } deriving (Show,Read,Eq,Ord)

getXmlContent :: Handle -> IO CTest 
getXmlContent inh = do
    xml <- U.hGetContents inh
    let content = parseXMLDoc xml
    case content of
      Just c -> do let name = case findChild (unqual "name") c of
                         Nothing -> "<unknown>"
                         Just n' -> strContent n' 
                   let path = case findChild (unqual "location") c of
                         Nothing -> "<unknown>"
                         Just path' -> case findAttr (unqual "path")
path' of Nothing -> "<unknown>"
                                      Just p -> p
                   return CTest { ctName=name, ctLocation=path}
      _  -> fail "not expected" 

  
readXmlFile :: FilePath -> IO CTest
readXmlFile f = do
  inh <- U.openBinaryFile f ReadMode
  xml <- getXmlContent inh 
  hClose inh
  return xml

doSomething :: Show a => a -> IO ()  
doSomething xml =
  print xml
  
main :: IO ()
main = do
  args <- getArgs
  xml <- readXmlFile $ head args 
  doSomething xml
<-----------------------------snap--------------------------------> 


Here is the test.xml I use:

<-----------------------------snip--------------------------------> 
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE entry SYSTEM 'test.dtd'>
<entry>
  <name>some name</name>
  <description>
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
  </description>
  <location path='/info/somepath'/>
</entry>
<-----------------------------snap--------------------------------> 

Here the output of the program:
CTest {ctName = "some name", ctLocation = "<unknown>"}


which is wrong.


If I delete just one of the 'bla bla' lines then the output of the
program is:
CTest {ctName = "some name", ctLocation = "/info/somepath"}

which is correct.


Question: What is my error?



-- 
Manfred





------------------------------

Message: 7
Date: Mon, 6 Jun 2011 21:55:23 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] xml light problem or pebkac
To: [email protected]
Cc: Manfred Lotz <[email protected]>
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="us-ascii"

On Monday 06 June 2011, 20:19:49, Manfred Lotz wrote:
> Hi all,
> I'm a bit lost. I'm having a problem with xml light but as a Haskell
> newbie it could be of course that the problem is sitting between my
> keyboard and chair.
> 

Not really. Yes, you're doing something wrong, but it's a bit delicate and 
nothing obvious. It's the unobvious behaviour of lazy IO.

> 
> I tried hard to create a minimal example.
> 
> Here is a minimal xmltest.hs:
> 
> <-----------------------------snip-------------------------------->
> module Main where
> 
> import System.Environment.UTF8
> import qualified System.IO.UTF8 as U



> import System.IO
> import Text.XML.Light
> 
> 
> data CTest = CTest {
>     ctName :: String
>   , ctLocation :: String
>   } deriving (Show,Read,Eq,Ord)
> 
> getXmlContent :: Handle -> IO CTest
> getXmlContent inh = do
>     xml <- U.hGetContents inh

Problem, part 1

>     let content = parseXMLDoc xml
>     case content of
>       Just c -> do let name = case findChild (unqual "name") c of
>                          Nothing -> "<unknown>"
>                          Just n' -> strContent n'
>                    let path = case findChild (unqual "location") c of
>                          Nothing -> "<unknown>"
>                          Just path' -> case findAttr (unqual "path")
> path' of Nothing -> "<unknown>"
>                                       Just p -> p
>                    return CTest { ctName=name, ctLocation=path}
>       _  -> fail "not expected"
> 
> 
> readXmlFile :: FilePath -> IO CTest
> readXmlFile f = do
>   inh <- U.openBinaryFile f ReadMode
>   xml <- getXmlContent inh
>   hClose inh

Problem, part 2, the real problem.

The point is that getXmlContent doesn't really parse the file yet, it 
returns a thunk saying how to get the result from the file contents.
Therefore, it doesn't need to read the entire file, just enough of it to 
find out whether parseXMLDoc returns a Just contents or a Nothing.

Then you close the handle, explicitly. That means, it's closed immediately, 
leaving the unread portion of the file unread. If the bla bla is long 
enough, the location tag is in the unread part, and when finally the search 
for that tag is forced by printing, the tag is not contained in the input.


If you leave out the call to hClose, leaving the closing to hGetContents 
when it reaches the end of the file, the file contents is not truncated, 
and the location found.
But then the file handle might remain half-closed longer than you wish (you 
could run out of file handles if you open a lot of files without explicitly 
closing them before the next [bunch] is opened).

If you force the result before closing the handle, enough of the file is 
read to find the desired elements, and you make sure that there's no 
leaking file handle -- unless there's an exception in getXmlContent, so 
that the hClose is never reached. To prevent that, use 
Control.Exception.bracket, or use withBinaryFile, which does that for you.

>   return xml
> 
> doSomething :: Show a => a -> IO ()
> doSomething xml =
>   print xml
> 
> main :: IO ()
> main = do
>   args <- getArgs
>   xml <- readXmlFile $ head args
>   doSomething xml
> <-----------------------------snap-------------------------------->
> 
 
> Question: What is my error?



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 36, Issue 16
*****************************************

Reply via email to