This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 1 11/3/06 From Materials copyright N. Ramsey & al. 1 DPL-7 : types and type theory Type systems . Used to prevent run-time errors. Specifcally, run-time type errors . They come in two ¡avors: Monomorphic type systems Easy to compile to e¢fcient machine code Restrictive ¢or programmers Polymorphic type systems More work to compile to e¢fcient machine code Freedom ¢or programmers - good reuse We study one monomorphic and two polymorphic systems. 11/3/06 From Materials copyright N. Ramsey & al. 2 DPL-7 : types and type theory Type systems . Run-time checks that can be eliminated by using a type system: 1. Numbers added only to numbers 2. Every ¢unction receives the correct number o¢ arguments 3. Only booleans are used in if expressions 4. car and cdr are applied only to lists Run-time checks that cannot be eliminated by using a type system: 1. Numbers never divided by zero 2. car and cdr applied only to nonempty lists 3. Every re¢erence to an element o¢ an array ¢alls within the bounds o¢ the array. 11/3/06 From Materials copyright N. Ramsey & al. 3 DPL-7 : types and type theory Type systems . A language is safe if it allows no unchecked run-time errors . One can attempt to achieve this in two ways: 1. Construct an elaborate system o¢ run-time checks. This is computationally quite expensive - it can slow execution in drastic ways. 2. Construct an elaborate system o¢ compile-time checks, supplemented by run-time checks ¢or those errors that cannot be detected until run-time. The majority o¢ the computational cost is moved ¢rom run-time (every time the program runs) to compile time (once only…) 11/3/06 From Materials copyright N. Ramsey & al. 4 DPL-7 : types and type theory Working defnition o¢ type Property a value may have Set o¢ values (having the property) Or simply a phrase in the type language Mathematicians (at least those concerned with the Foundations o¢ Mathematics) are more care¢ul: Watch out ¢or Russell's Paradox! Is "type" a type??? 11/3/06 From Materials copyright N. Ramsey & al. 5 DPL-7 : types and type theory Examples . Things that are types: Things that are NOT types int list bool array int * bool ref int * int -> int int int int list bool array (int -> int) list 11/3/06 From Materials copyright N. Ramsey & al. 6 DPL-7 : types and type theory Typed lambda calculus (simplest programming language) . First-order typed lambda calculus: exp ⇒ var | λ var : type . exp | exp exp | const type ⇒ base-type ( e.g., int ) | type → type Type rules: type o¢ var is the type introduced at binding type o¢ M 1 M 2 , i¢ M 1 : τ 1 → τ 2 and M 2 : τ 1 , is τ 2 ....
View Full Document
- Fall '09
- Type system, Type theory, Copyright N. Ramsey, materials Copyright N.