Artificial Intelligence: Natural Language and Prolog

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

Applications Computational LogicLecture 11 Michael Genesereth Spring 2004.
1 Knowledge Representation Introduction KR and Logic.
Artificial Intelligence: Knowledge Representation
Copyright © 2003 Pearson Education, Inc. Slide 1.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Formal Models of Computation Part II The Logic Model
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
SQL: The Query Language Part 2
Programming Language Concepts
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 2
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Some Prolog Prolog is a logic programming language
ABC Technology Project
Semantic Analysis and Symbol Tables
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
How to convert a left linear grammar to a right linear grammar
Squares and Square Root WALK. Solve each problem REVIEW:
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
Less Than Matching Orgad Keller.
Introduction to Recursion and Recursive Algorithms
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Chapter 11 :: Logic Languages
Prolog.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
LING 364: Introduction to Formal Semantics Lecture 4 January 24th.
Copyright © 2003 Bolton Institute Logical Analysis and Problem Solving, (LAPS) Programming in Prolog.
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
LING 388: Language and Computers Sandiway Fong Lecture 7.
CS 321 Programming Languages and Compilers Prolog part 2.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
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.
Context Free Grammars Reading: Chap 9, Jurafsky & Martin This slide set was adapted from J. Martin, U. Colorado Instructor: Rada Mihalcea.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
Artificial Intelligence: Natural Language
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
Knowledge Based Information System
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.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Presentation transcript:

Artificial Intelligence: Natural Language and Prolog More Prolog Prolog and NL Grammars

Prolog and Lists In Prolog you can use the following data structures: An atom (e.g., “john”) A functor-argument structure (e.g., “mother_of(john)”) A list of items. A list can be a list of atoms, functor argument structures, or a list of lists. Lists look like this: “[john, mary, sue]”

Prolog and Lists All these are lists: [1,2,3] [john, sue] [mary] [1, 2, mary, mother_of(john)] An empty list looks like this: [] All these are Prolog facts with lists: loves(mary, [john, joe,fred]). jugs_contain([3, 2]).

Lists and Matching Which do you think match? What bindings? [X, Y] = [1, 2] [X, 1] = [4, 1] [X,Y,Z] = [1, 2] [X] = [1, 2] A special pattern [X|Y] matches list with first item X and remainder Y (var names unimportant) [X|Y]=[1, 2, 3] X = 1, Y = [2, 3] [X|Y=[1] X = 1, Y = []

Arithmetic and Operators Prolog has the usual range of Arithmetic Operators (+, -, =, * ..) But unless you force it to, it won’t evalulate arithmetic expressions. They will simply be prolog structures to be matched. ?- 1+1 = 2. No ?- 1+1 = 1+X. X = 1 data(tweety, [legs=2, feathers=yes]). ?- data(tweety, Data), member(legs=X, Data). X = 2 (Note how we can call several goals from Prolog prompt)

Arithmetic We can force Prolog to evaluate things using the special operator “is”. ?- X is 1 + 1. X = 2. ?- Y=1, X is Y + 1. X is 2. We could therefore write a limbs-counting predicate: no_limbs(animal, N) :- no_arms(animal, Narms), no_legs(animal, Nlegs), N is Narms + Nlegs.

Negation What if you want to say that something is true if something is can NOT be proved. Logically we would have:  X  tall(X)  short(X) In prolog we use the symbol \+ and have rules such as: short(X) :- \+ tall(X). Some prologs allow the word “not”: short(X) :- not tall(X).

Other bits of Prolog Commenting In prolog can enter comments in two ways: % anything following a percent, on that line /* anything between these delimiters */ It is conventional to have: comments at start of file (author,purpose, date etc). Comment before each predicate. E.g., % member(Item, List): checks whether Item % is a member of List We also sometimes refer to e.g., member/2 to mean the member predicate with two args.

Disjunctions In prolog you CAN have disjunctions in body of rule, signaled by “;”. a(X) :- b(X) ; c(X). But this is equivalent to having two rules. a(X) :- b(X). a(X) :- c(X). Logical equivalence can be demonstrated. X (b(X)  c(X))  a(X) equiv to (X b(X)  a(X))  (X c(X)  a(X)) (But we won’t bother). Normally we use two rules.

Recursion Common in Prolog to use recursive rules, where rule “calls” itself. (term in head occurs in body). ancestor(Person, Someone) :- parent(Person, Someone). parent(Person, Parent), ancestor(Parent, Someone). Base case Recursive case Someone is your ancestor if they are your parent. Someone is your ancestor if Parent is your parent, and Parent has Someone as their ancestor.

Execution of recursive rules. Consider: parent(joe,jim). parent(jim, fred). Goal: ancestor(joe, fred). Execution: Goal matches head of first rule. Tries to prove parent(joe, fred). FAILS. Backtracks and tries second rule. Tries to prove: parent(joe, Parent). Succeeds with Parent=jim. Tries to prove: ancestor(jim, fred). Tries to prove parent(jim, fred). SUCCEEDS.

Other Examples of Recursive Rules connected(X, Y) :- touches(X, Y). connected(X, Z) :- touches(X, Y), connected(Y, Z). inheritsFrom(X, Y) :- subclass(X, Y). inheritsFrom(X, Y) :- subclass(X, Z), inheritsFrom(Z, Y).

Left recursion and infinite loops Problems can occur if you write recursive rules where the head of the rule is repeated on the LEFT hand side of the rule body, e.g., above(X, Y) :- above(X, Z), above(Z, Y). above(prolog_book, desk). above(ai_notes, prolog_book). ?- above(ai_notes, desk). Yes ?- above(desk, ai_notes). (doesn’t terminate) Left recursive call

Input and Output Prolog has various input and output predicates. write/1 - writes out a prolog term. read/2 - reads in a prolog term. put/1 - writes one character. get/1 - reads one character. Example: sayhello :- write(‘Hello’), nl. Use with care as weird things happen on backtracking.

The CUT Serious programs often need to control the backtracking. The “!” (cut) symbol is used to do this. We will aim to avoid it, but as an example: a(A) :- b(A), !, c(A). Prevents backtracking past the cut. Commits to particular solution path.

Consulting Programs So far used Can also (equivalently) use: consult(‘filename.pl’). OR consult(filename). [‘filename.pl’]. OR [filename]. Can also (equivalently) use: (Assumes .pl if not mentioned).

Prolog’s Grammar Notation Prolog has a built in grammar notation (definite clause grammars). Prolog’s built in search mechanism enables sentences to be easily parsed. Example: sentence --> np, vp. np --> article, noun. vp --> verb. article --> [the]. noun --> [cat]. verb --> [jumps]. Note how dictionary entries (words) are entered just as another rule.

Example 2 sentence --> np, vp. np --> article, noun. vp --> verb. vp --> verb, np. article --> [the]. article --> [a]. noun --> [cat]. verb --> [jumps]. verb --> [likes].

Behind the grammar notation Prolog in fact converts these rules into “ordinary” prolog when it reads them in. a --> b, c. Converted to: a(Words, Rest) :- b(Words, Temp), c(Temp, Rest). Words and Rest are lists. So we have: a list of words can be parsed as constituent a, with Rest words left over. This is ok if the list can be parsed as a “b”, with Temp words left over, and Temp can be parsed as a “c” with Rest left over. You may see this underlying form when tracing.

Parsing You can parse a sentence using a special “phrase” predicate: ?- phrase(sentence, [the, cat, jumps]). Yes You can even use it to generate all “grammatical” sentences: ?- phrase(sentence,X). X = [the, cat, jumps] ; X = [the, cat, jumps, the cat] ;

Returning the parse tree Just knowing whether a sentence parses isn’t terribly useful. Also want to know the structure of the sentence. Simplest way for small examples is to add another argument to all rules that will contain a partly filled in structured object: sentence(s(NP, VP)) --> np(NP), vp(VP). noun(noun(cat)) --> [cat]. End up with ?- phrase(sentence(Tree), [the, cat,.. ]). Tree = s(np(art(the), noun(cat)), vp(verb(jumps)))

Summary Have briefly covered: Grammars in Prolog - Prolog’s search method enables simple parsing. Other aspects of Prolog - but many more..

Practice Exercises Work out what order solutions would be returned given the following program and query: onTop(prolog_book, desk). onTop(ai_notes, prolog_book). onTop(timetable, ai_notes). onTop(ai_book, desk). above(X, Y) :- onTop(X, Y). above(X, Y) :- onTop(X, Z), above(Z, Y). ?- above(Object, desk).

Practice Exercises What are results of following matches: [X, Y] = [1, 2]. [a(X), b(Y)] = [a(1), b(2)]. [X | Y] = [1, 2]. [X, Y | Z ] = [1, 2]. [X, 1] = [2, Y]. [a | Y] = [[Y], b].