F unction Symb ols in Prolog� F rom Deductive Databases to Logic Programs In logic� there a re t w o kinds of objects� p redicates and functions � � Predicates rep resent statements ab out the w o rld� John hates Mary� hates�john�mary�� John is short� short�john� � hates is a p redicate symb ol� is an short�john� atomic fo rmula� F unction terms rep resent objects in the w o rld � the mother of Mary� mother�of�mary� a rectangle of length � and width �� rectangle����� � mother�of�mary� is a function term� rectangle is a function symb ol� ��
F unction terms do not have values� In Pro� log� they act as data structures� let denote a p oint in ��dim space p��X�Y� let denote a p oint in ��dim space� p��X�Y�Z� W rite a Prolog p rogram� SQDIST�Point��Point��D� � that returns the squa re of the distance b e� t w een t w o p oints� The p rogram should w o rk fo r �� and ��dim p oints� W ant� SQDIST�p������� p������� D� returns D � �������� � �������� � ��� � �� and SQDIST�p��������� p��������� D� returns D � �������� � �������� � �������� � ����� � �� and SQDIST�p������� p��������� D� is undefined ��
Prolog Program� ��� SQDIST�p��X��Y��� p��X��Y��� D� �� XD is X��X�� YD is Y��Y�� D is XD�XD � YD�YD� ��� SQDIST�p��X��Y��Z��� p��X��Y��Z��� D� �� XD is X��X�� YD is Y��Y�� ZD is Z��Z�� D is XD�XD � YD�YD � ZD�ZD� Query � SQDIST�p������� p������� D� This query uni�es with the head of rule ��� f X� n �� Y� n � g with Y� n �� X� n �� so� is XD X��X� � ��� � �� is YD Y��Y� � ��� � �� � � is D ���� � ���� � �� So� is returned D��� Note� the query do es not unify with the head of rule ��� � so only rule is used� ��� ��
Prolog Program� ��� SQDIST�p��X��Y��� p��X��Y��� D� �� XD is X��X�� YD is Y��Y�� D is XD�XD � YD�YD� ��� SQDIST�p��X��Y��Z��� p��X��Y��Z��� D� �� XD is X��X�� YD is Y��Y�� ZD is Z��Z�� D is XD�XD � YD�YD � ZD�ZD� Query � SQDIST�p���������p���������D�� This query uni�es with the head of rule ��� � f X� n �� Z� n � g with Y� n �� Z� n �� X� n �� Y� n �� so� is XD ��� � �� is YD ��� � �� is ZD ��� � �� is D ����� � �� So� is returned D��� Note� the query do es not unify with the head of rule ��� � so only rule is used� ��� ��
Prolog Program� ��� SQDIST�p��X��Y��� p��X��Y��� D� �� XD is X��X�� YD is Y��Y�� D is XD�XD � YD�YD� ��� SQDIST�p��X��Y��Z��� p��X��Y��Z��� D� �� XD is X��X�� YD is Y��Y�� ZD is Z��Z�� D is XD�XD � YD�YD � ZD�ZD� Query � SQDIST�p������� p��������� D�� Note� this query do es not unify with any rule� so Prolog simply returns no � i�e�� no answ ers fo r D � ��
Returning F unction T erms as Answ ers � � given a p oint� p��X�Y� � return a new p oint e�g with double the co o rdinates� � � e�g Query � double�p�������P� Answ er � P � p������� Prolog Program� double�p��X��Y��� p��X��Y��� �� X� is ��X�� Y� is ��Y�� In Plain English� if and ��Y� � X� � ��X� Y� � then the double of is p��X��Y�� � p��X��Y�� An equivalent p rogram using � � �� double�p��X��Y��� P� �� X� is ��X�� Y� is ��Y�� P � p��X��Y��� Here� � � � is b eing used to assign a value to va riable P � T ry to avoid this����� It re�ects p ro cedural thinking� ��
Sample Execution Prolog Program� double�p��X��Y��� p��X��Y��� �� X� is ��X�� Y� is ��Y�� Query � double�p�������P� The query uni�es with the head of the rule� where the mgu is f X� n �� P n p��X��Y�� g Y� n �� The b o dy of the rule then evaluates� ��X� � i�e�� � X� is ��Y� � i�e�� � Y� is The mgu b ecomes f X� n �� Y� n �� P n p������ g � So� the answ er is p������ � P � ��
Recursion with F unction Symb ols Example� Electrical circuits � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � Tw o resisto rs in series � with resistances R � and � resp ectively � R � � T otal resistance of the circuit is � � � � ��� Can rep resent the circuit as a function term� � series����� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � Tw o resisto rs in pa rallel � � � � � T otal resistance of the circuit is � � � � ��� Rep resent the circuit as a function term� � par����� � ��
Mo re Complex Circuits � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � par��� series������ � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � series�par������ par������ ��
Problem� W rite a Prolog p rogram that computes the total resistance of any circuit� F o r example� Query � resistance�series������ R� Answ er � R � ��� � � Query � resistance�par������ R� Answ er � R � ����������� � ��� � ��� Query � resistance�series���par������� R� Answ er � R � � � ��� � ��� Query � resistance��� R� Answ er � R � � ��
Solution ��� resistance�R�R� �� number�R�� ��� resistance�series�C��C��� R� �� resistance�C�� R��� resistance�C�� R��� R is R��R�� ��� resistance�par�C��C��� R� �� resistance�C��R��� resistance�C��R��� R is �R��R����R��R��� Sample Query� resistance�series���par������� TR� i�e� � compute the total resistance� TR � of the follo wing circuit� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��
resistance(series(3,par(6,3)),R), R = 5 RECURSIVE (WINDING) PHASE rule (2) resistance(3,R1), resistance(par(6,3),R2), R is R1+R2 is 5 UNWINDING PHASE rule (1) a R1 = 3 resistance(6,R21), resistance(3,R22), R2 is (R21*R22)/(R21+R22) rule (1) rule (1) is (6*3)/(6+3) is 2 R21 = 6 R22 = 3 ��
Recommend
More recommend