Semantics of PLs via Interpreters: Getting Started

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

Cs784(Prasad)L10Rec1 Implementing Recursion. cs784(Prasad)L10Rec2 let insufficient for recursion ( (let((fact(lambda (n) (if (zero? n) 1 (* n (fact (-
Closures & Environments CS153: Compilers Greg Morrisett.
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Tim Sheard Oregon Graduate Institute Lecture 4: Staging Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
CSE341: Programming Languages Lecture 17 Structs, Implementing Languages, Implementing Higher-Order Functions Dan Grossman Fall 2011.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
EOPL3: Section 3.3 PROC and App B: SLLGEN
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
CMSC 330: Organization of Programming Languages Operational Semantics.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Winter 2013.
Abstract Syntax cs7100 (Prasad) L7AST.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
Programming Languages Dan Grossman 2013
6.001 SICP Compilation Context: special purpose vs. universal machines
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Implementing Recursion
Env. Model Implementation
Emily Leland (Not Nick) Spring 2017
Case Study: Undefined Variables
2.3 Representation Strategies for Data Types
Chapter 4: Types.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
The Metacircular Evaluator
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
Procedures App B: SLLGEN 1.
3.7 Variable Assignment Recall instance variables in Python:
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Abstract Syntax cs7100 (Prasad) L7AST.
Adapted from slides by Nicholas Shahan and Dan Grossman
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
Compiler Construction
Assignments and Procs w/Params
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Compiler Construction
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Presentation transcript:

Semantics of PLs via Interpreters: Getting Started CS7100: Programming Languages Prabhaker Mateti 1

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 CS7100 (PM) 2

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 . at the top of the file instead. (NOT #lang racket -- the syntax for require/provide is slightly different). CS7100 (PM) 3

Interpreter vs Compiler Input: Abstract Syntax of a program (AST) Output:“Meaning” of the program An 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. CS7100 (PM) 4 4

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. Beware: minus is used as a function name, and uses std math notation for apply. CS7100 (PM) 5 5

Examples in the LET language let x = 5 in -(x,3) in -(z, -(x,y)) % here x = 4 in -(-(x,8), y) CS7100 (PM) 6 6

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

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 CS7100 (PM) 8 8

Functions in our LET-Interpreter constructors const-exp : Int →Exp zero?-exp : Exp → Exp if-exp : Exp × Exp × Exp → Exp diff-exp : Exp × Exp → Exp var-exp : Var → Exp let-exp : Var × Exp × Exp → Exp observer value-of : Exp × Env → ExpVal CS7100 (PM) 9 9

Spec of value-of (value-of (const-exp n) ρ) = (num-val n) = (apply-env ρ var) (expval->num (value-of exp2 ρ)))) Three more remaining. Note the Scheme minus. CS7100 (PM) 10 10

Notation Notation Denotes  env [] Empty env [ var = val ]  (extend-env var val ) «exp» AST for expression exp n (num-val n), val (expval->num val). Note: [[n]] =n CS7100 (PM) 11 11

Figure 3.3 An example calculation CS7100 (PM) 12 12

Specifying Programs initial environment [i=1, v=5, x=10] = (value-of exp [i=[1],v=[5], x= [10]]) CS7100 (PM) 13 13

Spec of value-of (value-of exp3 ρ)) [var = (value-of exp1 ρ)] ρ) CS7100 (PM) 14 14

Fig 3.4 A conditional expression CS7100 (PM) 15 15

Eval of an example let CS7100 (PM) 16 16

Eval of an example let (contd) CS7100 (PM) 17 17

Fig 3.6 LET lang AST def (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?) (body expression?))) CS7100 (PM) 18 18

(init-env) = [i= 1,v= 5,x=10] (define init-env (lambda () (extend-env 'i (num-val 1) 'v (num-val 5) 'x (num-val 10) (empty-env)))))) CS7100 (PM) 19 19

Fig 3.7a Expressed values (bool boolean?))) CS7100 (PM) 20 20

Fig 3.7b Expressed values ’num val))))) CS7100 (PM) 21 21

Fig 3.7c Expressed values (else (report-expval-extractor-error `bool val))))) CS7100 (PM) 22 22

value-of: Exp × Env → ExpVal CS7100 (PM) 23 23

run, value-of-program CS7100 (PM) 24 24

(value-of (zero?-exp exp1) env) CS7100 (PM) 25 25

(value-of (if-exp e1 e2 e3) env) CS7100 (PM) 26 26

(value-of (let-exp …) env) CS7100 (PM) 27 27