Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  Re: Apparent bug (Ozgur Akgun)
   2. Re:  Simple Haskell daemon (Chadda? Fouch?)
   3.  Re: Simple Haskell daemon (Amy de Buitl?ir)
   4. Re:  GHCi bug? (Bastian Erdn??)
   5. Re:  Re: Simple Haskell daemon (Patrick LeBoutillier)
   6. Re:  Re: Apparent bug (Yitzchak Gale)
   7.  Re: Simple Haskell daemon (Amy de Buitl?ir)


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

Message: 1
Date: Tue, 16 Nov 2010 18:52:42 +0000
From: Ozgur Akgun <ozgurak...@gmail.com>
Subject: Re: [Haskell-beginners] Re: Apparent bug
To: russ.abb...@gmail.com
Cc: beginners <beginners@haskell.org>
Message-ID:
        <aanlktinm7otz_az=p1_qtqksfpk0pw32udatybo=6...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On 16 November 2010 18:40, Russ Abbott <russ.abb...@gmail.com> wrote:

> why isn't
>
> instance Show Test
>
> interpreted to mean
>
> data Test = Test deriving (Show)
>
> *
> -- Russ *
>

One of them is declaring an instance, the other is asking the compiler
derive an instance declaration for you. I don't know why the empty one is
interpreted to be an instance deriving. Maybe because in such a case there
would be no way to declare an empty instance?

In addition, there is yet another similar syntax. See stand-alone
deriving<http://haskell.org/ghc/docs/6.12.2/html/users_guide/deriving.html>
.

Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101116/6f2d803c/attachment-0001.html

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

Message: 2
Date: Tue, 16 Nov 2010 20:37:52 +0100
From: Chadda? Fouch? <chaddai.fou...@gmail.com>
Subject: Re: [Haskell-beginners] Simple Haskell daemon
To: Amy de Buitl?ir <a...@nualeargais.ie>
Cc: beginners@haskell.org
Message-ID:
        <aanlktimvxvhjqpyr0ckgxzbr6ordbtoj1df_uumiv...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Tue, Nov 16, 2010 at 2:18 AM, Amy de Buitléir <a...@nualeargais.ie> wrote:
> 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 ()

To use a typeclass there is pretty strange and doesn't have much
advantage, it would be much easier and nicer to just write start to
take some functions as parameters (why would there be only one
instance for each type...). In other words it works but is only a
perversion of the idea of typeclass without any practical advantages.
On the other hand if you had some strange pedagogical requirements,
feel free to ignore this !

-- 
Jedaï


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

Message: 3
Date: Tue, 16 Nov 2010 20:28:00 +0000 (UTC)
From: Amy de Buitl?ir <a...@nualeargais.ie>
Subject: [Haskell-beginners] Re: Simple Haskell daemon
To: beginners@haskell.org
Message-ID: <loom.20101116t212355-...@post.gmane.org>
Content-Type: text/plain; charset=utf-8

Chaddaï Fouché <chaddai.fouche <at> gmail.com> writes:

> 
> To use a typeclass there is pretty strange and doesn't have much
> advantage, it would be much easier and nicer to just write start to
> take some functions as parameters...
> 

Doh! Yes, I kind of overlooked the obvious there. Thank you so much! Here's my 
new version, in case anyone Googles this.

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

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

termReceived = unsafePerformIO (newMVar False)

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

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

start 
  -- | This function will be invoked when the daemon starts.
  :: IO a
  -- | This function will be invoked in the main loop.
  -> (a -> IO a)
  -- | This function will be invoked when the daemon shuts down.
  -> (a -> IO ())
  -- | The result will always be Nothing.
  -> IO (Maybe a)
start initialise work finalise = 
    installHandler sigTERM (Catch handleTERM) Nothing >> initialise >>= loop 
work finalise

----- CountingExample.hs -----

module CountingExample where

import Daemon

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 initialise work finalise




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

Message: 4
Date: Tue, 16 Nov 2010 21:37:25 +0100
From: Bastian Erdn?? <earth...@web.de>
Subject: Re: [Haskell-beginners] GHCi bug?
To: beginners@haskell.org
Message-ID: <c4895c8d-2caa-446d-8104-67cf7e8b6...@web.de>
Content-Type: text/plain; charset=us-ascii

I didn't test the OP's example.  But if I do

  let x = y ; y = x

in GHCi and then run 'x', i can interrupt it with ^C.

As I understand the bug is that you cannot interrupt the infinite loop in this 
special case.

Btw. I thought

  instance Show Test where -- empty body

should not work.  An instance with no declaration should be just

  instance Show Test -- no body

Maybe the EOF after the 'where' has something todo with this "bug".

 Bastian


On Nov 16, 2010, at 13:17, Thomas Davie wrote:

> To explain that further as to why it's not a bug...
> 
> GHCi's job is to run the code you enter.  You asked it to show a value, so it 
> executed the show function (correctly).  The default implementation called 
> showPrec, which invoked show, etc.
> 
> There may well be a missing feature here (to check of you implement nothing 
> other than the defaults which are very likely to loop), but not a bug.
> 
> Bob
> 
> On 16 Nov 2010, at 11:44, Ozgur Akgun wrote:
> 
>> Unfortunately, yes.
>> 
>> On 16 November 2010 11:39, Russ Abbott <russ.abb...@gmail.com> wrote:
>> I'm not sure what you are saying here. Are you saying that it's not a bug 
>> that GHCi went into a coma when I file was loaded and a prompt entered?
>> 
>> -- Russ
>> 
>> 
>> 
>> 
>> On Tue, Nov 16, 2010 at 3:34 AM, Ozgur Akgun <ozgurak...@gmail.com> wrote:
>> 
>> On 16 November 2010 05:12, Russ Abbott <russ.abb...@gmail.com> wrote:
>> I know the code isn't correct. My point is that the compiler didn't complain 
>> when the code was loaded, and the interpreter died when it was executed. 
>> That shouldn't happen.
>> 
>> -- Russ
>> 
>> It would be cool for GHC to make an analysis about the instance methods of a 
>> type class, and their default implementations to find the minimal subset of 
>> methods you have to implement to get a valid (for some definition of valid) 
>> instance declaration. But it doesn't do such a thing. And it is not an easy 
>> task.
>> 
>> GHC only warns you if you do not implement a method which doesn't have a 
>> default implementation. In your example, this is not the case.
>> 
>> Best,
>> 
>> -- 
>> Ozgur Akgun
>> 
>> 
>> 
>> 
>> -- 
>> Ozgur Akgun
>> _______________________________________________
>> Beginners mailing list
>> Beginners@haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
> 
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 5
Date: Tue, 16 Nov 2010 15:44:01 -0500
From: Patrick LeBoutillier <patrick.leboutill...@gmail.com>
Subject: Re: [Haskell-beginners] Re: Simple Haskell daemon
To: Amy de Buitl?ir <a...@nualeargais.ie>
Cc: beginners@haskell.org
Message-ID:
        <aanlktimwjmjn4lshedoxn8r6_qpag5xygbd92g86w...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Amy,

Here's a small suggestion:

If the start and loop functions always returns Nothing, I think it may
be cleaner to
simply make them return ().


Patrick


On Tue, Nov 16, 2010 at 3:28 PM, Amy de Buitléir <a...@nualeargais.ie> wrote:
> Chaddaï Fouché <chaddai.fouche <at> gmail.com> writes:
>
>>
>> To use a typeclass there is pretty strange and doesn't have much
>> advantage, it would be much easier and nicer to just write start to
>> take some functions as parameters...
>>
>
> Doh! Yes, I kind of overlooked the obvious there. Thank you so much! Here's my
> new version, in case anyone Googles this.
>
> ----- Daemon.hs -----
> module Daemon where
>
> import System.Exit
> import System.IO.Unsafe
> import System.Posix.Signals
> import Control.Concurrent
>
> termReceived = unsafePerformIO (newMVar False)
>
> handleTERM :: IO ()
> handleTERM = swapMVar termReceived True >> return ()
>
> loop :: (a -> IO a) -> (a -> IO ()) -> a -> IO (Maybe a)
> loop work finalise d = do
>  timeToStop <- readMVar termReceived
>  if timeToStop
>    then finalise d >> return Nothing
>    else work d >>= loop work finalise
>
> start
>  -- | This function will be invoked when the daemon starts.
>  :: IO a
>  -- | This function will be invoked in the main loop.
>  -> (a -> IO a)
>  -- | This function will be invoked when the daemon shuts down.
>  -> (a -> IO ())
>  -- | The result will always be Nothing.
>  -> IO (Maybe a)
> start initialise work finalise =
>    installHandler sigTERM (Catch handleTERM) Nothing >> initialise >>= loop
> work finalise
>
> ----- CountingExample.hs -----
>
> module CountingExample where
>
> import Daemon
>
> 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 initialise work finalise
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


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

Message: 6
Date: Tue, 16 Nov 2010 22:53:02 +0200
From: Yitzchak Gale <g...@sefer.org>
Subject: Re: [Haskell-beginners] Re: Apparent bug
To: "Edward Z. Yang" <ezy...@mit.edu>
Cc: "Russ.Abbott" <russ.abb...@gmail.com>, beginners
        <beginners@haskell.org>
Message-ID:
        <aanlkti=dvas+efzyvzbvqwxtlg2uzorcckl_px_+v...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Edward Z. Yang wrote:
> It seems to me that there is a possibility we could reify some information
> that is traditionally specified in the documentation: that is, what
> functions must be defined by a minimal instance, which could then give
> GHC enough information to give meaningful warnings if not all functions
> for a minimal instance are provided.

It definitely should not be so easy to create runtime crashes
by not providing enough method implementations at compile
time.

In this case, though, showsPrec and readsPrec only exist for
historical reasons. Nowadays I don't think anyone would
consider ReadS or ShowS for a parser or pretty-printer
so complex that it needs to consider operator precedence.
It would be better to deprecate them and remove them,
together with the default method implementations for
shows and reads.

Regards,
Yitz


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

Message: 7
Date: Tue, 16 Nov 2010 21:11:10 +0000 (UTC)
From: Amy de Buitl?ir <a...@nualeargais.ie>
Subject: [Haskell-beginners] Re: Simple Haskell daemon
To: beginners@haskell.org
Message-ID: <loom.20101116t220107-...@post.gmane.org>
Content-Type: text/plain; charset=us-ascii

Patrick LeBoutillier <patrick.leboutillier <at> gmail.com> writes:

> 
> Amy,
> 
> Here's a small suggestion:
> 
> If the start and loop functions always returns Nothing, I think it may
> be cleaner to
> simply make them return ().

Thank you Patrick, that make the interface easier for the user to understand.

The "loop" function does return something when it is called recursively, but 
the 
outer call never returns anything, so my first try was to have two separate 
functions.

----- code snippet -----
loop :: (a -> IO a) -> (a -> IO ()) -> a -> IO ()
loop work finalise d = loop' work finalise d >> return ()

loop' :: (a -> IO a) -> (a -> IO ()) -> a -> IO (Maybe a)
loop' work finalise d = do
  timeToStop <- readMVar termReceived
  if timeToStop
    then finalise d >> return Nothing
    else work d >>= loop' work finalise
----- end of code snippet -----

But then I thought that looked more complicated, so I went with this. 
(CountingExample.hs is unchanged.)

----- Daemon.hs -----

module Daemon where

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

termReceived = unsafePerformIO (newMVar False)

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

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

start 
  -- | This function will be invoked when the daemon starts.
  :: IO a
  -- | This function will be invoked in the main loop.
  -> (a -> IO a)
  -- | This function will be invoked when the daemon shuts down.
  -> (a -> IO ())
  -- | Returns nothing.
  -> IO ()
start initialise work finalise = 
    installHandler sigTERM (Catch handleTERM) Nothing 
        >> initialise 
        >>= loop work finalise 
        >> return ()



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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


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

Reply via email to