[comp.lang.functional] Functional Product

pop@cs.umass.edu (03/02/91)

C, being a heteroousian language provides no defined way in which a
user can create code in a program and use it in the same program.
As Richard O'Keefe has pointed out, you can in fact do it by exploit
ing loopholes in the type-system of C, but in an entirely non-portable
way. In a functional language, when a function  returns a function as a
result, using some existing lambda expression or its equivalent, the
free variables of the lambda expression must be premanently bound to
the values they had at the time of call of the outer function (as in
the following Pop-11 (not an elegant syntax..)

define fp(f,g);           ;;; form product of functions f and g
  lvars f g;              ;;; declare f and g as lexical (UGH)
  procedure(x); f(g(x))   ;;; the inner lambda expression.
  endprocedure
enddefine;

the f and the g of the inner procedure (corresponding to a lambda
expression) are bound to the values they had at the call of fp. The
result is a NEW function, which binds the variables to this value and
then calls the body of the OLD lambda expression. One way of doing
this uses the technique used by POP-2 programmers and now called
 "lambda lifting" which gives extra parameters to the internal lambda
expression and then curries them in the fp function. With a
stack-based implementation, this provides an implementation mechanism
which is quite easy to do.

[In fact, the existing POP-11 implementation of function product
 written as concatenation  f<>g  does what Richard O'Keefe obviously
 did, and concatenates the code..., tho' of course other functionals
 would work as above].