1:- module(dcg_hashlib,
2 [
3 ]). 4
5
6:- ensure_loaded(dcg_hashexp). 8
9sentence --> noun_phrase, verb_phrase.
10noun_phrase --> determiner, noun.
11verb_phrase --> verb, noun_phrase.
12
13determiner --> [the].
14determiner --> [a].
15noun --> [cat].
16noun --> [mouse].
17verb --> [chases].
18
19dcg_test(DCG,Input):- phrase(DCG,Input,[]).
20
21:- dcg_test(sentence,[the,cat,chases,the,mouse]). 22
23:- abolish(sentence,2). 24
25
26sentence --> noun_phrase(N), verb_phrase(N).
27
28noun_phrase(N) --> determiner(N), noun(N).
29verb_phrase(N) --> verb(N), noun_phrase(_).
30
31determiner(_) --> [the].
32determiner(singular) --> [a].
33determiner(plural) --> [].
34
35noun(singular) --> [cat].
36noun(plural) --> [cats].
37noun(singular) --> [mouse].
38noun(plural) --> [mice].
39
40verb(singular) --> [chases].
41verb(plural) --> [chase].
42
43:- dcg_test(sentence,[the,cat,chases,the,mouse]). 44
45
46letter(X) --> [X],
47 {
48 member(X, [a,b,c])
49 }.
50
51:- dcg_test(letter(a),[a]). 52:- dcg_test(letter(X),[a]), X = a. 53:- dcg_test((letter(X),letter(X)),[a,a]). 54
55palin --> [].
56palin --> letter(_).
57palin --> letter(X), palin, letter(X).
58
59:- dcg_test(palin,[a,a,a]). 60:- dcg_test(palin,[b,a,a,a,b]).