Top Banner
Summary
21

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

Dec 14, 2015

Download

Documents

Wesley Garrison
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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).

Summary

Page 2: 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).

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

Page 3: 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).

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

Page 4: 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).

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.

Page 5: 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).

Cut: !

Eliminates choicesAlways succeeds but stops backtracking

a:-b,c,!,d.a:-e,f.

max(X,Y,Y) :- Y>X.max(X,Y,X). ?- max(1,2,X).X = 2 ;X = 1 ;No?-

max(X,Y,Y) :- Y>X, !. max(X,Y,X). ?- max(1,2,X).X = 2 ;No?-

Page 6: 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).

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

Page 7: 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).

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

Page 8: 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).

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

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

?- 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)

Page 10: 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).

GamesRobot control Natural language processingExpert systemsImage processingParsing of context-free languagesCompiler writingVLSI Design Relational database applicationsOther AI applications

Applications

Page 11: 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).

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

Page 12: 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).

expr --> term. expr --> term, addop, expr. term --> factor. term --> factor, multop, term. factor -->[x]. factor -->[y]. factor --> lbr, expr, rbr. addop -->['+']. addop -->['-']. multop -->['*']. multop -->['/']. lbr -->['(']. rbr -->[')'].

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

Page 13: 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).

A Grammar for a very small fragment of English

sentence --> noun_phrase, verb_phrase.

noun_phrase --> determiner, noun. noun_phrase --> proper_noun.

determiner -->[the]. determiner -->[a].

proper_noun -->[pedro].

noun -->[man]. noun -->[apple].

verb_phrase --> verb, noun_phrase. verb_phrase --> verb.

verb -->[eats]. verb -->[sings].

Applications

Page 14: 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).

InferenceEngine

Knowledge Base

Working Memory

ExplanationFacility

User

User Interface

Domain Expert(S)

Knowledge Engineer

Knowledge

FormalizedKnowledeg

Applications

Page 15: 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).

Horn ClausesDefinition: 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 => 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.

Page 16: 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).

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.

Page 17: 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).

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.

Page 18: 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).

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

Page 19: 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).

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 :

Page 20: 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).

 

Haskell Prolog

     

Kinds of objects functions relations

Arity (number of parameters) fixed; if fewer arguments, then return a function variable; indicated with /n ending

Variables start with lower case start with upper case

Values start with upper case start with lower case

Clause parameters separated by spaces separated 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)

Page 21: 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).

reverse [] = []

reverse (h:t) = (reverse t) ++ [h]

reverse([], []).

reverse([H|T], R) :-

reverse(T, RT), append(RT, [H], R).