Chapter 10

Higher-order Calls and Anonymous Definitions

On this page

A higher-order predicate or function takes a callable value as an argument. Picat supports callable values that name predicates or functions, optionally with arguments already supplied. Anonymous functions and anonymous predicates provide callable values without requiring named definitions.

10.1 Higher-order Call Conventions

The predicate call(S, Arg1 , …, Argn ) invokes a predicate, while the function apply(S, Arg1 , …, Argn ) returns the value of a function call. For a named callable, S is an atom or a structure. If S is an atom, it supplies the name of the predicate or function. If S is a structure, its name supplies the callable name and its arguments precede the additional arguments. For example:

F = $'*'(2),
Y = apply(F,5)              % Y = 10

The resulting function call is ’*’(2,5). The dollar symbol constructs a structure instead of evaluating a function call. A bare atom can also name a callable:

F = '+',
Y = apply(F,3,4)            % Y = 7

When S is a variable at compile time, both call conventions can be written as S(Arg1, …, Argn ). The context determines whether this notation denotes a predicate call or a function call:

P = $member(X),
P([1,2,3]),                % predicate context: call(P,[1,2,3])
F = $'*'(2),
Y = F(5)                   % function context: apply(F,5)

An occurrence used as a goal invokes a predicate; an occurrence used as a value-producing expression invokes a function. The result variable of a function call is not an additional explicit argument.

Within a term constructor, a higher-order call whose name is a variable is also treated as a function call:

F = $'*'(2),
T = $p(F(5))               % T = p(10)

Here, the value of F(5) becomes an argument of the constructed term. The higher-order call is not preserved as literal syntax in that term.

10.1.1 Arity and Supplying Arguments

The supplied arguments must select a predicate or function of the appropriate arity. In particular, apply does not return a partially evaluated function when too few arguments are supplied; an incorrect arity raises an exception. A structure such as $’*’(2) explicitly stores a supplied argument. Constructing that structure is different from invoking a function with too few arguments.

An anonymous definition has the explicit arity given by its parameter list. All of those arguments must be supplied when it is invoked. To return a function that takes further arguments, write a nested anonymous function explicitly, as illustrated below.

10.2 Anonymous Functions

An anonymous function, also called a lambda expression, has the form

([A1,A2,...,An] -> Res, B1,B2,...,Bm)

The outer parentheses are required. The bracketed list contains the parameter patterns, Res is the return expression, and the goals following the first comma form the body. The body is executed before the return expression is evaluated. When no body is needed, the function can be written as

([A1,A2,...,An] -> Res)

Omitting the body is equivalent to using the body true.

Evaluating a lambda expression constructs a callable value, called a closure; it does not execute the body or evaluate the return expression. No dollar symbol is needed to construct this value. For example:

F = ([X,Y] -> X+Y),
Z = F(3,4)                 % Z = 7

The invocation F(3,4) can also be written as apply(F,3,4). A function with a body can compute its result using intermediate variables:

F = ([X,Y] -> Z, T = X*X, Z = T+Y),
R = F(3,4)                 % R = 13

The body has the usual goal syntax, including conjunctions, disjunctions, conditionals, and loops. For example:

Sum = ([Xs] -> Total,
    Total = 0,
    foreach (X in Xs)
        Total := Total+X
    end),
S = Sum([1,2,3])           % S = 6

The function follows the execution conventions of an ordinary named function. It is not a generator of multiple return values.

10.2.1 Parameter Patterns

The parameters are terms, just like the arguments of a named rule head; they are not restricted to variables. Their matching follows the ordinary head pattern-matching conventions. For example:

First = ([[X|_]] -> X),
H = First([a,b,c])         % H = a

The outer bracketed list contains one parameter, whose pattern is a nonempty list. Matching does not instantiate the caller’s argument merely to make a pattern applicable. The same restrictions on head patterns apply here; parameter patterns are not evaluated as function calls.

A parameter list must be a proper list of patterns. Its length determines the arity. An empty list denotes a function with no explicit arguments:

F = ([] -> 42),
Y = F()                    % Y = 42

10.3 Anonymous Predicates

An anonymous predicate has the form

([A1,A2,...,An] => B1,B2,...,Bm)

Its parameter list has the same interpretation as that of an anonymous function. The body is a goal, and there is no return expression. For example:

Print = ([X] => println(X)),
Print(hello)

The invocation Print(hello) can also be written as call(Print,hello). An anonymous predicate with no explicit arguments uses [] as its parameter list and is invoked with P() or call(P).

An anonymous predicate consists of one rule, written with =>; there is no anonymous ?=> form. There is no alternative rule to try on backtracking. This does not require every goal in the body to be globally deterministic: a goal such as member can still supply multiple solutions within the body. For example:

Choose = ([X] => member(X,[a,b,c])),
L = findall(X,Choose(X))   % L = [a,b,c]

Use an anonymous predicate when success, failure, or bindings are the desired result; use an anonymous function when an expression must return a value.

10.4 Context Variables and Nested Definitions

An anonymous definition forms a name scope, just like a foreach loop. A variable that has occurred before the anonymous definition in the enclosing scope is global to the anonymous definition and refers to the existing variable. A variable whose first occurrence is inside the anonymous definition is a fresh local variable of that definition. This rule applies to variables in parameter patterns, the return expression, and the body. Here, “global” describes visibility across the scope boundary; it does not mean a module-global variable. A prior occurrence counts even if the variable is still unbound.

Local variables are fresh for each invocation and are not made visible to subsequent goals outside the definition. For example:

Offset = 10,
F = ([X] -> R, T = X+Offset, R = T*T),
A = F(1),                 % A = 121
B = F(2)                  % B = 144

The variable Offset is global to the anonymous definition, while X, R, and T first occur inside it and are local. The two invocations use fresh local variables. Nested anonymous definitions follow the same scope rule: a variable introduced in an outer definition can be global to an inner definition.

An anonymous definition can refer to variables from the enclosing context. A closure carries the values of these context variables at the time the closure is created. Subsequent changes to the context variables do not affect the values captured by that closure. This capture convention applies to both anonymous functions and anonymous predicates. For example:

Offset = 10,
AddOffset = ([X] -> X+Offset),
Offset := 20,
Y = AddOffset(5),         % Y = 15
NewAddOffset = ([X] -> X+Offset),
Z = NewAddOffset(5)       % Z = 25

The parameter X is supplied at invocation, while Offset comes from the context in which the closure is constructed. The captured variables are not additional arguments that the caller must supply. The first closure retains the captured value 10, even after Offset is assigned 20. The second closure is created after the assignment and therefore captures 20. Invoking either closure does not recapture the current value of Offset.

An anonymous function can return another anonymous function:

MakeAdder = ([A] -> ([B] -> A+B)),
Add10 = MakeAdder(10),
Y = Add10(5)               % Y = 15

The returned closure retains the context needed to use A after the outer invocation finishes. This example uses two explicit invocations rather than an implicit partial application.

10.5 Passing Anonymous Definitions as Arguments

A higher-order routine can invoke an anonymous definition using the same conventions as a named callable. For example:

transform(F,[]) = [].
transform(F,[X|Xs]) = [F(X)|transform(F,Xs)].

visit(P,[]) => true.
visit(P,[X|Xs]) =>
    P(X),
    visit(P,Xs).

These definitions can be used as follows:

Ys = transform(([X] -> X*X),[1,2,3]),
visit(([X] => println(X)),Ys)

The transformation returns [1,4,9], and the visit prints those values. Neither routine needs to know whether its argument was constructed from a named callable or an anonymous definition.

10.6 Modules and Call Resolution

For an ordinary named higher-order call whose target is not known at compile time, the runtime system searches the implicitly imported built-in modules basic, math, io, and sys, followed by the other loaded modules in the order returned by loaded_modules(), and then the global module. If the compiler knows the callable name, it binds the call using the ordinary compile-time resolution conventions.

A variable call can be module-qualified, as in m.F(X,Y). For this form, lookup starts in m, followed by the implicitly imported built-in modules and the global module. The module qualifier is an atom, not a variable. The restriction on constructing literal dot-notation terms still applies; qualification is call syntax rather than a module-qualified structure that can be constructed as data. Ordinary named higher-order terms cannot reference private symbols, which are compiled away at compile time.

A closure is associated with its defining code and captured context. Passing it to another module must preserve that association: calls written inside an anonymous body use the defining module’s resolution context. The receiving module can invoke the closure without importing the defining module.

10.7 Implementation of Anonymous Definitions

The compiler introduces a unique dummy name for each anonymous definition and compiles the definition just like a named function or predicate. The dummy name belongs to the global module. The body, however, is compiled in the module in which the anonymous definition occurs. Thus, calls in the body are resolved using the defining module’s definitions and imports, including its private definitions, just as in an ordinary named definition in that module. The return expression of an anonymous function is compiled in the same defining context.

The closure carries the generated name together with the values of the captured context variables. Conceptually, an anonymous function such as

Offset = 10,
F = ([X] -> X+Offset)

is replaced by a closure of the form

F = $lam999([Offset])

and the compiler introduces the function

lam999([Offset],X) = X+Offset => true.

Here, lam999 stands for a unique compiler-generated name, and the first argument carries the captured environment. Invoking F(5) supplies the explicit argument after that environment argument. An anonymous predicate is translated in the same way, using a generated predicate rather than a function. For example, the anonymous predicate

([X] => println(X+Offset))

has a corresponding generated definition of the form

lam1000([Offset],X) => println(X+Offset).

Its closure is conceptually $lam1000([Offset]).

Placing the generated name in the global module allows a receiving module to invoke the closure without importing the defining module. Compiling the body in the defining module preserves the meaning of calls within the body. The generated name is unique, so separately compiled anonymous definitions do not share a dummy name. The names and environment lists shown here illustrate the translation; programs should not depend on their particular spelling or representation.

10.8 Compiled Bodies and Callable Terms

Anonymous bodies are compiled code. They can contain assignments, loops, and conditionals, even though these constructs cannot be preserved as literal callable terms for general call interpretation. An anonymous predicate is therefore useful for passing such a computation to another routine.

The printed representation and generated name of a closure are implementation details. Programs should construct, pass, and invoke the closure rather than inspect or reconstruct its generated representation. No particular runtime lookup strategy or performance guarantee is implied by the anonymous syntax.