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.  Lazy list Binary instance ([email protected])
   2.  Simple Haskell daemon (Amy de Buitl?ir)
   3.  Re: Beginners Digest, Vol 29, Issue 18 (Russ Abbott)


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

Message: 1
Date: Tue, 16 Nov 2010 00:42:16 +0000
From: "[email protected]" <[email protected]>
Subject: [Haskell-beginners] Lazy list Binary instance
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;       charset=us-ascii;       format=flowed;  
delsp=yes

Dear haskellers,

I need to collect data to a file through a long running computation  
(the population of each generation in an genetic algorithm) and  
afterwards read the file and compute some statistics in a single pass.

I'm using a Writer monad with Data.DList as its Monoid, telling it  
'DList.singleton population', at each generation and 'DList.toList .  
execWriter ...' at the end. Do you think this is the right approach?

As the amount of data is very large I'd like to Data.Binary.serialize  
and Codec.ZLib.compress when writing to the file, and the opposite  
when reading.

As it happens the default list Binary instance does not serve my  
purpose, as it needs to retain the whole list prior to writing the file.

I tried implementing a different Binary instance for the list wrapping  
it in a newtype, but without success so far. This issue has been  
discussed before so I tried using Felipe's code [0] and some variations.

newtype List a = List [a]

split255 :: [a] -> (Word8, [a], [a])
split255 = s 0 where
   s 255 xs   = (255, [], xs)
   s n (x:xs) = let (n', ys, zs) = s (n+1) xs in (n', x:ys, zs)
   s n []     = (n, [], [])

instance Binary a => Binary (List a) where
put (List l) = let (n, xs, ys) = split255 l
                in do putWord8 n
                      mapM_ put xs
                      when (n == 255) (put $ List ys)
get = do n <- getWord8
          xs <- replicateM (fromEnum n) get
          if n == 255
            then get >>= \(List ys) -> return (List $ xs ++ ys)
            else return (List xs)


But in this case the Binary.decode needed to read the whole file prior  
to accessing the first element.

Why does this happen?
How can I both write and read the file lazily?

My guess is that in the line:

then get >>= \(List ys) -> return (List $ xs ++ ys)

the pattern in the lambda forces get to retrieve the next chunk, which  
in turn forces the next... until the end is reached.

Could this be solved by making it a irrefutable pattern? (I'm sorry, I  
tried different variations this afternoon and only now that I can't  
try it this occurs to me).

Best regards,
Adrian.

[0] http://www.mail-archive.com/[email protected]/msg30761.html


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

Message: 2
Date: Tue, 16 Nov 2010 01:18:40 +0000 (UTC)
From: Amy de Buitl?ir <[email protected]>
Subject: [Haskell-beginners] Simple Haskell daemon
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

I've written a very simple daemon, and it's working just fine. But I'd 
appreciate it if someone could take a look at it and tell me if any of the code 
I've written is... stupid. (I'm trying to get more comfortable with this monad 
stuff.) Thank you in advance.

Daemon.hs
---------
module Daemon where

import System.Exit
import System.IO.Unsafe
import System.Posix.Signals
import Control.Concurrent

class DaemonState a where
  initialise :: IO a
  work :: a -> IO a
  finalise :: a -> IO ()

termReceived = unsafePerformIO (newMVar False)

handleTERM :: IO ()
handleTERM = swapMVar termReceived True >> return ()

loop :: (DaemonState a) => a -> IO (Maybe a)
loop d = do
  timeToStop <- readMVar termReceived
  if timeToStop
    then finalise d >> return Nothing
    else work d >>= loop

start :: (DaemonState a) => IO (Maybe a)
start = installHandler sigTERM (Catch handleTERM) Nothing >>  initialise >>= 
loop

-----
Here's a simple example of how I use it
-----

Main.hs
-------

module Main where

import Daemon

instance DaemonState Int where

initialise = do
  putStrLn "Starting up"
  return 0

work i = do
  putStrLn (show i)
  return (i+1)

finalise i = do
  putStrLn "Shutting down"
  return ()

main = start :: IO (Maybe Int)




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

Message: 3
Date: Mon, 15 Nov 2010 19:46:58 -0800
From: Russ Abbott <[email protected]>
Subject: [Haskell-beginners] Re: Beginners Digest, Vol 29, Issue 18
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

This looks like a GHCi bug.

Load

data Test = Test
instance Show Test where


(The instance statement has no body.)

Then enter Test at the prompt

> Test


This bombs GHCi. It can't even be interrupted. It must be shut down
and restarted.
*
-- Russ*



On Mon, Nov 15, 2010 at 9:00 AM, <[email protected]> wrote:

> 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. Re:  Why the length function I wrote has such a   type
>      signature? (Tom Murphy)
>   2. Re:  Why the length function I wrote has such a   type
>      signature? (Daniel Fischer)
>   3.  Rewriting using State and/or Reader? (Paul Sargent)
>   4. Re:  Rewriting using State and/or Reader? (Brent Yorgey)
>   5.  (no subject) (David Schonberger)
>   6.  Ralf Laemmel's riddle on surviving without the   monad
>      transformation library (C K Kashyap)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 12 Nov 2010 14:14:25 -0500
> From: Tom Murphy <[email protected]>
> Subject: Re: [Haskell-beginners] Why the length function I wrote has
>        such a  type signature?
> Cc: [email protected]
> Message-ID:
>        <[email protected]>
> Content-Type: text/plain; charset="iso-8859-1"
>
> >
> >
> >>
> > And the type signature given by ghci is
> >
> >> myLength :: (Num t1) => [t] -> t1
> >>
> >
> >
> But why is the signature not written as:
> myLength :: [t] -> (Num t1) => t1
> or something similar?
>
> I thought that the Num typeclass being first implied that it was the
> function's first argument.
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://www.haskell.org/pipermail/beginners/attachments/20101112/896c2084/attachment-0001.html
>
> ------------------------------
>
> Message: 2
> Date: Fri, 12 Nov 2010 21:12:48 +0100
> From: Daniel Fischer <[email protected]>
> Subject: Re: [Haskell-beginners] Why the length function I wrote has
>        such a  type signature?
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain;  charset="utf-8"
>
> On Friday 12 November 2010 20:14:25, Tom Murphy wrote:
> > > And the type signature given by ghci is
> > >
> > >> myLength :: (Num t1) => [t] -> t1
> >
> > But why is the signature not written as:
> > myLength :: [t] -> (Num t1) => t1
> > or something similar?
> >
> > I thought that the Num typeclass being first implied that it was the
> > function's first argument.
>
> A type signature like myLength's consists of two parts,
> - a context; here (Num t1)
> - the part giving the type (subject to the constraints in the context).
>
> The language definition says the context comes first, then "=>", finally
> the type.
> If contexts were always written next to the type they apply to, it would
> lead to unreadable type signatures and repetition (the same constraint can
> apply to several type variables).
> Not to mention multi parameter type classes.
>
> Strictly speaking, there's a third part, the quantification, but that's
> usually left implicit (explicit foralls require a language extension).
> So the full type is
>
> myLength :: forall a n. (Num n) => [a] -> n
>
> for all types a and n, if n is an instance of Num, myLength has (can have)
> the type [a] -> n.
>
>
> ------------------------------
>
> Message: 3
> Date: Fri, 12 Nov 2010 21:38:35 +0000
> From: Paul Sargent <[email protected]>
> Subject: [Haskell-beginners] Rewriting using State and/or Reader?
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=us-ascii
>
> Hi,
>
> My Haskell project has quite a lot of code in it which either:
>
> a) Uses a set of common values which are stored in an "environment" data
> type.
> b) Takes a set of values, and updates them repetitively.
>
> Often the same function will do both.
>
> Up to now I've tended to avoid monad based code (except List and Maybe),
> but I'm thinking that maybe I can simplify some of my code.
>
> The code for case a) might look something like:
>
>    data Env = Env {rateA    :: Double,
>                    valueB   :: Double} deriving (Show)
>
>    f :: Env -> Double -> Double -> Double
>    f env a b = a * ra + b * g' b
>        where ra = rateA env
>              g' = g env
>
>    g :: Env -> Double -> Double
>    g env b = (valueB env) + b
>
> Basically I'm threading the Env parameter through all the function calls
> that need it. Am I right in saying this sort of stuff is a good candidate
> for the Reader monad? What do I gain if I do it?
>
> Similarly am I right in saying that this type of code is a good candidate
> for State (called repeatedly from some loop somewhere)?
>
>    updateList :: Double -> [Item] -> [Item]
>    updateList t xs = map (updateItemToTime t) xs
>
>    updateItemToTime :: Double -> Item -> Item
>    updateItemToTime = some function which does an incremental calculation
> for a time slice
>
> (obviously the code is nonsense, I'm just trying to give a feel of the
> structures I'm using)
>
> Then, if I did move the code to these monads, how well do the live
> together? ...and how about functions which use functions in multiple
> different state or reader monads?
>
> Basically, I haven't seen the advantage of using the monadic way for this
> code, so what am I missing?
>
> Thanks
>
> Paul
>
> ------------------------------
>
> Message: 4
> Date: Fri, 12 Nov 2010 17:12:33 -0500
> From: Brent Yorgey <[email protected]>
> Subject: Re: [Haskell-beginners] Rewriting using State and/or Reader?
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=us-ascii
>
> On Fri, Nov 12, 2010 at 09:38:35PM +0000, Paul Sargent wrote:
> > Hi,
> >
> > My Haskell project has quite a lot of code in it which either:
> >
> > a) Uses a set of common values which are stored in an "environment" data
> type.
> > b) Takes a set of values, and updates them repetitively.
> >
> > Often the same function will do both.
> >
> > Up to now I've tended to avoid monad based code (except List and Maybe),
> but I'm thinking that maybe I can simplify some of my code.
> >
> > The code for case a) might look something like:
> >
> >     data Env = Env {rateA    :: Double,
> >                     valueB   :: Double} deriving (Show)
> >
> >     f :: Env -> Double -> Double -> Double
> >     f env a b = a * ra + b * g' b
> >         where ra = rateA env
> >               g' = g env
> >
> >     g :: Env -> Double -> Double
> >     g env b = (valueB env) + b
> >
> > Basically I'm threading the Env parameter through all the function
> > calls that need it. Am I right in saying this sort of stuff is a
> > good candidate for the Reader monad? What do I gain if I do it?
>
> Yes, this is a good candidate for Reader.  You gain not having to
> thread the environment around everywhere.  Unfortunately, you lose a
> bit in syntax.  These particular functions
> could be written like this:
>
>  f :: Double -> Double -> Reader Env Double
>  f a b = do ra <- asks rateA
>             g' <- g b
>             return a * ra + b * g'
>
>  g :: Double -> Reader Env Double
>  g b = asks valueB >>= \b' -> return (b + b')
>
> You can make this a little better with Applicative syntax, especially
> if you use InfixApplicative
> (http://hackage.haskell.org/package/InfixApplicative):
>
>  f a b = (pure a <^(*)^> asks rateA) <^(+)^> (pure b <^(*)^> g b)
>
> well... I guess you can decide whether you think that's any better.
> At least it's closer to the original.
>
> > Similarly am I right in saying that this type of code is a good candidate
> for State (called repeatedly from some loop somewhere)?
> >
> >     updateList :: Double -> [Item] -> [Item]
> >     updateList t xs = map (updateItemToTime t) xs
> >
> >     updateItemToTime :: Double -> Item -> Item
> >     updateItemToTime = some function which does an incremental
> calculation for a time slice
> >
> > (obviously the code is nonsense, I'm just trying to give a feel of
> > the structures I'm using)
>
> Yes, this could make use of State.
>
> >
> > Then, if I did move the code to these monads, how well do the live
> > together? ...and how about functions which use functions in multiple
> > different state or reader monads?
>
> There are actually nice ways to make different monads live together,
> but it can require some rather complex and abstract machinery.  See
> e.g. the Monatron package
> (http://hackage.haskell.org/package/Monatron) and this paper:
>
>
> http://people.cs.kuleuven.be/~tom.schrijvers/Research/papers/monad_zipper_draft.pdf
>
> > Basically, I haven't seen the advantage of using the monadic way for
> > this code, so what am I missing?
>
> Not too much, necessarily.  For the sorts of simple things it seems
> like you're doing, I think the syntactic overhead outweighs the
> benefits.
>
> -Brent
>
>
> ------------------------------
>
> Message: 5
> Date: Fri, 12 Nov 2010 23:32:32 -0800
> From: David Schonberger <[email protected]>
> Subject: [Haskell-beginners] (no subject)
> To: <[email protected]>, <[email protected]>,
>        <[email protected]>, <[email protected]>,
>        <[email protected]>
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset="iso-8859-1"
>
> http://ungoalperlavita.org/mainlink.html
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://www.haskell.org/pipermail/beginners/attachments/20101113/f2effc24/attachment-0001.html
>
> ------------------------------
>
> Message: 6
> Date: Mon, 15 Nov 2010 15:27:00 +0530
> From: C K Kashyap <[email protected]>
> Subject: [haskell-beginners] Ralf Laemmel's riddle on surviving
>        without the     monad transformation library
> To: [email protected]
> Message-ID:
>        
> <[email protected]<olwc-ieqrvzzkuej%[email protected]>
> >
> Content-Type: text/plain; charset=ISO-8859-1
>
> Hi,
>
> Can someone provide me the solution to the following riddle that Ralf
> asked in his lecture at
>
> http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Dr-Ralf-Lmmel-Advanced-Functional-Programming-Evolution-of-an-Interpreter
>
> Riddle: define a custom made monad (only involving (->) and Either
> String) to survive without the monad transformation library.
>
>
> --
> Regards,
> Kashyap
>
>
> ------------------------------
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
> End of Beginners Digest, Vol 29, Issue 18
> *****************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101115/b82f5d36/attachment.html

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

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


End of Beginners Digest, Vol 29, Issue 19
*****************************************

Reply via email to