DOCUMENTED FOR INDIVIDUAL ASSIGNMENT OF

ADVANCED PROGRAMMING LANGUAGE CONCEPTS (CE300331-3)
Project Completed By
Maheeka Jayasuriya (CB003346 ) j GF10B1SE
Submitted To
Mr. Gamindu Hemachandra
Date of assigned
18
th
April 2011
Date of submission
13
th
June 2011
TABLE OF CONTENTS
TABLE OF CONTENTS..........................................................................................................................1
ABSTRACT..............................................................................................................................................2
1 HASKELL TEXT PROCESSING SYSTEM....................................................................................3
1.1 Simple Functions......................................................................................................................4
1.1.1 subString.......................................................................................................................4
1.1.2 insert.............................................................................................................................3
1.1.3 remove..........................................................................................................................3
1.1.4 replace..........................................................................................................................6
1.1.3 indexOf.........................................................................................................................6
1.2 Higher Order Functions............................................................................................................7
1.2.1 isCapitalized.................................................................................................................7
1.2.2 toCaseIndex..................................................................................................................7
1.2.3 toCase...........................................................................................................................8
1.2.4 freq...............................................................................................................................9
1.2.3 sort................................................................................................................................9
1.2.6 Validating an email.....................................................................................................10
1.3 Primitive Recursion................................................................................................................10
1.3.1 swap............................................................................................................................10
1.3.2 isSubString.................................................................................................................11
2 PROLOG FAMILY TREE...............................................................................................................12
2.1 FACTS....................................................................................................................................12
2.2 Rules.......................................................................................................................................13
2.3 QUERIES...............................................................................................................................14
3 CONCLUSION...................................................................................................................................17
4 REFERENCE......................................................................................................................................18
1
ABSTRACT
This documentation discusses two systems. The first system is made of Haskell and the other is Prolog.
The Haskell system is a text processing system. It incorporates some common text processing functions
that are handy in areas such as natural language processing. These functions can be used by any haskell
program by importing TextProcessor.hs. The prolog system is to implement a family tree. It defines
some relationships and also defines rules to derive relationships.
2
1 HASKELL ~ TEXT PROCESSING SYSTEM
This chapter discusses the functions in the text processing system developed using Haskell. The
functions fall under four categories. Following are the names of the functions that fall under each
category.
1. Text based menu
i. menu
2. Simple functions
i. compareToIgnoreCase
ii. subStringIndex
iii. subStringLen
iv. getElem
v. setElem
vi. insert
vii. remove
viii. replaceIndex
ix. indexOf
3. Higher order functions
i. isPascal
ii. toCase
iii. toCaseIndex
iv. freq
4. Primitive recursion
i. swap
ii. isSubString
iii. split
iv. unsplit
v. replace
3
vi. elements
S. List Comprehension
i. upper
ii. lower
iii. pascal
iv. delete
v. elemFreq
All above functions are in module Text Processing, except menu which is in module Menu. Following
is the discussion for each of the above functions.
1.1 Simple Functions
1.1.1 subString
subStringIndex
Function
subS1J1hgThdex ..Th1->Th1->S1J1hg->S1J1hg
Definition
subS1J1hgThdex 1 ¸ s1J
]{1>{-1)) && {¸>{-1)) = 1ake {¸-1+1) {dJop 1 s1J)
]o1heJw1se = eJJoJ "pìease eh1eJ hoh hega11ve 1hdexes"
Explanation
Th1s 1uhc11oh w1ìì 1ake 1h a s1J1hg, s1J ahd g1ves 1he subs1J1hg o1
11 by 1ak1hg chaJs 1h 1hdex Jahge o1 1 1o ¸. The 1hdex1hg o1 1he
chaJs 1h 1he s1J1hg s1aJ1s 1Jom 0.
Screen shot:
Test Case Expected Output Actual Output
Input numbers non-negative integers and
string given
Gives the correct substring
Input numbers negative or not valid. Gives error message
subStringLen
Function
subS1J1hgLeh ..Th1->Th1->S1J1hg->S1J1hg
Definition
subS1J1hgLeh 1 ìeh s1J
4
]{1>{-1)) && {ìeh>0) = 1ake ìeh {dJop 1 s1J)
]o1heJw1se = eJJoJ "pìease eh1eJ vaì1d 1hpu1"
Explanation
Th1s 1uhc11oh w1ìì 1ake 1h a s1J1hg, s1J ahd g1ves 1he subs1J1hg o1
11 by 1ak1hg a humbeJ o1 chaJs g1veh by ìehg1h s1aJ11hg 1Jom a g1veh
1hdex.
Screen shot:
Test Case Expected Output Actual Output
Input numbers non-negative integers and
string given
Gives the correct substring
Input numbers negative or not valid. Gives error message
1.1.2 insert
Function
1hseJ1 ..|a]->Th1->|a]->|a]
Definition
1hseJ1 ì1s1 1 hew
]{1>{-1)) = 1ake 1 ì1s1 ++ hew ++ dJop 1 ì1s1
]o1heJw1se = eJJoJ "pìease eh1eJ vaì1d 1hpu1"
Explanation
ThseJ1s a s1J1hg 1o aho1heJ s1J1hg g1veh, s1aJ11hg a1 a 1hdex g1veh.
S1hce 1h1s 1uhc11oh uses a 1oJ 11s paJame1eJs, wh1ch de11hes a
geheJaì case, 11 woJks 1oJ ahy k1hd o1 ì1s1, ho1 ì1m111hg 1o
s1J1hgs.
Screen shot:
Test Case Expected Output Actual Output
Input numbers non-negative integers and
string given
Gives the correct substring
Input numbers negative or not valid. Gives error message
1.1.3 remove
Function
Jemove ..|a]->Th1->Th1->|a]
3
Definition
Jemove ì1s1 s1aJ1 ehd
]{s1aJ1>{-1))&&{ehd>{-1))= 1ake s1aJ1 ì1s1 ++ dJop {ehd+1) ì1s1
]o1heJw1se = eJJoJ "pìease eh1eJ vaì1d 1hpu1"
Explanation
Removes a paJ1 o1 a s1J1hg JepJeseh1ed by s1aJ1 ahd ehd 1hdexes.
Screen shot:
Test Case Expected Output Actual Output
Input numbers non-negative integers and
string given
Gives the correct substring
Input numbers negative or not valid. Gives error message
1.1.4 replace
Function
Jepìace ..Eq a => a->a->|a]->|a]
Definition
Jepìace _ _ |] = |]
Jepìace oìd hew {x.xs)
]{x==oìd) = |hew] ++ Jepìace oìd hew xs
]o1heJw1se = |x] ++ Jepìace oìd hew xs
Explanation
Repìaces a g1veh chaJ by aho1heJ hew chaJ 1oJ a s1J1hg. Th1s woJks
1oJ ahy k1hd o1 ì1s1.
Screen shot:
Test Case Expected Output Actual Output
Input valid characters and valid string Gives correctly replaced string
Invalid input characters(i.e. Numbers) Converts it to a string and perform
same replace operation.

1.1.S indexOf
Function
1hdex01 ..Eq a => |a]->a->Th1
Definition
1hdex01 ì1s1 c
6
]d==ìehg1h ì1s1 = -1
]o1heJw1se = d
wheJe
d = ìehg1h {1akeWh1ìe {/=c) ì1s1)
Explanation
C1ves 1he 1hdex o1 a paJ11cuìaJ chaJac1eJ appeaJ1hg 1h a g1veh
s1J1hg. Re1uJhs 1he 1hdex o1 1he 11Js1 pos111oh o1 1he ma1ch1hg
chaJac1eJ 1ouhd. Re1uJhs -1 11 chaJac1eJ was ho1 1ouhd.
Screen shot:
Test Case Expected Output Actual Output
Give a valid char and a string Gives the correct index of the first
occurrence of the char

Char is not in the string Returns -1
1.2 Higher Order Functions
1.2.1 isCapitalized
Function
1sCap11aì1zed..S1J1hg->Booì
Definition
1sCap11aì1zed {x.xs) = {1sbppeJ x) && {1oìdJ1 {&&) {map 1sLoweJ xs))
Explanation
Checks whe1heJ a 1hpu1 s1J1hg 1s cap11aì1zed. Tha1 1s ohìy 11Js1
ìe11eJ 1s uppeJ case ahd o1heJ ìe11eJs aJe ìoweJ case. Th1s uses
h1gheJ-oJdeJ 1uhc11ohs map ahd 1oìdJ1. {map 1sLoweJ xs) w1ìì appìy
1sLoweJ 1uhc11oh oh each chaJ o1 1he s1J1hg ahd Je1uJh a booìeah
ì1s1. Th1s booìeah vaìues aJe 1akeh each ahd appì1ed `ahd`
opeJa11oh. T1 aìì vaìues 1h 1he ì1s1 aJe 1Jue ahd 1he 11Js1 ìe11eJ
Je1uJhs 1Jue 1Jom 1sbppeJ 1uhc11oh 11 Je1uJhs TJue.
Screen shot:
Test Case Expected Output Actual Output
7
Valid input string with capitalized string Return true
Valid input string not capitalizd Return false
1.2.2 toCaseIndex
Function
1oCaseThdex ..{S1J1hg->S1J1hg)->Th1->Th1->S1J1hg->S1J1hg
Definition
1oCaseThdex _ _ _ |] = |]
1oCaseThdex 1 s1aJ1 ehd s1J
]{s1aJ1>{-1)) && {ehd>{-1)) = 1ake s1aJ1 s1J ++ {1 {subS1J1hgThdex
s1aJ1 ehd s1J)) ++ dJop {ehd+1) s1J
]o1heJw1se = eJJoJ "pìease eh1eJ vaì1d 1hpu1"
Explanation
Th1s 1uhc11oh 1akes 1h a 1uhc11oh o1 1ype {S1J1hg->S1J1hg).
TheJe1oJe 11 1s a h1gheJ-oJdeJ 1uhc11oh. The 1uhc11ohs cohs1deJed 1o
be e11ec11ve 1h 1h1s 1s uppeJ,ìoweJ, swap ahd cap11aì1zed, wh1ch
cohveJ1s 1he cases o1 a s1J1hg. Wheh a s1aJ11hg ahd ehd1hg 1hdex 1s
g1veh 1oJ a sub s1J1hg, 11 cohveJ1s 1he case o1 1he sub s1J1hg w11h
1he g1veh 1uhc11oh.
Screen shot:
Test Case Expected Output Actual Output
When a string and valid indexes are given. Produces correct input
Invalid vertexes. Gives an error message
1.2.3 toCase
Function
1oCase ..{S1J1hg->S1J1hg)->S1J1hg->S1J1hg->S1J1hg
Definition
1oCase _ _ |] = |]
1oCase 1 sub {x.xs)
]s == sub = 1 {1ake {ìehg1h sub) {x.xs)) ++ 1oCase 1 sub xs
]o1heJw1se = |x] ++ 1oCase 1 sub xs
8
wheJe
s = 1ake {ìehg1h sub) {x.xs)
Explanation
F1hds a g1veh subs1J1hg 1h a s1J1hg, ahd cohveJ1s 1he1J case 1o 1he
g1veh case. S1m1ìaJ 1o 1he 11Js1, bu1 1hs1ead o1 ohe 1hs1ahce, 1h1s
may Jepìace ahy humbeJ o1 1hs1ahces 1he sub s1J1hg appeaJs.
Screen shot:
Test Case Expected Output Actual Output
Enter a valid substring and a string Converts case correctly
Enter a non available substring of the given
string
Returns the original string
1.2.4 freq
Function
1Jeq ..Eq a => |a]->a->Th1
Definition
1Jeq ì1s1 1 = ìehg1h {11ì1eJ {==1) ì1s1)
Explanation
Ce1s a s1J1hg ahd g1ves 1he 1Jequehcy o1 occuJJehce 1oJ a paJ11cuìaJ
chaJ 1h s1J1hg. bses 1he 11ì1eJ wh1ch 1s a h1gheJ-oJdeJ 1uhc11oh 1h
PJeìude.
Screen shot:
Test Case Expected Output Actual Output
Enter a available char in the string to find
frequency of
Returns the correct value
Enter a not available char in the string Returns zero
9
1.2.S sort
Function
soJ1 ..0Jd a=>|a]->|a]
Definition
soJ1 |] = |]
soJ1 {x . xs) = {soJ1 ìess) ++ {x . equaì) ++ {soJ1 gJea1)
wheJe
ìess = 11ì1eJ {< x) xs
equaì = 11ì1eJ {== x) xs
gJea1 = 11ì1eJ {> x) xs
Explanation
Th1s 1uhc11oh 1akes 1h a ì1s1 o1 s1J1hgs, aJJahges 1hem 1h ascehd1hg
oJdeJ ahd Je1uJhs 1he soJ1ed s1J1hg ì1s1. Th1s 1uhc11oh uses 11ì1eJ,
wh1ch 1s a h1gheJ-oJdeJ 1uhc11oh.
Screen shot:
Test Case Expected Output Actual Output
Input strings Sorts the strings in alphabetical order
1.2.6 Validating an email
Function
1sEma1ì..S1J1hg->Booì
Definition
--ema1ì 1oJma1 ìocaì-paJ1_doma1h.
1sEma1ì add = {1Jeq add `_` == 1) && {ìehg1h add <=254) && vaì1dLocaì &&
vaì1d0oma1h
wheJe
vaì1dLocaì = 1oìdJ1 {&&) {map 1sLocaì ìocaì)
ìocaì = 1akeWh1ìe {/=`_`) add
vaì1d0oma1h = {1Jeq doma1h `.` == 1) && 1oìdJ1 {&&) {map 1sAìpha
doma1hhoPeJ1od)
doma1h = 1a1ì {dJopWh1ìe {/=`_`) add)
doma1hhoPeJ1od = deìe1e `.` doma1h
spec1aì = "&-_."
1sLocaì..ChaJ->Booì
1sLocaì c = {eìem c spec1aì ]] 1sAìpha c ]] 1s01g11 c)
Explanation
Takes 1h a s1J1hg, sepaJa1es 11 1o domah ahd ìocaì paJ1 ahd
vaì1da1es each paJ1 1o see whe1heJ 11 has ohìy vaì1d chaJac1eJs ahd
vaì1d humbeJ o1 chaJac1eJs
Screen shot:
10
Test Case Expected Output Actual Output
Enter a valid email Produces true
Enter a wrong email Produces false
1.3 Primitive Recursion
1.3.1 swap
Function
swap ..S1J1hg->S1J1hg
Definition
swap |] = |]
swap {x.xs)
]1sLoweJ x = |1obppeJ x] ++ swap xs
]1sbppeJ x = |1oLoweJ x] ++ swap xs
]o1heJw1se = |x] ++ swap xs
Explanation
Swaps 1he case o1 each chaJac1eJ 1h ah 1hpu1 s1J1hg. Checks
chaJac1eJ by chaJac1eJ by coh11huousìy caìì1hg 1he same 1uhc11oh,
wh1ch 1s pJ1m111ve JecuJs1oh.
Screen shot:
Test Case Expected Output Actual Output
Enter a string with lower case only Converts to upper case
Enter a string with upper case only Converts to lower case
Enter a string with mixed case Converts lower to upper and upper
to lower

1.3.2 isSubString
Function
1sSubS1J1hg ..S1J1hg->S1J1hg->Booì
Definition
1sSubS1J1hg _ |] = Faìse
1sSubS1J1hg |] _ = TJue
1sSubS1J1hg {x.xs) {y.ys)--x s1J1hg, y subs1J1hg
11
]y == x = 1sSubS1J1hg ys xs
]y /= x = 1sSubS1J1hg ys {x.xs)
Explanation
Checks whe1heJ a g1veh s1J1hg 1s a subs1J1hg o1 aho1heJ s1J1hg.
Checks chaJac1eJ by chaJac1eJ by coh11huousìy caìì1hg 1he same
1uhc11oh, wh1ch 1s pJ1m111ve JecuJs1oh.
Screen shot:
Test Case Expected Output Actual Output
Enter a available substring Returns true
Enter a unavailable substring Returns false
12
2 PROLOG ~ FAMILY TREE
Above is the family tree which was used to define this system.
2.1 FACTS
CehdeJ
1. maìe{1omsJ).
2. 1emaìe{aì1ce).
PaJeh1
3. 1a1heJ{1omsJ,maJ1e).
4. mo1heJ{aì1ce,maJ1e).
Adop1ed paJeh1s
5. adop1edmo1heJ{maJ1e,¸ess1ca).
6. adop1ed1a1heJ{m1ke,¸eJemy).
13
Spouses
7. husbahd{1omsJ,aì1ce).
8. w11e{aì1ce,1omsJ).
0eceased
9. deceased{1omsJ).
0evoJced
10. devoJced{dough,add1e).
Above are some of the facts defined for the above family tree. Although 10 are listed here, this contains
around 100 facts under each relationship.
2.2 Rules
1. PaJeh1 . X 1s paJeh1 o1 Y 11 X 1s mo1heJ o1 Y oJ X 1s 1a1heJ o1 Y
paJeh1{X,Y).-mo1heJ{X,Y).
paJeh1{X,Y).-1a1heJ{X,Y).
2. Ch1ìd . X 1s ch1ìd o1 Y 11 Y 1s paJeh1 o1 X
ch1ìd{X,Y).-paJeh1{Y,X).
3. 0augh1eJ . X 1s daugh1eJ o1 Y 11 X 1s a ch1ìd o1 Y ahd X 1s 1emaìe
daugh1eJ{X,Y).-ch1ìd{X,Y),1emaìe{X).
4. Soh . X 1s soh o1 Y 11 X 1s a ch1ìd o1 Y ahd X 1s maìe
soh{X,Y).-ch1ìd{X,Y),maìe{X).
5. S1bì1hg . X 1s a s1bì1hg og Y 11 X`s mo1heJ ahd Y`s mo1heJ 1s W ahd X`s ahd Y`s
1a1heJ 1s Z. To avo1d ham1hg X ahd X oJ Y ahd Y as s1bì1hgs 1hseJ1s 1he cohd111oh X
1s ho1 equaìs 1o Y.
s1bì1hg{X,Y) .- mo1heJ{W,X),mo1heJ{W,Y),1a1heJ{Z,X),1a1heJ{Z,Y), ho1{X==Y).
bJo1heJ . X 1s bJo1heJ o1 Y, 11 X 1s a s1bì1hg o1 Y ahd X 1s maìe
bJo1heJ{X,Y).- s1bì1hg{X,Y), maìe{X)
s1s1eJ . X 1s s1s1eJ o1 Y, 11 X 1s s1bì1hg o1 Y ahd X 1s 1emaìe
s1s1eJ{X,Y).- s1bì1hg{X,Y), 1emaìe{X).
6. Auh1 ahd bhcìe . X 1s ah auh1 oJ uhcìe o1 Y 11 X`s paJeh1 1s a s1bì1hg o1 X
auh1oJuhcìe{X,Y) .- s1bì1hg{X,W),paJeh1{W,Y).
14
auh1{X,Y) .- auh1oJuhcìe{X,Y),1emaìe{X).
uhcìe{X,Y) .- auh1oJuhcìe{X,Y),maìe{X).
7. hephew ahd h1ece . X 1s a hephew oJ h1ece o1 Y 11 Y 1s a auh1 oJ uhcìe o1 X
hephewoJh1ece{X,Y) .- auh1oJuhcìe{Y,X).
hephew{X,Y) .- hephewoJh1ece{X,Y),maìe{X).
h1ece{X,Y) .- hephewoJh1ece{X,Y),1emaìe{X).
8. Cous1h . X 1s a cous1h o1 Y 11 X`s paJeh1 ahd Y`s paJeh1s aJe s1bì1hgs
cous1h{X,Y) .- s1bì1hg{b,W),paJeh1{b,Y),paJeh1{W,X).
cous1h1emaìe{X,Y) .- cous1h{X,Y), 1emaìe{X).
cous1hmaìe{X,Y) .- cous1h{X,Y), maìe{X).
9. CJahd PaJeh1 . X 1s a gJahd paJeh1 o1 Y 1s X`s paJeh1 has a paJeh1 as X
gJea1gJahdpaJeh1{X,Y) .- paJeh1{X,W),gJahdpaJeh1{W,Y).
gJea1gJahdmo1heJ{X,Y) .- gJea1gJahdpaJeh1{X,Y),1emaìe{X).
gJea1gJahd1a1heJ{X,Y) .- gJea1gJahdpaJeh1{X,Y),maìe{X).
10. S1bì1hgs 1h ìaw . X 1s a s1bì1hg 1h ìaw o1 Y 11 X`s husbahd 1s a s1bì1hg o1 Y
bJo1heJ1hìaw{X,Y) .- husbahd{X,W),s1bì1hg{W,Y).
bJo1heJ1hìaw{X,Y) .- bJo1heJ1hìaw{Y,X).
s1s1eJ1hìaw{X,Y) .- w11e{X,W),s1bì1hg{W,Y).
s1s1eJ1hìaw{X,Y) .- s1s1eJ1hìaw{Y,X).
11. 0escehdah1s ahd ahces1oJs.
ahces1oJ{X,Y) .- paJeh1{X,Y).
ahces1oJ{X,Y) .- paJeh1{X,W),ahces1oJ{W,Y).
descehdeh1{X,Y) .- ahces1oJ{Y,X).
2.3 QUERIES
1. Who aJe 1o aì1ce mo1heJ?
13
2. Who aJe maJ1es paJeh1s?
3. Who aJe s1bì1hgs o1 add1e?
4. Who aJe sohs o1 ìauJa?
5. Who aJe auh1s o1 ¸uì1e?
6. To whom aJe 1omsJ gJahdpaJeh1 1o?
7. Who aJe gJea1 gJahdpaJeh1s o1 Shawh?
8. To whom aJe beh bJo1heJ 1h ìaw 1o?
16
9. Who aJe descehdeh1s o1 add1e?
10. Who aJe ahces1oJs o1 s1eve?
17
3 CONCLUSION
The haskell system is not a complete system. It is a library. And it only deals with strings and chars.
Therefore, there is a lack of application of some of Haskell functionalities. Also the prolog system,
which is a family tree, is a very common system. Therefore, the relationships that are defined is very
common. It would be better if a different system was able to be implemented.
18
4 REFERENCE
Thompson, S., (1999) The Haskell: the Craft of Functional Programming. 2nd. Addison-Wesley
Longman Publishing Co., Inc.
19
ßEAû ThE5E Fûß PßûLûC ANû TEXT ßA5Eû NENu |Fûß Yûuß ßEFEßENCE ûNLY)
TEXT ßA5Eû NENu - Iû
− use recursion techniques for the menu.
− the user must not have to type the function on console. You should call the function for him.
− refer slides IO, thats all is needed.
PROLOG
3ohn ~ Mary Peter ~ Sue
[ [
------------------------------------------------------ ------------------------------------
[ [ [ [ [
3im 3oe Pam ~ Fred Nick
j j
----- ------------------
j j j
3udy Tim Tom
Following are the facts for the above family tree. Judy is adopted ( ,) ). All the following are included
in a .pl file. Everything in family.pl file is called as the database. If its a query running on swi console,
they are started with and given in red colour ok. Facts in database family.pl file is given in green
colour and rules in family.pl database is given in blue colour.
male(john). --- meaning °john is male¨. Ends with period. Queries also end with period.
male(peter).
female(mary).
female(sue).
male(joe).
male(jim).
male(fred).
male(nick).
female(pam).
20
father(john,jim).---- meaning °john is father of jim¨
father(john,joe).
father(john,pam).
father(peter,fred).
father(peter,nick).
mother(mary,jim).
mother(mary,joe).
mother(mary,pam).
mother(sue,fred).
mother(sue,nick).
father(john,jim). asking, °is john father of jim¨. Interpreter reads the database (.pl) file from
beginning to see whether a matching fact can be found. If so returns true, else false
True
father(john,X). asking who are john father two. X denotes anything. All facts and lower case.
Variables such as X here are capital case. Interpreter checks through all facts and returns all possible
values for X.
X ¬ jim initially it will only return jim, cos that is the first fact interpreter matches to. But when
you press semicolon , , it will return the other possible values for X as well.
X ¬ joe
X ¬ pam
• facts, queries end with period.
• If multiple answers needed hit ;
following are some rules that also come in family.pl file
parent(X,Y):-father(X,Y). meaning, °if X is father if Y, then X is also parent of Y¨ (start from right
when reading a query. But values inside parenthesis read from left to right. Any convension convenient
with you is ok.
parent(X,Y):-mother(X,Y). meaning °if X is mother of Y, then X is also parent Y¨
• this is the way of saying that X can be parent of Y, if X is either mother or father of Y
:- symbol means 'if'
21
Following are some new queries now tested.
parent(john,pam)
true
parent(john,X) jim,joe,pam meaning °to whom are john parent to¨
X ¬ jim
X ¬ joe
X ¬ pam
parent(Y,pam) meaning °who are parents of pam¨
Y ¬ john
Y ¬ mary
Now lets define another rule to family.pl
child(X,Y):-parent(Y,X). meaning °if Y is a parent of X, then X is a child of Y¨
daughter(X,Y):-child(X,Y),female(X). meaning ° if X is female and X is a child of Y, then X is
daughter of Y¨
comma , denotes 'and',
*It is important to follow a consistent way of reading a fact. Easy to start the relationship from
left to right.
A new fact :
mother(pam,tim). °pam is mother of tim¨
father (fred,tim)
A new rule :
grandmother(X,Y):-mother(X,W),parent(W,Y),female(X) °if X is female 'and' if W is parent
of Y 'and' if X is mother of W then X is grandmother of Y ( I think here female (X) is not
needed cos anyway mother is female. Further explained.
//X is the grandmother of Y :- (if) X is the mother of (parent of Y ¬ W)
22
//X is the gradmother of Y if, X is the mother of mother of Y
a new query
grandmother(R,Tim) asking °who is the grandmother of Tim¨
Mary
Sue
Now lets define ancestor. Ancestor is anyone who is a relative of a person. Includes parents,
grandparents, great-grandparents, etc
(1) ancestor(X,Y):-parent(X,Y) meaning °if X is a parent of Y then X is a parent of Y¨
but this is not enough. We need to have grandparents and great-grandparents relationship as well.
(2) ancestor(X,Y):-parent(X,W),parent(W,Y) meaning °if W is parent of Y and X is parent of
W, then X is an ancestor of Y¨. This in a more simplified manner is the relationship of grandparents.
Here, we can easily replace parent (W,Y) by ancestor (W,Y) using the first rule.
Therefore,
(3) ancestor(X,Y):-parent(X,W),ancestor(W,Y) becomes a more general and recursive rule that
can be used for any kind of ancestor. Therefore, only 1
st
and 3
rd
rules are enough to define ancestors.
Now, try to define,
mother_in_law, father_in_law, brother_in_law,sister_in_law
23