high_order.pl -- High order grammar operations
This library provides facilities comparable maplist/3, ignore/1 and foreach/2 for DCGs.
STATUS: This library is experimental. The interface and
implementation may change based on feedback. Please send feedback to the
mailinglist or the issue page of the swipl-devel.git
repository.
- sequence(:Element, ?List)// is nondet
- Match or generate a sequence of Element. This predicate is
deterministic if List is fully instantiated and Element is
deterministic. When parsing, this predicate is gready and does not
prune choice points. For example:
?- phrase(sequence(digit, Digits), `123a`, L). Digits = "123", L = [97] ; Digits = [49, 50], L = [51, 97] ; ...
- sequence(:Element, :Sep, ?List)// is nondet
- Match or generate a sequence of Element where each pair of elements
is separated by Sep. When parsing, a matched Sep commits. The
final element is not committed. More formally, it matches the
following sequence:
Element?, (Sep,Element)*
See also sequence//5.
- sequence(:Start, :Element, :Sep, :End, ?List)// is semidet
- Match or generate a sequence of Element enclosed by Start end End,
where each pair of elements is separated by Sep. More formally, it
matches the following sequence:
Start, Element?, (Sep,Element)*, End
The example below matches a Prolog list of integers:
?- phrase(sequence(("[",blanks), number, (",",blanks), (blanks,"]"), L), `[1, 2, 3 ] a`, Tail). L = [1, 2, 3], Tail = [32, 97].
- sequence_as(?List, :OnElem, :OnSep)//[private]
- Matches: Elem, (Sep,Elem)*
- optional(:Match, :Default)// is det
- Perform an optional match, executing Default if Match is not
matched. This is comparable to ignore/1. Both Match and Default are
DCG body terms. Default is typically used to instantiate the output
variables of Match, but may also be used to match a default
representation. Using
[]
for Default succeeds without any additional actions if Match fails. For example:?- phrase(optional(number(X), {X=0}), `23`, Tail). X = 23, Tail = []. ?- phrase(optional(number(X), {X=0}), `aap`, Tail). X = 0, Tail = `aap`.
- foreach(:Generator, :Element)// is det
- foreach(:Generator, :Element, :Sep)// is det
- Generate a list from the solutions of Generator. This predicate
collects all solutions of Generator, applies Element for each
solution and Sep between each pair of solutions. For example:
?- phrase(foreach(between(1,5,X), number(X), ", "), L). L = "1, 2, 3, 4, 5".
Undocumented predicates
The following predicates are exported, but not or incorrectly documented.