.ModelicaReference.Classes.'function'.'function partial application'

Information

A function partial application is a function call with certain formal parameters bound to expressions. A function partial application returns a partially evaluated function that is also a function, with the remaining not bound formal parameters still present in the same order as in the original function declaration. A function partial application is specified by the function keyword followed by a function call to func_name giving named formal parameter associations for the formal parameters to be bound, e.g.:

function func_name(..., formal_parameter_name = expr, ...)

[Note that the keyword function in a function partial application differentiates the syntax from a normal function call where some parameters have been left out, and instead supplied via default values.] The function created by the function partial application acts as the original function but with the bound formal input parameters(s) removed, i.e., they cannot be supplied arguments at function call. The binding occurs when the partially evaluated function is created. A partially evaluated function is "function compatible" to the same function where all bound arguments are removed [thus, for checking function type compatibility, bound formal parameters are ignored].

Example of function partial application as argument, positional argument passing, according to case (b) above:

model Test
   parameter Integer N;
   Real area;
algorithm
   area := 0;
   for i in 1:N loop
     area  := area + quadrature(0, 1, function Sine(A=2, w=i*time));
   end for;
end Test;

function Sine  "y = Sine(x,A,w)"
  extends Integrand;
  input Real A;
  input Real w;
algorithm
  y:=A*Modelica.Math.sin(w*x);
end Sine;

//Call with function partial application as named input argument:
area  := area + quadrature(0, 1, integrand = function Sine(A=2, w=i*time));

Example showing that function types are matching after removing the bound arguments A and w in a function partial application:

function Sine2  "y = Sine2(A,w,x)"
  input Real A;
  input Real w;
  input Real x; // Note: x is now last in argument list.
  output Real y;
algorithm
  y:=A*Modelica.Math.sin(w*x);
end Sine2;

// The partially evaluated Sine2 has only one argument:
// x - and is thus type compatible with Integrand.
area = quadrature(0, 1, integrand = function Sine2(A=2, w=3));

Example of a function partial application of a function that is a component, according to case (d) above:

partial function SurfaceIntegrand
   input Real x;
   input Real y;
   output Real z;
end SurfaceIntegrand;

function quadratureOnce
  input Real x;
  input Real y1;
  input Real y2;
  input SurfaceIntegrand integrand;
  output Real z;
algorithm
  // This is according to case (d) and needs to bind the 2nd argument
  z := quadrature(y1, y2, function integrand(y=x));
end quadratureOnce;

function surfaceQuadrature
  input Real x1;
  input Real x2;
  input Real y1;
  input Real y2;
  input SurfaceIntegrand integrand;
  output Real integral;
algorithm
   // Case (b) and (c)
   integral := quadrature(x1, x2,
     function quadratureOnce(y1=y1, y2=y2, integrand=integrand);
end surfaceQuadrature;

Generated at 2024-03-28T19:15:55Z by OpenModelicaOpenModelica 1.22.3 using GenerateDoc.mos