Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
?- go( state(left,left,left,left), state(right,right,right,right) ). A solution is: PowerPoint Presentation
Download Presentation
?- go( state(left,left,left,left), state(right,right,right,right) ). A solution is:

?- go( state(left,left,left,left), state(right,right,right,right) ). A solution is:

132 Views Download Presentation
Download Presentation

?- go( state(left,left,left,left), state(right,right,right,right) ). A solution is:

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. ?- go( state(left,left,left,left), state(right,right,right,right) ). A solution is: The farmer takes the Goat from left of the river to right The farmer crosses the river from right to left The farmer takes the Wolf from left of the river to right The farmer takes the Goat from right of the river to left The farmer takes the cabbage from left of the river to right The farmer crosses the river from right to left The farmer takes the Goat from left of the river to right A solution is: The farmer takes the Goat from left of the river to right The farmer crosses the river from right to left The farmer takes the cabbage from left of the river to right The farmer takes the Goat from right of the river to left The farmer takes the Wolf from left of the river to right The farmer crosses the river from right to left The farmer takes the Goat from left of the river to right No

  2. go(Start,Target):- path(Start, Target,[Start],Path), write(‘A solution is:’),nl, write_path(Path). path(Start, Target,Visited,Path):- move(Start, NextNode),% Generate a move not( unsafe(NextNode) ),% Check that it issafe not( member(NextNode,Visited) ),% Check for recurrence path(NextNode, Target,[NextNode |Visited],Path),!. path(Target, Target,Path,Path).% Reached the goal

  3. move(state(X,X,G,C),state(Y,Y,G,C)):-opposite(X,Y). % FARMER & WOLF move(state(X,W,X,C),state(Y,W,Y,C)):-opposite(X,Y).% FARMER &GOAT move(state(X,W,G,X),state(Y,W,G,Y)):-opposite(X,Y).%FARMER & Cbg. move(state(X,W,G,C),state(Y,W,G,C)):-opposite(X,Y).% FARMER alone unsafe( state(F,X,X,_) ):- opposite(F,X),!. % The wolf eats the goat Other unsafe clauses opposite(left,right). opposite(right,left).

  4. write_path( [] ). write_path( [H1,H2|T] ) :-write_move(H1,H2), write_path([H2|T]). write_move( state(X,W,G,C), state(Y,W,G,C) ) :-!, write(‘The farmer crosses the river from ‘), write(X),write(‘ to ‘),write(Y),nl. write_move( state(X,X,G,C), state(Y,Y,G,C) ) :-!, write(‘The farmer takes the Wolf from the ‘), write(X),write(‘side of the river to ‘),write(Y),nl.

  5. ?-go(X,Y). ?-go(state(left,left,left,left),Y).

  6. Parsing NLP

  7. x+(y*x) Scanning (Lexical Analysis) Tokens ([x, +, ‘(’, y, *, x, ‘)’] ) Parsing (Syntax Analysis) Parse tree Intermediate Code Gen. (Semantics Analysis) Intermediate code Optimization Optimized code Code Generation Object Code Phases of compilation

  8. while A > = B Do Scanner [while, A, > =, B, Do] Parser Yes, this conforms to the syntactic rules Parsingis the task of showing whether or not a string of symbols conforms to the syntactic rules (grammer) of a language. If the string conforms to the rules we say that the grammar generates the string. A parseris a program which embodies the rules of a grammar.

  9. Parsing Expressions • Arithmetic Expressions • x x+y (x-y) • ((x*y)) • x+(y*x) • A simple grammar for expressions expr ::= term | term addopexpr • term ::= factor | factormultop term • factor ::= ‘x’ | ‘y’ | lbrexprrbr • addop ::= ‘+’ | ‘-’ • multop ::= ‘*’ | ‘/’ • lbr ::= ‘(’ • rbr ::= ‘)’ E -> T E -> T A E T -> F T -> F M T …

  10. It is possible with most Prolog implementations to write a grammar directly in Prolog and use a special predicate to parse strings. expr --> term. phrase(P, List)

  11. Here is the grammar that we presented earlier rewritten using Prolog grammer rules: • 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 -->[')'].

  12. The Prolog grammar rules are just a bunch of facts, like any others we might have in our database. To use them for parsing we need to give Prolog the query: phrase(P, List) ?- 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 ?- ?- phrase(expr, L). L = [x] ; L = [y] ; L = ['(', x, ')'] ; L = ['(', y, ')'] ;

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

  14. ?-  phrase(sentence, [the, man, eats]). yes ?- phrase(sentence, [the, man, eats, the, apple]). yes ?-  phrase(sentence, [the, apple, eats, a, man]). yes ?-  phrase(sentence, [pedro, sings, the, pedro]). no ?- phrase(sentence,[eats, apple, man]). no ?- phrase(sentence,L).

  15. L = [the, man, eats, the, man] ; L = [the, man, eats, the, apple] ; L = [the, man, eats, a, man] ; L = [the, man, eats, a, apple] ; L = [the, man, eats, pedro] ; L = [the, man, sings, the, man] ; L = [the, man, sings, the, apple] ; L = [the, man, sings, a, man] ; L = [the, man, sings, a, apple] ; L = [the, man, sings, pedro] ; L = [the, man, eats] ; L = [the, man, sings] ; L = [the, apple, eats, the, man] ; L = [the, apple, eats, the, apple] ; L = [the, apple, eats, a, man] ; L = [the, apple, eats, a, apple] ; L = [the, apple, eats, pedro] ; L = [the, apple, sings, the, man] ; L = [the, apple, sings, the, apple] ; L = [the, apple, sings, a, man] ;

  16. L = [the, apple, sings, a, apple] ; L = [the, apple, sings, pedro] ; L = [the, man, sings, pedro] ; L = [the, man, eats] ; L = [the, man, sings] ; L = [the, apple, eats, the, man] ; L = [the, apple, eats, the, apple] ; L = [the, apple, eats, a, man] ; L = [the, apple, eats, a, apple] ; L = [the, apple, eats, pedro] ; L = [the, apple, sings, the, man] ; L = [the, apple, sings, the, apple] ; L = [the, apple, sings, a, man] ; L = [the, apple, sings, a, apple] ; L = [the, apple, sings, pedro] ; L = [a, apple, eats, the, man] ; L = [a, apple, eats, the, apple] ; L = [a, apple, eats, a, man] ; L = [a, apple, eats, a, apple] ; L = [a, apple, eats, pedro] ; L = [a, apple, sings, the, man] ;

  17. L = [a, apple, sings, the, apple] ; L = [a, apple, sings, a, man] ; L = [a, apple, sings, a, apple] ; L = [a, apple, sings, pedro] ; L = [a, apple, eats] ; L = [a, apple, sings] ; L = [pedro, eats, the, man] ; L = [pedro, eats, the, apple] ; L = [pedro, eats, a, man] ; L = [pedro, eats, a, apple] ; L = [pedro, eats, pedro] ; L = [pedro, sings, the, man] ; L = [pedro, sings, the, apple] ; L = [pedro, sings, a, man] ; L = [pedro, sings, a, apple] ; L = [pedro, sings, pedro] ; L = [pedro, eats] ; L = [pedro, sings] ; No ?-

  18. Comparison PROLOG/Haskell

  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 :

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

  21. reverse [] = [] reverse (h:t) = (reverse t) ++ [h] reverse([], []). reverse([H|T], R) :- reverse(T, RT), append(RT, [H], R).