John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
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.
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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
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.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Rahman Lavaee Mashhadi Mohammad Shadravan. Conditional expressions LISP was the first language to contain a conditional expression In Fortran and Pascal.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
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.
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.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
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.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 10
Functional Programming Languages
Section 15.4, 15.6 plus other materials
ML: a quasi-functional language with strong typing
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
LISP A brief overview.
Nondeterministic Evaluation
Using Lisp Lisp is a interactive system
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 A brief overview.
Announcements Quiz 5 HW6 due October 23
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
More Scheme CS 331.
The general format of case is the following: (case <key form>
LISP primitives on sequences
Presentation transcript:

John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning Proposed timesharing Mathematical theory …. Lisp stems from interest in symbolic computation (math, logic)

Language speeds www.bagley.org/~doug/shoutout: Completely Random and Arbitrary Point System

Example of formula (defun roots (a b c) (list (/ (+ (- b) (sqrt (- (expt b 2) (* 4 a c)) )) (* 2 a)) (* 2 a)) ))

eval and quote > (eval (cdr '(a + 2 3))) 5 > (setq a 'b) b error: unbound variable - b if continued: try evaluating symbol again 1> [ back to top level ] > (set 'a 'b) > (eval (eval ''a)) > 'a a

eval and quote > (eval (eval '(quote a))) b > 'a a > (eval '(list '* 9 6)) (* 9 6) > (eval (eval '(list * 9 6))) error: bad function - (* 9 6) 1> [ back to top level ] > (eval (eval '(list '* 9 6))) 54

Examples of tail recursion If last operation in function is recursive call, overwrite actuals and go to beginning of code: (defun last (lis) ; finds the last element of the list (if (null? (cdr lis) (car lis)) (last (crd lis)))) ; can be done with loop (defun length (lis) ; calculates the length of the list (if (null? lis) 0) (+ 1 (length (cdr lis)))) ; not tail recursive!

Example of Tree Recursion: Fibonacci Writing a function to compute the nth Fibonacci number Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, … fib(0) = 0 fib(1) = 1 fib(n) = fib(n-2) + fib(n-1)

Short Version of tree recursion (defun fib (n) (cond ((eql n 0) 0) ; base case ((eql n 1) 1) ; base case (t (+ (fib (- n 1)) ; recursively compute fib(n) (fib (- n 2))))))

Complete Version with Error Checking and Comments (defun fib (n) "Computes the nth Fibonacci number." (cond ((or (not (integerp n)) (< n 0)) ; error case (error "~s must be an integer >= 0.~&" n)) ((eql n 0) 0) ; base case ((eql n 1) 1) ; base case (t (+ (fib (- n 1)) ; recursively compute fib(n) (fib (- n 2))))))

Problems: 1. Write a function (power 3 2) = 3^2 = 9 2. Write a function that counts the number of atoms in an expression. (count-atoms '(a (b) c)) --> 3 3. (count-anywhere 'a '(a ((a) b) a)) --> 3 4. (dot-product '(10 20) '(3 4)) --> 10x3 + 20x4 = 110 5. Write a function (flatten '(a (b) () ((c)))) --> (a b c) which removes all levels of parenthesis and returns a flat list of atoms. 6. Write a function (remove-dups '(a 1 1 a b 2 b)) --> (a 1 b 2) which removes all duplicate atoms from a flat list. (Note: there is a built-in remove-duplicates in Common Lisp, do not use it).

Solutions 1-3 (defun power (a b) "compute a^b - (power 3 2) ==> 9" (if (= b 0) 1 (* a (power a (- b 1))))) (defun count-atoms (exp) "count atoms in expresion - (count-atoms '(a (b) c)) ==> 3" (cond ((null exp) 0) ((atom exp) 1) (t (+ (count-atoms (first exp)) (count-atoms (rest exp)))))) (defun count-anywhere (a exp) "count performances of a in expresion - (count-anywhere 'a '(a ((a) b) (a))) ==> 3" ((atom exp) (if (eq a exp) 1 0)) (t (+ (count-anywhere a (first exp)) (count-anywhere a (rest exp))))))

Solutions (defun flatten (exp) "removes all levels of paranthesis and returns flat list of atomsi (flatten '(a (b) () ((c)))) ==> (a b c)" (cond ((null exp) nil) ((atom exp) (list exp)) (t (append (flatten (first exp)) (flatten (rest exp))))))

Iteration – adding all elements from a list Iteration is done by recursion Analogous to while-loop (defun plus-red (a) (if (null a) 0 (plus (car a) (plus-red (cdr a)) )) )

Nested Loops Example : Cartesian product (defun all-pairs (M N) (if (null M) nil (append (distl (car M) N) (all-pairs (cdr M ) N )) )) (defun distl (x N) (if (null N) nil (cons (list x (car N)) (distl x (cdr N)) )) )

Hierarchical structures Are difficult to handle iteratively example: equal function eq only handles atoms initial states If x and y are both atoms (equal x y) = (eq x y) If exactly one of x and y is atom (equal x y) = nil (and (atom x) (atom y) (eq x y)) use car and cdr to write equal recursively

Equivalency of recursion and iteration it may be seemed that recursion is more powerful than iteration in theory these are equivalent As we said iteration can be done by recursion by maintaining a stack of activation records we can convert a recursive program to an iterative one.

Functional arguments and abstraction Suppress details of loop control and recursion example: applying a function to all elements of list (defun mapcar (f x) (if (null x) nil (cons (f (car x)) (mapcar f (cdr x)) )) ) (defun reduce (f a x) a (f (car x) (reduce f a (cdr x) )) ) )

Few more functions let* let* is similar to let, but the bindings of variables are performed sequentially rather than in parallel. The expression for the init-form of a var can refer to vars previously bound in the let*. The form (let* ((var1 init-form-1) (var2 init-form-2) ... (varm init-form-m)) declaration1 declaration2 declarationp form1 form2 formn) first evaluates the expression init-form-1, then binds the variable var1 to that value; then it evaluates init-form-2 and binds var2, and so on. The expressions formj are then evaluated in order; the values of all but the last are discarded (that is, the body of let* is an implicit progn).

Few more functions reduce reduce uses a binary operation, function, to combine the elements of sequence bounded by start and end. The function must accept as arguments two elements of sequence or the results from combining those elements. The function must also be able to accept no arguments. If key is supplied, it is used is used to extract the values to reduce. The key function is applied exactly once to each element of sequence in the order implied by the reduction order but not to the value of initial-value, if supplied. The key function typically returns part of the element of sequence. If key is not supplied or is nil, the sequence element itself is used. The reduction is left-associative, unless from-end is true in which case it is right-associative. If initial-value is supplied, it is logically placed before the subsequence (or after it if from-end is true) and included in the reduction operation. In the normal case, the result of reduce is the combined result of function's being applied to successive pairs of elements of sequence. If the subsequence contains exactly one element and no initial-value is given, then that element is returned and function is not called. If the subsequence is empty and an initial-value is given, then the initial-value is returned and function is not called. If the subsequence is empty and no initial-value is given, then the function is called with zero arguments, and reduce returns whatever function does. This is the only case where the function is called with other than two arguments.

Few more functions reduce Examples: (reduce #'* '(1 2 3 4 5)) => 120 (reduce #'append '((1) (2)) :initial-value '(i n i t)) => (I N I T 1 2) (reduce #'append '((1) (2)) :from-end t :initial-value '(i n i t)) => (1 2 I N I T) (reduce #'- '(1 2 3 4)) == (- (- (- 1 2) 3) 4) => -8 (reduce #'- '(1 2 3 4) :from-end t) ;Alternating sum. == (- 1 (- 2 (- 3 4))) => -2 (reduce #'+ '()) => 0 (reduce #'+ '(3)) => 3 (reduce #'+ '(foo)) => FOO (reduce #'list '(1 2 3 4)) => (((1 2) 3) 4) (reduce #'list '(1 2 3 4) :from-end t) => (1 (2 (3 4))) (reduce #'list '(1 2 3 4) :initial-value 'foo) => ((((foo 1) 2) 3) 4) (reduce #'list '(1 2 3 4) :from-end t :initial-value 'foo) => (1 (2 (3 (4 foo))))

Few more functions reduce SOME function searches the sequences for values for which predicate returns true. It there is such list of values that occupy same index in each sequence, return value is true, otherwise false. (some #'alphanumericp "") => NIL (some #'alphanumericp "...") => NIL (some #'alphanumericp "ab...") => T (some #'alphanumericp "abc") => T (some #'< '(1 2 3 4) '(2 3 4 5)) => T (some #'< '(1 2 3 4) '(1 3 4 5)) => T (some #'< '(1 2 3 4) '(1 2 3 4)) => NIL

Building Problem Solvers in LISP http://www.qrg.northwestern.edu/bps/directory.html

Genetic algorithm Common Lisp code

Genetic algorithm Common Lisp code from Dean: (defun reproduce (population) (let ( (offspring nil) (d (distribution population) )) (dotimes (i (/ (length population) 2) ) (let ( (x (selectone d) ) (y (selectone d)) ) (crossover x y) (setq offspring (nconc (list x y) offspring) ) )) offspring)) Takes population as argument Initializes offspring to empty list Distributed population Distributes initial population Repeats for the length of half population Selects parent x, one from population Selects parent y, one from population Does crossover of parents x and y End of adding new children to list offspring Creates new list offspring by adding new children x and y to old list offspring Returns new list offspring

Genetic algorithm Common Lisp code (importance of drawing trees for larger functions) reproduce (defun reproduce (population)    (let ( (offspring nil)          (d (distribution population) ))      (dotimes (i (/ (length population) 2) )        (let ( (x (selectone d) )              (y (selectone d)) )          (crossover x y)          (setq offspring (nconc (list x y) offspring) ) ))      offspring)) offspring distribution dotimes let length Selects parent x, one from population d selectone Selects parent y, one from population d setq crossover Does crossover of parents x and y nconc Creates new list offspring I found it very useful to create for myself such trees to know who is calling whom Returns list offspring

User defined function distribution Takes the initial population and distributes it according to fitness function Distributes initial population (defun distribution (population)    (let* ((genotypes (remove-duplicates population :test #'equal))    (sum (apply #'+ (mapcar #'fitness genotypes))))      (mapcar #'(lambda (x) (cons (/ (fitness x) sum) x)) genotypes))) Creates genotypes by removing the duplicates from population Uses function fitness Creates sum of fitness values Calculates fitness of all elements from list genotypes Creates a pair of normalized fitness and a genotype x Creates list of pairs for all elements of list genotypes

User defined function selectone This function was used in function reproduce two pages earlier Selects one parent from population (defun selectone (distribution)    (let ((random (random 1.0))           (prob 0)          genotype)      (some #'(lambda (pair)         (incf prob (first pair))         (if (> random prob) nil             ;;else             (setq genotype (rest pair))))         distribution)       (mutate genotype))) Initializes random Initializes prob Initializes genotype Compares elements of distribution. Selects those with higher prob Apply to the original distribution Calls function mutate to mutate the genotype

User defined functions for crossover and mutation (defun crossover (x y)    (if (> (random 1.0) 0.6) (list x y)  ;; in this case do nothing, return x and y as in input     ;;else      (let* ((site (random (length x)))      (swap (rest (nthcdr site x) )))        (setf (rest (nthcdr site x)) (rest (nthcdr site y)))        (setf (rest (nthcdr site y)) swap)))) (defun mutate (genotype)    (mapcar #'(lambda (x)          (if (> (random 1.0) 0.03) x             ;; else             (if (= x 1) 0                ;; else                1)))     genotype)) x and y are chromosomes Site is a place of cut Swap is temporary location Creates child 1 Execute crossover Creates child 2 Does mutation of a single genotype by flipping bits Can do several mutations at once

Calculate fitness function (defun fitness (x) (let ((xarg (/ (string2num x) 1073741823.0)) (v '(0.5 0.25 1.0 0.25)) (c '(0.125 0.375 0.625 0.875)) (w 0.003)) (reduce #'+ (mapcar #'(lambda (vi ci) (let ((xc (- xarg ci))) (* vi (exp (* -1 (/ (* 2 w)) xc xc))))) v c)))) (defun string2num (s) (loop for xi in (reverse s) for p = 1 then (* p 2) sum (* xi p))) Calculates fitness of a genotype x Set parameters Converts string to a number