f i r s t p rol og p rogr am m i n g a s s i gn m e n t s

31
First Prolog Programming Assignment Solution Learning Abstract This assignment focuses on getting acquainted with the various functionalities of Prolog. Task 1 familiarizes oneself with knowledge bases and predicates (rules) using the Map Coloring problem. Task 2 similarly explores the idea of a knowledge base and predicates by creating a world of shapes. Task 3 utilized Pokemon and presented challenges for coding predicates to fit different problems of retrieval from the provided knowledge base. Lastly, Task 4 dealt with list processing in Prolog. Some problems in this task featured recursion, while others focused on playing around with the Heads and Tails of lists, which can be likened to the ‘car’ and ‘cdr’ of lists in Racket. Task 1: Map Coloring Code % --------------------------------------------------------------------------- ----- % File: mapcoloring.pro % Line: Program to find a 4 color map rendering for a picture of multiple rectangles. % More: The colors used will be purple, green, yellow, gray. % More: R# denotes each rectangle. % --------------------------------------------------------------------------- ----- % different(X,Y) :: X is not equal to Y different(green,yellow). different(green,purple). different(green,gray). different(yellow,green). different(yellow,purple). different(yellow,gray). different(purple,green). different(purple,yellow). different(purple,gray). different(gray,green). different(gray,yellow).

Upload: others

Post on 02-Feb-2022

0 views

Category:

Documents


0 download

TRANSCRIPT

First Prolog Programming Assignment Solution

Learning Abstract This assignment focuses on getting acquainted with the various functionalities of Prolog. Task 1 familiarizes oneself with knowledge bases and predicates (rules) using the Map Coloring problem. Task 2 similarly explores the idea of a knowledge base and predicates by creating a world of shapes. Task 3 utilized Pokemon and presented challenges for coding predicates to fit different problems of retrieval from the provided knowledge base. Lastly, Task 4 dealt with list processing in Prolog. Some problems in this task featured recursion, while others focused on playing around with the Heads and Tails of lists, which can be likened to the ‘car’ and ‘cdr’ of lists in Racket.

Task 1: Map Coloring

Code % --------------------------------------------------------------------------- ----- % File: mapcoloring.pro % Line: Program to find a 4 color map rendering for a picture of multiple rectangles. % More: The colors used will be purple, green, yellow, gray. % More: R# denotes each rectangle.

% --------------------------------------------------------------------------- ----- % different(X,Y) :: X is not equal to Y

different(green,yellow). different(green,purple). different(green,gray). different(yellow,green). different(yellow,purple). different(yellow,gray). different(purple,green). different(purple,yellow). different(purple,gray). different(gray,green). different(gray,yellow).

different(gray,purple).

% --------------------------------------------------------------------------- ----- % coloring(R1, R2, R3, R4, R5, R6, R7, R8, R9) :: Colors each of the rectangles % such that no border shares the same color.

coloring( R1 , R2 , R3 , R4 , R5 , R6 , R7 , R8 , R9 ) :- different( R1 , R2 ), different( R1 , R4 ), different( R1 , R5 ), different( R2 , R1 ), different( R2 , R3 ), different( R2 , R4 ), different( R3 , R2 ), different( R3 , R4 ), different( R3 , R5 ), different( R4 , R1 ), different( R4 , R2 ), different( R4 , R3 ), different( R4 , R5 ), different( R4 , R6 ), different( R4 , R7 ), different( R4 , R9 ), different( R5 , R1 ), different( R5 , R3 ), different( R5 , R4 ), different( R5 , R7 ), different( R5 , R9 ), different( R6 , R4 ), different( R6 , R7 ), different( R6 , R8 ), different( R6 , R9 ), different( R7 , R4 ), different( R7 , R5 ), different( R7 , R6 ), different( R7 , R8 ), different( R8 , R6 ), different( R8 , R7 ), different( R8 , R9 ), different( R9 , R5 ),

Demo

Map Coloring

different( R9 , R6 ), different( R9 , R7 ), different( R9 , R8 ).

Task 2: The Floating Shapes World

Code

% --------------------------------------------------------------------- ----------- % --------------------------------------------------------------------- ----------- % --- File: floatingshapesworld.pro % --- Line: Loosely represented 2-D shapes world (simple take on SHRDLU) % --------------------------------------------------------------------- -----------

% --------------------------------------------------------------------- ----------- % --- Facts ... % --------------------------------------------------------------------- ----------- % --------------------------------------------------------------------- ----------- % --- square(N,side(L),color(C)) :: N is the name of a square with side L % --- and color C

square(sera,side( 7 ),color(purple)). square(sara,side( 5 ),color(blue)). square(sarah,side( 11 ),color(red)).

% --------------------------------------------------------------------- -----------

% --- circle(N,radius(R),color(C)) :: N is the name of a circle with % --- radius R and color C

circle(carla,radius( 4 ),color(green)). circle(cora,radius( 7 ),color(blue)). circle(connie,radius( 3 ),color(purple)). circle(claire,radius( 5 ),color(green)).

% --------------------------------------------------------------------- ----------- % Rules ... % --------------------------------------------------------------------- -----------

% --------------------------------------------------------------------- ----------- % --- circles :: list the names of all of the circles

circles :- circle( Name , _ , _ ), write( Name ),nl,fail. circles.

% --------------------------------------------------------------------- ----------- % --- squares :: list the names of all of the squares

squares :- square( Name , _ , _ ), write( Name ),nl,fail. squares.

% --------------------------------------------------------------------- ----------- % --- squares :: list the name of all of the shapes

shapes :- circles,squares.

% --------------------------------------------------------------------- ----------- % --- blue(Name) :: Name is a blue shape

blue( Name ) :- square( Name , _ ,color(blue)). blue( Name ) :- circle( Name , _ ,color(blue)).

% --------------------------------------------------------------------- ----------- % --- large(Name) :: Name is a large shape

large( Name ) :- area( Name , A ), A >= 100.

% --------------------------------------------------------------------- ----------- % --- small(Name) :: Name is a small shape

small( Name ) :- area( Name , A ), A < 100.

% --------------------------------------------------------------------- ----------- % --- area(Name,A) :: A is the area of the shape with name Name

area( Name , A ) :- circle( Name ,radius( R ), _ ), A is 3.14 * R * R . area( Name , A ) :- square( Name ,side( S ), _ ), A is S * S .

Demo

Task 3: Pokemon KB Interaction and Programming

Query Demo

Code

% --------------------------------------------------------------------- ----------- % --------------------------------------------------------------------- ----------- % --- File: pokemon.pro % --------------------------------------------------------------------- -----------

% --------------------------------------------------------------------- ----------- % --- cen(P) :: Pokemon P was "creatio ex nihilo"

cen(pikachu). cen(bulbasaur). cen(caterpie). cen(charmander). cen(vulpix). cen(poliwag). cen(squirtle). cen(staryu).

% --------------------------------------------------------------------- ----------- % --- evolves(P,Q) :: Pokemon P directly evolves to pokemon Q

evolves(pikachu,raichu). evolves(bulbasaur,ivysaur). evolves(ivysaur,venusaur). evolves(caterpie,metapod). evolves(metapod,butterfree). evolves(charmander,charmeleon). evolves(charmeleon,charizard). evolves(vulpix,ninetails). evolves(poliwag,poliwhirl). evolves(poliwhirl,poliwrath). evolves(squirtle,wartortle). evolves(wartortle,blastoise). evolves(staryu,starmie).

% --------------------------------------------------------------------- ----------- % --- pokemon(name(N),T,hp(H),attack(A,D)) :: There is a pokemon with % --- name N, type T, hit point value H, and attach named A that does % --- damage D.

pokemon(name(pikachu), electric, hp( 60 ), attack(gnaw, 10 )). pokemon(name(raichu), electric, hp( 90 ), attack(thunder-shock, 90 )).

pokemon(name(bulbasaur), grass, hp( 40 ), attack(leech-seed, 20 )). pokemon(name(ivysaur), grass, hp( 60 ), attack(vine-whip, 30 )). pokemon(name(venusaur), grass, hp( 140 ), attack(poison-powder, 70 )).

pokemon(name(caterpie), grass, hp( 50 ), attack(gnaw, 20 )). pokemon(name(metapod), grass, hp( 70 ), attack(stun-spore, 20 )). pokemon(name(butterfree), grass, hp( 130 ), attack(whirlwind, 80 )).

pokemon(name(charmander), fire, hp( 50 ), attack(scratch, 10 )). pokemon(name(charmeleon), fire, hp( 80 ), attack(slash, 50 )). pokemon(name(charizard), fire, hp( 170 ), attack(royal-blaze, 100 )).

pokemon(name(vulpix), fire, hp( 60 ), attack(confuse-ray, 20 )). pokemon(name(ninetails), fire, hp( 100 ), attack(fire-blast, 120 )).

pokemon(name(poliwag), water, hp( 60 ), attack(water-gun, 30 )). pokemon(name(poliwhirl), water, hp( 80 ), attack(amnesia, 30 )). pokemon(name(poliwrath), water, hp( 140 ), attack(dashing-punch, 50 )).

pokemon(name(squirtle), water, hp( 40 ), attack(bubble, 10 )). pokemon(name(wartortle), water, hp( 80 ), attack(waterfall, 60 )). pokemon(name(blastoise), water, hp( 140 ), attack(hydro-pump, 60 )).

pokemon(name(staryu), water, hp( 40 ), attack(slap, 20 )). pokemon(name(starmie), water, hp( 60 ), attack(star-freeze, 20 )).

% --------------------------------------------------------------------- ----------- % --- display_names :: Lists names of all pokemon.

display_names :- pokemon(name( Name ), _ , _ , _ ),write( Name ),nl,fail. display_names.

% --------------------------------------------------------------------- ----------- % --- display_attacks :: Lists names of all attacks.

display_attacks :- pokemon( _ , _ , _ ,attack( Attack , _ )),write( Attack ),nl,fail. display_attacks.

% --------------------------------------------------------------------- ----------- % --- powerful(Name) :: Predicate that takes an input of a pokemon name % --- and succeeds if its attack is greater than 55 damage.

powerful( Name ) :- pokemon(name( Name ), _ , _ ,attack( _ , Damage )), Damage > 55.

% --------------------------------------------------------------------- ----------- % --- tough(Name) :: Predicate that takes an input of a pokemon name % --- and succeeds if it can absorb more than 100 units of damage.

tough( Name ) :- pokemon(name( Name ), _ ,hp( HP ), _ ), HP > 99.

% --------------------------------------------------------------------- ----------- % --- type(Name,Type) :: Predicate that takes inputs of a pokemon name % --- and type and succeeds if the type is correct.

type( Name , Type ) :- pokemon(name( Name ), T , _ , _ ), Type = T .

% --------------------------------------------------------------------- ----------- % --- dump_kind(Type) :: Predicate that takes pokemon type as an input % --- and returns all of the information pertaining to pokemon of the type.

dump_kind( Type ) :- listing(pokemon( _ , Type , _ , _ )),nl,fail.

% --------------------------------------------------------------------- ----------- % --- display_cen :: Predicate that returns all "creatio ex nihilo" pokemon

display_cen :- cen( Name ),write( Name ),nl,fail. display_cen.

% --------------------------------------------------------------------- ----------- % --- family(CEN) :: Predicate that takes an input of the name % --- of a "creatio ex nihilo" pokemon and returns the "evolutionary family".

family( Cen ) :- evolves( Cen , Y ), write( Cen ), write( ' ' ), write( Y ), evolves( Y , Z ), write( ' ' ), write( Z ).

% --------------------------------------------------------------------- ----------- % --- families :: Predicate that returns all "evolutionary families."

families :- cen( Cen ), evolves( Cen , Y ), nl, write( Cen ), write( ' ' ), write( Y ), evolves( Y , Z ), write( ' ' ), write( Z ),fail. families.

% --------------------------------------------------------------------- ----------- % --- lineage(Name) :: Predicate that takes a pokemon name as an input and % --- returns all information pertaining to the Pokemon and its family.

lineage( Name ) :-

pokemon(name( Name ), Type ,hp( HP ),attack( Attack , Damage )), write(pokemon(name( Name ), Type ,hp( HP ),attack( Attack , Damage ))),nl,

evolves( Name , Y ),

pokemon(name( Y ), Type2 ,hp( HP2 ),attack( Attack2 , Damage2 )), write(pokemon(name( Y ), Type2 ,hp( HP2 ),attack( Attack2 , Damage2 ))),nl,

evolves( Y , Z ), pokemon(name( Z ), Type3 ,hp( HP3 ),attack( Attack3 , Damage3 )), write(pokemon(name( Z ), Type3 ,hp( HP3 ),attack( Attack3 , Damage3 ))).

Demo Pt. II

Task 4: Lisp Processing in Prolog

Head/Tail Referencing Exercises My Guesses

?- [H|T] = [red, yellow, blue, green]. H = red, T = [yellow, blue, green]. ?- [H, T] = [red, yellow, blue, green]. Doesn’t work - false ?- [F|_] = [red, yellow, blue, green]. F = red. ?- [_|[S|_]] = [red, yellow, blue, green]. S = yellow. ?- [F|[S|R]] = [red, yellow, blue, green]. F = red, S = yellow,

R = [blue, green]. ?- List = [this|[and, that]]. List = [this, and, that]. ?- List = [this, and, that]. List = [this, and, that]. ?- [a,[b, c]] = [a, b, c]. false ?- [a|[b, c]] = [a, b, c]. true ?- [cell(Row,Column)|Rest] = [cell(1,1), cell(3,2), cell(1,3)]. cell(Row,Column) = cell(1,1), Rest = [cell(3,2), cell(1,3)]. ?- [X|Y] = [one(un, uno), two(dos, deux), three(trois, tres)]. X = one(un,uno), Y = [two(dos, deux), three(trois, tres)].

Demo

Example List Processors

Code

% ----------------------------------------------------------- --------------------- % ----------------------------------------------------------- --------------------- % --- File: list_processors.pro % ----------------------------------------------------------- ---------------------

% ----------------------------------------------------------- --------------------- % --- first(List,Name) :: Predicate that returns head of list. first([ H | _ ], H ).

% ----------------------------------------------------------- --------------------- % --- rest(List,Name) :: Predicate that returns tail of list. rest([ _ | T ], T ).

% ----------------------------------------------------------- --------------------- % --- last(List,Name) :: Predicate that returns last element of list.

last([ H |[]], H ). last([ _ | T ], Result ) :- last( T , Result ).

% ----------------------------------------------------------- --------------------- % --- nth(Num,List,Name) :: Predicate that returns nth element of list. nth( 0 , [ H | _ ], H ). nth( N ,[ _ | T ], E ) :- K is N - 1 , nth( K , T , E ).

% ----------------------------------------------------------- --------------------- % --- writelist(List) :: Prints every list element. writelist([]). writelist([ H | T ]) :- write( H ), nl, writelist( T ).

% ----------------------------------------------------------- --------------------- % --- sum(List,Name) :: Adds all list elements together. sum([], 0 ). sum([ Head | Tail ], Sum ) :- sum( Tail , SumOfTail ), Sum is Head + SumOfTail .

% ----------------------------------------------------------- --------------------- % --- add_first(Element,List,Result) :: Adds Element to front of List. add_first( X , L ,[ X | L ]).

% ----------------------------------------------------------- --------------------- % --- add_last(Element,List,Name) :: Adds Element to end of List. add_last( X , [], [ X ]). add_last( X , [ H | T ], [ H | TX ]) :- add_last( X , T , TX ).

% ----------------------------------------------------------- --------------------- % --- iota(Num,Size) :: Predicate that prints standard numbers up to Num. iota( 0 ,[]). iota( N , IotaN ) :- K is N - 1 , iota( K , IotaK ), add_last( N , IotaK , IotaN ).

% ----------------------------------------------------------- --------------------- % --- pick(List,Name) :: Predicate that returns random element of List. pick( L , Item ) :- length( L , Length ), random( 0 , Length , RN ), nth( RN , L , Item ).

% ----------------------------------------------------------- ---------------------

% --- make_set(List,Name) :: Predicate that returns set based on List. make_set([],[]). make_set([ H | T ], TS ) :- member( H , T ), make_set( T , TS ). make_set([ H | T ],[ H | TS ]) :- make_set( T , TS ).

Demo

List Processing Exercises

My Code

% --------------------------------------------------------------------- ----------- % --- product(List,Name) :: Predicate that returns product of List. product([], 1 ). product([ Head | Tail ], Product ) :- product( Tail , ProductOfTail ), Product is Head * ProductOfTail .

% --------------------------------------------------------------------- ----------- % --- factorial(Num,Name) :: Predicate that returns factorial of Num. factorial( 0 , 0 ). factorial( Num , Name ) :- iota( Num , Iota ), product( Iota , Product ), Name is Product .

% ---------------------------------------------------------------------

----------- % --- make_list(Num,Element,Name) :: Predicate that returns repeated List of Num size. make_list( 0 , _ ,[]). make_list( Num , Element , Name ) :- K is Num - 1 , make_list( K , Element , NameK ), add_last( Element , NameK , Name ).

% --------------------------------------------------------------------- ----------- % --- but_first(List,Name) :: Predicate that returns cdr of list. but_first([],[]). but_first([ _ ],[]). but_first([ _ | N ], N ).

% --------------------------------------------------------------------- ----------- % --- but_last(List,Name) :: Predicate that returns rdc of list. but_last([],[]). but_last([ _ ],[]). but_last([ H | T ], Name ) :- reverse( T , [ _ | B ]), reverse( B , RDC ), add_first( H , RDC , Name ).

% --------------------------------------------------------------------- ----------- % --- is_palindrome(List) :: Predicate that checks if list is a palindrome. is_palindrome([]). is_palindrome([ _ ]). is_palindrome( List ) :- first( List , FirstChar ), last( List , LastChar ), FirstChar = LastChar , but_first( List , A ), but_last( A , B ), is_palindrome( B ).

% --------------------------------------------------------------------- ----------- % --- noun_phrase(Name) :: Predicate that creates random noun phrase. noun_phrase( Name ) :- pick([fluffy, silky, soul-crushing, dreadful, helpful, fun], Adj ), pick([society, art, exam, savior, sleep, life, goose, arcade], Noun ), add_last( Adj ,[the], Start ), add_last( Noun , Start , Name ).

% --------------------------------------------------------------------- ----------- % --- sentence(Name) :: Predicate that creates random sentence. sentence( Name ) :- noun_phrase( N ), noun_phrase( M ), pick([kicked, fought, smiled, broke, purchased, stole, passed], Verb ), add_last( Verb , N , T ), append( T , M , Name ).

Demo