KU NLP Artificial Intelligence1 Ch 14. An Introduction to Prolog q an Implementation of logic as a programming language(PROgramming in LOGig) q many interesting.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Prolog programming....Dr.Yasser Nada. Chapter 8 Parsing in Prolog Taif University Fall 2010 Dr. Yasser Ahmed nada prolog programming....Dr.Yasser Nada.
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Problem Solving Well-formed predicate calculus expressions provide a means of describing objects and relations in a problem domain and inference rule.
CSC411Artificial Intelligence 1 Chapter 3 Structures and Strategies For Space State Search Contents Graph Theory Strategies for Space State Search Using.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
1 CS 385 Fall 2006 Chapter 15 Introduction to PROLOG.
Structures and Strategies For Space State Search
CSC 423 ARTIFICIAL INTELLIGENCE
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Using Rules Chapter 6. 2 Logic Programming (Definite) logic program: A  B 1, B 2, …, B m program clause (Horn) head body  A 1, A 2, …, A n goal clause.
Chapter 12 - Logic Programming
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
AI - Programming Prolog Lisp AI - Programming Support for Symbolic Computational Exploratory Programming.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 Chapter 3 Knowledge Representation AI & ESChapter 3 2 Knowledge Representation Semantic Network Network Representation Conceptual Graph Frame.
Building Control Algorithms For State Space Search
Prolog A Production System Example.
Understanding Natural Language
1 Pertemuan 8 Representing Knowledge Using Rules Matakuliah: T0264/Inteligensia Semu Tahun: 2005 Versi: 1.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Problem.
Structures and Strategies For Space State Search
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Backtracking.
1 Control and Implementation of State Space Search 5 5.0Introduction 5.1Recursion-Based Search 5.2Pattern-directed Search 5.3Production Systems 5.4The.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
CS 321 Programming Languages and Compilers Prolog part 2.
KU NLP Structures and Strategies for State Space Search Depth-First and Breadth-First Search q A search algorithm must determine the order in which.
1 Human-Computer Interaction Web Interface & Natural Language.
Knowledge and search, page 1 CSI 4106, Winter 2005 Knowledge and search Points Properties of knowledge  Completeness  Objectivity  Certainty  Formalizability.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Artificial Intelligence Programming in Prolog Lecture 1: An Introduction 23/09/04.
Logical Agents Logic Propositional Logic Summary
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CS 415 – A.I. Slide Set 5. Chapter 3 Structures and Strategies for State Space Search – Predicate Calculus: provides a means of describing objects and.
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Control Algorithms 2 Chapter 6 Control Algorithms 2 Chapter 6 Production Systems.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Prolog Cut to Control Search Abstract Data Types Practical Examples Summary.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Basic Problem Solving Search strategy  Problem can be solved by searching for a solution. An attempt is to transform initial state of a problem into some.
Programming Languages Third Edition Chapter 4 Logic Programming.
STRUCTURES AND STRATEGIES FOR STATE SPACE SEARCH 3 3.0Introduction 3.1Graph Theory 3.2Strategies for State Space Search 3.3Using the State Space to Represent.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Artificial Intelligence Midterm 고려대학교 컴퓨터학과 자연어처리 연구실 임 해 창.
NATURAL LANGUAGE PROCESSING
1 Pertemuan 8 Representing Knowledge Using Rules Matakuliah: T0264/Inteligensia Semu Tahun: Juli 2006 Versi: 2/1.
Prolog Overview Syntax Mechanism Summary. Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Chapter 6 Representing Knowledge Using Rules Artificial Intelligence ดร. วิภาดา เวทย์ประสิทธิ์ ภาควิชาวิทยาการคอมพิวเตอร์ คณะ วิทยาศาสตร์ มหาวิทยาลัยสงขลานครินทร์
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
Section 16.5, 16.6 plus other references
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
A User study on Conversational Software
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Presentation transcript:

KU NLP Artificial Intelligence1 Ch 14. An Introduction to Prolog q an Implementation of logic as a programming language(PROgramming in LOGig) q many interesting contributions to AI problem solving  declarative semantics  built-in unification  high-powered techniques for pattern matching and search

KU NLP Artificial Intelligence Syntax for Predicate Calculus Programming q Representing Facts and Rules q Creating, Changing, and Monitoring the PROLOG Environment q Lists and Recursion in PROLOG q Recursive Search in PROLOG q The Use of Cut

KU NLP Artificial Intelligence Representing Facts and Rules(1) q English Predicate Calculus PROLOG and , or  ; only if  :- not  not q “Everyone likes Susie.”  likes(X, susie).orlikes(Everyone, susie). q The set of people that are liked by George AND Susie  likes(george, Y), likes(susie, Y). q “George likes Kate OR George likes Susie.”  likes(george, kate); likes(george, susie).

KU NLP Artificial Intelligence Representing Facts and Rules(2) q “George likes Susie if George likes Kate.”  likes(george, susie) :- likes(george, kate). q “Kate does not like Susie.”  not(likes(kate, susie)). q A Model for the database  Database - a set of specifications describing the objects and relations in a problem domain  Queries to the database are patterns in the same logical syntax as the database entries.  Prolog interpreter uses pattern-directed search to find whether these queries logically follow from the contents of the database.

KU NLP Artificial Intelligence Representing Facts and Rules(3) q Prolog exmaple likes(george, kate).likes(susie, wine). likes(george, susie).likes(kate, gin). likes(george, wine).likes(kate, susie). ?- likes(george, kate). yes ?- likes(kate, susie). yes ?- likes(george, X). X = kate ; X = susie ; X = wine ; no ?- likes(george, beer). no

KU NLP Artificial Intelligence Representing Facts and Rules(4)  Prolog interpertations  The prompts with (;) forces a backtrack on the most recent result. Continued prompts force Prolog to find all possible solutions to the query  Closed world assumption or Negations as failure  assumes that “anything is false if it is not known to be true.”  Horn clause logic  the left hand side(conclusion) of an implication must be a single positive literal.  An example of a rule friends(X, Y) :- likes(X, Z), likes(Y, Z). ?- friends(george, susie). yes  Prolog doesn’t have global variables, the scope of X, Y, and Z is limited to friends rule.  Values bound to, X, Y, Z are consistent across the entire expression.

KU NLP Artificial Intelligence Creating, Changing, and Monitoring(1) q assert - add a new predicate to the current DB.  ?- assert(likes(david, sarah)).  asserta(p) asserts the predicate P at the beginning.  assertz(p) adds p at the end. q retract(p) remove p from the DB q place the file in the DB  ?- consult(myfile)  ?- [myfile] q read(X) takes the next term from the current input stream and binds it to X. q write(X) puts X in the output stream.

KU NLP Artificial Intelligence Creating, Changing, and Monitoring(2) q see(X) opens the file X and defines the current input stream as originating in X. q tell(X) opens a file for the output stream. q seen(X) and told(X) close the files q listing(predicate_name) - all the clauses with that predicate name in the DB are returned. q trace allows the user to monitor the progress of the Prolog interpreter. q when a more selective trace is required, the goal spy is useful.

KU NLP Artificial Intelligence Lists and Recursion (1)  Recursion is the primary control mechanism for PROLOG programming  Recursion is the natural way to process the list structure  List is a data structure consisting of ordered sets of elements.  List elements are enclosed by [] and separated by commas. [tom,dick,harry,fred]  Using the vertical bar operator and unification, we can break a list into its components  [tom,dick,harry,fred][X | Y]  X=tom and Y=[dick,harry,fred]  [tom,dick,harry,fred][X,Y | Z]  X=tom, Y=dick, Z=[harry, fred]

KU NLP Artificial Intelligence Lists and Recursion (2) q Definition of member 1: member(X, [X|T]). 2: member(X, [Y|T]) :- member(X,T). ?- member(c, [a,b,c]). call 1. fail, since c  a call 2. X=c, Y=a, T=[b,c], member(c,[b,c])? call 1. fail, since c  b call 2. X=c, Y=b, T=[c], member(c,[c])? call 1. success, c=c yes (to second call 2.) yes(to first call 2.) yes

KU NLP Artificial Intelligence Lists and Recursion (3) q Anonymous variables - variables used solely for pattern-matching process  member(X,[X|_]) the content of the tail of the list is unimportant. q Writing one element of the list on each line. Writelist([]). writelist([H|T]) :- write(H), nl, writelist(T). q Writing out a list in reversed order reverse - writelist([]). reverse - writelist([H|T]) :- reverse_writelist(T), write(H), nl.

KU NLP Artificial Intelligence Recursive Search in PROLOG(1) q Knight’s tour problem  a knight can move two squares either horizontally or vertically followed by one square in an orthogonal direction as long as it does not move off the board.  3*3 chessboard with move rules move(1,6) move(3,4) move(6,7) move(8,3) move(1,8) move(3,8) move(6,1) move(8,1) move(2,7) move(4,3) move(7,6) move(9,4) move(2,9) move(4,9) move(7,2) move(9,2)  attempt to find a series of legal moves in which the knight lands on each square of the chessboard exactly once

KU NLP Artificial Intelligence Recursive Search in PROLOG(2) q Definition of Path using PROLOG path(Z,Z) path(X,Y) :- move(X,W), not(been(W)),assert(been(W)),path(W,Y).  To determine whether the desired goal state is over reached (termination condition of recursion), path(Z,Z) is needed.  been predicate is used to record previously visited states and avoid loops.

KU NLP Artificial Intelligence Recursive Search in PROLOG(3) q Depth-first graph search with backtracking algorithm of Path. 1. path (Z, Z, L). 2. path (X, Y, L) :- move(X,Z), not(member(Z,L)), path(Z,Y,[Z|L]). ?-path(1,3,[1]). path(1,3,[1]) attempts to match 1. Fail 1  3. path(1,3,[1]) matches 2. X is 1, Y is 3, L is [1] move(1,Z) matches Z as 6, not(member(6,[1])) is true, call path(6,3[6,1]) path(6,3,[6,1]) attempts to match 1. Fail 6  3. path(6,3,[6,1]) matches 2. X is 6, Y is 3, L is [6,1]. move(6,Z) matches Z as 7, not(member(7,[6,1])) is true, path(7,3,[7,6,1])

KU NLP Artificial Intelligence Recursive Search in PROLOG(4) path(7,3,[7,6,1]) attempts to match 1. Fail 7  3. path(7,3,[7,6,1]) matches 2. X is 7, Y is 3, L is [7,6,1]. move(7,Z) is Z=6, not(member(6,[7,6,1])) fails, backtrack! move(7,Z) is Z=2, not(member(2,[7,6,1])) true, path(2,3,[2,7,6,1]) path call attempts 1, fail, 2  3 path matches 2, X is 2, Y is 3, L is [2,7,6,1] move matches Z as 7, not(member(…)) fails, backtrack! move matches Z as 9, not(member(…)) fails, path(9,3,[9,2,7,6,1]) path fails 1, 9  3 path matches 2, X is 9, Y is 3, L is [9,2,7,6,1] move is Z=4, not(member(…)) true, path(4,3,[4,9,2,7,6,1]) path fails 1, 4  3 path matches 2, X is 4, Y is 3, L is [4,9,2,7,6,1] move Z=3, not(member(…)) true, path(3,3,[3,4,9,2,7,6,1]) path attempts 1, true, 3=3, yes

KU NLP Artificial Intelligence The use of Cut to Control Search in PROLOG(1)  Cut is represented by !  Side effects  when originally encountered it always succeeds.  If it is “failed back to” in the normal course of backtracking, it comes the entire goal to fail.  Example path2(X,Y):- move(X,Z), move(Z,Y). move(1,6). move(1,8). move(6,7). move(6,1). move(8,3). move(8,1). ?-path2(1,W). W=7; W=1; W=3; W=1; no path3(X,Y):-move(X,Z), !, move(Z,Y). ?-path3(1,W) W=7; W=1; no

KU NLP Artificial Intelligence The use of Cut to Control Search in PROLOG(2) q Usage of cut in programming  allows the programmer to control the shape of the search tree, when further search is not required, the tree can be explicitly pruned at that point  Controls recursion path(Z,Z,L) path(X,Z,L):- move(X,Y), not(member(Y,L)), path(Y,Z,[Y|L]),!. the addition of cut means that at most one solution is produced.

KU NLP Artificial Intelligence18 The farmer, wolf, goat and cabbage Problem (P 620)  A farmer with his wolf, goat, and cabbage come to the edge of a river they wish to cross.  There is a boat at the river’s edge, but of course, only the farmer can row.  The boat also can carry only two things (including the rower) at a time.  If the wolf is ever left alone with the goat, the wolf will eat the goat;  Similarly, if the goat is left alone with the cabbage, the goat will eat the cabbage.  Devise a sequence of crossings of the river so that all four characters arrive safely on the other side of the river.

KU NLP Artificial Intelligence19 Sample crossing for FWGC problem

KU NLP Artificial Intelligence20 State Space Representation of FWGC problem

KU NLP Artificial Intelligence21 FWGC problem in PROLOG q test:- go(state(w,w,w,w), state(e,e,e,e)).  go(Start, Goal) :- empty_stack(Empty_been_stack), stack(Start, Empty_been_stack, Been_stack) path(Start, Goal, Been_stack).  path(State, Goal, Been_stack) :- ……..  move(state(….), state(….)) :- ……..  unsafe, opp, empty_stack, stack, member_stack, reverse_print_stack, write_list, etc.

KU NLP Artificial Intelligence22 A parse Tree

KU NLP Artificial Intelligence23 A parser in PROLOG utterance(X) :- sentence(X, [ ]). sentence(Start, End) :- nounphrase(Start, Rest), verbphrase(Rest, End). nounphrase([Noun|End], End) :- noun(Noun). nounphrase([Article, Noun|End], End) :- article(Article), noun(Noun). verbphrase([Verb|End], End) :- verb(Verb). verbphrase([Verb|End], End) :- verb(Verb), nounphrase(Rest, End). article(a). article(the). noun(man). noun(dog). verb(likes). verb(bites).

KU NLP Artificial Intelligence24 ELIZA Person: Men are all alike. ELIZA: In what way? Person: They’re always bugging us about something or other. ELIZA: Can you think of a specific example? Person: Well, my boyfriend made me come here. ELIZA: Your boyfriend made you come here. Person: He says I’m depressed much of the time. ELIZA: I am sorry to hear you are depressed. Person: It’s true. I am unhappy. ELIZA: Do you think coming here will help you not to be unhappy? Person: I need some help, that much seems certain. ELIZA: What would it mean to you if you got some help? Person: Perhaps I could learn to get along with my mother. ELIZA: Tell me more about your family.  (X you Y)  (Why do remember X just now?)  (Who else in your family is Y?  (Tell me more about your family) (X me Y) (I remember Y) (My {family-member} is Y) (X {family-member} Y)

KU NLP Artificial Intelligence25 Prolog Homework q FWCG problem (exercise, p623) q Eliza (exercise) q A parser in PROLOG  Read (p 667) recursive parser  Write a grammar for parsing sentences in the own dialog  Produce a parse tree as follows:  ?- utterance([The, man, bites, the, dog]). [sentence, [nounphrase, [article, the], [noun, man]], [verbphrase, [verb, bites], [nounphrase, [article, the], [noun, dog]]]].