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:  MVar blocked or not? (Dean Herington)
   2. Re:  Re: Code help requested (Steve)
   3. Re:  Re: Code help requested (Daniel Fischer)
   4.  Re: Understanding Monadic I/O (Heinrich Apfelmus)
   5.  MULTICONF-10 Call for papers (John Edward)
   6.  Defining of subset of constructors as valid      values (Lev Broido)
   7. Re:  Defining of subset of constructors as valid  values
      (Stephen Tetley)


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

Message: 1
Date: Sat, 16 Jan 2010 22:16:41 -0500
From: Dean Herington <[email protected]>
Subject: Re: [Haskell-beginners] MVar blocked or not?
To: Adrian Adshead <[email protected]>, [email protected]
Message-ID: <a06240800c7782f313...@[192.168.1.107]>
Content-Type: text/plain; charset="us-ascii" ; format="flowed"

At 4:08 PM +0000 1/15/10, Adrian Adshead wrote:
>Hi everyone,
>
>I have a beginer question on using MVar.
>
>In the simple case I am getting what I expect...
>
>
>***CODE***
>module Main ( main ) where
>
>import Control.Concurrent
>import System.IO
>
>main = do
>   mv <- newEmptyMVar
>   putMVar mv 'a'
>   threadDelay (5 * 10^6)
>   putMVar mv 'b'
>   threadDelay (5 * 10^6)
>***/CODE***
>
>
>Output after the first 5 second delay is :-
>
>Demo.exe: thread blocked indefinitely
>
>
>
>It even works as expected when there is a reading thread
>
>***CODE***
>module Main ( main ) where
>
>import Control.Concurrent
>import Control.Monad
>import System.IO
>
>main = do
>   mv <- newEmptyMVar
>   forkIO (readMV mv)
>   putMVar mv 'a'
>   threadDelay (5 * 10^6)
>   putMVar mv 'b'
>   threadDelay (5 * 10^6)
>
>readMV mv = forever $ do
>   ch <- takeMVar mv
>   putChar ch >> hFlush stdout
>***/CODE***
>
>Output including expected delays :-
>ab
>
>
>
>Then it starts to act unexpectedly
>
>***CODE***
>module Main ( main ) where
>
>import Control.Concurrent
>import Control.Monad
>import System.IO
>
>main = do
>   mv <- newEmptyMVar
>   forkIO (readMV mv)
>   putMVar mv 'a'
>   threadDelay (5 * 10^6)
>   putMVar mv 'b'
>   threadDelay (5 * 10^6)
>
>readMV mv = forever $ do
>   ch <- modifyMVar mv modMV
>   putChar ch >> hFlush stdout
>   threadDelay (9*10^5)
>
>modMV 'a' = return ('c','a')
>modMV 'c' = return ('a','c')
>modMV x = return (x,x)
>***/CODE***
>
>Output is :-
>acacac
>
>
>The problem is that the output stops after 5 seconds
>and the program exits after 10 seconds. But why?
>
>Questions:-
>
>1. Why does the output stop? (Shouldn't the reader thread
>continue even if the main thread blocks on the second putMVar?)
>
>2. Why does it exit? (Shouldn't it be blocked at the second
>putMVar? The readMV thread now only modifies the MVar so it
>should never be empty)
>
>3. Is there a race condition using modifyMVar while there
>is another thread blocked in a putMVar? (I am assuming the
>second putMVar succeeded, but where did the 'b' go?)
>
>Many thanks for any help
>
>Adrian.

I believe the explanation is that modifyMVar simply wraps takeMVar 
and putMVar around your modification action.  It does not perform 
these two "atomically", contrary to what you seem to be expecting. 
Hence, the main thread is able to fill the MVar with 'b' in the short 
time the readMV thread leaves the MVar empty while doing its 
modifyMVar.  This blocks the readMV thread (1.), allowing the main 
thread to proceed (2.) and ultimately exit.  The 'b' went into the 
MVar, but the readMV thread never saw it, because it was waiting in 
vain to put back the 'a' it last withdrew.

Dean


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

Message: 2
Date: Sun, 17 Jan 2010 13:21:56 +0800
From: Steve <[email protected]>
Subject: Re: [Haskell-beginners] Re: Code help requested
To: [email protected]
Message-ID: <1263705716.7415.29.ca...@localhost>
Content-Type: text/plain

On Sat, 2010-01-16 at 14:42 -0500, [email protected] wrote:
> binary_find [1] 2
> ~> do_search [1] 2 0 1
> ~> mid = 0 + 1 `div` 2 = 0
> ~> [1] !! mid < 2
> ~> do_search [1] 2 (0+1) 1
> ~> mid = 1 + 0 `div` 2 = 1
> ~> [1] !! 1 => boom
> 
> >   where
> >     do_search list elem low high =
> >       if high < low then Nothing
> >       else
> >         if list !! mid > elem
> >         then do_search list elem low (mid - 1)
> >         else
> >           if list !! mid < elem
> >           then do_search list elem (mid + 1) high
> >           else Just mid
> >       where
> >         mid = low + (high - low) `div` 2
> 
> I'd prefer
>       mid = (low + high) `div` 2
> here.
mid = (low + high) `div` 2
is buggy code. Java had this bug for 9 years until someone noticed.
http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html

mid = low + (high - low) `div` 2
is useful because it avoids Int overflow errors (when using very, very
long lists).

Steve




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

Message: 3
Date: Sun, 17 Jan 2010 10:51:15 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: Code help requested
To: Steve <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Sonntag 17 Januar 2010 06:21:56 schrieb Steve:
> On Sat, 2010-01-16 at 14:42 -0500, [email protected] wrote:
> > binary_find [1] 2
> > ~> do_search [1] 2 0 1
> > ~> mid = 0 + 1 `div` 2 = 0
> > ~> [1] !! mid < 2
> > ~> do_search [1] 2 (0+1) 1
> > ~> mid = 1 + 0 `div` 2 = 1
> > ~> [1] !! 1 => boom
> >
> > >   where
> > >     do_search list elem low high =
> > >       if high < low then Nothing
> > >       else
> > >         if list !! mid > elem
> > >         then do_search list elem low (mid - 1)
> > >         else
> > >           if list !! mid < elem
> > >           then do_search list elem (mid + 1) high
> > >           else Just mid
> > >       where
> > >         mid = low + (high - low) `div` 2
> >
> > I'd prefer
> >       mid = (low + high) `div` 2
> > here.
>
> mid = (low + high) `div` 2
> is buggy code. Java had this bug for 9 years until someone noticed.
> http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it
>-nearly.html
>
> mid = low + (high - low) `div` 2
> is useful because it avoids Int overflow errors (when using very, very
> long lists).

True - except I'm Out Of Memory long before overflow ;)

>
> Steve



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

Message: 4
Date: Sun, 17 Jan 2010 11:07:38 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Understanding Monadic I/O
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Markus Böhm wrote:
> Tx Heinrich. Could You explain how this "modelling the execution order
> as data dependency" works and how its tranlsated into sequential,
> imperative code? I mean in principle/for dummies.
> 
> From my beginner's perspective I feel I need that insight to
> understand how sustainable the abstraction is.

I don't know the details, but I think it's translated like this:

    putStrLn "Hello" >> putStrLn "World"

  => { state monad }

    \w1 -> let (a,w2) = primPutStrLn "Hello" w1
           in primPutStrLn "World" w2

  => { print out C code }

    a = cprimPutStrLn("Hello");
    cprimPutStrLn("World");

The purpose of  w1  and  w2  is just to ensure that the first
cprimPutStrLn  is called before the second.

Not very enlightening overall, if you ask me. It's not necessary to
understand it anyway, the IO monad can be entirely understood as an
algebraic data type, see for instance

  Swierstra und Altenkirch. Beauty in the beast.
  http://www.cse.chalmers.se/~wouter/Publications/BeautyInTheBeast.pdf



Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



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

Message: 5
Date: Sun, 17 Jan 2010 03:59:09 -0800 (PST)
From: John Edward <[email protected]>
Subject: [Haskell-beginners] MULTICONF-10 Call for papers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

MULTICONF-10 Call for papers
 
The 2010 multi-conference (MULTICONF-10) (website: 
http://www.promoteresearch.org) will be held during July 12-14, 2010 in 
Orlando, Florida, USA. The primary goal of MULTICONF is to promote research and 
developmental activities in computer science, information technology, control 
engineering, and related fields. Another goal is to promote the dissemination 
of research to a multidisciplinary audience and to facilitate communication 
among researchers, developers, practitioners in different fields.The following 
conferences are planned to be organized as part of MULTICONF-10.
 
        * International Conference on Artificial Intelligence and Pattern 
Recognition (AIPR-10)
        *  International Conference on Automation, Robotics and Control Systems 
(ARCS-10)
        * International Conference on Bioinformatics, Computational Biology, 
Genomics and Chemoinformatics (BCBGC-10)
        * International Conference on Computer Networks (CN-10)
        * International Conference on Enterprise Information Systems and Web 
Technologies (EISWT-10)
        * International Conference on High Performance Computing Systems 
(HPCS-10)
        * International Conference on Information Security and Privacy (ISP-10) 
        * International Conference on Image and Video Processing and Computer 
Vision (IVPCV-10)
        * International Conference on Software Engineering Theory and Practice 
(SETP-10) 
        * International Conference on Theoretical and Mathematical Foundations 
of Computer Science (TMFCS-10) 
 
We invite draft paper submissions. Please see the website 
http://www.promoteresearch.org for more details.
 
Sincerely
John Edward


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100117/2b43566c/attachment-0001.html

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

Message: 6
Date: Mon, 18 Jan 2010 11:32:21 +0200
From: Lev Broido <[email protected]>
Subject: [Haskell-beginners] Defining of subset of constructors as
        valid   values
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi all
I have the following code :

data CmdTiming  = RAS Time | CAS Time | PCH Time | ACT Time deriving
(Ord,Show,Eq)

data Command  =
  RD_CMD {bank :: Int, col :: Int }  |
  WR_CMD {bank :: Int, col :: Int}  |
  ACT_CMD {bank :: Int, row :: Int} |
  PCH_CMD {bank :: Int} deriving Show

I'd like to have something like :
RD_CMD {bank :: Int, col :: Int,timing :: RAS Time }
to limit possible values to RAS Time .

The reason I use constructors is differentiate whether current value is
applicable to some function , getting CmdTiming as parameter or not .

I am not sure this is valid definition since RAS Time is not type .

Regards,
Lev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100118/a47441b8/attachment-0001.html

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

Message: 7
Date: Mon, 18 Jan 2010 13:41:00 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Defining of subset of constructors as
        valid   values
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Lev

Empty data declarations are often used for doing this (and extending
to GADTs if necessary - GADTs are covered in the GHC user manual).

Here's a simple revision of your code that doesn't use GADTs - the
code is literate Haskell so you should be able to copy-paste it into a
file with .lhs as its extension (writing in literate Haskell means I
can run the code myself in GHCi as I'm writing it, thus hopefully not
posting code that is full of errors). Because each constructor for
your original CmdTiming data type carried the same data (just Time)
the translation to use EmptyDataDecls was straight forward - if you
had different variants for the constructors it would have been more
work.



> {-# LANGUAGE EmptyDataDecls             #-}

> module RASTime where

Some dummy Time type to make the compiler happy

> type Time = Int

> data RAS
> data CAS
> data PCH
> data ACT



> data CmdTiming a  = CmdTiming a Time
>   deriving  (Ord,Show,Eq)



> data Command  =
>   RD_CMD {bank :: Int, col :: Int, timing :: CmdTiming RAS }  |
>   WR_CMD {bank :: Int, col :: Int}  |
>   ACT_CMD {bank :: Int, row :: Int} |
>   PCH_CMD {bank :: Int}

Note with EmptyDataDecls now used in the Command type you can't
derive Show.


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

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


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

Reply via email to