Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1...

Slides:



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

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
EOPL3: Section 3.3 PROC and App B: SLLGEN
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
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:
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Abstract Syntax cs7100 (Prasad) L7AST.
Additional Scheme examples
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Chapter 2: Data Abstraction 2.1 Specifying Data via Interfaces
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
6.001: Structure and Interpretation of Computer Programs
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Implementing Recursion
Original material by Eric Grimson
2.3 Representation Strategies for Data Types
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Lecture 15: Tables and OOP
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
3.7 Variable Assignment Recall instance variables in Python:
Lecture 16: Tables and OOP
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Data Mutation Primitive and compound data mutators set! for names
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Chapter 2: Data Abstraction 2.1 Specifying Data via Interfaces
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
6.001 SICP Data abstractions
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Presentation transcript:

Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1... s k ) '( v 1... v k ) [f] ) = [g] where g(s') = v i if s' = s i for some i, 1 ≤ i ≤ k =f (s') otherwise 2.3 Representation Strategies for Data Types

Example: > (define dxy-env (extend-env '(d x) '(6 7) (extend-env '(y) '(8) (empty-env)))) > (apply-env dxy-env 'x) 7

A constructor builds a datatype: empty-env, extend-env An observer extracts info from a datatype: apply-env We will consider three representations for environments:  Procedural  Abstract Syntax Tree  “Alternative” (list-based)

In Scheme, procedures (functions) are first-class objects: can be passed to / returned from other procedures, and stored in data structures E.g., we can represent numbers as procedures: [0] = s. z.z [1] = s. z.(s z) [2] = s. z. (s (s z)) [succ]= n. s. z. (s (n s z)) Now compute (succ 0).... Procedural representation of environments

Environment as procedure: (define empty-env (lambda () (lambda (sym) (eopl:error 'apply-env “No binding for ~s” sym)))) An environment is a function that takes a symbol and returns its assoicated value. Empty environment defines no symbols. Therefore empty environment returns an error if you try to apply it to a symbol

Environment as procedure: (define apply-env (lambda (env sym) (env sym))) So apply-env is really just “syntactic sugar”

Environment as procedure: (define extend-env (lambda (syms vals env) (lambda (sym) ; get position of sym in syms (let ((pos (list-find-pos sym syms))) (if (number? pos) ; we found it! ; return value at that pos (list-ref vals pos) ; not found; use current env (apply-env env sym))))))

Examples: > (define xy-env (extend-env '(x y) '(3 4) (empty-env))) > xy-env # > (apply-env xy-env 'y) 4 > (apply-env xy-env 'z) apply-env: No binding for z

Environment as Abstract Syntax Tree: ::= (empty-env) empty-env-record ::= (extend-env( { }* ) ( { }* ) ) extended-env-record (syms vals env

extended-env-record syms vals env empty-env-record (x y)(3 4) Environment as AST:

(define-datatype environment environment? (empty-env-record) (extended-env-record (syms (list-of symbol?)) (vals (list-of scheme-value?)) (envenvironment?))) (define scheme-value? (lambda (v) #t) > ((list-of number?) '(1 2 3)) #t Environment as AST:

(define empty-env (lambda () (empty-env-record))) (define extend-env (lambda (syms vals env) (extended-env-record syms vals env))) Environment as AST:

(define apply-env (lambda (env sym) (cases environment env (empty-env-record () (eopl:error...)) (extended-env-record (syms vals env) (let ((pos (list-find-pos sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym))))))) Environment as AST:

Alternative (List) Representation Actually the most intuitive: ::= () ::= (( { }* ) ( { }* ) ) (define empty-env (lambda () '())) (define extend-env (lambda (syms vals env) (cons (list syms vals) env)))

Alternative Representation (define apply-env (lambda (env sym) (if (null? env) (eopl:error...) (let (syms (car (car env))) (vals (cadr (car env))) (env (cdr env))) (let ((pos (rib-find-pos sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym))))))) (define rib-find-pos list-find-pos)

Alternative Representation: Ribcage dx67 8 y (define dxy-env (extend-env '(d x) '(6 7) (extend-env '(y) '(8) (empty-env))))