Artificial intelligence Prolog Language


Published on

Artificial intelligence -> Prolog Language

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Artificial intelligence Prolog Language

  1. 2. <ul><li>The first, official version of prolog was developed at the university of Marseilles, France by Alain Colmerauer in the early 1970s as a tool for programming in logic. </li></ul><ul><li>Today, prolog is an important tool in programming artificial intelligence applications and in the development of customized knowledge bases, expert systems, natural language interfaces, and smart information management systems. </li></ul><ul><li>Visual prolog addresses the same target market as SQL database systems, C++ development systems and other language tools like visual basic, Borland's Delphi, or IBM's visual age. </li></ul>
  2. 3. <ul><li>Facts are like relations in Database systems, representing the known data. </li></ul><ul><ul><li>Bill likes Cindy. </li></ul></ul><ul><ul><li>Cindy likes Bill. </li></ul></ul><ul><ul><li>Bill likes dogs. </li></ul></ul><ul><li>In Prolog </li></ul><ul><ul><li>likes(bill, cindy). </li></ul></ul><ul><ul><li>likes(cindy, bill). </li></ul></ul><ul><ul><li>likes(bill, dogs). </li></ul></ul>
  3. 4. <ul><li>Natural Language </li></ul><ul><ul><li>Cindy likes everything that Bill likes. </li></ul></ul><ul><ul><li>Caitlin likes everything that is green. </li></ul></ul><ul><li>Prolog </li></ul><ul><ul><li>likes(cindy, Something):- likes(bill, Something). </li></ul></ul><ul><ul><li>likes(caitlin, Something):- green(Something). </li></ul></ul><ul><li>We call “likes” for a predicate </li></ul><ul><li>The predicate has two clauses . </li></ul><ul><li>A clause has a head and a body . </li></ul><ul><li>A predicate has a name and an arity </li></ul><ul><li>Like has two arguments ( arity=2 ) </li></ul>Read as IF
  4. 5. <ul><li>Variables start with an uppercase letter or underscore. </li></ul><ul><li>VALID Variables </li></ul><ul><ul><li>My_first_correct_variable_name </li></ul></ul><ul><ul><li>_My first quarter </li></ul></ul><ul><ul><li>Sales_10_11_86 </li></ul></ul><ul><li>INVALID Variables </li></ul><ul><ul><li>1stattempt </li></ul></ul><ul><ul><li>second_attempt </li></ul></ul><ul><ul><li>&quot;disaster&quot; </li></ul></ul>
  5. 6. Variables in Prolog get their values by being unified with values. Until it gets a value, a variable is said to be free ; when it gets a value, it becomes bound . When backtracking; variables becomes free again. Variables are used as part of the pattern-matching, process, not as a kind of information storage.
  6. 7. <ul><li>PREDICATES </li></ul><ul><ul><li>nondeterm likes(symbol,symbol) </li></ul></ul><ul><li>CLAUSES </li></ul><ul><ul><li>likes(ellen,reading). </li></ul></ul><ul><ul><li>likes(john,computers). </li></ul></ul><ul><ul><li>likes(john,badminton). </li></ul></ul><ul><ul><li>likes(leonard,badminton). </li></ul></ul><ul><ul><li>likes(eric,swimming). </li></ul></ul><ul><ul><li>likes(eric,reading). </li></ul></ul><ul><li>Consider this query: Is there a person who likes both reading and swimming? </li></ul><ul><ul><li>likes(Person, reading), likes(Person, swimming). </li></ul></ul><ul><li>The solutions are found through Searching, Matching/Unification and Backtracking </li></ul>
  7. 8. <ul><li>Basic types: Integer, Real, char, Strings, Symbols </li></ul><ul><li>List’s [1,2,3,4] </li></ul><ul><li>Compound structures: person(“leo”,39) font(“arial”,b,21) </li></ul><ul><li>Special types like binary (an array of byte values) </li></ul><ul><ul><li>In natural language, we ask you: </li></ul></ul><ul><ul><ul><li>Does Bill like Cindy? </li></ul></ul></ul><ul><ul><li>In Prolog syntax, we ask Prolog: </li></ul></ul><ul><ul><ul><li>likes(bill, cindy). </li></ul></ul></ul><ul><ul><li>Given this query, Prolog would answer </li></ul></ul><ul><ul><ul><li>yes </li></ul></ul></ul>
  8. 9. <ul><li>Predicates and their arguments MUST be declared. They are declared in the PREDICATES sections. </li></ul><ul><li>Compound domains and sub domains can be declared in the DOMAINS sections. </li></ul><ul><li>Clauses for the predicates are given in the CLAUSES section. </li></ul><ul><li>Dynamic facts can be given in the FACTS sections (Former name was DATABASE). </li></ul><ul><li>Execution starts in the GOAL Section. </li></ul>
  9. 10. <ul><li>We could ask you in natural language: </li></ul><ul><ul><li>What does Bill like? </li></ul></ul><ul><li>In Prolog syntax, we ask Prolog: </li></ul><ul><ul><li>likes(bill, What). </li></ul></ul><ul><li>Prolog will return </li></ul><ul><ul><li>What=cindy </li></ul></ul><ul><ul><li>What=dogs </li></ul></ul><ul><li>2 Solutions </li></ul>
  10. 11. <ul><ul><li>/* This is an example of a comment */ </li></ul></ul><ul><ul><li>% This is also a comment </li></ul></ul><ul><ul><li>/***************************************/ </li></ul></ul><ul><ul><li>/* and so are these three lines */ </li></ul></ul><ul><ul><li>/***************************************/ </li></ul></ul><ul><ul><li>/*You can also nest a comment /*within a comment*/ like this */ </li></ul></ul><ul><ul><li>CONSTANTS </li></ul></ul><ul><ul><ul><li>restricted = 1 </li></ul></ul></ul><ul><ul><li>ifdef restricted </li></ul></ul><ul><ul><li>savebase(_):- </li></ul></ul><ul><ul><ul><li>write(&quot;nBase cannot be saved in demo version&quot;), </li></ul></ul></ul><ul><ul><ul><li>readchar(_). </li></ul></ul></ul><ul><ul><li>elsedef </li></ul></ul><ul><ul><li>savebase(Name):- </li></ul></ul><ul><ul><ul><li>write(&quot;nSaving &quot;,Name), </li></ul></ul></ul><ul><ul><ul><li>save(Name). </li></ul></ul></ul><ul><ul><li>enddef </li></ul></ul>
  11. 12. domains mydom = i(integer); s(string) predicates procedure wr(mydom) - (i) clauses wr(i(Int)):- write(&quot;Was integer: &quot;,Int). wr(s(Str)) :- write(&quot;Was String: &quot;,Str). GOAL wr(i(88)). Passing input to the predicate The variable will be bound to 88 Procedure means no fail! Only input flow accepted! Case/switch statement!
  12. 13. Factorial of 1 is 1 Factorial of N is N multiplied with factorial of N-1 <ul><li>PREDICATES </li></ul><ul><ul><li>factorial(integer,integer) </li></ul></ul><ul><li>CLAUSES </li></ul><ul><ul><li>factorial(1,1):-!. </li></ul></ul><ul><ul><li>factorial(X,FactX):- </li></ul></ul><ul><ul><ul><li>Y=X-1, </li></ul></ul></ul><ul><ul><ul><li>factorial(Y,FactY), </li></ul></ul></ul><ul><ul><ul><li>FactX = X*FactY. </li></ul></ul></ul>
  13. 14. Erroneous - Always exit failure - Always fail procedure - Always success determ - succeed or fails multi - 1 or more solutions (never fail) nondeterm - 0 or more solutions predicates append(list,list,list)- procedure (i,i,o), determ (i,i,i) nondeterm (o,i,i)
  14. 15. write(Param1, Param2, Param3, ..., ParamN) readln(Line) readchar(CharParam) file_str(Filename, Text) - file_str(&quot;t.dat&quot;, My_text) openread(SymbolicFileName, OSFileName) openwrite(SymbolicFileName, OSFileName) closefile(SymbolicFileName)
  15. 16. <ul><ul><li>Result = cast(returndomain,Expr) </li></ul></ul><ul><li>LongVal = cast(LONG,”This was a text”) </li></ul>save(fileName) /* (i) */ save(fileName, databaseName) /* (i, i) */
  16. 17. Stack: the stack is used for transferring arguments and return addresses for predicate calls. The stack also holds the information for backtrackpoints. Heap: the heap holds all objects that are more or less permanent, such as database facts, window buffers, file buffers etc. Gstack: the global stack, normally called gstack, is the place where lists, compound structures and strings are placed. The Gstack is only released during backtracking. Trail: the trail is only used when the program uses reference variables. It holds information about which reference variables must be unbound during backtracking. The trail is allocated in the heap.
  17. 18. <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li>www.g prolog .org/manual/g prolog .html </li></ul><ul><li> prolog . predicates .html </li></ul><ul><li> prolog /page_9.html </li></ul>