Plt-2002-2 10/12/2015 3.3-1 Data Abstraction Programming Language Essentials 2nd edition Chapter 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

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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:
Metacircular Evaluation SICP Chapter 4 Mark Boady.
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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
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
Plt /8/ Inductive Sets of Data Programming Language Essentials 2nd edition Chapter 1.2 Recursively Specified Programs.
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...
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.
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.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
The environment-based operational semantics Chapter
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.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
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.
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
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Chapter 2: Data Abstraction 2
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
2.3 Representation Strategies for Data Types
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 16: Tables and OOP
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types

plt /12/ Goals abstract datatype makes program independent of concrete representation look at implementation strategies environment maps a finite set of Scheme symbols to values taken from all Scheme values e.g., values of variables e.g., lexical addresses of symbols

plt /12/ Environment Interface (empty-env) constructor, represents an empty environment (apply-env environment symbol) observer, applies a representation to a symbol, returns associated value (extend-env symbol-list value-list environment) constructor, represents an environment which returns given values for given symbols and defaults to old environment otherwise

plt /12/ Example (define dxy-env (extend-env '(d x) '(6 7) (extend-env '(y) '(8) (empty-env)))) (apply-env dxy-env 'x) has to return 7 $ cd code/2; make

plt /12/ Represent values as procedures (define empty-env ; returns function that fails (lambda () (lambda (s) (error 'apply-env "No binding" s)) ) (define extend-env ; returns function returning vals (lambda (syms vals env) ; or asks env (lambda (sym) (let ((pos (list-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym) ) ) ) ) ) (define apply-env (lambda (env sym) (env sym)) )

plt /12/ Searching a linear list (define list-find-position ; returns pos >= 0 or #f (lambda (s list) ; of s in list (list-index (lambda (x) (eqv? x s)) list) ) (define list-index ; returns pos >= 0 or #f (lambda (p? l) ; where p? is true in l (cond ((null? l) #f) ((p? (car l)) 0) (else (let ((list-index-r (list-index p? (cdr l)))) (if (number? list-index-r) (+ list-index-r 1) #f ) ) )

plt /12/ How-to abstract using procedures if client uses data-type only by way of values returned by procedure calls: (1)for each lambda expression returning such a value: embed it into a constructor which takes the free variables as parameters. (2)replace the lambda expression in the client by invoking the constructor. (3)define an observer apply-x to invoke the result returned by the constructor. (4)replace every value by invoking apply-x.

plt /12/ Represent values using syntax trees env-rep: '(' 'empty-env' ')' empty-env-record | '(' 'extend-env' '(' 'Symbol'* ')' '(' 'Value'* ')' env-rep ')' extended-env-record (syms vals env)

plt /12/ Abstract syntax tree (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))

plt /12/ Constructors (define empty-env (lambda () (empty-env-record) ) (define extend-env (lambda (syms vals env) (extended-env-record syms vals env) )

plt /12/ Observer (define apply-env (lambda (env sym) (cases environment env (empty-env-record () (error 'apply-env "No binding" sym)) (extended-env-record (syms vals env) (let ((pos (list-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym) ) ) )

plt /12/ How-to abstract using syntax trees if client uses data-type only by way of values returned by procedure calls: (1)for each lambda expression returning such a value: create one variant with one field per free variable in lambda expression. (2)use define-datatype with those variants. (3)replace the lambda expression in the client by invoking the constructor. (4)define an observer apply-x based on cases where the bodies of the lambda expressions are the bodies of the consequents of cases. (5)replace every value by invoking apply-x.

plt /12/ Represent environment with lists env-rep: '()' | '(' '(' '(' 'Symbol'* ')' '.' '#(' 'Value'* ')' ')' '.' env-rep ')' ribcage c c b b a a e e d d

plt /12/ Constructors (define empty-env (lambda () '()) ) (define extend-env (lambda (syms vals env) (cons (cons syms (list->vector vals)) env ) ) ) $ cd code/2; make 2.3.4

plt /12/ Observer (define apply-env (lambda (env sym) (if (null? env) (error 'apply-env "No binding" sym) (let* ((syms (car (car env))) (vals (cdr (car env))) (pos (list-find-position sym syms)) ) (if (number? pos) (vector-ref vals pos) (apply-env (cdr env) sym) ) ) ) ) )