Did you know ... | Search Documentation: |

library(yall): Lambda expressions |

- Documentation
- Reference manual
- The SWI-Prolog library
- library(aggregate): Aggregation operators on backtrackable predicates
- library(ansi_term): Print decorated text to ANSI consoles
- library(apply): Apply predicates on a list
- library(assoc): Association lists
- library(broadcast): Broadcast and receive event notifications
- library(charsio): I/O on Lists of Character Codes
- library(check): Consistency checking
- library(clpb): CLP(B): Constraint Logic Programming over Boolean Variables
- library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
- library(clpqr): Constraint Logic Programming over Rationals and Reals
- library(csv): Process CSV (Comma-Separated Values) data
- library(dcg/basics): Various general DCG utilities
- library(dcg/high_order): High order grammar operations
- library(debug): Print debug messages and test assertions
- library(dicts): Dict utilities
- library(error): Error generating support
- library(gensym): Generate unique identifiers
- library(intercept): Intercept and signal interface
- library(iostream): Utilities to deal with streams
- library(lists): List Manipulation
- library(main): Provide entry point for scripts
- library(nb_set): Non-backtrackable set
- library(www_browser): Activating your Web-browser
- library(occurs): Finding and counting sub-terms
- library(option): Option list processing
- library(optparse): command line parsing
- library(ordsets): Ordered set manipulation
- library(pairs): Operations on key-value lists
- library(persistency): Provide persistent dynamic predicates
- library(pio): Pure I/O
- library(predicate_options): Declare option-processing of predicates
- library(prolog_jiti): Just In Time Indexing (JITI) utilities
- library(prolog_pack): A package manager for Prolog
- library(prolog_xref): Prolog cross-referencer data collection
- library(quasi_quotations): Define Quasi Quotation syntax
- library(random): Random numbers
- library(readutil): Read utilities
- library(record): Access named fields in a term
- library(registry): Manipulating the Windows registry
- library(settings): Setting management
- library(simplex): Solve linear programming problems
- library(solution_sequences): Modify solution sequences
- library(thread): High level thread primitives
- library(thread_pool): Resource bounded thread management
- library(ugraphs): Unweighted Graphs
- library(url): Analysing and constructing URL
- library(varnumbers): Utilities for numbered terms
- library(yall): Lambda expressions

- The SWI-Prolog library
- Packages

- Reference manual

- author
- Paulo Moura and Jan Wielemaker
- To be done
- Extend optimization support

Prolog realizes *high-order* programming with meta-calling. The
core predicate of this is call/1,
which simply calls its argument. This can be used to define higher-order
predicates such as ignore/1
or forall/2. The call/N
construct calls a *closure* with N-1 *additional arguments*.
This is used to define higher-order predicates such as the maplist/N
family or foldl/N.

The problem with higher order predicates based on call/N is that the
additional arguments are always added to the end of the closure's
argument list. This often requires defining trivial helper predicates to
get the argument order right. For example, if you want to add a common
postfix to a list of atoms you need to apply
`atom_concat(In,Postfix,Out)`

, but `maplist(x(PostFix),ListIn,ListOut)`

calls `x(PostFix,In,Out)`

. This is where this library comes
in, which allows us to write

?- maplist([In,Out]>>atom_concat(In,'_p',Out), [a,b], ListOut). ListOut = [a_p, b_p].

The `{...}`

specifies which variables are *shared*
between the lambda and the context. This allows us to write the code
below. Without the
`{PostFix}`

a free variable would be passed to atom_concat/3.

add_postfix(PostFix, ListIn, ListOut) :- maplist({PostFix}/[In,Out]>>atom_concat(In,PostFix,Out), ListIn, ListOut).

This introduces the second application area of lambda expressions:
the ability to stop binding variables in the context. This features
shines when combined with bagof/3
or setof/3 where
you normally have to specify the the variables in whose binding you are *not*
interested using the
`Var^Goal`

construct (marking `Var` as existential
quantified). Lambdas allow doing the reverse: specify the variables in
which you are interested.

Lambda expressions use the syntax below

{...}/[...]>>Goal.

The `{...}`

optional part is used for lambda-free
variables. The order of variables doesn't matter hence the `{...}`

set notation.

The `[...]`

optional part lists lambda parameters. Here
order of variables matters hence the list notation.

As `/`

and `>>`

are standard infix
operators, no new operators are added by this library. An advantage of
this syntax is that we can simply unify a lambda expression with
Free/Parameters`>>`

Lambda to access each of its
components. Spaces in the lambda expression are not a problem although
the goal may need to be written between ()'s. Goals that are qualified
by a module prefix also need to be wrapped inside parentheses.

Combined with `library(apply_macros)`

, `library(yall)`

allows writing one-liners for many list operations that have the same
performance as hand written code.

The module name, *yall*, stands for Yet Another Lambda Library.

This module implements Logtalk's lambda expressions syntax. The development of this module was sponsored by Kyndi, Inc.

`+Parameters`**>>**`+Lambda`**>>**(`+Parameters, +Lambda, ?A1`)**>>**(`+Parameters, +Lambda, ?A1, ?A2`)**>>**(`+Parameters, +Lambda, ?A1, ?A2, ?A3`)**>>**(`+Parameters, +Lambda, ?A1, ?A2, ?A3, ?A4`)**>>**(`+Parameters, +Lambda, ?A1, ?A2, ?A3, ?A4, ?A5`)**>>**(`+Parameters, +Lambda, ?A1, ?A2, ?A3, ?A4, ?A5, ?A6`)**>>**(`+Parameters, +Lambda, ?A1, ?A2, ?A3, ?A4, ?A5, ?A6, ?A7`)- Calls a copy of
`Lambda`. This is similar to`call(Lambda,A1,...)`

, but arguments are reordered according to the list`Parameters`:- The first
`length(Parameters)`

arguments from`A1`, ... are unified with (a copy of)`Parameters`, which*may*share them with variables in`Lambda`. - Possible excess arguments are passed by position.

`Parameters`is either a plain list of parameters or a term `{Free}/List`

.`Free`represents variables that are shared between the context and the`Lambda`term. This is needed for compiling`Lambda`expressions. - The first
`+Free`**/**`:Lambda`**/**(`+Free, :Lambda, ?A1`)**/**(`+Free, :Lambda, ?A1, ?A2`)**/**(`+Free, :Lambda, ?A1, ?A2, ?A3`)**/**(`+Free, :Lambda, ?A1, ?A2, ?A3, ?A4`)**/**(`+Free, :Lambda, ?A1, ?A2, ?A3, ?A4, ?A5`)**/**(`+Free, :Lambda, ?A1, ?A2, ?A3, ?A4, ?A5, ?A6`)**/**(`+Free, :Lambda, ?A1, ?A2, ?A3, ?A4, ?A5, ?A6, ?A7`)- Shorthand for
`Free/[]>>Lambda`

. This is the same as applying call/N on`Lambda`, except that only variables appearing in`Free`are bound by the call. For examplep(1,a). p(2,b). ?- {X}/p(X,Y). X = 1; X = 2.

This can in particularly be combined with bagof/3 and setof/3 to

*select*particular variables to be concerned rather than using existential quantification (^/2) to*exclude*variables. For example, the two calls below are equivalent.setof(X, Y^p(X,Y), Xs) setof(X, {X}/p(X,_), Xs)

- [semidet]
**is_lambda**(`@Term`) - True if
`Term`is a valid Lambda expression. - [det]
**lambda_calls**(`+LambdaExpression, -Goal`) - [det]
**lambda_calls**(`+LambdaExpression, +ExtraArgs, -Goal`) `Goal`is the goal called if call/N is applied to`LambdaExpression`, where`ExtraArgs`are the additional arguments to call/N.`ExtraArgs`can be an integer or a list of concrete arguments. This predicate is used for cross-referencing and code highlighting.

Tag confusing pages with **doc-needs-help**|Tags are associated to your profile if you are logged in|Report abuse

Tags:

- doc-needs-help

It's a wonderful little module, but why the name "yall"?

Stumbled upon the following two links and now I am here

https://billwadge.wordpress.com/2016/01/18/extensional-higher-order-prolog/

https://github.com/acharal/hopes

I am wondering how they compare to swi

Perfect! This is **exactly** what I was looking for. This needs to be better advertised! I stumbled across this after writing in my code:

% Geeze, get lambdas, already!! % strings_concat(Strings, X):- foldl(A\B\C string_concat(B,A,C), Strings, "", X). % Hmm.. this should be possible in swipl. After all they implemented dictionaries.

And then I thought let me google "swipl lambda" ....

Finally, there is the package

http://eu.swi-prolog.org/pack/file_details/lambda/prolog/lambda.pl

(which is thus subsumed by the present library)

It references

http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord

which is also good to peruse.

Please add the live link to "maplist:"

http://eu.swi-prolog.org/pldoc/doc_for?object=maplist/3

...and some slight typos:

This introduces the second application area of lambda expressions: the ability to stop binding variables in the context. This features shines when combined with bagof/3 or setof/3 where you normally have to specify **the the** variables in whose binding you are not interested using the Var^Goal construct (marking Var as existential*ly* quantified). Lambda **expressions** allow doing the reverse: specify the variables in which you are interested.

There is a also a shift in the meaning of "closure" and "lambda expression" as these are really "partially bound predicates" not "partially bound functions".