1 Prolog III. 2 Lists [ ] is the empty list. [x, 2+2, [a, b, c]] is a list of three elements. The first element in the list is its “head”. The list with.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Prolog Nonmonotonic logic.
PROLOG 8 QUEENS PROBLEM.
Modern Programming Languages
Prolog.
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.
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).
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
For Monday Take home exam due Exam 1. For Wednesday Read chapter 10, sections 1-2 Prolog Handout 4.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
1 Prolog II. 2 The Notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
11-Jun-15 Prolog II Unification and clause order.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
Formal Models of Computation Part II The Logic Model
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 321 Programming Languages and Compilers Prolog part 2.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
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.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Derived from csc.villanova.edu/~dmatusze/8310summer2001/index.html/prolog1.ppt1 Prolog.
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.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Programming Languages Third Edition Chapter 4 Logic Programming.
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
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.
23-Feb-16 Prolog II Unification and clause order.
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.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Prolog Concepts.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog Lists.
Prolog Programming (Volume 2)
Unification and clause order
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Prolog III.
Prolog Lists.
Programming Techniques
Prolog Concepts.
Lisp.
Prolog III Lists 8-Jul-19.
Prolog Concepts.
Presentation transcript:

1 Prolog III

2 Lists [ ] is the empty list. [x, 2+2, [a, b, c]] is a list of three elements. The first element in the list is its “head”. The list with the head removed is the “tail”.

3 Lists Unification can be performed on lists: –[a, b, c] = [X, Y, Z] results in results in X = a, Y = b, Z = c –[a, b, c] = [Head | Tail] results in Head = a, Tail = [b, c] Nonempty lists can be matched against [Head|Tail]. Empty lists will not match [Head|Tail].

4 Matching Heads and Tails If [a, b, c] = [Head | Tail], then a = Head and [b, c] = Tail If [a, b, c] = [X, Y | Tail], then a = X, b = Y, and [c] = Tail If [a, b, c] = [X, Y, Z | Tail], then a = X, b = Y, c = Z, and [ ] = Tail The tail of a list is always itself a list. [X | Y, Z] isn’t legal.

5 Making Use of Unification Prolog has no functions. But you can use a parameter as an “output variable.” –first([Head | Tail], X) :- X = Head. You can use unification in parameter lists to do much of the needed work –first([X | _ ], X). –second([ _, X | _ ], X). –third([ _, _, X | _ ], X).

6 Structures and Lists The “univ” operator, =.., can be used to convert between structures and lists: –loves(chuck, X) =.. [loves, chuck, X] Double quotes indicate a list of ASCII values: –“abc” = [97, 98, 99] –This isn’t usually very useful

7 Recursion Recursion is fully supported element(1, [X | _ ], X). element(N, [ _ | X], Y) :- M is N - 1, element(M, X, Y). This is the typical way to process lists: do something with the head, recur with the tail.

8 member member(X, [X | _ ]). member(X, [ _ | Y]) :- member(X, Y). As usual, base cases go first, then recursive cases. There is in general no need for a “fail” case, because that’s automatic. –member( _, [ ]) :- fail.

9 Accumulated Information If you reach a clause, you can assume that the earlier clauses of the same predicate have failed. member(X, [X | _ ]). If you fail this clause, the first element is not the one you want, so member(X, [ _ | Y] :- member(X, Y).

10 Backtracking and Beads Each Prolog call is like a “bead” in a string of beads: loves(chuck, X) :- female(X), rich(X). call fail exit redo loves(chuck, X)female(X)rich(X) exit redo call fail

11 Fail Loops It is possible to build a “fail loop” in Prolog print_elements(List) :- member(X, List), write(X), nl, fail. But recursion is almost always better: print_elements([Head|Tail]) :- write(Head), nl, print_elements(Tail).

12 Forcing a predicate to succeed notice_objects_at(Place) :- at(X, Place), write('There is a '), write(X), write(' here.'), nl, fail. notice_objects_at(_).

13 Forcing a predicate to fail loves(chuck, X) :- really_ugly(X), !, fail. loves(chuck, X) :- female(X), rich(X).

14 "Wrapping" another predicate The buzz_off/0 predicate might succeed or fail. This is usually what we want. But sometimes we want to ignore failure. optional_buzz_off :- buzz_off. optional_buzz_off.

15 Asserting Clauses assert(new_clause). –assert(path(garden, n, toolshed)). –assert(( loves(chuck,X) :- female(X), rich(X) )). asserta(new_clause). assertz(new_clause).

16 Removing clauses retract(clause). –retract(path(garden, n, toolshed)). –retract(path(X, Y, X)). –retract(( loves(chuck,X) :- female(X), rich(X) )). abolish(path, 3).

17 Marking Clauses as “Dynamic” Standard Prolog allows you to assert and retract clauses without any restrictions. Sicstus and some others require you to mark variable clauses as “dynamic.” :- dynamic i_am_at/1, at/2, alive/0. The “ :- ” at the beginning says “do it now.”

18 Solving problems with dynamic If Prolog already knows a clause, and it's static, it's too late to mark it dynamic Prolog must see :- dynamic functor/arity before it sees any clauses of functor/arity. –This includes clauses loaded in from an earlier consult You can restart Sicstus Prolog, or… …you can use abolish(functor, arity)

19 Arithmetic The equals sign, =, means “unify.” 2+2 does not unify with 4. To force arithmetic to be performed, use “ is ”: X is 2 + 2, X = 4. Comparisons =:= =/= > >= < <= also force their operands to be evaluated. + - * / mod, when evaluated, have their usual meanings.

20 The End