Prolog "dcg"

Admin User, created Apr 07. 2025
         
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
/**
* Source of test cases are the following standards and proposals:
* - Definite Clause Grammar Rules, ISO/IEC DTR 13211–3:2006
* <a href="https://www.complang.tuwien.ac.at/ulrich/iso-prolog/dcgs/dcgsdin140720.pdf">www.complang.tuwien.ac.at/ulrich/iso-prolog/dcgs/dcgsdin140720.pdf</a>
*/
runner_file(stream, dcg, 'XLOG 4.1 dcg').
/****************************************************************/
/* Term Expansion */
/****************************************************************/
/* X --> Y */
runner_pred(-->, 2, stream, dcg, 'XLOG 4.1.3').
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.3, XLOG 1') :-
expand_term((p --> q), X),
X = (p(A, B) :- q(C, D)),
C == A, D == B.
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.3, XLOG 2') :-
expand_term((p, q --> r), X),
X = (p(A, B) :- r(C, D), q(E, F)),
C == A, F == D, E == B.
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.3, XLOG 3') :-
expand_term((p; q --> r), X),
X = (p(A, B); q(C, D) :- r(E, F)),
C == A, D == B, E == A, F == B.
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.3, XLOG 4') :-
expand_term((p --> [a], !, [b]), X),
X = (p([a|A], B) :- true, !, C = [b|D]),
C == A, D == B.
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.3, XLOG 5') :-
catch(expand_term((_ --> q), _), error(E,_), true),
E == instantiation_error.
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.3, XLOG 6') :-
catch(expand_term((p, [a|_] --> q), _), error(E,_), true),
E == instantiation_error.
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.3, XLOG 7') :-
catch(expand_term((p --> [a|b], q), _), error(E,_), true),
E == type_error(list, b).
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.3, XLOG 8') :-
catch(expand_term((p --> 1), _), error(E,_), true),
E == type_error(callable, 1).
/****************************************************************/
/* Phrase Expansion */
/****************************************************************/
/* phrase(X, Y, Z) */
test1 --> test4, test5.
test2 --> test4; test5.
test3a(X) --> (test3b(X) -> test4; test5).
test3b(1) --> [].
test4 --> "foo".
test5 --> "bar".
test6(X) --> test3b(X), !, test4.
test6(_) --> test5.
test7(X) --> {X=1}, !, test4.
test7(_) --> test5.
test8 --> test4, \+ test5.
runner_pred(phrase, 3, stream, dcg, 'XLOG 4.1.4').
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.4, XLOG 1') :-
test1("foobarbaz", X), X == "baz".
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.4, XLOG 2') :-
test2("barbaz", X), X == "baz".
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.4, XLOG 3') :-
test3a(2, X, ""), X == "bar".
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.4, XLOG 4') :-
\+ test4("baz", "").
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.4, XLOG 5') :-
test5(X, ""), X == "bar".
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.4, XLOG 6') :-
test6(1, X, ""), X == "foo".
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.4, XLOG 7') :-
test7(2, X, ""), X == "bar".
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.4, XLOG 8') :-
test8("foobaz", X), X == "baz".