Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

Similar documents
Symbolic Computation and Common Lisp

Introduction to Functional Programming and basic Lisp

Introduction to lambda calculus Part 3

Documentation for LISP in BASIC

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Functional Programming

Functional Programming. Big Picture. Design of Programming Languages

FP Foundations, Scheme

Functional Programming. Pure Functional Programming

CS 314 Principles of Programming Languages

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Functional Programming. Pure Functional Languages

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

15 Unification and Embedded Languages in Lisp

Recursive Functions of Symbolic Expressions and Their Application, Part I

4/19/2018. Chapter 11 :: Functional Languages

Functional Programming. Pure Functional Languages

1.3. Conditional expressions To express case distinctions like

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

Functional Programming Languages (FPL)

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Building a system for symbolic differentiation

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Chapter 1. Fundamentals of Higher Order Programming

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

A short note on short differentiation programs in lisp, and a comment on logarithmic differentiation

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

CSc 520 Principles of Programming Languages

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

A Small Interpreted Language

Control in Sequential Languages

Functional Programming

A Brief Introduction to Scheme (II)

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Building a system for symbolic differentiation

SOFTWARE ARCHITECTURE 6. LISP

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS 360 Programming Languages Interpreters

Scheme: Expressions & Procedures

Functional Languages. Hwansoo Han

CSc 520 Principles of Programming Languages

Common LISP Tutorial 1 (Basic)

Functional programming with Common Lisp

Lambda Calculus. Gunnar Gotshalks LC-1

SCHEME AND CALCULATOR 5b

Problem Set CVO 103, Spring 2018

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Sample Final Exam Questions

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

by the evening of Tuesday, Feb 6

Introduction to Scheme

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

LECTURE 16. Functional Programming

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Introduction to Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Functional programming in LISP

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Lambda Calculus LC-1

Programming Systems in Artificial Intelligence Functional Programming

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Organization of Programming Languages CS3200/5200N. Lecture 11

Department of Computer and information Science Norwegian University of Science and Technology

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Lecture Notes on Lisp A Brief Introduction

CSCE 314 Programming Languages

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Chapter 15. Functional Programming Languages

Imperative, OO and Functional Languages A C program is

Lambda Calculus see notes on Lambda Calculus

6.001: Structure and Interpretation of Computer Programs

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Example Scheme Function: equal

Lisp. Versions of LISP

A LISP Interpreter in ML

CSCC24 Functional Programming Scheme Part 2

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

1 Maple Introduction. 1.1 Getting Started. 1.2 Maple Commands

An Explicit-Continuation Metacircular Evaluator

Program Calculus Calculational Programming

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Macros. Pat Hanrahan. CS448h Fall 2015

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Transcription:

Symbolic Reasoning Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Spring 2019 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 1 / 86

Introduction Definition: Symbolic Reasoning Inference using symbols abstract items which stand for something else coupled with rules to rewrite or transform symbolic expressions. Examples Common mathematical systems Algebra Differential calculus Various logics Computer algebra systems (Mathematica, Maxima, etc.) Symbolic Planners Outcomes Relate infix notations and symbolic data structures. Understand the s-expression notation. Apply recursion to s-expressions. Implement algorithms for symbolic reasoning. Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 2 / 86

Rewrite Systems Outline Rewrite Systems Symbolic Expressions Reductions List and S-Expression Manipulation Application: Computer Algebra Partial Evaluation Differentiation Notation and Programming Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 3 / 86

Rewrite Systems Rewrite Systems Expressions Reductions Arithmetic: a 0 x + a 1 x 2 + a 3 x 3 3x + 1 = 10 Propositional Logic: etc. (p 1 p 2 ) p 3 (p 1 p 2 ) = p 3 Distributive Properties: x (y + z) xy + xz α (β γ) (α β) (α γ) De Morgan s Laws: (α β) ( α β) (α β) ( α β) etc. Progressively apply reductions until reaching desired expression. Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 4 / 86

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) Symbolic Reasoning Spring 2019 5 / 86

Rewrite Systems Symbolic Expressions S-Expressions s t r u c t cons { void f i r s t ; s t r u c t cons r e s t ; } ; (1 2 3) CAR / first CDR / rest 1 2 3 NIL CONS cell: Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 6 / 86

Rewrite Systems Symbolic Expressions Example: S-Expression 3x + 1 = 10 Abstract Syntax Tree = S-expression (= (+ (* 3 x) 1) 10) + 10 * 3 x 1 (= (+ (* 3 x) 1) 10) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 7 / 86

Rewrite Systems Symbolic Expressions Cell Diagram 3x + 1 = 10 (= (+ (* 3 x) 1) 10) 10 NIL = 1 NIL + 3 NIL * x Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 8 / 86

Rewrite Systems Symbolic Expressions List vs. Tree List Tree s t r u c t cons { void f i r s t ; s t r u c t cons r e s t ; } ; s t r u c t t r e e n o d e { void f i r 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 ; } ; Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 9 / 86

Rewrite Systems Symbolic Expressions Data Structure, Redux 3x + 1 = 10 = = 10 NIL * + 1 10 + 1 NIL 3 x 3 * x NIL Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 10 / 86

Rewrite Systems Symbolic Expressions Exercise 1: S-Expression 2(x-1) = 4 2(x 1) = 4 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 11 / 86

Rewrite Systems Symbolic Expressions Exercise 2: S-Expression a + bx + cx 2 a + bx + cx 2 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 12 / 86

Rewrite Systems Symbolic Expressions Exercise 2: S-Expression a + bx + cx 2 continued Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 13 / 86

Rewrite Systems Reductions Rewrites -1 simplify /3 simplify 3x + 1 = 10 3x + 1-1 = 10-1 3x = 9 3x / 3 = 9 / 3 x = 3 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 14 / 86

Rewrite Systems Reductions Evaluation Function + * 2 3 / 4 2 eval : s-exp R 8 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 15 / 86

Rewrite Systems Reductions Recursive Evaluation Algorithm Base Case: If argument is a value: return the value Recursive Case: Else (argument is an expression): 1. Recurse on arguments 2. Apply operator to results Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 16 / 86

Rewrite Systems Reductions Example: Evaluation 2*3 + 4/2 + * 2 3 / 4 2 eval * 2 3, / 4 2 eval eval add Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 17 / 86

Rewrite Systems Reductions Example: Evaluation 2*3 + 4/2 continued add eval * 2 3, eval / 4 2 ( ( add mul eval 2 ) (, eval 3 ) ) ( (, div eval 4 ) (, eval 2 ) ) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 18 / 86

Rewrite Systems Reductions Example: Evaluation 2*3 + 4/2 continued ( ( add mul eval 2 ) (, eval 3 ) ) ( (, div eval 4 ) (, eval 2 ) ) add ( mul ( 2, 3 ) (, div 4, 2 ) ) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 19 / 86

Rewrite Systems Reductions Example: Evaluation 2*3 + 4/2 continued add ( mul ( 2, 3 ) (, div 4, 2 ) ) add ( 6, 2 ) 8 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 20 / 86

Rewrite Systems Reductions Evaluation via S-Expressions 2*3 + 4/2 (+ (* 2 3) (/ 4 2))) eval : s-exp R 8 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 21 / 86

List and S-Expression Manipulation Outline Rewrite Systems Symbolic Expressions Reductions List and S-Expression Manipulation Application: Computer Algebra Partial Evaluation Differentiation Notation and Programming Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 22 / 86

List and S-Expression Manipulation CONStruct Creating Lists (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) Symbolic Reasoning Spring 2019 23 / 86

List and S-Expression 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) Symbolic Reasoning Spring 2019 24 / 86

List and S-Expression Manipulation List Function (list) NIL (list α) (cons α NIL) α NIL (list α β) (cons α (list β)) α β NIL (list α β γ) (cons α (list β γ)) α β γ NIL Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 25 / 86

List and S-Expression Manipulation S-Expression Quoting Expressions vs. Execution Execute: (fun a b c) return value of fun called on arguments a, b, and c Expression: (fun a b c) The s-expression (fun a b c) Examples: (list 1 2 3) (1 2 3) (list (+ 1 2) 3) (list 3 3) (3 3) (list (+ 1 2) 3) ((+ 1 2) 3) (list + 1 ( 2 3)) (list + 1 6) (+ 1 6) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 26 / 86

List and S-Expression Manipulation Exercise: 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)) (list + ( a 2) ( 3 4)) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 27 / 86

List and S-Expression Manipulation List Template Syntax Backquote ( ): Create a template (x 0... x n ) (list x 0... x n ) (+ a (* b c)) (list + a (list * b c)) (+ a (* b c)) Comma (,): Evaluate next element and insert into list evaluated {}}{ (α..., y β...) (list α... y β... ) (+ a,(* 2 3)) (list + a (* 2 3)) (+ a 6) Comma-At (,@): Evaluate next element and splice into list splice {}}{ (α..., @y β...) (append α... y β...) (+ a,@(list (* 2 3) (* 4 5)) (append (list + a) (list (* 2 3) (* 4 5))) (+ a 6 20) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 28 / 86

List and S-Expression Manipulation Comma (,) vs. Comma-At (,@) (1,(list 2 3) 4) (1 (2 3) 4) 1 4 NIL 2 3 NIL (1,@(list 2 3) 4) (1 2 3 4) 1 2 3 4 NIL Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 29 / 86

List and S-Expression Manipulation Exercise: List Template Syntax (1 2,(+ 3 4)) (,1,2 (+ 3 4)) (+ 1,2,(+ 3 4)) (1 2,@(list + 3 4)) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 30 / 86

Outline Rewrite Systems Symbolic Expressions Reductions List and S-Expression Manipulation Application: Computer Algebra Partial Evaluation Differentiation Notation and Programming Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 31 / 86

Example: Evaluation via S-Expressions 2*3 + 4/2 (eval (+ (* 2 3) (/ 4 2))) (+ (eval (* 2 3)) (eval (/ 4 2)))) (+ (* (eval 2) (eval 3)) (/ (eval 4) (eval 2)))) (+ (* 2 3) (/ 4 2))) (+ 6 2)) 8 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 32 / 86

Evaluation Algorithm Procedure eval(e) 1 if value?(e) then /* Argument is a value */ 2 return e; 3 else /* Argument is an expression */ 4 operator first(e) ; 5 arg-sexp rest (e) ; 6 arg-vals map (eval, arg-sexp); 7 switch operator do 8 case + do f +; 9 case - do f ; 10 case / do f /; 11 case * do f ; 12 return apply(f, arg-vals); Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 33 / 86

Map function map : (X Y) }{{} function }{{} X n input sequence Y n }{{} output sequence Recursive Implementation Iterative Implementation Procedure map(f,s) 1 if empty?(s) then /* s is empty */ 2 return nil 3 else /* s has members */ 4 return cons (f (first(s)), map(f, rest (s)); Procedure map(f,s) 1 n length(s); 2 Y make-sequence(n); 3 i 0; 4 while i < n do 5 Y [i] = f (s[i]); 6 return Y ; Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 34 / 86

Exercise 1: Evaluation 2*(1+2+3) - 5 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 35 / 86

Exercise 1: Evaluation continued Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 36 / 86

Partial Evaluation Example: Partial Evaluation Given: f (x 0, x 1, x 2 ) = x 2 (2x 0 + 3x 1 + x 2 ) a = 1 b = 2 Find: Simplification of f (a, b, c) Solution: initial c(2a + 3b + c) substitute c(2 1 + 3 2 + c) evaluate c(2 + 6 + c) evaluate c(8 + c) expand 8c + c 2 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 37 / 86

Partial Evaluation Partial Evaluation via S-Expressions (* (+ (* 2 a) (* 3 b) c) c) (* (+ (* 2 1) (* 3 2) c) c) (* (+ 2 6 c) c) (* (+ 8 c) c) (+ (* 8 c) (* c c)) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 38 / 86

Partial Evaluation Partial Evaluation Function (* (+ (* 2 a) (* 3 b) c) c) (a. 1) (b. 2) p-eval : s-exp (symbol R) s-exp (+ (* 8 c) (* c c)) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 39 / 86

Partial Evaluation Recursive Partial Evaluation (p-eval (* (+ (* 2 a) (* 3 b) c) c) (p-eval-* (p-eval (+ (* 2 a) (* 3 b) c) (p-eval c)) (p-eval-* (p-eval-+ (p-eval (* 2 a)) (p-eval (* 3 b)) (p-eval c)) c) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 40 / 86

Partial Evaluation Recursive Partial Evaluation continued (p-eval-* (p-eval-+ (p-eval (* 2 a)) (p-eval (* 3 b)) (p-eval c)) c) (p-eval-* (p-eval-+ (p-eval-* (p-eval 2) (p-eval a)) (p-eval-* (p-eval 3) (p-eval b)) c) c) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 41 / 86

Partial Evaluation Recursive Partial Evaluation continued (p-eval-* (p-eval-+ (p-eval-* (p-eval 2) (p-eval a)) (p-eval-* (p-eval 3) (p-eval b)) c) c) (p-eval-* (p-eval-+ (p-eval-* 2 1) (p-eval-* 3 2) c) c) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 42 / 86

Partial Evaluation Recursive Partial Evaluation continued (p-eval-* (p-eval-+ (p-eval-* 2 1) (p-eval-* 3 2) c) c) (p-eval-* (p-eval-+ 2 6 c) c) (p-eval-* (+ 8 c) c) (+ (* 8 c) (* c c)) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 43 / 86

Partial Evaluation Algorithm: Partial Evaluation Procedure p-eval(e,bindings) 1 if number?(e) then 2 return e; 3 else if symbol?(e) then 4 if bindings[e] then return bindings[e] ; 5 else return e ; 6 else 7 y map (p-eval, rest (e)); 8 switch first(e) do 9 case + do f p-eval-+; 10 case * do f p-eval-*; 11... 12 return apply (f, y); Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 44 / 86

Partial Evaluation Algorithm: Partial Evaluation Continued Addition Algebraic Properties Commutative: α + β β + α Associative: (α + β) + γ α + (β + γ) Identity: α + 0 α Procedure p-eval-+(e...) 1 N {e E number?(e)}; 2 n fold-left (+, 0, N); 3 S {e E number?(e)}; 4 if 0 = n then 5 if = S then return 0; 6 else if 1 = S then return first(s); 7 else return cons ( +, S); 8 else 9 if = S then return n; 10 else return cons ( +, cons (n, S)); Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 45 / 86

Partial Evaluation Fold-Left Definition (fold-left) Apply a binary function to every member of a sequence and the result of the previous call, starting from the left-most (initial) element. fold-left : (Y X Y) Y }{{} function }{{} init. }{{} X n sequence Y }{{} result Function Application... + + y x 0 x 1 +... x n Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 46 / 86

Partial Evaluation Fold-left Pseudocode Procedural Function fold-left(f,y,x) 1 i 0; 2 while i < X do 3 y f (y, X i ) ; 4 return y; Function fold-left(f,y,x) Recursive 1 if empty?(x) then return y ; /* Base Case */ 2 else /* Recursive Case */ 3 y f (y, first(x )); 4 return fold-left (f, y, rest (X )); Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 47 / 86

Partial Evaluation Exercise: Partial Evaluation Given a = 3 b = 5 c = 7 e = a 1+b+c d Find: Recursively simplify e Solution: Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 48 / 86

Partial Evaluation Exercise: Partial Evaluation continued 1 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 49 / 86

Partial Evaluation Exercise: Partial Evaluation continued 2 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 50 / 86

Partial Evaluation Exercise: Partial Evaluation continued continued 3 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 51 / 86

Differentiation Derivative d f (t) dt = change in f (t) change in t = f (t) t = lim h 0 f (t+h) f (t) h Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 52 / 86

Differentiation Differential Calculus Rewrite Rules Constant: Variable: Constant Power (var): Constant Power (fun): Addition: Subtraction: Multiplication: Division: Chain Rule: d dt k 0 d dt t 1 d dt tk k t k 1 d dt f (t)k k (f (t)) k 1 d dt f (t) d dt (f (t) + g(t)) d dt f (t) + d dt g(t) d dt (f (t) g(t)) d dt f (t) d dt g(t) d dt (f (t) g(t)) ( d dt f (t)) g(t) + f (t) ( d dt ) g(t)) d dt f (t) d dt ( f (t) g(t) g(t) f (t) d dt g(t) (g(t)) 2 d dt f (g(t)) f (g(t)) d dt g(t) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 53 / 86

Differentiation Derivatives of Common Functions Sine: sin x cos x Cosine: cos x sin x Natural Logarithm: ln x 1 x Exponential: exp x exp x Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 54 / 86

Differentiation Differentiation Steps ( d dt cos t 2 ) ( cos t 2) d dt t2 d dt f (g(t)) f (g(t)) d dt g(t) ( sin t 2 ) d dt t2 d dx cos(x) sin x d dt tk kt k 1 ( sin t 2 ) 2 t Just apply rewrite rules Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 55 / 86

Differentiation Differentiation via Symbolic Expressions d dt α (diff α t) α NIL diff t Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 56 / 86

Differentiation Example: Differentiation S-exps d dt t (diff t t) NIL diff t d (diff (expt t 2) dt t2 t) NIL diff t 2 expt Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 57 / 86

Differentiation Exercise: Differentiation S-exps d sin t dt cos t Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 58 / 86

Differentiation Differential Calculus S-expression Rewrite Rules d dt f (t) (diff (f t) t) Constant: (diff k t) 0 Variable: (diff t t) 1 Constant Power: (diff (expt t k) t) ( k (expt t ( k 1))) Addition: (diff (+ (f t) (g t)) t) (+ (diff (f t) t) (diff (g t) t)) Multiplication: (diff (+ (f t) (g t)) t) Chain Rule: (diff (f (g t))t) (+ ( (diff (f t) t) (g t)) ( (f t) (diff (g t) t))) (* (deriv f (g t)) (diff (g t) t)) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 59 / 86

Differentiation Exercise: Differential Calculus S-expression Rewrite Rules Subtraction: d dt (f (t) g(t)) d dt f (t) d dt g(t) Division: d dt ( ) f (t) g(t) d dt f (t) g(t) f (t) d dt g(t) (g(t)) 2 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 60 / 86

Differentiation Derivatives of Common Functions S-expressions f (x) (deriv f x) Sine: (deriv sin α) (cos α) Cosine: (deriv cos α) ( (sin α)) Natural Logarithm: (deriv ln α) (/ 1 α) Exponential: (deriv exp α) (exp α) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 61 / 86

Differentiation S-expression Differentiation Steps (diff (cos (expt t 2)) t) (* (deriv cos (expt t 2)) (diff (expt t 2) t)) (* (- (sin (expt t 2))) (diff (expt t 2) t)) (* (- (sin (expt t 2))) (* 2 t))) (diff (f (g t))) ( ((deriv f (g t))) (diff (g t))) (deriv cos x) ( (sin x)) (diff (expt t k)) ( k (expt t ( k 1))) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 62 / 86

Differentiation Symbolic Differentiation Function d dx α (cos (expt t 2)) t d-eval : α {}}{ s-exp x {}}{ symbol d dx α {}}{ s-exp (* (- (sin (expt t 2))) (* 2 t))) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 63 / 86

Differentiation Symbolic Differentiation Algorithm Procedure d-eval(e,v) 1 if constant?(e) then return 0; // d dv k 0 2 else if v = e then return 1; // d dv v 1 3 else 4 f first(e); 5 if + = f then return d-eval-+(e,v) ; // d (f (t) + g(t)) dt 6 else if = f then return d-eval-*(e,v) ; // d (f (t) g(t)) dt 7 else if (expt = f ) constant?(third (e)) then // d f k (t) kf k 1 (t)( d f (t)) dt dt 8 return d-eval-expt(e,v) 9... 10 else if 1 = rest (e) then // d dt f (g(t)) = f (g(t)) d dt g(t) 11 return d-eval-chain(e,v); 12 else error( Unhandled expression ) ; Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 64 / 86

Differentiation Symbolic Differentiation Algorithm d-eval-+ Procedure d-eval-+(e,v) /* d (f (t) + g(t)) d f (t) + d g(t) dv dv dv 1 return cons ( +, map (d-eval, rest (e))) */ Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 65 / 86

Differentiation Symbolic Differentiation Algorithm d-eval-* Procedure d-eval-*(e,v) /* d dv (f (v) g(v)) ( d dv f (v)) g(v) + f (v) ( d dv g(v)) */ 1 a rest (e); 2 if 0 = a then return 0 ; 3 else if 1 = a then return d-eval(first(a),v) ; 4 else if 2 = a then 5 a 0 first(a) ; // f (t) 6 a 1 second (a) ; // g(t) 7 return (+ d dv f (v) g(v) {}}{ (, (d-eval a 0 v), a 1 ) d g(v) f (v) dv {}}{ (, a 0, (d-eval a 1 v))); 8 else // n-ary multiply: ( a β 0... β n) ( a ( β 0... β n)) 9 return d-eval-*(first(a), cons(, rest (a))); Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 66 / 86

Differentiation Symbolic Differentiation Algorithm d-eval-expt Procedure d-eval-expt(e,v) /* d dv f k (v) k (f (v)) k 1 ( d dv f (v)) */ 1 a 0 second (e); 2 k third (e); 3 return ( k (f (v)) k 1 {}}{ (expt a 0 ( k 1)) d dt f (v) {}}{ (d-eval a 0 v)) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 67 / 86

Differentiation Symbolic Differentiation Algorithm d-eval-chain Procedure d-eval-chain(e,v) /* d f (g(v)) f (g(v)) d g(v) */ dv dv 1 f first(e); 2 a 0 second (e) ; // g(v) 3 if constant?(a 0 ) then 4 return 0; 5 else f (g(v)) d dv g(v) {}}{{}}{ 6 return (, (deriv f a 0 ), (d-eval a 0 v)); Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 68 / 86

Differentiation Deriv Function f (g(t)) cos (expt t 2) deriv : f {}}{ symbol g(t) {}}{ s-exp f (g(t)) {}}{ s-exp (- (sin (expt t 2))) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 69 / 86

Differentiation Symbolic Differentiation Algorithm deriv Procedure deriv(f,a) 1 switch f do 2 case sin do return (cos, a) ; // sin a = cos a 3 case cos do return (- (sin, a)) ; // cos a = sin a 4 case ln do return (/ 1, a) ; // ln a = 1 a 5 case exp do return (exp, a) ; // exp a = exp a 6... /* Else: */ 7 error( Unhandled function ) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 70 / 86

Differentiation Example 0: Symbolic Differentiation Recursion Trace (d-eval (cos (expt t 2)) t) d-eval-chain (*,(deriv,cos,(expt t 2)),(d-eval (expt t 2) t)) (* (- (sin (expt t 2))),(d-eval (expt t 2) t)) (deriv cos x) ( (sin x)) d-eval-expt (* (- (sin (expt t 2))) (* 2 (expt t 1),(d-eval t t))) d dt t = 1 (* (- (sin (expt t 2))) (* 2 (expt t 1) 1)) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 71 / 86

Differentiation Exercise 1: Symbolic Differentiation Recursion Trace d dt sin2 t Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 72 / 86

Differentiation Exercise 2: Symbolic Differentiation Recursion Trace d dx ( ln x + a x 2 ) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 73 / 86

Differentiation Exercise 2: Symbolic Differentiation Recursion Trace d dx ( ln x + a x 2 ) continued 1 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 74 / 86

Differentiation Exercise 2: Symbolic Differentiation Recursion Trace d dx ( ln x + a x 2 ) continued 2 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 75 / 86

Differentiation Exercise 2: Symbolic Differentiation Recursion Trace d dx ( ln x + a x 2 ) continued 3 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 76 / 86

Differentiation Exercise 2: Symbolic Differentiation Recursion Trace d dx ( ln x + a x 2 ) continued 4 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 77 / 86

Notation and Programming Outline Rewrite Systems Symbolic Expressions Reductions List and S-Expression Manipulation Application: Computer Algebra Partial Evaluation Differentiation Notation and Programming Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 78 / 86

Notation and Programming Historical Note: Algebra The first quadrate, which is the square, and the two quandrangle sides, which are the ten roots, make together 39. Muhammad ibn Musa al-khwarizmi محمد بن موسی خوارزمی 5 x Algoritmi C.E 780-850 x 5 Modern Notation x 2 + 10x = 39 x 2 + 10x + 25 = 39 + 25 (x + 5) 2 = 64 x + 5 = 8 x = 3 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 79 / 86

Notation and Programming Sapir-Whorf Hypothesis Language determines / constraints thought. Edward Sapir Benjamin Lee Whorf Appropriate language/notation/abstraction makes math easier. Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 80 / 86

Notation and Programming S-Expressions and Programming McCarthy, John. Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I Math: { 1 if n = 0 n! = n (n 1)! if n 0 M-expression: n! = (n = 0 1, T n (n 1)!) S-expression: ( defun f a c t o r i a l ( n ) ( i f (= n 0) 1 ( n ( f a c t o r i a l ( n 1 ) ) ) ) ) Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 81 / 86

Notation and Programming Factorial Cell Diagram NIL defun factorial NIL n if = n 1 NIL 1 NIL * n NIL NIL factorial 1 NIL - n Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 82 / 86

Notation and Programming Homoiconic Code is Data NIL defun factorial NIL n ( defun f a c t o r i a l ( n ) ( i f (= n 0) 1 ( n ( f a c t o r i a l ( n 1 ) ) ) ) ) if = n 1 NIL 1 NIL NIL * n NIL factorial 1 NIL - n data processing code processing Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 83 / 86

Notation and Programming Lisp LISt Processor 1960: John McCarthy. Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I. 1961: Tim Hart and Mike Levin. The New Compiler. MIT AI Memo 39. 1975: Gerald Sussman and Guy Steele, Jr. Scheme: An Interpreter for Extended Lambda Calculus. MIT AI Memo 349. 1994: ANSI Common Lisp Standard Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 84 / 86

Notation and Programming Common Lisp Implementations Use SBCL! Name Compiler License URL Steel Bank Common Lisp Good Public Domain http://sbcl.org/ Clozure Common Lisp Fair Apache https://ccl.clozure.com/ Embeddable Common Lisp Fair LGPL https://common-lisp.net/project/ecl/ CLISP Bytecode GPL http://clisp.org/ LispWorks Good Commercial http://www.lispworks.com/ Allegro Common Lisp Good Commercial https://franz.com Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 85 / 86

Notation and Programming Summary Rewrite Systems Symbolic Expressions Reductions List and S-Expression Manipulation Application: Computer Algebra Partial Evaluation Differentiation Notation and Programming Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 86 / 86