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:  How to display a time difference? (Yitzchak Gale)
   2. Re:  How to display a time difference? (Colin Paul Adams)
   3.  Re: folds again -- myCycle (Will Ness)
   4. Re:  How to display a time difference? (Sean Lee)
   5. Re:  How to display a time difference? (Daniel Fischer)
   6. Re:  How to display a time difference? (Colin Paul Adams)
   7. Re:  How to display a time difference? (Sean Lee)
   8. Re:  How to display a time difference? (Colin Paul Adams)
   9. Re:  How to display a time difference? (Sean Lee)


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

Message: 1
Date: Wed, 18 Mar 2009 12:21:16 +0200
From: Yitzchak Gale <[email protected]>
Subject: Re: [Haskell-beginners] How to display a time difference?
To: Colin Paul Adams <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Colin Paul Adams wrote:
> The code of the following routine is intended to indicate how long it
> takes for the computer to make a move. However the time is printed (as
> very close to zero) long before the move is made.

You are writing a thunk to the IORef. It only gets computed
later on when you read the value.

Try using:

> readIORef game_state_ior >>=
>    evaluate . update_interactive_from_move move >>=
>    writeIORef game_state_ior

evaluate is in Control.Exception.

-Yitz


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

Message: 2
Date: Wed, 18 Mar 2009 10:28:39 +0000
From: Colin Paul Adams <[email protected]>
Subject: Re: [Haskell-beginners] How to display a time difference?
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

>>>>> "Yitzchak" == Yitzchak Gale <[email protected]> writes:

    Yitzchak> Colin Paul Adams wrote:
    >> The code of the following routine is intended to indicate how
    >> long it takes for the computer to make a move. However the time
    >> is printed (as very close to zero) long before the move is
    >> made.

    Yitzchak> You are writing a thunk to the IORef. It only gets
    Yitzchak> computed later on when you read the value.

    Yitzchak> Try using:

    >> readIORef game_state_ior >>= evaluate
    >> . update_interactive_from_move move >>= writeIORef
    >> game_state_ior

    Yitzchak> evaluate is in Control.Exception.

Still no joy with:

play_move :: IORef Game_state -> IO ()
play_move game_state_ior = do
  (_, state, _) <- readIORef game_state_ior  
  putStr "Playing AI: "
  start_time <- getCurrentTime
  let move = recommended_move state
  readIORef game_state_ior >>=
    evaluate . update_interactive_from_move move >>=
             writeIORef game_state_ior
  end_time <- getCurrentTime
  putStrLn $ show $ (diffUTCTime end_time start_time)


-- 
Colin Adams
Preston Lancashire


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

Message: 3
Date: Wed, 18 Mar 2009 10:33:10 +0000 (UTC)
From: Will Ness <[email protected]>
Subject: [Haskell-beginners] Re: folds again -- myCycle
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Bas van Dijk <v.dijk.bas <at> gmail.com> writes:

> 
> On Sun, Mar 15, 2009 at 7:35 PM, Will Ness <will_n48 <at> yahoo.com> wrote:
> > which is then just rewritable as:
> >
> > myCycle xs = ys where ys = foldr (:) ys xs
> >
> > or (arriving at the same result)
> >
> > myCycle xs = foldr (:) (myCycle xs) xs
> 
> Note that, because 'ys' only has to be calculated once, GHC makes the
> most efficient code for the former one. In the latter 'myCycle xs' has
> to be calculated each time 'xs' runs empty.
> 
> Here are some efficiency tests:
> 
> myCycle1 xs = ys where ys = foldr (:) ys xs
> myCycle2 xs = foldr (:) (myCycle2 xs) xs
> myCycle3 xs = ys where ys = xs ++ ys
> 
> main = do ns:ms:_ <- getArgs
>           print $ sum $ take (read ns) (myCycle1 [1..read ms])
> 

So you've discovered an inefficiency in GHC, which should probably improve on 
its deforestation skills. :)

"Smart" compiler would NOT allocate any extra storage in the three cases above, 
only altering the access to it. The meaning of all the three is the same - they 
are all rewritable one into another - it's a loop around on access past end.

The "very bright" compiler would just translate 

sum $ take 30000000 [1..1000]

into

sum [1..1000]*30000

producing the same result:

Prelude> sum [1..1000]*30000
15015000000


Cheers,




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

Message: 4
Date: Wed, 18 Mar 2009 21:35:50 +1100
From: Sean Lee <[email protected]>
Subject: Re: [Haskell-beginners] How to display a time difference?
To: Colin Paul Adams <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Colin

The following code probably would do what I wanted to do.

play_move :: IORef Game_state -> IO ()
play_move game_state_ior = do
 (_, state, _) <- readIORef game_state_ior
 putStr "Playing AI: "
 start_time <- getCurrentTime
 let move = recommended_move state
 end_time <- move `seq` (modifyIORef game_state_ior $!
update_interactive_from_move move) `seq` getCurrentTime
 putStrLn $ show $ (diffUTCTime end_time start_time)

Due to the non-strictness of Haskell, the evaluation of the
expressions between start_time and end_time is deferred until there is
a need.
By using `seq` and ($!), the strictness can be forced.


Sean

On Wed, Mar 18, 2009 at 9:28 PM, Colin Paul Adams
<[email protected]> wrote:
>>>>>> "Yitzchak" == Yitzchak Gale <[email protected]> writes:
>
>    Yitzchak> Colin Paul Adams wrote:
>    >> The code of the following routine is intended to indicate how
>    >> long it takes for the computer to make a move. However the time
>    >> is printed (as very close to zero) long before the move is
>    >> made.
>
>    Yitzchak> You are writing a thunk to the IORef. It only gets
>    Yitzchak> computed later on when you read the value.
>
>    Yitzchak> Try using:
>
>    >> readIORef game_state_ior >>= evaluate
>    >> . update_interactive_from_move move >>= writeIORef
>    >> game_state_ior
>
>    Yitzchak> evaluate is in Control.Exception.
>
> Still no joy with:
>
> play_move :: IORef Game_state -> IO ()
> play_move game_state_ior = do
>  (_, state, _) <- readIORef game_state_ior
>  putStr "Playing AI: "
>  start_time <- getCurrentTime
>  let move = recommended_move state
>  readIORef game_state_ior >>=
>    evaluate . update_interactive_from_move move >>=
>             writeIORef game_state_ior
>  end_time <- getCurrentTime
>  putStrLn $ show $ (diffUTCTime end_time start_time)
>
>
> --
> Colin Adams
> Preston Lancashire
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Sean Lee
PhD Student
Programming Language and Systems Research Group
School of Computer Science and Engineering
University of New South Wales
http://www.cse.unsw.edu.au/~seanl


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

Message: 5
Date: Wed, 18 Mar 2009 11:41:56 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] How to display a time difference?
To: Colin Paul Adams <[email protected]>, [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Mittwoch, 18. März 2009 11:28 schrieb Colin Paul Adams:
> >>>>> "Yitzchak" == Yitzchak Gale <[email protected]> writes:
>
>     Yitzchak> Colin Paul Adams wrote:
>     >> The code of the following routine is intended to indicate how
>     >> long it takes for the computer to make a move. However the time
>     >> is printed (as very close to zero) long before the move is
>     >> made.
>
>     Yitzchak> You are writing a thunk to the IORef. It only gets
>     Yitzchak> computed later on when you read the value.
>
>     Yitzchak> Try using:
>     >> readIORef game_state_ior >>= evaluate
>     >> . update_interactive_from_move move >>= writeIORef
>     >> game_state_ior
>
>     Yitzchak> evaluate is in Control.Exception.
>
> Still no joy with:
>
> play_move :: IORef Game_state -> IO ()
> play_move game_state_ior = do
>   (_, state, _) <- readIORef game_state_ior
>   putStr "Playing AI: "
>   start_time <- getCurrentTime
>   let move = recommended_move state
>   readIORef game_state_ior >>=
>     evaluate . update_interactive_from_move move >>=
>              writeIORef game_state_ior
>   end_time <- getCurrentTime
>   putStrLn $ show $ (diffUTCTime end_time start_time)

You would have to completely evaluate move. Depending on what type move is, 
that might require much more than what seq or evaluate do.

Perhaps

import Control.Parallel.Strategies

let move = ...
(rnf move) `seq` mofifyIORef (...)

? 



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

Message: 6
Date: Wed, 18 Mar 2009 10:45:26 +0000
From: Colin Paul Adams <[email protected]>
Subject: Re: [Haskell-beginners] How to display a time difference?
To: Sean Lee <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

>>>>> "Sean" == Sean Lee <[email protected]> writes:

    Sean> Hi Colin The following code probably would do what I wanted
    Sean> to do.

    Sean> play_move :: IORef Game_state -> IO () play_move
    Sean> game_state_ior = do (_, state, _) <- readIORef
    Sean> game_state_ior putStr "Playing AI: " start_time <-
    Sean> getCurrentTime let move = recommended_move state end_time <-
    Sean> move `seq` (modifyIORef game_state_ior $!
    Sean> update_interactive_from_move move) `seq` getCurrentTime
    Sean> putStrLn $ show $ (diffUTCTime end_time start_time)

    Sean> Due to the non-strictness of Haskell, the evaluation of the
    Sean> expressions between start_time and end_time is deferred
    Sean> until there is a need.  By using `seq` and ($!), the
    Sean> strictness can be forced.

It certainly causes a time delay, and a non-zero time is printed, but
the state doesn't get modified correctly now.

??

I will ponder Daniel's suggestion.
-- 
Colin Adams
Preston Lancashire


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

Message: 7
Date: Wed, 18 Mar 2009 21:51:17 +1100
From: Sean Lee <[email protected]>
Subject: Re: [Haskell-beginners] How to display a time difference?
To: Colin Paul Adams <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

> The following code probably would do what I wanted to do.

I mean "what you wanted to do." :)


-- 
Sean Lee
PhD Student
Programming Language and Systems Research Group
School of Computer Science and Engineering
University of New South Wales
http://www.cse.unsw.edu.au/~seanl


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

Message: 8
Date: Wed, 18 Mar 2009 10:53:33 +0000
From: Colin Paul Adams <[email protected]>
Subject: Re: [Haskell-beginners] How to display a time difference?
To: Sean Lee <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

>>>>> "Sean" == Sean Lee <[email protected]> writes:

    >> The following code probably would do what I wanted to do.
    Sean> I mean "what you wanted to do." :)

I guessed, but then, when it didn't, I decided you typed the right
thing in the first place :-)
-- 
Colin Adams
Preston Lancashire


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

Message: 9
Date: Wed, 18 Mar 2009 21:54:09 +1100
From: Sean Lee <[email protected]>
Subject: Re: [Haskell-beginners] How to display a time difference?
To: Colin Paul Adams <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

You get some random values? or no change at all on the state?

On Wed, Mar 18, 2009 at 9:45 PM, Colin Paul Adams
<[email protected]> wrote:
>>>>>> "Sean" == Sean Lee <[email protected]> writes:
>
>    Sean> Hi Colin The following code probably would do what I wanted
>    Sean> to do.
>
>    Sean> play_move :: IORef Game_state -> IO () play_move
>    Sean> game_state_ior = do (_, state, _) <- readIORef
>    Sean> game_state_ior putStr "Playing AI: " start_time <-
>    Sean> getCurrentTime let move = recommended_move state end_time <-
>    Sean> move `seq` (modifyIORef game_state_ior $!
>    Sean> update_interactive_from_move move) `seq` getCurrentTime
>    Sean> putStrLn $ show $ (diffUTCTime end_time start_time)
>
>    Sean> Due to the non-strictness of Haskell, the evaluation of the
>    Sean> expressions between start_time and end_time is deferred
>    Sean> until there is a need.  By using `seq` and ($!), the
>    Sean> strictness can be forced.
>
> It certainly causes a time delay, and a non-zero time is printed, but
> the state doesn't get modified correctly now.
>
> ??
>
> I will ponder Daniel's suggestion.
> --
> Colin Adams
> Preston Lancashire
>



-- 
Sean Lee
PhD Student
Programming Language and Systems Research Group
School of Computer Science and Engineering
University of New South Wales
http://www.cse.unsw.edu.au/~seanl


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

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


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

Reply via email to