This is a discussion on Logic Programming in C-Kermit - Protocols ; After a few rounds of Object-Oriented Programming in C-Kermit, I hope you've seen that C-Kermit can do OOP to solve some tough problems. I am about to embark on Patterns, an advanced software subject, when I tumble on the following ...
After a few rounds of Object-Oriented Programming in C-Kermit, I hope you've
seen that C-Kermit can do OOP to solve some tough problems. I am about to embark
on Patterns, an advanced software subject, when I tumble on the following puzzle
in a Prolog book (Prolog and Lisp are considered as the two big tools to
research artificial intelligence):
================================================== =============================
Implement an inference engine that can handle symmetric 2-place predicates
(those whose arguments can be interchanged) without looping. Put the functor
symmetric in front of each fact that is to have the symmetric property, and let
symmetric be a prefix operator. For example:
symmetric married(john,mary).
symmetric married(melody,michael).
?- prove(married(mary,john)).
yes
?- prove(married(john,mary)).
yes
?- prove(married(michael,Who)).
Who = melody
?- prove(married(abc,xyz)).
no
================================================== =============================
Following is my implementation in C-Kermit:
################################################## #############################
define symmetric {
if > \find({\%2},\m(\%1.contain)) 0 {
echo Relation \%1 \%2 already defined
return 1
}
local \&z[]
void \fsplit(\%2,&z,{,})
if > \farraylook(who,&z) -1 {
echo Cannot relate with who
return 0
}
_define \%1 1
_asg \%1.contain \m(\%1.contain)|\%2|
return 1
}
define prove {
if not define \m(\%1) {
echo Relation \%1 does not exist
return 0
}
local \%n \&w[] \&z[]
asg \%n \fsplit(\m(\%1.contain),&w,|)
void \fsplit(\%2,&z,{,})
if > \farraylook(\%2,&w) -1 {
echo Yes
return 1
} else if > \farraylook({\&z[2],\&z[1]},&w) -1 {
echo Yes
return 1
} else if > \find(\&z[2],who) 0 {
local \%f \%i
asg \%f 0
for \%i 1 \%n 1 {
if > \find(\&z[1],\&w[\%i]) 0 {
echo Who = \freplace(\freplace(\&w[\%i],\&z[1]),{,})
asg \%f 1
}
}
return \%f
} else {
echo No
return 0
}
}
################################################## #############################
# In a typical soccer tournament, the following matches are usual:
symmetric playsoccer Brazil,Italy
symmetric playsoccer Germany,Italy
symmetric playsoccer England,Brazil
symmetric playsoccer England,Germany
symmetric playsoccer Italy,Germany
symmetric playsoccer Holland,Germany
symmetric playsoccer France,Italy
symmetric playsoccer England,France
# In a chess tournament, the following encounters are possible:
symmetric playchess Karpov,Kasparov
symmetric playchess Leko,Kasparov
symmetric playchess Leko,Kramnik
symmetric playchess Karpov,Kramnik
symmetric playchess Annand,Kramnik
symmetric playchess Leko,Karpov
symmetric playchess Ponomariov,Karpov
symmetric playchess Polga,Karpov
symmetric playchess Leko,Polga
# In a party, the following pairs would happen:
symmetric danceWith Jane,Robert
symmetric danceWith Jennifer,Mark
symmetric danceWith Mary,John
symmetric danceWith Mary,Robert
symmetric danceWith Suzie,George
symmetric danceWith Kevin,Carol
symmetric danceWith Mark,Carol
# All relations playsoccer, playchess, and danceWith can live in the same
environment.
# To see if France play Germany:
prove playsoccer France,Germany
# To see which teams Brazil plays:
prove playsoccer Brazil,Who
# To see if Brazil plays Belgium:
prove playsoccer Brazil,Belgium
# To see if Mark kissed any girl in the party:
prove kiss Mark,Who
# You'll find the relation kiss hasn't been defined, that is nobody has kissed
anybody. Playboy is a rare animal.
As you see, everything, be it Expert system or AI, is only the fakes happening
on the surface. Below the surface is just the handling of bits and bytes.
With this, I've just entered the world of logic programming in C-Kermit. Stay
tuned, many exciting things are going to happen here.
The script is also found at:
ftp://kermit.columbia.edu/kermit/scr...rmit/symmetric
Dat