转载自:https://mp.weixin.qq.com/s/UYedzZIXz0JRYrYTNjItbw
?- [user].
|: s(Z):- np(X), vp(Y), append(X,Y,Z).
|: np(Z):- det(X), n(Y), append(X,Y,Z).
|: vp(Z):- v(X), np(Y), append(X,Y,Z).
|: vp(Z):- v(Z).
|: det([the]).
|: det([a]).
|: n([woman]).
|: n([man]).
|: v([shoots]).
|: ^D% user://2 compiled 0.01 sec, 11 clauses
true.
?- s([a,woman,shoots,a,man]).
true ;
true ;
true ;
false.
?- s(X).
X = [the, woman, shoots, the, woman] ;
X = [the, woman, shoots, the, man] ;
X = [the, woman, shoots, a, woman] ;
X = [the, woman, shoots, a, man] ;
X = [the, woman, shoots] ;
X = [the, man, shoots, the, woman] ;
X = [the, man, shoots, the, man] ;
X = [the, man, shoots, a, woman] ;
X = [the, man, shoots, a, man] ;
X = [the, man, shoots] ;
X = [a, woman, shoots, the, woman] ;
X = [a, woman, shoots, the, man] ;
X = [a, woman, shoots, a, woman] ;
X = [a, woman, shoots, a, man] ;
X = [a, woman, shoots] ;
X = [a, man, shoots, the, woman] ;
X = [a, man, shoots, the, man] ;
X = [a, man, shoots, a, woman] ;
X = [a, man, shoots, a, man] ;
X = [a, man, shoots] ;
X = [a, woman, shoots, a, man] ;
X = [the, woman, shoots, the, woman] ;
X = [the, woman, shoots, the, man] ;
X = [the, woman, shoots, a, woman] ;
X = [the, woman, shoots, a, man] ;
X = [the, woman, shoots] ;
X = [the, man, shoots, the, woman] ;
X = [the, man, shoots, the, man] ;
X = [the, man, shoots, a, woman] ;
X = [the, man, shoots, a, man] ;
X = [the, man, shoots] ;
X = [a, woman, shoots, the, woman] ;
X = [a, woman, shoots, the, man] ;
X = [a, woman, shoots, a, woman] ;
X = [a, woman, shoots, a, man] ;
X = [a, woman, shoots] ;
X = [a, man, shoots, the, woman] ;
X = [a, man, shoots, the, man] ;
X = [a, man, shoots, a, woman] ;
X = [a, man, shoots, a, man] ;
X = [a, man, shoots].
?- np([a,woman]).
true ;
false.
?- np(X).
X = [the, woman] ;
X = [the, man] ;
X = [a, woman] ;
X = [a, man].
?- halt.
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- [user].
|: s(Z):- append(X,Y,Z), np(X), vp(Y).
|: np(Z):- append(X,Y,Z), det(X), n(Y).
|: vp(Z):- append(X,Y,Z), v(X), np(Y).
|: vp(Z):- v(Z).
|: det([the]).
|: det([a]).
|: n([woman]).
|: n([man]).
|: v([shoots]).
|:
|: ^D% user://1 compiled 0.00 sec, 9 clauses
true.
?- s([a,woman,shoots,a,man]).
true ;
false.
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- [user].
|: s(X,Z):- np(X,Y), vp(Y,Z).
|: np(X,Z):- det(X,Y), n(Y,Z).
|: vp(X,Z):- v(X,Y), np(Y,Z).
|: vp(X,Z):- v(X,Z).
|: det([the|W],W).
|: det([a|W],W).
|: n([woman|W],W).
|: n([man|W],W).
|: v([shoots|W],W).
|: ^D% user://1 compiled 0.00 sec, 9 clauses
true.
?- n([man|W],[W]).
W = [W].
?- s([a,woman,shoots,a,man],[]).
true ;
false.
?- s([a,woman,shoots,a,man],[]).
true .
?- s(X,[]).
X = [the, woman, shoots, the, woman] ;
X = [the, woman, shoots, the, man] ;
X = [the, woman, shoots, a, woman] ;
X = [the, woman, shoots, a, man] ;
X = [the, woman, shoots] ;
X = [the, man, shoots, the, woman] ;
X = [the, man, shoots, the, man] ;
X = [the, man, shoots, a, woman] ;
X = [the, man, shoots, a, man] ;
X = [the, man, shoots] ;
X = [a, woman, shoots, the, woman] ;
X = [a, woman, shoots, the, man] ;
X = [a, woman, shoots, a, woman] ;
X = [a, woman, shoots, a, man] ;
X = [a, woman, shoots] ;
X = [a, man, shoots, the, woman] ;
X = [a, man, shoots, the, man] ;
X = [a, man, shoots, a, woman] ;
X = [a, man, shoots, a, man] ;
X = [a, man, shoots].
?- np([a,woman],[]).
true.
?- np(X,[]).
X = [the, woman] ;
X = [the, man] ;
X = [a, woman] ;
X = [a, man].
?- halt.
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- [user].
|: s-->np,vp.
|: np-->det,n.
|: vp-->v,np.
|: vp-->v.
|: det-->[the].
|: det-->[a].
|: n-->[woman].
|: n-->[man].
|: v-->[shoots].
|: ^D% user://1 compiled 0.01 sec, 9 clauses
true.
?- s([a,woman,shoots,a,man],[]).
true ;
false.
?- s([a,woman,shoots,a,man],[]).
true .
?- s(X,[]).
X = [the, woman, shoots, the, woman] ;
X = [the, woman, shoots, the, man] ;
X = [the, woman, shoots, a, woman] ;
X = [the, woman, shoots, a, man] ;
X = [the, woman, shoots] ;
X = [the, man, shoots, the, woman] ;
X = [the, man, shoots, the, man] ;
X = [the, man, shoots, a, woman] ;
X = [the, man, shoots, a, man] ;
X = [the, man, shoots] ;
X = [a, woman, shoots, the, woman] ;
X = [a, woman, shoots, the, man] ;
X = [a, woman, shoots, a, woman] ;
X = [a, woman, shoots, a, man] ;
X = [a, woman, shoots] ;
X = [a, man, shoots, the, woman] ;
X = [a, man, shoots, the, man] ;
X = [a, man, shoots, a, woman] ;
X = [a, man, shoots, a, man] ;
X = [a, man, shoots].
?- np([a,woman],[]).
true.
?- np(X,[]).
X = [the, woman] ;
X = [the, man] ;
X = [a, woman] ;
X = [a, man].
?- listing(s).
s(A, C) :-
np(A, B),
vp(B, C).
true.
?- listing(np).
np(A, C) :-
det(A, B),
n(B, C).
true.
?- listing(det).
det([the|A], A).
det([a|A], A).
true.
?- [user].
|: s --> s,conj,s.
Warning: user://2:79:
Warning: Redefined static procedure s/2
Warning: Previously defined at user://1:8
|: conj --> [and].
|: conj --> [or].
|: conj --> [but].
|: ^D% user://2 compiled 0.00 sec, 4 clauses
true.
?- listing(s).
s(A, D) :-
s(A, B),
conj(B, C),
s(C, D).
true.
?- s([a,woman,shoots],[]).
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 1.0Gb, global: 66Kb, trail: 4Kb
ERROR: Stack depth: 11,182,627, last-call: 0%, Choice points: 3
ERROR: Probable infinite recursion (cycle):
ERROR: [11,182,627] user:s([length:3], _17166)
ERROR: [11,182,626] user:s([length:3], _17192)
?- halt.
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- [user].
|: s --> simple_s.
|: s --> simple_s,conj,s.
|: simple_s --> np,vp.
|: np --> det,n.
|: vp --> v,np.
|: vp --> v.
|: det --> [the].
|: det --> [a].
|: n --> [woman].
|: n --> [man].
|: v --> [shoots].
|: conj --> [and].
|: conj --> [or].
|: conj --> [but].
|: ^D% user://1 compiled 0.00 sec, 14 clauses
true.
?- s([a,woman,shoots],[]).
true ;
false.
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- [user].
|: s --> [].
|: s --> l,s,r.
|: l --> [a].
|: r --> [b].
|: ^D% user://1 compiled 0.00 sec, 4 clauses
true.
?- s([a,a,a,b,b,b],[]).
true .
?- s([a,a,a,b,b,b,b],[]).
false.
?- s(X,[]).
X = [] ;
X = [a, b] ;
X = [a, a, b, b] ;
X = [a, a, a, b, b, b] ;
X = [a, a, a, a, b, b, b, b] ;
X = [a, a, a, a, a, b, b, b, b|...] ;
X = [a, a, a, a, a, a, b, b, b|...] ;
X = [a, a, a, a, a, a, a, b, b|...] ;
X = [a, a, a, a, a, a, a, a, b|...] .
?-
练习
?- [user].
|: s --> foo,bar,wiggle.
|: foo --> [choo].
|: foo --> foo,foo.
|: bar --> mar,zar.
|: mar --> me,my.
|: me --> [i].
|: my --> [am].
|: zar --> blar,car.
|: blar --> [a].
|: car --> [train].
|: wiggle --> [toot].
|: wiggle --> wiggle,wiggle.
|: ^D% user://1 compiled 0.00 sec, 12 clauses
true.
?- s(X,[]).
X = [choo, i, am, a, train, toot] ;
X = [choo, i, am, a, train, toot, toot] ;
X = [choo, i, am, a, train, toot, toot, toot] ;
X = [choo, i, am, a, train, toot, toot, toot, toot] ;
X = [choo, i, am, a, train, toot, toot, toot, toot|...] ;
X = [choo, i, am, a, train, toot, toot, toot, toot|...] .
?- listing(s).
s(A, D) :-
foo(A, B),
bar(B, C),
wiggle(C, D).
true.
?- listing(foo).
foo([choo|A], A).
foo(A, C) :-
foo(A, B),
foo(B, C).
true.
?- listing(bar).
bar(A, C) :-
mar(A, B),
zar(B, C).
true.
?- listing(mar).
mar(A, C) :-
me(A, B),
my(B, C).
true.
?- listing(me).
me([i|A], A).
true.
?- listing(my).
my([am|A], A).
true.
?- listing(zar).
zar(A, C) :-
blar(A, B),
car(B, C).
true.
?- listing(blar).
blar([a|A], A).
true.
?- listing(car).
car([train|A], A).
true.
?- listing(wiggle).
wiggle([toot|A], A).
wiggle(A, C) :-
wiggle(A, B),
wiggle(B, C).
true.
?- halt.
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- [user].
|: s --> [a,b].
|: s --> a, s, b.
|: a --> [a].
|: b --> [b].
|: ^D% user://1 compiled 0.00 sec, 4 clauses
true.
?- halt.
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- [user].
|: s --> [].
|: s --> a, s, b.
|: a --> [a].
|: b --> [b,b].
|: ^D% user://1 compiled 0.00 sec, 4 clauses
true.
?- halt.
后续可以根据实践环节的要求参考本上机操作熟练掌握本章内容!