CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

Slides:



Advertisements
Similar presentations
Artificial Intelligence
Advertisements

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.
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12–Prolog examples: Himalayan club, member, rem_duplicate,
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
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.
Knowledge Representation Methods
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Formal Models of Computation Part II The Logic Model
ARTIFICIAL INTELLIGENCE [INTELLIGENT AGENTS PARADIGM] Professor Janis Grundspenkis Riga Technical University Faculty of Computer Science and Information.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
CS 152: Programming Language Paradigms February 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
F28PL1 Programming Languages Lecture 16: Prolog 1.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Getting Started with Visual Prolog
1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Topics Covers everything –1) in slides –2) from the very beginning. Language classification, history, and comparison Programming paradigms –Structured.
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.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
On Using Scheme to Introduce Prolog Daniel E. Stevenson and Michael R. Wick Department of Computer Science University of Wisconsin - Eau Claire Eau Claire,
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Programming Languages Third Edition Chapter 4 Logic Programming.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
11 Artificial Intelligence CS 165A Thursday, October 25, 2007  Knowledge and reasoning (Ch 7) Propositional logic 1.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Invitation to Computer Science, Java Version, Second Edition 1 Logic Programming Logic programming  Various facts are asserted to be true  On the basis.
Knowledge Based Information System
1-1 An Introduction to Logical Programming Sept
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
CS 154 Formal Languages and Computability April 12 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
Section 16.5, 16.6 plus other references
CS 326 Programming Languages, Concepts and Implementation
Introduction to Prolog
CPSC 322 Introduction to Artificial Intelligence
Programming Languages 2nd edition Tucker and Noonan
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 2 Assignment #3  You are provided with Scheme code that differentiates polynomial expressions using infix notation.  Write two additional procedures evaluate and evaluate-deriv that each takes two parameters, f and x. f is a list that represents the function’s polynomial expression in infix notation. x is the value of x to evaluate f or f ', respectively. _ (deriv '(a x ^ 5 + b x ^ x ^ x ^ x + 7) 'x)  (5 a x ^ b x ^ x ^ x + 3) No * operators.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 3 Assignment #3, cont’d  The coefficient values can be defined beforehand. Example: (define a 1)  Your procedures must work at least with the following values for f, a, and b, and x bound to 0 and then to 1. Of course, you should test with other values. (define f '(a x ^ 5 + b x ^ x ^ x ^ x + 7)) (deriv f 'x)  (5 a x ^ b x ^ x ^ x + 3) (define a 1) (define b 1) (evaluate f 0)  7 (evaluate f 1)  20 (evaluate-deriv f 0)  3 (evaluate-deriv f 1)  30 No * operators.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 4 First-Class Objects  A first-class object is an object that Can have variables bound to it.  (define v obj) Can be passed as an argument to a procedure.  (proc obj) Can be returned as a value by a procedure (i.e., function).  (define obj (proc x y))  A Scheme procedure is a first-class object. A procedure can be bound to a variable.  (define p proc) A procedure can have a procedure as an argument.  (proc1 x y proc2) A procedure can return a procedure as its return value.  (define proc2 (proc1 x y))

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 5 Example: Passing a Procedure as an Argument (define first-or-second (lambda (pred x y) (if (pred x y) x y) )) (first-or-second < 5 6)  5 (first-or-second > 5 6)  6 (first-or-second equal? '(1 2 3) (cons 1 '(2 3)))  (1 2 3) (first-or-second equal? '(1 2 3) (cons 1 '(2 4)))  (1 2 4)  Apply predicate argument pred to arguments x and y.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 6 Passing a Procedure: map  Apply procedure argument proc to each top-level element of argument lst. (define map (lambda (proc lst) (if (null? lst) '() (cons (proc (car lst)) (map proc (cdr lst)))) )) (map add1 '( ))  ( ) (map (lambda (n) (* n n)) '( ))  ( )

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 7 Passing a Procedure: map cont’d (map (lambda (lst) (car lst)) '((a) (b c d) (e f)))  (a b e) (define list-of-first-items (lambda (lst) (map car lst))) (list-of-first-items '((a) (b c d) (e f)))  (a b e)

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 8 Returning a Procedure: compose  compose is a procedure that takes two arguments f and g. It returns a procedure that takes a single argument x. (define compose (lambda (f g) (lambda (x) (f (g x))) )) compose  # (compose sqrt add1)  # ((compose sqrt add1) 8)  3 ((compose add1 sqrt) 8) 

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 9 Only a Brief Taste of Lisp  Lisp is a language that epitomizes the functional programming paradigm. It is rich with features and concepts not often found in object-oriented languages. Scheme is a major dialect of Lisp, along with Common Lisp.  You only got a brief taste of the power, flexibility, and usefulness of this language!  The classic reference for Scheme is the textbook Structure and Interpretation of Computer Programs, by Harold Abelson and Gerald Jay Sussman of M.I.T. For years, Scheme was the language they taught in their Introduction to Programming classes. Free copy of the second edition:

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 10 Programming Paradigms  Different programming paradigms (cultures). Functional  Lisp, Scheme, ML, Haskell, F# Logic  Prolog Object-oriented  C++, C#, Objective C, Java, etc. _

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 11 Logic  Logic is the “science of reasoning and proof”.  Studied by several ancient civilizations. India, China, Persia Greece  Established as a formal discipline by Aristotle as a fundamental place in philosophy.  Deductive reasoning “Top down” logic. Reach a conclusion from one or more premises. Example: 1. All men are mortal. 2. Aristotle is a man. 3. Therefore, Aristotle is mortal.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 12 Prolog  Prolog (Programming in Logic) language A logical and a declarative programming language. Logical  Based on deductive reasoning Declarative  Your program specifies what result you want, and the Prolog system figures out how to compute it. _

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 13 Objects and Relations  Prolog programs solve problems that involve objects and the relationships between objects. Example: “John owns the book.”  Objects: John, the book  Relationship: ownership  Relationship has a specific order. John owns the book, but the book doesn’t own John.  Query a relationship. Does John own the book? _

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 14 Objects and Relations  Objects and relationships are abstractions. How much detail we need to provide about the objects and their relationships depends on the problem we want to solve.  Example: “Two people are sisters if they are both female and have the same parents.” Perhaps an oversimplified rule. But acceptable as a definition. Sufficient for the query: “Are Charlotte and Emily sisters?” _

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 15 Programming in Prolog  Prolog programming consists of: Declaring some facts about objects and their relationships. Defining some rules about objects and their relationships. Asking questions about objects and their relationships.  A Prolog system enables a computer To be a database of facts and rules. To make inferences from one fact to another.  Inference: The process of deriving logical conclusions from premises known or assumed to be true. Prolog is an inference engine. _

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 16 Programming in Prolog  Prolog is an interactive language, like Scheme.  Download and install SWI Prolog on Windows or Mac: Reference manual: pdf pdf Windows documentation:  In the interaction window, load the file demo/likes.pl : [swi('demo/likes')].  Prolog tutorial “Learn Prolog Now!”

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 17 Facts  Examples of facts, with possible English interpretations:  Prolog syntax: The names of all objects and relationships must begin with a lower-case letter. The name of a relationship is the predicate. The objects that the predicate uses are its arguments. Each fact must end with a period. valuable(gold). Gold is valuable. female(mary). Mary is female. owns(john, gold). John owns gold. father(sam, john). Sam is John’s father. likes(john, mary). John likes Mary. gives(john, flowers, mary). John gives flowers to Mary.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 18 Questions  A question in Prolog: Does Mary own a book? Is it a fact that Mary owns a book?  When you ask a question in Prolog: Prolog searches through its database of facts. It looks for facts that match the fact in question.  Two facts match if they have the same predicate. If it finds a match, Prolog answers yes or true. If there is no matching fact in the database, Prolog answers no or false. _ ?- owns(mary, book).

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 19 Questions, cont’d  Consider this database:  Then we can have this interactive session with Prolog: likes(joe, fish). likes(joe, mary). likes(mary, book). likes(john, book). ?- likes(joe, money). false. ?- likes(mary, joe). false. ?- likes(mary, book). true. ?- king(john, france). ERROR The answer false means only that there was no match in the database. Or that the question is not provable. Demo likes1.pl

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 20 Variables  Consider this database:  We can ask a question containing a variable. A variable name starts with a capital letter. What does John like? Prolog will respond: By pressing the Enter key, you tell Prolog that you are satisfied with the answer, and it should not search further. If you press the semicolon key, Prolog will continue its search: likes(john, flowers). likes(john, mary). likes(paul, mary). ?- likes(john, X). X = flowers X = mary. likes2.pl

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 21 Variables, cont’d  A variable is instantiated when it stands for an object. In this example, X is instantiated to flowers Otherwise, the variable is not instantiated. _ likes(john, flowers). likes(john, mary). likes(paul, mary). ?- likes(john, X). X = flowers

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 22 Place Markers  Whenever Prolog finds a fact that matches a question, it places a marker in the database at that fact.  If you ask it to search further, Prolog starts from the place marker. Prolog attempts to re-satisfy the question.  Another question: Who likes Mary? ?- likes(X, mary). X = john ; X = paul. First answer. We type ; in reply. No more answers. likes(john, flowers). likes(john, mary). likes(paul, mary).

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 23 Conjunctions  Consider this database:  Does John like Mary and does Mary like John? The comma, is pronounced “and”. Prolog must now satisfy the two separate subgoals.  Is there anything that Mary and John both like?  Prolog keeps a separate place marker for each goal. likes(mary, food). likes(mary, wine). likes(john, wine). likes(john, mary). ?- likes(john, mary), likes(mary, john). ?- likes(mary, X), likes(john, X). likes3.pl

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 24 Backtracking  Suppose Prolog satisfies the first goal of a conjunction.  If then it cannot satisfy the second goal, it backtracks to the first goal and attempts to re-satisfy that goal. If it succeeds, then it will attempt the second goal again. _ ?- likes(mary, X), likes(john, X).

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 25 Backtracking, cont’d  The first goal succeeds. X instantiates to food.  Next attempt the second goal. There is no rule likes(John, food). The second goal fails. _ ?- likes(mary, X), likes(john, X). likes(mary, food). likes(mary, wine). likes(john, wine). likes(john, mary).

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 26 Backtracking, cont’d  Backtrack to the first goal. X instantiates to wine.  Next attempt the second goal. Now the second goal succeeds.  Mary and John both like wine. _ ?- likes(mary, X), likes(john, X). likes(mary, food). likes(mary, wine). likes(john, wine). likes(john, mary).

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 27 Rules  An example rule in Prolog: John likes anyone who likes wine. The symbol :- is pronounced “if”: John likes X if X likes wine.  A rule consists of a head and a body. The head is to the left of :- The body is to the right of :-  Another rule: John loves women who like wine. likes(john, X) :- likes(X, wine). loves(john, X) :- female(X), likes(X, wine).

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 28 Rules, cont’d male(john). male(bob). female(mary). likes(bob, wine). likes(mary, wine). likes(john, X) :- likes(X, wine). loves(john, X) :- female(X), likes(X, wine). ?- likes(john, X). X = bob ; X = mary ; false. ?- loves(john, Y). Y = mary. likes4.pl Demo

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 29 Rules, cont’d  Is Alice the sister of Edward?  Match rule sister_of(X, Y). X instantiates to alice. Y instantiates to edward. male(albert). male(edward). female(alice). female(victoria). parents(edward, victoria, albert). parents(alice, victoria, albert). sister_of(X, Y) :- female(X), parents(X, Mom, Dad), parents(Y, Mom, Dad). ?- sister_of(alice, edward).  Satisfy the subgoals of the rule sister_of(X, Y). Match fact female(alice). Match fact parents(alice, victoria, albert).  Mom instantiates to victoria.  Dad instantiates to albert. Match fact parents(edward, victoria, albert).  All the subgoals succeeded, therefore Prolog answers true _ family.pl

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 30 Rules, cont’d  Match rule sister_of(X, Y). The question’s variable X is shared with the rule’s variable Y. When one is instantiated, so is the other to the same object. male(albert). male(edward). female(alice). female(victoria). parents(edward, victoria, albert). parents(alice, victoria, albert). sister_of(X, Y) :- female(X), parents(X, Mom, Dad), parents(Y, Mom, Dad). ?- sister_of(alice, X).  Satisfy the subgoals of the rule sister_of(X, Y). Match fact female(alice). Match fact parents(alice, victoria, albert).  Mom instantiates to victoria.  Dad instantiates to albert. Variable Y is still uninstantiated. Match fact parents(edward, victoria, albert).  Y instantiates to edward.  All the subgoals succeeded. Y is shared with the question’s X  X also instantiates to edward. Prolog answers X = edward family.pl

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 31  Satisfy the subgoals of the rule may_steal(X, Y). Match fact thief(john). Search for likes(john, Y).  Y is still uninstantiated. Match the head of the rule likes(john, X).  The subgoal’s variable Y is shared with the head’s variable X.  Both remain uninstantiated. Satisfy the subgoal likes(X, wine).  Match fact likes(mary, wine).  Variable X instantiates to mary.  Shared Y also instantiates to mary.  Subgoal likes(X, wine) satisfied.  All subgoals satisfied. Variable X shared with Y also instantiates to mary. So John may steal Mary: X = Mary Rules, cont’d thief(john). likes(mary, food). likes(mary, wine). likes(john, X) :- likes(X, wine). may_steal(X, Y) :- thief(X), likes(X, Y). ?- may_steal(john, X).  What may John steal?  Match rule may_steal(X, Y). X instantiates to john. The question’s variable X is shared with rule’s variable Y. thief.pl