430 likes | 546 Views
AI - Programming. Prolog Lisp AI - Programming Support for Symbolic Computational Exploratory Programming. Support for Symbolic Computation. Programming Invokes - manipulating symbols (Numbers are not involved) Symbols Objects Relationship between objects
E N D
AI - Programming Prolog Lisp AI - Programming Support for Symbolic Computational Exploratory Programming
Support for Symbolic Computation • Programming Invokes - manipulating symbols (Numbers are not involved) • Symbols • Objects • Relationship between objects • Complex structures of symbols are needed to be captured
Symbol Structures • Are represented using “List” data structure • Element of the “List” may be a • Symbol • Another “List” • Example: (friends jim (joe mary anne))
Symbol Manipulation(Involve Pattern Matching) • Example • likes (kate, susie) • likes(X, susie) • By pattern matching X = kate (from above)
Two Requirements For AI-Prog. Language Are 1. Support flexible list-based data structures 2. Pattern matching
Support for Exploratory Programming • For most AI-languages classical software-engineering • Complete specification are very difficult to be described before a ‘Prototype’ is developed • Thus AI-Programming is ‘Exploratory in nature’ • The language should have exploratory characteristics • Such as EXTENSIBILITY, INTERACTIVITY
Support for Exploratory Programming (Cont’d) Extensibility:The ability to develop special purpose interpreters for solving classes of problems Interactivity:The programmer could test small sections of their programs interactively and flexibly
Lisp Extensively used in U.S.A Developed in 1950, and is based on Functional Definitions Name comes from two wordsList & Processing - LISP Prolog Widely used in Europe and Japan First program was written in 1970’s Writing programs is similar to writing predicate calculus statements. PROgramming in LOGic ProLog Main AI-Programming Languages
ProLog:A prolog programme is a set of specifications in predicate calculus describing objects in a problem domain Programme A prolog programme will have Division: two main parts a. Sets of facts b. Sets of rules (type declarations, initialisations…….. are not present)
Facts: • A predicate name (functor)(beginning with a lower letter case) • May have no arguments or no. of arguments. • These must end with a FULL STOP. • likes(X, Y). must end with a full stop Functor Arguments
Rules: • Consist of Implication statement • Examples: • bird(x): - animal(x), has-feathers(x) (only if)
Database:The set of specifications, or what is TRUE about a particular problem situation is called DATABASE Example: “Word of likes” likes (george, kate). likes(george, susie). likes(george, juile). likes(kate, susie). DATABASE
Closed Word Assumption “ Anything is FALSE whose opposite is not provably true” Example: likes(ali, book). If the compiler/interpreter will not find it in the database as ‘True’ it will assert its negation
Database • Needs to be very comprehensive • Any mistake could take you far from correct results
Connectives English Practical ProLog and ^ , or ; only if :- not ¬ not ^
Connectives (Cont’d) • likes (everyone, shahid).“Everyone likes shahid” • likes (asim, Y), likes (shazia, Y). • likes(ahsan , kamran):- likes(ahsan,cricket ).Ahsan likes Kamran if Ahsan likes cricket.
Database lectures (aliya, ai). lectures(john, databases). female(aliya). age(aliya, 29) office(aliya, s134). animal(lion). animal(sparrow). has-feathers(sparoow). Once the database is loaded. The interpreters could be asked questions. ? lectures(aliya,ai) yes ? lectures(aliya, databases) no ? lecture (aliya, X) X=ai ? lectures(Someone, ai) Someone = aliya
? lectures(X, Y) x = aliya y = ai; x = john y = databases; no
Suppose we have a rule: bird(x):- animal(x), has-feather(x). ? - bird(B) B = sparrow
Steps In Creating ProLog Programmes Constructs/ Commands Assert Prolog predicate to add new predicate to the present set. Syntax: assert (likes(d,s)) assert a : adds at the beginning assert s: adds at the end
Steps In Creating ProLog Programmes (Cont’d) Retract: To remove a predicate. Consult: To place an existing file in the database?[myfile] shorter form of consult Read: Takes the next term from the current input stream and binds it to the argumente.g., read(X), binds it to X.
Steps In Creating ProLog Programmes (Cont’d) See: Reads information from a file. Tell: Places information in a file. Listing: Listing(member).Will list all the classes with the “member” predicatelisting(predicate-name).
Steps In Creating ProLog Programmes (Cont’d) Trace: To monitor the progress of the prolog interpreter. Spy: Selective trace. Spy[member/2, append/3].
Recursion - Based Search Prolog Lists: [1, 2, 3, 4] [[george, kate], [ali, aneela]] [ali, shahid, hassan] [] “ ” bar operator: is used to separate element from the tail of the list.
[X | Y] Tail of the list (all the elements other than the head) Head of the list (first element of the list) 1. [tom, dick, harry, fred] matched with [X |Y]X = tomY = [dick, harry, fred] 2. [tom, dick, harry, fred] matched with [X, Y | Z]X = tom, Y = dick, Z = [harry, fred]
In order to check that any number is a member of a list or not ? member (a, [a, b, c, d, e]). yes ? member(a,[1, 2, 3, 4]). no ? member(X, [b, c, d]). X = b ; X = c; X = d; no
Example Comments 1. member (X, [X|T]). Base case 2. member(X,[Y|T]):- member(X,T). Recursion ? member(c, [a, b, c]). Call 1. Fail since c a Call 2. X=c, Y=a, T=[b,c], member (c,[b,c])? Call 1. Fail cb Call 2 . X=c, Y=b,T=[c], member(c,[c])? Call 1. Success, c = c yes(to second call 2) yes (to first call 2)
Example 1. Member (x, [x|t]). 2. Member(x,[y|t]):- member(x,t). ? Member(c, [a, b, c]). Call1. Fail since c a Call2. x=c, y=a, t=[b,c], member (c,[b,c])? Call1 fail cb Call2 x=c, y=b,t=[c], member(c,[c])? Call1. Success, c = c yes(to second call2) yes (to first call 2)
Writing Out A List writing list([]). writing list([H|T]):- write(H), nl, writelist(T). new line “writes one element of the list on each line” reverse - write list ([]). reverse - write list ([H|T]):- reverse - write list (T), write (H), nl.
Prolog Matching Example: 1. book(title(X), another(Y))book(title(databases), another(ali))X = databases.Y = ali. 2. ? 1 + 2 = 3no (no match possible).
Prolog Matching (Cont’d) ?- X + Y = 1 + 2 X = 1Y = 2 ?- lectures(X, ai) = lectures (ahsan, Y).X = alisonY = ai
Back Tracking • bird (type(sparrow), name(steve)). - R1 • bird(type(penguin), name( tweety)). - R2 • bird(type(penguin),name(percy)). - R3 • ?- bird(type(penguin), name(X)). • It will first try to match R1 - but fail. • When it will try to match R2 and succeed X = tweety • It will will put a pointer to this location and move on to succeed again x = percy.
Declarative and Procedural Views of Programs p :- q , r. (with and operator ,) p :- q ; r. (with or operator ;) Declarative reading: “p is true if q is true and r is true”. Procedural reading: “To solve problem p first solve problem q and then solve problem r”. or“do satisfy p, first satisfy q and then r”.
Recursion Base case ancestor (Person, Ancestor):- parent(Person, Ancestor). ancestor(Person, Ancestor):- parent(Person, Parent), ancestor(Parent, Ancestor). R1 Recursive case R2
Recursion Examples parent(alison, david). - F1 parent(alison, kathleen). - F2 parent(david, harold). - F3 parent(david, ida). - F4 parent(kathleen, john). - F5
? - ancestor(alison, harold). • Try to prove parent(alison, harold )- but will fail • will back track and try rule no.2parent (alison, parent) - succeed parent - david,ancestor(david, harold) - succeed - with rule 1 • The whole every will succeed.
Another Example • Develop a programme in PROLOG that reverses the following sentences • Example: • Do you speak English • Required output is : No I know Urdu
Steps Involved • Accept a sentence that is typed in by the user • Change each you into I • Likewise change any ARE to AM NOT • Change ENGLISH to URDU • Change DO to NO
Develop a Recursive Programme • Represent sentences as input and output list. • CHANGE the head of the input list into another word and let the head of the output list stand for that word. • ALTER the tail of the input list and let the tail of the output list stand for the altered tail. • If we have reached the end of the input list, then there is nothing more to go onto the output list, so we can terminate the output list with an empty list and stop.
Recursive Programme alter ( [] , [] ). alter ( [H | T] , [X | Y] ) :- change (H,X) , alter (T,Y). Database of facts change(you,i). change(are,[am , not]). change(english, urdu). change(do,no). change(X,X). /* catch all*/
Operation • ? alter([do,you,speak,english],Z). • Base case will not be satisfied • Recursive case will generate new subgoals • change(H,X) and alter(T,Y) where • H=do and T=[you,speak,english] • X will matched from the database of facts as no • alter(T,Y) will be called, base will not be satisfied again and the recursive call with T will be generated where H will be the head of the tail in the previous change call. and so on.
Trace of the Programme • Call 1 fails • Call 2 change(do,X) where X=no • ? alter([you] | [speak,english]) • Call 1 fails • Call2 change(you,X) where X=I • ? alter([speak] | [english]) • Call 1 fails • Call2 change(X,X) where X=speak • ? alter([english] | []) • Call 1 fails • Call2 change(english,X) where X=urdu • ? alter([] | []) • Call 1 succeeds