AI Homework 1 Aim: To be familiar with Prolog. If you have not written anything with prolog, consult to how to install prolog documents in homes.ieu.edu.tr/uyabas/ai.html Try all the examples, queries and rules and prepare a report. Write the query you executed and the output you get. 1- Knowledge Base: Knowledge Base may include facts in prolog. Prolog is case-sensetive. Save the following facts in a file with extension “.pl”. dances(george). dances(mary). listenstoMusic(george). listenstoMusic(mary). listenstoMusic(sally). dances(michael). Prolog now knows that George, Mary and Michael can dance and George, Mary and Sally listens music. Write to code above to a “.pl” document and consult this file in prolog. Note that a unary predicate like “dances” starts with lowercase and has one parameter. a) Lets query some questions to prolog. Who dances? Would be in Prolog written on the command line as: dances(X). Note that “X” is here used as a vraiable or “place-holder”. It will match anything that can dance. We can get all the people that dance by typing “;” after Prolog displays an answer. You can also check if Mary can dance by typing on the Prolog command the following query: dances(mary). b) Exercise: Query these in Prolog and report . dances(george). dances(sally). listenstoMusic(sally). 2- Rules: Lets define a rule now. Name of our rule will be “happy”. Someone who dances and listens to music is happy. Add the lines below to the same file and don’t forget to consult to your file again with the command “consult.pl”. a) happy(X):dances(X), listenstoMusic(X). Exercise: Try the following queries and report the answers. happy(george). happy(mary). happy(sally). happy(X). Some Examples: We will define some family relations. Add these lines to your file: parent(ali,ahmet). parent(ahmet,ayse). parent(zeynep,ayse). parent(ali,recep). parent(recep,hulya). male(ahmet). male(recep). male(ali). female(ayse). female(zeynep). female(hulya). Lets now define the “father” relation as a prolog rule: father(X,Y) :parent(X,Y), male(X). b) Try: father(X,ayse). father(recep,hulya). father(X,Y). Father(X,ayse). c) Exercise: Define the rules for “mother”, “grandfather”, “brother”, and “sister”. (You can add more facts to your knowledge base if needed). Here is an example rule that deifnes ancestorship relation in Prolog: ancestor(X,Y):parent(X,Z), parent(Z,Y). 3- Mathematical Functions a) Try Copy these lines to a file and consult the file. factorial(0,1). factorial(N,F) :N>0, N1 is N-1, factorial(N1,F1), F is N * F1. Now query the rule; factorial(5,F). b) Exercise: Try to write a rule called power(X,Y,Z) in Prolog in which Z=XY. 4- Lists Prolog uses brackets [...] as a list builder. The notation [X|Y] refers to a list whose first element is X and whose tail is Y. A finite list can be explicitly enumerated, such as [1,2,3,4]. The following three definitions manipulate lists, where 'car' refers to the first element of a list, 'cdr' refers to the tail or rest of the list, and 'cons' is the list constructor. car([X|Y],X). cdr([X|Y],Y). cons(X,R,[X|R]). meaning ... The head (car) of [X|Y] is X. The tail (cdr) of [X|Y] is Y. Putting X at the head and Y as the tail constructs (cons) the list [X|R]. Consider the following Prolog predicate: listsplit([H|T],H,T). a) Query: listsplit([1,2,3,4,5], A, B). However, we will see that these explicit definitions are unneeded. A list whose head is X and whose tail is Y can just be referred to using the Prolog term [X|Y]. Conversely, if the list can be unified with the Prolog term '[X|Y]' then the first element of the list is bound to (unified with) X and the tail of the list is bound to Y. Many of the predicates discussed in this section are "built-in" for many Prolog interpreters. b) Here is an example member rule: member(X,[X|T]). member[X,[Y|T]) :- member(X,T). Query these: member(a,[1,2,a,3]). member(a,[1,2,3]). member([3,Y], [[1,a],[2,m],[3,z],[4,v],[3,p]]). c) Let us try these rules that reverse a list. rev(X,Y,Z) is true iff list Z is the reverse of list X. rev([],X,X). rev([X|Y],Z,W) :- rev(Y,[X|Z],W). Note that the middle variable is used like a buffer. d) Try these and report: rev([a,b,c,d,e],[],Z). rev(X,[],[1,2,3,4,5]).