プログラム断片(2008/05/28)

for VisualWorks 7.6 with Jun780, Prologインタプリタのマニュアル(PDF)

Prologインタプリタを開く

JunPrologInterpreter open
01 Prologインタプリタのマニュアル(PDF)

論理プログラミングの事始め

apple.
tomato.
orange.
?- apple.
?- orange.
?- banana.
02
?- apple.
yes
?- orange.
yes
?- banana.
no

事実と規則のリスト

fruit :- apple.
fruit :- orange.
jam :- fruit, sugar.
?- listing.
03
?- listing.
apple.
fruit :- 
        apple.
fruit :- 
        orange.
jam :- 
        fruit, 
        sugar.
orange.
tomato.
yes

事実の追加と再度の質問

?- jam.
sugar.
?- jam.
04
?- jam.
no
?- jam.
yes

事実と規則の削除

?- remove.
?- listing.
05
?- remove.
yes
?- listing.
yes

表示の消去

?- clear.
06
yes

事実と規則そして質問

?- remove.
human(socrates).
human(platon).
human(aristotle).
mortal(X) :- human(X).
?- clear.
?- listing.
?- mortal(socrates).
?- mortal(aoki).
human(aoki).
?- mortal(aoki).
07
yes
?- listing.
human(socrates).
human(platon).
human(aristotle).
mortal(X) :- 
        human(X).
yes
?- mortal(socrates).
yes
?- mortal(aoki).
no
?- mortal(aoki).
yes

高度な質問

?- remove.
human(socrates).
human(platon).
human(aristotle).
human(aoki).
mortal(X) :- human(X).
?- clear.
?- listing.
?- mortal(X).
08
yes
?- listing.
human(socrates).
human(platon).
human(aristotle).
human(aoki).
mortal(X) :- 
        human(X).
yes
?- mortal(X).
X = socrates ;
X = platon ;
X = aristotle ;
X = aoki ;
no

ユニフィケーション

?- remove.
likes(john, apple).
likes(john, wine).
likes(mary, wine).
likes(mary, john).
?- clear.
?- listing.
?- likes(john, X).
?- likes(X, john).
?- likes(john, X), likes(mary, X).
09
yes
?- listing.
likes(john,apple).
likes(john,wine).
likes(mary,wine).
likes(mary,john).
yes
?- likes(john,X).
X = apple ;
X = wine ;
no
?- likes(X,john).
X = mary ;
no
?- likes(john,X), likes(mary,X).
X = wine ;
no

バックトラック

?- remove.
on(hen, cat).
on(cat, dog).
on(dog, donkey).
above(X, Y) :- on(X, Y).
above(X, Y) :- on(X, Z), above(Z, Y).
?- clear.
?- listing.
?- above(hen, X).
?- above(X, donkey).
?- above(X, Y).
10
13
yes
?- listing.
above(X,Y) :- 
        on(X,Y).
above(X,Y) :- 
        on(X,Z), 
        above(Z,Y).
on(hen,cat).
on(cat,dog).
on(dog,donkey).
yes
?- above(hen,X).
X = cat ;
X = dog ;
X = donkey ;
no
?- above(X,donkey).
X = dog ;
X = hen ;
X = cat ;
no
?- above(X,Y).
X = hen ,
Y = cat ;
X = cat ,
Y = dog ;
X = dog ,
Y = donkey ;
X = hen ,
Y = dog ;
X = hen ,
Y = donkey ;
X = cat ,
Y = donkey ;
no

高階述語

?- remove.
?- clear.
?- listing.
?- is(X, F(3, 4)).
11
yes
?- listing.
yes
?- is(X,F(3,4)).
F = + ,
X = 7 ;
F = - ,
X = -1 ;
F = * ,
X = 12 ;
F = / ,
X = (3/4) ;
F = F1 ,
X = F1(3,4) ;
no

相思相愛の問題【lovers(X,Y) :- 作成して.】

?- remove.
likes(john, apple).
likes(john, wine).
likes(john, mary).
likes(mary, wine).
likes(mary, john).
lovers(X, Y) :- 作成して.
?- clear.
?- listing.
?- lovers(X, Y).
12
yes
?- listing.
likes(john,apple).
likes(john,wine).
likes(john,mary).
likes(mary,wine).
likes(mary,john).
lovers(X,Y) :-  作成して.
yes
?- lovers(X,Y).
X = john ,
Y = mary ;
X = mary ,
Y = john ;
no

for VisualWorks 7.6 with Jun780, Prologインタプリタのマニュアル(PDF)


Updated: 2015/11/08 (Created: 2008/05/28) KSU AokiHanko