Bool and Nat Types - NUS School of Computing

CS5205: Foundations in Programming
Languages
“Introduction to
Type Systems”
CS5205
Intro to Type System
1
Rise of Lightweight Formal Methods
Don’t prove correctness: just find bugs ..
- model checking
- light specification and verification (e.g. ESC, SLAM ..)
- type-checking!
Basic ideas are long established; but industrial attitudes
have been softened by the success of model checking in
hardware design.
“Formal methods will never have any impact until they can be
used by people that don’t understand them” : Tom Melham
CS5205
Intro to Type System
2
What is a Type Systems?
A Type System is a
•
tractable syntactic method
• for proving the absence of certain program
behaviors
• by classifying phrases according to the
kinds of values they compute
CS5205
Intro to Type System
3
Why Type Systems?
Type systems are good for:
•
•
•
•
•
•
•
detecting errors
abstraction
documentation
language design
efficiency
safety
.. etc.. (security,exception,theorem-proving,webmetadata,categorical grammer)
CS5205
Intro to Type System
4
Mathematical Preliminaries
• Sets, Relations, Functions.
• Can regard relations as set, e.g (s1,..,sn) 2 R
Binary relation denoted using (s1,s2) 2 R or s1 R s2
• A binary relation R on set S is:
• reflexive
if 8 s 2 S. (s,s) 2 R
• symmetric
if s R t ) t R s
• transitive
if s R t Æ t R u ) s R u
• antisymmetric if s R t Æ t R s ) t ´ s
• A reflexive and transitive relation R on set S is called a
preorder on S.
• A preorder that is antisymmetric is called a partial order.
• A partial order is total if (8 s,t 2 S. s · t Ç t · s)
CS5205
Intro to Type System
5
Well-Foundedness
• Suppose we have a preorder · on set S.
• A decreasing chain in · is a sequence s1,s2,s3,… of elements
of S such that each element of the sequence is strictly less
than its predecessor: si+1<si
• We say a preorder · is well-founded if it contains no
infinite decreasing chains.
• Example: The usual order < on the natural numbers is
well-founded, but the same order on integers is not.
CS5205
Intro to Type System
6
Syntax of Arithmetic Expressions
The set of terms T is defined by the following grammar.
T ::=
terms
constant true
constant false
conditional
constant zero
successor
predecessor
zero test
true
false
if t then t else t
0
succ t
pred t
iszero t
description
syntax
CS5205
Intro to Type System
7
Set of Expressions – Inference Rule
Set of terms can be define by the following inference rules:
true 2 T
false 2 T
t2T
02T
t2T
succ t 2 T
t2T
pred t 2 T
t1 2 T
t2 2 T
iszero t 2 T
t3 2 T
if t1 then t2 else t3 2 T
CS5205
Intro to Type System
8
Example Terms
iszero
pred
iszero (pred (succ 0))
succ
0
if
if (iszero (pred 0)) then 0 else 1
iszero
pred
0
1
0
CS5205
Intro to Type System
9
Abstract Syntax Tree (AST)
• In abstract syntax, only essential components are
mentioned.
• No need to worry about operator precedences,
parentheses, spaces etc.
• Parsing : from concrete syntax to abstract syntax tree
CS5205
Intro to Type System
10
Operational Semantics of Terms
One step evaluation relation ! is the smallest binary relation
satisfying the rules:
if true then t2 else t3 ! t2
(E-IfTrue)
if false then t2 else t3 ! t3
(E-IfFalse)
t1 ! t‘1
if t1 then t2 else t3 ! if
CS5205
t‘
1
then t2 else t3
Intro to Type System
(E-If)
11
Operational Semantics of Terms (cont)
t ! t‘
pred t ! pred
t ! t‘
succ t ! succ
t ! t‘
(E-Succ)
t‘
iszero t ! iszero
CS5205
(E-Pred)
t‘
t‘
(E-Pred)
Intro to Type System
12
Values
Values are possible final results of evaluation.
v ::=
values:
true value
false value
numerical value
true
false
nv
nv ::=
0
succ nv
CS5205
numerical values:
zero value
successor value
Intro to Type System
13
Operational Semantics of Terms (cont)
CS5205
iszero 0 ! true
(E-IszeroZero)
iszero (succ nv1) ! false
(E-IszeroSucc)
pred 0 ! 0
(E-PredZero)
pred (succ nv1) ! nv1
(E-PredSucc)
Intro to Type System
14
Example of One-Step Evaluation
Step 1:
pred 0 ! 0
(E-PredZero)
succ(pred 0) ! succ 0
(E-Succ)
pred (succ(pred 0)) ! pred (succ 0)
(E-Pred)
Step 2:
pred (succ 0) ! 0
CS5205
(E-PredSucc)
Intro to Type System
15
Normal Form
A term t is a normal form if there is no t’ such that t ! t’.
The multi-step evaluation relation !* is the reflexive,
transitive closure of one-step relation.
pred (succ(pred 0))
!
pred (succ 0)
!
0
CS5205
pred (succ(pred 0))
!*
0
Intro to Type System
16
Stuckness
Evaluation may fail to reach a value:
succ (if true then false else true)
!
succ (false)
!
A term is stuck if it is a normal form but not a value.
Stuckness is a way to characterize runtime errors.
CS5205
Intro to Type System
17
Motivation for Typing
• Evaluation of a term either results in a value or gets
stuck!
• Typing can prove that an expression cannot get stuck.
• Typing is static and can be checked at compile-time.
CS5205
Intro to Type System
18
Bool and Nat Types
• We introduce two types : Bool and Nat.
• A term t is typable (or well-typed) if there is some T
such that t:T.
• Well-typed terms:
(if true then 0 else succ 0) : Nat
iszero (succ (pred 0) : Bool
• Ill-typed terms.
succ (true) : Error!
(if (iszero 0) then 0 else false) : Error!
CS5205
Intro to Type System
19
Types
Types are used to denote a set of allowable terms
T ::=
Bool
Nat
types:
type of boolean
type of natural numbers
Typing relation t:T is the smallest binary relation between
terms and types satisfying all instances of its type rules.
CS5205
Intro to Type System
20
Typing Rules
True : Bool (T-true)
False : Bool (T-false)
t1:Bool t2:T t3:T
if t1 then t2 else t3 : T
t : Nat
(T-Succ)
succ t : Nat
CS5205
0 : Nat (T-Zero)
(T-If)
t : Nat
(T-Pred)
pred t : Nat
Intro to Type System
t : Nat
(T-Iszero)
iszero t : Bool
21
Example of Typing Derivation
0 : Nat
(T-Zero)
iszero 0 : Bool
0 : Nat
(T-Iszero)
0 : Nat
(T-Zero)
if (iszero 0) then 0 else (pred 0) : Nat
CS5205
Intro to Type System
(T-Zero)
(pred 0) : Nat
(T-Pred)
(T-If)
22
Purpose of Types
• A type system is a tractable syntactic method for
proving the absence of certain program behaviors by
classifying phrases according to the kinds of values they
compute.
• What “bad behavior” can terms have?
Answer : They can get stuck.
A type systems for terms is safe/sound if the
evaluation of well-typed terms does not get stuck
CS5205
Intro to Type System
23
Safety = Progress + Preservation
• Progress : A well-typed term is not stuck. Either it is a
value, or it can take a step according to the evaluation
rules.
Suppose t is a well-typed term (that is t:T for some T).
Then either t is a value or else there is some t‘ with t ! t‘
CS5205
Intro to Type System
24
Safety = Progress + Preservation
• Preservation : If a well-typed term takes a step of
evaluation, then the resulting term is also well-typed.
If t:T Æ t ! t` then t`:T .
CS5205
Intro to Type System
25
Properties
• Every value is a normal form.
• (Determinacy of one-step evaluation)
If t ! t` and t ! t`` then t`= t``.
• (Uniqueness of normal form)
If t !* u and t !* u’ and both u, u’ are normal form
then u= u’.
• (Termination of Evaluation)
For every term t, there is some normal form u such that t
t !* u .
CS5205
Intro to Type System
26
Explicitly Typed Lambda Calculus
•
t ::=
terms
…
l x : T.t
t1 t2
•
v ::=
•
T ::=
l x : T.t
…
Bool
Nat
T!T
CS5205
abstraction
application
value
abstraction value
types
type of booleans
type of natural
type of functions
Intro to Type System
27
Examples
true
l x:Bool . x
(l x:Bool . x) true
if false then (l x:Bool . True) else (l x:Bool . x)
CS5205
Intro to Type System
28
Operational Semantics of Terms (Cont)
One step evaluation relation for application :
t1 ! l v . t
t2 ! v2
(E-App)
t1 t2 ! [v ! v2] t
CS5205
Intro to Type System
29
Erasure
• The erasure of a simply typed term t is defined as:
erase(x)
erase(lx :T.t)
erase(t1 t2)
=
=
=
x
l x. erase(t)
erase(t1) erase(t2)
• A term m in the untyped lambda calculus is said to be
typable in l! (simply typed l-calculus) if there are some
simply typed term t, type T and context  such that:
erase(t)=m Æ  ` t : T
CS5205
Intro to Type System
30
Typing Rule for Functions
• First attempt:
t2 : T2
l x:T1 . t2 : T1 ! T2
• But t2:T2 can assume that x has type T1
CS5205
Intro to Type System
31
Need for Type Assumptions
• Typing relation becomes ternary
x:T1 ` t2 : T2
l x:T1.t2 : T1 ! T2
• For nested functions, we may need several assumptions.
CS5205
Intro to Type System
32
Typing Context
• A typing context is a finite map from variables to their
types.
• Examples:
x : Bool
x : Bool, y : Bool ! Bool, z : (Bool ! Bool) ! Bool
CS5205
Intro to Type System
33
Type Rule for Abstraction
Shall use  to denote typing context.
, x:T1 ` t2 : T2
 ` l x:T1.t2 : T1 ! T2
CS5205
Intro to Type System
(T-Abs)
34
Other Type Rules
• Variable
x:T 2 
(T-Var)
 ` x:T
• Application
 ` t1 : T1 ! T2
 ` t2 : T1
 ` t1 t2 : T2
(T-App)
• Boolean Terms.
CS5205
Intro to Type System
35
Typing Rules
True : Bool (T-true)
False : Bool (T-false)
t1:Bool t2:T t3:T
if t1 then t2 else t3 : T
t : Nat
(T-Succ)
succ t : Nat
CS5205
0 : Nat (T-Zero)
(T-If)
t : Nat
(T-Pred)
pred t : Nat
Intro to Type System
t : Nat
(T-Iszero)
iszero t : Bool
36
Example of Typing Derivation
x : Bool 2 x : Bool
x : Bool ` x : Bool
(T-Var)
` (l x : Bool. x) : Bool ! Bool
(T-Abs)
` (l x : Bool. x) true : Bool
CS5205
Intro to Type System
` true : Bool
(T-True)
(T-App)
37
Canonical Forms
• If v is a value of type Bool, then v is either true or false.
• If v is a value of type T1 ! T2, then v=l x:T1. t2 where t:T2
CS5205
Intro to Type System
38
Progress
Suppose t is a closed well-typed term (that is {} ` t : T
for some T).
Then either t is a value or else there is some t’
such that t ! t’.
CS5205
Intro to Type System
39
Preservation of Types (under Substitution)
If ,x:S ` t : T and  ` s : S
then  ` [x a s]t : T
CS5205
Intro to Type System
40
Preservation of Types (under reduction)
If  ` t : T and t ! t’
then  ` t’ : T
CS5205
Intro to Type System
41
Motivation for Typing
• Evaluation of a term either results in a value or gets
stuck!
• Typing can prove that an expression cannot get stuck.
• Typing is static and can be checked at compile-time.
CS5205
Intro to Type System
42
Normal Form
A term t is a normal form if there is no t’ such that t ! t’.
The multi-step evaluation relation !* is the reflexive,
transitive closure of one-step relation.
pred (succ(pred 0))
!
pred (succ 0)
!
0
CS5205
pred (succ(pred 0))
!*
0
Intro to Type System
43
Stuckness
Evaluation may fail to reach a value:
succ (if true then false else true)
!
succ (false)
!
A term is stuck if it is a normal form but not a value.
Stuckness is a way to characterize runtime errors.
CS5205
Intro to Type System
44
Safety = Progress + Preservation
• Progress : A well-typed term is not stuck. Either it is a
value, or it can take a step according to the evaluation
rules.
Suppose t is a well-typed term (that is t:T for some T).
Then either t is a value or else there is some t‘ with t ! t‘
CS5205
Intro to Type System
45
Safety = Progress + Preservation
• Preservation : If a well-typed term takes a step of
evaluation, then the resulting term is also well-typed.
If t:T Æ t ! t‘ then t’:T .
CS5205
Intro to Type System
46