Next: , Previous: Conditional expressions, Up: Data-terms   [Contents]


2.15.5 Lambda expressions

A lambda expression is a compound term of one of the following forms

pred(Arg1::Mode1, Arg2::Mode2, …) is Det :- Goal
pred(Arg1::Mode1, Arg2::Mode2, …, DCGMode0, DCGMode1) is Det --> DCGGoal
func(Arg1::Mode1, Arg2::Mode2, …) = (Result::Mode) is Det :- Goal
func(Arg1, Arg2, …) = (Result) is Det :- Goal
func(Arg1, Arg2, …) = Result :- Goal

where Arg1, Arg2, … are zero or more data-terms, Result is a data-term, Mode1, Mode2, … are zero or more modes (see Modes), DCGMode0 and DCGMode1 are modes (see Modes), Det is a determinism (see Determinism), Goal is a goal (see Goals), and DCGGoal is a DCG Goal (see DCG-goals). The ‘:- Goal’ part is optional; if it is not specified, then ‘:- true’ is assumed. A lambda expression denotes a higher-order predicate or function term whose value is the predicate or function of the specified arguments determined by the specified goal. See Higher-order.

A lambda expression introduces a new scope: any variables occurring in the arguments Arg1, Arg2, … are locally quantified, i.e. any occurrences of variables with that name in the lambda expression are considered to name a different variable than any variables with the same name that occur outside of the lambda expression. For variables which occur in Result or Goal, but not in the arguments, the usual Mercury rules for implicit quantification apply (see Implicit quantification).

The form of lambda expression using ‘-->’ as its top level functor is a syntactic abbreviation: an expression of the form

pred(Var1::Mode1, Var2::Mode2, …, DCGMode0, DCGMode1) is Det --> DCGGoal

is equivalent to

pred(Var1::Mode1, Var2::Mode2, …,
    DCGVar0::DCGMode0, DCGVar1::DCGMode1) is Det :- Goal

where DCGVar0 and DCGVar1 are fresh variables, and Goal is the result of ‘DCG-transform(DCGVar0, DCGVar1, DCGGoal)’ where DCG-transform is the function specified in DCG-goals.


Next: , Previous: Conditional expressions, Up: Data-terms   [Contents]