:- use_module(library(dcg/basics)). :- begin_tests(dcg). % Parsing the (unicode) codepoints in "Codes" with the callable % integer(X) which actually resolves to predicate integer/3, taking % itself "Codes" and "Rest" in addition to "X". After the call, leftover % character codes from "Codes" are in "Rest". test(parsing, [true(ResultCmp)] ) :- atom_codes('42 times', Codes), phrase(integer(X), Codes, Rest), atom_codes(RestAtom, Rest), ResultCmp = (result(X,RestAtom) == result(42,' times')). % Generating the (unicode) codepoints in "Codes" based on an integer % passed to the callable integer(X) and a suffix of codepoints in "Rest". % After the call, the "Rest", prefixed with whatever integer(42) as % generated, can be found in "Codes". test(generating, [true(ResultCmp)] ) :- atom_codes(' times', Rest), phrase(integer(42), Codes, Rest), atom_codes(CodesAtom, Codes), ResultCmp = (CodesAtom == '42 times'). :- end_tests(dcg). rt(dcg) :- run_tests(dcg).
?- listing(integer//1). integer(I, A, B) :- digit(D0, A, C), digits(D, C, E), number_codes(I, [D0|D]), B=E. true.