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: Re: Re: testing and the culture of Haskell (Daniel Fischer)
2. different function implementations for different values of
typevar - is it possible? (Sergey Mironov)
3. Re: Re: Re: testing and the culture of Haskell
(Brandon S. Allbery KF8NH)
4. Re: different function implementations for different values
of typevar - is it possible? (Stephen Tetley)
5. Re: QuickCheck 2 entering infinite loop (Brent Yorgey)
6. Re: different function implementations for different values
of typevar - is it possible? (Sergey Mironov)
7. Re: different function implementations for different values
of typevar - is it possible? (Stephen Tetley)
8. Re: Re: Re: testing and the culture of Haskell (Isaac Dupree)
9. adding state handing to existing code (Scott Thoman)
----------------------------------------------------------------------
Message: 1
Date: Sat, 23 Jan 2010 22:08:14 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: Re: testing and the culture of
Haskell
To: [email protected]
Cc: Adrian Adshead <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Samstag 23 Januar 2010 21:31:47 schrieb Adrian Adshead:
> Hi Alex,
>
> That does help, but raises a further question.
>
> If (>>=) :: m a -> (a -> m b) -> m b
>
> Then whenever the IO monad is entered it can not be escaped,
> so in the example you gave:-
>
> f :: IO a -> Int
> f _ = 10
>
>
> while f (putStrLn "foo") is a pure function evaluating to 10,
> neither the IO Action, nor the a (from 'IO a') can be used without
> using the (>>=) or (>>). Therefore since the parameter can not
> be used in the function f without entering the IO monad, your
> function would be equivalent to :-
>
> f :: Int
> f = 10
Rather to
const 10
except with a restricted type.
>
> I have two questions to try and clarify my understanding.
>
> 1) Is it possible to have a function with an IO Action as an input
> parameter (and not just ignore the parameter) and have a non-IO
> return value?
> (ie is    f :: IO a -> b   possible without ignoring the IO a)
Excluding unsafePerformIO, only in a very weak sense.
ioseq :: IO a -> b -> b
ioseq ioa b = ioa `seq` b
doesn't quite ignore the parameter, but apart from that, I only see
rustyBallpointTracheotomy (Kydos to Brent Yorgey for that :).
>
> 2) Is it possible to have a function which evaluates any IO Action
> within the body and have a non-IO return value?
> (ie is   f :: a -> b    possible while evaluating any IO Action
> within the body.)
>
Assuming by evaluating you mean executing and not
(return 5 >> return 6) ~> IO (\s -> (s,6))
, no, that's not possible.
>
>
> My understanding of Haskell was that both of these situations
> would not be possible, and the return value would always be
> of the form IO b. (For these questions I am excluding use of
> unsafePerformIO)
>
> Many thanks for your time.
>
> Adrian.
------------------------------
Message: 2
Date: Sun, 24 Jan 2010 00:17:20 +0300
From: Sergey Mironov <[email protected]>
Subject: [Haskell-beginners] different function implementations for
different values of typevar - is it possible?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi all.
I am writing a Monad intace for my own Writer-like data type
"HtmlWriter" (see code below).
The main HtmlWriter's difference is in return() function. I'd like it
to convert user data (a) to html.
But here come troubles. In general, there are two cases:
1) (HTML a) exists
2) (HTML a) doesn't exist
So, i need to write different implementations of return() for both
cases. Something like this:
1) return a = HtmlWriter (a, toHtml a)
2) return a = HtmlWriter (a, toHtml (show a))
I can't find a way to do it. Is it possible somehow? Please, help.
Here is the code:
-------------
import Control.Monad
import Text.XHtml.Transitional
newtype HtmlWriter a = HtmlWriter { runHtmlWriter :: (a, Html) }
instance Monad (HtmlWriter) where
return a = HtmlWriter (a, toHtml a)
-- ??? return a = HtmlWriter (a, toHtml (show a))
m >>= k = HtmlWriter $ let
(a, w) = runHtmlWriter m
(b, w') = runHtmlWriter (k a)
in (b, w `mappend` w')
-------
Thanks, Sergey.
------------------------------
Message: 3
Date: Sat, 23 Jan 2010 16:21:31 -0500
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Re: Re: testing and the culture of
Haskell
To: Daniel Fischer <[email protected]>
Cc: Adrian Adshead <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On Jan 23, 2010, at 16:08 , Daniel Fischer wrote:
> Am Samstag 23 Januar 2010 21:31:47 schrieb Adrian Adshead:
>> I have two questions to try and clarify my understanding.
>>
>> 1) Is it possible to have a function with an IO Action as an input
>> parameter (and not just ignore the parameter) and have a non-IO
>> return value?
>> (ie is f :: IO a -> b possible without ignoring the IO a)
>
> Excluding unsafePerformIO, only in a very weak sense.
And, while you could do the dance to make unsafePerformIO available
and use it, the aggressive optimizations the compiler makes on pure
code can lead to extremely unexpected results.
>> 2) Is it possible to have a function which evaluates any IO Action
>> within the body and have a non-IO return value?
>> (ie is f :: a -> b possible while evaluating any IO Action
>> within the body.)
>
> Assuming by evaluating you mean executing and not
>
> (return 5 >> return 6) ~> IO (\s -> (s,6))
>
> , no, that's not possible.
Again, barring unsafePerformIO, and that will bite you if you use it
without knowing exactly what you're doing and why and *forcing* the
compiler to agree with you.
(As it turns out, you can often figure out the implementation details
of your chosen compiler's IO and force the issue. But any code that
do so *is* reimplementing unsafePerformIO, and you will pay dearly.)
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100123/ad5e0dc8/PGP-0001.bin
------------------------------
Message: 4
Date: Sat, 23 Jan 2010 22:11:50 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] different function implementations
for different values of typevar - is it possible?
To: Sergey Mironov <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Sergey
Are you sure you always want return to write as well?
'return' for the standard' MTL writer monad produces _a_ within the
monad, but does no "writing":
instance (Monoid w) => Monad (Writer w) where
return a = Writer (a, mempty)
m >>= k = Writer $ let
(a, w) = runWriter m
(b, w') = runWriter (k a)
in (b, w `mappend` w')
Text.XHtml.Transitional has the noHtml function which would be the
analogue to mempty.
Best wishes
Stephen
------------------------------
Message: 5
Date: Sat, 23 Jan 2010 17:38:23 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] QuickCheck 2 entering infinite loop
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Fri, Jan 22, 2010 at 04:47:14PM +0000, Dominic Mulligan wrote:
> Dear Brent,
>
> Thanks for the reply. The type of args is [Type a]. Ascribing a specific
> type, like so:
>
> prop_InjectTypeConFreeVars :: [Type String] -> Bool
> prop_InjectTypeConFreeVars args =
> (freeTypeVars . (tyCon "Fun")) args == (concatMap freeTypeVars args)
>
> Leads to the same problem; quickCheck is brought to its knees after about 6
> tests.
I suspect what is happening here is that 'Type' is recursive and
QuickCheck is going crazy generating huge deeply nested random types.
This is rather a failing of QuickCheck. For the current state of the
art in QuickCheck for recursive data types, see "Generating Recursive
Data Types" here:
http://www.cs.chalmers.se/~rjmh/QuickCheck/manual.html
although even this leaves a lot to be desired. There is actually
ongoing research (by me and others) into improving the situation.
-Brent
------------------------------
Message: 6
Date: Sun, 24 Jan 2010 01:42:28 +0300
From: Sergey Mironov <[email protected]>
Subject: Re: [Haskell-beginners] different function implementations
for different values of typevar - is it possible?
To: Stephen Tetley <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
2010/1/24 Stephen Tetley <[email protected]>:
> Hi Sergey
>
> Are you sure you always want return to write as well?
>
> 'return' for the standard' MTL writer monad produces _a_ within the
> monad, but does no "writing":
>
> instance (Monoid w) => Monad (Writer w) where
> return a = Writer (a, mempty)
> m >>= k = Writer $ let
> (a, w) = runWriter m
> (b, w') = runWriter (k a)
> in (b, w `mappend` w')
>
> Text.XHtml.Transitional has the noHtml function which would be the
> analogue to mempty.
>
> Best wishes
>
> Stephen
>
Mmm, yes.. it is not what i want actually. Seems, that my
return-with-writing will break monad laws from "Real world Haskell".
Now its clear, that i should use simple Writer Html (). Now code looks like:
nested :: (Html -> Html) -> (Writer Html ()) -> Html
nested a x = a << ( execWriter $ x )
buildPage :: Writer Html ()
buildPage = do
tell $ header `nested` do
tell $ meta ! [ strAttr "http-equiv" "Content-Type", strAttr
"content" "text/html", strAttr "http-equiv" "text/html; charset=UTF-8"
]
tell $ thetitle << "PageTitle"
tell $ css_link ! [ href "css/blueprint/screen.css", media
"screen, projection"]
tell $ css_link ! [ href "css/blueprint/print.css", media "print"]
tell $ css_link ! [ href "css/custom.css" ]
tell $ body `nested` do
tell $ noHtml
-- ... and so on
Thanks for warnings!
--
Sergey
------------------------------
Message: 7
Date: Sat, 23 Jan 2010 22:42:38 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] different function implementations
for different values of typevar - is it possible?
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Sergey
Further...
The writer monad is accompanied by a function 'tell' that is called
explicitly to produce output.
'tell' is the 'non-proper morphism' of the writer monad (well the
standard Monad Transformer Library - MTL version has a couple of other
functions but that's an implementation detail). It characterizes the
writer monad in the same way that 'ask' characterises the reader monad
and the combination of get & put characterise the state monad. In
Haskell, you have to represent a Monad with a data type or newtype -
non-proper morphisms need to 'unpeel' the constructor to access the
internals of the monad. Here is the code from MTL for Writers tell:
instance (Monoid w) => MonadWriter w (Writer w) where
tell w = Writer ((), w)
... (listen and pass - the other non-proper morphisms).
Non-proper morphisms contrast with general monadic functions that are
usable with any monad. Take liftM as an example - liftM needs only
(>>=) and 'return' it doesn't have to look inside a monad's
constructor:
-- | Promote a function to a monad.
liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r
liftM f m1 = do { x1 <- m1; return (f x1) }
-- aka. liftM f m1 = m1 >>= \x1 -> return (f x1)
You would want to add a 'tell' function to your module. You're not
obliged to call it 'tell' of course, the MonadLib library, which is an
alternative to the standard MTL library, calls the function 'put'.
Best wishes
Stephen
------------------------------
Message: 8
Date: Sat, 23 Jan 2010 18:39:16 -0500
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] Re: Re: testing and the culture of
Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Adrian Adshead wrote:
> 1) Is it possible to have a function with an IO Action as an input
> parameter (and not just ignore the parameter) and have a non-IO
> return value?
> (ie is f :: IO a -> b possible without ignoring the IO a)
pretty much no, but consider, we can return a data structure constructed
out of those IOs, without actually learning anything about what actions
they are intended to do. For example:
f :: IO a -> [IO a]
f io = iterate (\io2 -> (io2 >> io2)) io
returns a list, in which the first action would do the IO once, the
second would do it twice, the third element would do it four times, the
fourth element, eight times, and so forth.
Obviously, it is only occasionally useful to do something strange like that.
-Isaac
------------------------------
Message: 9
Date: Sat, 23 Jan 2010 20:36:15 -0500
From: Scott Thoman <[email protected]>
Subject: [Haskell-beginners] adding state handing to existing code
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Since I'm very new to Haskell I have what is probably a simple
question yet I'm having trouble finding a clear example of how it
works. The basic question is: how do I pass state through existing
code without the intermediate code knowing about it. If I have, for
example, several layers of function calls and the innermost function
needs to access some state that is only "seeded" by the outermost
function, how do I do that without the functions in between knowing
about the additional state being threaded through them?
I have a simple example (that may *not* be good idiomatic Haskell):
--
process :: Integer -> Integer -> Integer
process x y =
2 * x * y
doit :: IO ()
doit = do
printf "f x y = %d\n" $ process 42 43
main :: IO ()
main = do
doit
putStrLn "done"
--
(I'm not totally sure about the type of "doit" but the code compiles
and runs as expected)
What I want to do is add some state handing to "process" to have it,
say, count the number of times it's been called (putting
threading/thread-local concerns aside for the moment). I'm trying to
understand how to add state to "process" along the lines of:
--
process :: Integer -> Integer -> State Integer Integer
process x y = do
s <- get
put $ s + 1
return $ 2 * x * y
--
but I want to only seed the state from "main" without "doit" having to
change -- I can call "process" from "doit" like "(execState (process
42 43) 0)" but I want the initial state to be determined at the top
level, from main.
I have a feeling there's some kind of "ah ha" moment that I'm just not
seeing yet. Any help or pointers to where I can look for myself would
be greatly appreciated.
Thanks in advance,
-thor
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 19, Issue 23
*****************************************