Prolog: Lists Recall lists in Scheme: '(a b c) = (cons 'a (cons 'b (cons c '()))) ls = (cons (car ls) (cdr ls)) Want the same operations in Prolog: [a,

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
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).
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
Bahar Pamuk. Outline Introduction Syntaxa Clauses, Programs and Queries List Manipulation Operators Backtracking, Cuts References.
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.
Prolog Programming for Artificial Intelligence Three edition 2001
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
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/
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
Picking and Choosing… …Amb Above the Line. Intro to Nondeterminism So before we programmed to compute an answer to a problem. What if we set up a system.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
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.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia The Evolution of PLs1.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
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.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
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),
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof:  '(abc 123) = (cons 'abc (cons 123 '()))  '() is a list, so (cons 123.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Introduction to Prolog What if all we could do was to make assertions (statements) about the world? Example: family tree parent(linda, simon).male(simon).
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
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.
Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
CS314 – Section 5 Recitation 10
Additional Scheme examples
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
Functional Programming
FP Foundations, Scheme In Text: Chapter 14.
Announcements Quiz 5 HW6 due October 23
More Scheme CS 331.
Presentation transcript:

Prolog: Lists Recall lists in Scheme: '(a b c) = (cons 'a (cons 'b (cons c '()))) ls = (cons (car ls) (cdr ls)) Want the same operations in Prolog: [a, b, c] =.(a,.(b,.(c, []))) car([X|Tail], X). cdr([X|Tail], Tail). cons(X, Tail, [X|Tail]). lists.P

Prolog: Lists Try it out: | ?- consult(lists). | ?- car([1,2,3], H). H = 1 | ?- cdr([1,2,3], T). T = [2,3] | ?- cons(1, [2,3], L). L = [1,2,3]

Prolog: Anonymous Variables What really happens: | ?- consult(lists). ++Warning[XSB]: [Compiler]./lists: Singleton variable Tail in a clause of car/2 ++Warning[XSB]: [Compiler]./lists: Singleton variable X in a clause of cdr/2 Problem: We're not using some variables: car([X|Tail], X). % Tail unused cdr([X|Tail], Tail).% X unused

Prolog: Anonymous Variables Solution: Anonymous variables car([X|_], X). % _ = whatever cdr([_|Tail], Tail).

Prolog: Lists Can also write it in ML: fun member(a, nil) = false | member(a, x::t) = if a = x then true else member(a, t); Recall member? function (predicate) in Scheme: (define member? (lambda (a ls) (cond ((null? ls) #f) ((equal? a (car ls)) #t) (else (member? a (cdr ls)))))) How about Prolog? member(X, [X|_]). member(X, [_|T]) :- member(X, T).

Prolog: Backtracking and Cut Recall family tree example: parent(linda, simon). parent(sam, simon). parent(linda, sharon). parent(sam, sharon). female(sharon). sister(S,X) :- parent(P,S), parent(P,X), female(S). | ?- sister(Who, simon). Who = sharon; Who = sharon Look under the hood using trace :

Prolog: Backtracking and Cut | ?- trace. yes [trace] | ?- sister(Who, simon). (0) Call: sister(_h81,simon) ?% sister(S, X=simon) (1) Call: parent(_h148,_h81) ? % parent(P, S) (1) Exit: parent(linda,simon) ? % parent(P=linda, S=simon) (2) Call: parent(linda,simon) ? % parent(P=linda, X=simon) (2) Exit: parent(linda,simon) ? (3) Call: female(simon) ? % female(S=simon) (3) Fail: female(simon) ? (2) Redo: parent(linda,simon) ?% parent(P=linda, X=simon) (2) Fail: parent(linda,simon) ? % fail on redo (1) Redo: parent(linda,simon) ? % parent(P=linda, S=simon) (1) Exit: parent(linda,sharon) ? % parent(P=linda, S=sharon) (4) Call: parent(linda,simon) ? % parent(P=linda, X=simon) (4) Exit: parent(linda,simon) ? (5) Call: female(sharon) ? % female(S=sharon) (5) Exit: female(sharon) ? (0) Exit: sister(sharon,simon) ?% P=linda, X=simon, S=sharon Who = sharon; sister(S, X) :- parent(P,S), parent(P,X), female(S).

Prolog: Backtracking and Cut (0) Redo: sister(sharon,simon) ? (5) Redo: female(sharon) ?% female(S=sharon) (5) Fail: female(sharon) ? % fail on redo (4) Redo: parent(linda,simon) ? % parent(P=linda, X=simon) (4) Fail: parent(linda,simon) ? % fail on redo (1) Redo: parent(linda,sharon) ? % parent(P=linda, S=sharon) (1) Exit: parent(sam,simon) ? % parent(P=sam, S=simon) (6) Call: parent(sam,simon) ? % parent(P=sam, X=simon) (6) Exit: parent(sam,simon) ? (7) Call: female(simon) ? % female(S=simon) (7) Fail: female(simon) ? (6) Redo: parent(sam,simon) ? % parent(P=sam, X=simon) (6) Fail: parent(sam,simon) ? % fail on redo (1) Redo: parent(sam,simon) ? % parent(P=sam, S=simon) (1) Exit: parent(sam,sharon) ? % parent(P=sam, S=sharon) (8) Call: parent(sam,simon) ? % parent(P=sam, X=simon) (8) Exit: parent(sam,simon) ? (9) Call: female(sharon) ? % female(S=sharon) (9) Exit: female(sharon) ? (0) Exit: sister(sharon,simon) ? % P=sam, X=simon, S=sharon sister(S, X) :- parent(P,S), parent(P,X), female(S).

Prolog: Backtracking and Cut We can view this process as a tree, where each branch represents a binding choice:

Prolog: Backtracking and Cut We can view this process as a tree, where each branch represents a binding choice: X simon

Prolog: Backtracking and Cut X P S simon linda

Prolog: Backtracking and Cut X P S simon linda simon FAIL

Prolog: Backtracking and Cut X P S simon linda Backtrack to most recent success:

Prolog: Backtracking and Cut X P S simon linda sharon Succeed

Prolog: Backtracking and Cut X P S simon linda sharon Succeed User asks for more answers

Prolog: Backtracking and Cut X P S simon linda Backtrack: no more choices for S

Prolog: Backtracking and Cut X P simon Backtrack

Prolog: Backtracking and Cut X P simon sam S

Prolog: Backtracking and Cut X P simon sam S sharon Succeed

Prolog: Backtracking and Cut Cut ( ! ) prevents backtracking: sister(S,X) :- parent(P,S), parent(P,X), female(S), !. % don't backtrack! | ?- sister(Who, simon). Who = sharon; no No longer a “pure” logic paradigm: we are exploiting sequential order.