CSC 530 Lecture Notes Week 2 Discussion of Assignment 1 Topics from - - PDF document

csc 530 lecture notes week 2 discussion of assignment 1
SMART_READER_LITE
LIVE PREVIEW

CSC 530 Lecture Notes Week 2 Discussion of Assignment 1 Topics from - - PDF document

CSC530-W02-L2 Slide 1 CSC 530 Lecture Notes Week 2 Discussion of Assignment 1 Topics from the Lisp Primer Topics from Part 1 of the Readings CSC530-W02-L2 Slide 2 I. Reading this week -- papers 1-4 on functional programming. II. Discussion


slide-1
SLIDE 1

CSC530-W02-L2 Slide 1

CSC 530 Lecture Notes Week 2 Discussion of Assignment 1 Topics from the Lisp Primer Topics from Part 1 of the Readings

slide-2
SLIDE 2

CSC530-W02-L2 Slide 2

  • I. Reading this week -- papers 1-4 on

functional programming.

  • II. Discussion of Assignment 1
  • A. What are you doing here?
  • B. The read-xeval-print-loop
slide-3
SLIDE 3

CSC530-W02-L2 Slide 3

Assignment 1, cont’d

(defun read-xeval-print-loop () (prog (alist result) (setq alist ’(nil)) loop (princ "X>") (setq result (xeval (read) alist)) (princ (car result)) (setq alist (cadr result)) (terpri)(terpri) (go loop) ) )

  • C. The meat of the matter is the

alist.

slide-4
SLIDE 4

CSC530-W02-L2 Slide 4

  • III. "Naive" alist layout
  • A. A list of bindings.
  • B. General form

( name value )

  • C. For Lisp, two categories:
slide-5
SLIDE 5

CSC530-W02-L2 Slide 5

Alist layout, cont’d

  • 1. variable binding

( var-name data-value )

  • 2. Function binding is a triple of the

form

( function-name formal-parms function-body )

slide-6
SLIDE 6

CSC530-W02-L2 Slide 6

Alist layout, cont’d

  • D. Distinguish variable versus function

bindings by lengths.

  • E. In Assignment 1, bindings created and

modified in three ways:

  • 1. Variable bindings by (xsetq x v)
  • 2. Function bindings by (xdefun f

parms body)

  • 3. Function call bindings by (f a1 ...

an)

slide-7
SLIDE 7

CSC530-W02-L2 Slide 7

Assignment 1, cont’d

  • F. Addition, removal, and search done

LIFO.

  • G. What is naive about the organization --

does not accurately represent scoping rules of Common Lisp.

  • H. The bottom line for Assignment 1
  • 1. same evaluation results as mine
  • 2. may differ in alist dump
slide-8
SLIDE 8

CSC530-W02-L2 Slide 8

On to the Lisp Primer

slide-9
SLIDE 9

CSC530-W02-L2 Slide 9

  • IV. Selected primer topics
  • A. Let’s hav

e a look

  • B. Complete details in Lisp ref man
slide-10
SLIDE 10

CSC530-W02-L2 Slide 10

  • 1. Overview

Compared to C, the major diffs are

  • syntax
  • interpretive environment
  • lack of explicit type declarations
slide-11
SLIDE 11

CSC530-W02-L2 Slide 11

Overview, cont’d Major similarities include:

  • Program structure and scoping.
  • Function invocation, conditionals
  • Underlying similarity in data struc-

tures

slide-12
SLIDE 12

CSC530-W02-L2 Slide 12

  • 2. An Introductory Session

% ˜/classes/530/bin/gcl GCL (GNU Common Lisp) ... >(+ 2 2) 4 >(defun TwoPlusTwo () (+ 2 2)) TWOPLUSTWO >(defun TwoPlusXPlusY (x y) (+ 2 x y)) TWOPLUSXPLUSY >(TwoPlusXPlusY 10 20) 32 >(load "avg.l") Loading avg.l Finished loading avg.l T

slide-13
SLIDE 13

CSC530-W02-L2 Slide 13

>(avg ’(1 2 3 4 5)) 3 >(avg ’(a b c)) Error: C is not of type NUMBER. Fast links are on: ... Error signalled by +. Broken at +. Type :H for Help. >>:q Top level. >(help) GCL (GNU Common Lisp) >(bye) Bye.

slide-14
SLIDE 14

CSC530-W02-L2 Slide 14

  • 3. Lexical and Syntactic Structure
  • Very simple -- atoms and lists.
  • Atoms are:

identifier integer or real double-quoted string constants t and nil

  • A list is zero or more elements in

matching parentheses

slide-15
SLIDE 15

CSC530-W02-L2 Slide 15

3.1. Expr and Function Call Syntax Lisp C (+ a b) a + b (f 10 20) f(10, 20) (< (+ a b) (- c d)) (a + b) < (c - d)

  • General format:

(function-name arg1 ... argn)

slide-16
SLIDE 16

CSC530-W02-L2 Slide 16

Function Call Syntax, cont’d

  • Function call evaluated as:
  • 1. function-name checked.
  • 2. Each argi evaluated.
  • 3. Each argi bound call-by-value disci-

pline

  • 4. Body of function evaluated
  • Quite similar to C
slide-17
SLIDE 17

CSC530-W02-L2 Slide 17

3.2. The Quote Function

  • Consider

>(defun f (x) ... ) > (defun g (x) ...)

  • Giv

en these, consider

(f (g 10))

  • Alternatively, consider

(f ’(g 10))

slide-18
SLIDE 18

CSC530-W02-L2 Slide 18

3.3. No main Function Necessary

  • Simply defun and load
  • Any defined function can be called.
slide-19
SLIDE 19

CSC530-W02-L2 Slide 19

4. Arithmetic, Logical, Conditional Expressions

(+ numbers) (1+ number) (- numbers) (1- number) (* numbers) (/ numbers)

See ref man for others.

slide-20
SLIDE 20

CSC530-W02-L2 Slide 20

4.1. Type Predicates

(atom expr) (listp expr) (null expr) (numberp expr) (stringp expr) (functionp expr)

See ref man for others.

slide-21
SLIDE 21

CSC530-W02-L2 Slide 21

4.2. The cond Conditional (cond ( (test-expr1) expr1 ... exprj ) . . . ( (test-exprn) expr1 ... exprk )

slide-22
SLIDE 22

CSC530-W02-L2 Slide 22

4.3. Equality Functions = numeric string= string equal general expr eq same-object

slide-23
SLIDE 23

CSC530-W02-L2 Slide 23

  • 5. Function Definitions

Lisp: C: (defun f (x y) int f(int x,y) { (plus x y) return x + y ) }

slide-24
SLIDE 24

CSC530-W02-L2 Slide 24

  • 6. Lists and List Operations
  • List is the basic data structure.
  • Lisp does support others -- but who

cares.

slide-25
SLIDE 25

CSC530-W02-L2 Slide 25

6.1. Three Basic List Ops Operation Meaning car first element cdr ev erything except first element cons construct a new list (es- sentially)

slide-26
SLIDE 26

CSC530-W02-L2 Slide 26

The tail recursion idiom

(defun PrintListElems (l) (cond ( (not (null l)) (print (car l)) (PrintListElems(cdr l)) ) ) )

Comparable to in C:

void PrintArrayElems(int a[], int n) { for (i=0; i<n; i++) printf("%d0, a[i]);

slide-27
SLIDE 27

CSC530-W02-L2 Slide 27

6.2. cXr forms

  • General form:

cXr

where the X can be replaced by two, three, or four a’s and/or d’s

  • E.g., (cadr L)
slide-28
SLIDE 28

CSC530-W02-L2 Slide 28

6.3. Other Useful List Ops

(append lists) (list elements) (member element list) (length list) (reverse list) (nth n list) (nthcdr n list) (assoc key alist)

slide-29
SLIDE 29

CSC530-W02-L2 Slide 29

(sort list)

slide-30
SLIDE 30

CSC530-W02-L2 Slide 30

6.4. Dot Notation

(a b c d).

Figure 1: Internal representation of the list a b c d nil a b Internal representatino of Figure 2:

(cons ’a ’b).

slide-31
SLIDE 31

CSC530-W02-L2 Slide 31

  • 7. Building C-Like Data Structures

7.1. Arrays

  • Trivially represented as lists
  • Implementation of array indexing

(defun my-nth (n l) (cond ( (< n 0) nil ) ( (eq n 0) (car l) ) ( t (my-nth (- n 1) (cdr l)) ) ) )

slide-32
SLIDE 32

CSC530-W02-L2 Slide 32

7.2. Structs

  • General format:

( (field-name1 value1) ... (field-name1 value1) )

  • Functions to access and modify

(defun getfield (field-name struct) (cond ( (eq struct nil) nil ) ( (eq field-name (caar struct)) (car struct) ) ( t (getfield field-name (cdr struct)) ) ) )

slide-33
SLIDE 33

CSC530-W02-L2 Slide 33

Structs, contd’

(defun setfield (field-name value struct) (cond ( (null struct) nil ) ( (eq field-name (caar struct) ) (cons (cons (caar struct) (list value)) (cdr struct)) ) ( t (cons (car struct) (setfield field-name value (cdr struct))) ) ) )

slide-34
SLIDE 34

CSC530-W02-L2 Slide 34

7.3. Linked Lists and Trees 7.3.1. Linked Lists

  • Just plain lists in Lisp
  • At underlying dot-notation level,

Lisp lists are implemented using pointers

slide-35
SLIDE 35

CSC530-W02-L2 Slide 35

7.3.2. N-Ary Tr ees

  • General form

( root subtree1 ... subtreen )

  • For example

(a (b (c d) e) (f g h) i)

a b f i c d e g h Lisp: Corresponding Graphic:

slide-36
SLIDE 36

CSC530-W02-L2 Slide 36

  • 8. A Multi-Function Example
  • Merge sort
  • Illustrates typical Lisp style
slide-37
SLIDE 37

CSC530-W02-L2 Slide 37

  • 9. Basic Input and Output

(read [stream]) (print expr [stream]) (prin1 expr [stream]) (princ expr [stream]) (terpri [stream]) (open UNIX-filename)

slide-38
SLIDE 38

CSC530-W02-L2 Slide 38

  • 10. Programs as Data
  • Lists and function calls are syntacti-

cally identical

  • Programs and data can be manipu-

lated interchangeably

  • Any expr can be treated equally well

as program or data

slide-39
SLIDE 39

CSC530-W02-L2 Slide 39

10.1. Eval

  • Callable eval same as in read-eval-

print loop

  • Any leg

al Lisp expression can be executed 10.2. Apply

  • "Junior" function slightly less pow-

erful than eval

  • E.g.,

(apply ’+ ’(2 2))

slide-40
SLIDE 40

CSC530-W02-L2 Slide 40

produces 4.

slide-41
SLIDE 41

CSC530-W02-L2 Slide 41

  • 11. Scoping with Let

Lisp: C: (let { ( i int i; (j 10) int j = 10; (k 20) ) int k = 20; expr1 stmt1 ... ... exprn stmtn ) }

  • Also let*
slide-42
SLIDE 42

CSC530-W02-L2 Slide 42

  • 12. Imperative Features
  • Features thus far comprise the func-

tional subset.

  • Imperative features of Lisp make it

more "C-like".

slide-43
SLIDE 43

CSC530-W02-L2 Slide 43

12.1. Assignment Statements

  • setq is Lisp assignment
  • E.g.,

(setq x (+ 2 2))

  • More general form is setf
  • E.g.,

(setf (cadr x) 10)

slide-44
SLIDE 44

CSC530-W02-L2 Slide 44

12.2. Scope and Binding

  • No explicit var decls in Lisp.
  • Site of binding defines scope.

Top-level binding means global Local binding (i.e., inside defun) means local Function parms are local

  • In Lisp free means not bound in

the current scope.

slide-45
SLIDE 45

CSC530-W02-L2 Slide 45

12.3. Prog (prog ((var1 val1))... (varn valn) expr1 ... exprk)

Lisp: C: (prog { ((i 10) int i = 10; (j 20.5) float j = 20.5; (k "xyz")) char* k = "xyz" (setq i (+ i 1)) i = i + 1; (setq j (1+ j)) j += 1; (print (+ i j)) printf("%d0, i + j); ) }

slide-46
SLIDE 46

CSC530-W02-L2 Slide 46

Prog, cont’d

  • return returns from prog
  • Don’t confuse Lisp’s return with

C’s

  • go is a standard goto, e.g.

(defun read-eval-print-loop () (prog () loop (princ ">") (print (eval (read))) (terpri) (go loop)

slide-47
SLIDE 47

CSC530-W02-L2 Slide 47

) )

slide-48
SLIDE 48

CSC530-W02-L2 Slide 48

12.4. Iterative Control

  • General form:

(do ((var1 val1 rep1) ... (varn valn repn)) exit-clause expr1 ... exprk)

  • exit-clause form:

([test [test-expr1 ... test-exprm]])

  • Similar to C for loop, except test is
slide-49
SLIDE 49

CSC530-W02-L2 Slide 49

an until. 12.5. Destructive List Operations

(rplaca cons-cell expr) (rplacd cons-cell expr) (nconc lists) (setf cons-cell expr)

slide-50
SLIDE 50

CSC530-W02-L2 Slide 50

Destructive, cont’d

>(setq x-safe ’(a b c)) (A B C) >(setq y-safe x-safe) (A B C) >(setq x-safe (cons ’x (cdr x-safe))) (X B C) >y-safe (A B C)

slide-51
SLIDE 51

CSC530-W02-L2 Slide 51

Destructive, cont’d

>(setq x-unsafe ’(a b c)) (A B C) >(setq y-unsafe x-unsafe) (A B C) >(rplaca x-unsafe ’x) (X B C) >y-unsafe (X B C)

slide-52
SLIDE 52

CSC530-W02-L2 Slide 52

Destructive, cont’d >(setf (cadr y-unsafe) ’y) Y >x-unsafe (X Y C) >y-unsafe (X Y C)

slide-53
SLIDE 53

CSC530-W02-L2 Slide 53

12.6. Call-by-Reference

  • Destructive list ops make it possible
  • E.g.,

(defun dsetfield (field-name value struct) (setf (cdr (assoc field-name struct)) (list value)) )

slide-54
SLIDE 54

CSC530-W02-L2 Slide 54

12.7. Pointers Fully Revealed

  • Consider:

>(setq x ’(a b c)) (A B C) >(defun f (i j k) (setf (cdddr k) (cdr j)) (setf (cadr j) i) (setf (cdr j) k) --primer typo-- (setq z k) nil ) F >(f x (cdr x) ’(a b c)) NIL

slide-55
SLIDE 55

CSC530-W02-L2 Slide 55

Pointers Revealed, cont’d

  • Three snapshots:
  • a. after assignment to x, before f

called

  • b. after f called, parameters bound,

before body executed

  • c. after execution of f
slide-56
SLIDE 56

CSC530-W02-L2 Slide 56

Pointers Revealed, cont’d

a b c nil x

a

slide-57
SLIDE 57

CSC530-W02-L2 Slide 57

Pointers Revealed, cont’d

a b c nil a b c nil x i j k

b

slide-58
SLIDE 58

CSC530-W02-L2 Slide 58

Pointers Revealed, cont’d

a b c a b nil x z

c

slide-59
SLIDE 59

CSC530-W02-L2 Slide 59

Pointers Revealed, cont’d

  • An important point -- non-destruc-

tive ops are more efficient than they might appear

  • E.g.,

(cons huge-list1 huge-list2)

takes the same time as any cons

  • It just copies pointers
slide-60
SLIDE 60

CSC530-W02-L2 Slide 60

  • V. Types of languages Lisp can be
  • A. Pure Applicative
  • B. Single-Assignment Applicative
  • C. Large-Grain Applicative
  • D. Imperative
  • E. Nasty Imperative
slide-61
SLIDE 61

CSC530-W02-L2 Slide 61

More on Functional Programming

slide-62
SLIDE 62

CSC530-W02-L2 Slide 62

  • VI. "Compelling" motivations
  • A. Referential transparency, aka no

side effects

  • B. Verifiability
  • C. Concurrency
  • D. Other techniques for efficient evalu-

ation, including lazy evaluation and memoization.

slide-63
SLIDE 63

CSC530-W02-L2 Slide 63

  • VII. Referential transparency
  • A. An expression always has the same

value

  • B. I.e., "side effect free"
  • C. Important implications:
  • 1. Any expr need only be evaluated
  • nce in a given context
  • 2. Non-nested exprs can be evalu-

ated in parallel

slide-64
SLIDE 64

CSC530-W02-L2 Slide 64

Referential transparency, cont’d

  • D. Any data modification operator vio-

lates referential transparency

  • 1. Side-effects lead to different val-

ues in the same context

  • 2. E.g.,
slide-65
SLIDE 65

CSC530-W02-L2 Slide 65

Referential transparency, cont’d

>(setq z 0) >(defun expr (x) (setq z (+ z x))) expr >(defun f (x y) (+ x y z)) f >(f (expr 1) (expr 1)) 5 >(f (expr 1) (expr 1))

slide-66
SLIDE 66

CSC530-W02-L2 Slide 66

11

slide-67
SLIDE 67

CSC530-W02-L2 Slide 67

  • VIII. Benefits of side-effect-free pro-

gramming ...

slide-68
SLIDE 68

CSC530-W02-L2 Slide 68

  • IX. Program verifiability
  • A. Outline:
  • 1. Provide a spec of input, P
  • 2. Provide a spec of output, Q
  • 3. Prove P{program}Q
  • B. P a function of all possible inputs,

Q of all possible outputs.

  • C. Also, language must be formally
slide-69
SLIDE 69

CSC530-W02-L2 Slide 69

defined.

slide-70
SLIDE 70

CSC530-W02-L2 Slide 70

  • X. Concurrency models
  • A. Consider

>(defun f (x y z) ... ) >(f (big1 ...) (big2 ...) (big3 ...))

  • B. bigi are costly computations.
  • C. A basic form of concurrency is par-

allel eval of function args

  • D. Another model is dataflow.
slide-71
SLIDE 71

CSC530-W02-L2 Slide 71

  • XI. Dataflow evaluation
  • A. Expr eval as tree traversal
  • B. Sequential, depth-first
  • C. In dataflow model:
  • 1. One operator per processor
  • 2. Processor awaits inputs
  • 3. Proceeds independently
  • 4. Outputs results
slide-72
SLIDE 72

CSC530-W02-L2 Slide 72

Dataflow, cont’d

* +

  • a

b c d

  • a. Sequential tree-based model.
slide-73
SLIDE 73

CSC530-W02-L2 Slide 73

* +

  • a

b c d

  • b. Concurrent dataflow model.
slide-74
SLIDE 74

CSC530-W02-L2 Slide 74

  • XII. Lazy evaluation
  • A. Some terminology
  • 1. Synonymous: lazy, strict,

demand-driven.

  • 2. Synonymous: eager, non-strict,

data-driven.

  • 3. More in future lectures.
slide-75
SLIDE 75

CSC530-W02-L2 Slide 75

Lazy eval, cont’d

  • B. Normal in most languages is

"eager"

  • 1. Recall fundamental rules for

Lisp function eval:

  • a. Eval args
  • b. Eval function body
  • 2. I.e., eval all args, even if not

necessary.

slide-76
SLIDE 76

CSC530-W02-L2 Slide 76

Lazy eval, cont’d

  • C. Basic idea of lazy eval:
  • 1. Dont eval args before body
  • 2. Rather, wait until arg is used
  • D. Consider
slide-77
SLIDE 77

CSC530-W02-L2 Slide 77

Lazy eval, cont’d

>(defun stupid-but-lazy (x y) (cond ( (= 1 1) x ) ( t y ) ) ) >(stupid-but-lazy 1 (some-hugely-lengthy-\ computation))

slide-78
SLIDE 78

CSC530-W02-L2 Slide 78

Lazy eval, cont’d

  • E. Not intelligent, but advantage is

clear

  • F. Can we be lazy in an imperative lan-

guage?

  • 1. In general, no.
  • 2. We cannot guarantee no side

effects.

  • 3. E.g.,
slide-79
SLIDE 79

CSC530-W02-L2 Slide 79

Lazy eval, cont’d

>(defun way-stupid-but-lazy (x y) (cond ( (= 1 1) z ) ;z free ( t y ) ) ) >(way-stupid-but-lazy 1 (setq z 1))

slide-80
SLIDE 80

CSC530-W02-L2 Slide 80

  • XIII. How lazy do we get?
  • A. When must we perform arg eval?
  • B. What language primitives are lazy?
  • C. Consider rules for a lazy Lisp:
slide-81
SLIDE 81

CSC530-W02-L2 Slide 81

How Lazy, cont’d

  • 1. cond, cons, car, and cdr are

lazy.

  • 2. User-defined functions are lazy.
  • 3. print and arithmetic/logical
  • ps are eager.
  • 4. Stop being lazy when eager

function "demands" a value, or when we eval a literal.

slide-82
SLIDE 82

CSC530-W02-L2 Slide 82

How Lazy, cont’d

  • D. Consider:

L>(defun (lazy+ (x y) (+ x y))) lazy+ L>(lazy+ 2 (lazy+ 2 (lazy+ 2 2))) 8

  • E. Trace ...
slide-83
SLIDE 83

CSC530-W02-L2 Slide 83

How Lazy, cont’d

  • F. Important to understand order of

eval.

  • 1. With eager eval, an inside-out
  • rder.
  • 2. With lazy eval (notes typo),
  • rder is outside-in.
slide-84
SLIDE 84

CSC530-W02-L2 Slide 84

  • XIV. Lazy eval of infinite functions
  • A. Can cope effectively
  • 1. Consider

>(defun not-so-stupid-but-lazy (x y) (cond ( (= 1 1) x ) ( t y ) ) ) >(defun infinite-computation () (prog () loop (go loop) ) ) >(not-so-stupid-but-lazy 1 (infinite-computation)) 1

slide-85
SLIDE 85

CSC530-W02-L2 Slide 85

Lazy infinite, cont’d

  • B. Potentially infinite generator func-

tions

  • 1. Consider

>(defun all-ints () (cons 0 (1+ (all-ints)))) all-ints >(nth 2 (all-ints)) 2

  • 2. In this example:
slide-86
SLIDE 86

CSC530-W02-L2 Slide 86

Generator functions, cont’d

  • a. What scheme for lazy eval

could work?

  • b. How exactly does finite

execution proceed?

  • c. What does GCL do with this

example?

  • d. How would you implement

this?

slide-87
SLIDE 87

CSC530-W02-L2 Slide 87

  • XV. Lazy dataflow
  • A. A natural idea.
  • B. A node begins with just enough

inputs.

  • C. A radical approach is fully demand-

driven eval.

  • 1. All nodes start immediately.
  • 2. A node demands from input line
  • nly when needed.
slide-88
SLIDE 88

CSC530-W02-L2 Slide 88

  • XVI. Memoization
  • A. Referential transparency implies

expr eval only once.

  • B. An eval strategy:
  • 1. First time, compute the function.
  • 2. Store result for given args in a

table -- the memo.

  • 3. On subsequent evals, look up

args, return memo if found.

slide-89
SLIDE 89

CSC530-W02-L2 Slide 89

Memoization, cont’d

  • C. Memoization in imperative lan-

guages?

  • 1. Answer same as for lazy eval
  • 2. Viz., must guarantee side-effect-

free behavior.

  • D. We’ll consider in an upcoming

assignment.

slide-90
SLIDE 90

CSC530-W02-L2 Slide 90

  • XVII. Memoization in dataflow
  • A. A number of interesting approaches
  • B. One is to allow dataflow lines to

remember.

slide-91
SLIDE 91

CSC530-W02-L2 Slide 91

  • XVIII. To think about
  • A. Do lazy evaluation and memoiza-

tion make sense together?

  • 1. If so, how?
  • 2. If not, why not?
  • B. More to come.
slide-92
SLIDE 92

CSC530-W02-L2 Slide 92

  • XIX. Concluding thoughts
  • A. Concepts extremely influential.
  • B. E.g., C compilers implement mem-
  • ization and lazy eval.
  • C. So-called "modern" practices based
  • n functional concepts:
slide-93
SLIDE 93

CSC530-W02-L2 Slide 93

Concluding thoughts, cont’d

  • 1. Lessening use of global vars
  • 2. Defining vars and args constant

where possible

  • 3. Formally specifying behavior
  • D. Ongoing research continues to pio-

neer new concepts.