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: A basic misunderstanding of how to program with IO
(Thomas Davie)
2. Re: A basic misunderstanding of how to program with IO
(Ozgur Akgun)
3. RE: A basic misunderstanding of how to program with IO
(Edward Z. Yang)
4. comprehending monad execution ([email protected])
----------------------------------------------------------------------
Message: 1
Date: Sat, 8 May 2010 17:44:35 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] A basic misunderstanding of how to
program with IO
To: Ozgur Akgun <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On 8 May 2010, at 17:09, Ozgur Akgun wrote:
> I might have misunderstood you, but what about using the existing interact
> function:
> http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Prelude.html#v:interact
>
> And for your interact, since it is not in the IO monad, you cannot write such
> a function. [ don't tell him about the unsafePerformIO :) ]
Uh, interact absolutely *is* in the IO monad, and is totally implementable
without unsafePerformIO, like this:
interact f = putStr =<< f <$> getContents
Bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100508/d70fb13c/attachment-0001.html
------------------------------
Message: 2
Date: Sat, 8 May 2010 17:56:38 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] A basic misunderstanding of how to
program with IO
To: Thomas Davie <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
On 8 May 2010 17:44, Thomas Davie <[email protected]> wrote:
>
> On 8 May 2010, at 17:09, Ozgur Akgun wrote:
>
> I might have misunderstood you, but what about using the existing interact
> function:
> http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Prelude.html#v:interact
>
> And for your interact, since it is not in the IO monad, you cannot write
> such a function. [ don't tell him about the unsafePerformIO :) ]
>
>
> Uh, interact absolutely *is* in the IO monad, and is totally implementable
> without unsafePerformIO, like this:
>
>
Upps now I need to clarify myself I guess.
I said "your interact is not in the IO monad", because the interact OP
defined had the type:
interact :: String -> Resp
Which obviously is not in the IO monad, if he is not hiding something in
Resp (looks unlikely to me)
> interact f = putStr =<< f <$> getContents
>
> Bob
>
And of course, interact is something implementable :)
Best,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100508/4502557a/attachment-0001.html
------------------------------
Message: 3
Date: Sat, 08 May 2010 15:24:38 -0400
From: "Edward Z. Yang" <[email protected]>
Subject: RE: [Haskell-beginners] A basic misunderstanding of how to
program with IO
To: Ken Overton <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <1273346563-sup...@ezyang>
Content-Type: text/plain; charset=UTF-8
Excerpts from Ken Overton's message of Sat May 08 12:20:12 -0400 2010:
> Thanks; I guess my 'problem' is that I naturally gravitate to a function that
> returns *the result of the interaction* rather than IO, but that seems
> impossible anywhere IO is used.
That's correct; there's no (normal) way to write a function IO a -> a.
> So looking at the 'real' interact function, I guess the Haskell way is to
> pass in my function that acts on the data that was read. I guess that makes
> sense as then I can separate all my 'action' code from IO code. Is that a
> reasonable understanding?
That is also correct. Notice the types inside your do-block:
do
rsp <- getLine
getLine is of type IO String, but rsp is of type String! As long as we're in
a do-block (which "keeps" us in the IO monad), we can temporarily extract
the real value without IO to pass to a pure function.
Cheers,
Edward
------------------------------
Message: 4
Date: Sat, 08 May 2010 22:18:44 +0000
From: [email protected]
Subject: [Haskell-beginners] comprehending monad execution
To: [email protected]
Message-ID: <w551212790219091273357...@webmail44>
Content-Type: text/plain; charset="utf-8"
All:
I've been analyzing monads for a while now and have a question on execution.
In the snippet below the ghc debugger shows that in just one single step
Discriminate has been substituted for g. Even with :trace turned on in the
debugger there is no evidence of an execution step to carry out the
substitution which leads me to assume a compile time operation identifies
Discriminate as a substitute for g. Could someone correct my assumption or
explain how this works?
Note: this snippet was extracted from [1].
> {-# LANGUAGE EmptyDataDecls, Rank2Types #-}
> module LEM where
> import Control.Monad.Identity
>
> data F -- no constructors
>
> data Discriminate a b = L a | R b
> instance Monad (Discriminate e) where
> return = R
> R x >>= f = f x
> L x >>= f = L x
>
> h :: (forall m. Monad m => ((a -> m F) -> m F)) -> a
> h g = case g (\x -> L x) of
> R x -> error "Not reachable"
> L x -> x
>
> -- A proof term for a -> NOT NOT a
> lift :: forall a m. Monad m => a -> ((a -> m F) -> m F)
> lift x = \k -> k x
>
> t = h $ lift True
>
ghci trace ...
C:\haskell\lem>ghci lem.lhs
GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
[1 of 1] Compiling LEM ( lem.lhs, interpreted )
Ok, modules loaded: LEM.
*LEM> :b h
Breakpoint 0 activated at lem.lhs:(14,2)-(16,17)
*LEM> :trace t
Loading package mtl-1.1.0.1 ... linking ... done.
Stopped at lem.lhs:(14,2)-(16,17)
_result :: a = _
[lem.lhs:(14,2)-(16,17)] *LEM> :step
Stopped at lem.lhs:(14,8)-(16,17)
_result :: a = _
g :: (a -> Discriminate a F) -> Discriminate a F = _
[lem.lhs:(14,8)-(16,17)] *LEM> :hist
-1 : h (lem.lhs:(14,2)-(16,17))
-2 : t (lem.lhs:22:6-18)
<end of history>
[lem.lhs:(14,8)-(16,17)] *LEM>
My previous analysis includes running single step execution on Wadler's I monad
here [2], where I am able to observe execution of the I monad.
[essence.lhs:32:33-37] *Essence> :hist
-1 : unitI (essence.lhs:7:34)
-2 : unitI (essence.lhs:7:2-34)
-3 : interp (essence.lhs:32:26-38)
-4 : interp (essence.lhs:(31,2)-(39,44))
-5 : test (essence.lhs:54:41-51)
-6 : showval (essence.lhs:(26,2)-(28,45))
-7 : showI (essence.lhs:9:34-42)
-8 : showI (essence.lhs:9:2-42)
-9 : test (essence.lhs:54:34-52)
-10 : test (essence.lhs:54:2-52)
<end of history>
[1] http://okmij.org/ftp/Computation/lem.html
[2] http://homepages.inf.ed.ac.uk/wadler/papers/essence/essence.ps
Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100508/70d1f80c/attachment-0001.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 23, Issue 11
*****************************************