UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.

Slides:



Advertisements
Similar presentations
More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
Advertisements

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.
SLD-resolution Introduction Most general unifiers SLD-resolution
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Cryptography and Network Security
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 580 Artificial Intelligence Ch.5 [P]: Propositions and Inference Sections.
Programming Languages Third Edition
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge: Lists and Difference Lists Notes for Ch.3 of Poole.
Discrete Mathematics Lecture 4 Harper Langston New York University.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 580 Artificial Intelligence Ch.12 [P]: Individuals and Relations Proofs.
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 CSCE 330 Programming Language Structures Haskell Fall 2005 Marco Valtorta
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering A Hole in Goal Trees Notes for: D.W. Loveland and M. Stickel. “A Hole in Goal.
Discrete Structures Chapter 2 Part B Mathematical Induction
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge Notes for Ch.3 of Poole et al. CSCE 580 Marco Valtorta.
© 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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operations on Data Structures Notes for Ch.9 of Bratko For CSCE 580 Sp03 Marco.
1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Relations Chapter 9.
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
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
LING 388: Language and Computers Sandiway Fong Lecture 4.
CS 321 Programming Languages and Compilers Prolog part 2.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
Declarative Programming Arithmetic in PROLOG Autumn 2014.
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Naïve Set Theory. Basic Definitions Naïve set theory is the non-axiomatic treatment of set theory. In the axiomatic treatment, which we will only allude.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Programming Languages Third Edition Chapter 4 Logic Programming.
Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)
Artificial Intelligence
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
1 Artificial Intelligence CS370D Prolog programming List operations.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Logic Programming: Prolog Notes for CSCE 190 Based on Bratko,
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
Tests, Backtracking, and Recursion
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Representation of Lists Prolog provides a convenient representation of lists [ ann,tennis,tom,skiing] –ann is the head of the list above –[ tennis,tom,skiing] is its tail The head of a list is its first element The tail of a list is the list without its first element

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists as Terms The empty list: [ ] (a constant) Any other list is a structure whose principal functor is the list constructor, which is represented by the dot (.) in Prolog Any non-empty list matches.( Head,Tail) [ ann, tennis, tom, skiing] is a “cosmetic improvement” (“syntactic sugar”) for.( ann,.( tennis,.(tom,.(skiing, [ ])))) See Fig. 3.1 for a tree representation of this term

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More List Notation Prolog converts the internal representation of a list to its “neater” form: ?- L =.( ann,.( tennis,.( tom,.(skiing, []) ) ) ). L = [ann, tennis, tom, skiing] 3 ?- L =.(.( ann, []),.(.( tom, []), [])). L = [[ann], [tom]].. ann [ ].. tom[ ]

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More List Notation L = [a | Tail] [ a,b,c] = [ a | [ b,c]] = [ a,b | [c]] = [ a,b,c | [ ]]

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Some Operations on Lists Membership: checking whether some object is an element of a list Concatenation of two lists, obtaining a third list, which is the juxtaposition of the first two Adding an element to a list Deleting an element from a list Checking whether a list is a sublist of another Generating permutations of a list There are built-in versions of some of these predicates in SWI: see section 4.29 (p.114) pr3_1.pl

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering pr3_1.pl Note the non-determinism and reversibility of many of these operations member is built-in; we call ours memb Note that member can be used to generate lists Alternate member: memb1( X,L) :- conc( L1,[ X | L2], L).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operator Notation 2*a + b*c Infix notation is a convenience for external presentation The principal functor is (one of the) highest precedence operator(s) Fig.3.8 is a table of predefined operators; it is a subset of the predefined operators in SWI (Section E.4, p.255)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operator Precedence Xfx vs. xfy vs. yfx (a – b) – c or a – (b – c)? Since – is defined as yfx, a – (b – c) Is not not p correct? Yes, because not is defined as fy

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Arithmetic Try: ?- X = Surprise! Try ?- X is is is not reversible! Notation: -Number is +Expr Moreover, Expr must have a value and Number should be unbound. Do not use is to test for equality See Section 4.26 (p.109) and Appendix E3 of manual

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Equality = for unification: tries to match terms by finding a unifier =:= for equality: forces arithmetic evaluation, but no substitution is made Try ?- 1+2 = 2+1 (fails) ?- 1+2 =:= 2+1 (succeeds) ?- 1+A = B+2 (succeeds) ?- 1+A =:= B+2 (fails) 5 ?- 1+A =:= B+2. ERROR: Arguments are not sufficiently instantiated

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Euclid’s Algorithm euclid.pl Another non-reversible program 2 ?- gcd( 10, 55, D). D = 5 3 ?- gcd( 10, Y, 5). ERROR: Arguments are not sufficiently instantiated

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Just for Fun… Correctness gcd( X,X,X). Obvious Clause 2. Induction. Assume X < Y. –case 1: Y = nX. Obvious –case 2: Y = nX + r. Any divisor D of Y and X is also a divisor of r. Since r < X, the last subgoal in the body of the clause is (eventually) gcd(X,r,D). We only need to show that, if D is a GCD of X and r, it is also a GCD of X and Y. Any divisor of X and r is also a divisor of r. So, the set of divisors of Y and X is the same as the set of divisors of X and r. So, the Greatest CD of X and Y is the same as the GCD of X and r. But r < X, so the last subgoal in the body of the clause is (eventually) set up with X, r, D. Clause 3. Induction. Assume Y < X. Analogous to clause 2 case. See Knuth, vol.1, Section 1.1.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Length A case study in evaluated vs. non-evaluated terms: length.pl length is built-in. It is reversible. Warning: dirty secrets of SWI-Prolog ahead! length(List, Length) :- $length(List, Length), !.% written in C length(List, Length) :- var(Length), length2(List, Length). length2([], 0). length2([_|List], N) :- length2(List, M), succ(M, N).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Length II len1 is interpreted. It is reversible, but it will loop infinitely if asked for multiple lists of some length! len2 fixes this (at the cost of using a cut) len3 returns a not interpreted term len4 does the same with a shorter second clause len5 uses len4 and then interprets the result len6 is to len5 what len2 is to len1

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Axiomatizing Arithmetic Unary (“tally”) arithmetic: Giuseppe Peano’s axioms, also known as “Peano’s Postulates.” See Russell and Norvig, Section 8.3 for an informal presentation of the axioms. See and.ac.uk/~history/Mathematicians/Peano.html for a biographical sketch of Giuseppe Peano) and.ac.uk/~history/Mathematicians/Peano.html Binary arithmetic: UseDefK7.ppt –Note: examples are in Cilog, a dialect of Prolog, based on an exercise in Poole et al.’s AI Textbook

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Peano’s Postulates (P1) 0 is a natural number (P2) If x is natural number, there is another natural number denoted by x’ (and called the successor of x) (P3) 0 != x’ for any natural number x (P4) If x’ = y’, then x = y (P5) If Q is a property that may or may not hold of the natural numbers, and if (I) 0 has the property Q, and (II) whenever a natural number x has the property Q, then x’ has the property Q, then all numbers have the property Q (Principle of Induction) (Mendelson,E. Introduction to Mathematical Logic. Princeton, NJ: Van Nostrand, 1965.)