Prolog "dcg"

         
/**
* 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').
/****************************************************************/
/* Definite Clause Grammars (DCG) */
/****************************************************************/
/* expand_term(X, Y) */
runner_pred(expand_term, 2, stream, dcg, 'XLOG 4.1.1').
runner_case(expand_term, 2, stream, dcg, 'XLOG 4.1.1, XLOG 1') :-
expand_term((p --> q), X),
X = (p(A, B) :- q(C, D)),
C == A, D == B.
runner_case(expand_term, 2, stream, dcg, 'XLOG 4.1.1, 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(expand_term, 2, stream, dcg, 'XLOG 4.1.1, 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(expand_term, 2, stream, dcg, 'XLOG 4.1.1, XLOG 4') :-
expand_term((p --> [a], !, [b]), X),
X = (p([a|A], B) :- true, !, C = [b|D]),
C == A, D == B.
runner_case(expand_term, 2, stream, dcg, 'XLOG 4.1.1, XLOG 5') :-
expand_term((_ --> q), X),
X = (phrase(_, A, B) :- q(C, D), phrase(_, E, F)),
A == C, D == F, B == E.
runner_case(expand_term, 2, stream, dcg, 'XLOG 4.1.1, XLOG 6') :-
catch(expand_term((p, [a|_] --> q), _), error(E,_), true),
E == instantiation_error.
runner_case(expand_term, 2, stream, dcg, 'XLOG 4.1.1, XLOG 7') :-
catch(expand_term((p --> [a|b], q), _), error(E,_), true),
E == type_error(list, b).
runner_case(expand_term, 2, stream, dcg, 'XLOG 4.1.1, XLOG 8') :-
catch(expand_term((p --> 1), _), error(E,_), true),
E == type_error(callable, 1).
/* X --> Y (clauses) */
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(-->, 2, stream, dcg, 'XLOG 4.1.2').
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.2, XLOG 1') :-
test1("foobarbaz", X),
X == "baz".
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.2, XLOG 2') :-
test2("barbaz", X),
X == "baz".
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.2, XLOG 3') :-
test3a(2, X, ""),
X == "bar".
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.2, XLOG 4') :-
\+ test4("baz", "").
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.2, XLOG 5') :-
test5(X, ""),
X == "bar".
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.2, XLOG 6') :-
test6(1, X, ""),
X == "foo".
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.2, XLOG 7') :-
test7(2, X, ""),
X == "bar".
runner_case(-->, 2, stream, dcg, 'XLOG 4.1.2, XLOG 8') :-
test8("foobaz", X),
X == "baz".
/* phrase(B, I, O) */
test9(X) --> X.
succ(X, Y) :- Y is X+1.
runner_pred(phrase, 3, stream, dcg, 'XLOG 4.1.3').
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.3, XLOG 1') :-
phrase("abc", X, Y),
X == [97, 98, 99|Y].
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.3, XLOG 2') :-
test9("abc", X, Y),
X == [97, 98, 99|Y].
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.3, XLOG 3') :-
phrase((sort,reverse), [3,1,2], L),
L == [3, 2, 1].
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.3, XLOG 4') :-
findall(Y, (member(X,[2,1]), phrase((succ;=;succ,succ), X, Y)), L),
L == [3, 2, 4, 2, 1, 3].
runner_case(phrase, 3, stream, dcg, 'XLOG 4.1.3, XLOG 5') :-
catch(phrase(_, _, _), error(E,_), true),
E == instantiation_error.