1 / 43

AI - Programming

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

Download Presentation

AI - Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AI - Programming Prolog Lisp AI - Programming Support for Symbolic Computational Exploratory Programming

  2. 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

  3. Symbol Structures • Are represented using “List” data structure • Element of the “List” may be a • Symbol • Another “List” • Example: (friends jim (joe mary anne))

  4. Symbol Manipulation(Involve Pattern Matching) • Example • likes (kate, susie) • likes(X, susie) • By pattern matching X = kate (from above)

  5. Two Requirements For AI-Prog. Language Are 1. Support flexible list-based data structures 2. Pattern matching

  6. 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

  7. 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

  8. 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

  9. 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)

  10. 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

  11. Rules: • Consist of Implication statement • Examples: • bird(x): - animal(x), has-feathers(x) (only if)

  12. 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

  13. 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

  14. Database • Needs to be very comprehensive • Any mistake could take you far from correct results

  15. Connectives English Practical ProLog and ^ , or ; only if :- not ¬ not ^

  16. 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.

  17. 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

  18. ? lectures(X, Y) x = aliya y = ai; x = john y = databases; no

  19. Suppose we have a rule: bird(x):- animal(x), has-feather(x). ? - bird(B) B = sparrow

  20. 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

  21. 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.

  22. 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).

  23. Steps In Creating ProLog Programmes (Cont’d) Trace: To monitor the progress of the prolog interpreter. Spy: Selective trace. Spy[member/2, append/3].

  24. 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.

  25. [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]

  26. 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

  27. 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 cb 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)

  28. 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 cb Call2 x=c, y=b,t=[c], member(c,[c])? Call1. Success, c = c yes(to second call2) yes (to first call 2)

  29. 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.

  30. 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).

  31. Prolog Matching (Cont’d) ?- X + Y = 1 + 2 X = 1Y = 2 ?- lectures(X, ai) = lectures (ahsan, Y).X = alisonY = ai

  32. 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.

  33. 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”.

  34. Recursion Base case ancestor (Person, Ancestor):- parent(Person, Ancestor). ancestor(Person, Ancestor):- parent(Person, Parent), ancestor(Parent, Ancestor). R1 Recursive case R2

  35. Recursion Examples parent(alison, david). - F1 parent(alison, kathleen). - F2 parent(david, harold). - F3 parent(david, ida). - F4 parent(kathleen, john). - F5

  36. ? - 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.

  37. Another Example • Develop a programme in PROLOG that reverses the following sentences • Example: • Do you speak English • Required output is : No I know Urdu

  38. 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

  39. 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.

  40. 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*/

  41. 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.

  42. 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

More Related