Semantics of PLs via Interpreters: Getting Started

27
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti

description

Semantics of PLs via Interpreters: Getting Started. CS784: Programming Languages Prabhaker Mateti. Study …. EOPL3 Chapter 3: 3.1, 3.2 source code: chapter3/let- lang /* 1722 data- structures.scm 4415 drscheme-init.scm 1450 environments.scm 2057 interp.scm 1501 lang.scm - PowerPoint PPT Presentation

Transcript of Semantics of PLs via Interpreters: Getting Started

Page 1: Semantics of PLs via Interpreters: Getting Started

Semantics of PLs via Interpreters: Getting Started

CS784: Programming LanguagesPrabhaker Mateti

Page 2: Semantics of PLs via Interpreters: Getting Started

2

Study …

• EOPL3 Chapter 3: 3.1, 3.2• source code: chapter3/let-lang/*• 1722 data-structures.scm• 4415 drscheme-init.scm• 1450 environments.scm• 2057 interp.scm• 1501 lang.scm• 1857 tests.scm• 2007 top.scm

CS784 (PM)

Page 3: Semantics of PLs via Interpreters: Getting Started

3

EOPL3 Source Code• All the code in the book, as a zip archive (211 Kb).

• http://www.eopl3.com/allcode.zip• tested under PLT Scheme versions 372 and 4.2. • It should run unchanged in Racket 5.1.3 .• To make it more consistent with current Racket code, remove

(module name (lib "eopl.ss" "eopl") .... )around each file, and write #lang eoplat the top of the file instead.

• If the module says(module name mzscheme .... ) use #lang mzschemeinstead (NOT #lang racket -- the syntax for require/provide is slightly different).

CS784 (PM)

Page 4: Semantics of PLs via Interpreters: Getting Started

4

Interpreter vs Compiler

Input: Abstract Syntax of a program (AST)Output:“Meaning” of the programAn interpreter carries out the meaning of a

program.A compiler transforms a program in one

language into a program in a lower-level language preserving the meaning.

CS784 (PM)

Page 5: Semantics of PLs via Interpreters: Getting Started

5

Fig 3.2 The LET language

Concrete/Abstract Syntax Comments on the syntax• Six kinds of expressions.• Assume Scheme syntax for id

and number.• Minus is used as a function

name.• Uses std math notation for apply.• then-body and else-body are

single expressions.• exp1 is the first expression and

body is the second expression.

CS784 (PM)

Page 6: Semantics of PLs via Interpreters: Getting Started

6

Examples in the LET language1. let x = 5 in -(x,3)2. let z = 5

in let x = 3in let y = -(x,1) % here x = 3in let x = 4in -(z, -(x,y)) % here x = 4

3. let x = 7in let y = 2in let y =let x = -(x,1)in -(x,y)in -(-(x,8), y)

CS784 (PM)

Page 7: Semantics of PLs via Interpreters: Getting Started

7

scan&parse

(scan&parse "-(55, -(x,11))")#(struct:a-program #(struct:diff-exp #(struct:const-exp 55) #(struct:diff-exp #(struct:var-exp x) #(struct:const-exp 11))))

CS784 (PM)

Page 8: Semantics of PLs via Interpreters: Getting Started

8

Values Of The Language

• Expressed values are the values of exp.• Denoted values are bound to variables.• Not always the same, in general.• In LET, they are the same:• ExpVal = Int + Bool• DenVal = Int + Bool

CS784 (PM)

Page 9: Semantics of PLs via Interpreters: Getting Started

9

Functions in our LET-Interpreter

constructors1. const-exp : Int →Exp2. zero?-exp : Exp → Exp3. if-exp : Exp × Exp × Exp → Exp4. diff-exp : Exp × Exp → Exp5. var-exp : Var → Exp6. let-exp : Var × Exp × Exp → Expobserver7. value-of : Exp × Env → ExpVal

CS784 (PM)

Page 10: Semantics of PLs via Interpreters: Getting Started

10

Spec of value-of

1. (value-of (const-exp n) ρ) = (num-val n)2. (value-of (var-exp var) ρ)

= (apply-env ρ var)3. (value-of (diff-exp exp1 exp2) ρ)

= (num-val(-(expval->num (value-of exp1 ρ))(expval->num (value-of exp2 ρ))))

Three more remaining.

CS784 (PM)

Page 11: Semantics of PLs via Interpreters: Getting Started

11

NotationNotation Denotes env[] Empty env[ var = val ] (extend-env var val )«exp» AST for expression expn (num-val n), val (expval->num val).

Note: [[n]] =n

CS784 (PM)

Page 12: Semantics of PLs via Interpreters: Getting Started

12

Figure 3.3 An example calculation

CS784 (PM)

Page 13: Semantics of PLs via Interpreters: Getting Started

13

Specifying Programs

• initial environment[i=1, v=5, x=10]

• (value-of-program exp)= (value-of exp [i=[1],v=[5], x=

[10]])

CS784 (PM)

Page 14: Semantics of PLs via Interpreters: Getting Started

14

Spec of value-of

4. (value-of (if-exp exp1 exp2 exp3) ρ)= (if (expval->bool (value-of exp1

ρ))(value-of exp2 ρ)(value-of exp3 ρ))

5. (value-of (let-exp var exp1 body) ρ)= (value-of body

[var = (value-of exp1 ρ)] ρ)CS784 (PM)

Page 15: Semantics of PLs via Interpreters: Getting Started

15

Fig 3.4 A conditional expression

CS784 (PM)

Page 16: Semantics of PLs via Interpreters: Getting Started

16

Eval of an example let

CS784 (PM)

Page 17: Semantics of PLs via Interpreters: Getting Started

17

Eval of an example let (contd)

CS784 (PM)

Page 18: Semantics of PLs via Interpreters: Getting Started

18

Fig 3.6 LET lang AST def

(define-datatype program program?(a-program(exp1 expression?)))

(define-datatype expression expression? (const-exp

(num number?))

(diff-exp (exp1 expression?) (exp2 expression?))

(zero?-exp (exp1 expression?))

(if-exp (exp1 expression?) (exp2 expression?) (exp3 expression?))

(var-exp (var identifier?))

(let-exp (var identifier?) (exp1 expression?) (body expression?)))

CS784 (PM)

Page 19: Semantics of PLs via Interpreters: Getting Started

19

(init-env) = [i= 1,v= 5,x=10]

(define init-env (lambda () (extend-env 'i (num-val 1) (extend-env 'v (num-val 5) (extend-env 'x (num-val 10) (empty-env))))))

CS784 (PM)

Page 20: Semantics of PLs via Interpreters: Getting Started

20

Fig 3.7a Expressed values

• (define-datatype expval expval?(num-val

(num number?))(bool-val

(bool boolean?)))

CS784 (PM)

Page 21: Semantics of PLs via Interpreters: Getting Started

21

Fig 3.7b Expressed values

• (define expval->num (lambda (val)

(cases expval val(num-val (num) num)(else (report-expval-

extractor-error’num val)))))

CS784 (PM)

Page 22: Semantics of PLs via Interpreters: Getting Started

22

Fig 3.7c Expressed values

• (define expval->bool(lambda (val)

(cases expval val (bool-val (bool) bool) (else (report-expval-extractor-

error `bool val)))))

CS784 (PM)

Page 23: Semantics of PLs via Interpreters: Getting Started

23

value-of: Exp × Env → ExpVal

CS784 (PM)

Page 24: Semantics of PLs via Interpreters: Getting Started

24

run, value-of-program

CS784 (PM)

Page 25: Semantics of PLs via Interpreters: Getting Started

25

(value-of (zero?-exp exp1) env)

CS784 (PM)

Page 26: Semantics of PLs via Interpreters: Getting Started

26

(value-of (if-exp e1 e2 e3) env)

CS784 (PM)

Page 27: Semantics of PLs via Interpreters: Getting Started

27

(value-of (let-exp …) env)

CS784 (PM)