CSE-321 Programming Languages Simply Typed -Calculus 박성우 POSTECH April 3, 2006
-Calculus Abstract syntax Operational semantics (call-by-value)
Simulating Base Types Booleans Natural numbers Fixed point combinator
Motivation The -calculus is equally expressive as Turing machines. booleans, integers, lists, recursive functions, ... But it is highly inefficient to program in the -calculus. Why not just use instead of ? So we introduce a type system!
Simply Typed -Calculus An extension of the untyped -calculus with types Assumes a fixed set of base types E.g. base type primitive constructs A subset of Standard ML
Outline Introduction V The simply typed -calculus Abstract syntax Operational semantics Type system Type safety
Abstract Syntax
What if there is no base type? No interesting expression!
Outline Introduction V The simply typed -calculus Abstract syntax V Operational semantics Type system Type safety
Simply Untyped -Calculus
Reduction Rules for Booleans
Capture-Avoiding Substitutions Completely analogous
Free Variables Completely analogous
Outline Introduction V The simply typed -calculus Abstract syntax V Operational semantics V Type system Type safety
What is the type of ? Answer:
How to find the type of Assume that the type of x is A. okay Find the type of x. A Build a function type A ! A Need to make assumptions on types of variables!
Type System Typing context Typing judgment
Typing Rules --- Top-down
Typing Rules --- Bottom-up
Typing Rules for Booleans
Typing Derivation
Typing Derivation
Outline Introduction V The simply typed -calculus V Abstract syntax V Operational semantics V Type system V Type safety
Unsafe Operations in C Adding two pointers Subtracting an integer from a string which is okay, but likely to be unintended Null-pointer dereferencing Argh... segmentation fault! Using an integer as a destination address in a function call ...
Huffman Coding in SML Suppose that you spent 10 hours before getting your program to typecheck. 0am: You start. 10am: Your program compiles with no type errors. How many more hours did you spend after that?
Type Safety Slogan "well-typed expressions never go wrong" Two theorems Type preservation: "A well-typed expression reduces to another expression of the same type." Progress: "A well-typed expression is not stuck: either it is a value or reduces to another expression."
Type Preservation + Progress A well-typed expression e: If it is a value, we are finished. If it is not, It reduces to another e' [Progress] e' has the same type as e. [Type preservation]
Type Safety Type preservation Progress
Proof of Type Safety Use the rule induction. In the next lecture!