Extensible functions

Extensible functions allows the definition of pattern matching functions which are extensible by adding new cases that are inserted automatically at the proper place by comparing the patterns. The pattern cases are ordered according to syntax trees representing them, "when" statements being inserted before the cases without "when".

Notice that extensible functions are functional: when extending a function, a new function is returned.

The extensible functions are used in the pretty printing system of Camlp5.

  1. Syntax
  2. Semantics

Syntax

The syntax of the extensible functions, when loading "pa_extfun.cmo", is the following:

           expression ::= extensible-function
  extensible-function ::= "extfun" expression "with" "[" match-cases "]"
          match-cases ::= match-case "|" match-cases
           match-case ::= pattern "->" expression
                        | pattern "when" expression "->" expression

It is an extension of the same syntax as the "match" and "try" constructions.

Semantics

The statement "extend" defined by the syntax takes an extensible function and return another extensible function with the new match cases inserted at the proper place within the initial extensible function.

Extensible functions are of type "Extfun.t a b", which is an abstract type, where "a" and "b" are respectively the type of the patterns and the type of the expressions. It corresponds to a function of type "a -> b".

The function "Extfun.apply" takes an extensible function as parameter and returns a function which can be applied like a normal function.

The value "Extfun.empty" is an empty extensible function, of type "Extfun.t 'a 'b". When applied with "Extfun.apply" and a parameter, it raises the exception "Extfun.Failure" whatever the parameter.

For debugging, it is possible to use the function "Extfun.print" which displays the match cases of the extensible functions. (Only the patterns are displayed in clear text, the associated expressions are not.)

The match cases are inserted according to the following rules:


Copyright 2007-2010 Daniel de Rauglaudre (INRIA)

Valid XHTML 1.1