CSE321 Programming Languages Simply Typed Calculus POSTECH March

  • Slides: 29
Download presentation
CSE-321 Programming Languages Simply Typed -Calculus 박성우 POSTECH March 28, 2007

CSE-321 Programming Languages Simply Typed -Calculus 박성우 POSTECH March 28, 2007

 -Calculus • Abstract syntax • Operational semantics (call-by-value) 2

-Calculus • Abstract syntax • Operational semantics (call-by-value) 2

Simulating Base Types • Booleans • Natural numbers • Fixed point combinator 3

Simulating Base Types • Booleans • Natural numbers • Fixed point combinator 3

Motivation • The -calculus is expressive. – booleans, integers, lists, recursive functions, . .

Motivation • The -calculus is expressive. – 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! 4

Simply Typed -Calculus • An extension of the untyped -calculus with types • Assumes

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 5

Outline • Introduction V • The simply typed -calculus – Abstract syntax – Operational

Outline • Introduction V • The simply typed -calculus – Abstract syntax – Operational semantics – Type system • Type safety 6

Abstract Syntax 7

Abstract Syntax 7

What if there is no base type? No interesting expression! 8

What if there is no base type? No interesting expression! 8

Outline • Introduction V • The simply typed -calculus – Abstract syntax V –

Outline • Introduction V • The simply typed -calculus – Abstract syntax V – Operational semantics – Type system • Type safety 9

Simply Untyped -Calculus 10

Simply Untyped -Calculus 10

Reduction Rules for Booleans 11

Reduction Rules for Booleans 11

Capture-Avoiding Substitutions • Completely analogous 12

Capture-Avoiding Substitutions • Completely analogous 12

Free Variables • Completely analogous 13

Free Variables • Completely analogous 13

Outline • Introduction V • The simply typed -calculus – Abstract syntax V –

Outline • Introduction V • The simply typed -calculus – Abstract syntax V – Operational semantics V – Type system • Type safety 14

What is the type of Answer: ?

What is the type of Answer: ?

How to find the type of 1. Assume that the type of x is

How to find the type of 1. Assume that the type of x is A. • okay 2. Find the type of x. • A 3. Build a function type • A!A Need to make assumptions on types of variables! 16

Type System • Typing context • Typing judgment 17

Type System • Typing context • Typing judgment 17

Typing Rules --- Top-down 18

Typing Rules --- Top-down 18

Typing Rules --- Bottom-up 19

Typing Rules --- Bottom-up 19

Typing Rules for Booleans 20

Typing Rules for Booleans 20

Typing Derivation 21

Typing Derivation 21

Typing Derivation 22

Typing Derivation 22

Outline • Introduction V • The simply typed -calculus V – Abstract syntax V

Outline • Introduction V • The simply typed -calculus V – Abstract syntax V – Operational semantics V – Type system V • Type safety 23

Unsafe Operations in C • Adding two pointers • Subtracting an integer from a

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 • . . . 24

Assignment 3+ • Count the number of hours that you spend before getting your

Assignment 3+ • Count the number of hours that you spend before getting your Matrix program to typecheck. – Example: • 0 am: You start. • 10 am: Your program compiles with no type errors. • Then count the number of extra hours that you spend before completing Assignment 3. 25

Type Safety • Slogan "well-typed expressions never go wrong" • Two theorems – Type

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 does not get stuck: either it is a value or reduces to another expression. " 26

Type Preservation + Progress • A well-typed expression e: – If it is a

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] 27

Type Safety • Type preservation • Progress 28

Type Safety • Type preservation • Progress 28

Proof of Type Safety • Use the rule induction. • In the next lecture!

Proof of Type Safety • Use the rule induction. • In the next lecture! 29