UMBC CMSC 331 - Variables: Names, Bindings, Type Checking and Scope

Unformatted text preview:

Chapter 5 Variables: Names, Bindings, Type Checking and ScopeIntroductionOn NamesNamesNames: implied attributesExamples of implied attributesVariablesAttributes of VariablesVariables TypeFunctions have types tooContrasts in Type SystemsVariable Valuelvalue and rvalueBindingPossible binding timesType BindingsDeclarationsImplicit Variable DeclarationsDynamic Type BindingSlide 29Static Type BindingType InferencingType Inferencing in MLDuck TypingDuck Typing exampleType CheckingStrong vs. Weak TypingStrong Typing FeaturesWhich languages have strong typing?Weak typing and coersionWeak typing and coercionWhat about Scheme and Python?Type CompatibilitySubtypes and rangesSlide 49Type Compatibility Language examplesType SafetyCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1Chapter 5Chapter 5 Variables:Names, Bindings, Type Checking and ScopeCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 2IntroductionThis lecture introduces the fundamental semantic issues of variables –It covers the nature of names and special words in programming languages, attributes of variables, concepts of binding and binding times. –It investigates type checking, strong typing and type compatibility rules. –At the end it discusses named constraints and variable initialization techniques.CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 3On Names•Humans are the only species to have the concept of a name.•It’s given philosophers, writers and computer scientists lots to do for many years.–The logician Frege’s famous morning star and evening star example.–"What's in a name? That which we call a rose by any other word would smell as sweet." -- Romeo and Juliet, W. Shakespeare –The semantic web proposes using URLs as names for everything.•In programming languages, names are character strings used to refer to program entities – e.g., labels, procedures, parameters, storage locations, etc.CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 4NamesThere are some basic design issues involving names:–Maximum length?–What characters are allowed?–Are names case sensitive?–Are special words reserved words or keywords?–Do names determine or suggest attributesCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 11Names: implied attributes•We often use conventions that associate attributes with name patterns.•Some of these are just style conventions while others are part of the language spec and are used by compilersCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 12Examples of implied attributes•LISP: global variables begin and end with an asterisk, e.g.,(if (> t *time-out-in-seconds*) (blue-screen)) •JAVA: class names begin with an upper case character, field and method names with a lower case characterfor(Student s : theClass) s.setGrade(“A”);•PERL: scalar variable names begin with a $, arrays with @, and hashtables with %, subroutines begin with a &.$d1 = “Monday”; $d2=“Wednesday”; $d3= “Friday”;@days = ($d1, $d2, $d3);•FORTRAN: default variable type is float unless it begins with one of {I,J,K,L,M,N} in which case it’s integer DO 100 I = 1, N100 ISUM = ISUM + ICMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 13Variables•A variable is an abstraction of a memory cell•Can represent complex data structures with lots of structure (e.g., records, arrays, objects, etc.)current_studentCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 14Attributes of VariablesVariables can be characterized as a 6-tuple of attributes:–Name: identifier used to refer to the variable–Address: memory location(s) holding the variables value–Value: particular value at a moment–Type: range of possible values–Lifetime: when the variable can be accessed–Scope: where in the program it can be accessedCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 18Variables Type•Typing is a rich subject in Computer Science•Most languages associate a variable with a single type•The type determines the range of values and the operations allowed for a variable•In the case of floating point, type usually also determines the precision (e.g., float vs. double)•In some languages (e.g., Lisp, Python, Prolog) a variable can take on values of any type.•OO languages (e.g. Java) have a few primitive types (int, float, char) and everything else is a pointer to an object, but the objects form a kind of user-defined type systemCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 19Functions have types too•Procedures or mehtods that return a value have a type, also: the type of the value returned•One of the most common way to describe a function is by its type signature•A type signature describes the types of each input and the type of the outputpower: float × integer floatCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 20Contrasts in Type Systems•Type systems are often described by their design decisions along several dimensions–Static vs. dynamic types–Strong vs. Weak typing–Explicit vs. implicit type conversion–Explicit vs. implicit type declarations•Although the dimensions appear to be binary choices, there are intermediate choices in many casesCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 21Variable Value•The value is the contents of the memory location with which the variable is associated.•Think of an abstract memory location, rather than a physical one.–Abstract memory cell - the physical cell or collection of cells associated with a variable•A variable’s type will determine how the bits in the cell are interpreted to produce a value.•We sometimes talk about lvalues and rvalues.CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 22lvalue and rvalueAre the two occurrences of “a” in this expression the same?a := a + 1;In a sense, •The one on the left of the assignment refers to the location of the variable whose name is a;•The one on the right of the assignment refers to the value of the variable whose name is a;We sometimes speak of a variable’s lvalue and rvalue •The lvalue of a variable is its address•The rvalue of a variable is its valueCMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 23Binding•Def: A binding is an association, such as between an attribute and an entity, or between an operation and a


View Full Document

UMBC CMSC 331 - Variables: Names, Bindings, Type Checking and Scope

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Variables: Names, Bindings, Type Checking and Scope
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Variables: Names, Bindings, Type Checking and Scope and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Variables: Names, Bindings, Type Checking and Scope 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?