This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Re: Functional composition operator?
• Pierre Weis
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 1998-12-09 (11:00) From: Pierre Weis Subject: Re: Functional composition operator?
```> | 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/

```