170 likes | 414 Views
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,john). Prolog Facts. Queries ?- likes(jerry,cheeze). yes ?-likes(X,john). X=mary; X=tom; no. Rules.
E N D
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). Prolog Facts Queries ?- likes(jerry,cheeze). yes ?-likes(X,john). X=mary; X=tom; no
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).
Backtracking 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. ?- concen(X,Y). X = us Y = 91.6667 ; X = china Y = 315.5 ; X = nz Y = 16 ; X = india Y = 304.136 ; No
Cut: ! Eliminates choices Always 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 ?-
Lists [ ] [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] Recursion and Lists member(X,[X| _ ]). member(X, [ _ | Y]) :- member(X, Y). ?- member(1, [3,4,5,8,1,9]). Yes
I/O put(Ch). get(Ch). get0(Ch). tab(X). nl. read(X). write(X). File I/O tell(Filename) telling(X) told see(Filename) seeing(X) seen
findall(X,Term,List). 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]
functor(Term, Functor, Arity) ?- 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 arg(N,Term,Value) ?- 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
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
A simple grammar for expressions • expr ::= term | term addopexpr • term ::= factor | factormultop term • factor ::= ‘x’ | ‘y’ | lbrexprrbr • addop ::= ‘+’ | ‘-’ • multop ::= ‘*’ | ‘/’ • lbr ::= ‘(’ • rbr ::= ‘)’
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 ?-
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].
Domain Expert(S) User Knowledge User Interface Knowledge Engineer Formalized Knowledeg Inference Engine Explanation Facility Knowledge Base Working Memory
PrologGrammer 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 }
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 :
reverse [] = [] reverse (h:t) = (reverse t) ++ [h] reverse([], []). reverse([H|T], R) :- reverse(T, RT), append(RT, [H], R).