61A Lecture 27 Announcements Interpreting Scheme The Structure of - - PowerPoint PPT Presentation

61a lecture 27 announcements interpreting scheme the
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 27 Announcements Interpreting Scheme The Structure of - - PowerPoint PPT Presentation

61A Lecture 27 Announcements Interpreting Scheme The Structure of an Interpreter 4 The Structure of an Interpreter Eval Apply 4 The Structure of an Interpreter Eval Base cases: Apply 4 The Structure of an Interpreter Eval Base cases:


slide-1
SLIDE 1

61A Lecture 27

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Interpreting Scheme

slide-4
SLIDE 4

The Structure of an Interpreter

4

slide-5
SLIDE 5

The Structure of an Interpreter

4

Apply Eval

slide-6
SLIDE 6

The Structure of an Interpreter

4

Apply Eval Base cases:

slide-7
SLIDE 7

The Structure of an Interpreter

4

Apply Eval Base cases:

  • Primitive values (numbers)
slide-8
SLIDE 8

The Structure of an Interpreter

4

Apply Eval Recursive calls: Base cases:

  • Primitive values (numbers)
slide-9
SLIDE 9

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions

Base cases:

  • Primitive values (numbers)
slide-10
SLIDE 10

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)

Base cases:

  • Primitive values (numbers)
slide-11
SLIDE 11

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)

Base cases:

  • Primitive values (numbers)

Base cases:

  • Built-in primitive procedures
slide-12
SLIDE 12

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)

Base cases:

  • Primitive values (numbers)
  • Look up values bound to symbols

Base cases:

  • Built-in primitive procedures
slide-13
SLIDE 13

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)

Base cases:

  • Primitive values (numbers)
  • Look up values bound to symbols

Base cases:

  • Built-in primitive procedures
slide-14
SLIDE 14

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)
  • Eval(sub-expressions) of special forms

Base cases:

  • Primitive values (numbers)
  • Look up values bound to symbols

Base cases:

  • Built-in primitive procedures
slide-15
SLIDE 15

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)
  • Eval(sub-expressions) of special forms

Base cases:

  • Primitive values (numbers)
  • Look up values bound to symbols

Base cases:

  • Built-in primitive procedures

Recursive calls:

  • Eval(body) of user-defined procedures
slide-16
SLIDE 16

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)
  • Eval(sub-expressions) of special forms

Base cases:

  • Primitive values (numbers)
  • Look up values bound to symbols

Base cases:

  • Built-in primitive procedures

Recursive calls:

  • Eval(body) of user-defined procedures
slide-17
SLIDE 17

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)
  • Eval(sub-expressions) of special forms

Base cases:

  • Primitive values (numbers)
  • Look up values bound to symbols

Base cases:

  • Built-in primitive procedures

Recursive calls:

  • Eval(body) of user-defined procedures

Requires an environment for symbol lookup

slide-18
SLIDE 18

The Structure of an Interpreter

4

Apply Eval Recursive calls:

  • Eval(operator, operands) of call expressions
  • Apply(procedure, arguments)
  • Eval(sub-expressions) of special forms

Base cases:

  • Primitive values (numbers)
  • Look up values bound to symbols

Base cases:

  • Built-in primitive procedures

Recursive calls:

  • Eval(body) of user-defined procedures

Requires an environment for symbol lookup Creates a new environment each time a user-defined procedure is applied

slide-19
SLIDE 19

Special Forms

slide-20
SLIDE 20

Scheme Evaluation

6

slide-21
SLIDE 21

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

6

slide-22
SLIDE 22

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment

6

slide-23
SLIDE 23

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values

6

slide-24
SLIDE 24

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

6

slide-25
SLIDE 25

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>)

6

slide-26
SLIDE 26

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>) (lambda (<formal-parameters>) <body>)

6

slide-27
SLIDE 27

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>) (define <name> <expression>) (lambda (<formal-parameters>) <body>)

6

slide-28
SLIDE 28

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>) (define <name> <expression>) (lambda (<formal-parameters>) <body>) (<operator> <operand 0> ... <operand k>)

6

slide-29
SLIDE 29

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>) (define <name> <expression>) (lambda (<formal-parameters>) <body>) (<operator> <operand 0> ... <operand k>) Special forms are identified by the first list element

6

slide-30
SLIDE 30

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>) (define <name> <expression>) (lambda (<formal-parameters>) <body>) (<operator> <operand 0> ... <operand k>) Special forms are identified by the first list element

6

slide-31
SLIDE 31

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>) (define <name> <expression>) (lambda (<formal-parameters>) <body>) (<operator> <operand 0> ... <operand k>) Special forms are identified by the first list element Any combination that is not a known special form is a call expression

6

slide-32
SLIDE 32

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>) (define <name> <expression>) (lambda (<formal-parameters>) <body>) (<operator> <operand 0> ... <operand k>) Special forms are identified by the first list element Any combination that is not a known special form is a call expression (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))) ))

6

slide-33
SLIDE 33

Scheme Evaluation

The scheme_eval function choose behavior based on expression form:

  • Symbols are looked up in the current environment
  • Self-evaluating expressions are returned as values
  • All other legal expressions are represented as Scheme lists, called combinations

(if <predicate> <consequent> <alternative>) (define <name> <expression>) (lambda (<formal-parameters>) <body>) (<operator> <operand 0> ... <operand k>) Special forms are identified by the first list element Any combination that is not a known special form is a call expression (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))) )) (demo (list 1 2))

6

slide-34
SLIDE 34

Logical Forms

slide-35
SLIDE 35

Logical Special Forms

8

slide-36
SLIDE 36

Logical Special Forms

Logical forms may only evaluate some sub-expressions

8

slide-37
SLIDE 37

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)

8

slide-38
SLIDE 38

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)

8

slide-39
SLIDE 39

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))

8

slide-40
SLIDE 40

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))

The value of an if expression is the value of a sub-expression:

8

slide-41
SLIDE 41

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))

The value of an if expression is the value of a sub-expression:

  • Evaluate the predicate

8

slide-42
SLIDE 42

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))

The value of an if expression is the value of a sub-expression:

  • Evaluate the predicate
  • Choose a sub-expression: <consequent> or <alternative>

8

slide-43
SLIDE 43

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))

The value of an if expression is the value of a sub-expression:

  • Evaluate the predicate
  • Choose a sub-expression: <consequent> or <alternative>
  • Evaluate that sub-expression to get the value of the whole expression

8

slide-44
SLIDE 44

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))

The value of an if expression is the value of a sub-expression:

  • Evaluate the predicate
  • Choose a sub-expression: <consequent> or <alternative>
  • Evaluate that sub-expression to get the value of the whole expression

do_if_form

8

slide-45
SLIDE 45

Logical Special Forms

Logical forms may only evaluate some sub-expressions

  • If expression: (if <predicate> <consequent> <alternative>)
  • And and or: (and <e1> ... <en>), (or <e1> ... <en>)
  • Cond expression: (cond (<p1> <e1>) ... (<pn> <en>) (else <e>))

The value of an if expression is the value of a sub-expression:

  • Evaluate the predicate
  • Choose a sub-expression: <consequent> or <alternative>
  • Evaluate that sub-expression to get the value of the whole expression

do_if_form (Demo)

8

slide-46
SLIDE 46

Quotation

slide-47
SLIDE 47

Quotation

10

slide-48
SLIDE 48

Quotation

The quote special form evaluates to the quoted expression, which is not evaluated

10

slide-49
SLIDE 49

Quotation

The quote special form evaluates to the quoted expression, which is not evaluated (quote <expression>)

10

slide-50
SLIDE 50

Quotation

The quote special form evaluates to the quoted expression, which is not evaluated (quote <expression>)

10

(quote (+ 1 2)) (+ 1 2) evaluates to the 
 three-element Scheme list

slide-51
SLIDE 51

Quotation

The quote special form evaluates to the quoted expression, which is not evaluated (quote <expression>) The <expression> itself is the value of the whole quote expression

10

(quote (+ 1 2)) (+ 1 2) evaluates to the 
 three-element Scheme list

slide-52
SLIDE 52

Quotation

The quote special form evaluates to the quoted expression, which is not evaluated (quote <expression>) The <expression> itself is the value of the whole quote expression '<expression> is shorthand for (quote <expression>)

10

(quote (+ 1 2)) (+ 1 2) evaluates to the 
 three-element Scheme list

slide-53
SLIDE 53

Quotation

The quote special form evaluates to the quoted expression, which is not evaluated (quote <expression>) The <expression> itself is the value of the whole quote expression '<expression> is shorthand for (quote <expression>)

10

(quote (+ 1 2)) (+ 1 2) evaluates to the 
 three-element Scheme list (quote (1 2)) '(1 2) is equivalent to

slide-54
SLIDE 54

Quotation

The quote special form evaluates to the quoted expression, which is not evaluated (quote <expression>) The <expression> itself is the value of the whole quote expression '<expression> is shorthand for (quote <expression>) The scheme_read parser converts shorthand ' to a combination that starts with quote

10

(quote (+ 1 2)) (+ 1 2) evaluates to the 
 three-element Scheme list (quote (1 2)) '(1 2) is equivalent to

slide-55
SLIDE 55

Quotation

The quote special form evaluates to the quoted expression, which is not evaluated (quote <expression>) The <expression> itself is the value of the whole quote expression '<expression> is shorthand for (quote <expression>) The scheme_read parser converts shorthand ' to a combination that starts with quote

10

(Demo) (quote (+ 1 2)) (+ 1 2) evaluates to the 
 three-element Scheme list (quote (1 2)) '(1 2) is equivalent to

slide-56
SLIDE 56

Lambda Expressions

slide-57
SLIDE 57

Lambda Expressions

12

slide-58
SLIDE 58

Lambda Expressions

Lambda expressions evaluate to user-defined procedures

12

slide-59
SLIDE 59

Lambda Expressions

Lambda expressions evaluate to user-defined procedures (lambda (<formal-parameters>) <body>)

12

slide-60
SLIDE 60

Lambda Expressions

Lambda expressions evaluate to user-defined procedures (lambda (<formal-parameters>) <body>) (lambda (x) (* x x))

12

slide-61
SLIDE 61

Lambda Expressions

Lambda expressions evaluate to user-defined procedures (lambda (<formal-parameters>) <body>) (lambda (x) (* x x)) class LambdaProcedure: def __init__(self, formals, body, env): self.formals = formals self.body = body self.env = env

12

slide-62
SLIDE 62

Lambda Expressions

Lambda expressions evaluate to user-defined procedures (lambda (<formal-parameters>) <body>) (lambda (x) (* x x)) class LambdaProcedure: def __init__(self, formals, body, env): self.formals = formals self.body = body self.env = env

12

A scheme list of symbols

slide-63
SLIDE 63

Lambda Expressions

Lambda expressions evaluate to user-defined procedures (lambda (<formal-parameters>) <body>) (lambda (x) (* x x)) class LambdaProcedure: def __init__(self, formals, body, env): self.formals = formals self.body = body self.env = env

12

A scheme list of symbols A scheme list of expressions

slide-64
SLIDE 64

Lambda Expressions

Lambda expressions evaluate to user-defined procedures (lambda (<formal-parameters>) <body>) (lambda (x) (* x x)) class LambdaProcedure: def __init__(self, formals, body, env): self.formals = formals self.body = body self.env = env

12

A scheme list of symbols A scheme list of expressions A Frame instance

slide-65
SLIDE 65

Frames and Environments

13

slide-66
SLIDE 66

Frames and Environments

A frame represents an environment by having a parent frame

13

slide-67
SLIDE 67

Frames and Environments

A frame represents an environment by having a parent frame Frames are Python instances with methods lookup and define

13

slide-68
SLIDE 68

Frames and Environments

A frame represents an environment by having a parent frame Frames are Python instances with methods lookup and define In Project 4, Frames do not hold return values

13

slide-69
SLIDE 69

Frames and Environments

A frame represents an environment by having a parent frame Frames are Python instances with methods lookup and define In Project 4, Frames do not hold return values

g: Global frame y z 3 5

13

slide-70
SLIDE 70

Frames and Environments

A frame represents an environment by having a parent frame Frames are Python instances with methods lookup and define In Project 4, Frames do not hold return values

g: Global frame y z 3 5 f1: [parent=g] x z 2 4

13

slide-71
SLIDE 71

Frames and Environments

A frame represents an environment by having a parent frame Frames are Python instances with methods lookup and define In Project 4, Frames do not hold return values

g: Global frame y z 3 5 f1: [parent=g] x z 2 4

13

(Demo)

slide-72
SLIDE 72

Define Expressions

slide-73
SLIDE 73

Define Expressions

15

slide-74
SLIDE 74

Define Expressions

Define binds a symbol to a value in the first frame of the current environment.

15

slide-75
SLIDE 75

Define Expressions

Define binds a symbol to a value in the first frame of the current environment. (define <name> <expression>)

15

slide-76
SLIDE 76

Define Expressions

Define binds a symbol to a value in the first frame of the current environment. (define <name> <expression>)

  • 1. Evaluate the <expression>

15

slide-77
SLIDE 77

Define Expressions

Define binds a symbol to a value in the first frame of the current environment. (define <name> <expression>)

  • 1. Evaluate the <expression>
  • 2. Bind <name> to its value in the current frame

15

slide-78
SLIDE 78

Define Expressions

Define binds a symbol to a value in the first frame of the current environment. (define <name> <expression>)

  • 1. Evaluate the <expression>
  • 2. Bind <name> to its value in the current frame

(define x (+ 1 2))

15

slide-79
SLIDE 79

Define Expressions

Define binds a symbol to a value in the first frame of the current environment. (define <name> <expression>) Procedure definition is shorthand of define with a lambda expression

  • 1. Evaluate the <expression>
  • 2. Bind <name> to its value in the current frame

(define x (+ 1 2))

15

slide-80
SLIDE 80

Define Expressions

Define binds a symbol to a value in the first frame of the current environment. (define <name> <expression>) (define (<name> <formal parameters>) <body>) Procedure definition is shorthand of define with a lambda expression

  • 1. Evaluate the <expression>
  • 2. Bind <name> to its value in the current frame

(define x (+ 1 2))

15

slide-81
SLIDE 81

Define Expressions

Define binds a symbol to a value in the first frame of the current environment. (define <name> <expression>) (define (<name> <formal parameters>) <body>) (define <name> (lambda (<formal parameters>) <body>)) Procedure definition is shorthand of define with a lambda expression

  • 1. Evaluate the <expression>
  • 2. Bind <name> to its value in the current frame

(define x (+ 1 2))

15

slide-82
SLIDE 82

Applying User-Defined Procedures

16

slide-83
SLIDE 83

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure

16

slide-84
SLIDE 84

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure Evaluate the body of the procedure in the environment that starts with this new frame

16

slide-85
SLIDE 85

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure Evaluate the body of the procedure in the environment that starts with this new frame (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))

16

slide-86
SLIDE 86

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure Evaluate the body of the procedure in the environment that starts with this new frame (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s)))))

g: Global frame demo LambdaProcedure instance [parent=g]

16

slide-87
SLIDE 87

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure Evaluate the body of the procedure in the environment that starts with this new frame (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))))) (demo (list 1 2))

g: Global frame demo LambdaProcedure instance [parent=g]

16

slide-88
SLIDE 88

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure Evaluate the body of the procedure in the environment that starts with this new frame (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))))) (demo (list 1 2))

1

Pair

2

Pair

nil g: Global frame demo LambdaProcedure instance [parent=g]

16

slide-89
SLIDE 89

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure Evaluate the body of the procedure in the environment that starts with this new frame (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))))) (demo (list 1 2))

1

Pair

2

Pair

nil [parent=g] s g: Global frame demo LambdaProcedure instance [parent=g]

16

slide-90
SLIDE 90

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure Evaluate the body of the procedure in the environment that starts with this new frame (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))))) (demo (list 1 2))

1

Pair

2

Pair

nil [parent=g] s [parent=g] s g: Global frame demo LambdaProcedure instance [parent=g]

16

slide-91
SLIDE 91

Applying User-Defined Procedures

To apply a user-defined procedure, create a new frame in which formal parameters are bound to argument values, whose parent is the env attribute of the procedure Evaluate the body of the procedure in the environment that starts with this new frame (define (demo s) (if (null? s) '(3) (cons (car s) (demo (cdr s))))) (demo (list 1 2))

1

Pair

2

Pair

nil [parent=g] s [parent=g] s [parent=g] s g: Global frame demo LambdaProcedure instance [parent=g]

16

slide-92
SLIDE 92

Eval/Apply in Lisp 1.5

17

slide-93
SLIDE 93

Eval/Apply in Lisp 1.5

17