CS302: Paradigms of Programming Variations on the Scheme Interpreter - - PowerPoint PPT Presentation

cs302 paradigms of programming variations on the scheme
SMART_READER_LITE
LIVE PREVIEW

CS302: Paradigms of Programming Variations on the Scheme Interpreter - - PowerPoint PPT Presentation

CS302: Paradigms of Programming Variations on the Scheme Interpreter (Cont.) Manas Thakur Feb-June 2020 Graduate to Post-Graduate We can use the knowledge gained to experiment with programming language technology in several ways: 1. Extend


slide-1
SLIDE 1

CS302: Paradigms of Programming Variations on the Scheme Interpreter (Cont.)

Manas Thakur

Feb-June 2020

slide-2
SLIDE 2

Graduate to Post-Graduate

  • We can use the knowledge gained to experiment with

programming language technology in several ways:

  • 1. Extend the language with new syntactic constructs
  • 2. Explore various design choices in the space of programming

languages

  • 3. Design new programming languages themselves!

2

slide-3
SLIDE 3

3

  • 2a. Exploring design choices:


Lexical vs Dynamic Scoping

slide-4
SLIDE 4
  • What’s the value returned by g?
  • Lexical scoping: 0
  • Dynamic scoping: 1
  • Lexical scoping: Free variables are bound in the environment in

which the procedure was defined (closure).

  • Examples: Pascal, Ada, Scheme, Haskell, C and family.
  • Dynamic scoping: Free variables are bound the most recently

assigned value during program execution.

  • Examples: Original Lisp, Bash, LaTeX.

Lexical vs dynamic scoping

4

int x = 0; int f() { return x; } int g() { int x = 1; return f(); }

slide-5
SLIDE 5
  • What’s the value returned by g?
  • Lexical scoping: 1
  • Dynamic scoping: 2
  • Which language is this?
  • Homework: Find out which scoping is used in R.

Lexical vs dynamic scoping (Cont.)

5

x <- 1 f <- function(a) x + a g <- function() { x <- 2 f(0) } g()

slide-6
SLIDE 6

Discussion

  • Which one — lexical or dynamic scoping — is more natural?
  • Which one should be easier to implement?
  • Original Lisp had dynamic scoping!
  • Then why switch to lexical scoping in Scheme?

6

slide-7
SLIDE 7

Flashback

  • Recall the general sum procedure from the days we learnt about

higher order functions:

  • Usage:
  • One more (sum of nth powers):

7

(define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) (define (sum-cubes a b) (define (cube x) (expt x 3)) (sum cube a inc b)) (define (sum-powers a b n) (define (nth-power x) (expt x n)) (sum nth-power a inc b))

Notice the
 binding

slide-8
SLIDE 8

8

Why not dynamic scoping?

slide-9
SLIDE 9

Problem with dynamic scoping

  • Say the programmer renamed b in the procedure sum to n:
  • Whoops! Problem irukke.

9

(define (sum term a next n) (if (> a n) (+ (term a) (sum term (next a) next n)))) (define (sum-powers a b n) (define (nth-power x) (expt x n)) (sum nth-power a inc b))

n got bound to b

slide-10
SLIDE 10

10

Then why dynamic scoping?

slide-11
SLIDE 11

Classwork (for me!)

  • Can we reuse nth-power?
  • Lexical scoping: No.
  • Dynamic scoping: Yes!

11

(define (product-powers a b n) (define (nth-power x) (expt x n)) (product nth-power a inc b)) (define (sum-powers a b n) (define (nth-power x) (expt x n)) (sum nth-power a inc b)) (define (sum-powers a b n) (sum nth-power a inc b)) (define (product-powers a b n) (product nth-power a inc b)) (define (nth-power x) (expt x n))

  • Write an analogous procedure to multiply the nth powers.
slide-12
SLIDE 12

12

Now comes fun!

slide-13
SLIDE 13

Make Scheme dynamically scoped

  • Remarkably simple: Just extend the correct environment!

13

In fact, we don’t need to bind the environment while creating procedure objects!

slide-14
SLIDE 14

Scoping in action

14

slide-15
SLIDE 15

Assignment 3 Part A (7 Marks)

  • Extend the eval/apply interpreter to have both lexical and

dynamic scoping.

  • A parameter may be declared to be dynamically scoped as:
  • A variable may be referenced dynamically as:
  • Referencing a variable that wasn’t declared as dynamically

scoped defaults to lexical scoping.

  • Interpreter implementation from textbook will be provided to you.

15

(define (foo a (dynamic b)) ( ... ) (define (foo a (dynamic b)) (+ a (dynamic-ref b))

slide-16
SLIDE 16

Insight of the month (semester?)

  • How do we solve the limitation of lexical scoping?

16

(define (product-powers a b n) (product (make-exp n) a inc b)) (define (sum-powers a b n) (sum (make-exp n) a inc b)) (define make-exp (lambda (n) (lambda (x) (expt x n))))

  • What’s the powerful feature without which you couldn’t solve

this problem?

  • Ability to return functions as values.
  • In general, granting first-class citizenship to functions!

By being good in
 computing science :-)