Scope Chapter Ten Modern Programming Languages.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Programming Languages and Paradigms
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.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
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.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
5/15/2015IT 3271 Polymorphism (Ch 8) n A functions that is not fixed is polymorphic n Applies to a wide variety of language features n Most languages have.
Chapter TenModern Programming Languages1 Scope. Chapter TenModern Programming Languages2 Reusing Names n Scope is trivial if you have a unique name for.
Scope Chapter TenModern Programming Languages, 2nd ed.1.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Scope.
Functional Programming Element of Functional Programming.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
CPS 506 Comparative Programming Languages Names, Scope, Expression.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Programming Languages and Paradigms Imperative Programming.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Chapter 6 Methods Chapter 6 - Methods.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
Chapter TenModern Programming Languages1 Scope. Chapter TenModern Programming Languages2 Reusing Names Scope is trivial when all names are unique But.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Dan Grossman 2013 ML Expressions and Variable Bindings.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Advanced Programming in C
Polymorphism (Ch 8) Scope (Ch 10) Who are you? What do you do now?
Names and Attributes Names are a key programming language feature
Principles of programming languages 4: Parameter passing, Scope rules
Memory Locations For Variables
Scope, Visibility, and Lifetime
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Binding Times Binding is an association between two things Examples:
Abstract data types Programming Language Design and Implementation
Recursive Procedures and Scopes
Chapter 3 Discussion Pages
Methods Scope How are names handled?
Presentation transcript:

Scope Chapter Ten Modern Programming Languages

Outline Definitions and scope Scoping with blocks Scoping with labeled namespaces Scoping with primitive namespaces Dynamic scoping Chapter Ten Modern Programming Languages

Definitions When there are different variables with the same name, there are different possible bindings for that name Not just variables: type names, constant names, function names, etc. Anything you can define A definition is anything that establishes a possible binding for a name Chapter Ten Modern Programming Languages

Examples We’ll discuss the scope of bindings… fun square n = n * n; fun square square = square * square; We’ll discuss the scope of bindings… Chapter Ten Modern Programming Languages

Outline Definitions and scope Scoping with blocks Scoping with labeled namespaces Scoping with primitive namespaces Dynamic scoping Chapter Ten Modern Programming Languages

Blocks A block is any language construct that contains definitions, and also contains the region of the program where those definitions apply let val x = 1; val y = 2; in x+y end Chapter Ten Modern Programming Languages

Different ML Blocks The let is just a block: no other purpose A fun definition includes a block: Multiple alternatives have multiple blocks: Each rule in a match is a block: fun cube x = x*x*x; fun f (a::b::_) = a+b | f [a] = a | f [] = 0; case x of (a,0) => a | (_,b) => b Chapter Ten Modern Programming Languages

Outline Definitions and scope Scoping with blocks Scoping with labeled namespaces Scoping with primitive namespaces Dynamic scoping Chapter Ten Modern Programming Languages

Labeled Namespaces A labeled namespace is any language construct that contains definitions and a region of the program where those definitions apply, and also has a name that can be used to access those definitions from outside the construct ML has one called a structure… Chapter Ten Modern Programming Languages

ML Structures structure Fred = struct val a = 1; fun f x = x + a; end; A little like a block: a can be used anywhere from definition to the end But the definitions are also available outside, using the structure name: Fred.a and Fred.f Chapter Ten Modern Programming Languages

Other Labeled Namespaces Namespaces that are just namespaces: C++ namespace Modula-3 module Ada package Java package Namespaces that serve other purposes too: Class definitions in class-based object- oriented languages Chapter Ten Modern Programming Languages

Outline Definitions and scope Scoping with blocks Scoping with labeled namespaces Scoping with primitive namespaces Dynamic scoping Chapter Ten Modern Programming Languages

Do Not Try This At Home It is legal to have a variable named int - val int = 3; val int = 3 : int It is legal to have a variable named int ML is not confused You can even do this (ML understands that int*int is not a type here): - fun f int = int*int; val f = fn : int -> int - f 3; val it = 9 : int Chapter Ten Modern Programming Languages

Primitive Namespaces ML’s syntax keeps types and expressions separated ML always knows whether it is looking for a type or for something else There is a separate namespace for types fun f(int:int) = (int:int)*(int:int); These are in the ordinary namespace These are in the namespace for types Chapter Ten Modern Programming Languages

Primitive Namespaces Not explicitly created using the language (like primitive types can’t) They are part of the language definition Some languages have several separate primitive namespaces See scope.cpp Java: packages, types, methods, fields, and statement labels are in separate primitive namespaces C++ is different: all class members are in the same namespace (that class) But class members are in the same namespace in OO languages. Chapter Ten Modern Programming Languages

Outline Definitions and scope Scoping with blocks Scoping with labeled namespaces Scoping with primitive namespaces Dynamic scoping Chapter Ten Modern Programming Languages

When Is Scoping Resolved? All scoping tools we have seen so far are static They answer the question (whether a given occurrence of a name is in the scope of a given definition) at compile time Some languages postpone the decision until runtime: dynamic scoping Chapter Ten Modern Programming Languages

Dynamic Scoping Each function has an environment of definitions If a name that occurs in a function is not found in its environment, its caller’s environment is searched And if not found there, the search continues back through the chain of callers This generates a rather odd scope rule… Chapter Ten Modern Programming Languages

Classic Dynamic Scope Rule The scope of a definition is the function containing that definition, from the point of definition to the end of the function, along with any functions when they are called (even indirectly) from within that scope—minus the scopes of any redefinitions of the same name in those called functions Somewhat like a global variable hence the caution to avoid them :-) Chapter Ten Modern Programming Languages

Example fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end; What is the value of g 5 using ML’s classic block scope rule? Chapter Ten Modern Programming Languages

Block Scope (Static) fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end; With block scope, the reference to inc is bound to the previous definition in the same block. The definition in f’s caller’s environment is inaccessible. g 5 = 6 in ML Chapter Ten Modern Programming Languages

Dynamic Scope fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end; With dynamic scope, the reference to inc is bound to the definition in the caller’s environment. g 5 = 7 if ML used dynamic scope Chapter Ten Modern Programming Languages

Where It Arises Only in a few languages: some dialects of Lisp and APL Advantage: “Dynamic scoping is useful as a substitute for globally scoped variables. A function can say "let current_numeric_base = 16; call other functions;" and the other functions will all print in hexadecimal. Then when they return, and the base-setting function returns, the base will return to whatever it was.” (c2.com) Chapter Ten Modern Programming Languages

Where It Arises Drawbacks: Difficult to implement efficiently Creates large and complicated scopes, since scopes extend into called functions Choice of variable name in caller can affect behavior of called function kills referential transparency! Chapter Ten Modern Programming Languages