caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* RE: Functional composition operator?
@ 1998-12-08 18:08 Andrew Kay
  1998-12-08 20:32 ` John Prevost
  1998-12-09 16:17 ` Anton Moscal
  0 siblings, 2 replies; 12+ messages in thread
From: Andrew Kay @ 1998-12-08 18:08 UTC (permalink / raw)
  To: caml-list


John Whitley raised again a question that I had asked earlier.
We were converting source from caml to ocaml, and needed to
change our infix operator for function compostion.

We found the performance and accuracy of the ocaml compiler (relative
to the caml compiler) to be excellent.  There were only two teething
problems in our 1.2MB of source code, once all the syntax changes had
been sorted out with the help of caml2csl, and these were resolved
within a day.  (Both were to do with questionable code of our own,
which probably should not have worked in caml, but which somehow
did.)  This seems to us to be an amazing achievement of the ocaml
development team, and gives us confidence in using and recommending
ocaml in the future.

From: John Whitley <whitley@cse.buffalo.edu>
> is there a consensus for choice of infix composition operator?  

In the end we settled on >> and << for forward and reverse
composition respectively, satisfying the equations:

    (f << g) x = f (g x) = (g >> f) x

The chevrons give a nice feeling of a data pipeline running from g
to f in each case.  Since composition is associative (in the absence
of side effects) we can write (f << g << h << i), which is more elegant
than (compose (compose (compose f g) h) i), without fear of being
misunderstood.

We're still interested to know if other people have different approaches.

Best wishes
Andrew Kay

--
Sharp Labs Europe Ltd, Oxford Science Park, Oxford, UK, OX4 4GB
Andrew.Kay@sharp.co.uk  Tel:+44 1865 747711 FAX:+44 1865 747717




^ permalink raw reply	[flat|nested] 12+ messages in thread
* RE: Functional composition operator?
@ 1998-12-09 12:00 Don Syme
  0 siblings, 0 replies; 12+ messages in thread
From: Don Syme @ 1998-12-09 12:00 UTC (permalink / raw)
  To: 'Pierre Weis', John.Harrison; +Cc: caml-list


> 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 think it simply depends on what you're doing: highly symbolic work like
theorem proving certainly benefits from a compositional style, but for
anything involving complex state control (interfaces, objects, networking,
i/o, whatever) it is not so appealing.  I think the latter tasks are what
Pierre means by "the working programmer" -- (a rather loaded term in the ML
world? ;-) )

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

Some invisible uses of side effects are highly compatible with a functional
style, e.g. memoizing and building tables based on an environment, and
certainly John uses these effectively in his programs.  Externally visible
effects are, of course, to be avoided when using a compositional approach,
as Pierre suggests.

So you're both right, and that's just why CaML is so appealing: when doing
structural data manipulations it supports the functional model, but it also
supports state-based programming in a very traditional fashion (mutable
record fields, objects, arrays, for-loops, while-loops, begin-end statements
all being part of this).  For example, I would teach both styles as part of
a CaML programming course, and I frequently use both styles in different
modules of the one program.

Don




^ permalink raw reply	[flat|nested] 12+ messages in thread
* RE: Functional composition operator?
@ 1998-12-08 20:09 Don Syme
  0 siblings, 0 replies; 12+ messages in thread
From: Don Syme @ 1998-12-08 20:09 UTC (permalink / raw)
  To: 'Pierre Weis', whitley; +Cc: caml-list

> 
>  -- it only save a few characters in programs
>      (Compare
>       let h = f o g 
>       with
>       let h x = f (g x);;)

As you say, you can always define the infix identifier, so there's no
hassle, i.e. I'm not complaining.  However, the comparison should be
    f1 << f2 << f3 << f4 
v.
    (fun x -> f1(f2(f3(f4 x))))

because we may not be binding the on-the-fly value to a name.  Saving
parentheses is clearly a good idea - most the ML syntax is directed to this.
Even worse consider

    f1 << f2 x y z << f3 << f4 a b

v.
   (let f4' = f4 a b in
    let f2' = f2 x y z in
    (fun x -> f1(f2' z(f3(f4' x)))))

since in the latter case we may be taking advantage of the partial
evaluation of the closures to, for example, precompute some tables based on
the information in x,y,z and a,b.  I know which I prefer!

In 16,000 lines implementing the interactive theorem prover DECLARE there
are 220 uses of composition, so one every 70 lines or so.  There are 500
uses of "map", so it's relatively common in that one random example at
least.

Cheers,
Don

------------------------------------------------------------------------
At the lab:                                     At home:
Microsoft Research Cambridge                    11 John St
St George House                                 CB1 1DT
Cambridge, CB2 3NH, UK
Ph: +44 (0) 1223 744797                         Ph: +44 (0) 1223 722244
http://research.microsoft.com/users/dsyme
email: dsyme@microsoft.com
   "You've been chosen as an extra in the movie
        adaptation of the sequel to your life"  -- Pavement, Shady Lane
------------------------------------------------------------------------





^ permalink raw reply	[flat|nested] 12+ messages in thread
* RE: Functional composition operator?
@ 1998-12-08 19:51 Don Syme
  0 siblings, 0 replies; 12+ messages in thread
From: Don Syme @ 1998-12-08 19:51 UTC (permalink / raw)
  To: 'akay@sharp.co.uk', caml-list


Yes, I chose the same operator, which seems very natural. 

Don

> > is there a consensus for choice of infix composition operator?  
> 
> In the end we settled on >> and << for forward and reverse
> composition respectively, satisfying the equations:
> 
>     (f << g) x = f (g x) = (g >> f) x
> 
> The chevrons give a nice feeling of a data pipeline running from g
> to f in each case.  Since composition is associative (in the absence
> of side effects) we can write (f << g << h << i), which is 
> more elegant
> than (compose (compose (compose f g) h) i), without fear of being
> misunderstood.




^ permalink raw reply	[flat|nested] 12+ messages in thread
* Functional composition operator?
@ 1998-12-08  5:23 John Whitley
  1998-12-08 17:02 ` Pierre Weis
  0 siblings, 1 reply; 12+ messages in thread
From: John Whitley @ 1998-12-08  5:23 UTC (permalink / raw)
  To: caml-list


Andrew Kay <akay@sharp.co.uk> wrote, in the caml-list archives:
 > We are in the process of converting our Caml code into OCaml, and
 > have a problem choosing an infix syntax for function composition
 > [...] What do other OCaml people use for function composition? Is
 > there standard emerging?

I found no answer in the archives, so I'd like to raise the same
question again: is there a consensus for choice of infix composition
operator?  Failing that, is there some design principle that warranted
its omission?

Thanks,
John Whitley




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~1998-12-11 14:15 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-08 18:08 Functional composition operator? Andrew Kay
1998-12-08 20:32 ` John Prevost
1998-12-09 16:17 ` Anton Moscal
  -- strict thread matches above, loose matches on Subject: below --
1998-12-09 12:00 Don Syme
1998-12-08 20:09 Don Syme
1998-12-08 19:51 Don Syme
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
1998-12-11 13:57         ` Pierre Weis

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).