View source with raw 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-2015, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(apply,
   37          [ include/3,                  % :Pred, +List, -Ok
   38            exclude/3,                  % :Pred. +List, -NotOk
   39            partition/4,                % :Pred, +List, -Included, -Excluded
   40            partition/5,                % :Pred, +List, ?Less, ?Equal, ?Greater
   41            maplist/2,                  % :Pred, +List
   42            maplist/3,                  % :Pred, ?List, ?List
   43            maplist/4,                  % :Pred, ?List, ?List, ?List
   44            maplist/5,                  % :Pred, ?List, ?List, ?List, ?List
   45            convlist/3,                 % :Pred, +List, -List
   46            foldl/4,                    % :Pred, +List, ?V0, ?V
   47            foldl/5,                    % :Pred, +List1, +List2, ?V0, ?V
   48            foldl/6,                    % :Pred, +List1, +List2, +List3, ?V0, ?V
   49            foldl/7,                    % :Pred, +List1, +List2, +List3, +List4,
   50                                        % ?V0, ?V
   51            scanl/4,                    % :Pred, +List, ?V0, ?Vs
   52            scanl/5,                    % :Pred, +List1, +List2, ?V0, ?Vs
   53            scanl/6,                    % :Pred, +List1, +List2, +List3, ?V0, ?Vs
   54            scanl/7                     % :Pred, +List1, +List2, +List3, +List4,
   55                                        % ?V0, ?Vs
   56          ]).   57:- autoload(library(error),[must_be/2]).

Apply predicates on a list

This module defines meta-predicates that apply a predicate on all members of a list.

See also
- apply_macros.pl provides compile-time expansion for part of this library.
- http://www.cs.otago.ac.nz/staffpriv/ok/pllib.htm
To be done
- Add include/4, include/5, exclude/4, exclude/5 */
   70:- meta_predicate
   71    include(1, +, -),
   72    exclude(1, +, -),
   73    partition(1, +, -, -),
   74    partition(2, +, -, -, -),
   75    maplist(1, ?),
   76    maplist(2, ?, ?),
   77    maplist(3, ?, ?, ?),
   78    maplist(4, ?, ?, ?, ?),
   79    convlist(2, +, -),
   80    foldl(3, +, +, -),
   81    foldl(4, +, +, +, -),
   82    foldl(5, +, +, +, +, -),
   83    foldl(6, +, +, +, +, +, -),
   84    scanl(3, +, +, -),
   85    scanl(4, +, +, +, -),
   86    scanl(5, +, +, +, +, -),
   87    scanl(6, +, +, +, +, +, -).
 include(:Goal, +List1, ?List2) is det
Filter elements for which Goal succeeds. True if List2 contains those elements Xi of List1 for which call(Goal, Xi) succeeds.
See also
- Older versions of SWI-Prolog had sublist/3 with the same arguments and semantics.
   98include(Goal, List, Included) :-
   99    include_(List, Goal, Included).
  100
  101include_([], _, []).
  102include_([X1|Xs1], P, Included) :-
  103    (   call(P, X1)
  104    ->  Included = [X1|Included1]
  105    ;   Included = Included1
  106    ),
  107    include_(Xs1, P, Included1).
 exclude(:Goal, +List1, ?List2) is det
Filter elements for which Goal fails. True if List2 contains those elements Xi of List1 for which call(Goal, Xi) fails.
  115exclude(Goal, List, Included) :-
  116    exclude_(List, Goal, Included).
  117
  118exclude_([], _, []).
  119exclude_([X1|Xs1], P, Included) :-
  120    (   call(P, X1)
  121    ->  Included = Included1
  122    ;   Included = [X1|Included1]
  123    ),
  124    exclude_(Xs1, P, Included1).
 partition(:Pred, +List, ?Included, ?Excluded) is det
Filter elements of List according to Pred. True if Included contains all elements for which call(Pred, X) succeeds and Excluded contains the remaining elements.
  133partition(Pred, List, Included, Excluded) :-
  134    partition_(List, Pred, Included, Excluded).
  135
  136partition_([], _, [], []).
  137partition_([H|T], Pred, Incl, Excl) :-
  138    (   call(Pred, H)
  139    ->  Incl = [H|I],
  140        partition_(T, Pred, I, Excl)
  141    ;   Excl = [H|E],
  142        partition_(T, Pred, Incl, E)
  143    ).
 partition(:Pred, +List, ?Less, ?Equal, ?Greater) is semidet
Filter List according to Pred in three sets. For each element Xi of List, its destination is determined by call(Pred, Xi, Place), where Place must be unified to one of <, = or >. Pred must be deterministic.
  153partition(Pred, List, Less, Equal, Greater) :-
  154    partition_(List, Pred, Less, Equal, Greater).
  155
  156partition_([], _, [], [], []).
  157partition_([H|T], Pred, L, E, G) :-
  158    call(Pred, H, Diff),
  159    partition_(Diff, H, Pred, T, L, E, G).
  160
  161partition_(<, H, Pred, T, L, E, G) :-
  162    !,
  163    L = [H|Rest],
  164    partition_(T, Pred, Rest, E, G).
  165partition_(=, H, Pred, T, L, E, G) :-
  166    !,
  167    E = [H|Rest],
  168    partition_(T, Pred, L, Rest, G).
  169partition_(>, H, Pred, T, L, E, G) :-
  170    !,
  171    G = [H|Rest],
  172    partition_(T, Pred, L, E, Rest).
  173partition_(Diff, _, _, _, _, _, _) :-
  174    must_be(oneof([<,=,>]), Diff).
  175
  176
  177                 /*******************************
  178                 *          MAPLIST/2...        *
  179                 *******************************/
 maplist(:Goal, ?List1)
 maplist(:Goal, ?List1, ?List2)
 maplist(:Goal, ?List1, ?List2, ?List3)
 maplist(:Goal, ?List1, ?List2, ?List3, ?List4)
True if Goal is successfully applied on all matching elements of the list. The maplist family of predicates is defined as:
maplist(P, [X11,...,X1n], ..., [Xm1,...,Xmn]) :-
    P(X11, ..., Xm1),
    ...
    P(X1n, ..., Xmn).

This family of predicates is deterministic iff Goal is deterministic and List1 is a proper list, i.e., a list that ends in [].

  199maplist(Goal, List) :-
  200    maplist_(List, Goal).
  201
  202maplist_([], _).
  203maplist_([Elem|Tail], Goal) :-
  204    call(Goal, Elem),
  205    maplist_(Tail, Goal).
  206
  207maplist(Goal, List1, List2) :-
  208    maplist_(List1, List2, Goal).
  209
  210maplist_([], [], _).
  211maplist_([Elem1|Tail1], [Elem2|Tail2], Goal) :-
  212    call(Goal, Elem1, Elem2),
  213    maplist_(Tail1, Tail2, Goal).
  214
  215maplist(Goal, List1, List2, List3) :-
  216    maplist_(List1, List2, List3, Goal).
  217
  218maplist_([], [], [], _).
  219maplist_([Elem1|Tail1], [Elem2|Tail2], [Elem3|Tail3], Goal) :-
  220    call(Goal, Elem1, Elem2, Elem3),
  221    maplist_(Tail1, Tail2, Tail3, Goal).
  222
  223maplist(Goal, List1, List2, List3, List4) :-
  224    maplist_(List1, List2, List3, List4, Goal).
  225
  226maplist_([], [], [], [], _).
  227maplist_([Elem1|Tail1], [Elem2|Tail2], [Elem3|Tail3], [Elem4|Tail4], Goal) :-
  228    call(Goal, Elem1, Elem2, Elem3, Elem4),
  229    maplist_(Tail1, Tail2, Tail3, Tail4, Goal).
 convlist(:Goal, +ListIn, -ListOut) is det
Similar to maplist/3, but elements for which call(Goal, ElemIn, _) fails are omitted from ListOut. For example (using library(yall)):
?- convlist([X,Y]>>(integer(X), Y is X^2),
            [3, 5, 4.4, 2], L).
L = [9, 25, 4].
Compatibility
- Also appears in YAP library(maplist) and SICStus library(lists).
  246convlist(Goal, ListIn, ListOut) :-
  247    convlist_(ListIn, ListOut, Goal).
  248
  249convlist_([], [], _).
  250convlist_([H0|T0], ListOut, Goal) :-
  251    (   call(Goal, H0, H)
  252    ->  ListOut = [H|T],
  253        convlist_(T0, T, Goal)
  254    ;   convlist_(T0, ListOut, Goal)
  255    ).
  256
  257
  258                 /*******************************
  259                 *            FOLDL             *
  260                 *******************************/
 foldl(:Goal, +List, +V0, -V)
 foldl(:Goal, +List1, +List2, +V0, -V)
 foldl(:Goal, +List1, +List2, +List3, +V0, -V)
 foldl(:Goal, +List1, +List2, +List3, +List4, +V0, -V)
Fold a list, using arguments of the list as left argument. The foldl family of predicates is defined by:
foldl(P, [X11,...,X1n], ..., [Xm1,...,Xmn], V0, Vn) :-
      P(X11, ..., Xm1, V0, V1),
      ...
      P(X1n, ..., Xmn, V', Vn).
  277foldl(Goal, List, V0, V) :-
  278    foldl_(List, Goal, V0, V).
  279
  280foldl_([], _, V, V).
  281foldl_([H|T], Goal, V0, V) :-
  282    call(Goal, H, V0, V1),
  283    foldl_(T, Goal, V1, V).
  284
  285
  286foldl(Goal, List1, List2, V0, V) :-
  287    foldl_(List1, List2, Goal, V0, V).
  288
  289foldl_([], [], _, V, V).
  290foldl_([H1|T1], [H2|T2], Goal, V0, V) :-
  291    call(Goal, H1, H2, V0, V1),
  292    foldl_(T1, T2, Goal, V1, V).
  293
  294
  295foldl(Goal, List1, List2, List3, V0, V) :-
  296    foldl_(List1, List2, List3, Goal, V0, V).
  297
  298foldl_([], [], [], _, V, V).
  299foldl_([H1|T1], [H2|T2], [H3|T3], Goal, V0, V) :-
  300    call(Goal, H1, H2, H3, V0, V1),
  301    foldl_(T1, T2, T3, Goal, V1, V).
  302
  303
  304foldl(Goal, List1, List2, List3, List4, V0, V) :-
  305    foldl_(List1, List2, List3, List4, Goal, V0, V).
  306
  307foldl_([], [], [], [], _, V, V).
  308foldl_([H1|T1], [H2|T2], [H3|T3], [H4|T4], Goal, V0, V) :-
  309    call(Goal, H1, H2, H3, H4, V0, V1),
  310    foldl_(T1, T2, T3, T4, Goal, V1, V).
  311
  312
  313                 /*******************************
  314                 *             SCANL            *
  315                 *******************************/
 scanl(:Goal, +List, +V0, -Values)
 scanl(:Goal, +List1, +List2, +V0, -Values)
 scanl(:Goal, +List1, +List2, +List3, +V0, -Values)
 scanl(:Goal, +List1, +List2, +List3, +List4, +V0, -Values)
Left scan of list. The scanl family of higher order list operations is defined by:
scanl(P, [X11,...,X1n], ..., [Xm1,...,Xmn], V0,
      [V0,V1,...,Vn]) :-
      P(X11, ..., Xm1, V0, V1),
      ...
      P(X1n, ..., Xmn, V', Vn).
  333scanl(Goal, List, V0, [V0|Values]) :-
  334    scanl_(List, Goal, V0, Values).
  335
  336scanl_([], _, _, []).
  337scanl_([H|T], Goal, V, [VH|VT]) :-
  338    call(Goal, H, V, VH),
  339    scanl_(T, Goal, VH, VT).
  340
  341
  342scanl(Goal, List1, List2, V0, [V0|Values]) :-
  343    scanl_(List1, List2, Goal, V0, Values).
  344
  345scanl_([], [], _, _, []).
  346scanl_([H1|T1], [H2|T2], Goal, V, [VH|VT]) :-
  347    call(Goal, H1, H2, V, VH),
  348    scanl_(T1, T2, Goal, VH, VT).
  349
  350
  351scanl(Goal, List1, List2, List3, V0, [V0|Values]) :-
  352    scanl_(List1, List2, List3, Goal, V0, Values).
  353
  354scanl_([], [], [], _, _, []).
  355scanl_([H1|T1], [H2|T2], [H3|T3], Goal, V, [VH|VT]) :-
  356    call(Goal, H1, H2, H3, V, VH),
  357    scanl_(T1, T2, T3, Goal, VH, VT).
  358
  359
  360scanl(Goal, List1, List2, List3, List4, V0, [V0|Values]) :-
  361    scanl_(List1, List2, List3, List4, Goal, V0, Values).
  362
  363scanl_([], [], [], [], _, _, []).
  364scanl_([H1|T1], [H2|T2], [H3|T3], [H4|T4], Goal, V, [VH|VT]) :-
  365    call(Goal, H1, H2, H3, H4, V, VH),
  366    scanl_(T1, T2, T3, T4, Goal, VH, VT).
  367
  368
  369                 /*******************************
  370                 *            SANDBOX           *
  371                 *******************************/
  372
  373:- multifile
  374    sandbox:safe_meta_predicate/1.  375
  376safe_api(Name/Arity, sandbox:safe_meta_predicate(apply:Name/Arity)).
  377
  378term_expansion(safe_api, Clauses) :-
  379    module_property(apply, exports(API)),
  380    maplist(safe_api, API, Clauses).
  381
  382safe_api