Post on 07-Jan-2016
description
transcript
Programming Language Concepts (CIS 635)
Elsa L Gunter
4303 GITC
NJIT, www.cs.njit.edu/~elsa/635
Copyright 2002 Elsa L. Gunter
Type Checking
• When is op(arg1,…,argn) allowed?• Type checking assures that operations
are applied to the right number of arguments of the right types– Right type may mean same type as
was specified, or may mean that there is a predefined implicit coercion that will be applied
• Used to resolve overloaded operations
Copyright 2002 Elsa L. Gunter
Type Checking
• Type checking may be done statically at compile time or dynamically at run time
• Untyped languages (eg LISP, Prolog) do only dynamic type checking
• Typed languages can do most type checking statically
Copyright 2002 Elsa L. Gunter
Dynamic Type Checking
• Performed at run-time before each operation is applied
• Types of variables and operations left unspecified until run-time– Same variable may be used at
different types
Copyright 2002 Elsa L. Gunter
Dynamic Type Checking
• Data object must contain type information
• Errors aren’t detected until violating application is executed (maybe years after the code was written)
Copyright 2002 Elsa L. Gunter
Static Type Checking
• Performed after parsing, before code generation
• Type of every variable and signature of every operator must be known at compile time
Copyright 2002 Elsa L. Gunter
Static Type Checking
• Can eliminate need to store type information in data object if no dynamic type checking is needed
• Catches many programming errors at earliest point
Copyright 2002 Elsa L. Gunter
Strongly Typed Language
• When no application of an operator to arguments can lead to a run-time type error, language is strongly typed
• Depends on definition of “type”
Copyright 2002 Elsa L. Gunter
Strongly Typed Language
• C is “strongly typed” but type coercions may cause unexpected (undesirable) effects; no array bounds check (in fact, no runtime checks at all)
• SML “strongly typed” but still must do dynamic array bounds checks, arithmetic overflow checks
Copyright 2002 Elsa L. Gunter
How to Handle Type Mismatches
• Type checking to refuse them
• Apply implicit function to change type of data–Coerce int into real
–Coerce char into int
Copyright 2002 Elsa L. Gunter
Conversion Between Types:
• Explicit: all conversions between different types must be specified
• Implicit: some conversions between different types implied by language definition– Implicit conversions called coercions
Copyright 2002 Elsa L. Gunter
Coercion Examples
Example in Pascal:var A: real;B: integer;
A := B–Implicit coercion - an automatic
conversion from one type to another
Copyright 2002 Elsa L. Gunter
Coercions Versus Conversions
• When A has type int and B has type real, many languages allow coercion implicit in
A := B
• In the other direction, often no coercion allowed; must use explicit conversion:
– B := round(A); Go to integer nearest A
– B := trunc(A); Delete fractional part of A
Copyright 2002 Elsa L. Gunter
Subtypes
• A is a subtype of B if every value of A is a value of B (or more accurately it appears to be)
• Frequently candidate for coercion
• In C many things are subtypes of integer – many things can be implicitly coerced into a integers
Copyright 2002 Elsa L. Gunter
Type Equality (aka Type Compatibility)
• When are two types “the same”?
• Name equivalence: two types equal only if they have the same name– Simple but restrictive
– Usually loosened to allow two types to be equal when one is defined with the name of the other (declaration equivalence)
Copyright 2002 Elsa L. Gunter
Type Equality• Structure equivalence: Two
types are equivalent if the underlying data structures for each type are the same–Problem: how far to go – are two
records with the same number of fields of same type, but different labels equivalent?
Copyright 2002 Elsa L. Gunter
Scope of Variable• Range of program that can reference
that variable (ie access the corresponding data object by the variable’s name)
• Variable is local to program or block if it is declared there
• Variable is nonlocal to program unit if it is visible there but not declared there
Copyright 2002 Elsa L. Gunter
Static Scoping
• Scope computed at compile time, based on program text
• To determine the variable of a name used must find statement declaring variable
Copyright 2002 Elsa L. Gunter
Static Scoping
• Subprograms and blocks generate hierarchy of scopes
–Subprogram or block that declares current subprogram or contains current block is its static parent
Copyright 2002 Elsa L. Gunter
Static Scoping
• General procedure to find declaration:– First see if variable is local; if yes,
done– If nonlocal to current subprogram or
block recursively search static parent until declaration is found
– If no declaration is found this way, undeclared variable error detected
Copyright 2002 Elsa L. Gunter
Example
program main;
var x : integer;
procedure sub1;
var x : integer;
begin { sub1 }
… x …
end; { sub1 }
begin { main }
… x …
end; { main }
Copyright 2002 Elsa L. Gunter
Dynamic Scope
• Now generally thought to have been a mistake
• Main example of use: original versions of LISP
–Common LISP uses static scope
–Perl allows variables to be decalred to have dynamic scope
Copyright 2002 Elsa L. Gunter
Dynamic Scope
• Determined by the calling sequence of program units, not static layout
• Name bound to corresponding variable most recently declared among still active subprograms and blocks
Copyright 2002 Elsa L. Gunter
Example
program main;
var x : integer;
procedure sub1;
begin { sub1 }
… x …
end; { sub1 }
procedure sub2;
var x : integer;
begin { sub2 }
… call sub1 …
end; { sub2 }
… call sub2…
end; { main }
Copyright 2002 Elsa L. Gunter
Example: Static Scope
program main;
var x : integer;
procedure sub1;
begin { sub1 }
… x …
end; { sub1 }
procedure sub2;
var x : integer;
begin { sub2 }
… call sub1 …
end; { sub2 }
… call sub2…
end; { main }
Copyright 2002 Elsa L. Gunter
Example: Dynamic Scope
program main;
var x : integer;
procedure sub1;
begin { sub1 }
… x …
end; { sub1 }
procedure sub2;
var x : integer;
begin { sub2 }
… call sub1 …
end; { sub2 }
… call sub2…
end; { main }
Copyright 2002 Elsa L. Gunter
Referencing Environment
• The referencing environment of a program point is the set of all variables (names bound to data objects) visible to that program point
• In a static scoped language referencing environment is all local variables (declared so far) together with all declared variables of all static ancestors minus those that have been hidden
Copyright 2002 Elsa L. Gunter
Example (Static Scope)
program main;
var x, y : integer;
procedure sub1;
var z : integer
begin { sub1 }
… point1 …
end; { sub1 }
procedure sub2;
var w, x : integer;
begin { sub2 }
… point2 …
end; { sub2 }
…point3 …
end; { main }
Copyright 2002 Elsa L. Gunter
Example
• Referencing Envirnoments:
• Point1: main.x main.y sub1.z
• Point2: main.y sub2.x sub2.w
• Point3: main.x main.y