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 "pease 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 "pease eh1eJ va1d 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 "pease eh1eJ va1d 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 "pease eh1eJ va1d 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 Jepace ..Eq a => a->a->|a]->|a] Definition Jepace _ _ |] = |] Jepace od hew {x.xs) ]{x==od) = |hew] ++ Jepace od hew xs ]o1heJw1se = |x] ++ Jepace od hew xs Explanation Repaces 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 {1akeWh1e {/=c) 1s1) Explanation C1ves 1he 1hdex o1 a paJ11cuaJ 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 1sCap11a1zed..S1J1hg->Boo Definition 1sCap11a1zed {x.xs) = {1sbppeJ x) && {1odJ1 {&&) {map 1sLoweJ xs)) Explanation Checks whe1heJ a 1hpu1 s1J1hg 1s cap11a1zed. Tha1 1s ohy 11Js1 e11eJ 1s uppeJ case ahd o1heJ e11eJs aJe oweJ case. Th1s uses h1gheJ-oJdeJ 1uhc11ohs map ahd 1odJ1. {map 1sLoweJ xs) w1 appy 1sLoweJ 1uhc11oh oh each chaJ o1 1he s1J1hg ahd Je1uJh a booeah 1s1. Th1s booeah vaues aJe 1akeh each ahd app1ed `ahd` opeJa11oh. T1 a vaues 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 "pease eh1eJ va1d 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 cap11a1zed, 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. S1m1aJ 1o 1he 11Js1, bu1 1hs1ead o1 ohe 1hs1ahce, 1h1s may Jepace 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 {111eJ {==1) 1s1) Explanation Ce1s a s1J1hg ahd g1ves 1he 1Jequehcy o1 occuJJehce 1oJ a paJ11cuaJ chaJ 1h s1J1hg. bses 1he 111eJ wh1ch 1s a h1gheJ-oJdeJ 1uhc11oh 1h PJeude. 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 = 111eJ {< x) xs equa = 111eJ {== x) xs gJea1 = 111eJ {> 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 111eJ, 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) && va1dLoca && va1d0oma1h wheJe va1dLoca = 1odJ1 {&&) {map 1sLoca oca) oca = 1akeWh1e {/=`_`) add va1d0oma1h = {1Jeq doma1h `.` == 1) && 1odJ1 {&&) {map 1sApha doma1hhoPeJ1od) doma1h = 1a1 {dJopWh1e {/=`_`) add) doma1hhoPeJ1od = dee1e `.` doma1h spec1a = "&-_." 1sLoca..ChaJ->Boo 1sLoca c = {eem c spec1a ]] 1sApha c ]] 1s01g11 c) Explanation Takes 1h a s1J1hg, sepaJa1es 11 1o domah ahd oca paJ1 ahd va1da1es each paJ1 1o see whe1heJ 11 has ohy va1d chaJac1eJs ahd va1d 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 coh11huousy ca1hg 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 _ |] = Fase 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 coh11huousy ca1hg 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. mae{1omsJ). 2. 1emae{a1ce). PaJeh1 3. 1a1heJ{1omsJ,maJ1e). 4. mo1heJ{a1ce,maJ1e). Adop1ed paJeh1s 5. adop1edmo1heJ{maJ1e,ess1ca). 6. adop1ed1a1heJ{m1ke,eJemy). 13 Spouses 7. husbahd{1omsJ,a1ce). 8. w11e{a1ce,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. Ch1d . X 1s ch1d o1 Y 11 Y 1s paJeh1 o1 X ch1d{X,Y).-paJeh1{Y,X). 3. 0augh1eJ . X 1s daugh1eJ o1 Y 11 X 1s a ch1d o1 Y ahd X 1s 1emae daugh1eJ{X,Y).-ch1d{X,Y),1emae{X). 4. Soh . X 1s soh o1 Y 11 X 1s a ch1d o1 Y ahd X 1s mae soh{X,Y).-ch1d{X,Y),mae{X). 5. S1b1hg . X 1s a s1b1hg 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 s1b1hgs 1hseJ1s 1he cohd111oh X 1s ho1 equas 1o Y. s1b1hg{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 s1b1hg o1 Y ahd X 1s mae bJo1heJ{X,Y).- s1b1hg{X,Y), mae{X) s1s1eJ . X 1s s1s1eJ o1 Y, 11 X 1s s1b1hg o1 Y ahd X 1s 1emae s1s1eJ{X,Y).- s1b1hg{X,Y), 1emae{X). 6. Auh1 ahd bhce . X 1s ah auh1 oJ uhce o1 Y 11 X`s paJeh1 1s a s1b1hg o1 X auh1oJuhce{X,Y) .- s1b1hg{X,W),paJeh1{W,Y). 14 auh1{X,Y) .- auh1oJuhce{X,Y),1emae{X). uhce{X,Y) .- auh1oJuhce{X,Y),mae{X). 7. hephew ahd h1ece . X 1s a hephew oJ h1ece o1 Y 11 Y 1s a auh1 oJ uhce o1 X hephewoJh1ece{X,Y) .- auh1oJuhce{Y,X). hephew{X,Y) .- hephewoJh1ece{X,Y),mae{X). h1ece{X,Y) .- hephewoJh1ece{X,Y),1emae{X). 8. Cous1h . X 1s a cous1h o1 Y 11 X`s paJeh1 ahd Y`s paJeh1s aJe s1b1hgs cous1h{X,Y) .- s1b1hg{b,W),paJeh1{b,Y),paJeh1{W,X). cous1h1emae{X,Y) .- cous1h{X,Y), 1emae{X). cous1hmae{X,Y) .- cous1h{X,Y), mae{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),1emae{X). gJea1gJahd1a1heJ{X,Y) .- gJea1gJahdpaJeh1{X,Y),mae{X). 10. S1b1hgs 1h aw . X 1s a s1b1hg 1h aw o1 Y 11 X`s husbahd 1s a s1b1hg o1 Y bJo1heJ1haw{X,Y) .- husbahd{X,W),s1b1hg{W,Y). bJo1heJ1haw{X,Y) .- bJo1heJ1haw{Y,X). s1s1eJ1haw{X,Y) .- w11e{X,W),s1b1hg{W,Y). s1s1eJ1haw{X,Y) .- s1s1eJ1haw{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 a1ce mo1heJ? 13 2. Who aJe maJ1es paJeh1s? 3. Who aJe s1b1hgs o1 add1e? 4. Who aJe sohs o1 auJa? 5. Who aJe auh1s o1 u1e? 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 PLC AN TEXT A5E NENu |F Yu EFEENCE 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