Tutorial (draft)PolymorphismFunctions - Abstraction and applicationDocumentation and user's manualTable of contentsOCaml programs

Functions - Abstraction and application

   
  Consider the following code.

First three values:

2 + (3 * 4) ;;

5 + (0 * 8) ;;

4 + (1 * 7) ;;

Then the same values expressed using local definitions:

let
  a = 2 and b = 3 and c = 4
in
  a + (b * c) ;;

let
  a = 5 and b = 0 and c = 8
in
  a + (b * c) ;;

let
  a = 4 and b = 1 and c = 7
in
  a + (b * c) ;;

Now the abstraction step:

function a, b, c -> a + (b * c) ;;

Finally the application step (three times):

(function a, b, c -> a + (b * c)) (2, 3, 4) ;;

(function a, b, c -> a + (b * c)) (5, 0, 8) ;;

(function a, b, c -> a + (b * c)) (4, 1, 7) ;;

   
  Now consider this code.

First three values:

3.  *. sin (2.)       +. 0.5 *. cos (4.) ;;

7.2 *. abs_float (3.) +. 8.  *. exp (6.1) ;;

5.  *. log (10.5)     +. 3.1 *. sin (0.7) ;;

Then the same values expressed using local definitions:

let
  a = 3. and b = sin and c = 2. and d = 0.5 and e = cos and f = 4.
in
  a *. b (c) +. d *. e (f) ;;

let
  a = 7.2 and b = abs_float and c = 3. and d = 8. and e = exp and f = 6.1
in
  a *. b (c) +. d *. e (f) ;;

let
  a = 5. and b = log and c = 10.5 and d = 3.1 and e = sin and f = 0.7
in
  a *. b (c) +. d *. e (f) ;;

Now the abstraction step:

function a, b, c, d, e, f -> a  *. b (c) +. d *. e (f) ;;

Finally the application step (three times):

(function a, b, c, d, e, f -> a  *. b (c) +. d *. e (f)) (3., sin, 2., 0.5, cos, 4.) ;;

(function a, b, c, d, e, f -> a  *. b (c) +. d *. e (f)) (7.2, abs_float, 3., 8., exp, 6.1) ;;

(function a, b, c, d, e, f -> a  *. b (c) +. d *. e (f)) (5., log, 10.5, 3.1, sin, 0.7) ;;

   
  In both cases, there is first an abstraction step from three expressions.

Abstraction consists of:
  1. Noting that the structures of the expressions are similar.
  2. Identifying what is identical and what is not.
  3. Transforming
    • What is identical into an expression having a functional value. Let f denote this expression.
    • What is not identical into the formal parameters of the expression f.

So, at the end of the abstraction step a functional value is written.

Then in both cases there is (three times) an application step.

An application step consists of applying the (functional) value of the expression f to arguments that correspond to what is particular in the initial expression considered. This results in obtaining the value of this initial expression.

Application consists of:
  1. Bounding each of the formal parameters in the expression f to the values of the corresponding arguments.
  2. Evaluating the right-hand part of the expression f in this context.

   
  Clearly both cases are similar, though the initial values are functional in the second one, which gives a higher-order function.


Latest update : October 5, 2006
This document was translated from LaTeX by Hyperlatex 2.5, which is not the latest version of Hyperlatex.

Tutorial (draft)PolymorphismFunctions - Abstraction and applicationDocumentation and user's manualTable of contentsOCaml programs