1. An Overview of Prolog.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Discrete Math Methods of proof 1.
Structures. Procedural (Imperative) Languages Procedural languages work on the basis of explicitly telling the computer ‘how to do something’; by using.
Software Development Languages and Environments. Programming languages High level languages are problem orientated contain many English words are easier.
Semantics Static semantics Dynamic semantics attribute grammars
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Experiments and Variables
PROLOG Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
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).
Logic.
Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Prolog Programming for Artificial Intelligence Three edition 2001
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
Prolog programming Introduction to Prolog (part4) CS 370 – CS461.
Prolog programming Introduction to Prolog
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
October 13, 2009Theory of Computation Lecture 11: A Universal Program III 1 Coding Programs by Numbers Gödel numbers are usually very large, even for small.
Formal Logic Mathematical Structures for Computer Science Chapter 1 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesFormal Logic.
Chapter 12 - Logic Programming
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Logic Programming Languages. Objective To introduce the concepts of logic programming and logic programming languages To introduce a brief description.
CSE115/ENGR160 Discrete Mathematics 02/07/12
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
Copyright © 2003 Bolton Institute Logical Analysis and Problem Solving, (LAPS) Programming in Prolog.
1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia
Copyright © Cengage Learning. All rights reserved.
ELEMENTARY NUMBER THEORY AND METHODS OF PROOF
DEDUCTIVE DATABASE.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Formal Models of Computation Part II The Logic Model
Evolution of Programming Languages Generations of PLs.
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,
1 Sections 1.5 & 3.1 Methods of Proof / Proof Strategy.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
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.
Slide 1 Propositional Definite Clause Logic: Syntax, Semantics and Bottom-up Proofs Jim Little UBC CS 322 – CSP October 20, 2014.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
First Order Logic Lecture 2: Sep 9. This Lecture Last time we talked about propositional logic, a logic on simple statements. This time we will talk about.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications Lecture#6: Relational calculus.
1 Introduction to Abstract Mathematics Chapter 2: The Logic of Quantified Statements. Predicate Calculus Instructor: Hayk Melikya 2.3.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1 Use graphs and not pure logic Variables represented by nodes and dependencies by edges. Common in our language: “threads of thoughts”, “lines of reasoning”,
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Artificial Intelligence CS370D
For Friday No reading Prolog Handout 2. Homework.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Logic Programming: Prolog Notes for CSCE 190 Based on Bratko,
Overview of the theory of computation Episode 3 0 Turing machines The traditional concepts of computability, decidability and recursive enumerability.
General Analysis Procedure and Calculator Policy Calculator Policy.
Foundations of Discrete Mathematics Chapter 1 By Dr. Dalia M. Gil, Ph.D.
#1 Make sense of problems and persevere in solving them How would you describe the problem in your own words? How would you describe what you are trying.
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
1 Artificial Intelligence CS370D Prolog programming Introduction to Prolog.
Logic.
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)
Logic Programming Languages
Prolog programming Introduction to Prolog
Prolog programming Introduction to Prolog (part3)
Applied Discrete Mathematics Week 9: Integer Properties
Prolog programming Introduction to Prolog (part4)
This Lecture Substitution model
Representations & Reasoning Systems (RRS) (2.2)
Recursion.
Presentation transcript:

1. An Overview of Prolog

Contents An example program: defining family relations Extending the example program by rules A recursive rule definition How Prolog answers questions Declarative and procedure meaning of programs

An Example Program Prolog is a programming language for symbolic, non-numeric computation. It is specially well suited for solving problems that involve objects and relations between objects. pam tom bob liz ann pat jim parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

An Example Program ?- parent(bob,pat). yes ?- parent(liz,pat). no ?-parent(tom,ben). ?- parent(X,liz). X=tom. ?- parent(bob,X). X=ann; X=pat; parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

An Example Program Who is a parent of whom? Find X and Y such that X is a parent of Y. ?- parent(X,Y). X=pam Y=bob; X=tom Y=liz; ... parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

An Example Program Who is a grandparent of Jim? (1) Who is a parent of Jim? Assume that this is some Y. (2) Who is a parent of Y? Assume that this is some X. ?- parent(Y,Jim), parent(X,Y). X=bob Y=pat X Y jim parent grandparent parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). ?- parent(X,Y),parent(Y,Jim). will produce the same result.

An Example Program Who are Tom’s grandchildren? ?- parent(tom,X),parent(X,Y). X=bob Y=ann; Y=pat parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

An Example Program Do Ann and Pat have a common parent? (1) Who is a parent, X, of Ann? (2) Is (this same) X a parent of Pat? ?- parent(X,ann), parent(X,pat). X=bob parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

An Example Program It is easy in Prolog to define a relation by stating the n-tuples of objects that satisfy the relation. The user can query the prolog system about relations defined in the program. The arguments of relations can be: concrete objects, or constants, or general objects. Questions to the system consist of one or more goals. An answer to the question can either be positive or negative. If several answers satisfy the question than Prolog will find as many of them as desired by the user.

Extending the Example Program by Rules Adding more facts female(pam). male(tom). male(bob). female(liz). female(pat). female(ann). male(jim). sex(pam, feminine). sex(tom, masculine). sex(bob, masculine). sex(liz , feminine). sex(pat , feminine). sex(ann , feminine). sex(jim, masculine).

Extending the Example Program by Rules We could define offspring in a similar way as the parent relation. However, the offspring relation can be defined much more elegantly by making use of the fact that it is the inverse of parent, and that parent has already been defined. offspring(Y,X):-parent(X,Y). For all X and Y, Y is an offspring of X if X is a parent of Y. A rule in Prolog

Extending the Example Program by Rules Difference between facts and rules: A fact is something that is always, unconditionally, true. Rules specify things that are true if some condition is satisfied.

Extending the Example Program by Rules Rules have: a condition part (the LHS of the rule) and a conclusion part (the RHS of the rule). offspring(Y,X):-parent(X,Y). head body

Extending the Example Program by Rules ?- offspring(liz,tom). Applying the rule, it becomes offspring(liz,tom):-parent(tom,liz). parent(tom,bob). parent(pam,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). Prolog tries to find out whether the condition part is true.

Extending the Example Program by Rules The mother relation can be based on the following logical statement: For all X and Y, X is the mother of Y if X is a parent of Y and X is a female. mother(X,Y):-parent(X,Y), female(X).

Extending the Example Program by Rules The grandparent relation can be immediately written in Prolog as: grandparent(X,Z):-parent(X,Y),parent(Y,Z). X Y Z parent grandparent

Extending the Example Program by Rules For any X and Y, X is a sister of Y if (1) both X and Y have the same parent, and (2) X is a female. sister(X,Y):- parent(Z,X), parent(Z,Y), female(Z). ?- sister(ann,pat). yes ?- sister(X,pat). X=ann; X=pat Z parent parent X Y sister female Pat is a sister of herself !

Extending the Example Program by Rules An improved rule for the sister relation can then be: sister(X,Y):- parent(Z,X), parent(Z,Y), female(Z), different(X,Y).

A Recursive Rule Definition X X Z parent predecessor X parent parent Y1 Y1 parent predecessor(X,Z):- parent(X,Z). predecessor predecessor parent Y2 Y2 parent X parent parent Y3 Z predecessor parent Y parent predecessor(X,Z):- parent(X,Y1), parent(Y1,Y2), parent(Y2,Z). Z Z predecessor(X,Z):- parent(X,Y1), parent(Y1,Y2), parent(Y2,Y3), parent(Y3,Z). predecessor(X,Z):- parent(X,Y), parent(Y,Z).

A Recursive Rule Definition There is, however, an elegant and correct formulation of the predecessor relation: it will be correct in the sense that it will work for predecessor at any depth. The key idea is to define the predecessor relation in terms of itself.

A Recursive Rule Definition For all X and Z, X is a predecessor of Z if there is a Y such that (1) X is a parent of Y and (2) Y is a predecessor of Z predecessor(X,Z):- parent(X,Y), predecessor(Y,Z).

A Recursive Rule Definition We have thus constructed a complete program for the predecessor relation, which consists of two rules: one for direct predecessor and one for indirect predecessor. predecessor(X,Z):- parent(X,Z). parent(X,Y), predecessor(Y,Z). ?- predecessor(pam,X). X=bob; X=ann; X=pat; X=jim

A Recursive Rule Definition The use of predecessor itself may look surprising: When defining something, can we use this same thing that has not yet been completely defined? Such definitions are, in general, called recursive definitions.

How Prolog Answers Questions A question to Prolog is always a sequence of one or more goals. To answer a question, Prolog tries to satisfy all the goals. To satisfy a goal means to demonstrate that the goal logically follows from the facts and rules in the program. If the question contains variables, Prolog also has to find what are the particular objects for which the goals are satisfied.

How Prolog Answers Questions An appropriate view of the interpretation of a Prolog program in mathematical terms is then as follows: Prolog accepts facts and rules as a set of axioms, and the user’s question as a conjectured theorem; then it tries to prove this theorem.

How Prolog Answers Questions Prolog starts with the goals and, using rules, substitutes the current goals with new goals, until new goals happen to be simple facts. predecessor(tom,pat) by rule pr1 by rule pr1 parent(tom,pat) parent(tom,Y) predecessor(Y,pat) no Y=bob by fact parent(tom,bob) parent(tom,Y) predecessor(Y,pat) by rule pr1 parent(tom,Y) predecessor(Y,pat)

Declarative and Procedural Meaning The declarative meaning is concerned only with the relation defined by the program. The declarative meaning thus determines what will be the output of the program. The procedural meaning determines how this output is obtained; i.e., how are the relations actually evaluated by the Prolog system.

Declarative and Procedural Meaning The ability of Prolog to work out many procedural details on its own is considered to be one of its specific advantages. It encourages the programmer to consider the declarative meaning of program relatively independently of their procedural meaning. This is of practical importance because the declarative aspects of programs are usually easier to understand than the procedural details.