Prolog Lists.

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.
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).
Prolog for Linguists Symbolic Systems 139P/239P John Dowding Week 4, October 29, 2001
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.
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.
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.
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:
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
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:
29-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.
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.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
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.
Derived from csc.villanova.edu/~dmatusze/8310summer2001/index.html/prolog1.ppt1 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 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.
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 Friday No reading Prolog Handout 2. Homework.
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.
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.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
1 Lecture 16: Prolog’s Lists, Negation and Imperative Control Flow (Section 11.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed.
COSC 2P93 Prolog: Debugging
Prolog Concepts.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Accumulators in Programming
Recursion 12-Nov-18.
Chapter 11 :: Logic Languages
Writing LISP functions
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Prolog Programming (Volume 2)
Recursion 29-Dec-18.
Unification and clause order
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Programming Paradigms and Languages
Prolog III.
Prolog Lists.
Programming Techniques
Problem Set 2 Martin Kay Stanford University.
Recursion 23-Apr-19.
Prolog Concepts.
Lisp.
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Prolog III Lists 8-Jul-19.
Prolog Concepts.
Chapter 12 :: Logic Languages
Presentation transcript:

Prolog Lists

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. The head of [x, 2+2, [a, b, c]] is x The tail is [2+2, [a, b, c]]

Lists Unification can be performed on lists: [a, b, c] = [X, Y, Z] 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].

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.

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

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

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.

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 the predicate automatically fails if no clause is matched. member(_, [ ]) :- fail.

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).

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

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).

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(_).

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

“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 (meaning, force the predicate to succeed). optional_buzz_off :- buzz_off. optional_buzz_off.

The End