Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Slides:



Advertisements
Similar presentations
More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
Advertisements

Part 1 The Prolog Language Chapter 3 Lists, Operators, Arithmetic
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Logic Programming Lecture 9: Constraint logic programming.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
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 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Meta-interpreters Interpreted languages like Prolog can easily treat program code as data Interpreted languages like Prolog can easily treat program code.
Functional Programming Languages
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
ISBN Chapter 15 Functional Programming Languages.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
ISBN Chapter 15 Functional Programming Languages.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
ISBN Chapter 15 Functional Programming Languages.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Lecture 9: Constraint logic programming
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Artificial Intelligence
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Week 3 Help desks – Mon 4-5 CO238 Tutor: Su – Wed 1-2 CO238 Tutor: Urvesh – Fri CO238 Tutor: Bing Lectures: Mon, Tue, 3-4, HU119 Tutorial: Fri, 3-4,
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Logic Programming Lecture 9: Constraint logic programming.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Comparative Programming Languages Functional programming with Lisp/Scheme.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Abstract Syntax cs7100 (Prasad) L7AST.
CS314 – Section 5 Recitation 10
Additional Scheme examples
Functional Programming Languages
Functional Programming
CS 550 Programming Languages Jeremy Johnson
Chapter 15 – Functional Programming Languages
CS 270 Math Foundations of CS Jeremy Johnson
Artificial Intelligence CS370D
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
Announcements Quiz 5 HW6 due October 23
To understand recursion, one must first understand recursion.
Presentation transcript:

cs774 (Prasad)L4ListProcessing1 List processing in Prolog

List Structures List containing elements a, b, and c (in that order): [a, b, c] [a | [b, c]] [a, b | [c] ] cs774 (Prasad)L4ListProcessing2 element separator cons operator

(cont’d) List containing elements a, b, and c (in that order):.(a,.(b,.(c, []))) cs774 (Prasad)L4ListProcessing3 special list functor for cons empty list

Membership Test: Scheme vs Prolog (define (member e lis) (cond ((null? lis) #f) ((eq? e (car lis)) #t ) (else (member e (cdr lis))))) > (member 'a '(1 21 2)) #f member(E, [E|_]). member(E, [_|L]) :- member(E,L). ?- member(1,[12,1,3]). true ; cs774 (Prasad)L4ListProcessing4

Cartesian Product cs774 (Prasad)L4ListProcessing5 cart([],_,[]). cart([HA|TA],B,AB) :- pair(HA,B,C), cart(TA,B,D), append(C,D,AB). pair(_,[],[]). pair(HA,[HB|TB],[(HA,HB)|R]) :- pair(HA,TB,R).

Permutations [a,b] ->permute-> [[a,b], [b,a]] [a,b,c] ->permute-> [[c,a,b],[a,c,b],[a,b,c]] [[c,b,a],[b,c,a],[b,a,c]] cs774 (Prasad)L4ListProcessing6

cs774 (Prasad)L4ListProcessing7 permutation([],[]). permutation([H|T],P) :- permutation(T,Q), insert(H,Q,P). insert(H,Q,[H|Q]). insert(H,[S|R],[S|T]) :- insert(H,R,T). /*Generate all permutations through backtracking */ ?-setof(X,permutation([1,2,3],X),R). R = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]].

Collection Predicates p(a,b). p(b,b). p(c,b). p(e,d). p(a,d). p(a,a). /* collect all values of X for each Y */ ?-setof(X,p(X,Y),S). Y = a, S = [a] ; Y = b, S = [a, b, c] ; Y = d, S = [a, e]. cs774 (Prasad)L4ListProcessing8

(cont’d) p(a,b). p(b,b). p(c,b). p(e,d). p(a,d). p(a,a). /* collect all values of Y for which an X exists */ ?-setof(Y,X^p(X,Y),S). S = [a, b, d] ; cs774 (Prasad)L4ListProcessing9 ordered, no dups

(cont’d) p(a,b). p(b,b). p(c,b). p(e,d). p(a,d). p(a,a). /* collect all values of X for each Y */ ?-bagof(X,p(X,Y),S). Y = a, S = [a] ; Y = b, S = [a, b, c] ; Y = d, S = [e, a]. cs774 (Prasad)L4ListProcessing10

(cont’d) p(a,b). p(b,b). p(c,b). p(e,d). p(a,d). p(a,a). /* collect all values of Y for which an X exists */ ?-bagof(Y,X^p(X,Y),S). S = [b, b, b, d, d, a]. cs774 (Prasad)L4ListProcessing11 unordered, dups present

(cont’d) p(a,b). p(b,b). p(c,b). p(e,d). p(a,d). p(a,a). /* collect all values of X for which a Y exists, in order*/ ?-findall(X,p(X,Y),S). S = [a, b, c, e, a, a]. /* Free variables existentially quantified by default. */ ?-findall(Y,X^p(X,Y),S). S = [b, b, b, d, d, a]. cs774 (Prasad)L4ListProcessing12

Operators Normal syntax of a term : prefix form f(g(x,Y),5) Normal syntax of an expression with operators : infix form x * Y + 5 Besides operator name and arity, the following information is necessary to disambiguate expressions containing operators. Associativity Precedence Fixity (prefix/infix/postfix) cs774 (Prasad)L4ListProcessing13

INFIX x f y : Right-associative y f x : Left-associative x f x : Non-associative PREFIX f x : Non-associative f y : Right-associative POSTFIX x f : Non-associative y f : Left-associative Operator Declaration :- op(precedence, type, name). Examples: :- op( 1200, xfx, [ :-, --> ]). :- op(500, yfx, [ +, - ]). :- op( 500, fx, [ +, - ]). :- op(900, fy, [ \+ ]). Higher precedence class => Lower binding strength cs774 (Prasad)L4ListProcessing14

| 1200 |xfx |-->, :- | | 1200 | fx |:-, ?- | | 1150 | fx |dynamic, discontiguous, initialization, mod- | | | |ule_transparent, multifile, thread_local,| | | |volatile | | 1100 |xfy |;, | | | 1050 |xfy |->, op*-> | | 1000 |xfy |, | | 900 | fy |\+ | | 900 | fx |~ | | 700 |xfx |, | | \=, \==, is | | 600 |xfy |: | | 500 | yfx |+, -, /\, \/, xor | | 500 | fx |? | | 400 | yfx |*, /, //, rdiv, >, mod, rem | | 200 |xfx |** | | 200 |xfy |^ | |__200_|_fy__|+,_-,_\________________________________________|_ Table 4.1: Predefined system operators cs774 (Prasad)L4ListProcessing15

Examples : “Term Disambiguation” p; q; r p :- q :- r :- :- p \+ \+ p a + b + c a ^ b ^ c a – b / c p;(q;r) Illegal \+ (\+ p) (a + b) + c a ^ ( b ^ c) a – ( b / c ) cs774 (Prasad)L4ListProcessing16

Exercise p :- a, b -> c ; d. p :- (a, (b -> c ; d)). vs p :- (((a, b) -> c) ; d). q :- b -> c ; d, a. q :-(b -> (c ; (d, a))). vs q :-((b -> c) ; (d, a)). cs774 (Prasad)L4ListProcessing17