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. What should I use (the equivalent of Windows schtasks) to
schedule a task to run? (Hong Yang)
2. Re: overflow safe Word types (Maciej Piechotka)
3. Mixing monads? (Ron Leisti)
4. Re: Mixing monads? (Stephen Tetley)
5. Re: Mixing monads? (Stephen Tetley)
6. Re: Mixing monads? (Felipe Lessa)
7. [OT] To the Google Group 'fa.haskell' admin. (aditya siram)
8. Addable function combinators (edgar klerks)
----------------------------------------------------------------------
Message: 1
Date: Fri, 26 Mar 2010 23:29:42 -0500
From: Hong Yang <[email protected]>
Subject: [Haskell-beginners] What should I use (the equivalent of
Windows schtasks) to schedule a task to run?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
I searched "schedul" on hayoo!, but had no clue. It seems that I can use
scheduleEventAt from Control.Monad.Event.Classes. But what is type "e"? It
seems like an event. How do I define an event?
I have not learned Monad yet. Can some one give me a simple example? Suppose
I want to run an external program "pg" which in Haskell's language has the
type of String -> IO ().
Thanks,
Hong
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100326/5b2b91ac/attachment-0001.html
------------------------------
Message: 2
Date: Sat, 27 Mar 2010 23:44:34 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: overflow safe Word types
To: [email protected]
Message-ID: <1269733474.11263.40.ca...@picard>
Content-Type: text/plain; charset="utf-8"
On Fri, 2010-03-26 at 14:23 -0400, Ashish Agarwal wrote:
> Is there an alternative implementation of the types Word8, Word16,
> etc. that disallow overflow? For example, currently:
>
>
> Prelude Word> (fromInteger 256) :: Word8
> 0
>
>
> I'd like a type whose constructors disallow this.
>
>
safeConvert :: (Integral a, Integral b) :: a -> Maybe b
safeConvert x | fromIntegral y == x = Just $! y
| otherwise = Nothing
where y = fromIntegral x
You can write:
newtype Safe a = Safe a deriving Eq
instance Show a => Show (Safe a) where
show (Safe x) = show x
instance Integral a => Num (Safe a) where
(Safe x) + (Safe y) = Safe $! x + y
(Safe x) * (Safe y) = Safe $! x * y
(Safe x) - (Safe y) = Safe $! x - y
negate (Safe y) = Safe $! negate y
abs (Safe y) = Safe $! abs y
signum (Safe y) = Safe $! signum y
fromInteger x =
Safe $! fromMaybe (error "overflow") (safeConvert x)
...
if you want
Regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100327/b0128f30/attachment-0001.bin
------------------------------
Message: 3
Date: Sun, 28 Mar 2010 11:41:24 -0400
From: Ron Leisti <[email protected]>
Subject: [Haskell-beginners] Mixing monads?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I've run into a situation where I need to 'weave' two monads together,
but I can't figure out how it's done.
The first monad is "Parser a" from Attoparsec, and the second is one
that I've created called "CBC a", with the following relevant
functions:
-- Decrypts a word in cipher-block-chaining mode
decryptCBC :: Word128 -> CBC Word128
-- Performs a cipher-block-chaining mode operation with the given
-- key and initialization vector.
cbc :: Key -> IV -> CBC a -> a
I've also defined a special parser for Attoparsec, for extracting the
next 128 bit word from the stream:
word128 :: Parser Word128
My problem is that I would like to write a function which reads a
word, decrypts it, and then uses the result to make a decision about
whether to read further words or return a result.
So far, I think my function would have a result type of:
func :: Parser (CBC foo)
but within the function, if I do something like:
func = do
x <- word128
let r = do
y <- decryptCBC x
if (predicate y)
then return bar
else -- somehow read more from the parser?
return r
But within this function I run into the situation where my types turn
into something like Parser (CBC (Parser a)).
I have a feeling that I'm just going about this in a wrong way. I
realize that I probably don't need to use Parser here, since I'm just
reading a stream of words, but I'd still like to learn how to do this
in the general case. Is there anyone that could point me in a better
direction?
Thanks,
Ron
------------------------------
Message: 4
Date: Sun, 28 Mar 2010 18:03:04 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Mixing monads?
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Ron
Generally you would want a monad transformer for combining the effects
of of two monads - one monad remains as it is, the other is modified
slightly to make it a "transformer" rather than a regular monad.
It looks like Attoparsec doesn't supply a transformer instance so that
obliges you to make CBC a transformer instead - prosaically this means
making CBC an instance of monad transformer class Trans and extending
the type of CBC to be something like "CbcT m a" rather than "CBC a" so
the monad it transforms can be supplied as a parameter "m".
As Attoparsec isn't a transformer you will have to "lift" Attoparsec
operations into the CbcT monad so func will look something like this -
note the change on the second line:
func = do
x <- lift $ word128
let r = do
y <- decryptCBC x
if (predicate y)
then return bar
else -- somehow read more from the parser?
return r
Best wishes
Stephen
------------------------------
Message: 5
Date: Sun, 28 Mar 2010 19:06:52 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Mixing monads?
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hello Ron
Further, here's a code sketch that shows roughly what you will need to do.
I've stubbed the CBC monad and CbcT transformer to be the Identity
monad (with different names) - the Identity Monad is the simplest
monad.
-- Code follows:
-- Stubbing CBC just as a warm up and so you can see the
-- difference between the monad and monad transformer
-- versions:
newtype CBC a = CBC { getCBC :: a }
-- The bind (>>=) and return of the Identity monad.
--
instance Monad CBC where
return a = CBC a
(CBC a) >>= mf = mf a
func :: Parser (CBC Word128)
func = do
x <- word128
let r = do
y <- decryptCBC x
if (y == 255)
then return 0
else undefined -- somehow read more from the parser?
return r
--- With a transformer:
-- Make a transformer version of CBC:
--
-- For simplicity again I'm stubbing the monad as the Identity
-- monad transformer.
--
newtype CbcT m a = CbcT { getCbcT :: m a }
-- Note the instance of Monad for the transformer can use (>>=)
-- and return from the base monad.
--
-- The bind (>>=) and return here are equivalent to (>>=) and
-- return of the Identity monad.
--
instance Monad m => Monad (CbcT m) where
return a = CbcT (return a)
(CbcT ma) >>= mf = CbcT $ ma >>= (getCbcT . mf)
-- Make a type synonym for the combination of the CbcT
-- transformer and the base monad Parser.
--
type CbcParser a = CbcT Parser a
-- Make an instance of lift.
--
-- With a more powerfully monad than Identity - lift will
-- be more complicated.
--
-- Often, if the monad is based on a stack of transformers
-- lift is in the form
--
-- > lift = MyMonadT . lift . lift
--
--
instance MonadTrans CbcT where
lift = CbcT
-- Again, a stub of the decryptCBC function.
--
-- Note the type signature here is more general than
-- necessary allowing it to be used with any base monad
-- not just Parser.
--
decryptCBC_T :: Monad m => Word128 -> CbcT m Word128
decryptCBC_T x = return (x-1) -- just a dummy
-- The "func" function again.
--
-- Because we are using the combined monad CbcParser
-- (aka CbcT <transformer> + Parser <base>), we don't need
-- the "let r = ..." code that previously "ran" the CBC monad.
--
func_T :: CbcParser Word16
func_T = do
x <- lift $ word128
y <- decryptCBC_T x
if (y == 255)
then return 0
else (lift $ word128) -- silly idea,
-- just return next Word128
------------------------------
Message: 6
Date: Sun, 28 Mar 2010 15:43:48 -0300
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Mixing monads?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sun, Mar 28, 2010 at 06:03:04PM +0100, Stephen Tetley wrote:
> It looks like Attoparsec doesn't supply a transformer instance so that
> obliges you to make CBC a transformer instead
I should note that in general monad transoformers do not commute.
That is,
ParserT CBC a
and
CbcT Parser a
may not mean the same thing. For example, suppose you get a
parse failure and you need to backtrack. Do you want to maintain
the same CBC state, or do you want to backtrack the CBC state as
well? I'd guess the latter, so probably having a CbcT won't help
you, I think.
--
Felipe.
------------------------------
Message: 7
Date: Sun, 28 Mar 2010 14:53:41 -0500
From: aditya siram <[email protected]>
Subject: [Haskell-beginners] [OT] To the Google Group 'fa.haskell'
admin.
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I noticed that my posts to the 'fa.haskell' Google group don't make it
to the Haskell-Cafe archives and I suspect they are invisible to the
Haskell-Cafe readers. They seem to be visible to only those who are
subscribe to the Google Group.
Conversely some of the other people's posts to Haskell-Cafe don't
appear in the Google group.
For instance I posted to this thread:
http://www.haskell.org/pipermail/haskell-cafe/2010-March/075250.html
and it is not in the Haskell-Cafe archives.
And this response by Jeff Wheeler:
http://www.haskell.org/pipermail/haskell-cafe/2010-March/075251.html
is not visible in the Google Group.
As of now I'll stop using the Google group, but I wanted to let someone know.
Thanks ...
-deech
------------------------------
Message: 8
Date: Mon, 29 Mar 2010 02:04:54 +0200
From: edgar klerks <[email protected]>
Subject: [Haskell-beginners] Addable function combinators
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
He All,
I was toying a bit around and found a function, which combines an function
of arity 1 with 2.
Then I produced a whole sequence of these function, the number of * stands
for the arity of the left function and the number of | stands for arity the
right function. so <*|||> :: (b -> c -> d) -> (a -> c) -> a -> b -> d:
(<*****|>) = flip (<*|||||>)
(<****|>) = flip (<*||||>)
(<***|>) = flip (<*|||>)
(<**|>) = flip (<*||>)
(<*|>) = (.)
(<*||>) = (<*|>).(<*|>)
(<*|||>) =(<*||>).(<*|>)
(<*||||>) = (<*||>).(<*||>)
(<*|||||>) = (<*||||>).(<*|>)
con = (flip.) .(.)
(<**||>) = (<*||>).flip
(<**|||>) = (<*|>).(<**||>)
(<**||||>) = (<*|>).(<**|||>)
(<***||>) = (<*||>).(con flip)
(<***|||>) = (<*|>).(<***||>)
(<****||>) = (<*||>).(con.con $ flip)
I found it interesting, but now I am stuck. is it possible to make the stars
addable instead of the | or both?
Greets,
Edgar
Edgar
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100328/47165260/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 29
*****************************************