1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 28: Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language.

Slides:



Advertisements
Similar presentations
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Advertisements

Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Declarative Programming Lists in PROLOG Autumn 2014.
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).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Logic Programming with Prolog: Resolution, Unification, Backtracking COMP 144 Programming Language Concepts Spring 2003 Stotts, Hernandez-Campos Modified.
1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill.
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 36: Global Optimization, Loop Improvement and Profiling COMP 144 Programming Language.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 22: Object-Oriented Programming COMP 144 Programming Language Concepts Spring 2002.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
Introduction to Logic Programming with Prolog (Section 11.3)
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.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Programming Languages Third Edition Chapter 4 Logic Programming.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
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.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI.
Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Logic Programming Lecture 2: Unification and proof search.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages And other online sources (see links on schedule page) Michael L. Scott Based on Programming Language.
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.
Recursive stack-based version of Back-chaining using Propositional Logic
Prolog Concepts.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Logic Programming with Prolog: Resolution, Unification, Backtracking
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Prolog Concepts.
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Chapter 12 :: Logic Languages
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 28: Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 1 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lists ConstructorsConstructors –[] Empty list constant –. Constructor functor ExampleExample –.(a,.(b,.(c, []))) –[a, b, c] (syntactic sugar) Tail notation:Tail notation: –[a | [b, c]] –[a, b | [c]] Head::a Tail::[a]

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lists Examples No notion of input or output parameters

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Tic-Tac-Toe Example 3x3 grid3x3 grid Two Players:Two Players: –X (computer) –O (human) Fact x(n) indicates a movement by XFact x(n) indicates a movement by X –E.g. x(5), x(9) Fact o(n) indicates a movement by OFact o(n) indicates a movement by O –E.g. o(1), o(6)

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Tic-Tac-Toe Example Winning conditionWinning condition

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Tic-Tac-Toe Example Strategy: good moves Ordered List of Choices     

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Tic-Tac-Toe Example       Winning Split X

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Imperative Control Flow The cut Prolog has a number of explicit control flow featuresProlog has a number of explicit control flow features ! Known as the cut! Known as the cut –This is a zero-argument predicate that always succeeds –It commits the interpreter to the unification made between the parent goal and the left-hand side of the current rules ExampleExample member(X, [X|T]). member(X, [H|T]) :- member(X, T). member(X, [X|T]) :- !. member(X, [H|T]) :- member(X, T). member may succeed n times member may succeed at most one time If this rule succeeded, do not try to use the following ones

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Imperative Control Flow AlternativeAlternative member(X, [X|T]). member(X, [H|T]) :- not(X=H), member(X, T). How does not work?How does not work? not(P) :- call(P), !, fail. not(P). –call attempts to satisfy the goal P. –fail always fails.

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Prolog Database Manipulation Two built-in predicates can be used to modify the database of known factsTwo built-in predicates can be used to modify the database of known facts assert(P) adds a new fact.assert(P) adds a new fact. –E.g. assert(parent(kevin, john)) retract(P) removes a known fact.retract(P) removes a known fact. –E.g. retract(parent(kevin, john))

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Backward Chaining in Prolog Backward chaining follows a classic depth-first backtracking algorithmBackward chaining follows a classic depth-first backtracking algorithm ExampleExample –Goal: Snowy(C) Snowy(C)

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Infinite Regression Goal

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment ReadRead –Rest of Scott Sect Guide to Prolog Example, Roman BartákGuide to Prolog Example, Roman Barták –Go through all the examples –