CS603 Programming Language Organization

13
Lecture 11 Spring 2003 Department of Computer Science CS603 Programming Language Organization

description

CS603 Programming Language Organization. Lecture 11 Spring 2003 Department of Computer Science. Overview. Questions Finishing µ-Scheme µ-Scheme Operational Semantics Reading for next time. HOF for Polymorphism: List of Functions. ->(val mk-set-ops (lambda (eqfun) (list2 - PowerPoint PPT Presentation

Transcript of CS603 Programming Language Organization

Page 1: CS603 Programming Language Organization

Lecture 11Spring 2003

Department of Computer Science

Lecture 11Spring 2003

Department of Computer Science

CS603 Programming Language OrganizationCS603 Programming

Language Organization

Page 2: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

• Questions• Finishing µ-Scheme• µ-Scheme Operational Semantics• Reading for next time

• Questions• Finishing µ-Scheme• µ-Scheme Operational Semantics• Reading for next time

OverviewOverview

Page 3: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

HOF for Polymorphism:List of Functions

HOF for Polymorphism:List of Functions

->(val mk-set-ops (lambda (eqfun) (list2 (lambda (x s) ; member?

(exists? ((curry eqfun) x) s)) (lambda (x s) ; add-element

(if (exists? ((curry eqfun) x) s) s (cons x s)))))->(val list-of-al-ops (mk-set-ops =alist?))

->(val mk-set-ops (lambda (eqfun) (list2 (lambda (x s) ; member?

(exists? ((curry eqfun) x) s)) (lambda (x s) ; add-element

(if (exists? ((curry eqfun) x) s) s (cons x s)))))->(val list-of-al-ops (mk-set-ops =alist?))

Pair Up: • Draw a diagram (of environment, cons cells and closures) for (val list-of-al-ops (mk-set-ops =alist?))

Pair Up: • Draw a diagram (of environment, cons cells and closures) for (val list-of-al-ops (mk-set-ops =alist?))

Page 4: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

HOF for Polymorphism:List of Functions

HOF for Polymorphism:List of Functions

-> (val al-member? (car list-of-al-ops))-> (val al-add-element (cadr list-of-al-ops))So uses will look like:->(val emptyset ‘())->(val s (al-add-element ‘((U Thant)((I Ching))) emptyset)(((U Thant) (I Ching)))->(val s (al-add-element ‘((E coli)(I Ching)) s))(((E coli) (I Ching)) ((U Thang) (I Ching)))

Page 5: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

A polymorphic, higher-order sort

A polymorphic, higher-order sort

->(define mk-insertion-sort (lt) (letrec ( (insert (lambda (x l) (if (null? l) (list1 x) (if (lt x (car l)) (cons x l) (cons (car l) (insert x (cdr l))))))) (sort (lambda (l) (if (null? l) ’() (insert (car l) (sort (cdr l))))))) sort))

->(define mk-insertion-sort (lt) (letrec ( (insert (lambda (x l) (if (null? l) (list1 x) (if (lt x (car l)) (cons x l) (cons (car l) (insert x (cdr l))))))) (sort (lambda (l) (if (null? l) ’() (insert (car l) (sort (cdr l))))))) sort))

Page 6: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

A polymorphic, higher-order sort (cont.)

A polymorphic, higher-order sort (cont.)

->(val sort< (mk-insertion-sort <))->(val sort> (mk-insertion-sort >))->(sort< ‘(6 9 1 7 4 3 8 5 2 10))(1 2 3 4 5 6 7 8 9 10)->(sort> ‘(6 9 1 7 4 3 8 5 2 10))(10 9 8 7 6 5 4 3 2 1)->(define pair< (p1 p2) (or (< (car p1) (car p2) (and (= (car p1) (car p2)) (< (cadr p1) (cadr p2)))))->((mk-insertion-sort pair<) ‘((4 5) (2 9) (3 3) (8 1) (2 7)))((2 7) (2 9) (3 3) (4 5) (8 1))

->(val sort< (mk-insertion-sort <))->(val sort> (mk-insertion-sort >))->(sort< ‘(6 9 1 7 4 3 8 5 2 10))(1 2 3 4 5 6 7 8 9 10)->(sort> ‘(6 9 1 7 4 3 8 5 2 10))(10 9 8 7 6 5 4 3 2 1)->(define pair< (p1 p2) (or (< (car p1) (car p2) (and (= (car p1) (car p2)) (< (cadr p1) (cadr p2)))))->((mk-insertion-sort pair<) ‘((4 5) (2 9) (3 3) (8 1) (2 7)))((2 7) (2 9) (3 3) (4 5) (8 1))

Page 7: CS603 Programming Language Organization

µ-Scheme Concrete Syntax

µ-Scheme Concrete Syntax

toplevel ::= exp | (use file-name) | (val variable-name exp) | (define function-name (formals) exp)exp ::= literal | variable-name | (if exp exp exp) | (while exp exp) | (set variable-name exp) | (begin {exp}) | (exp {exp}) | (let-keyword ({(variable-name exp)}) exp) | (lambda (formals) exp) | primitivelet-keyword ::= let | let* | letrec

toplevel ::= exp | (use file-name) | (val variable-name exp) | (define function-name (formals) exp)exp ::= literal | variable-name | (if exp exp exp) | (while exp exp) | (set variable-name exp) | (begin {exp}) | (exp {exp}) | (let-keyword ({(variable-name exp)}) exp) | (lambda (formals) exp) | primitivelet-keyword ::= let | let* | letrec

Page 8: CS603 Programming Language Organization

µ-Scheme Concrete Syntax (cont.)

µ-Scheme Concrete Syntax (cont.)

formals ::= {variable-name}literal ::= integer | #t | #f | ‘S-exp | (quote S-exp)S-exp ::= literal | symbol-name | ({S-exp})primitive ::= + | - | * | / | = | < | > | print | error | car | cdr | cons | number? | symbol? | pair? | null? | boolean? | procedure?integer ::= sequence of digits, possibly prefixed with a minus sign*-name ::= sequence of characters not an integer and not containing (, ), ;, or whitespace

formals ::= {variable-name}literal ::= integer | #t | #f | ‘S-exp | (quote S-exp)S-exp ::= literal | symbol-name | ({S-exp})primitive ::= + | - | * | / | = | < | > | print | error | car | cdr | cons | number? | symbol? | pair? | null? | boolean? | procedure?integer ::= sequence of digits, possibly prefixed with a minus sign*-name ::= sequence of characters not an integer and not containing (, ), ;, or whitespace

Page 9: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

Abstract Syntax and Values

Abstract Syntax and Values

Pair Up: • Draw an AST for (val list-of-al-ops (mk-set-ops =alist?))

Pair Up: • Draw an AST for (val list-of-al-ops (mk-set-ops =alist?))

• Same as last time, for toplevel, exp, lambda, and values, a separate structure, with a union element for each production

• Same as last time, for toplevel, exp, lambda, and values, a separate structure, with a union element for each production

Page 10: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

• Differences from Impcore• Single environment , not three• Environments bind names to locations,

rather than to values.• Environments must be captured and it

is never safe to mutate a binding in an environment.

• State of abstract machine evaluating expression e is <e, , >

• Differences from Impcore• Single environment , not three• Environments bind names to locations,

rather than to values.• Environments must be captured and it

is never safe to mutate a binding in an environment.

• State of abstract machine evaluating expression e is <e, , >

Operational SemanticsOperational Semantics

Page 11: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

• We write <e, , >⇓<v, ’> to mean• The result of evaluating expression e

in environment when the state of the store is , is the value v

• The evaluation may update the store to produce a new state ’

• We write <e, , >⇓<v, ’> to mean• The result of evaluating expression e

in environment when the state of the store is , is the value v

• The evaluation may update the store to produce a new state ’

Operational Semantics (cont.)

Operational Semantics (cont.)

Page 12: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

µ-Scheme Judgmentsµ-Scheme JudgmentsPair Up: • Give me the tags for every production in toplevel and exp• Write up the corresponding judgment• Write judgment for (positive? 0) given:(define curry (f) (lambda (x) (lambda (y) (f x

y)))(val zero? ((curry =) 0))(zero? 0)

Pair Up: • Give me the tags for every production in toplevel and exp• Write up the corresponding judgment• Write judgment for (positive? 0) given:(define curry (f) (lambda (x) (lambda (y) (f x

y)))(val zero? ((curry =) 0))(zero? 0)

Page 13: CS603 Programming Language Organization

©2003 Joel Jones©2003 Joel Jones

• Sections 3.12–3.14• Sections 3.12–3.14

Reading for Next TimeReading for Next Time