Logic Programming Part 3: Control Flow

Slides:



Advertisements
Similar presentations
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Advertisements

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.
Logic Programming Lecture 3: Recursion, lists, and data structures.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
CS 2104 – Prog. Lang. Concepts Final wrap-up Dr. Abhik Roychoudhury School of Computing.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Overview of Programming Paradigms
Formal Aspects of Computer Science – Week 12 RECAP Lee McCluskey, room 2/07
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
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.
Logical and Functional Programming
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 2: Unification and proof search.
제 9 주. 응용 -4: Robotics Artificial Life and Real Robots R.A. Brooks, Proc. European Conference on Artificial Life, pp. 3~10, 1992 학습목표 시뮬레이션 로봇과 실제 로봇을.
Review for Test 2 Chapters 5 (start at 5.4), 6.1, , 12, 13, 15.1, Python.
Programming Language History and Evolution
Functional Programming
CS 3723 Programming Languages
Algorithms and Problem Solving
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
EA C461 – Artificial Intelligence Logical Agent
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Computability and Complexity
CSPs: Search and Arc Consistency Computer Science cpsc322, Lecture 12
Representation, Syntax, Paradigms, Types
The function of knowledge representation scheme is
Artificial Intelligence Chapter 17 Knowledge-Based Systems
Artificial Intelligence Chapter 17 Knowledge-Based Systems
Programming Language History and Evolution
Nondeterministic Evaluation
CSPs: Search and Arc Consistency Computer Science cpsc322, Lecture 12
Complexity 6-1 The Class P Complexity Andrei Bulatov.
Prolog syntax + Unification
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
UNIT 3 CHAPTER 1 LESSON 4 Using Simple Commands.
Representation, Syntax, Paradigms, Types
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Representation, Syntax, Paradigms, Types
Programming Paradigms and Languages
Artificial Intelligence
Negations of quantifiers
Artificial Intelligence Chapter 17. Knowledge-Based Systems
Algorithms and Problem Solving
Principles of Programming Languages
Representation, Syntax, Paradigms, Types
Some Programming Paradigms
Relational Calculus Chapter 4, Part B 7/1/2019.
Controlling Backtracking : Cuts
Representations & Reasoning Systems (RRS) (2.2)
Presentation transcript:

Logic Programming Part 3: Control Flow James Cheney CS 411

Declarative Programming Ideal: Write down logical formulas that define programs clearly & concisely In logic, clause order doesn’t matter Control flow, efficiency invisible Reality: Must know how programs are run Efficiency (backtracking, clause order) Correctness (determinism, termination)

Clause order matters p(X) :- p(X). p(a). Has answer X  p(a) But depth-first search doesn’t terminate.

Clause order matters p(a). p(X) :- p(X). Terminates with X  p(a) .

Backtracking Consider a :- b,c,d,e,f. a :- b,c,d,e,g. b. c. d. e. g. First we solve b,c,d,e, then fail on f. Then we solve b,c,d,e again and then g. Duplicate effort!

Order and Backtracking If instead we do (logically equivalent) a :- f,b,c,d,e. a :- g,b,c,d,e. b. c. d. e. g. then the failure occurs earlier, and there is less wasted effort.

Nondeterminism Consider mem(A,A::L). mem(A,B::L) :- mem(A,L). Then ?- member (1,[1,2,3,1]). can succeed in two different ways.

“Cut” PROLOG includes a goal called “cut”, written !, for pruning the search space Removes current backtracking state. Allows more control over search, efficiency However, “cut” damages correspondence to logic

“Cut” Example mem(A,A::L) :- !. mem(A,B::L) :- mem(A,L). ?- mem([1,X],[[1,2],[1,3]]). yes X  2; no Only the first solution is found.

“Cut” Example But, the definition of mem is no longer “complete”: ?- mem(X,[1,2,3,4]). yes X  1; no “cut” may exclude desirable solutions

More Reality In PROLOG, I/O primitives are “impure” predicates Example: main :- write(“What is your name?”), read(X), write(“Hello, ”), write(“X”). Now duplication can also change program.

Other LP systems lProlog: (l X. F X) a == f a ? PROLOG uses FOL, lProlog uses higher-order logic Typed “Functional programming” (map, fold) Powerful, but complex, higher-order unification (l X. F X) a == f a ?

Sorting sorted([]). sorted([A]). sorted([A,B|M]) :- A < B, sorted(B,M). sort(L,M) :- perm(L,M), sorted (L,M). Complexity?

Mergesort msort([],[]). msort(L,L’) :- split(L,L1,L2), msort(L1,L1’), merge(L1’,L2’,L’). split([],[]). split([A|L],[A|M],N) :- split(L,N,M).

Mergesort (Cont’d) merge([],L,L). merge(L,[],L). merge([A|L],[B|M],[A|N]) :- A <= B, merge(L,[B|M],N). merge([A|L],[B|M],[B|N]) :- A > B, merge([A|L],M,N).

Other LP systems: Mercury Mercury: typed, “pure” ML-style polymorphism, datatypes Modes & determinism checking I/O primitives take “world argument” main(W0,W4) :- read(X,W1,W2), write(“Hello World”,W2,W3), write(X,W3,W4).

Other LP systems: lProlog Idea: Mix functional and logic paradigms Term language is higher-order typed l-calculus Requires solving hard (undecidable!) unification problems (lx. F x) a = f a  F  f, F  (lx. a) Can encode variable binding syntax using l’s

Applications Artificial intelligence Constraint solving/optimization Natural language processing Expert systems Constraint solving/optimization Logic programs + constraints describe solutions to complex problems Query languages (eg SQL, XQuery) declarative in flavor

Summary Logic programming a powerful paradigm “Algorithm = logic + control” Unfortunately, for efficiency reasons, LP programs diverge from this ideal Mathematical clarity != programming efficiency “cut”, imperative features lead to opaque programs Lesson: TANSTAAFL.