Re: Functional composition operator?

From: Pierre Weis (Pierre.Weis@inria.fr)
Date: Wed Dec 09 1998 - 11:58:26 MET


From: Pierre Weis <Pierre.Weis@inria.fr>
Message-Id: <199812091058.LAA12029@pauillac.inria.fr>
Subject: Re: Functional composition operator?
To: John.Harrison@cl.cam.ac.uk (John Harrison)
Date: Wed, 9 Dec 1998 11:58:26 +0100 (MET)
In-Reply-To: <E0znV41-00070g-00@heaton.cl.cam.ac.uk> from "John Harrison" at Dec 8, 98 09:52:54 pm

> | 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 ?

> | -- it only save a few characters in programs
> | (Compare
> | let h = f o g
> | with
> | let h x = f (g x);;)
> | -- it breaks the polymorphism
> | (if defined as
> | let h = f o g
> | h is not generalized, since its definition is a function
> | application, whereas inline expansion of functional composition
> | let h x = f (g x)
> | being the definition of a function is properly generalized.)
>
> 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). 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. 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.

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.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:17 MET