1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2021, 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(foldnl,
   36          [ foldnl/4,
   37            foldnl/5,
   38            foldnl/6,
   39            foldnl/7,
   40            foldnl/8,
   41            foldnl/9
   42          ]).   43
   44:- meta_predicate
   45    foldnl(3, +, +, -),
   46    foldnl(4, +, +, +, -),
   47    foldnl(5, +, +, +, +, -),
   48    foldnl(6, +, +, +, +, +, -),
   49    foldnl(7, +, +, +, +, +, +, -),
   50    foldnl(8, +, +, +, +, +, +, +, -).
 foldnl(:Goal, +NList, +V0, -V)
 foldnl(:Goal, +NList1, +NList2, +V0, -V)
 foldnl(:Goal, +NList1, +NList2, +NList3, +V0, -V)
 foldnl(:Goal, +NList1, +NList2, +NList3, +NList4, +V0, -V)
 foldnl(:Goal, +NList1, +NList2, +NList3, +NList4, +NList5, +V0, -V)
 foldnl(:Goal, +NList1, +NList2, +NList3, +NList4, +NList5, +NList6, +V0, -V)
   59foldnl(Goal, List) -->
   60    foldnl_(List, Goal).
   61
   62foldnl_([], _) -->
   63    !.
   64foldnl_([H|T], Goal) -->
   65    !,
   66    foldnl_(H, Goal),
   67    foldnl_(T, Goal).
   68foldnl_(E, Goal) -->
   69    call(Goal, E).
   70
   71foldnl(Goal, List1, List2) -->
   72    foldnl_(List1, List2, Goal).
   73
   74foldnl_([], [], _) -->
   75    !.
   76foldnl_([H1|T1], [H2|T2], Goal) -->
   77    !,
   78    foldnl_(H1, H2, Goal),
   79    foldnl_(T1, T2, Goal).
   80foldnl_(E1, E2, Goal) -->
   81    call(Goal, E1, E2).
   82
   83foldnl(Goal, List1, List2, List3) -->
   84    foldnl_(List1, List2, List3, Goal).
   85
   86foldnl_([], [], [], _) -->
   87    !.
   88foldnl_([H1|T1], [H2|T2], [H3|T3], Goal) -->
   89    !,
   90    foldnl_(H1, H2, H3, Goal),
   91    foldnl_(T1, T2, T3, Goal).
   92foldnl_(E1, E2, E3, Goal) -->
   93    call(Goal, E1, E2, E3).
   94
   95foldnl(Goal, List1, List2, List3, List4) -->
   96    foldnl_(List1, List2, List3, List4, Goal).
   97
   98foldnl_([], [], [], [], _) -->
   99    !.
  100foldnl_([H1|T1], [H2|T2], [H3|T3], [H4|T4], Goal) -->
  101    !,
  102    foldnl_(H1, H2, H3, H4, Goal),
  103    foldnl_(T1, T2, T3, T4, Goal).
  104foldnl_(E1, E2, E3, E4, Goal) -->
  105    call(Goal, E1, E2, E3, E4).
  106
  107foldnl(Goal, List1, List2, List3, List4, List5) -->
  108    foldnl_(List1, List2, List3, List4, List5, Goal).
  109
  110foldnl_([], [], [], [], [], _) -->
  111    !.
  112foldnl_([H1|T1], [H2|T2], [H3|T3], [H4|T4], [H5|T5], Goal) -->
  113    !,
  114    foldnl_(H1, H2, H3, H4, H5, Goal),
  115    foldnl_(T1, T2, T3, T4, T5, Goal).
  116foldnl_(E1, E2, E3, E4, E5, Goal) -->
  117    call(Goal, E1, E2, E3, E4, E5).
  118
  119foldnl(Goal, List1, List2, List3, List4, List5, List6) -->
  120    foldnl_(List1, List2, List3, List4, List5, List6, Goal).
  121
  122foldnl_([], [], [], [], [], [], _) -->
  123    !.
  124foldnl_([H1|T1], [H2|T2], [H3|T3], [H4|T4], [H5|T5], [H6|T6], Goal) -->
  125    !,
  126    foldnl_(H1, H2, H3, H4, H5, H6, Goal),
  127    foldnl_(T1, T2, T3, T4, T5, T6, Goal).
  128foldnl_(E1, E2, E3, E4, E5, E6, Goal) -->
  129    call(Goal, E1, E2, E3, E4, E5, E6)