Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to make code least strict?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Robin Green  
View profile  
 More options Jan 19 2009, 11:49 am
Newsgroups: fa.haskell
From: Robin Green <gree...@greenrd.org>
Date: Mon, 19 Jan 2009 16:49:18 UTC
Local: Mon, Jan 19 2009 11:49 am
Subject: [Haskell-cafe] How to make code least strict?
What guidelines should one follow to make Haskell code least-strict?
--
Robin
_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ChrisK  
View profile  
 More options Jan 19 2009, 12:11 pm
Newsgroups: fa.haskell
From: ChrisK <hask...@list.mightyreason.com>
Date: Mon, 19 Jan 2009 17:11:21 UTC
Local: Mon, Jan 19 2009 12:11 pm
Subject: [Haskell-cafe] Re: How to make code least strict?

Robin Green wrote:
> What guidelines should one follow to make Haskell code least-strict?

Obviously the use of "seq" and bang-patterns make code more strict.

Code is strict when it evaluates values to determine a pattern match.  So
avoiding that makes code lazier.  Values are evaluated when decisions have to be
make in order to choose what an expression will evaluate to.  Avoiding "case"
statements and things that de-sugar to case statements such as "if then else"
and pattern matching.  Put off examining the input values.  Occasionally the use
of "lazy" patterns, preceded by ~, can help make code both more compact and less
strict.

Consider that the order of pattern matching can matter as well, the simplest
common case being zip:

zip xs [] = []
zip [] ys = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys

The order of the first two lines of zip's definition affects whether
zip [] (error "boom")
or
zip (error "bam") []
will be an error.  This shows that "least-strict" is not a unique goal.

For the choice I just made the "zip [] (error "boom")" will cause an error
because the first definition line of zip checks the second argument, while "zip
(error "bam") []" will evaluate to [].

The other way to reduce strictness is to be more polymorphic because this
reduces what can be sensibly done with the arguments.

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas DuBuisson  
View profile  
 More options Jan 19 2009, 12:37 pm
Newsgroups: fa.haskell
From: "Thomas DuBuisson" <thomas.dubuis...@gmail.com>
Date: Mon, 19 Jan 2009 17:37:51 UTC
Local: Mon, Jan 19 2009 12:37 pm
Subject: Re: [Haskell-cafe] How to make code least strict?

On Mon, Jan 19, 2009 at 4:48 PM, Robin Green <gree...@greenrd.org> wrote:
> What guidelines should one follow to make Haskell code least-strict?

There was a great Cafe discussion started by Henning on just this.  He
provided this link:

http://www.haskell.org/haskellwiki/Maintaining_laziness
_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robin Green  
View profile  
 More options Jan 19 2009, 1:50 pm
Newsgroups: fa.haskell
From: Robin Green <gree...@greenrd.org>
Date: Mon, 19 Jan 2009 18:50:40 UTC
Local: Mon, Jan 19 2009 1:50 pm
Subject: Re: [Haskell-cafe] How to make code least strict?
On Mon, 19 Jan 2009 17:36:30 +0000

"Thomas DuBuisson" <thomas.dubuis...@gmail.com> wrote:
> On Mon, Jan 19, 2009 at 4:48 PM, Robin Green <gree...@greenrd.org>
> wrote:
> > What guidelines should one follow to make Haskell code least-strict?

> There was a great Cafe discussion started by Henning on just this.  He
> provided this link:

> http://www.haskell.org/haskellwiki/Maintaining_laziness

Thanks - wow, my memory is terrible! I submitted this page to the
Haskell reddit myself 20 days ago! I had a sneaking feeling of deja
vu after I asked the question. :-D
--
Robin
_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan Ingram  
View profile  
 More options Jan 19 2009, 3:27 pm
Newsgroups: fa.haskell
From: "Ryan Ingram" <ryani.s...@gmail.com>
Date: Mon, 19 Jan 2009 20:27:43 UTC
Local: Mon, Jan 19 2009 3:27 pm
Subject: Re: [Haskell-cafe] Re: How to make code least strict?

On Mon, Jan 19, 2009 at 9:10 AM, ChrisK <hask...@list.mightyreason.com> wrote:
> Consider that the order of pattern matching can matter as well, the simplest
> common case being zip:

> zip xs [] = []
> zip [] ys = []
> zip (x:xs) (y:ys) = (x,y) : zip xs ys

If you are obsessive about least-strictness and performance isn't a
giant concern, this seems like a perfect use for Conal's unamb[1]
operator.

zipR xs [] = []
zipR [] ys = []
zipR (x:xs) (y:ys) = (x,y) : zip xs ys

zipL [] ys = []
zipL xs [] = []
zipL (x:xs) (y:ys) = (x,y) : zip xs ys

zip xs ys = unamb (zipL xs ys) (zipR xs ys)

This runs both zipL and zipR in parallel until one of them gives a
result; if neither of them is _|_ they are guaranteed to be identical,
so we can "unambiguously choose" whichever one gives a result first.

  -- ryan

[1] http://conal.net/blog/posts/functional-concurrency-with-unambiguous-c...
_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Conal Elliott  
View profile  
 More options Jan 19 2009, 5:43 pm
Newsgroups: fa.haskell
From: Conal Elliott <co...@conal.net>
Date: Mon, 19 Jan 2009 22:43:28 UTC
Local: Mon, Jan 19 2009 5:43 pm
Subject: Re: [Haskell-cafe] Re: How to make code least strict?

I second Ryan's recommendation of using unamb [1,2,3] to give you unbiased
(symmetric) laziness.

The zip definition could also be written as

    zip xs@(x:xs') ys@(y:ys') =
      assuming (xs == []) [] `unamb`
      assuming (ys == []) [] `unamb`
      (x,y) : zip xs' ys'

The 'assuming' function yields a value if a condition is true and otherwise
is bottom:

    assuming :: Bool -> a -> a
    assuming True  a = a
    assuming False _ = undefined

This zip definition is a special case of the annihilator pattern, so

    zip = parAnnihilator (\ (x:xs') (y:ys') -> (x,y) : zip xs' ys') []

where 'parAnnihilator' is defined in Data.Unamb (along with other goodies)
as follows:

    parAnnihilator :: Eq a => (a -> a -> a) -> a -> (a -> a -> a)
    parAnnihilator op ann x y =
      assuming (x == ann) ann `unamb`
      assuming (y == ann) ann `unamb`
      (x `op` y)

[1] http://haskell.org/haskellwiki/Unamb
[2]
http://hackage.haskell.org/packages/archive/unamb/latest/doc/html/Dat...
[3] http://conal.net/blog/tag/unamb/

   - conal

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan Ingram  
View profile  
 More options Jan 19 2009, 6:01 pm
Newsgroups: fa.haskell
From: "Ryan Ingram" <ryani.s...@gmail.com>
Date: Mon, 19 Jan 2009 23:01:53 UTC
Local: Mon, Jan 19 2009 6:01 pm
Subject: Re: [Haskell-cafe] Re: How to make code least strict?
Actually, I see a nice pattern here for unamb + pattern matching:

> zip xs ys = foldr unamb undefined [p1 xs ys, p2 xs ys, p3 xs ys] where
>     p1 [] _ = []
>     p2 _ [] = []
>     p3 (x:xs) (y:ys) = (x,y) : zip xs ys

Basically, split each pattern out into a separate function (which by
definition is _|_ if there is no match), then use unamb to combine
them.

The invariant you need to maintain is that potentially overlapping
pattern matches (p1 and p2, here) must return the same result.

With a little typeclass hackery you could turn this into

> zip = unambPatterns [p1,p2,p3] where {- p1, p2, p3 as above -}

Sadly, I believe the performance of "parallel-or"-style operations is
pretty hideous right now.  Conal?

  -- ryan

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Conal Elliott  
View profile  
 More options Jan 20 2009, 8:09 pm
Newsgroups: fa.haskell
From: Conal Elliott <co...@conal.net>
Date: Wed, 21 Jan 2009 01:09:18 UTC
Local: Tues, Jan 20 2009 8:09 pm
Subject: Re: [Haskell-cafe] Re: How to make code least strict?

Lovely reformulation, Ryan!

I think lub [4] is sufficient typeclass hackery for unambPatterns:

   unambPatterns == lubs == foldr lub undefined

[4] http://conal.net/blog/posts/merging-partial-values

I think performance is okay now, if you have very recent versions of unamb
*and* GHC head (containing some concurrency bug fixes).  See
http://haskell.org/haskellwiki/Unamb .  The GHC fix will take a while to get
into common use.

My definitions of zip via (a) 'assuming' & 'unamb' and (b) parAnnihilator
are badly broken.  For one, the unamb arguments are incompatible (since i
didn't check for both non-null args in the third case).  Also, the types
aren't right for parAnnihilator.

I tried out this idea, and it seems to work out very nicely.  See the
brand-new blog post
http://conal.net/blog/posts/lazier-function-definitions-by-merging-pa....
 Blog comments, please!

   - Conal

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Davie  
View profile  
 More options Jan 21 2009, 3:51 am
Newsgroups: fa.haskell
From: Thomas Davie <tom.da...@gmail.com>
Date: Wed, 21 Jan 2009 08:51:21 UTC
Local: Wed, Jan 21 2009 3:51 am
Subject: Re: [Haskell-cafe] Re: How to make code least strict?

Further to all the playing with unamb to get some very cool behaviors,  
you might want to look at Olaf Chitil's paper here:

http://www.cs.kent.ac.uk/pubs/2006/2477/index.html

It outlines a tool for checking if your programs are as non-strict as  
they can be.

Bob

On 21 Jan 2009, at 02:08, Conal Elliott wrote:

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jan Christiansen  
View profile  
 More options Jan 22 2009, 9:01 am
Newsgroups: fa.haskell
From: Jan Christiansen <j...@informatik.uni-kiel.de>
Date: Thu, 22 Jan 2009 14:01:12 UTC
Local: Thurs, Jan 22 2009 9:01 am
Subject: Re: Re: [Haskell-cafe] How to make code least strict?

Thomas Davie wrote:

> Further to all the playing with unamb to get some very cool behaviors,  
> you might want to look at Olaf Chitil's paper here:

> http://www.cs.kent.ac.uk/pubs/2006/2477/index.html

> It outlines a tool for checking if your programs are as non-strict as  
> they can be.

We have discussed StrictCheck on the thread "Maintaining Laziness" which
also covered the topic least strictness but wasn't as popular as this one.

I have put up a page with functions from Data.List which are not least
strict. I want to extend this with other examples from standard libraries.
The page can be found at
http://www.informatik.uni-kiel.de/~jac/strictcheck/.

Jan
--
View this message in context: http://www.nabble.com/How-to-make-code-least-strict--tp21546891p21604...
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »