LING 388: Language and Computers Sandiway Fong Lecture 2: 8/25.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Getting started with Prolog
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
LING 364: Introduction to Formal Semantics Lecture 24 April 13th.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
Outline Recap Knowledge Representation I Textbook: Chapters 6, 7, 9 and 10.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
LING 364: Introduction to Formal Semantics Lecture 4 January 24th.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
LING 388 Language and Computers Lecture 3 9/09/03 Sandiway FONG.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING/C SC/PYSC 438/538 Computational Linguistics
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
LING 388 Language and Computers Take-Home Final Examination 12/9/03 Sandiway FONG.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
LING 388: Language and Computers Sandiway Fong Lecture 4: 9/1.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 5: 9/5.
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
LING 388: Language and Computers Sandiway Fong Lecture 1: 8/22.
1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 13: 10/10.
LING 388: Language and Computers Sandiway Fong Lecture 8.
LING 388: Language and Computers Sandiway Fong 10/4 Lecture 12.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
Formal Models of Computation Part II The Logic Model
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 5.
LING 388: Language and Computers Sandiway Fong Lecture 4.
LING 388: Language and Computers Sandiway Fong Lecture 7.
LING 388: Language and Computers Sandiway Fong Lecture 3.
LING 388: Language and Computers Sandiway Fong Lecture 6.
LING/C SC/PSYC 438/538 Lecture 2 Sandiway Fong. Today’s Topics Did you read Chapter 1 of JM? – Short Homework 2 (submit by midnight Friday) Today is Perl.
Getting Started with Visual Prolog
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 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.
LING 388: Language and Computers Sandiway Fong Lecture 1: 8/23.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
1 Knowledge Based Systems (CM0377) Introductory lecture (Last revised 28th January 2002)
COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Knowledge Based Information System
For Friday No reading Prolog Handout 2. Homework.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
JavaScript/ App Lab Programming:
LING 581: Advanced Computational Linguistics
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
LING/C SC/PSYC 438/538 Lecture 19 Sandiway Fong.
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

LING 388: Language and Computers Sandiway Fong Lecture 2: 8/25

Administrivia LING 388 Homework #1 –handed out today –submit homework by –arrive in my mailbox by midnight Wednesday September 1st –see lecture 1 slides for general policy about write-ups –you should collect all your answers to the homework exercises into one file put your name on the homework Use Word or plain text (something I can easily read) Adobe PDF is also acceptable

From last time… Have you installed SWI-Prolog? –Latest version is

Today’s Topic first hands-on experience using SWI-Prolog –SWI-Prolog is already installed on all the lab machines –via Program menu

Today’s Topic Gentle hands-on introduction to Prolog –a powerful tool for writing grammars –grammars that can also be run on a computer

Some Background Prolog = Programming in Logic –Horn clause logic (subset of first-order predicate calculus) –roots in Mechanical Theorem Proving Resolution Rule (Robinson, 65) –invented in the early 1970s –designed to support natural language processing … has grammar rules –has been taught to schoolkids

Prolog is a Database Prolog can be used to store a list of facts –facts are things that are true in Prolog’s world –initially Prolog’s world is empty We can look up facts just like in a database –mechanism is a Prolog database query Usage –We can store facts in the database by using assert –e.g. assert(bird(robin)). adds fact bird(robin). to the Prolog world Example Facts in the database bird(robin). “robin is a bird” bird(eagle). “eagle is a bird” Database queries ?- bird(robin).Yes ?- bird(tiger).No Jargon –bird is a predicate –the predicate bird has one argument –e.g. robin and eagle are arguments of the predicate bird –bird(robin) is a fact meaning bird(robin) is true in the database –?- bird(eagle). is a query meaning we want to know if bird(eagle) is true in the database

SWI-Prolog how to start it? –from the Windows Program menu –interpreter window pops up and –is ready to accept database queries ( ?- ) how to see what’s in the Prolog database? –?- listing. Other useful commands –once we start storing facts in files instead of entering them using assert. how to see what the current working directory is? –(the working directory is where your files are stored) –important: every machine in the lab is different –?- working_directory(X,Y). –X : current working directory, Y : new working directory how to change to a new working directory? –?- working_directory(X,NEW). Note: on the Mac: use /opt/local/bin/swipl in Terminal

Exercise 1 1.Let’s start Prolog 2.Enter the bird facts into the database –Using the assert/1 query 3.Query the database 4.List the database –using query listing. Note: all facts and queries must end in a period. Prolog is case-sensitive –don’t mix upper and lower case –anything that begins with an uppercase letter is a logic variable –use lower case for symbols Facts in the database bird(robin). bird(eagle). Database queries ?- bird(robin). ?- bird(tiger). To remove a fact from the database use retract (the counterpart of assert ) e.g. ?- retract(bird(robin)). would leave just tiger

Exercise 2: Variables Database –bird(robin). –bird(eagle). Query containing a variable X –?- bird(X). –X = robin ; –X = eagle ; –No Query with a variable –?- bird(X). means –tell me if bird(X). is true in Prolog’s world. Closed World Assumption –In general, Prolog obeys the closed world assumption: things aren’t true unless explicitly stated to be true –e.g. our database might be incomplete, for example, daffy might be a bird –but unless we explicitly tell Prolog daffy is a bird, Prolog assumes it’s not. Notation: variables and symbols –X (capitalized 1st letter) is a variable –robin, tiger (beginning with a lowercase letter) are simple symbols (not variables) semicolon (;) represents disjunction (or) in the context of a Prolog query, it means “give me the next answer”

Exercise 3: Negation Prolog negation –“failure to prove” –a limited form of logical negation denoted by \+ –used in queries and bodies (not head) of rules –negative facts are not permitted, can’t put them in the database –can’t do ?- assert(\+ bird(cat)). Example ?- \+ bird(robin).No ?- \+ bird(tiger). Yes Reasoning \+ bird(robin) is true –if bird(robin) is false, i.e. can’t be proved from the database –but bird(robin) is true –so \+ bird(robin) is false \+ bird(cat) is true –if bird(cat) is false, i.e. can’t be proved from the database –bird(cat) is not in the database, so it’s false –so \+ bird(cat) is true ERROR: assert/1: No permission to modify static_procedure

Homework Question 1 Use database bird(robin). “robin is a bird” bird(eagle). “eagle is a bird” (2pts) What do the following queries return as an answer? The queries ?- \+ bird(Eagle). ?- \+ \+ bird(robin). (6pts) Give the logical reasoning for each answer –(in the form given in Exercise 3)

Exercise 4: Rules Database bird(robin). “robin is a bird” bird(eagle). “eagle is a bird” Rules (to be asserted) has_feathers(Y) :- bird(Y). “Y has feathers if Y is a bird” can_fly(X) :- has_feathers(X). “X can fly if X has feathers” Run Queries –?- has_feathers(robin).Yes if bird(robin) is true –?- can_fly(robin). Yes (can chain inferences) ?- has_feathers(robin). –?- bird(robin). »bird(robin) true Notation –:- means “if”

Homework Question 2 (2pts) In Exercise 4, we stated: –has_feathers(Y) :- bird(Y). –However, it is common knowledge that, of all the animals in the world, all birds have feathers and only birds have feathers. –What is missing from my Prolog rule? –Submit the missing rule. (4pts) Although all birds have feathers, not all birds fly, e.g. penguins and ostriches –Add these two birds to the database –Make sure bird/1 and has_feathers/1 are true for them –Modify the can_fly rule to exclude them –hint: look up conjunction (comma symbol in Prolog) –Submit the completed database

SWI-Prolog Getting stuck? –Type -C –Type a (for abort) –gets you back to the Prolog interpreter prompt ( ?- )

SWI-Prolog How to enter facts and rules into the database? –Method 2: Use your favorite text editor Create a file in the current working directory containing database facts and rules. Load the file at the Prolog prompt using: ?- consult(FILE). or ?- [FILE]. –(comma-delimited list notation) or Note: file are loaded from Prolog’s working directory see slide 7 for working directory queries Replace with actual filename

SWI-Prolog How to enter facts and rules into the database? –consult/edit/new under the File menu

SWI-Prolog A note on filenames –Convention: Prolog files normally have extension.pl e.g. mydb.pl (.pl extension is also used by Perl – which came later in 1987 but is much more popular) –FILE above should be the filename without the extension e.g. ?- [mydb]. –The period (.) is a special symbol in Prolog. If you wish to specify the full name, you must enclose it in single quotes e.g. ?- [’mydb.pl’].

Prolog Resources SWI-Prolog manual and help system Useful Online Tutorials –An introduction to Prolog (Michel Loiseleur & Nicolas Vigier) attacks.org/~boklm/prolog/ attacks.org/~boklm/prolog/ –Learn Prolog Now! (Patrick Blackburn, Johan Bos & Kristina Striegnitz) saarland.de/~kris/learn-prolog- now/lpnpage.php?pageid=onlinehttp:// saarland.de/~kris/learn-prolog- now/lpnpage.php?pageid=online