/usr/lib/swi-prolog/library/yall.pl is in swi-prolog-nox 7.6.4+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | /* Part of SWI-Prolog
Author: Paulo Moura
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2015, Paulo Moura, Kyndi Inc., VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(yall,
[ (>>)/2, (>>)/3, (>>)/4, (>>)/5, (>>)/6, (>>)/7, (>>)/8, (>>)/9,
(/)/2, (/)/3, (/)/4, (/)/5, (/)/6, (/)/7, (/)/8, (/)/9,
lambda_calls/2, % +LambdaExt, -Goal
lambda_calls/3, % +Lambda, +Args, -Goal
is_lambda/1 % @Term
]).
:- use_module(library(error)).
:- use_module(library(lists)).
:- meta_predicate
'>>'(?, 0),
'>>'(?, :, ?),
'>>'(?, :, ?, ?),
'>>'(?, :, ?, ?, ?),
'>>'(?, :, ?, ?, ?, ?),
'>>'(?, :, ?, ?, ?, ?, ?),
'>>'(?, :, ?, ?, ?, ?, ?, ?),
'>>'(?, :, ?, ?, ?, ?, ?, ?, ?).
:- meta_predicate
'/'(?, 0),
'/'(?, 1, ?),
'/'(?, 2, ?, ?),
'/'(?, 3, ?, ?, ?),
'/'(?, 4, ?, ?, ?, ?),
'/'(?, 5, ?, ?, ?, ?, ?),
'/'(?, 6, ?, ?, ?, ?, ?, ?),
'/'(?, 7, ?, ?, ?, ?, ?, ?, ?).
/** <module> Lambda expressions
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.
@tbd Extend optimization support
@author Paulo Moura and Jan Wielemaker
*/
%! >>(+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.
%
% @arg 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.
'>>'(Parms, Lambda) :-
unify_lambda_parameters(Parms, [],
ExtraArgs, Lambda, LambdaCopy),
Goal =.. [call, LambdaCopy| ExtraArgs],
call(Goal).
'>>'(Parms, Lambda, A1) :-
unify_lambda_parameters(Parms, [A1],
ExtraArgs, Lambda, LambdaCopy),
Goal =.. [call, LambdaCopy| ExtraArgs],
call(Goal).
'>>'(Parms, Lambda, A1, A2) :-
unify_lambda_parameters(Parms, [A1,A2],
ExtraArgs, Lambda, LambdaCopy),
Goal =.. [call, LambdaCopy| ExtraArgs],
call(Goal).
'>>'(Parms, Lambda, A1, A2, A3) :-
unify_lambda_parameters(Parms, [A1,A2,A3],
ExtraArgs, Lambda, LambdaCopy),
Goal =.. [call, LambdaCopy| ExtraArgs],
call(Goal).
'>>'(Parms, Lambda, A1, A2, A3, A4) :-
unify_lambda_parameters(Parms, [A1,A2,A3,A4],
ExtraArgs, Lambda, LambdaCopy),
Goal =.. [call, LambdaCopy| ExtraArgs],
call(Goal).
'>>'(Parms, Lambda, A1, A2, A3, A4, A5) :-
unify_lambda_parameters(Parms, [A1,A2,A3,A4,A5],
ExtraArgs, Lambda, LambdaCopy),
Goal =.. [call, LambdaCopy| ExtraArgs],
call(Goal).
'>>'(Parms, Lambda, A1, A2, A3, A4, A5, A6) :-
unify_lambda_parameters(Parms, [A1,A2,A3,A4,A5,A6],
ExtraArgs, Lambda, LambdaCopy),
Goal =.. [call, LambdaCopy| ExtraArgs],
call(Goal).
'>>'(Parms, Lambda, A1, A2, A3, A4, A5, A6, A7) :-
unify_lambda_parameters(Parms, [A1,A2,A3,A4,A5,A6,A7],
ExtraArgs, Lambda, LambdaCopy),
Goal =.. [call, LambdaCopy| ExtraArgs],
call(Goal).
%! /(+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 example
%
% ==
% p(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)
% ==
'/'(Free, Lambda) :-
lambda_free(Free),
copy_term_nat(Free+Lambda, Free+LambdaCopy),
call(LambdaCopy).
'/'(Free, Lambda, A1) :-
lambda_free(Free),
copy_term_nat(Free+Lambda, Free+LambdaCopy),
call(LambdaCopy, A1).
'/'(Free, Lambda, A1, A2) :-
lambda_free(Free),
copy_term_nat(Free+Lambda, Free+LambdaCopy),
call(LambdaCopy, A1, A2).
'/'(Free, Lambda, A1, A2, A3) :-
lambda_free(Free),
copy_term_nat(Free+Lambda, Free+LambdaCopy),
call(LambdaCopy, A1, A2, A3).
'/'(Free, Lambda, A1, A2, A3, A4) :-
lambda_free(Free),
copy_term_nat(Free+Lambda, Free+LambdaCopy),
call(LambdaCopy, A1, A2, A3, A4).
'/'(Free, Lambda, A1, A2, A3, A4, A5) :-
lambda_free(Free),
copy_term_nat(Free+Lambda, Free+LambdaCopy),
call(LambdaCopy, A1, A2, A3, A4, A5).
'/'(Free, Lambda, A1, A2, A3, A4, A5, A6) :-
lambda_free(Free),
copy_term_nat(Free+Lambda, Free+LambdaCopy),
call(LambdaCopy, A1, A2, A3, A4, A5, A6).
'/'(Free, Lambda, A1, A2, A3, A4, A5, A6, A7) :-
lambda_free(Free),
copy_term_nat(Free+Lambda, Free+LambdaCopy),
call(LambdaCopy, A1, A2, A3, A4, A5, A6, A7).
%! unify_lambda_parameters(+ParmsAndFree, +Args, -CallArgs,
%! +Lambda, -LambdaCopy) is det.
%
% @arg ParmsAndFree is the first argumen of `>>`, either a list
% of parameters or a term `{Free}/Params`.
% @arg Args is a list of input parameters, args 3.. from `>>`
% @arg CallArgs are the calling arguments for the Lambda
% expression. I.e., we call call(LambdaCopy, CallArgs).
unify_lambda_parameters(Parms, _Args, _ExtraArgs, _Lambda, _LambdaCopy) :-
var(Parms),
!,
instantiation_error(Parms).
unify_lambda_parameters(Free/Parms, Args, ExtraArgs, Lambda, LambdaCopy) :-
!,
lambda_free(Free),
must_be(list, Parms),
copy_term_nat(Free/Parms>>Lambda, Free/ParmsCopy>>LambdaCopy),
unify_lambda_parameters_(ParmsCopy, Args, ExtraArgs,
Free/Parms>>Lambda).
unify_lambda_parameters(Parms, Args, ExtraArgs, Lambda, LambdaCopy) :-
must_be(list, Parms),
copy_term_nat(Parms>>Lambda, ParmsCopy>>LambdaCopy),
unify_lambda_parameters_(ParmsCopy, Args, ExtraArgs,
Parms>>Lambda).
unify_lambda_parameters_([], ExtraArgs, ExtraArgs, _) :- !.
unify_lambda_parameters_([Parm|Parms], [Arg|Args], ExtraArgs, Culprit) :-
!,
Parm = Arg,
unify_lambda_parameters_(Parms, Args, ExtraArgs, Culprit).
unify_lambda_parameters_(_,_,_,Culprit) :-
domain_error(lambda_parameters, Culprit).
lambda_free(Free) :-
var(Free),
!,
instantiation_error(Free).
lambda_free({_}) :- !.
lambda_free({}) :- !.
lambda_free(Free) :-
type_error(lambda_free, Free).
%! expand_lambda(+Goal, -Head) is semidet.
%
% True if Goal is a sufficiently instantiated Lambda expression
% that is compiled to the predicate Head. The predicate Head is
% added to the current compilation context using
% compile_aux_clauses/1.
expand_lambda(Goal, Head) :-
Goal =.. ['>>', Parms, Lambda| ExtraArgs],
is_callable(Lambda),
nonvar(Parms),
lambda_functor(Parms>>Lambda, Functor),
( Parms = Free/ExtraArgs
-> is_lambda_free(Free),
free_to_list(Free, FreeList)
; Parms = ExtraArgs,
FreeList = []
),
append(FreeList, ExtraArgs, Args),
Head =.. [Functor|Args],
compile_aux_clause_if_new(Head, Lambda).
expand_lambda(Goal, Head) :-
Goal =.. ['/', Free, Closure|ExtraArgs],
is_lambda_free(Free),
is_callable(Closure),
free_to_list(Free, FreeList),
lambda_functor(Free/Closure, Functor),
append(FreeList, ExtraArgs, Args),
Head =.. [Functor|Args],
Closure =.. [ClosureFunctor|ClosureArgs],
append(ClosureArgs, ExtraArgs, LambdaArgs),
Lambda =.. [ClosureFunctor|LambdaArgs],
compile_aux_clause_if_new(Head, Lambda).
lambda_functor(Term, Functor) :-
copy_term_nat(Term, Copy),
variant_sha1(Copy, Functor0),
atom_concat('__aux_yall_', Functor0, Functor).
free_to_list({}, []).
free_to_list({VarsConj}, Vars) :-
conjunction_to_list(VarsConj, Vars).
conjunction_to_list(Term, [Term]) :-
var(Term),
!.
conjunction_to_list((Term, Conjunction), [Term|Terms]) :-
!,
conjunction_to_list(Conjunction, Terms).
conjunction_to_list(Term, [Term]).
compile_aux_clause_if_new(Head, Lambda) :-
prolog_load_context(module, Context),
( predicate_property(Context:Head, defined)
-> true
; compile_aux_clauses([(Head :- Lambda)])
).
lambda_like(Goal) :-
compound(Goal),
compound_name_arity(Goal, Name, Arity),
lambda_functor(Name),
Arity >= 2.
lambda_functor(>>).
lambda_functor(/).
:- dynamic system:goal_expansion/2.
:- multifile system:goal_expansion/2.
system:goal_expansion(Goal, Head) :-
lambda_like(Goal),
prolog_load_context(source, _),
\+ current_prolog_flag(xref, true),
expand_lambda(Goal, Head).
%! is_lambda(@Term) is semidet.
%
% True if Term is a valid Lambda expression.
is_lambda(Term) :-
compound(Term),
compound_name_arguments(Term, Name, Args),
is_lambda(Name, Args).
is_lambda(>>, [Params,Lambda|_]) :-
is_lamdba_params(Params),
is_callable(Lambda).
is_lambda(/, [Free,Lambda|_]) :-
is_lambda_free(Free),
is_callable(Lambda).
is_lamdba_params(Var) :-
var(Var), !, fail.
is_lamdba_params(Free/Params) :-
!,
is_lambda_free(Free),
is_list(Params).
is_lambda_free(Free) :-
nonvar(Free), !, (Free = {_} -> true ; Free == {}).
is_callable(Term) :-
strip_module(Term, _, Goal),
callable(Goal).
%! lambda_calls(+LambdaExpression, -Goal) is det.
%! lambda_calls(+LambdaExpression, +ExtraArgs, -Goal) is det.
%
% 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.
lambda_calls(LambdaExtended, Goal) :-
compound(LambdaExtended),
compound_name_arguments(LambdaExtended, Name, [A1,A2|Extra]),
lambda_functor(Name),
compound_name_arguments(Lambda, Name, [A1,A2]),
lambda_calls(Lambda, Extra, Goal).
lambda_calls(Lambda, Extra, Goal) :-
integer(Extra),
!,
length(ExtraVars, Extra),
lambda_calls_(Lambda, ExtraVars, Goal).
lambda_calls(Lambda, Extra, Goal) :-
must_be(list, Extra),
lambda_calls_(Lambda, Extra, Goal).
lambda_calls_(Params>>Lambda, Args, Goal) :-
unify_lambda_parameters(Params, Args, ExtraArgs, Lambda, LambdaCopy),
extend(LambdaCopy, ExtraArgs, Goal).
lambda_calls_(Free/Lambda, ExtraArgs, Goal) :-
copy_term_nat(Free+Lambda, Free+LambdaCopy),
extend(LambdaCopy, ExtraArgs, Goal).
extend(Var, _, _) :-
var(Var),
!,
instantiation_error(Var).
extend(Cyclic, _, _) :-
cyclic_term(Cyclic),
!,
type_error(acyclic_term, Cyclic).
extend(M:Goal0, Extra, M:Goal) :-
!,
extend(Goal0, Extra, Goal).
extend(Goal0, Extra, Goal) :-
atom(Goal0),
!,
Goal =.. [Goal0|Extra].
extend(Goal0, Extra, Goal) :-
compound(Goal0),
!,
compound_name_arguments(Goal0, Name, Args0),
append(Args0, Extra, Args),
compound_name_arguments(Goal, Name, Args).
/*******************************
* SYNTAX HIGHLIGHTING *
*******************************/
:- multifile prolog_colour:goal_colours/2.
yall_colours(Lambda, built_in-[classify,body(Goal)|ArgSpecs]) :-
catch(lambda_calls(Lambda, Goal), _, fail),
Lambda =.. [>>,_,_|Args],
classify_extra(Args, ArgSpecs).
classify_extra([], []).
classify_extra([_|T0], [classify|T]) :-
classify_extra(T0, T).
prolog_colour:goal_colours(Goal, Spec) :-
lambda_like(Goal),
yall_colours(Goal, Spec).
/*******************************
* XREF SUPPORT *
*******************************/
:- multifile prolog:called_by/4.
prolog:called_by(Lambda, yall, _, [Goal]) :-
lambda_like(Lambda),
catch(lambda_calls(Lambda, Goal), _, fail).
/*******************************
* SANDBOX SUPPORT *
*******************************/
:- multifile
sandbox:safe_meta_predicate/1,
sandbox:safe_meta/2.
sandbox:safe_meta_predicate(yall:(/)/2).
sandbox:safe_meta_predicate(yall:(/)/3).
sandbox:safe_meta_predicate(yall:(/)/4).
sandbox:safe_meta_predicate(yall:(/)/5).
sandbox:safe_meta_predicate(yall:(/)/6).
sandbox:safe_meta_predicate(yall:(/)/7).
sandbox:safe_meta(yall:Lambda, [Goal]) :-
compound(Lambda),
compound_name_arity(Lambda, >>, Arity),
Arity >= 2,
lambda_calls(Lambda, Goal).
|