PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.

Slides:



Advertisements
Similar presentations
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: ?-
Advertisements

Prolog.1 Prolog. Prolog.2 Textbook and Software  Title PROLOG programming for artificial intelligence  Author Ivan Bratko  Get the software – windows.
SLD-resolution Introduction Most general unifiers SLD-resolution
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
1 Chapter 4:Constraint Logic Programs Where we learn about the only programming concept rules, and how programs execute.
1 Part 1 The Prolog Language Chapter 2 Syntax and Meaning of Prolog programs.
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
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.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
PROLOG Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
QUALITATIVE MODELLING AND REASONING Ivan Bratko Faculty of Computer and Information Science Ljubljana University 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).
2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and.
1 COMP313A Programming Languages Logic Programming (3)
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
4. PROLOG Data Objects And PROLOG Arithmetic
Declarative Programming Autumn 2014 Basic syntax and sample programs.
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax and Meaning of Prolog Programs Notes for Ch.2 of Bratko For CSCE 580.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia
(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
Circuits Chapter 23.
Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things.
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: ?-
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.
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.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida 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
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Syntax and meaning of prolog programs Part 1.
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.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
A Conjunction is a logical operator that connects two logical terms.
Tests, Backtracking, and Recursion
Prolog programming Introduction to Prolog (part2)
Artificial Intelligence CS370D
Prolog syntax + Unification
Chapter Two: Syntax and Meaning of Prolog Programs
Logic Programming Language
Chapter 2 Syntax and meaning of prolog programs
PROLOG.
Presentation transcript:

PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia

DATA OBJECTS data objects simple objects structures constants variables atoms numbers

SYNTAX FOR DATA OBJECS Type of object always recognisable from its syntactic form

THREE SYNTACIC FORMS FOR ATOMS (1) Strings of letters, digits, and “_”, starting with lowercase letter: x x15 x_15 aBC_CBa7 alpha_beta_algorithm taxi_35 peter missJones miss_Jones2

ATOMS, CTD. (2) Strings of special characters ---> << !...::. ::= []

ATOMS, CTD. (3) Strings in single quotes ‘X_35’ ‘Peter’ ‘Britney Spears’

SYNTAX FOR NUMBERS Integers Real numbers (floating point) E e-21

SYNTAX FOR VARIABLES Strings of letters, digits, and underscores, starting with uppercase letter X Results Object2B Participant_list _x35 _335 Lexical scope of variable names is one clause Underscore stands for an anonymous variable Each appearance of underscore: another anon. var.

ANONYMOUS VARIABLES visible_block( B) :- see( B, _, _). Equivalent to: visible_block( B) :- see( B, X, Y).

STRUCTURES Structures are objects that have several components For example: dates are structured objects with three components Date 17 June 2006 can be represented by term: date( 17, june, 2006) functor arguments An argument can be any object, also a structure

FUNCTORS Functor name chosen by user Syntax for functors: atoms Functor defined by name and arity

TREE REPRESENTATION OF STRUCTURES Often, structures are pictured as trees date( 17, june, 2006) date 17 june 2006

Therefore all structured objects in Prolog can be viewed as trees This is the only way of building structured objects in Prolog

SOME GEOMETRIC OBJECTS P2 = (2,3) (6,4) S T (4,2) P1=(1,1) (7,1) P1 = point( 1, 1) P2 = point( 2, 3) S = seg( P1, P2) = seg( point(1,1), point(2,3)) T = triangle( point(4,2), point(5,4), point(7,1))

LINE SEGMENT S = seg( point(1,1), point(2,3)) S = seg point point

ARITHMETIC EXPRESSIONS ARE ALSO STRUCTURES For example: (a + b) * (c - 5) Written as term with functors: *( +( a, b), -( c, 5)) * + - a b c 5

MATCHING Matching is operation on terms (structures) Given two terms, they match if: (1) They are identical, or (2) They can be made identical by properly instantiating the variables in both terms

EXAMPLE OF MATCHING Matching two dates: date( D1, M1, 2006) = date( D2, june, Y2) This causes the variables to be instantianted as: D1 = D2 M1 = june Y2 = 2006 This is the most general instantiation A less general instantiation would be: D1=D2=17,...

MOST GENERAL INSTANTIATION In Prolog, matching always results in most general instantiation This commits the variables to the least possible extent, leaving flexibility for further instantiation if required For example: ?- date( D1, M1, 2006) = date( D2, june, Y2), date( D1, M1, 2006) = date( 17, M3, Y3). D1 = 17, D2 = 17, M1 = june, M3 = june, Y2 = 2006, Y3 = 2006

MATCHING Matching succeeds or fails; if succeeds then it results in the most general instantiation To decide whether terms S and T match: (1) If S and T are constants then they match only if they are identical (2) If S is a variable then matching succeeds, S is instantiated to T; analogously if T is a variable (3) If S and T are structures then they match only if (a) they both have the same principal functor, and (b) all their corresponding arguments match

MATCHING ≈ UNIFICATION Unification known in predicate logic Unification = Matching + Occurs check What happens when we ask Prolog: ?- X = f(X). Matching succeeds, unification fails

COMPUTATION WITH MATCHING % Definition of vertical and horizontal segments vertical( seg( point( X1,Y1), point( X1, Y2))). horizontal( seg( point( X1,Y1), point( X2, Y1))). ?- vertical( seg( point( 1,1), point( 1, 3))). yes ?- vertical( seg( point( 1,1), point( 2, Y))). no ?- vertical( seg( point( 2,3), P)). P = point( 2, _173).

AN INTERESTING SEGMENT Is there a segment that is both vertical and horizontal? ?- vertical( S), horizontal( S). S = seg( point( X,Y), point(X,Y)) Note, Prolog may display this with new variables names as for example: S = seg( point( _13,_14), point( _13, _14))

DECLARATIVE MEANING Given a program P and a goal G, G is true ( i.e. logically follows from P) if and only if: (1) There is a clause C in P such that (2) there is a clause instance I of C such that (a) the head of I is identical to G, and (b) all the goals in the body of I are true An instance of a clause C is obtained by renaming each variable in C and possibly substituting the variable by some term. E.g. an instance of p(X,Y) :- q(Y,Z) is p(U,a) :- q(a,V).

DECLARATIVE vs PROCEDURAL MEANING OF PROLOG PROGRAMS Consider: P :- Q, R. Declarative readings of this: P is true if Q and R are true. From Q and R follows P. Procedural readings: To solve problem P, first solve subproblem Q and then R. To satisfy P, first satisfy Q and then R.

PROCEDURAL MEANING Specifies how Prolog answers questions Procedural meaning is an algorithm to execute a list of goals given a Prolog program: program success/failure indication goal list execute instantiation of variables

procedure execute( Program, GoalList, Success) execute = declarative meaning + procedural elements G is true ( i.e. logically follows from P) if and only if: (1) there is a clause C in P such that (2) there is a clause instance I of C such that (a) the head of I is identical to G, and (b) all the goals in the body of I are true Search program from top to bottom to find such clause Match G and head of C Execute goals in order as they appear in program

EXAMPLE QUALITATIVE CIRCUIT ANALYSIS Electric circuits made of resistors and diodes through sequential and parallel connections Circuit = par( res, seq( res, diode)) Current Voltage

RELATION CIRC circ( Circuit, Voltage, Current) Current Circuit + - Voltage

% Qualitative definition of electric circuits % Currents and voltages have qualitative values: zero, pos or neg % All circuits have two terminals % A circuit may consist of a simple element, % or it can be composed from two circuits through parallel or sequential % connection % circ( Circuit, Voltage, Current) % The behaviour of resistor: "qualitative" Ohm's law circ( res, pos, pos). % Both voltage and current positive circ( res, zero, zero). circ( res, neg, neg).

% The behaviour of diode circ( diode, zero, pos). % Open diode: zero voltage, any pos. current circ( diode, zero, zero). circ( diode, neg, zero). % Closed diode, zero current, any neg. voltage % Reversed diode circ( revdiode, zero, neg). % Open: zero voltage, any neg. current circ( revdiode, zero, zero). circ( revdiode, pos, zero).

% Sequential composition of circuits circ( seq( Circuit1, Circuit2), Volt, Curr) :- circ( Circuit1, Volt1, Curr), circ( Circuit2, Volt2, Curr), % The same current qsum( Volt1, Volt2, Volt). % Add voltages % Parallel composition of circuits circ( par( Circuit1, Circuit2), Volt, Curr) :- circ( Circuit1, Volt, Curr1), circ( Circuit2, Volt, Curr2), % The same voltage qsum( Curr1, Curr2, Curr). % Add currents

% qsum( Q1, Q2, Q3): % Q3 = Q1 + Q2, qualitative sum over domain [pos,zero,neg] qsum( pos, pos, pos). qsum( pos, zero, pos). qsum( pos, neg, pos). qsum( pos, neg, zero). qsum( pos, neg, neg). qsum( zero, pos, pos). qsum( zero, zero, zero). qsum( zero, neg, neg). qsum( neg, pos, pos). qsum( neg, pos, zero). qsum( neg, pos, neg). qsum( neg, zero, neg). qsum( neg, neg, neg).

SOME QUERIES ?- circ( par( seq( res, res), res), V, C). C = pos,V = pos ? ; C = zero,V = zero ? ; C = neg,V = neg ? ; % Construct a circuit ?- circ( C, pos, pos), circ( C, zero, zero), circ( C, neg, neg).

?- circ( C, pos,pos), circ(C, neg, zero). C = seq(res,diode) ? ; C = seq(res,seq(res,diode)) ? ; C = seq(res,seq(res,seq(res,diode))) ? ; C = seq(res,seq(res,seq(res,seq(res,diode)))) ? ; % How about some parallel compositions?