2.3 Representation Strategies for Data Types

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 (-
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
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.
מבוא מורחב - שיעור 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.
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...
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
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.
מבוא מורחב שיעור 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.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Abstract Syntax cs7100 (Prasad) L7AST.
Additional Scheme examples
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
Chapter 2: Data Abstraction 2.1 Specifying Data via Interfaces
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
5.4.1: Implementation Method Invocation
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Lecture 15: Tables and OOP
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
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
Extending our interpreted language with Scheme’s set!
Streams, Delayed Evaluation and a Normal Order Interpreter
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:
Chapter 3: Environment-Passing Interpreters
6.001 SICP Data abstractions
Assignments cs7100(Prasad) L13Assg.
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
Presentation transcript:

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

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)

Procedural representation of environments 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] = ls.lz.z [1] = ls.lz.(s z) [2] = ls.lz. (s (s z)) [succ] = ln.ls.lz. (s (n s z)) Now compute (succ 0) ....

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))) #<procedure:13:7> > (apply-env xy-env 'y) 4 > (apply-env xy-env 'z) apply-env: No binding for z

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

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

Environment as AST: (define-datatype environment environment? (empty-env-record) (extended-env-record (syms (list-of symbol?)) (vals (list-of scheme-value?)) (env environment?))) (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)))))))

Alternative (List) Representation Actually the most intuitive: <env-rep> ::= () ::= (({<symbol>}*) ({<value>}*) <env-rep>) (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)

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