1/*  File:    dcg/sequence.pl
    2    Author:  Roy Ratcliffe
    3    Created: Aug 22 2026
    4    Purpose: DCG Sequence with Flexible Separator Handling
    5
    6Copyright (c) 2026, Roy Ratcliffe, Northumberland, United Kingdom
    7
    8Permission is hereby granted, free of charge,  to any person obtaining a
    9copy  of  this  software  and    associated   documentation  files  (the
   10"Software"), to deal in  the   Software  without  restriction, including
   11without limitation the rights to  use,   copy,  modify,  merge, publish,
   12distribute, sublicense, and/or sell  copies  of   the  Software,  and to
   13permit persons to whom the Software is   furnished  to do so, subject to
   14the following conditions:
   15
   16    The above copyright notice and this permission notice shall be
   17    included in all copies or substantial portions of the Software.
   18
   19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT  WARRANTY OF ANY KIND, EXPRESS
   20OR  IMPLIED,  INCLUDING  BUT  NOT   LIMITED    TO   THE   WARRANTIES  OF
   21MERCHANTABILITY, FITNESS FOR A PARTICULAR   PURPOSE AND NONINFRINGEMENT.
   22IN NO EVENT SHALL THE AUTHORS  OR   COPYRIGHT  HOLDERS BE LIABLE FOR ANY
   23CLAIM, DAMAGES OR OTHER LIABILITY,  WHETHER   IN  AN ACTION OF CONTRACT,
   24TORT OR OTHERWISE, ARISING FROM,  OUT  OF   OR  IN  CONNECTION  WITH THE
   25SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   26
   27*/
   28
   29:- module(dcg_sequence,
   30          [ sequence_as//3 % +List, :OnElement, :OnSeparator
   31          ]).   32
   33:- meta_predicate sequence_as(+, :, :, ?, ?).
 sequence_as(+List, :OnElement, :OnSeparator)// is semidet
Matches or generates a sequence of elements from List, applying OnElement to each element and OnSeparator between elements.

This DCG rule is useful for parsing or generating sequences of elements where the separator is not fixed. It solves a problem with the standard sequence//3 predicate, which cuts when it encounters any separator, including a final separator that does not have a subsequent element. This variation looks for a separator with a subsequent element but ignores a separator without a subsequent element that may be present at the end of the sequence, allowing for more flexible parsing and generation of sequences.

Fails for trailing separators without a subsequent element. A separator requires something to follow it, and if nothing follows, the sequence fails. This behaviour is useful for parsing sequences where a trailing separator is not allowed.

Also fails for empty lists. An empty list has no elements to match, and the sequence consequently fails.

Arguments:
List- The list of elements to match or generate.
OnElement- The DCG rule to apply to each element.
OnSeparator- The DCG rule to apply between elements.
   60sequence_as([H|T], OnElement, OnSeparator) -->
   61    call(OnElement, H),
   62    (   OnSeparator,
   63        sequence_as(T, OnElement, OnSeparator)
   64    ->  !
   65    ;   { T = []
   66        }
   67    )