Introduction to Prolog Facts, Questions & Rules Atoms & Variables.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
1. An Overview of Prolog.
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
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).
Prolog Programming for Artificial Intelligence Three edition 2001
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Chapter 12 - Logic Programming
Introduction to Prolog Proof Procedures. Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad,
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
TCP1211-Logic Programming Formal Syntax and Semantics of Logic Programs Faculty of Information Technology Multimedia University.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
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.
Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things.
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Flow of Control Chapter 5. Flow of Control n What order computer uses to get answers –sub-goal ordering –clause ordering n Prolog flow-of-control –sequence.
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.
Getting Started with Visual Prolog
Artificial Intelligence Programming in Prolog Lecture 1: An Introduction 23/09/04.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
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.
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.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
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.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
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.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Python Let’s get started!.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Knowledge Based Information System
Artificial Intelligence CS370D
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
JavaScript: Conditionals contd.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog programming Introduction to Prolog
Prolog programming Introduction to Prolog (part2)
Topics Introduction to Functions Defining and Calling a Function
Programming Languages 2nd edition Tucker and Noonan
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
PROLOG.
Presentation transcript:

Introduction to Prolog Facts, Questions & Rules Atoms & Variables

LISP/Prolog Data n LISP/Prolog developed for AI –LISP in late 1950s –Prolog in mid-late 1970s n Artificial Intelligence = intelligent artifact n Ability to perceive, reason and act –LISP/Prolog concerned mostly with reasoning –Symbolic manipulation of (model of) world

Symbolic Programming n Most languages work with numbers & text –Also more complicated combinations of same n Prolog/LISP work with symbols –Also work with numbers & text –Not as suitable for numeric/textual programs n Symbols represent things in the world –Language makes it easy to use them

Prolog n PROgrammation en LOGique –PROgramming in LOGic n Logic model –Program = axioms –Execution gives theorems n Restricted logic – can’t say as much

Logic Model n Proof (vs. Command) n Process –predicate specificationassertions –predicate applicationquestions n Data –mathematical objectsatoms, lists & terms

Prolog Facts n Simply state what’s true –Need to decide on symbols to state axioms n Family facts parent(mark, alex).% Mark is Alex’s parent parent(di, alex).% Di is Alex’s parent parent(bob, mark).% Bob is Mark’s parent % sign starts a comment Comment continues to end of line

Prolog Atoms n mark, di, alex & bob are not variables n Atoms are themselves and nothing else –an atom does not have a value –only one atom with any given name (no local scope, only global) n Named atoms start with lower-case letter –may contain letters (UPPER and lower case), numbers & underscores

Syntax of Prolog Facts n Consist of functor…parent –AKA name of the predicate n …and argumentsmark, alex n Arguments in (parentheses)… –Right up against the functor n …separated, by, commas n Ends with a period. –Can be split over multiple lines, if you like

Syntax of Prolog Facts parent( mark, alex ). parent( mark, alex ).% functor parent( mark, alex ).% parentheses parent( mark, alex ).% 1 st argument parent( mark, alex ).% comma parent( mark, alex ).% 2 nd argument parent( mark, alex ).% period

Representations n Parenthood a relationship with two people –parent represents the relationship –mark and alex represent the people –Position distinguishes parent from child –Note: all start with lowercase letters n Arity of parenthood relationship is 2 –The predicate is parent/2 –May be other predicates with same name

% parents(Child, Father, Mother) parents(mark, bob, isabel). % Mark’s dad & mom are Bob And Isabel % parent(Parent, Child) parent(mark, alex).% Mark is Alex’s parent Multiple Representations n Choose based on how info to be used % father(Child, Parent) father(alex, mark).% Alex’s father is Mark

Exercise n Write a set of facts about your family using the second representation above. –your parents’ family if you don’t have one n My family: parents(mark, bob, isabel). parents(di, ralph, esther). parents(alex, mark, di). parents(zachary, mark, di).

Program File n A program may consist of just facts –family_1.pl n Want to start Prolog, assert the facts, and then start asking questions –We want to consult the file –Windows – double-click file to start Prolog and file is consulted automatically –Linux – need to use consult/1 predicate

Some Predicates to Start With ?- help.% general help ?- help(help).% help on … ?- apropos(help).% help available... ?- consult(‘family_1.pl’).% load a file ?- [‘family_1.pl’].% ditto ?- edit(‘family_1.pl’).% edit/reload file ?- [user].% enter facts interactively ?- halt.% exit Prolog

Asking Yes/No Questions n Can ask about parentage n ?- is Prolog prompts for a question –type in (what looks like) a fact –Prolog will tell you if it’s in the database ?- parent(mark, alex). yes ?- parent(bob, di). no

Show Prolog’s Response… parent(mark, alex). parent(di, alex). parent(bob, mark). parent(isabel, mark). parent(ralph, di). parent(esther, di). parent(franklin, bob). ?- parent(mark, alex). ?- parent(ralph, esther). ?- parent(alex, mark). ?- parent(ralph, di).

Asking Who Questions n Can also ask who is parent of whom ?- parent(Who, mark). Who = bob yes n Prolog pauses after saying who Who is –Press Enter or space to signal you’re ready –(systems differ in what keys to press)

Asking Whom Questions n Can also ask who is parent of whom ?- parent(bob, Whom). Whom = mark yes ?- parent(Who, Whom). Who = mark Whom = alex yes

Variables n Words that start with Capital Letters are variables –More accurately “unknowns” n If you ask a question with variables, Prolog says what values they could have to make what you wrote true –If there’s no such, Prolog just says no

Show Prolog’s Response… parent(mark, alex). parent(di, alex). parent(bob, mark). parent(isabel, mark). parent(ralph, di). parent(esther, di). parent(franklin, bob). ?- parent(Who, bob). ?- parent(ralph, Whom). ?- parent(alex, Whom).

Variables & No n “Closed world assumption” –if it’s not in the database, it isn’t true –everything relevant to the question is known ?- parent(garvie, franklin). no n “no” means “don’t know of any” ?- parent(Who, isabel). no

Variable Names n Prolog doesn’t care about the names ?- parent(bob, X). X = mark yes ?- parent(bob, Child). Child = mark yes ?- parent(alex, Anyone). no use meaningful variable names X usually not good

Multiple Answers n Sometimes more than one correct answer ?- parent(Who, alex). Who = mark ; Who = di ; no n Type a semi-colon to get next answer –n and r also work –(different systems use different characters) Semi-colons here mean – “are there more answers?” This “no” means there were no more answers

Asking Anybody Questions n Start the variable with an underscore –Prolog won’t tell you its value ?- parent(mark, _Anybody). yes ?- parent(Parent, _Anyone). Parent = mark ; Parent = di yes Why did it say “yes” here instead of “no”? And why didn’t it find Bob? Is mark anyone’s parent (don’t care who, just whether)

Show Prolog’s Response… parent(mark, alex). parent(di, alex). parent(bob, mark). parent(isabel, mark). parent(ralph, di). parent(esther, di). parent(franklin, bob). ?- parent(Who, di). (show all answers) ?- parent(_Who, alex). ?- parent(alex, _Whom).

Compound Questions n Separate terms with commas –Still just one question –All things must be true, so comma = AND ?- parent(GP, P), parent(P, C). GP = bob P = mark C = alex yes Important Note: -- P appears twice in the question -- only gets one value -- same value in both places

Single Assignment n Each variable can only have one value in one answer ?- parent(GP, P), parent(P, C). n Find GP, P and C such that –GP is a parent of P, and –P is a parent of C n Variable can only have different values in different answers

Exercise n Answer these questions based on the database on pp. 1 & 2 of the text ?- parent(pam, Whom). ?- parent(tom, Whom).% give all answers ?- parent(bob, Child), parent(Child, _Any). ?- parent(Ann, jim).% careful! ?- parent(pam, Whom), parent(Who, liz). ?- parent(tom, _Any), parent(_Any, GChild).

Rules n That compound question is one we might want to ask often –Who is the grandparent of whom n New predicate: grandparent/2 –First argument is grandparent of second grandparent(GP, C) :- parent(GP, P), parent(P, C). –This is a rule, and we can put it in the file

Using Rules n Just ask the question ?- grandparent(ralph, alex). yes ?- grandparent(GP, C). GP = bob C = alex yes n Note – parent not mentioned in the answer

Understanding Rules n Consists of head and body –Head and body separated by colon-hyphen n Head is just like a fact –Tho’ it usually has variables as arguments n Body is like a question –It’s usually compound (separated by commas) n Variables connect arguments together

Rules in More Detail grandparent(GP, C) :- parent(GP, P), parent(P, C). n Note the way it’s written –Indentation –Punctuation n GP is a grandparent of C if there is some P such that –GP is a parent of P, and –P is a parent of C

Rule Diagrams n May help you see n Define grandparent/2 predicate –GP = grandparent –C = child n True if there’s a P “between” them –GP parent of P –P parent of C C GP P parent grandparent

Clauses n Facts and rules are called clauses n A predicate can be made up of any number of clauses –All facts, all rules, or some of each –Usually kept together, but need not be n Looking at a predicate: ?- listing(PredicateName). ?- listing(PredicateName/Arity).

Multi-Clause Rules n Two ways to be an uncle uncle(Uncle, NieceOrNephew) :- parent(Parent, NieceOrNephew), brother(Uncle, Parent). uncle(Uncle, NieceOrNephew) :- parent(Parent, NieceOrNephew), sister(Aunt, Parent), husband(Uncle, Aunt). Still need to define brother/2, sister/2 & husband/2.

Uncle Example parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad, cathy). ?- uncle(Who, alex). Who = brian ; Who = brad ; no

Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad, cathy). n Who are Alex’s aunts? n Write a definition of aunt/2 –aunt(Aunt, NorN).

Recursion n Predicates may be recursive –Needs a base case – fact or non-recursive rule ancestor(Anc, Desc) :- parent(Anc, Desc). ancestor(Anc, Desc) :- parent(Parent, Desc), ancestor(Anc, Parent). n Usually write base case first – more later Base case: your parent is your ancestor Recursive case: an ancestor of your parent is your ancestor

Rule Diagrams n Each diagram a clause n Note recursion –Needs base case D A parent ancestor D A P parent ancestor

Alex’s Ancestors parent(mark, alex). parent(bob, mark). parent(franklin, bob). parent(garvie, franklin). ancestor(A, D) :- parent(A, D). ancestor(A, D) :- parent(P, D), ancestor(A, P). ?- ancestor(Who, alex). Who = mark ; Who = bob ; Who = franklin ; Who = garvie ; no

Next Time n Terms & Proof Procedures n Bratko, Chapter 2