Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
Part 1 The Prolog Language Chapter 3 Lists, Operators, Arithmetic
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
Chapter 11 :: Logic Languages
1. An Overview of Prolog.
1 Part 1 The Prolog Language Chapter 2 Syntax and Meaning of Prolog programs.
Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Chapter Three: Lists, Operators, Arithmetic CS 461.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
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).
1 COMP313A Programming Languages Logic Programming (3)
Prolog Programming for Artificial Intelligence Three edition 2001
Introduction to Prolog Language Presented by San Myint.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
© 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 Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
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 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
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.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Formal Models of Computation Part II The Logic Model
2440: 211 Interactive Web Programming Expressions & Operators.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
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.
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Artificial Intelligence CS370D
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Chapter 2 Syntax and meaning of prolog programs Part 1.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
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.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
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),
Section 16.5, 16.6 plus other references
Chapter 2 - Introduction to C Programming
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Chapter 2 - Introduction to C Programming
Tests, Backtracking, and Recursion
Prolog programming Introduction to Prolog (part2)
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter Two: Syntax and Meaning of Prolog Programs
Programming Techniques
Chapter 2 Syntax and meaning of prolog programs
Presentation transcript:

Introduction to PROLOG ME 409 Lab - 1

Introduction to PROLOG

Data Objects

Atoms Strings of letters, digits, underscore character starting with lower case letter: sarah_jones, x25, x__y, x_yAB String of special characters:, ===>, …. Strings of characters enclosed in single quotes: ‘India’, ‘Tom’

Data Objects Numbers Include integers and real numbers 1, 3131, , 3.14 Variables String of letters, digits and underscore characters that starts either with an upper-case letter or with an underscore: Y, Child, _a23, Student_List

Data Objects Structures Objects that have many components Components can themselves be structures Functor is used to combine components into single structure date(1, jan, 2007), date(Date, Month, 2007)‏ date(31, cat, -4.3), segment(point(1,1),point(3,3))‏ Functors are recognized by: Name Number of arguments (Arity)‏

Data Objects Predicate A predicate consists of a head and a number of arguments Is a function which returns true/false For example: father(sam, pat). %sam is father of pat

Clauses Facts Declare things that are unconditionally true Have a head and empty body Examples: parent( tom, bob). % tom is parent of bob likes(joe,dogs). % joe likes dogs

Clauses Rules Declare things (predicates) that are true depending on a given condition Have a non-empty body Example: mother(X,Y):-parent(X,Y), female(X). X is mother of Y if X is parent of Y and X is female

Clauses Recursive rules Example Rule 1: predecessor(X,Z):- parent (X,Z). Rule 2: predecessor(X,Z):- parent(X,Y),predecessor(Y,Z)‏

Clauses Queries Means of asking the program what things are true Have empty head Example: ? parent(tom, bob). Is tom parent of bob ? Example: ? parent( tom, X). Find X such that tom is parent of X.

Matching Two terms (eg. predicate instance) will match if: They are identical, or The variables in the terms should be instantiated such that after substitution of variables by these objects the terms become identical Example: ?date(D,M,2007)=date(10,jan,Y)‏ D=10 M=jan Y=2007

Example Program % Facts: parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). % Rules: predecessor(X,Z) :- parent(X,Z). % R1 predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z). % R2

Example Program And-Or Graph

Example Program Queries and Output ?predecessor(tom,pat). Yes. ?predecessor(bob,X). X=ann; X=pat; X=jim; No. “ ; “ is used to generate other (different) solutions

How PROLOG works ? predecessor(tom,pat).

PROLOG execution summary At each stage there is a list of goals to be satisfied. PROLOG selects the leftmost subgoal and searches for a rule/fact head which matches it. The matching process can bind variables. The first subgoal is then replaced by the body of the matching rule/fact, yielding a new list of subgoals. If there are no (more) matching rule/fact (subgoal fails!)‏ or if the user asks for the next solution, PROLOG backtracks.

Program Execution Summary Unless all instantiation fails for a given rule, next rule is not explored. System remembers where call rests in AND/OR graph and what instantiations are there for each term. The most recent choice of rule/fact is undone, including any variable bindings which were made when the head was matched. PROLOG searches for an alternative matching rule/fact.

Declarative Meaning A goal G is true if and only if there is a clause C in the program such that C matches G, or There is a clause instance I of C such that the head of I is identical to G, and all the goals in the body of I are true P:-Q,R. % P is true if Q and R are ture

Procedural Meaning Specifies how Prolog answer questions Procedural meaning of a “call”: First match all arguments arg i with corresponding patterns pat i. Then perform calls cond 1,..., cond m in that order. goal pred(arg 1,..., arg n ) gives rise to subgoals cond 1,..., cond m to be satisfied from left to right.

Procedural meaning big(bear). % clause 1 big(elephant). % clause 2 small(cat). % clause 3 brown( bear). % clause 4 black( cat). % clause 5 gray( elephant). % clause 6 dark( Z) :- black( Z) % clause 7: anything black is dark dark( Z) :- brown( Z) % clause 8: anything brown is dark

Procedural Meaning ?- dark( X), big( X). Execution trace: (1) Initial goal list: dark( X), big( X). (2) Scan the program from top to bottom looking for a clause whose head matches the first goal dark( X). Clause 7 is found, which is dark( Z) :- black( Z). Replace the first goal by the instantiated body of clause 7, giving a new goal list: black( X), big( X). (3) Scan the program to find a match with black( X). Clause 5 is found: black (cat). This clause has no body, so the goal list properly instantiated, shrinks to: big( cat)‏

Procedural Meaning (4) Scan the program for the goal big( cat). No clause is found. Therefore it backtracks to step(3) and undo the instantiation X = cat. Now the goal list is again: black (X), big( X)‏ Continue scanning the program below clause 5. No clause is found. Therefore backtrack to step(2) and continue scanning below clause 7. Clause 8 is found: dark (Z) :- brown( Z) Replace the first goal in the goal list by brown (X), giving: brown( X), big( X)‏ (5) Scan the program to match brown( X), finding brown (bear). This clause has no body, so the goal list shrinks to: big( bear)‏ (6) Scan the program to find clause with big( bear). This is given as fact in clause 1, and it has no body so the list shrinks to empty. This indicates successful termination, and the corresponding variable instantiation is: X = bear.

List List is a sequence of any number of items Example : [mech, elec, civil, aero, cse, john, [1,2], [], [X]] List consists of two parts i.e L = [Head|Tail] : Head – First item of the list Tail – Remaining part of the list (tail itself is a list)‏ Empty list is also a list ( [ ] )‏ Lists are handled in Prolog as trees List1=[a, b, c] [a, b, c] = [a | [b,c] ] = [a, b | [c] ] = [a, b, c| [ ] ]

Membership member(X,L) : X is an object that occurs in L X is a member of list L if either: X is the head of L. or X is a member of the tail of L. member( X, [X| Tail] ). member( X, [Head| Tail] ) :- member( X, Tail ). Example: ?- member( a, [a, b, c]). Yes. ?- member( [b, c], [a, [b, c]]). Yes. ?- member(b, [a, [b, c]]). No.

Concatenation conc(L1,L2,L3) : L3 is the concatenation of L1 & L2 If the first argument is empty list then second and third arguments must be the same list. conc([ ], L, L). If the first argument is non-empty list then it can be represented as [X|L1]. Then the result of concatenation will be [X|L3] where L3 is the concatenation of L1 and L2. conc([X|L1], L2, [X|L3]) :- conc(L1, L2, L3).

Concatenation Examples: ?- conc( [a,[b,c],d], [a, [ ],b], L). L = [a, [b,c], d, a, [ ], b] ?- conc( Before, [feb| After], [ jan, feb, mar]). Before = [ jan ] After = [ mar ] Note: Refer to rule - conc([X|L1], L2, [X|L3]) :- conc(L1, L2, L3).

Deleting an item del(X,L,L1) : L1 is equal to L with the first occurrence of item X removed. If X is the head of the list then the result after deletion is the tail of the list. del( X, [X| Tail], Tail). If X is in the tail then it is deleted from there. del(X, [Y| Tail], [Y| Tail1]) :- del(X, Tail, Tail1)‏ Example: ?- del(a, [a, b, a, c], L). L = [b, a, c]; L = [a, b, c]; no

Inserting an item insert(X,L,L1) : L1 is any list such that deleting X from L1 gives L. insert( X, List, BiggerList) :- del( X, BiggerList, List). Example: ?- insert( a, [ b, c, d], L). L = [a, b, c, d]; L = [b, a, c, d]; L = [b, c, a, d]; L = [b, c, d, a]; no

Adding an item add(X,L,L1) : L1 is the list obtained by adding X to L. X is added in front of the list L. The resulting list L1 is simply [X|L]. add(X,L,[X|L]). Example: ? add(4.2, [1, a, f, [2] ], L1). L1 = [4.2, 1, a, f, [2] ]

Sublist S is a sublist of L if: L can be decomposed into two lists, L1 and L2, & L2 can be decomposed into two lists, S and some L3 L L2 L1SL3

Sublist sublist(S,L):- conc(L1,L2,L), conc(S,L3,L2)‏ Example: ? sublist([b,c],[a,b,c,d,e]). Yes ? sublist (S, [a,b,c]). S = [ ]; S = [a]; S = [a,b]; S = [a,b,c]; S = [ ]; S= [b]; …

Operators Some of the operators are in-built in Prolog. Some predefined arithmetic operators: + addition - subtraction * multiplication /division mod modulo, the remainder of integer division Precedence of operator decides the correct interpretation of expressions. Operator with highest precedence is the principle functor of the term. Operators are functors. 3+4 = 3+4 ( 3+4 ≠ 7)‏

Operators New operators can be defined by inserting into special kind of clauses called directives. Example: :-op(600,xfx, )‏ Three group of operators: infix: xfx, xfy, yfx prefix: fx, fy postfix: xf, yf These definition helps in unambiguous interpretation of expressions which have sequence of binary operators.

Operators x represents an argument whose precedence must be strictly lower than that of operator y represents an argument whose precedence is lower or equal to that of operator Precedence of an argument in parentheses or unstructured object is zero Precedence of a structured argument is equal to the precedence of its principal functor Example: :-op(500,yfx,- ) a – b – c Interpreted as (a-b)-c and not as a-(b-c)‏

Arithmetic ‘=‘ is matching operator (it does not evaluate on its own)‏ Matches right hand side with left hand side Examples: 1.? X=1+2 X=1+2 2.? 1+3=2+2 No 3. ?X+3=4+Y X=4 Y=3

Arithmetic ‘is’ operator forces evaluation of expression on RHS forcing instantiation of values on LHS to the evaluated value Examples: 1. X is 1+2 X=3 1. Y is 5 mod 2 Y=1 At the time of evaluation all arguments must be instantiated to numbers

Arithmetic Values of arithmetic expressions can be compared by arithmetic operators: X<Y :-X is less than Y X>Y :- X is greater than Y X >=Y :- X is greater than or equal to Y X =<Y :- X is less than or equal to Y X =:= Y :- the values of X and Y are equal X =\= Y :- the values of X and Y are not equal

Example Calculating number of elements in a list length(List,N) % N is the number of elements in List length([ ], 0). %R1 length( [Head|Tail], N):- length(Tail, N1), N is 1+N1.%R2 Query: ? length ([a,b,c,d],N). N = 4