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. Identifying general patterns / typeclass instances
(Tim Cowlishaw)
2. Re: Further constraining types (Christopher Howard)
3. hslogger question (Manfred Lotz)
4. Re: Further constraining types (Thomas)
5. Re: Further constraining types (Christopher Howard)
6. Re: Further constraining types (Antoine Latter)
7. haddock instance documentation (Christopher Howard)
8. Re: hslogger question [Solved] (Manfred Lotz)
9. Re: haddock instance documentation (Antoine Latter)
10. Re: Further constraining types (Henk-Jan van Tuyl)
----------------------------------------------------------------------
Message: 1
Date: Sat, 6 Aug 2011 11:14:00 +0100
From: Tim Cowlishaw <[email protected]>
Subject: [Haskell-beginners] Identifying general patterns / typeclass
instances
To: [email protected]
Message-ID:
<camugue+go4zgtj5fafb_yzktwnx3xeix306xyatfuwfqueg...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi there all,
I've come up with the following sketch for a simple agent-based
simulation program in Haskell. However, I'd be interested in knowing
whether there are any obvious instances of more general typeclasses
hidden in there that I haven't recognised!
https://gist.github.com/1129216
Some explanation: A simulation is comprised of a starting state, and a
set of agents which take in a state and output an updated state. The
particular simulation I'm working on has very specific timing
semantics in that, for each 'step' of the simulation, all the agents
must see the same state (so they cannot simply be composed and applied
sequentially), and the resulting states must be applied to create the
next state in a well-defined order (hence the monoid restriction on
the SimState type). For this reason, I'm having trouble coming up with
a monad instance (which would seem to me to imply that the 'agent'
functions would be applied sequentially). However, are there any other
more general typeclasses that might offer useful abstractions in this
case?
In addition, the 'building up' of a simulation from an initial state
also looks to me like an anamorphism of some kind - is this correct?
Many thanks,
Tim
------------------------------
Message: 2
Date: Sat, 06 Aug 2011 11:39:40 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] Further constraining types
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 08/05/2011 01:10 PM, Brent Yorgey wrote:
>
> Sorry, I meant this is how you would do it in Agda. It is not
> possible to do this directly in Haskell (because Haskell does not have
> dependent types). (There are ways of "faking" some dependent types in
> Haskell with type-level-programming techniques... but they can be
> quite convoluted and would definitely be taking this email too far
> afield.)
>
{-
Supposing for now we set aside dependent types, I'm thinking that the
closest I get to my ideal is a combination of safe constructors and
law-style rewriting. I'm thinking this could be workable:
-}
module Natural ( Natural(), natural, (==), (+), (-), (*),
abs, signum, fromInteger ) where
data Natural = Natural Integer | Indeterminate deriving (Show)
-- fundamental constructor
natural :: Integer -> Natural
natural n | n < 0 = Indeterminate
natural n = Natural n
-- trying to fit it into the Num class
instance Eq Natural where
-- very controversial, but necessary to satisfy signum law
(==) Indeterminate Indeterminate = True
(==) Indeterminate _ = False
(==) _ Indeterminate = False
(==) (Natural a) (Natural b) | a == b = True
| otherwise = False
instance Num Natural where
Indeterminate + _ = Indeterminate
_ + Indeterminate = Indeterminate
(Natural a) + (Natural b) = Natural ((Prelude.+) a b)
Indeterminate - _ = Indeterminate
_ - Indeterminate = Indeterminate
(Natural a) - (Natural b) | a < b = Indeterminate
| otherwise = Natural ((Prelude.-) a b)
Indeterminate * _ = Indeterminate
_ * Indeterminate = Indeterminate
(Natural a) * (Natural b) | a < 0 || b < 0 = Indeterminate
| otherwise = Natural ((Prelude.*) a b)
abs Indeterminate = Indeterminate
abs (Natural a) = (Natural a)
signum Indeterminate = Indeterminate
signum (Natural 0) = (Natural 0)
signum (Natural a) = (Natural 1)
fromInteger n = natural n
{-
Fundamentally, this is not really much different than using a
constructor that returns a Maybe value. But it does gives me a much more
"natural" type syntax, while still allowing my calculations to error-out
without bottoms or crashes.
Thoughts?
-}
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 3
Date: Sat, 6 Aug 2011 21:43:17 +0200
From: Manfred Lotz <[email protected]>
Subject: [Haskell-beginners] hslogger question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Hi there,
Using hslogger I want to get
- error messages and above to stdout and to logfile
- debug messages and above to logfile only.
Here is what I tried:
import System.Log.Logger
import System.Log.Handler.Simple
import System.Log.Handler (setFormatter)
import System.Log.Formatter
-- By default, all messages of level WARNING and above are sent to
stderr. -- Everything else is ignored.
main :: IO ()
main = do
-- all messages of level ERROR and above to stderr.
updateGlobalLogger rootLoggerName (setLevel ERROR)
h <- fileHandler "debug.log" DEBUG >>= \lh -> return $
setFormatter lh (simpleLogFormatter "[$time :
$loggername : $prio] $msg") updateGlobalLogger rootLoggerName
(addHandler h)
errorM "MyApp.Init" "This is a bad error"
-- This message will go to logfile only.
debugM "MyApp.Main" "This buggy component is buggy"
-- This message will go to logfile only.
warningM "MyApp.Finish" "Still Buggy"
errorM goes to stderr and logfile as expected. However, debugM and
warningM are not to be seen at all.
What am I doing wrong?
--
Manfred
------------------------------
Message: 4
Date: Sat, 06 Aug 2011 22:54:35 +0200
From: Thomas <[email protected]>
Subject: Re: [Haskell-beginners] Further constraining types
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> (==) (Natural a) (Natural b) | a == b = True
> | otherwise = False
Essentially the same, but I usually write this a little shorter as
(==) (Natural a) (Natural b) = a == b
> (Natural a) * (Natural b) | a < 0 || b < 0 = Indeterminate
> | otherwise = Natural ((Prelude.*) a b)
Why this? A 'Natural x' should already guarantee that 'x' is not
negative (otherwise it would be 'Indeterminate').
> Thoughts?
It seems very reasonable to me.
Regards,
Thomas
------------------------------
Message: 5
Date: Sat, 06 Aug 2011 21:54:29 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] Further constraining types
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 08/06/2011 12:54 PM, Thomas wrote:
>
>> (==) (Natural a) (Natural b) | a == b = True
>> | otherwise = False
>
> Essentially the same, but I usually write this a little shorter as
> (==) (Natural a) (Natural b) = a == b
>
>> (Natural a) * (Natural b) | a < 0 || b < 0 = Indeterminate
>> | otherwise = Natural ((Prelude.*) a b)
>
> Why this? A 'Natural x' should already guarantee that 'x' is not
> negative (otherwise it would be 'Indeterminate').
>
>> Thoughts?
>
> It seems very reasonable to me.
>
> Regards,
> Thomas
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
One more quick question: If I hide my actual constructors in favor of a
smart constructor, is it no longer possible for me to do direct pattern
matching on the values? E.g., the compiler does not allow this:
analyze (Natural 5) = "It's a five!!!"
...so I have to do this:
analyze a | natural 5 = "It's a five!!!"
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 6
Date: Sun, 7 Aug 2011 01:08:31 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Further constraining types
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<CAKjSnQFu-mqgTvWpZUVfs4ED8ix6c6o-yo=cujhkkzy4+ab...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Sun, Aug 7, 2011 at 12:54 AM, Christopher Howard
>
> One more quick question: If I hide my actual constructors in favor of a
> smart constructor, is it no longer possible for me to do direct pattern
> matching on the values? E.g., the compiler does not allow this:
>
> analyze (Natural 5) = "It's a five!!!"
>
> ...so I have to do this:
>
> analyze a | natural 5 = "It's a five!!!"
You can use non-standard GHC extensions:
Given a function:
someView :: Natural a -> a
you can pattern match like so:
case myNat of
(someView -> 5) -> "It's a five!!!"
This requires {-# LANGUAGE ViewPatterns #-} at the top of your source
file to enable.
Antoine
>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 7
Date: Sat, 06 Aug 2011 23:03:01 -0800
From: Christopher Howard <[email protected]>
Subject: [Haskell-beginners] haddock instance documentation
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I'm learning to comment my code for haddock. However, haddock seems to
be ignoring my documentation comments for instance implementations.
Instead, it just uses the default explanation given in the original
class definition. Does this require some kind of special syntax?
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 8
Date: Sun, 7 Aug 2011 09:32:22 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] hslogger question [Solved]
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
On Sat, 6 Aug 2011 21:43:17 +0200
Manfred Lotz <[email protected]> wrote:
> Hi there,
> Using hslogger I want to get
> - error messages and above to stdout and to logfile
> - debug messages and above to logfile only.
>
>
It seems the stderr handler must be redefined.
Solved now by coding like this:
import System.IO
import System.Log.Logger
import System.Log.Handler.Simple
import System.Log.Handler (setFormatter)
import System.Log.Formatter
-- By default, all messages of level WARNING and above are sent to
stderr. -- Everything else is ignored.
main :: IO ()
main = do
-- set global level to DEBUG
updateGlobalLogger rootLoggerName (setLevel DEBUG)
-- want only ERROR and above to stderr
s <- streamHandler stderr ERROR
-- want all log messages to logfile
h <- fileHandler "debug.log" DEBUG >>= \lh -> return $
setFormatter lh (simpleLogFormatter "[$time :
$loggername : $prio] $msg")
-- set handlers completely new
updateGlobalLogger rootLoggerName (setHandlers [s,h])
errorM "MyApp.Init" "This is a bad error"
-- This message will go to logfile only.
debugM "MyApp.Main" "This buggy component is buggy"
-- This message will go to logfile only.
warningM "MyApp.Finish" "Still Buggy"
--
Manfred
------------------------------
Message: 9
Date: Sun, 7 Aug 2011 02:37:20 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] haddock instance documentation
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<CAKjSnQEi8r4hKuwz9HVnybi4Xh0-Z3+c==wicuxu+yhcujv...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Sun, Aug 7, 2011 at 2:03 AM, Christopher Howard
<[email protected]> wrote:
> I'm learning to comment my code for haddock. However, haddock seems to be
> ignoring my documentation comments for instance implementations. Instead, it
> just uses the default explanation given in the original class definition.
> Does this require some kind of special syntax?
Can you give an example of what you are doing which doesn't do what
you expect? I've had no trouble with this using Haddock 2.9.2.
Antoine
>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 10
Date: Sun, 07 Aug 2011 11:36:58 +0200
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] Further constraining types
To: "Christopher Howard" <[email protected]>, "Antoine
Latter" <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
delsp=yes
On Sun, 07 Aug 2011 08:08:31 +0200, Antoine Latter <[email protected]>
wrote:
>
> You can use non-standard GHC extensions:
>
> Given a function:
>
> someView :: Natural a -> a
>
> you can pattern match like so:
>
> case myNat of
> (someView -> 5) -> "It's a five!!!"
>
> This requires {-# LANGUAGE ViewPatterns #-} at the top of your source
> file to enable.
Not using any extension:
case fromNatural myNat of
5 -> "It's a five!!!"
The function fromNatural is, of course, imported from the module defining
Natural.
Regards,
Henk-Jan van Tuyl
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 38, Issue 11
*****************************************