1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera Menendez
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2016, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(mapnargs, [mapnargs/2,
   36                     mapnargs/3,
   37                     mapnargs/4,
   38                     mapnargs/5,
   39                     mapnargs/6,
   40                     mapnargs/7]).   41
   42mapnargs_(N, Goal, T) :-
   43    arg(N, T, A),
   44    !,
   45    call(Goal, N, A),
   46    succ(N, N1),
   47    mapnargs_(N1, Goal, T).
   48mapnargs_(_, _, _).
   49
   50mapnargs_(N, Goal, T1, T2) :-
   51    arg(N, T1, A1),
   52    arg(N, T2, A2),
   53    !,
   54    call(Goal, N, A1, A2),
   55    succ(N, N1),
   56    mapnargs_(N1, Goal, T1, T2).
   57mapnargs_(_, _, _, _).
   58
   59mapnargs_(N, Goal, T1, T2, T3) :-
   60    arg(N, T1, A1),
   61    arg(N, T2, A2),
   62    arg(N, T3, A3),
   63    !,
   64    call(Goal, N, A1, A2, A3),
   65    succ(N, N1),
   66    mapnargs_(N1, Goal, T1, T2, T3).
   67mapnargs_(_, _, _, _, _).
   68
   69mapnargs_(N, Goal, T1, T2, T3, T4) :-
   70    arg(N, T1, A1),
   71    arg(N, T2, A2),
   72    arg(N, T3, A3),
   73    arg(N, T4, A4),
   74    !,
   75    call(Goal, N, A1, A2, A3, A4),
   76    succ(N, N1),
   77    mapnargs_(N1, Goal, T1, T2, T3, T4).
   78mapnargs_(_, _, _, _, _, _).
   79
   80mapnargs_(N, Goal, T1, T2, T3, T4, T5) :-
   81    arg(N, T1, A1),
   82    arg(N, T2, A2),
   83    arg(N, T3, A3),
   84    arg(N, T4, A4),
   85    arg(N, T5, A5),
   86    !,
   87    call(Goal, N, A1, A2, A3, A4, A5),
   88    succ(N, N1),
   89    mapnargs_(N1, Goal, T1, T2, T3, T4, T5).
   90mapnargs_(_, _, _, _, _, _, _).
   91
   92mapnargs_(N, Goal, T1, T2, T3, T4, T5, T6) :-
   93    arg(N, T1, A1),
   94    arg(N, T2, A2),
   95    arg(N, T3, A3),
   96    arg(N, T4, A4),
   97    arg(N, T5, A5),
   98    arg(N, T6, A6),
   99    !,
  100    call(Goal, N, A1, A2, A3, A4, A5, A6),
  101    succ(N, N1),
  102    mapnargs_(N1, Goal, T1, T2, T3, T4, T5, T6).
  103mapnargs_(_, _, _, _, _, _, _, _).
  104
  105:- meta_predicate
  106    mapnargs(2,?),
  107    mapnargs(3,?,?),
  108    mapnargs(4,?,?,?),
  109    mapnargs(5,?,?,?,?),
  110    mapnargs(6,?,?,?,?,?),
  111    mapnargs(7,?,?,?,?,?,?).  112
  113mapnargs(Goal, Term) :-
  114    compound(Term),
  115    !,
  116    mapnargs_(1, Goal, Term).
  117mapnargs(_, _).
  118
  119mapnargs(Goal, T1, T2) :-
  120    compound(T1),
  121    !,
  122    functor(T1, N, A),
  123    functor(T2, N, A),
  124    mapnargs_(1, Goal, T1, T2).
  125mapnargs(_, T, T).
  126
  127mapnargs(Goal, T1, T2, T3) :-
  128    compound(T1),
  129    !,
  130    functor(T1, N, A),
  131    functor(T2, N, A),
  132    functor(T3, N, A),
  133    mapnargs_(1, Goal, T1, T2, T3).
  134mapnargs(_, T, T, T).
  135
  136mapnargs(Goal, T1, T2, T3, T4) :-
  137    compound(T1),
  138    !,
  139    functor(T1, N, A),
  140    functor(T2, N, A),
  141    functor(T3, N, A),
  142    functor(T4, N, A),
  143    mapnargs_(1, Goal, T1, T2, T3, T4).
  144mapnargs(_, T, T, T, T).
  145
  146mapnargs(Goal, T1, T2, T3, T4, T5) :-
  147    compound(T1),
  148    !,
  149    functor(T1, N, A),
  150    functor(T2, N, A),
  151    functor(T3, N, A),
  152    functor(T4, N, A),
  153    functor(T5, N, A),
  154    mapnargs_(1, Goal, T1, T2, T3, T4, T5).
  155mapnargs(_, T, T, T, T, T).
  156
  157mapnargs(Goal, T1, T2, T3, T4, T5, T6) :-
  158    compound(T1),
  159    !,
  160    functor(T1, N, A),
  161    functor(T2, N, A),
  162    functor(T3, N, A),
  163    functor(T4, N, A),
  164    functor(T5, N, A),
  165    functor(T6, N, A),
  166    mapnargs_(1, Goal, T1, T2, T3, T4, T5, T6).
  167mapnargs(_, T, T, T, T, T, T)