Compatible Doclog docs for library(dcgs)
This commit is contained in:
@@ -1,3 +1,13 @@
|
|||||||
|
/** Support for Definite Clause Grammars.
|
||||||
|
|
||||||
|
A Prolog definite clause grammar (DCG) describes a sequence. Operationally, DCGs
|
||||||
|
can be used to parse, generate, complete and check sequences manifested as lists.
|
||||||
|
|
||||||
|
Check [The Power of Prolog chapter on DCGs](https://www.metalevel.at/prolog/dcg)
|
||||||
|
to learn more about them.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
:- module(dcgs,
|
:- module(dcgs,
|
||||||
[op(1105, xfy, '|'),
|
[op(1105, xfy, '|'),
|
||||||
phrase/2,
|
phrase/2,
|
||||||
@@ -16,9 +26,44 @@
|
|||||||
|
|
||||||
:- meta_predicate phrase(2, ?, ?).
|
:- meta_predicate phrase(2, ?, ?).
|
||||||
|
|
||||||
|
%% phrase(+Body, ?Ls).
|
||||||
|
%
|
||||||
|
% True iff Body describes the list Ls. Body must be a DCG body.
|
||||||
|
% It is equivalent to `phrase(Body, Ls, [])`.
|
||||||
|
%
|
||||||
|
% Examples:
|
||||||
|
%
|
||||||
|
% ```
|
||||||
|
% as --> [].
|
||||||
|
% as --> [a], as.
|
||||||
|
%
|
||||||
|
% ?- phrase(as, Ls).
|
||||||
|
% Ls = []
|
||||||
|
% ; Ls = "a"
|
||||||
|
% ; Ls = "aa"
|
||||||
|
% ; Ls = "aaa"
|
||||||
|
% ; ... .
|
||||||
|
%
|
||||||
|
% ?- phrase(as, "aaa").
|
||||||
|
% true.
|
||||||
|
% ```
|
||||||
|
|
||||||
phrase(GRBody, S0) :-
|
phrase(GRBody, S0) :-
|
||||||
phrase(GRBody, S0, []).
|
phrase(GRBody, S0, []).
|
||||||
|
|
||||||
|
%% phrase(+Body, ?Ls, ?Ls0).
|
||||||
|
%
|
||||||
|
% True iff Body describes part of the list Ls and the rest of Ls is Ls0.
|
||||||
|
%
|
||||||
|
% Example:
|
||||||
|
%
|
||||||
|
% ```
|
||||||
|
% ?- phrase(seq(X), "aaa", Y).
|
||||||
|
% X = [], Y = "aaa"
|
||||||
|
% ; X = "a", Y = "aa"
|
||||||
|
% ; X = "aa", Y = "a"
|
||||||
|
% ; X = "aaa", Y = [].
|
||||||
|
% ```
|
||||||
phrase(GRBody, S0, S) :-
|
phrase(GRBody, S0, S) :-
|
||||||
strip_module(GRBody, M, GRBody1),
|
strip_module(GRBody, M, GRBody1),
|
||||||
( var(GRBody) ->
|
( var(GRBody) ->
|
||||||
@@ -131,6 +176,9 @@ user:term_expansion(Term0, Term) :-
|
|||||||
nonvar(Term0),
|
nonvar(Term0),
|
||||||
dcg_rule(Term0, Term).
|
dcg_rule(Term0, Term).
|
||||||
|
|
||||||
|
|
||||||
|
%% seq(Seq)//
|
||||||
|
%
|
||||||
% Describes a sequence
|
% Describes a sequence
|
||||||
seq(Xs, Cs0,Cs) :-
|
seq(Xs, Cs0,Cs) :-
|
||||||
var(Xs),
|
var(Xs),
|
||||||
@@ -141,10 +189,14 @@ seq(Xs, Cs0,Cs) :-
|
|||||||
seq([]) --> [].
|
seq([]) --> [].
|
||||||
seq([E|Es]) --> [E], seq(Es).
|
seq([E|Es]) --> [E], seq(Es).
|
||||||
|
|
||||||
|
%% seqq(SeqOfSeqs)//
|
||||||
|
%
|
||||||
% Describes a sequence of sequences
|
% Describes a sequence of sequences
|
||||||
seqq([]) --> [].
|
seqq([]) --> [].
|
||||||
seqq([Es|Ess]) --> seq(Es), seqq(Ess).
|
seqq([Es|Ess]) --> seq(Es), seqq(Ess).
|
||||||
|
|
||||||
|
%% ...//
|
||||||
|
%
|
||||||
% Describes an arbitrary number of elements
|
% Describes an arbitrary number of elements
|
||||||
...(Cs0,Cs) :-
|
...(Cs0,Cs) :-
|
||||||
Cs0 == [],
|
Cs0 == [],
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
Provides predicate dif/2. dif/2 is a constraint that is true only if both of its
|
Provides predicate `dif/2`. `dif/2` is a constraint that is true only if both of its
|
||||||
arguments are different terms.
|
arguments are different terms.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -45,10 +45,11 @@ verify_attributes(Var, Value, Goals) :-
|
|||||||
|
|
||||||
%% dif(?X, ?Y).
|
%% dif(?X, ?Y).
|
||||||
%
|
%
|
||||||
% True iff X and Y are different terms. Unlike \\=/2, dif/2 is more declarative because if X and Y can
|
% True iff X and Y are different terms. Unlike `\=/2`, `dif/2` is more declarative because if X and Y can
|
||||||
% unify but they're not yet equal, the decision is delayed, and prevents X and Y to become equal later.
|
% unify but they're not yet equal, the decision is delayed, and prevents X and Y to become equal later.
|
||||||
% Examples:
|
% Examples:
|
||||||
%
|
%
|
||||||
|
% ```
|
||||||
% ?- dif(a, a).
|
% ?- dif(a, a).
|
||||||
% false.
|
% false.
|
||||||
% ?- dif(a, b).
|
% ?- dif(a, b).
|
||||||
@@ -57,6 +58,7 @@ verify_attributes(Var, Value, Goals) :-
|
|||||||
% dif:dif(X,b).
|
% dif:dif(X,b).
|
||||||
% ?- dif(X, b), X = b.
|
% ?- dif(X, b), X = b.
|
||||||
% false.
|
% false.
|
||||||
|
% ```
|
||||||
dif(X, Y) :-
|
dif(X, Y) :-
|
||||||
X \== Y,
|
X \== Y,
|
||||||
( X \= Y -> true
|
( X \= Y -> true
|
||||||
|
|||||||
Reference in New Issue
Block a user