Quick viewing(Text Mode)

L02: Symbolic Computation and Common Lisp (Pre Lecture)

L02: Symbolic Computation and (Pre Lecture)

Dr. Neil . 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 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 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 ◮ : 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 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) ( 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 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 ... xn) (list ’x0 ... ’xn) ◮ ‘(+ a (* b c)) (list ’+ ’a (list ’∗ ’b ’c)) (+ a (∗ b c))

Comma (,) Comma-At (,@) Evaluate and insert: Evaluate and splice: ◮ ‘(α... ,y β...) splice evaluated ◮ ‘(α... ,@y β...) (append α... y β...)   z}|{ list α... y β... ◮ ‘(+ a ,@ (list (∗ 2 3) (∗ 4 5)) ) z}|{   (append ’(+ a) (list (* 2 3) (* 4 5))) ◮ ‘(+ a ,(∗ 2 3)) (+ a 6 20) (list ’+ ’a (* 2 3)) (+ a 6)

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 31 / 58 Symbolic Computing List Manipulation Comma (,) vs. Comma-At (,@)

Comma ,: Insert ‘(1 , (list 2 3) 4) (1 (2 3) 4) 1 4 NIL

2 3 NIL

Comma-At ,@: Splice ‘(1 ,@ (list 2 3) 4) (1 2 3 4)

1 2 3 4 NIL

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 32 / 58 Symbolic Computing List Manipulation Exercise 4: List Template Syntax ◮ ‘(1 2 ,(+ 3 4))

◮ ‘(,1 ,2 (+ 3 4))

◮ ‘(+ 1 ,2 ,(+ 3 4))

◮ ‘(1 2 ,@(list ’+ ’3 ’4))

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 33 / 58 Lisp Programming Overview 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 34 / 58 Lisp Programming Overview Hello World

C Common Lisp

#include ( format t "hello , world~%" )

main ( ) { p r i n t f ( "hello , world\n" ); }

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 35 / 58 Lisp Programming Overview Booleans and Equality

“Math” Lisp Notes False nil equivalent to the empty list () True t or any non-nil value ¬a (not a) a = b (= a b) numerical comparison a = b (eq a b) same object (“physical equality”) a = b (eql a b) same object, same number and type, or same character a = b (equal a b) eql objects, or lists/arrays with equal elements

a = b (equalp a b) = numbers, or same character (case-insensitive), or recursively-equalp cons cells, arrays, structures, hash tables a =6 b (/= a b) numerical inequality a =6 b (not (eq a b)) and similarly for other equality functions

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 36 / 58 Lisp Programming Overview Example: Lisp Equality Operators

◮ (= 1 1) t ◮ (= "a" "a") error ◮ (eq 1 1) t ◮ (eq "a" "a") nil integer float ◮ (eql "a" "a") nil ◮ (= 1 1.0 ) t ◮ z}|{ z}|{ (equal "a" "a") t integer float ◮ (equal "a" "A") nil ◮ (eq 1 1.0 ) nil z}|{ z}|{ ◮ (equalp "a" "A") t integer float ◮ (not t) nil ◮ (eql 1 1.0 ) nil z}|{ z}|{ ◮ (not nil) t integer float ◮ (not "a") nil ◮ (equal 1 1.0 ) nil z}|{ z}|{ integer float ◮ (equalp 1 1.0 ) t z}|{ z}|{

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 37 / 58 Lisp Programming Overview Exercise 5: Lisp Equality Operators

◮ (eq (list ”a””b”) (list ”a””b”)) ◮ (not 0) ◮ (equal (list ”a””b”) (list ”a””b”)) ◮ (not 1) ◮ (eq t (not nil)) ◮ ◮ (eq t 1) (eq (list ”a””b”) (list ”a””B”)) ◮ ◮ (eq nil (not 1)) (equal (list ”a””b”) (list ”a””B”)) ◮ (eq nil (not "a")) ◮ (equalp (list ”a””b”) (list ”a””B”))

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 38 / 58 Lisp Programming Overview Numeric Relations

“Math” Lisp a < b (< a b) a ≤ b (<= a b) a > b (> a b) a ≥ b (>= a b)

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 39 / 58 Lisp Programming Overview Boolean Operators

Operators Example (Short-circuiting) “Math” Lisp ◮ (and t nil) nil ¬a (not a) ◮ (and t 1) 1 a ∧ b (and ab) ◮ (or nil 1) 1 a ∨ b (or ab) ◮ (or 1 nil) 1

Example (N-ary) ◮ (and t 1 nil) nil ◮ (and t 1 2) 2 ◮ (or nil nil 1) 1

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 40 / 58 Lisp Programming Overview Arithmetic Operators

Operators Example (N-ary) “Math” Lisp ◮ (+ 1 2 3) 6 a + b (+ ab) ◮ (− (+ 1 2)) −3 a − b (− ab) a ∗ b (∗ ab) a/b (/ ab)

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 41 / 58 Lisp Programming Overview Function Definition

DEFUN Defines a new function in the global environment.

(defun FUNCTION-NAME ARGUMENTS BODY...)

Example (Pseudocode) Example (Common Lisp) function name formal arguments Function increment(n) 1 return n + 1; (defun incrementz }| { z}|{(n) (+ n 1))

result | {z }

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 42 / 58 Lisp Programming Overview Exercise 6: Function Definition

a b a ∧ b nand(a,b) 0 0 0 1 0 1 0 1 nand(a, b) , ¬(a ∧ b) 1 0 0 1 1 1 1 0

Pseudocode Common Lisp Procedure nand(a, b) 1 return ¬(a ∧ b);

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 43 / 58 Lisp Programming Overview Conditional IF IF Conditional execution based on a single test: (if TEST-EXPRESSION THEN-EXPRESSION [ELSE-EXPRESSION])

Pseudocode Common Lisp Procedure max(x, y) (defun max (x y) test 1 if x > y then (if (> x y) 2 return z }| { x; then clause else 3 x 4 return y; z}|{y )) else clause |{z} Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 44 / 58 Lisp Programming Overview Conditional COND COND Conditional execution of the first clause with a true test: (cond CLAUSES...) where each clause is of the form: (TEST EXPRESSIONS...)

Pseudocode Common Lisp Procedure sign(n) (defun sign (n) test result 1 if n > 0 then return 1 ; (cond ((> n 0) 1 ) 2 else if then return z }| { n < 0 −1 ; test resultz}|{ 3 else return 0 ; ((< n 0) −1 ) z }| { z}|{ test result ( t 0 ))) z}|{ z}|{

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 45 / 58 Lisp Programming Overview Exercise 7: Conditionals

Ternary NAND using IF, COND, and NOT,.

Truth Table If Cond a b c AND NAND 0 0 0 0 1 0 0 1 0 1 0 1 0 0 1 0 1 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 0 0 1 1 1 1 1 0

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 46 / 58 Lisp Programming Overview Local Variables LET, LET* LET and LET* create and initialize new local variables. LET operates in “parallel” and LET* operates sequentially.

Example (LET) Example (LET*)

( l e t ( ( a 1) ) ( l e t ( ( a 1) ) ( l e t ( ( a 2) ( l e t ∗ ( ( a 2) ( b a ) ) ( b a ) ) ( p r i n t ( l i s t a b ) ) ) ) ( p r i n t ( l i s t a b ) ) ) ) Output Output (2 1) (2 2)

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 47 / 58 Implementation Details 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 48 / 58 Implementation Details Data Types

Definition (Data type) Example A classification of data/objects based on how ◮ int the data/object is intended to or able to be use. ◮ float ◮ List The set of values a variable may take. ◮ String ◮ Structures: ◮ int × string ◮ float4 ◮ Function: ◮ int × int 7→ bool

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 49 / 58 Implementation Details Data Type Systems

Type Checking/Binding Type Enforcement Static: Check types at compile time Strong: Types are strictly enforced (statically) Weak: Can treat objects as different Dynamic: Check types at run time types (dynamically). (casting, “type punning”)

Example ◮ C: static/weak ◮ Python: dynamic/strong ◮ ML and Haskell: static/strong ◮ Lisp: dynamic (mostly) / strong

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 50 / 58 Implementation Details Machine Words – Representing Data word x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 031 unsigned 42 0x2a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 031 signed -42 0xffffffd6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 0 031 float 42. = 1.3125 ∗ 25 0x42280000 1 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 031

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 51 / 58 Implementation Details Words and Types

word 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 031 ? 0xc0490fd0 −1068953648 (signed) ? 0xc0490fd0 3226013648 (unsigned) ? 0xc0490fd0 −3.141590 (float) 0xc0490fd0 ? valid pointer

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 52 / 58 Implementation Details Type Tags Data Tag

1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 031

SBCL Tags (32-bit) data tag even fixnum Type Tag 0x180921FA × 000b (0x180921FA >> 2) z }| { z }| { Even Fixnum 000b 806503412 Odd Fixnum 100b Instance Pointer 001b List Pointer 011b Function Pointer 101b

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 53 / 58 Implementation Details Example: Tagged Storage

64 64 64 64 64 64 64-bit SBCL: bit bit bit bit bit bit 1 1 NIL Fixnum: (eq 1 1) t eq

Single Float: (eq 1.0s0 1.0s0) t 1.0 1.0 NIL eq

Double Float: (eq 1.0d0 1.0d0) nil NIL

eq 1.0d0 1.0d0

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 54 / 58 Implementation Details Manual Memory Management

malloc(n) free(ptr) 1. Find a free block of at least n bytes 1. Add block back to the free list(s) 2. If no such block, get more memory from the OS 3. Return pointer to the block

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 55 / 58 Implementation Details Garbage Collection Roots Heap CPU registers Global Variables r0 h0 r1 h4 ... h1 h6 rk−1 rk h2 h7 h5 h3

Local Variables Stack

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 56 / 58 Implementation Details McCarthy on Lisp https://youtu.be/KuU82i3hi8c?t=24m23s

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 57 / 58 Implementation Details References

◮ Peter Seibel. . http://www.gigamonkeys.com/book/ ◮ Common Lisp Hyperspec. http://www.lispworks.com/documentation/HyperSpec/Front/index.htm ◮ . ANSI Common Lisp. http://www.paulgraham.com/acl.html

Dantam (Mines CSCI-561) L02: Symbolic Computation and Common Lisp (Pre Lecture) Fall 2021 58 / 58