Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010 http://www.ida.liu.se/ext/TDDC65/

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
ISBN Chapter 15 Functional Programming Languages.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
CS314 – Section 5 Recitation 9
Functional Programming
Functional Programming Languages
Artificial Intelligence and Lisp Lecture 3 LiU Course TDDC65 Autumn Semester,
CS 326 Programming Languages, Concepts and Implementation
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
A I (Artificial Intelligence)
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lists in Lisp and Scheme
Functional Programming Languages
Env. Model Implementation
LISP A brief overview.
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
First Lecture on Introductory Lisp
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Modern Programming Languages Lecture 21 Fakhar Lodhi
The Metacircular Evaluator
Modern Programming Languages Lecture 20 Fakhar Lodhi
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Lisp and Scheme I.
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Macros in Lisp
Announcements Quiz 5 HW6 due October 23
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Modern Programming Languages Lecture 18 Fakhar Lodhi
15.2 Mathematical Functions
Common Lisp II.
Lisp.
More Scheme CS 331.
Defining Macros in Scheme
LISP primitives on sequences
Presentation transcript:

Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010 http://www.ida.liu.se/ext/TDDC65/

List processing (in CEL) Recursively nested sequences as the only composite expressions, with symbols, strings, numbers as elements Functions on sequences (no side-effects!) (e1 <a b c>) = a (e2 <a b c>) = b (en 4 <a b c d e f>) = d (t1 <a b c>) = <b c> t2, tn similarly (cons g <a b c>) = <g a b c> Notice: (cons (e1 .x) (t1 .x)) = x

List processing (in CEL) Predicates on lists [equal <a b c> <a b c>] = true Control operators, for example (if [equal .a <>] .b .a) Other necessary function (type-of .a) The value is either symbol, string, integer, real, sequence Other functions can be defined in terms of these, for example and, or, not

List Processing - Recursive Functions (length .s) == (if [equal .s <>] 0 (+ 1 (length (t1 .s)))) (replace .a .b .s) == (if [equal .s .a] .b (if [atom .s] s (cons (replace .a .b (e1 .s)) (replace .a .b (t1 .s)) ))) (append .a .b) == (if [equal .a <>] b (cons (e1 .a)(append (t1 .a) b)) )

S-expression Style Examples: color.red "Yes or no: " 421 (walkto busstop-4) (walk :from home :to busstop-4) (seq (walkto busstop-4)(takebus Lkp-C)) (set busstop-4 busstop-7 busstop-12) (+ (timefor (walkto busstop-d)) (timefor (waitfor (some (and bus (going-to Lkp-C)) ))) (timefor (go bus busstop-4 Lkp-C)) )

S-expression Style 1. Nested parenthesized expressions (lists) 2. Convention: operator as first element in list 3. Convention: use of tags e.g. :from, :to 4. Convention, sometimes: variables as ?vbl Can be used for programming language, but also for plain data, for logic, and for alternative languages S-expression-based programming languages: Lisp, Scheme

Lisp Programming Lisp uses S-expression style, all data are formed as recursively nested expressions with round parentheses Using function names such as e1 and t1, rewrite as follows (e1 <a b c>) (e1 (quote (a b c))) ssv .a <red green> (setq a (quote (red green))) (e1 .a) (e1 a) General rule: a list (g a b c) is considered as a term with g as a function and the rest as arguments, except for a small number of special operators, such as quote, if, ... This results in the simplest possible system, but at some expense of readability. KRE / CEL is closer to the standard notation of logic and mathematics.

Lisp Recursive Function Definitions (defun length (s) (if (equal s nil) 0 (+ 1 (length (t1 s)))) ) (defun replace (a b s) (if (equal s a) b (if (atom s) s (cons (replace a b (e1 s)) (replace a b (t1 s)) )))) (defun append (a b) (if (equal a nil) b (cons (e1 a)(append (t1 a) b)) ))

Other Lisp Control Primitives (cond (c1 v1)(c2 v2) ...) same as (if c1 v1 (if c2 v2 ... )) (dolist (x '(a b c)) (...)) allows x to range over a, b, c and performs (...) (dotimes (n 10) (...)) allows n to range from 0 to 9 and performs (...) (let ((a 5)(b 10)) (...)) binds a and b and evaluates (...) with those bindings (let* ((a 5)(b (- a 1))) (...)) binds a and b successively and then evaluates (...) The expressions in (...) may read, print, assign, etc.

Lisp Systems - Data Structure (red green blue) represented as nil red green blue The e1 and t1 operations follow a pointer (but they are actually called car and cdr in Lisp) The cons operation constructs a new 'yellow' cell This was the origin of the method of garbage collection.

Performance in Alternative Definitions (defun reverse (a) (if (null a) nil (append (reverse (t1 a)) (list (e1 a))) )) Note: nil is the same as (), (null a) is (equal a nil), (list a b c...) forms a list, (list a) is (cons a nil) (defun reverse (a)(rev2 a nil)) (defun rev2 (a b) (if (null a) b (rev2 (t1 a)(cons (e1 a) b))) ) Compare the number of allocated cons cells for the computation as a function of the length of the argument

Definitions as Propositions in Logic (defun reverse (a) (if (null a) nil (append (reverse (t1 a)) (list (e1 a))) )) Compare: (reverse nil) = nil (reverse (cons a b)) = (append (reverse b)(list a)) Evaluation of (reverse '(red green blue)) Rewrite as (reverse (cons 'red '(green blue))) Rewrite as (append (reverse '(green blue)) (list 'red)) and so on... (append '(blue green) (list 'red)) Then apply the function append directly, or evaluate it in the same way. -- Possible but inefficient (?)

Program Correctness Proof Specification of the reverse function: (reverse nil) = nil (reverse (cons a b)) = (append (reverse b)(list a)) Implementation of that specification: (defun reverse1 (a)(rev2 a nil)) (defun rev2 (a b) (if (null a) b (rev2 (t1 a)(cons (e1 a) b))) ) Desired to prove: (reverse a) = (reverse1 a) for all a Make proof by induction over a: 1. (reverse nil) = nil (reverse1 nil) = (rev2 nil nil) = nil 2. (reverse (cons x y)) = (append (reverse y)(list x)) = (append (reverse1 y)(list x)) according to ind.hyp. (reverse1 (cons x y)) = (rev2 (cons x y) nil) = (rev2 y (list x))

Program Correctness Proof Specification of the reverse function: (reverse nil) = nil (reverse (cons a b)) = (append (reverse b)(list a)) Implementation of that specification: (defun reverse1 (a)(rev2 a nil)) (defun rev2 (a b) (if (null a) b (rev2 (t1 a)(cons (e1 a) b))) ) Desired to prove: (append (reverse y)(list x)) = (rev2 y (list x)) Try for more general: (append (reverse y) x) = (rev2 y x) Make proof by induction over y: 1. (append (reverse nil) x) = x (rev2 nil x) = x 2. (append (reverse (cons u v)) x) = ... (rev2 (cons u v) x) = (rev2 v (cons u x))

Program Correctness Proof Specification of the reverse function: (reverse nil) = nil (reverse (cons a b)) = (append (reverse b)(list a)) Implementation of that specification: (defun reverse1 (a)(rev2 a nil)) (defun rev2 (a b) (if (null a) b (rev2 (t1 a)(cons (e1 a) b))) ) Desired to prove: (append (reverse y)(list x)) = (rev2 y (list x)) Try for more general: (append (reverse y) x) = (rev2 y x) Make proof by induction over y: 1. (append (reverse nil) x) = x (rev2 nil x) = x 2. (append (reverse (cons u v)) x) = (append (append (reverse v) (list u)) x) = (append (reverse v) (list u) x) = (append (reverse v) (append (list u) x)) = (rev2 v (append (list u) x)) = (rev2 v (cons u x)) (rev2 (cons u v) x) = (rev2 v (cons u x))

Program Property Proof Specification of the append function: (append nil c) = c (append (cons a b) c) = (cons a (append b c)) Desired to prove: (append (append a b) c) = (append a (append b c)) Make proof by induction over a: 1. (append (append nil b) c), (append b c) (append nil (append b c)), (append b c) 2. (append (append (cons x y) b) c) (append (cons x (append y b)) c) (cons x (append (append y b) c)) (append (cons x y)(append b c)) (cons x (append y (append b c))) Equality is obtained using the induction hypothesis.

Principles illustrated by this proof example Use the combination of a specification (clean, simple, expressed in logic, has recursive counterpart that is often quite inefficient) and an implementation (takes performance into account, therefore often more complex, requires correctness proof using the specification) Correctness proofs are almost always induction proofs Finding the induction proof sometimes comes naturally from the structure of the two definitions, but sometimes it requires some thinking to find the right induction hypothesis

Side-Effect Functions in Lisp nil red green blue (rplaca a b) assumes a is a cons cell. It replaces the e1 pointer in a so that (e1 a) becomes b, and returns a. (rplacd a b) is similar but with respect to the t1 pointer. Here rplaca is “replace car”, rplacd is “replace cdr”.

Side-effect Oriented Functions (setq vbl value) Used above (prog2 a b) Evaluates a and b and returns the value of b (progn a b ... d) Evaluates arguments in order and returns value of last arg Example (“destructive append”): (defun nconc (a b)(if (null a) b (nconc2 a a b))) (defun nconc2 (aa a b) (if (null (t1 a)) (progn (rplacd a b) aa) (nconc2 aa (t1 a) b) ))

Iterative Function Definitions The definitions of rev2 and nconc2 are examples of recursive definitions that are iterative A recursive function definition is iterative if the function itself only occurs on the top level of then and else branches (second and third argument of if), recursively, but not in subexpressions of these branches and not in the if branch (first argument of the if operator) For systems of several recursive functions that invoke each other, this must hold for all the definitions Iterative function definitions can be executed particularly efficiently since there is no need to build the execution stack. This applies in both interpreted and compiled mode. Notice that the definitions above that avoid unnecessary cons operations also happen to be iterative. (This is a tendency but not a general rule).

The implicit progn (defun foo (a b) (fie a)(fum b)) is the same as (defun foo (a b)(progn (fie a)(fum b))) (cond ((foo a b)(fie a)(fum b)) (t (fie b)(fum a)) ) (cond ((foo a b)(progn (fie a)(fum b))) (t (progn (fie b)(fum a))) ) Similar implicit progn is used e.g. in let expressions

Evaluation order for and and or The Lisp functions and and or consider nil as 'false' and everything else as 'true' Therefore (and a b) is nil if some argument is nil and (or a b) is nil if all arguments are nil These functions evaluate their arguments in order, and only as many as are needed Therefore (and a b) is not necessarily the same as (and b a), if a and b are subexpressions, and similarly for or Example: (or (progn (setq a 5) t) (setq a 6)) Example: Suppose (faculty -1) goes into infinite loop, and compare (or (faculty 1)(faculty -1)) with (or (faculty -1)(faculty 1))