Download presentation
Presentation is loading. Please wait.
1
Approaches to Typing Programming Languages Robert Dewar
2
Typing: Why? There are several different motivations for typing in high level languages There are several different motivations for typing in high level languages Efficiency Efficiency Error Checking Error Checking Redundancy Redundancy Overloading Overloading
3
Efficiency Compiler can generate better code if it knows exactly what is in each variable. Compiler can generate better code if it knows exactly what is in each variable. Historically that’s where typing comes from Historically that’s where typing comes from In Fortran In Fortran INTEGER I INTEGER I REAL R REAL R Subroutines have parameters of specified type Subroutines have parameters of specified type Compiler can generate efficient code for exactly the type of data present without any kind of checks. Compiler can generate efficient code for exactly the type of data present without any kind of checks. Types match machine datatypes Types match machine datatypes For example: 32 bit integers For example: 32 bit integers
4
Error Checking In a freely typed system (e.g. assembler) In a freely typed system (e.g. assembler) Can add a float to an integer?! Can add a float to an integer?! Can compare a string to a float?! Can compare a string to a float?! Can use a float as a pointer?! Can use a float as a pointer?! Can assume a buffer is 1K! Can assume a buffer is 1K! When really it is only 512 bytes?! When really it is only 512 bytes?! Etc. Etc.
5
Error Checking (continued) Typing prevents errors of this kind Typing prevents errors of this kind By detecting the error either at compile time or run-time. By detecting the error either at compile time or run-time. Stronger typing capabilities mean that more errors can be detected. Stronger typing capabilities mean that more errors can be detected. For instance, the “buffer overrun” For instance, the “buffer overrun” Detected in Ada, but not in C Detected in Ada, but not in C
6
Redundancy Eliminates Errors Cannot misspell an identifier Cannot misspell an identifier Shuttle_Presure := 1.0 Shuttle_Presure := 1.0 undefined identifier undefined identifier possible misspelling of Shuttle_Pressure possible misspelling of Shuttle_Pressure Cannot accidentally mix units Cannot accidentally mix units Probe_Velocity := Probe_Velocity + Accel; Probe_Velocity := Probe_Velocity + Accel; type mismatch type mismatch Probe_Velocity is of type MPH Probe_Velocity is of type MPH Accel is of type KPH Accel is of type KPH
7
Overloading A = B + C A = B + C With typing, we know whether to do an integer or real addition With typing, we know whether to do an integer or real addition Obj->Draw Obj->Draw We know which Draw to call based on the type of Obj. We know which Draw to call based on the type of Obj.
8
Compile Time vs Run Time Some typing semantics are compile time Some typing semantics are compile time In Fortran, do we do integer or real addition In Fortran, do we do integer or real addition In Ada, cannot assign Float to Integer In Ada, cannot assign Float to Integer Some typing semantics are run-time Some typing semantics are run-time In Ada, subscript out of range In Ada, subscript out of range In C++, dynamic dispatch for virtual method In C++, dynamic dispatch for virtual method
9
Different Approaches to Typing Explicit Static Typing at Compile Time Explicit Static Typing at Compile Time Dynamic Typing at Run Time Dynamic Typing at Run Time Static Typing by Inference at Compile Time Static Typing by Inference at Compile Time
10
Explicit Static Typing at Compile Time Code contains explicit declarations Code contains explicit declarations A : Integer; A : Integer; int *p; int *p; Compiler knows the types Compiler knows the types Compiler checks the types at compile time Compiler checks the types at compile time
11
Run-Time Typing Type is not determined till run-time but is fixed once it is is determined Type is not determined till run-time but is fixed once it is is determined B : Integer range 1.. N; B : Integer range 1.. N; C : array (Integer range 1.. N) of integer; C : array (Integer range 1.. N) of integer; “type” error is now a run-time exception “type” error is now a run-time exception But some static analysis is possible But some static analysis is possible Type R is new integer range 1.. N; Type R is new integer range 1.. N; M, K : R; M, K : R; M := K; -- definitely OK M := K; -- definitely OK
12
Run-Time Typing Type is dynamically modified at run-time Type is dynamically modified at run-time type sp is access String; type sp is access String; A, B : sp; A, B : sp; … A.all := B.all; -- run-time exception if wrong A.all := B.all; -- run-time exception if wrong Dynamic dispatching is similar case Dynamic dispatching is similar case Q *R; Q *R;.... R->Draw(); R->Draw();
13
Dynamically Typed Languages Go all the way with run-time typing Go all the way with run-time typing SNOBOL-4 is an example SNOBOL-4 is an example No explicit type declarations No explicit type declarations No variable has a fixed type at run-time No variable has a fixed type at run-time Can put any value of any type anywhere Can put any value of any type anywhere But values are still strongly typed But values are still strongly typed
14
SNOBOL4 as an Example No explicit declarations No explicit declarations Variable “declared” by assigning to it Variable “declared” by assigning to it X = 123;* X now has integer 123 X = 123;* X now has integer 123 X = X + 1;* X now has integer 124 X = X + 1;* X now has integer 124 X = “ABC”;* X now has string “ABC” X = “ABC”;* X now has string “ABC” X = X “M”;* X now has string “ABCM” X = X “M”;* X now has string “ABCM” X = X + 1;* run-time error X = X + 1;* run-time error
15
Type Inference A3 := B4 + 1; A3 := B4 + 1; Q: What type is A3 and B4; Q: What type is A3 and B4; A: Must be integer A: Must be integer if Test1 then … if Test1 then … Q: What type is Test1 Q: What type is Test1 A: Must be Boolean A: Must be Boolean If we can answer these questions why bother with redundant declarations? If we can answer these questions why bother with redundant declarations?
16
Type Inference Languages No explicit declarations No explicit declarations Language designed so that all types can be inferred. Language designed so that all types can be inferred. Inference involves a chain of reasoning: Inference involves a chain of reasoning: A3 := Func (Test1); A3 := Func (Test1); Q: What is Func Q: What is Func A: Must be array/function Boolean->Integer A: Must be array/function Boolean->Integer Would be better to have separate notations for functions and arrays! Would be better to have separate notations for functions and arrays!
17
More on Type Inference A language with a sound type system is one in which all types can always be inferred in any valid program. A language with a sound type system is one in which all types can always be inferred in any valid program. Example of language designed this way is Standard ML. Example of language designed this way is Standard ML. Note that we still have strong static typing and many of the benefits that come from that. Note that we still have strong static typing and many of the benefits that come from that.
18
ML: Example of Type Inference Types always implied: Types always implied: - val x = 5; (*user input *) - val x = 5; (*user input *) val x = 5: int (*system response*) val x = 5: int (*system response*) - fun len lis = if (null lis) then 0 else 1 + len (tl lis); - fun len lis = if (null lis) then 0 else 1 + len (tl lis); val len = fn : ‘a list -> int val len = fn : ‘a list -> int Type inference for local entities Type inference for local entities - x * x * x; - x * x * x; val it = 125: int (* it denotes the last computation*) val it = 125: int (* it denotes the last computation*)
19
Limits on Inference Inference is Structural. Cannot duplicate: Inference is Structural. Cannot duplicate: type A is new Integer; type B is new integer; VA : A; VB : B; … VA := 1; VB := 1; VA := VB type A is new Integer; type B is new integer; VA : A; VB : B; … VA := 1; VB := 1; VA := VB
20
Summary EfficiencyError Checking Redundancy Overloading EfficiencyError Checking Redundancy Overloading Explicit YES YES YESYES Explicit YES YES YESYES Dynamic NO YES NO YES Dynamic NO YES NO YES Inference YES YES? YES?YES? Inference YES YES? YES?YES?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.