
L02: Symbolic Computation and Common Lisp (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2021 Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 1 / 58 Kay on Lisp https://youtu.be/pUoBSC3uoeo?t=40m49s Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 2 / 58 “Maxwell’s Equations of Programming” Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 3 / 58 What is Lisp? Definition (Lisp) A family of programming languages based on s-expressions. Data Structure “Math” NIL n defun factorial NIL n 1 if = 0 != n n n n ( ∗ ( − 1)! if =6 0 1 NIL if 0 NIL Code = n ( defun f a c t ( n ) NIL ( i f (= n 0) n * NIL 1 (∗ n ( f a c t (− n 1 ) ) ) ) ) factorial 1 NIL - n Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 4 / 58 Code is Data “Homoiconic” Code Data Structure NIL ( defun f a c t ( n ) defun factorial NIL ( i f (= n 0) n 1 (∗ n ( f a c t (− n 1 ) ) ) ) ) 1 NIL if 0 NIL = n NIL n * NIL factorial 1 NIL - n “data processing” ⇐⇒ “code processing” Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 5 / 58 Introduction Why study Lisp? Outcomes ◮ Different way of thinking about programming ◮ Understand s-expressions ◮ Functional Programming: mathematical ◮ Understand lisp programming functions w/o (explicit) state change operators ◮ Language theory algorithms and proofs often are functional/recursive/inductive ◮ Lisp has good support for functional programming ◮ Symbolic Computing: manipulating expressions of mathematical functions ◮ Symbolic expressions in language theory ◮ Lisp has good support for symbolic processing Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 6 / 58 The Lisp Learning Process Yee-haw! start “Lisp is weird!” “I dislike Learn to think differences!” give symbolically? not yet up yes end “Lisp is awesome!” end Lisp might feel like it’s “from outer space” (at first). The symbolic view of programming is different, often useful. Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 7 / 58 Outline Symbolic Computing Rewrite Systems Implementing Expressions List Manipulation Lisp Programming Overview Implementation Details Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 8 / 58 Symbolic Computing Outline Symbolic Computing Rewrite Systems Implementing Expressions List Manipulation Lisp Programming Overview Implementation Details Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 9 / 58 Symbolic Computing Rewrite Systems Rewrite Systems Definition (Rewrite System) A well defined method for mathematical reasoning employing axioms and rules of inference or transformation. A formal system or calculus. Example (Expressions) Example (Reductions) ◮ Algebraic Equations: ◮ Distributive Properties: 2 3 ◮ a0x + a1x + a3x ◮ x ∗ (y + z) xy + xz ◮ 3x + 1 = 10 ◮ ◮ Propositional Logic: α ∨ (β ∧ γ) (α ∨ β) ∧ (α ∨ γ) ◮ ◮ (p1 ∨ p2) ∧ p3 De Morgan’s Laws: ◮ (p1 ∧ p2) =⇒ p3 ◮ ¬(α ∧ β) (¬α ∨¬β) ◮ ¬(α ∨ β) (¬α ∧¬β) Progressively apply reductions until reaching desired expression. Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 10 / 58 Symbolic Computing Rewrite Systems Example: Algebra Given: 3x + 1 = 10 Find: x Solution: Initial 3x + 1 = 10 −1 3x + 1 − 1 = 10 − 1 Simplify 3x = 9 /3 3x/3 = 9/3 Simplify x = 3 How would you write a program to solve for x? Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 11 / 58 Symbolic Computing Implementing Expressions The Cons Cell Declaration Example s t r u c t cons { S-Expression void ∗ f i r s t ; s t r u c t cons ∗ r e s t ; (1 2 3) }; Cons Cell Diagram Diagram CAR / CDR / 1 2 3 NIL first rest Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 12 / 58 Symbolic Computing Implementing Expressions Example: Nested Lists nested nested (a (b c) (d e)) a b c d e ( z( }| {) z( }| {)) a NIL d e NIL (a (b c) c NIL (d e)) b Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 13 / 58 Symbolic Computing Implementing Expressions Exercise 1: Nested Lists (a (b c) d e) (a b c ((d) e)) Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 14 / 58 Symbolic Computing Implementing Expressions Abstract Syntax Concrete / Infix Abstract Syntax Tree S-Expression ◮ Function / Operator ◮ Root: ◮ First: Root, Function / Operator Function / Operator ◮ Arguments / Operands ◮ Children: ◮ Rest: Children, Arguments / Operands Arguments / Operands f (x0,..., xn) f (f x0 ... xn) x0 ... xn a + b + (+ a b) a b Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 15 / 58 Symbolic Computing Implementing Expressions Example: S-Expression Infix Expression Abstract Syntax Tree S-expression 3x + 1 = 10 = (= (+ (* 3 x) 1) 10) + 10 * 1 (= (+ (* 3 x) 1) 10) 3 x Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 16 / 58 Symbolic Computing Implementing Expressions Example: Cell Diagram S-Expression (= (+ (* 3 x) 1) 10) Cell Diagram 10 NIL = 1 NIL + 3 NIL * x Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 17 / 58 Symbolic Computing Implementing Expressions List vs. Tree List Tree s t r u c t cons { s t r u c t t r e e n o d e { void ∗ f i r s t ; void ∗ f i r s t ; s t r u c t cons ∗ r e s t ; s t r u c t cons ∗ c h i l d r e n ; }; }; s t r u c t cons { void ∗ f i r s t ; s t r u c t cons ∗ r e s t ; }; Same structure Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 18 / 58 Symbolic Computing Implementing Expressions Data Structure, Redux 3x + 1 = 10 Cons Cells Tree = = 10 NIL + 10 + 1 NIL * 1 * 3 x 3 NIL x Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 19 / 58 Symbolic Computing Implementing Expressions Exercise 2.a: S-Expression 2(x-1) = 4 2(x − 1)= 4 Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 20 / 58 Symbolic Computing Implementing Expressions Exercise 2.b: S-Expression a ∧ (b ∨ x) ∧ (c ∨¬x) a ∧ (b ∨ x) ∧ (c ∨¬x) Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 21 / 58 Symbolic Computing Implementing Expressions Exercise 2.c: S-Expression a ∧ (b ∨ x) ∧ (c ∨¬x) – continued Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 22 / 58 Symbolic Computing Implementing Expressions Hey! That’s a lot of irritating, silly parenthesis! Lisp Syntax Python Syntax (= (+ (∗ 3 x ) [ ’=’ ,[ ’+’ ,[ ’ ∗ ’ , 3 , ’ x ’ ] 1) 1 ] , 10) 10] The point is the data structure for code: process code like any other data! Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 23 / 58 Symbolic Computing Implementing Expressions Evaluation and Quoting Evaluation Quoting Evaluating (executing) an Returns the quoted s-expression: expression and yielding its return value: ’(fun abc) The s-expression: (fun abc) (fun abc) return value of fun applied to a, b, and c Example Example ◮ ’(+ 1 2) (+ 1 2) ◮ (+ 1 2) 3 ◮ ’1 1 ◮ 1 1 ◮ ’x x ◮ (quote x) x Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 24 / 58 Symbolic Computing List Manipulation Dotted List Notation ’(x . y) x y ’(x y) NIL x y ’(x . (y z)) NIL x y z ’(x (y z)) NIL x NIL y z Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 25 / 58 Symbolic Computing List Manipulation CONStruct Creating Lists CONS Contruct a new cons cell: (cons x y) a fresh cons cell with x in the car (first) and y in the cdr (rest) (cons α β) (α . β) α β (cons 1 NIL) (1 . nil) (1) 1 NIL (cons 2 (cons 1 NIL)) (2 . (1 . nil)) (2 1) 2 1 NIL Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 26 / 58 Symbolic Computing List Manipulation List Function LIST Return a list containing the supplied objects: (list a0 ... an) a list containing objects a0,..., an (list) NIL (list α) (cons α NIL) α NIL (list α β) (cons α (list β)) α β NIL (list αβγ) (cons α (list β γ)) α β γ NIL Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 27 / 58 Symbolic Computing List Manipulation Exercise 3: List Construction ◮ (cons ’x ’y) ◮ (cons ’x ’(y z)) ◮ (cons ’x (list ’y ’z)) ◮ (list (+ 1 2 3)) ◮ (list ’(+ 1 2 3)) ◮ (list ’∗ (+ 2 2) ’(− 2 2)) Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 28 / 58 Symbolic Computing List Manipulation List Access CAR / CDR Cons cell CAR CDR CAR CDR Return the car of a cons cell: Return the cdr of a cons cell: (car cell) (cdr cell) the car (first) of cell the cdr (rest) of cell (car ’(α . β)) α (cdr ’(α . β)) β Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 29 / 58 Symbolic Computing List Manipulation Example: CAR / CDR (car ’(x . y)) x (cdr ’(x . y)) y (car ’(xyz)) x (cdr ’(xyz)) NIL y z Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 30 / 58 Symbolic Computing List Manipulation List Template Syntax Backquote (‘): Create a template ◮ ‘(x0 ..
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages58 Page
-
File Size-