From: Jon Harrop <jon@jdh30.plus.com>
To: caml-list <caml-list@pauillac.inria.fr>
Subject: Re: [Caml-list] laziness
Date: Mon, 6 Sep 2004 13:55:05 +0100 [thread overview]
Message-ID: <200409061355.05523.jon@jdh30.plus.com> (raw)
In-Reply-To: <20040906005741.GA20406@annexia.org>
On Monday 06 September 2004 01:57, Richard Jones wrote:
> One thing that worries me about laziness.
>
> Doesn't laziness often indicate a bug in the code?
No. Hence some languages (like Haskell) are completely lazy. Some people
advocate the use of approaches which are lazy by default (like Haskell's),
others prefer to use laziness explicitly, only when they feel it is
necessary. The principle problem with lots of laziness is the difficulty in
predicting memory usage, which is very implementation dependent.
> ie. You've
> written an expression in the program, but that expression is never
> used. This is dead code, right? Hence a bug?
Laziness is used when there is code which *might* need executing, not when an
expression is never going to be executed.
Examples include recurrence relations, when the code will be executed until
the base case is reached. This is true for lazy lists. Another example is
propagators (where further computations may be performed, up to the
discretion of a predicate).
[lazy list]
> ...
> But in a sense this contains dead code too. You've written down your
> desire to construct all the squares, and then later you change your
> mind and take only the first 10.
You haven't written down a "desire to construct all squares" any more than
writing the equivalent recurrence relation does:
x_0 = 0
x_(n+1) = 1 + 2 x_n
You have simply defined how any square may be computed.
> In OCaml you'd write this correctly as something like:
>
> List.map (fun x -> x * x) (range 1 10)
This assumes that you know the 10 in advance, it also loses out on
memoization. What if you want to compute up to 10, 12 and 14?
> (Sorry if this appears like a troll, but actually I'm genuinely
> interested in whether laziness is useful, or indicates a bug).
Laziness can definitely be useful. Whilst writing a compiler, I recently noted
that my computing and storing the set of types used in each expression and
subexpression was not always used. By making the construction lazy, I avoided
many unnecessary computations (but not all!, hence it isn't dead code) and
the program now runs much faster.
From a theoretical standpoint, Okasaki pointed out that imperative programs
are often faster because they choose to amortise the costs of a sequence of
operations, grouping them into batches. In contrast, functional programs
often don't amortise costs. This gives them better worst case behaviour but
worse average case behaviour. The Map and Hashtbl data structures are fine
examples of this. Okasaki suggested that the solution might be to use
laziness in functional approaches to amortise the costs of operations, lazily
storing the necessary changes and then forcing the evaluation of all
outstanding changes only when necessary.
So laziness can be used as an optimisation.
Cheers,
Jon.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
next prev parent reply other threads:[~2004-09-06 12:59 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-09-05 1:07 Jason Smith
2004-09-05 5:46 ` skaller
2004-09-06 0:57 ` Richard Jones
2004-09-06 6:11 ` Nicolas Cannasse
2004-09-06 8:24 ` skaller
2004-09-06 8:44 ` Erik de Castro Lopo
2004-09-06 12:55 ` Jon Harrop [this message]
2004-09-06 16:21 ` William Lovas
2004-09-06 22:35 ` Hartmann Schaffer
2004-09-07 8:31 ` Richard Jones
2004-09-07 8:37 ` Nicolas Cannasse
-- strict thread matches above, loose matches on Subject: below --
2004-09-06 12:17 Jason Smith
2004-09-06 17:00 ` skaller
2004-09-06 9:16 Jason Smith
2004-09-06 9:07 Jason Smith
2004-09-06 10:18 ` skaller
2004-09-04 6:30 skaller
2004-09-04 8:40 ` Marcin 'Qrczak' Kowalczyk
2004-09-04 11:21 ` skaller
2004-09-04 11:49 ` Richard Jones
2004-09-04 20:40 ` Hartmann Schaffer
2004-09-05 10:50 ` Jon Harrop
2004-09-05 14:07 ` skaller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200409061355.05523.jon@jdh30.plus.com \
--to=jon@jdh30.plus.com \
--cc=caml-list@pauillac.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox