caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Harrison <John.Harrison@cl.cam.ac.uk>
To: Pierre Weis <Pierre.Weis@inria.fr>
Cc: Don Syme <dsyme@microsoft.com>,
	John.Harrison@cl.cam.ac.uk (John Harrison),
	caml-list@inria.fr
Subject: Re: Functional composition operator?
Date: Wed, 09 Dec 1998 17:17:22 +0000	[thread overview]
Message-ID: <E0znnEp-00050H-00@heaton.cl.cam.ac.uk> (raw)
In-Reply-To: Your message of "Wed, 09 Dec 1998 11:58:26 +0100." <199812091058.LAA12029@pauillac.inria.fr>


| > | In fact we discourage the usage of functional composition as a general
| > | programming tool
| > 
| > A very bad idea, in my opinion.
|
| I'm not sure I got the point here. Why is it a very bad idea ?
| Do you consider FP style programming the right functional programming style ?

The whole point of a higher order functional language is to treat functions 
(more) on a par with other values. Therefore it seems wrongheaded to seek
to discourage programmers from using one of the most basic operations on 
functions.

| > These alternatives are not semantically equivalent. If f and g are
| > complicated expressions that can be further evaluated, it is often
| > highly undesirable to perform the evaluation every time h is called,
| > which is what "let h x = f(g x)" entails.
|
| Right, if you consider that efficiency has something to do with the
| semantics, which is not the classical acceptation of this term. To be
| precise, if f and g are side-effect free, these alternatives are
| indeed ``semantically equivalent'' in the classical view of semantics
| (same answer). 

Well, even for side-effect-free expressions, the difference in efficiency
could be decisive in practical terms. And if you do use side effects it
matters even more. 

| Anyway, I was implicitely assuming that f and g were
| functions, and h was invariant by eta-expansion. In my mind, if h is
| not invariant by eta-expansion, the program is hard to read and hard
| to understand. Anyway, if f and g are not mere functions but ``very
| complicated expressions'' the program is likely to be clearer if you
| bind these expressions with a let and give them a name, instead of
| composing these complex expressions inline. Let alone the case when
| these expressions involve side-effects: then, if you still insist at
| composing them on the fly, you get an order of evaluation dependant
| program and this must be carefully avoided.

It depends on what you mean by order of evaluation. If you mean whether
"f" or "x" gets evaluated first in "f(x)", then I agree. But the fact
that the body of a function is not evaluated till it gets its argument
is basic to an ML-like functional language, and a dependence on this is not
at all something to be avoided. On the contrary, a serious use of side
effects requires an understanding of just when things get evaluated, which
is a good reason why the combination of side effects and a lazy language is
unusual.

Here is a simple example (from theorem proving, funnily enough) where
composition seems to me natural and where partial evaluation is important.

  let rule = rewrite[theorems2] o rewrite[theorems1];;

Typically, the partial evaluation of "rewrite[theorems?]" is critical 
because it builds a data structure from the theorems before starting 
the rewrite.

| In conclusion, if you freely mix inline composition, side-effects and
| partial evaluation you get very complex programs, hard to understand and
| hard to maintain. That's my experience in maintaining and developping a
| compiler written with that style, using a free mixing of composition,
| partial evaluation, and continuations.

I wasn't necessarily proposing such a ragbag. But as Don Syme has pointed
out, there are many perfectly natural and disciplined ways of using side
effects within globally functional code.

| Anyway, if you can manage the additional complexity, there is no
| problem at using a highly ``composing'' style in Caml. However, in my
| mind, it is not a good idea to promote this style, especially for the
| working programmer.

I don't really know what you mean by "working programmer". If you mean
"programmer who writes the same kinds of programs as I do", then it's
a poor foundation for a panoptic view of the field. I'm happy for
people to avoid composition if that's what they want. However I don't like
the idea of discouraging new programmers from using it even when it
could be very helpful and natural.

John.




  reply	other threads:[~1998-12-11 13:58 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-08  5:23 John Whitley
1998-12-08 17:02 ` Pierre Weis
1998-12-08 21:52   ` John Harrison
1998-12-09 10:58     ` Pierre Weis
1998-12-09 17:17       ` John Harrison [this message]
1998-12-11 13:57         ` Pierre Weis
1998-12-08 18:08 Andrew Kay
1998-12-08 20:32 ` John Prevost
1998-12-09 16:17 ` Anton Moscal
1998-12-08 19:51 Don Syme
1998-12-08 20:09 Don Syme
1998-12-09 12:00 Don Syme

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=E0znnEp-00050H-00@heaton.cl.cam.ac.uk \
    --to=john.harrison@cl.cam.ac.uk \
    --cc=Pierre.Weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=dsyme@microsoft.com \
    /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;
as well as URLs for NNTP newsgroup(s).