Catriel Beeri Pls/Winter 2004/5 environment1 1 The Environment Model  Introduction and overview  A look at the execution model  Dynamic scoping  Static.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
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.
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
Catriel Beeri Pls/Winter 2004/5 functional-language 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
Catriel Beeri Pls/Winter 2004/5 types 1 Types Three chapters:  General introduction to types  A simple static monomorphic type system (with type checking.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Catriel Beeri Pls/Winter 2004/05 types 65  A type-checking algorithm The task: (since we start with empty H, why is the goal not just E?) The rule set.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Catriel Beeri Pls/Winter 2004/5 environment 19 II. Frames and frame structures Frame – set of bindings generated together in a binding generation event.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Imperative Programming
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Compiler Construction
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Basic Semantics Associating meaning with language entities.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Programming Languages and Paradigms Imperative Programming.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
SE424 Languages with Context A Block Structured Language.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
7. Runtime Environments Zhang Zhizheng
CS162 Week 4 Kyle Dewey. Overview Reactive imperative programming in a nutshell Reactive imperative programming implementation details.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
5.3 EVALUATION OF POSTFIX EXPRESSION For example, consider the evaluation of the following postfix expression using stacks: abc+d*f/- where, a=6, b=3,
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Operational Semantics of Scheme
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Run-Time Environments Chapter 7
Type Checking, and Scopes
Implementing Subprograms
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Bindings, Scope, and Extent
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Closures and Streams cs7100(Prasad) L11Clos
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Chapter 3 Discussion Pages
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Catriel Beeri Pls/Winter 2004/5 environment1 1 The Environment Model  Introduction and overview  A look at the execution model  Dynamic scoping  Static scoping  Some details of implementation  Liveness, storage allocation and language restrictions

Catriel Beeri Pls/Winter 2004/5 environment1 2 Associations of variables with values are created by: definition elaboration/function application How are they handled? Substitution model: –Substitute value for variable uses in scope (  program is modified) Environment model: –Create dynamic binding, store in environment (  no change of program) –Look up uses in environment when needed Introduction and overview

Catriel Beeri Pls/Winter 2004/5 environment1 3 Bindings and environments : A (dynamic) value binding = a (run-time) association of a variable with a value A value: any run-time entity that can be bound to variables and manipulated by programmers: Values of base types Variables Function values Composite date (lists, tuples,..) …. We say simply binding

Catriel Beeri Pls/Winter 2004/5 environment1 4 A (value) environment (two equivalent views): A finite mapping from variables to value A set of bindings (at most one per variable) Terminology & Operations: Lookup (apply map) : E(x) dom(E) : the domain of E Create “small” environment (when bindings are generated) Merge with priority :

Catriel Beeri Pls/Winter 2004/5 environment1 5 A new environment is generated when new bindings for some variables are created When execution leaves the scope of these variables the previous environment is restored The environment used at a point: the current environment  Program execution consists of code segments with a fixed current environment, separated by environment change events

Catriel Beeri Pls/Winter 2004/5 environment1 6 Evaluation with fixed environment : Many program phrases are evaluated w/o changing the environment: Atomic or composite values References Operation application If, while, … A lambda expression The only one that involves the environment is the evaluation of a variable – variable resolution

Catriel Beeri Pls/Winter 2004/5 environment1 7 Evaluation with fixed environment –rules : And similarly for if, cell allocation, assignment, …. Variable resolution: determine a value for a a variable use by a look-up –rule (env-nat-var)

Catriel Beeri Pls/Winter 2004/5 environment1 8 The rule for lambda depends on how functions are applied; for now: Function application, let, letrec – their evaluation involves environment changes (at entrance/exit) let* -- sequential non-recursive block = nested lets letrec* -- sequential recursive block = nested letrecs  We discuss let and letrec (but examples use let*)

Catriel Beeri Pls/Winter 2004/5 environment1 9 Environment changing constructs : These involve Evaluation of the expressions E i to v i ECreation of new environment E that contains the bindings (and more) EEvaluation of B w.r.t. E Upon exit, restoration of previous environment Entrance to new environment, exit --- matching pairs in a computation

Catriel Beeri Pls/Winter 2004/5 environment1 10 Details: For function application & let, the arguments/defining expressions are evaluated in the current environment (before new environment is formed) But, for a letrec, the defining expressions are in the scope of the defined variables, must be evaluated in the new environment – a cycle Q1: how are the defining expressions evaluated, and new environment created, for a letrec? (temp) A: define only functions, the current environment is then irrelevant

Catriel Beeri Pls/Winter 2004/5 environment1 11 E After bindings are generated, in current environment E, need to create new environment, with new bindings and also with previous bindings For let/letrec : (evaluation in place) But, a function is applied in position unrelated to its definition; should the base environment be from the definition, or from the use Q2: which environment is the basis for the the new environment for a function application?

Catriel Beeri Pls/Winter 2004/5 environment1 12

Catriel Beeri Pls/Winter 2004/5 environment1 13  A look at the execution model I. Activations and the activation stack Activation: computation segment between matching environment change events ~ evaluation of the body of function/let/letrec (term typically used for functions, we generalize to all regions) Entrance to activation ~ binding generation and new environment creation Exit from activation ~ restoration of previous environment

Catriel Beeri Pls/Winter 2004/5 environment1 14 Each activation: Is associated with an environment (created for it!) Executes some segments of code Additional code may be executed in nested activations, with their own environments! Activations entrance/exit -- LIFO  Activation stack (a conceptual structure ) The dynamic parent of an activation – the one below it on the stack

Catriel Beeri Pls/Winter 2004/5 environment1 15 An activation is : live --- is on the stack (after it has terminated, it will never be on the stack again) current – top of the stack – executing every other live activation is suspended An activation stops being live ~ its environment is exited Span of activation – from entrance to exit (when it dies) The current environment = the one associated with the current activation a one-to-one correspondence

Catriel Beeri Pls/Winter 2004/5 environment1 16 In an activation, some expression/statements (s) are evaluated These may contain blocks/function calls  evaluation includes Direct evaluation (in activation itself) : – Evaluation of literals, variable resolution, arithmetic ops, if, assignment, loops, sequencing, … –Returning a value to calling activation Code evaluated in nested activations Remember to distinguish evaluation from direct evaluation!

Catriel Beeri Pls/Winter 2004/5 environment1 17 In the next example, we assume that the base environment for a function application is the one current at the place of application – rule (env-nat-applic-fun-t)

Catriel Beeri Pls/Winter 2004/5 environment1 18 Example: (start with empty environment) Activation directly evaluated environment of evaluation The value 7 is returned, one activation at a time, to act 0