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: Overlapping instances (Nathan H?sken)
2. Re: deep seq and bang patterns (Emmanuel Touzery)
3. Re: deep seq and bang patterns (Emmanuel Touzery)
4. Fwd: deep seq and bang patterns (Emmanuel Touzery)
5. Re: deep seq and bang patterns (Daniel Fischer)
6. Re: Fwd: deep seq and bang patterns (Daniel Fischer)
7. Re: Fwd: deep seq and bang patterns (Henk-Jan van Tuyl)
----------------------------------------------------------------------
Message: 1
Date: Tue, 25 Dec 2012 17:46:15 +0100
From: Nathan H?sken <[email protected]>
Subject: Re: [Haskell-beginners] Overlapping instances
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On 12/25/2012 05:05 PM, Brandon Allbery wrote:
> On Tue, Dec 25, 2012 at 10:52 AM, Nathan H?sken
> <[email protected] <mailto:[email protected]>> wrote:
>
> instance B b => A b where
> doSomething = doMore
>
>
> This doesn't quite do what you think; it matches *all* types, then
> afterward applies the context.
>
> Your terminology suggests you're trying to do OOP with typeclasses.
> Don't; they're not OOP, and treating them like they are leads only to
> grief.
Well, you are completely right. The reason is, that I am trying to write
FFI for a OOP library.
------------------------------
Message: 2
Date: Tue, 25 Dec 2012 19:07:33 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] deep seq and bang patterns
To: "[email protected]" <[email protected]>
Message-ID:
<cac42renxwu5_8a1qzq5s+b6wkc2_clpk2nmmofmqgkqp3k6...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Thank you. I think you are as clear as possible, and complete.
I do wonder though, isn't there an annotation or technique that would force
the parameters of a constructor to be evaluated to WHNF when the
constructor is evaluated? In my case i believe the constructor is evaluated
immediately because it's in a do block of the IO monad.
I thought that's what the strictness annotations on the data members meant.
Thank you!
Emmanuel
On 25 Dec 2012 15:39, "Daniel Fischer" <[email protected]>
wrote:
> On Dienstag, 25. Dezember 2012, 13:20:54, Emmanuel Touzery wrote:
> > to be clear: I definitely have strict evaluation now. It works. And it
> > helped me to fix my bug (it's fixed now).
> >
> > But I think that to actually get strict evaluation I needed BOTH bang
> > patterns AND deep seq, at that same time... Which seems strange to me, I
> > would think that either would suffice.
>
> You need a bit less than what you (seem to) have.
>
> First, though,
>
> {-# LANGUAGE BangPatterns #-}
>
> data TvShow = TvShow
> {
> channel :: Channel,
> title :: !T.Text,
> startTime :: !T.Text,
> summary :: !T.Text
> }
> deriving (Eq, Show)
>
> the `!'s here are not bang patterns, they are strictness annotations on
> fields, and supported without extensions (Haskell2010, Haskell98, and
> presumably also earlier versions).
>
> Defining TvShow with strict fields for title, startTime and summary makes
> sure
> these fields are evaluated (to WHNF, but in case of `Data.Text`, that means
> fully evaluated) **when the TvShow value is evaluated to WHNF**.
>
> But when the value isn't evaluated, as in
>
> do ...
> let result = someFunction some arguments
> return result
>
> result remains a thunk, and thus its fields are not evaluated, even if
> marked
> strict.
>
> A simple
>
> return $! result
>
> to force evaluation of result to WHNF suffices to require the fields
> (except
> the `channel' field that's not marked strict) being evaluated.
>
> instance NFData TvShow
>
> that means you make TvShow an instance using the default implementation of
> `rnf', which is
>
> rnf a = a `seq` ()
>
> In other words, with that instance, deepseq is exactly the same as seq for
> TvShow values, and ($!!) the same as ($!). Neither involves any of the
> fields.
>
> So what you have is exactly the same as strict fields + strict return
> (`return
> $! result'), although it looks like it would do more.
>
> The `return $!! result' alone (or `return $! result') without strictness
> annotations on the fields evaluates only the top-level constructor, TvShow.
>
> An NFData instance that would force the fields,
>
> instance NFData TvShow where
> rnf (TvShow c t st su) = c `seq` t `seq` st `seq` su `seq` ()
>
> (that one involves the `channel', you can leave that out to get the
> behaviour
> you have now) with a
>
> return $!! result
>
> would achieve the evaluation without strictness annotations on the fields.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121225/1245fa35/attachment-0001.htm>
------------------------------
Message: 3
Date: Tue, 25 Dec 2012 19:50:09 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] deep seq and bang patterns
To: "[email protected]" <[email protected]>
Message-ID:
<cac42rekkx7lx7vlao0qpgocsxy7z66o8kpnp4wfchfwucjn...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
> A simple
>
> return $! result
>
> to force evaluation of result to WHNF suffices to require the fields
> (except
> the `channel' field that's not marked strict) being evaluated.
>
Otherwise I tested that right now and you are 100% right.
So strictness annotations + $! works fine and solves my problem and I also
mostly understand why it works like that :-)
I still need to do a little more reading on the topic but I'm much closer
now.
Thank you!
Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121225/f1e2e302/attachment-0001.htm>
------------------------------
Message: 4
Date: Tue, 25 Dec 2012 20:00:18 +0100
From: Emmanuel Touzery <[email protected]>
Subject: [Haskell-beginners] Fwd: deep seq and bang patterns
To: "[email protected]" <[email protected]>
Message-ID:
<CAC42Re==Mym_5wbhs4G_cUY0FXwr1JOLyDpdqf59oaZgZDWb=w...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hello,
On Tue, Dec 25, 2012 at 3:39 PM, Daniel Fischer <
[email protected]> wrote:
>
> Defining TvShow with strict fields for title, startTime and summary makes
> sure
> these fields are evaluated (to WHNF, but in case of `Data.Text`, that means
> fully evaluated) **when the TvShow value is evaluated to WHNF**.
>
how do I know what does "evaluated to WHNF" means for every possible type?
For instance, what does that mean for "String"? What does that mean for
"Maybe String"?
If I want the field to be fully evaluated, is !(Maybe String) enough or
should I even do !(Maybe !String) or something more?
Any source of information where I can read about this in depth would be
welcome (I have yet to read Real World Haskell, maybe it's covered there?).
Thank you!
Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121225/cce4fa9f/attachment-0001.htm>
------------------------------
Message: 5
Date: Tue, 25 Dec 2012 20:08:41 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] deep seq and bang patterns
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On Dienstag, 25. Dezember 2012, 19:07:33, Emmanuel Touzery wrote:
> Thank you. I think you are as clear as possible, and complete.
> I do wonder though, isn't there an annotation or technique that would force
> the parameters of a constructor to be evaluated to WHNF when the
> constructor is evaluated?
That's what strictness annotations do.
> In my case i believe the constructor is evaluated
> immediately because it's in a do block of the IO monad.
IO is still nonstrict in the values,
data Strict = S !Int
*Strict> do { putStrLn "Strictly"; return (S undefined); }
Strictly
*Strict> do { putStrLn "Strictly"; return $! (S undefined); }
Strictly
*** Exception: Prelude.undefined
returning a value doesn't force its evaluation to WHNF.
------------------------------
Message: 6
Date: Tue, 25 Dec 2012 20:37:09 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Fwd: deep seq and bang patterns
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On Dienstag, 25. Dezember 2012, 20:00:18, Emmanuel Touzery wrote:
> Hello,
>
> On Tue, Dec 25, 2012 at 3:39 PM, Daniel Fischer <
>
> [email protected]> wrote:
> > Defining TvShow with strict fields for title, startTime and summary
> > makes
> > sure
> > these fields are evaluated (to WHNF, but in case of `Data.Text`, that
> > means fully evaluated) **when the TvShow value is evaluated to WHNF**.
>
> how do I know what does "evaluated to WHNF" means for every possible type?
For non-function types, it means evaluate until the outermost constructor is
known. For function types (which have no constructors), it means evaluating to
the outermost lambda.
>
> For instance, what does that mean for "String"?
String is a list, so there it means evaluate until it is known whether the
value is [] or (_:_).
In particular, characters (or generally list elements) are only evaluated if
that is necessary to determine whether the list is empty or not.
If `dropWhile isSpace someString` is evaluated to WHNF, the characters in
someString must be evaluated until the first non-space character is found (or
the end of someString is reached, or a _|_ is reached) since that is necessary
to find the outermost constructor. So evaluating
dropWhile isSpace [' ' .. ]
to WHNF produces ('!':_) and not only (_:_)
> What does that mean for "Maybe String"?
Find out whether it's a `Just someThing` or `Nothing`.
Again, in the case of a `Just someThing`, the someThing is only evaluated if
that's necessary to find out whether it's a `Just` at all.
>
> If I want the field to be fully evaluated, is !(Maybe String) enough or
> should I even do !(Maybe !String) or something more?
You can't have something like
Maybe !String
`!String' is not a type, so it's not an allowed argument for `Maybe'.
A field `field :: !(Maybe String)' would only force evaluation of the `Maybe'
constructor when the containing value is evaluated, the String in a `Just'
would still remain completely unevaluated (unless that is necessary to find
out whether the `Maybe String' value is a `Just').
You can define a strict version of `Maybe`,
data SMaybe a
= SJust !a
| SNothing
and have a field `field :: !(SMaybe String)'. But that would still only
evaluate the String to WHNF, so `SJust (undefined : undefined)' is still
possible.
If you want complete evaluation, you need to recur through every type adding a
constructor layer, until you reach a function type or a base case
(enumerations
data Foo
= Bar
| Baz
| Quux
and wrappers of primitive types like
data Int = I# Int#
[for GHC]).
>
> Any source of information where I can read about this in depth would be
> welcome (I have yet to read Real World Haskell, maybe it's covered there?).
I don't know to what depth it is covered in RWH, it must be covered in some
depth there.
And there's of course the invaluable and authoritative language report
http://www.haskell.org/onlinereport/haskell2010/
Cheers,
Daniel
------------------------------
Message: 7
Date: Tue, 25 Dec 2012 22:52:45 +0100
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] Fwd: deep seq and bang patterns
To: [email protected], "Daniel Fischer"
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
delsp=yes
On Tue, 25 Dec 2012 20:37:09 +0100, Daniel Fischer
<[email protected]> wrote:
> On Dienstag, 25. Dezember 2012, 20:00:18, Emmanuel Touzery wrote:
>> how do I know what does "evaluated to WHNF" means for every possible
>> type?
>
> For non-function types, it means evaluate until the outermost
> constructor is
> known. For function types (which have no constructors), it means
> evaluating to
> the outermost lambda.
Can I use your text for the HaskellWiki?
Regards,
Henk-Jan van Tuyl
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 54, Issue 43
*****************************************