Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Catriel Beeri Pls/Winter 2004/5 environment 19 II. Frames and frame structures Frame – set of bindings generated together in a binding generation event.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
ISBN Chapter 15 Functional Programming Languages.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
Operational Semantics of Scheme
CS314 – Section 5 Recitation 10
Additional Scheme examples
Functional Programming Languages
6.001 SICP Object Oriented Programming
The interpreter.
Introduction to Scheme
Evaluating Algebraic Expressions
The Environment Model*
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
Env. Model Implementation
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Scope of Variables.
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Scoping and Binding of Variables
Lecture 16: Tables and OOP
Lecture 26: The Metacircular Evaluator Eval Apply
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
Lecture 13: Assignment and the Environment Model (EM)
CSE 341 Lecture 11 b closures; scoping rules
topics interpreters meta-linguistic abstraction eval and apply
Changing Data: (Continued)
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((= op ’numer) n) ((= op ’denom) d) (else: (error ”Bogus operation")))))) ((rat 3 4) ’numer) => 3

In Java: String m = new String("CS212");... if (m.equals("CS211")) {... if (m == "CS211") {...

New Special Form — set! (set! identifier expression)

set! trashes the substitution model: (define put-in-bowl (lambda (thing) (set! thing (cons thing '(in a bowl))) thing)) (put-in-bowl 'goldfish) => (goldfish in a bowl)

Which variable does set! set? (define x 'barney) => x (let ((x 'purple) (y (set! x 'monster))) x) => ??? x => ???

Which variable does set! set? (define x 'barney) => x (let* ((x 'purple) (y (set! x 'monster))) x) => monster x => barney

An example not involving set! (define x 10) (define foo (lambda () x)) (let ((x 15)) (foo)) => ???

An example not involving set! (define x 10) (define foo (lambda () x)) (let ((x 15)) (foo)) => 10

Static vs. Dynamic Scoping Static scoping: free variables resolved in the environment of the function definition Dynamic scoping: free variables resolved in the environment of the function call Closure: when a function object is created, the environment is packaged with it

Referential Transparency replace equal values with equal values set! destroys referential transparency

The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are resolved in Scheme (and in almost any other programming language.) An expression only has a value with respect to an environment

Binding identifier : value Frame unordered set of bindings Environment ordered list of frames

x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff)  global environment (top level frame) (machine code for adding numbers)

The Lookup Rule the value of an identifier x is the value associated with x in the first (lowest) frame containing a binding for x undefined if there aren't any

Define rule To evaluate (define ident expr): evaluate expr in the current environment add a binding to the top-level frame (the global environment) Set! rule To evaluate (set! ident expr): evaluate expr in the current environment look up ident using lookup rule change that binding to value of expr

x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff) x:10 +:{add} y:20 d:12 z:30 x:() w:foobar x:oh y:(list of stuff) (define d (+ 5 7)) beforeafter

x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff) x:10 +:{add} y:20 z:30 x:() w:foobar x:my y:(list of stuff) (set! x 'my) beforeafter