F28PL1 Programming Languages Lecture 16: Prolog 1.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

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.
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
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
Prolog.
Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
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).
F28PL1 Programming Languages Lecture 17: Prolog 2.
Chapter 12 - Logic Programming
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CPSC 322 Introduction to Artificial Intelligence September 15, 2004.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
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.
Evolution of Programming Languages Generations of PLs.
Logic Programming Programming Lecture 1: Getting started with Prolog.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
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.
Logical and Functional Programming
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)
Logic Programming (LP) expresses code using of a restricted form of symbolic logic. LP programs are declarative, rather than algorithmic. An LP program.
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
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.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
Tests, Backtracking, and Recursion
3.5 Programming paradigms
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 12 :: Logic Languages
Logic Programming Language
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
Presentation transcript:

F28PL1 Programming Languages Lecture 16: Prolog 1

Overview logic programming language roots in predicate logic developed by Alan Colmerauer & collaborators in Marseilles, in early 1970s ISO standard derived from University of Edinburgh adapted for Japanese 5 th Generation programme, 1980s now widely used for Artificial Intelligence research & education

Overview based on logic programming – use of predicate logic as a specification language – an implementation of predicate logic would enable the use of specifications directly as programs concentrate on describing a problem solution as an input/output relation – not an input/output process – i.e. in a descriptive rather than a prescriptive manner

Overview enables a high degree of abstraction and of implementation independence emphasis is on what is to be done rather than how it is to be done predicate logic has a well developed proof theory – use formal techniques to manipulate/verify specifications specification can be used to : – check that outputs correspond to inputs – find outputs from inputs – find inputs from outputs

Overview not a pure logic programming language known evaluation order for predicate manipulation – implementation considerations are used by programmers many predicates can only be used for checking or for finding outputs from inputs but not both quantification must be made explicit

Overview differences with imperative languages: – no necessary distinction between programs and data – there is no concept of a statement as a state change, for example, through assignment like functional languages – evaluation order is not necessarily linear

Prolog resources we will use SICStus Prolog interpreter – from the Swedish Institute of Computer Science – licenses cost real money – don’t buy one! SICStus documentation from: – ml ml free Prologs from: – – W. F. Clocksin & C. S. Mellish, Programming in Prolog: Using the ISO Standard, (5 th edition), Springer, 2003

Running Prolog to run Prolog on Linux: $ sicstus SICStus (x86_64-linux-glibc2.7): Wed Feb 1 01:15:06 CET 2012 Licensed to SP4macs.hw.ac.uk | ?- | ?- - Prolog prompt system commands are Prolog terms – end with a.

Running Prolog system does not support interactive editing – use separate windows for program & interpreter to load a program | ?- [file name]. file name is any valid Linux path if not a single word then in ‘...’ file name convention – Prolog files end with.pl

Running Prolog to turn on tracing: | ? – trace. to turn off tracing: | ? – notrace. to leave Prolog | ?- ^D

Prolog summary weak types – i.e. can change type associated with variable dynamic typing – i.e. types checked at run time ad-hoc polymorphism – variable can be bound to different types as program runs non-linear evaluation – programs may backtrack to unbind and rebind variables

Memory model database – holds asserted facts and rules – searched and manipulated to answer questions – may change arbitrarily during program stack – variable bindings – information about current position(s) in database heap – space allocated to data structures

Programs Prolog program consists of series of clauses specifying: – facts – rules – questions load program from file system will: – remember facts & rules in database – attempt to satisfy questions using facts & rules in database

Terms clauses made up of terms atom – words or symbols sequence of lower case letters, digits & _ s – starting with a lower case letter sequence of characters in ‘...’ e.g. size top fish_finger ‘one hundred'

Terms integer e.g variable – sequence of letters, digits and _ s – starting with an upper case letter or a _ e.g. Cost X_11 _Property

Terms structure – constructs for program and data structures functor ( arguments ) functor – atom arguments – one or more terms separated by, s e.g. wet(water) cost(milk,95) recursive definition – nested structures as arguments e.g. parents(mark,mother(margaret), father(dennis))

Terms infix structures term atom term atom usually a symbol used for infix operations e.g. 7*8 X=99 NB these are structures not expressions – * and = are symbols

Facts a fact is a structure e.g. fly(pigs) e.g. ordered(1,3,2) NB facts have no necessary meanings

Questions 1 suppose l16.pl holds: wet(water). cost(milk,95). | ?- [‘l16.pl’]. … yes facts now in database

Question matching question is a structure if no variables in question then system: – looks for a database clause with the same functor and arguments as the question – displays yes or no

Question matching is wet(water) a fact? | ?- wet(water). yes try wet(water) – water matches water

Question matching does milk cost 85 ? | ?- cost(milk,85). no try cost(milk,95) – milk matches milk – 85 doesn’t match 95

Questions with variables if variables in question then system: – looks for a clause in the database with: same functor as question atoms in same argument positions as in question – instantiates variables in question to terms in same positions in assertion – displays question variable instantiations use this form to search database for values in clauses satisfying query

Questions with variables for what X is wet true? | ?- wet(X). X = water ? – press Return yes match wet(X) try wet(water) – X instantiated to water

Questions with variables what X has cost 95 ? | ?- cost(X,95). X = milk ? – press Return yes try cost(milk,95) – cost matches cost – 95 matches 95 – X instantiated to milk

Questions with variables what X has cost Y ? | ?- cost(X,Y). X = milk Y = 95 ? – press Return yes try cost(milk,95) – cost matches cost – X instantiated to milk – Y instantiated to 95

Multiple facts can have multiple facts with same functor and different arguments e.g. wet(water). wet(milk). wet(orange_juice).

Multiple facts multiple facts with the same functor are logical disjunctions functor (argument 1 ). functor (argument 2 )....  functor (argument 1 ) or functor (argument 2 ) or...

Backtracking when system offers solution 1.press Return – accept solution – system displays yes 2.enter no – reject solution – system will backtrack – uninstantiate any variables in question – try to find another clause matching question

Backtracking | -? wet(X) match wet(X) try wet(water) – X instantiated to water X = water ? no uninstantiate X from water try wet(milk) – X instantiated to milk X = milk ?

Backtracking X = milk ? no uninstantiate X from milk try wet(orange_juice) – X instantiated to orange_juice X = orange_juice ? no uninstantiate X from orange_juice no more matches no

Terms and variables all occurrences of a variable in a term are the same instance whenever one occurrence of a variable is bound to a value – all occurrences now reference that value e.g. same(X,X). – both X ’s are the same variable

Matching variables when a question term with variables matches a database term with variables – variables in the same position share | ?- same(fish,Y). match same(X,X) try same(fish,Y) – X instantiated to fish – X shares with Y Y = fish ?

Rules rules are superficially similar to methods or functions – fundamental differences... a rule has the form: head :- body means: 1.the head is true if the body is true 2.the body implies the head head – term, usually an atom or structure body – term, often a conjunction of terms separated by, i.e., behaves like logical and

Variables in rules all occurrences of a variable in a term are the same instance so occurrences of variables in the head are the same instance as occurrences in the body whenever an occurrence of a variable in the body is bound to a value – all other occurrences reference that value – including occurrences in the head use this to get results back from body of rule to head

Rules suppose we have the facts: mother(betty,ann). mother(delia,betty). X is Y ’s parent if they are Y ’s mother parent(X,Y) :- mother(X,Y). X ’s are same variable Y ’s are same variable

Rule matching to match a rule, try to match the body to match the body, try all body options in turn if matching the body fails: – backtrack, undoing any variable instantiations – try the next rule option

Rule matching | ?- parent(delia,P). try parent(X,Y) :- mother(X,Y) X instantiated to delia Y and P share – match mother(delia,Y) – try mother(betty,ann) – delia does not match betty

Rule matching – backtrack – match mother(delia,Y) – try mother(delia,betty) – delia matches delia – bind Y to betty P shares with Y so: P = betty ?

Multiple rules multiple rules with the same functor are like logical disjunctions functor (arguments 1 ) :- body 1. functor (arguments 2 ) :- body  functor (arguments 1 ) :- body 1 or functor (arguments 2 ) :- body 2 or...

Rules suppose we have the facts: mother(betty,ann). mother(delia,betty). father(chris,ann). father(eric,betty). X is Y ’s parent if they are Y ’s mother or Y ’s father parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y).

Rules | ?- parent(P,Q). try parent(X,Y) :- mother(X,Y) – P shares with X – Q shares with Y – match mother(X,Y) – try mother(betty,ann) X instantiated to betty (shares with P ) Y instantiated t o ann (shares with Y ) P = betty Q = ann? no

Rules – backtrack – match mother(X,Y) – try mother(delia,betty) X insantiated to delia (shared with P ) Y instantiated to betty (shared with Q ) P = delia Q = betty ? no – backtrack

Rules try parent(X,Y) :- father(X,Y) – P shares with X – Q shares with Y – match father(X,Y) – try father(chris,ann) X instantiated to chris (shared with P ) Y instantiated t o ann (shared with Q ) P = chris Q = ann? no...

Rules if the body is a conjunction: functor(arguments) :- term 1,term 2... body is equivalent to: term 1 and term 2 and... to match conjunctive body, match each term i in turn if matching term i fails then backtrack to term i-1 and try again NB system must remember how far each term i has progressed NB term i will also involve nested terms for nested rules

Rule example consider the train from Dundee to Aberdeen: Dundee->Arbroath->Montrose->Stonehaven->Aberdeen next(dundee,arbroath). next(arbroath,montrose). next(montrose,stonehaven). next(stonehaven,aberdeen). X is before Y if X is next to Y or X is next to W and W is before Y before(X,Y) :- next(X,Y). before(X,Y) :- before(X,W),next(W,Y).

Rule example | ?- before(arbroath,aberdeen). yes try before(arbroath,aberdeen) :- next(arbroath,aberdeen) – try next(arbroath,aberdeen) – fail & backtrack try before(arbroath,aberdeen) :- before(arbroath,W),next(W,aberdeen) – try before(arbroath,W),next(W,aberdeen) try before(arbroath,W) :- next(arbroath,W) – next(arbroath,W) – matches next(arbroath,montrose) before(arbroath,W) succeeds with W instantiated to montrose try next(montrose,aberdeen) fail & backtrack

Rule example try before(arbroath,W) :- before(arbroath,W’),next(W’,W) - where W’ is a new variable – try before(arbroath,W’) :- next(arbroath,W’) try next(arbroath,W’) matches next(arbroath,montrose) – before(arbroath,W’) succeeds with W’ instantiated to montrose – try next(montrose,W) matches next(montrose,stonehaven) before(arbroath,W) succeeds with W instantiated to stonehaven try next(stonehaven,aberdeen) – matches next(stonehaven,aberdeen) before(arbroath,aberdeen) succeeds