Formalize Types _______________________________________________________________________________________________________________________ Types prevent bad things from happening ... formally: static samantics e ::= v | e1 + e2 | if e0 then e1 else e2 v ::= n | true | false for plus we have: n1 + n2 -> n (where n = n1 + n2) e1 -> e1' e2 -> e2' _____________________ ____________________ e1 + e2 -> e1' + e2 v1 + e2 -> v1 + e2' if true then e1 else e2 -> e1 if false then e1 else e2 -> e2 e -> e' ____________________________________________ if e then e1 else e2 -> if e' then e else e2 _______________________________________________________________________________________________________________________ example of "a bad thing": true + 1 true + n -> 1 + n false + n -> n n + true 0 -> n + 1 n + false -> n you don't necessarily want to do this - because you end up with situations like: 'a' = 3 -> 'aaa' '1' = _______________________________________________________________________________________________________________________ Static Types prevent the operational semantics from getting "stuck". We want to formalize typing: Typing Rules: / Typing Judgements: The shape of a "typing judgment" is going to be: |- e : T The "|-" is called a "\vdash" (or know as a "Turnstile") it (...see video) "e has type T" "e is typeable" "e is well-typed" T ::= int | bool |- n : int |- true: bool |- false: bool + e1: int + e2 : int ____________________ + e1 + e2 : int + e0 : bool |- e1 : T |- e2 : T ___________________________________ |- if e0 then e1 else e2 : T _______________________________________________________________________________________________________________________ The above definition is "flawed" because if you have the following: if true then e1 else e2 -> e1 |- true:bool |- 1:int |- false:bool ___________________________________ |- if true then 1 else false XXX we could have had a rule that: |- true:bool |- e1 : T ______________________________ |- if true then e1 else e2 : T _______________________________________________________________________________________________________________________ Examples: |- 1 : int |- 2: int _____________________ |- 1+2 : int |- 3 : int |- true:bool ___________________________________ |- if true then 1+2 else 3:int Typing derivation of |- e:T => this is proof that the expression e has type T _______________________________________________________________________________________________________________________ Extending the Language: Static Samantics e ::= v | e1 + e2 | if e0 then e1 else e2 | x | e1 e2 v ::= n | true | false | \x: T.e T ::= int | bool | T1 -> T2 Operational Semantics e1 -> e1' e2 -> e2' _____________________ ____________________ e1 + e2 -> e1' + e2 v1 + e2 -> v1 + e2' if true then e1 else e2 -> e1 if false then e1 else e2 -> e2 e -> e' ____________________________________________ if e then e1 else e2 -> if e' then e else e2 (\x: T.e) v -> e[x |-> v] e1 -> e2 e2 -> e2' _________________ _______________ e1 e2 -> e1' e2 v1 e2 -> v1 e2' Gamma -> context typing enviroment is a set of type bindings x : T Gamma ::= 0 | Gma, x:T Gma(x) <- which means: lookup 'x' in the enviroment Gma(x) = T iff x:T e Gma _______________________________________________________________________________________________________________________ we add: T ::= int | bool | T1 -> T2 which represents the "type" of a function - in that it takes a type of "T" and returns a "T" G |- x : G(x) (T-Var) G |- e1 : T->T' G |- e2:T _____________________________ (T-App) G |- e1 e2 : T' G,x:T |- e:T' _____________________ (T-Abs) G |- \x:T.e : T -> T' Call By Value (CBV) Simply Typed (lambda) \ calculus _______________________________________________________________________________________________________________________ y: int, x:int |- x:int G |- 1:int __________________________________ G = y: int, x: int |- x + 1 : int ___________________________________ (T-Abs) y: int |- \x:int . x +1 : int -> int y: int |- y: int (T-Var) _____________________________ (T-App) y:int |- (\x : int . x + 1) y : int _______________________________________________________________________________________________________________________ x:bool |- 1: ? (int) x:bool |- x:bool x:bool |- 2: ? (int) y:int |- true:bool _________________________________ _____________________________________ x:bool |- if x then 1 else 2 : ?b (int) 0 |- \y:int.true: ? -> ? 0|- 3:int _____________________________________________________________________________________ 0 |- \x:bool.if x then 1 else 2: ?r -> ?b (int) 0 |- (\y:int.true) 3: ?r _____________________________________________________________________________________ 0 |- [(\x:bool . if x then 1 else 2) ( (\y:int.true) 3)] : int _______________________________________________________________________________________________________________________ example: let x = 1 +2 int x + 3 -> 6 e ::= let x = e1 in e2 | (e1, e2) | fst e | snd e |- let x 1 + 2 in x + 3 : G |- e1 : T1 G |- e2 : T2 (is this correct? No ->) G |- e1 : T1 G, x:T1 |- e2 : T2 ____________________________ G |- let x = e1 int e2 : T _______________________________________________________________________________________________________________________ _______________________________________________________________________________________________________________________ _______________________________________________________________________________________________________________________