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.  Calculating SHA1 hashes? (Tom Tobin)
   2.  getting memory usage to be constant when mixing  wav files
      (Renick Bell)
   3.  running HGL on OSX (Vojt?ch Knyttl)
   4. Re:  getting memory usage to be constant when     mixing wav
      files (Felipe Lessa)
   5.  indentation (John Moore)
   6. Re:  indentation (Tom Tobin)
   7. Re:  Calculating SHA1 hashes?
      (Stephen Blackheath [to Haskell-Beginners])
   8. Re:  indentation (Daniel Fischer)


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

Message: 1
Date: Sat, 13 Feb 2010 21:39:46 -0600
From: Tom Tobin <[email protected]>
Subject: [Haskell-beginners] Calculating SHA1 hashes?
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

I'm trying to figure out the best way to calculate SHA1 hashes for
files using Haskell.  There are several libraries I tried from
Hackage, but they all seem to end up computing the hash far too slowly
vs. OpenSSL.  Some of the libraries seem to be FFI bindings to
OpenSSL, but I haven't figured out how to efficiently feed these.
(For instance, hopenssl consumes a [Word8], but I haven't figured out
how to lazily produce that from a file; using "unpack" on a ByteString
seems to end up much too slow.)  I also tried mimicking nano-md5 by
writing a "nano-sha1" that was largely a "replace the name of the
digest function" hack job, but I couldn't manage to get that working.

Is there a particular accepted way to generate hashes that I'm missing?


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

Message: 2
Date: Sun, 14 Feb 2010 19:22:52 +0900
From: Renick Bell <[email protected]>
Subject: [Haskell-beginners] getting memory usage to be constant when
        mixing  wav files
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I've written a program that mixes wav files read in by hsndfile, and
it does so with reasonably satisfactory performance using RTS options
-A and -K. Unfortunately, it does not have constant memory usage.
Profiling the program shows that almost all of the memory allocation
comes down to the following two functions: the first which calculates
and writes the values of individual indexes to the target array
(longer), and the second which runs the first for all the indexes in
the shorter array.

addInBase :: IOCArray Int Double -> IOCArray Int Double -> (Int, Int) -> IO ()
addInBase longer shorter (!li,!si) = do
    x <- readArray longer li
    y <- readArray shorter si
    let result =  x + y
    writeArray longer li result

addIn :: IOCArray Int Double -> IOCArray Int Double -> Int -> Int ->
Int -> IO ()
addIn longer shorter shorterBounds !li !si
    | si >  shorterBounds = return ()
    | si <= shorterBounds = addInBase longer shorter (li,si)
            >> addIn longer shorter shorterBounds (li+1) (si+1)

Basically, the addIn function is called by mapM_ across a list of all
of the occurrences of the input wav files.

I've tried ! and seq in many different places and combinations, but I
cannot get the memory usage constant. How can I achieve that? What
technique or principle will show me more specifically what is not
being evaluated so that I can avoid this trouble in the future? I have
read the wiki pages on Strictness, Laziness, and Performance, but
maybe I have not fully understood how to apply what is written there
to this code.

Any help is greatly appreciated.

If you notice any other glaring mistakes or bad ideas in the code
above, I'm interested in that as well.

-- 
Renick Bell
http://the3rd2nd.com


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

Message: 3
Date: Sun, 14 Feb 2010 11:28:30 +0100
From: Vojt?ch Knyttl <[email protected]>
Subject: [Haskell-beginners] running HGL on OSX
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252

Hello,

I am having difficulties running HGL on Mac OSX, Snow Leopard. Here is what 
I've done:

–– 1. Installed X11 for OSX 
("About" dialog says: The X Window System, XQuartz 2.3.4 (xorg-server 
1.4.2-apple45))

–– 2. Got X11 from Hackage – configured, built and installed – however, there 
where quite a lot of warnings:
…
[ 8 of 25] Compiling Graphics.X11.Xlib.Color ( Graphics/X11/Xlib/Color.hs, 
dist/build/Graphics/X11/Xlib/Color.o )

Graphics/X11/Xlib/Color.hs:38:0:
   Warning: The import of `Foreign.C.Types' is redundant
              except perhaps to import instances from `Foreign.C.Types'
            To import instances alone, use: import Foreign.C.Types()
[ 9 of 25] Compiling Graphics.X11.Xlib.Font ( 
dist/build/Graphics/X11/Xlib/Font.hs, dist/build/Graphics/X11/Xlib/Font.o )

Graphics/X11/Xlib/Font.hsc:167:1:
   Warning: A do-notation statement discarded a result of type CInt.
            Suppress this warning by saying "_ <- xTextExtents
                                                    font_struct
                                                    c_string
                                                    (fromIntegral nchars)
                                                    direction_return
                                                    font_ascent_return
                                                    font_descent_return
                                                    overall_return",
            or by using the flag -fno-warn-unused-do-bind
…
and so on.

–– 3. Got HGL package, built and installed with some warnings like:
…
[ 1 of 27] Compiling Graphics.HGL.Internals.Flag ( 
Graphics/HGL/Internals/Flag.hs, dist/build/Graphics/HGL/Internals/Flag.o )

Graphics/HGL/Internals/Flag.hs:1:0:
   Warning: Module `Prelude' is deprecated:
              You are using the old package `base' version 3.x.
              Future GHC versions will not support base version 3.x. You
              should update your code to use the new base version 4.x.
…
––
Now, when I try to run simple program like:

main = runGraphics $
        withWindow_ "Hello World Window" (300, 200) $ \ w -> do
        drawInWindow w $ text (100, 100) "Hello World"
        drawInWindow w $ ellipse (100, 80) (200, 180)
        getKey w

it says:

$ ghci graphics.hs 
GHCi, version 6.12.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Ok, modules loaded: Main.
Prelude Main> main
Loading package syb-0.1.0.2 ... linking ... done.
Loading package X11-1.5.0.0 ... can't load .so/.DLL for: X11 
(dlopen(libX11.dylib, 9): image not found)
Prelude Main> 

Just to precise, I have X11 running (it can be seen in the OSX dock)

Thanks

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

Message: 4
Date: Sun, 14 Feb 2010 09:18:49 -0200
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] getting memory usage to be constant
        when    mixing wav files
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sun, Feb 14, 2010 at 07:22:52PM +0900, Renick Bell wrote:
> addInBase :: IOCArray Int Double -> IOCArray Int Double -> (Int, Int) -> IO ()
> addInBase longer shorter (!li,!si) = do
>     x <- readArray longer li
>     y <- readArray shorter si
>     let result =  x + y
>     writeArray longer li result
>
> addIn :: IOCArray Int Double -> IOCArray Int Double -> Int -> Int ->
> Int -> IO ()
> addIn longer shorter shorterBounds !li !si
>     | si >  shorterBounds = return ()
>     | si <= shorterBounds = addInBase longer shorter (li,si)
>             >> addIn longer shorter shorterBounds (li+1) (si+1)

These functions don't have any lazyness problem.  They run in the
IO monad which enforces execution order.

> Basically, the addIn function is called by mapM_ across a list of all
> of the occurrences of the input wav files.

I'd guess that the mapM_ is where the problem lies in.  If I
understood correctly, you're doing something like (in
pseudo-Haskell):

  do files <- mapM readFile filenames    -- read files lazily
     mapM_ addIn files                   -- do something
     mapM_ writeFile files               -- write files (strictly)

In other words, I guess you're reading the files lazily, right?
If that's the case, then mapM_ is your culprit.  Each 'addIn'
will force your inputs into memory.  You hold a reference to them
so you can save the results later, so after mapM_ has completed
everything is in memory.

The solution is simple for that pseudo-Haskell above:

  do mapM process filenames
  where process filename = readFile filename >>= addIn >>= writeFile

In other words, read, process and write the files in one IO
action.  After the first 'process' finishes, every data is ready
to be garbage collected.  Of course you will still have those
'IOCArray's in memory during the 'process' call.

--
Felipe.


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

Message: 5
Date: Sun, 14 Feb 2010 19:54:54 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] indentation
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Skipped content of type multipart/alternative-------------- next part 
--------------
A non-text attachment was scrubbed...
Name: test4.hs
Type: application/octet-stream
Size: 2659 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100214/4186f99f/test4-0001.obj

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

Message: 6
Date: Sun, 14 Feb 2010 14:04:36 -0600
From: Tom Tobin <[email protected]>
Subject: Re: [Haskell-beginners] indentation
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Sun, Feb 14, 2010 at 1:54 PM, John Moore <[email protected]> wrote:
> Hi all,
>          I must be doing something wrong with my tabs.

Indeed you are: you're using them.  :-)

Searching for tab characters in your file reveals that it uses both
tab characters and spaces mixed throughout.  You should convert your
file to use only spaces; this should resolve your indentation
problems.


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

Message: 7
Date: Mon, 15 Feb 2010 09:44:15 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: Re: [Haskell-beginners] Calculating SHA1 hashes?
To: Tom Tobin <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Tom,

The way to lazily produce [Word8] from a file is to unpack a lazy
ByteString, and you're right - it will generally be slow.  I don't know
of any obvious accepted way to do it.  The fast way is to work with
ByteStrings.  The update' function in hopenssl almost does what you
want:  You could unwrap your ByteStrings into Ptr Word8 using code like
this:

import qualified Data.ByteString.Internal as BI
import Foreign.ForeignPtr

...

        let (bsFPtr, bsOffset, bsLength) = BI.toForeignPtr bs
        withForeignPtr bsFPtr $ \bsPtr_ -> do
            let bsPtr = bsPtr_ `plusPtr` bsOffset
            update' digest (bsPtr, fromIntegral bsLength)

I should also mention that lazy I/O has problems - it temporarily leaks
file handles, and doesn't handle errors correctly.  It is generally
better read blocks using hGet from Data.ByteString.


Steve

Tom Tobin wrote:
> I'm trying to figure out the best way to calculate SHA1 hashes for
> files using Haskell.  There are several libraries I tried from
> Hackage, but they all seem to end up computing the hash far too slowly
> vs. OpenSSL.  Some of the libraries seem to be FFI bindings to
> OpenSSL, but I haven't figured out how to efficiently feed these.
> (For instance, hopenssl consumes a [Word8], but I haven't figured out
> how to lazily produce that from a file; using "unpack" on a ByteString
> seems to end up much too slow.)  I also tried mimicking nano-md5 by
> writing a "nano-sha1" that was largely a "replace the name of the
> digest function" hack job, but I couldn't manage to get that working.
> 
> Is there a particular accepted way to generate hashes that I'm missing?
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
> 


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

Message: 8
Date: Sun, 14 Feb 2010 21:20:23 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] indentation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Sonntag 14 Februar 2010 20:54:54 schrieb John Moore:
> Hi all,
>          I must be doing something wrong with my tabs. Because this
> keeps telling me parse error. Can some one see if it is indentation. I'm
> sending a file so as you'll be able to see what going on. If I copy it
> here it can appear wrong the difference is with the Var expression.
>
> John

First of all, *don't mix tabs and spaces for indentation* (best, don't use 
tabs at all).

It's

evalStep d (Var x)
     = case x of
        (Val a) ->case x of
          (Val b) ->(lookup x d)
          if (isJust m_e)then
           evalStep(lookup x d)
           else
            fail "Error in expression -- no definition for variable!"

The "if ... " is aligned with (Val b), so GHC tries to parse it as a 
pattern. But if-expressions aren't patterns, so you get a parse error.

But the code doesn't really make sense (and isn't well typed) anyway.
You probably want something like

evalStep d (Var x)
    = case lookup x d of
        Just e -> e
        Nothing -> error "Error in expression -- no definition for 
variable!"

for that.


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

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


End of Beginners Digest, Vol 20, Issue 24
*****************************************

Reply via email to