David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6:...

30
David Evans http://www.cs.virginia.edu/ ~evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would appear that we have reached the limits of what it is possible to achieve with computer technology, although one should be careful with such statements, as they tend to sound pretty silly in five years.” John Von Neumann, 1949

Transcript of David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6:...

Page 1: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

David Evanshttp://www.cs.virginia.edu/~evans

CS655: Programming LanguagesUniversity of VirginiaComputer Science

Lecture 6: Types of Types

“It would appear that we have reached the limits of what it is possible to achieve with computer technology, although one should be careful with such statements, as they tend to sound pretty silly in five years.”

John Von Neumann, 1949

Page 2: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 2

Menu• PS 1 Results (John)

• Latent Types, Dynamic Checking

• Algol60 Group Report

• Manifest Types, Dynamic Checking

• Manifest Types, Static Checking

Page 3: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 3

Typesintegers in [0, …) Strings

Beatle’s Song Titles

pointers that points to integer that is prime number

Colors

secret information

pointers that points to pointer that points to storageshared by some other pointer

programs that halt

• Type is any set of values

• Next week’s definition will be different

Page 4: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 4

Why have types?• Overloading operators

+ vs. FADD – compiler needs types to figure out what “+” means

• Detecting program errors– Better to notice error than report incorrect result

• Make programs easier to read, understand and maintain – Better than comments if they are checked and can be

trusted

• Security– Can use types to constrain the behavior of programs (we’ll

see Proof-Carrying Code later…)

Page 5: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 5

Taxonomy• Latent vs. Manifest

– Are types visible in the program text?

• Checked statically vs. checked dynamically– Do you have to run the program to know if it has

type errors?

• Checked weakly vs. strongly– How strict are the rules for using types?

• All combinations are (theoretically) possible– Language that is manifest + static + weak?– Language that is latent + dynamic + strong?

Page 6: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 6

Labrador: BARK with Latent Types Instruction ::= STORE Loc Literal | HALT | ERROR (Same as BARK)

| ADD Loc1 Loc2 Loc1 gets the value of Loc1 + Loc2. Loc1 and Loc2

must be the same type, result has same type.

| MUL Loc1 Loc2 Loc1 gets the value of Loc1 * Loc2. Loc1 and Loc2

must be the same type.

| IF Loc1 THEN Loc1 If value in Loc1 is non-zero, jump to instruction

corresponding to value in Loc2. Loc1 and Loc2

must contain integers.Literal ::= IntLiteral | RealLiteralIntLiteral ::= [-]?[0-9][0-9]* Has type integer.RealLiteral ::= [-]?[0-9][0-9]*.[0-9]* Has type real.

As companions Labradors are kindly, patient,intelligent and always keen to please. They make perfect family dogs being especially good with children.

Walter & Shackles Guide to Dogs

Page 7: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 7

Labrador Program

[0] STORE R0 3.14159

[1] STORE R1 4

[2] MUL R1 R1

[3] MUL R0 R1

.

Page 8: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 8

Operational Semantics: ADD

Instructions[PC] = ADD Loc1 Loc2

< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile’ >where

PC’ = PC + 1RegisterFile’[n] = RegisterFile[n] if n LocRegisterFile’[n] = if n Loc1

RegisterFile[Loc1] + RegisterFile[Loc2]

BARK rule:

What does this mean?

Page 9: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 9

Typed Register File

C = Instructions x PC x RegisterFileRegisterFile[i] = <type, value> for all

integers itype = integer | realvalue = an integer if type if integer, a real

if type is realAssume functions typeof(RegisterFile[i]),

valueof(RegisterFile[i])

Page 10: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 10

Operational Semantics: ADDinteger

Instructions[PC] = ADD Loc1 Loc2,

< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile’ >where PC’ = PC + 1

RegisterFile’[n] = RegisterFile[n] if n LocRegisterFile’[n] = if n Loc1<integer,

valueof(RegisterFile[Loc1]) +integer

valueof(RegisterFile[Loc2])>

typeof(RegisterFile[Loc1]) = integer, typeof(RegisterFile[Loc2]) = integer

Page 11: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 11

Operational Semantics: ADDreal

Instructions[PC] = ADD Loc1 Loc2, typeof(RegisterFile[Loc1]) = real, typeof(RegisterFile[Loc2]) = real

< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile’ >where PC’ = PC + 1

RegisterFile’[n] = RegisterFile[n] if n LocRegisterFile’[n] = if n Loc1

<real, valueof(RegisterFile[Loc1]) +real

valueof(RegisterFile[Loc2])>

Page 12: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 12

Strong vs. Weak Typing• What do we have?

– Latent, dynamic, strongly typed language

• To get: latent, dynamic, weakly typed language:– Allow ADD and MUL to work on mixed types,

result is real, allow IF predicate to be real– Add transition rules for

Instructions[PC] = ADD Loc1 Loc2, typeof(RegisterFile[Loc1]) = real,

typeof(RegisterFile[Loc2]) = integer etc.

Page 13: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 13

Is Dynamic Type Checking Useful?

Page 14: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 14

Manifest Types

Often, however, explicit (manifest) types make programs easier for compilers to read, not easier for humans to read; and explicit (manifest) types are generally cumbersome for the program writer as well. Implicitly (latently) typed programming languages thus have clear advantages in terms of readability and writability.

Turbak & Gifford

Page 15: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 15

Mastiff: BARK with Manifest Types

Program ::= Declaration* Instruction*Declaration ::=

TYPE Loc INTEGER Loc will hold integral values. | TYPE Loc REAL Loc will hold real values.

Instruction ::= STORE Loc Literal Loc gets the value of Literal. Loc

musthave been declared with the sametype as Literal.

… (same as Labrador)

Mastiff: An excellent guard dog, yet gentle and affectionate to its family.Walter & Shackles Guide to Dogs

Page 16: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 16

Mastiff Program

[D0] TYPE R0 REAL

[D1] TYPE R1 INTEGER

[0] STORE R0 3.14159

[1] STORE R1 4

[2] MUL R1 R1

[3] MUL R0 R1

Page 17: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 17

Input Function: I: Program CC = Instructions x PC x RegisterFile

whereInstructions = same as before, PC = 0RegisterFile[n] = <integer, 0> if

TYPE Rn INTEGER is in DeclarationsRegisterFile[n] = <real, 0.0> if

TYPE Rn REAL is in DeclarationsRegisterFile[n] = <undeclared, 0>

for all other integers n RegisterFile[n] = <error, 0> if

(TYPE Rn INTEGER and TYPE Rn REAL are in Declarations)

Page 18: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 18

STORE Loc IntLiteralInstructions[PC] = STORE Loc IntLiteral, typeof(RegisterFile[Loc]) = integer

< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile’ >where

PC’ = PC + 1RegisterFile’[n] = RegisterFile[n] if n LocRegisterFile’[n] =

<integer, value of IntLiteral> if n Loc

Page 19: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 19

Static Semantics• Static checking = at compile-time

– Dynamic checking = at run (simulate)-time

• Know a whole program is type-correct without running it

• Can make claims about all possible executions

• Drawbacks: – May limit expressiveness of types (not everything

can be checked statically)– Some type-correct programs may not pass static

checking

Page 20: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 20

Premise; ...; Premise Conclusion

Conclusions are type judgments: A E : TRead: A proves E has type T.Use type okay to mean it type-checks, but has no type.

Type environment: A

Type bindings: [I1:T1, ..., In:Tn]

In (Loc) has type Tn

Typing Rules

Page 21: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 21

Mastiff Typing Rules

A IntLiteral : integer [int-literal]

RealLiteral : real [real-literal]

A contains [Loc:T] Loc : T [location]

true

Page 22: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 22

Typing ADD

A Loc1 : integer , A Loc22 : integer

A ADD Loc1 Loc2 : okay

[add-integers]

A Loc1 : real , A Loc22 : real

A ADD Loc1 Loc2 : okay

[add-reals]

Page 23: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 23

Typing MUL

A Loc1 : integer , A Loc22 : integer

A MUL Loc1 Loc2 : okay

[mul-integers]

A Loc1 : real , A Loc2 : integer

A MUL Loc1 Loc2 : okay

[mul-weak]

Page 24: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 24

Typing IF

A Loc1 : integer , A Loc2 : integer

A IF Loc1 THEN Loc2 : okay[if]

A Loc1 : real , A Loc2 : integer

A IF Loc1 THEN Loc2 : okay[if-weak]

Page 25: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 25

Type Checking

• Statement is well-typed if and only if it has a provable type judgment:– Construct a proof tree, where the root is the type

judgment for this statement, and leaves are the axioms

• Declarations provide the axioms:Declarations =

TYPE Loci0 Ti0; TYPE Loci1 Ti1, ...

A = [Loci0: Ti0, Loci1 : Ti1, ... ]

Page 26: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 26

Type Checking Example

Check instruction 3: MUL R0 R1

[D0] TYPE R0 REAL

[D1] TYPE R1 INTEGER

[0] STORE R0 3.14159

[1] STORE R1 4

[2] MUL R1 R1

[3] MUL R0 R1

A R0 : real , A R1 : integer

A MUL R0 R1 : okay

[mul-weak]

A contains [R0 : real] R0 : real same for R1: integer

A = [R0: real; R1: integer] from Declarations

Page 27: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 27

Mastiff with ExpressionsProgram ::= Declaration* Instruction*

Instruction ::=

STORE Loc Expression Expression must match declared type of Loc

| GOTO Expression Expression must be integer

| IF Expressionp THEN Expressionj

Expressionj must be integer

Expression ::=

MUL Expression Expression

| ADD Expression Expression

| Loc | Literal | ( Expression )

Page 28: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 28

Type checking Example[D0] TYPE R0 REAL

[0] STORE R0 (MUL 3.14159 (MUL 4 4))

[store-real]A R0 : real

, A (MUL …): real

A STORE R0 (MUL …): okay

A 3.14: real , A (MUL 4 4) : integer

A MUL 3.14 (MUL 4 4): okay

[mul-weak]

… [mul-int]… [real-literal]

Page 29: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 29

Summary

Statically Checked

Dynamically Checked

Manifest Types Mastiff-S Mastiff-D

Latent Types Labrador

CLU, Pascal

ML, FLScheme, Smalltalk

Page 30: David Evans evans CS655: Programming Languages University of Virginia Computer Science Lecture 6: Types of Types “It would.

1 Feb 2000 University of Virginia CS 655 30

Charge• Continue working on your projects!• Tuesday: Data Abstraction

– CLU: language designed to support methodology based on data abstraction

– Reasoning (informally) about data abstractions

• Next: Object-Oriented Languages • Later: (after Spring Break)

– How to do static checking with latent types (type reconstruction, type inference)

– How to use types for security

• Today at 4:00, Jordan Hall:Mark S. Boguski, Natl Institutes of HealthInterface between computation and biology