Summary. likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book)

  • Published on
    14-Dec-2015

  • View
    221

  • Download
    3

Embed Size (px)

Transcript

<ul><li>Slide 1</li></ul> <p>Summary Slide 2 likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). Queries ?- likes(jerry,cheeze). yes ?-likes(X,john). X=mary; X=tom; no Prolog: Facts Slide 3 Rules X is brother of Y if X is a male and X and Y have the same parents. In Prolog is_brother_of(X,Y):-male(X), parents(X, Father, Mother), parents(Y, Father, Mother). Slide 4 Backtracking ?- concen(X,Y). X = us Y = 91.6667 ; X = china Y = 315.5 ; X = nz Y = 16 ; X = india Y = 304.136 ; No population(us,275). population(china,1262). population(nz,4). Population(india,1000). land(us,3000). land(china,4000). land(nz,250). land(india,3288). concen(X,Y):- population(X,P), land(X,L), Y is P*1000/L. Slide 5 Cut: ! Eliminates choices Always succeeds but stops backtracking a:-b,c,!,d. a:-e,f. max(X,Y,Y) :- Y&gt;X. max(X,Y,X). ?- max(1,2,X). X = 2 ; X = 1 ; No ?- max(X,Y,Y) :- Y&gt;X, !. max(X,Y,X). ?- max(1,2,X). X = 2 ; No ?- Slide 6 [ ] [a,b,c,d,e] [5,8,3,9,7] [the, boy, run] A list can be split into a head and a tail: [H|T]. grades(john, [70,87,90,58]). ?- grades(john, [H|T]). H = 70 T = [87,90,58] member(X,[X| _ ]). member(X, [ _ | Y]) :- member(X, Y). ?- member(1, [3,4,5,8,1,9]). Yes Recursion and Lists Lists Slide 7 put(Ch). get(Ch). get0(Ch). tab(X). nl. read(X). write(X). I/O tell(Filename) telling(X) told see(Filename) seeing(X) seen File I/O Slide 8 male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). ?- male(X). X= andrew; X= john ; X= george ; X= greg ; X= adam ; ?- female(X). X= mary; X= jennifer; X= eve; ?- parents(X, adam, eve). X= greg; X= jennifer; X= andrew; ?- findall(X, male(X), List). List= [andrew, john, george, greg, adam] ?- findall(X, female(X), List). List= [mary, jennifer, eve] ?- findall(X, parents(X,adam,eve), List). List= [greg, jennifer, andrew] findall(X,Term,List). Slide 9 ?- arg(2,likes(mary,john),X). X = john Yes ?- arg(2,likes(mary,X),john). X = john Yes ?- arg(3,parents(john,george,X),Val). X = _G346 Val = _G346 Yes ?- arg(3,parents(john,george,victoria),Val). Val = victoria Yes ?- functor(likes(mary,john),Fun,Arity). Fun = likes Arity = 2 Yes ?- X=likes(mary,john),functor(X,Func,Arity). X = likes(mary, john) Func = likes Arity = 2 Yes ?- functor(parents(adam,john,mary),F,N). F = parents N = 3 Yes ?- functor(X,likes,2). X = likes(_G303, _G304) Yes functor(Term, Functor, Arity) arg(N,Term,Value) Slide 10 Games Robot control Natural language processing Expert systems Image processing Parsing of context-free languages Compiler writing VLSI Design Relational database applications Other AI applications Applications Slide 11 expr ::= term | term addop expr term ::= factor | factor multop term factor ::= x | y | lbr expr rbr addop ::= + | - multop ::= * | / lbr ::= ( rbr ::= ) A simple grammar for expressions Applications Slide 12 expr --&gt; term. expr --&gt; term, addop, expr. term --&gt; factor. term --&gt; factor, multop, term. factor --&gt; [x]. factor --&gt; [y]. factor --&gt; lbr, expr, rbr. addop --&gt; ['+']. addop --&gt; ['-']. multop --&gt; ['*']. multop --&gt; ['/']. lbr --&gt; ['(']. rbr --&gt; [')']. ?- phrase(expr, [y, '*', '(', x, '+', x, ')']) Yes ?- phrase(factor, [y]) Yes ?- phrase(rbr, [')']) Yes ?- phrase(factor, [y, '*', x]) No ?- phrase(expr, [y, '*', x]). Yes ?- phrase(factor, ['(',y, '*', x,')']). Yes ?- Applications Slide 13 A Grammar for a very small fragment of English sentence --&gt; noun_phrase, verb_phrase. noun_phrase --&gt; determiner, noun. noun_phrase --&gt; proper_noun. determiner --&gt; [the]. determiner --&gt; [a]. proper_noun --&gt; [pedro]. noun --&gt; [man]. noun --&gt; [apple]. verb_phrase --&gt; verb, noun_phrase. verb_phrase --&gt; verb. verb --&gt; [eats]. verb --&gt; [sings]. Applications Slide 14 Inference Engine Knowledge Base Working Memory Explanation Facility User User Interface Domain Expert(S) Knowledge Engineer Knowledge Formalized Knowledeg Applications Slide 15 Horn Clauses Definition: A Horn clause is a clause with at most one positive literal. A Horn clause therefore belongs to one of four categories: A rule: 1 positive literal, at least 1 negative literal. A rule has the form: ~P1 V ~P2 V... V ~Pk V Q This is logically equivalent to P1^P2^... ^Pk =&gt; Q thus, an if-then implication with any number of conditions but one conclusion. Examples: ~man(X) V mortal(X) (All men are mortal); ~parent(X,Y) V ~ancestor(Y,Z) V ancestor(X,Z) If X is parent of Y and Y is ancestor of Z then X is ancestor of Z. Slide 16 A fact or unit: 1 positive literal, 0 negative literals. Examples: man(socrates) parent(elizabeth,charles)", A negated goal : 0 positive literals, at least 1 negative literal. In virtually all implementations of Horn clause logic, the negated goal is the negation of the statement to be proved The null clause : 0 positive and 0 negative literals. Appears only as the end of a proof. Slide 17 Prolog is designed to represent Horn clauses, does backward chaining only, is a full Turing equivalent programming language, and can compute anything any other programming language can. Modern Prolog implementations have GUI facilities, fast compilers and (if your code is designed appropriately) very high run-time performance. Slide 18 Program ::= Clause... Query | Query Clause ::= Predicate. | Predicate :- PredicateList. PredicateList ::= Predicate | PredicateList, Predicate Predicate ::= Atom | Atom( TermList ) TermList ::= Term | TermList, Term Term ::= Numeral | Atom | Variable | Structure Structure ::= Atom ( TermList ) Query ::= ?- PredicateList. Numeral ::= an integer or real number Atom ::= string of characters beginning with a lowercase letter or enclosed in apostrophes. Variable ::= string of characters beginning with an uppercase letter or underscore Terminals = {Numeral, Atom, Variable, :-, ?-, comma, period, left and right parentheses } Prolog Grammer Slide 19 Comparing Prolog and Haskell Syntax In Prolog: functions are not evaluated - they are like data constructors the language is untyped variables begin with upper-case letters or an underscore predicate and function symbols begin with a lower-case letter the list constructor functor is [ | ], not : Slide 20 HaskellProlog Kinds of objects functionsrelations Arity (number of parameters) fixed; if fewer arguments, then return a functionvariable; indicated with /n ending Variables start with lower casestart with upper case Values start with upper casestart with lower case Clause parameters separated by spacesseparated by commas Head-body separator = (after possible alternatives):- (when body is needed) Alternatives preceded by | (or can use if-then-else)separated by semi-colons Clause ending off-side rule (or semi-colon)full stop List constructor : (inside parens when potentially ambiguous)| (always inside brackets) Slide 21 reverse [] = [] reverse (h:t) = (reverse t) ++ [h] reverse([], []). reverse([H|T], R) :- reverse(T, RT), append(RT, [H], R). </p>