View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2007-2023, University of Amsterdam
    7                              VU University Amsterdam
    8                              SWI-Prolog Solutions b.v.
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(apply,
   38          [ include/3,                  % :Pred, +List, -Ok
   39            exclude/3,                  % :Pred. +List, -NotOk
   40            partition/4,                % :Pred, +List, -Included, -Excluded
   41            partition/5,                % :Pred, +List, ?Less, ?Equal, ?Greater
   42            maplist/2,                  % :Pred, +List
   43            maplist/3,                  % :Pred, ?List, ?List
   44            maplist/4,                  % :Pred, ?List, ?List, ?List
   45            maplist/5,                  % :Pred, ?List, ?List, ?List, ?List
   46            convlist/3,                 % :Pred, +List, -List
   47            foldl/4,                    % :Pred, +List, ?V0, ?V
   48            foldl/5,                    % :Pred, +List1, +List2, ?V0, ?V
   49            foldl/6,                    % :Pred, +List1, +List2, +List3, ?V0, ?V
   50            foldl/7,                    % :Pred, +List1, +List2, +List3, +List4,
   51                                        % ?V0, ?V
   52            scanl/4,                    % :Pred, +List, ?V0, ?Vs
   53            scanl/5,                    % :Pred, +List1, +List2, ?V0, ?Vs
   54            scanl/6,                    % :Pred, +List1, +List2, +List3, ?V0, ?Vs
   55            scanl/7                     % :Pred, +List1, +List2, +List3, +List4,
   56                                        % ?V0, ?Vs
   57          ]).   58:- autoload(library(error),[must_be/2]).   59:- set_prolog_flag(generate_debug_info, false).   60
   61/** <module> Apply predicates on a list
   62
   63This module defines meta-predicates  that  apply   a  predicate  on  all
   64members of a list.
   65
   66All predicates support partial application in the  Goal  argument.  This
   67means that these calls are identical:
   68
   69```
   70?- maplist(=, [foo, foo], [X, Y]).
   71?- maplist(=(foo), [X, Y]).
   72```
   73
   74@see    apply_macros.pl provides compile-time expansion for part of this
   75        library.
   76@see    http://www.cs.otago.ac.nz/staffpriv/ok/pllib.htm
   77@see    Unit test code in tests/library/test_apply.pl
   78@tbd    Add include/4, include/5, exclude/4, exclude/5
   79*/
   80
   81:- meta_predicate
   82    include(1, +, -),
   83    exclude(1, +, -),
   84    partition(1, +, -, -),
   85    partition(2, +, -, -, -),
   86    maplist(1, ?),
   87    maplist(2, ?, ?),
   88    maplist(3, ?, ?, ?),
   89    maplist(4, ?, ?, ?, ?),
   90    convlist(2, +, -),
   91    foldl(3, +, +, -),
   92    foldl(4, +, +, +, -),
   93    foldl(5, +, +, +, +, -),
   94    foldl(6, +, +, +, +, +, -),
   95    scanl(3, +, +, -),
   96    scanl(4, +, +, +, -),
   97    scanl(5, +, +, +, +, -),
   98    scanl(6, +, +, +, +, +, -).   99
  100
  101%!  include(:Goal, +List1, ?List2) is det.
  102%
  103%   Filter elements for which Goal  succeeds.   True  if  List2 contains
  104%   those elements Xi of List1 for which call(Goal, Xi) succeeds.
  105%
  106%   @see exclude/3, partition/4, convlist/3.
  107%   @compat Older versions of SWI-Prolog had sublist/3 with the same
  108%   arguments and semantics.
  109
  110include(_, [], []).
  111include(Goal, [X1|Xs1], Included) :-
  112    (   call(Goal, X1)
  113    ->  Included = [X1|Included1]
  114    ;   Included = Included1
  115    ),
  116    include(Goal, Xs1, Included1).
  117
  118
  119%!  exclude(:Goal, +List1, ?List2) is det.
  120%
  121%   Filter elements for which Goal fails.   True if List2 contains those
  122%   elements Xi of List1 for which call(Goal, Xi) fails.
  123%
  124%   @see include/3, partition/4
  125
  126exclude(_, [], []).
  127exclude(Goal, [X1|Xs1], Included) :-
  128    (   call(Goal, X1)
  129    ->  Included = Included1
  130    ;   Included = [X1|Included1]
  131    ),
  132    exclude(Goal, Xs1, Included1).
  133
  134
  135%!  partition(:Pred, +List, ?Included, ?Excluded) is det.
  136%
  137%   Filter elements of List according  to   Pred.  True  if Included
  138%   contains all elements  for  which   call(Pred,  X)  succeeds and
  139%   Excluded contains the remaining elements.
  140%
  141%   @see include/3, exclude/3, partition/5.
  142
  143partition(_, [], [], []).
  144partition(Pred, [H|T], Incl, Excl) :-
  145    (   call(Pred, H)
  146    ->  Incl = [H|I],
  147        partition(Pred, T, I, Excl)
  148    ;   Excl = [H|E],
  149        partition(Pred, T, Incl, E)
  150    ).
  151
  152
  153%!  partition(:Pred, +List, ?Less, ?Equal, ?Greater) is semidet.
  154%
  155%   Filter List according to Pred in three sets. For each element Xi
  156%   of List, its destination is determined by call(Pred, Xi, Place),
  157%   where Place must be unified to  one   of  =|<|=, =|=|= or =|>|=.
  158%   Pred must be deterministic.
  159%
  160%   @see partition/4
  161
  162partition(_, [], [], [], []).
  163partition(Pred, [H|T], L, E, G) :-
  164    call(Pred, H, Diff),
  165    partition_(Diff, H, Pred, T, L, E, G).
  166
  167partition_(<, H, Pred, T, L, E, G) :-
  168    !,
  169    L = [H|Rest],
  170    partition(Pred, T, Rest, E, G).
  171partition_(=, H, Pred, T, L, E, G) :-
  172    !,
  173    E = [H|Rest],
  174    partition(Pred, T, L, Rest, G).
  175partition_(>, H, Pred, T, L, E, G) :-
  176    !,
  177    G = [H|Rest],
  178    partition(Pred, T, L, E, Rest).
  179partition_(Diff, _, _, _, _, _, _) :-
  180    must_be(oneof([<,=,>]), Diff).
  181
  182
  183                 /*******************************
  184                 *            MAPLIST           *
  185                 *******************************/
  186
  187%!  maplist(:Goal, ?List1).
  188%!  maplist(:Goal, ?List1, ?List2).
  189%!  maplist(:Goal, ?List1, ?List2, ?List3).
  190%!  maplist(:Goal, ?List1, ?List2, ?List3, ?List4).
  191%
  192%   True if Goal is successfully applied on all matching elements of the
  193%   list. The maplist family of predicates is defined as:
  194%
  195%     ```
  196%     maplist(G, [X_11, ..., X_1n],
  197%                [X_21, ..., X_2n],
  198%                ...,
  199%                [X_m1, ..., X_mn]) :-
  200%        call(G, X_11, ..., X_m1),
  201%        call(G, X_12, ..., X_m2),
  202%        ...
  203%        call(G, X_1n, ..., X_mn).
  204%     ```
  205%
  206%   This family of predicates is deterministic iff Goal is deterministic
  207%   and List1 is a proper list, i.e., a list that ends in `[]`.
  208
  209maplist(_, []).
  210maplist(Goal, [Elem|Tail]) :-
  211    call(Goal, Elem),
  212    maplist(Goal, Tail).
  213
  214maplist(_, [], []).
  215maplist(Goal, [Elem1|Tail1], [Elem2|Tail2]) :-
  216    call(Goal, Elem1, Elem2),
  217    maplist(Goal, Tail1, Tail2).
  218
  219maplist(_, [], [], []).
  220maplist(Goal, [Elem1|Tail1], [Elem2|Tail2], [Elem3|Tail3]) :-
  221    call(Goal, Elem1, Elem2, Elem3),
  222    maplist(Goal, Tail1, Tail2, Tail3).
  223
  224maplist(_, [], [], [], []).
  225maplist(Goal, [Elem1|Tail1], [Elem2|Tail2], [Elem3|Tail3], [Elem4|Tail4]) :-
  226    call(Goal, Elem1, Elem2, Elem3, Elem4),
  227    maplist(Goal, Tail1, Tail2, Tail3, Tail4).
  228
  229
  230%!  convlist(:Goal, +ListIn, -ListOut) is det.
  231%
  232%   Similar to maplist/3, but elements for   which call(Goal, ElemIn, _)
  233%   fails are omitted from ListOut.  For example (using library(yall)):
  234%
  235%   ```
  236%   ?- convlist([X,Y]>>(integer(X), Y is X^2),
  237%               [3, 5, foo, 2], L).
  238%   L = [9, 25, 4].
  239%   ```
  240%
  241%   @compat  Also  appears  in  YAP   =|library(maplist)|=  and  SICStus
  242%   =|library(lists)|=.
  243
  244convlist(_, [], []).
  245convlist(Goal, [H0|T0], ListOut) :-
  246    (   call(Goal, H0, H)
  247    ->  ListOut = [H|T],
  248        convlist(Goal, T0, T)
  249    ;   convlist(Goal, T0, ListOut)
  250    ).
  251
  252
  253                 /*******************************
  254                 *            FOLDL             *
  255                 *******************************/
  256
  257%!  foldl(:Goal, +List, +V0, -V).
  258%!  foldl(:Goal, +List1, +List2, +V0, -V).
  259%!  foldl(:Goal, +List1, +List2, +List3, +V0, -V).
  260%!  foldl(:Goal, +List1, +List2, +List3, +List4, +V0, -V).
  261%
  262%   Fold an ensemble of _m_  (0  <=  _m_   <=  4)  lists  of  length _n_
  263%   head-to-tail ("fold-left"), using columns of   _m_  list elements as
  264%   arguments for Goal. The `foldl` family   of predicates is defined as
  265%   follows, with `V0` an initial value and   `V` the final value of the
  266%   folding operation:
  267%
  268%     ```
  269%     foldl(G, [X_11, ..., X_1n],
  270%              [X_21, ..., X_2n],
  271%              ...,
  272%              [X_m1, ..., X_mn], V0, V) :-
  273%        call(G, X_11, ..., X_m1, V0, V1),
  274%        call(G, X_12, ..., X_m2, V1, V2),
  275%        ...
  276%        call(G, X_1n, ..., X_mn, V<n-1>, V).
  277%     ```
  278%
  279%   No implementation for a corresponding `foldr`   is  given. A `foldr`
  280%   implementation would consist in first calling   reverse/2 on each of
  281%   the _m_ input lists, then applying  the appropriate `foldl`. This is
  282%   actually  more  efficient  than  using   a  properly  programmed-out
  283%   recursive algorithm that cannot be   tail-call optimized.
  284
  285foldl(_, [], V, V).
  286foldl(Goal, [H|T], V0, V) :-
  287    call(Goal, H, V0, V1),
  288    foldl(Goal, T, V1, V).
  289
  290
  291foldl(_, [], [], V, V).
  292foldl(Goal, [H1|T1], [H2|T2], V0, V) :-
  293    call(Goal, H1, H2, V0, V1),
  294    foldl(Goal, T1, T2, V1, V).
  295
  296
  297foldl(_, [], [], [], V, V).
  298foldl(Goal, [H1|T1], [H2|T2], [H3|T3], V0, V) :-
  299    call(Goal, H1, H2, H3, V0, V1),
  300    foldl(Goal, T1, T2, T3, V1, V).
  301
  302
  303foldl(_, [], [], [], [], V, V).
  304foldl(Goal, [H1|T1], [H2|T2], [H3|T3], [H4|T4], V0, V) :-
  305    call(Goal, H1, H2, H3, H4, V0, V1),
  306    foldl(Goal, T1, T2, T3, T4, V1, V).
  307
  308
  309                 /*******************************
  310                 *             SCANL            *
  311                 *******************************/
  312
  313%!  scanl(:Goal, +List, +V0, -Values).
  314%!  scanl(:Goal, +List1, +List2, +V0, -Values).
  315%!  scanl(:Goal, +List1, +List2, +List3, +V0, -Values).
  316%!  scanl(:Goal, +List1, +List2, +List3, +List4, +V0, -Values).
  317%
  318%   Scan an ensemble of _m_  (0  <=  _m_   <=  4)  lists  of  length _n_
  319%   head-to-tail ("scan-left"), using columns of   _m_  list elements as
  320%   arguments for Goal. The `scanl` family   of predicates is defined as
  321%   follows, with `V0` an initial value and   `V` the final value of the
  322%   scanning operation:
  323%
  324%     ```
  325%     scanl(G, [X_11, ..., X_1n],
  326%              [X_21, ..., X_2n],
  327%              ...,
  328%              [X_m1, ..., X_mn], V0, [V0, V1, ..., Vn] ) :-
  329%        call(G, X_11, ..., X_m1, V0, V1),
  330%        call(G, X_12, ..., X_m2, V1, V2),
  331%        ...
  332%        call(G, X_1n, ..., X_mn, V<n-1>, Vn).
  333%     ```
  334%
  335%  `scanl` behaves like a `foldl` that collects the sequence of
  336%  values taken on by the `Vx` accumulator into a list.
  337
  338scanl(_, [], V, [V]).
  339scanl(Goal, [H|T], V, [V|VT]) :-
  340    call(Goal, H, V, VH),
  341    scanl(Goal, T, VH, VT).
  342
  343
  344scanl(_, [], [], V, [V]).
  345scanl(Goal, [H1|T1], [H2|T2], V, [V|VT]) :-
  346    call(Goal, H1, H2, V, VH),
  347    scanl(Goal, T1, T2, VH, VT).
  348
  349
  350scanl(_, [], [], [], V, [V]).
  351scanl(Goal, [H1|T1], [H2|T2], [H3|T3], V, [V|VT]) :-
  352    call(Goal, H1, H2, H3, V, VH),
  353    scanl(Goal, T1, T2, T3, VH, VT).
  354
  355
  356scanl(_, [], [], [], [], V, [V]).
  357scanl(Goal, [H1|T1], [H2|T2], [H3|T3], [H4|T4], V, [V|VT]) :-
  358    call(Goal, H1, H2, H3, H4, V, VH),
  359    scanl(Goal, T1, T2, T3, T4, VH, VT).
  360
  361
  362                 /*******************************
  363                 *            SANDBOX           *
  364                 *******************************/
  365
  366:- multifile
  367    sandbox:safe_meta_predicate/1.  368
  369safe_api(Name/Arity, sandbox:safe_meta_predicate(apply:Name/Arity)).
  370
  371term_expansion(safe_api, Clauses) :-
  372    module_property(apply, exports(API)),
  373    maplist(safe_api, API, Clauses).
  374
  375safe_api