CS618: Program Analysis 2016-17 I
stSemester
Types and Program Analysis
Amey Karkare
karkare@cse.iitk.ac.in karkare@cse.iitb.ac.in
Department of CSE, IIT Kanpur/Bombay
karkare, CSE, IITK/B CS618 1/24
Reference Book
Types and Programming Languages by Benjamin C. Pierce
karkare, CSE, IITK/B CS618 2/24
Type: Definition
karkare, CSE, IITK/B CS618 3/24
Types in Programming
A collection ofvalues
+
karkare, CSE, IITK/B CS618 4/24
Types in Programming
A collection ofvalues
The operations that are permitted on these values
+
karkare, CSE, IITK/B CS618 4/24
Type System
A collection of rules for checking the correctness of usages of types
karkare, CSE, IITK/B CS618 5/24
Type System
A collection of rules for checking the correctness of usages of types
“Consistency” of programs
karkare, CSE, IITK/B CS618 5/24
The World of Programming Languages
Typed
karkare, CSE, IITK/B CS618 6/24
The World of Programming Languages
Typed
C, C++, Java, Python, . . .
karkare, CSE, IITK/B CS618 6/24
The World of Programming Languages
Typed
C, C++, Java, Python, . . . Untyped
karkare, CSE, IITK/B CS618 6/24
The World of Programming Languages
Typed
C, C++, Java, Python, . . . Untyped
Assembly,any other?
karkare, CSE, IITK/B CS618 6/24
The World of Programming Languages
Statically Typed Dynamically Typed Strongly Typed
Weekly Typed
karkare, CSE, IITK/B CS618 7/24
The World of Programming Languages
Statically Typed Dynamically Typed Strongly Typed ML, Haskell, Pascal
(almost), Java (al- most)
Weekly Typed
karkare, CSE, IITK/B CS618 7/24
The World of Programming Languages
Statically Typed Dynamically Typed Strongly Typed ML, Haskell, Pascal
(almost), Java (al- most)
Lisp, Scheme
Weekly Typed
karkare, CSE, IITK/B CS618 7/24
The World of Programming Languages
Statically Typed Dynamically Typed Strongly Typed ML, Haskell, Pascal
(almost), Java (al- most)
Lisp, Scheme
Weekly Typed C, C++
karkare, CSE, IITK/B CS618 7/24
The World of Programming Languages
Statically Typed Dynamically Typed Strongly Typed ML, Haskell, Pascal
(almost), Java (al- most)
Lisp, Scheme
Weekly Typed C, C++ Perl
karkare, CSE, IITK/B CS618 7/24
Applications of Type-based Analyses
Error Detection
karkare, CSE, IITK/B CS618 8/24
Applications of Type-based Analyses
Error Detection Language Safety
karkare, CSE, IITK/B CS618 8/24
Applications of Type-based Analyses
Error Detection Language Safety Verification
karkare, CSE, IITK/B CS618 8/24
Applications of Type-based Analyses
Error Detection Language Safety Verification Abstraction
karkare, CSE, IITK/B CS618 8/24
Applications of Type-based Analyses
Error Detection Language Safety Verification Abstraction Documentation
karkare, CSE, IITK/B CS618 8/24
Applications of Type-based Analyses
Error Detection Language Safety Verification Abstraction Documentation Maintenance
karkare, CSE, IITK/B CS618 8/24
Applications of Type-based Analyses
Error Detection Language Safety Verification Abstraction Documentation Maintenance Efficiency
karkare, CSE, IITK/B CS618 8/24
Untyped Arithmetic Expression Language
t
:= – termskarkare, CSE, IITK/B CS618 9/24
Untyped Arithmetic Expression Language
t
:= – termstrue – constant true
karkare, CSE, IITK/B CS618 9/24
Untyped Arithmetic Expression Language
t
:= – termstrue – constant true
false – constant false
karkare, CSE, IITK/B CS618 9/24
Untyped Arithmetic Expression Language
t
:= – termstrue – constant true
false – constant false if
t
thent
elset
– conditionalkarkare, CSE, IITK/B CS618 9/24
Untyped Arithmetic Expression Language
t
:= – termstrue – constant true
false – constant false if
t
thent
elset
– conditional0 – constant zero
karkare, CSE, IITK/B CS618 9/24
Untyped Arithmetic Expression Language
t
:= – termstrue – constant true
false – constant false if
t
thent
elset
– conditional0 – constant zero
succ
t
– successorkarkare, CSE, IITK/B CS618 9/24
Untyped Arithmetic Expression Language
t
:= – termstrue – constant true
false – constant false if
t
thent
elset
– conditional0 – constant zero
succ
t
– successorpred
t
– predecessorkarkare, CSE, IITK/B CS618 9/24
Untyped Arithmetic Expression Language
t
:= – termstrue – constant true
false – constant false if
t
thent
elset
– conditional0 – constant zero
succ
t
– successorpred
t
– predecessoriszero
t
– zero testkarkare, CSE, IITK/B CS618 9/24
Syntax: Inductive Definition
The set oftermsis the smallest setT such that
karkare, CSE, IITK/B CS618 10/24
Syntax: Inductive Definition
The set oftermsis the smallest setT such that 1. {true,false,0} ⊆ T
karkare, CSE, IITK/B CS618 10/24
Syntax: Inductive Definition
The set oftermsis the smallest setT such that 1. {true,false,0} ⊆ T
2. ift1∈ T, then{succt1,predt1,iszerot1} ⊆ T
karkare, CSE, IITK/B CS618 10/24
Syntax: Inductive Definition
The set oftermsis the smallest setT such that 1. {true,false,0} ⊆ T
2. ift1∈ T, then{succt1,predt1,iszerot1} ⊆ T
3. ift1∈ T,t2∈ T, andt3∈ T thenift1thent2elset3∈ T
karkare, CSE, IITK/B CS618 10/24
Syntax: Inference Rules
The set ofterms,T is defined by the following rules:
karkare, CSE, IITK/B CS618 11/24
Syntax: Inference Rules
The set ofterms,T is defined by the following rules:
true∈ T false∈ T 0∈ T
karkare, CSE, IITK/B CS618 11/24
Syntax: Inference Rules
The set ofterms,T is defined by the following rules:
true∈ T false∈ T 0∈ T t1∈ T
succt1∈ T
t1∈ T predt1∈ T
t1∈ T iszerot1∈ T
karkare, CSE, IITK/B CS618 11/24
Syntax: Inference Rules
The set ofterms,T is defined by the following rules:
true∈ T false∈ T 0∈ T t1∈ T
succt1∈ T
t1∈ T predt1∈ T
t1∈ T iszerot1∈ T
t1∈ T t2∈ T t3∈ T
ift1thent2elset3∈ T
karkare, CSE, IITK/B CS618 11/24
Concrete Syntax
S0 = ∅
karkare, CSE, IITK/B CS618 12/24
Concrete Syntax
S0 = ∅
Si+1 = {true,false,0}
karkare, CSE, IITK/B CS618 12/24
Concrete Syntax
S0 = ∅
Si+1 = {true,false,0}
∪ {succt1,predt1,iszerot1|t1∈ Si}
karkare, CSE, IITK/B CS618 12/24
Concrete Syntax
S0 = ∅
Si+1 = {true,false,0}
∪ {succt1,predt1,iszerot1|t1∈ Si}
∪ {ift1thent2elset3|t1,t2,t2∈ Si}
karkare, CSE, IITK/B CS618 12/24
Concrete Syntax
S0 = ∅
Si+1 = {true,false,0}
∪ {succt1,predt1,iszerot1|t1∈ Si}
∪ {ift1thent2elset3|t1,t2,t2∈ Si} LetS=S
iSi.
karkare, CSE, IITK/B CS618 12/24
Concrete Syntax
S0 = ∅
Si+1 = {true,false,0}
∪ {succt1,predt1,iszerot1|t1∈ Si}
∪ {ift1thent2elset3|t1,t2,t2∈ Si} LetS=S
iSi. Then,T =S.
karkare, CSE, IITK/B CS618 12/24
Induction on Terms
Anyt∈ T
karkare, CSE, IITK/B CS618 13/24
Induction on Terms
Anyt∈ T
Either a ground term, i.e. ∈ {true,false,0}
karkare, CSE, IITK/B CS618 13/24
Induction on Terms
Anyt∈ T
Either a ground term, i.e. ∈ {true,false,0}
Or is created from some smaller terms∈ T
karkare, CSE, IITK/B CS618 13/24
Induction on Terms
Anyt∈ T
Either a ground term, i.e. ∈ {true,false,0}
Or is created from some smaller terms∈ T
Allows for inductive definitions and inductive proofs.
karkare, CSE, IITK/B CS618 13/24
Induction on Terms
Anyt∈ T
Either a ground term, i.e. ∈ {true,false,0}
Or is created from some smaller terms∈ T
Allows for inductive definitions and inductive proofs.
Three sample inductive properties
karkare, CSE, IITK/B CS618 13/24
Induction on Terms
Anyt∈ T
Either a ground term, i.e. ∈ {true,false,0}
Or is created from some smaller terms∈ T
Allows for inductive definitions and inductive proofs.
Three sample inductive properties Consts(t)
karkare, CSE, IITK/B CS618 13/24
Induction on Terms
Anyt∈ T
Either a ground term, i.e. ∈ {true,false,0}
Or is created from some smaller terms∈ T
Allows for inductive definitions and inductive proofs.
Three sample inductive properties Consts(t)
size(t)
karkare, CSE, IITK/B CS618 13/24
Induction on Terms
Anyt∈ T
Either a ground term, i.e. ∈ {true,false,0}
Or is created from some smaller terms∈ T
Allows for inductive definitions and inductive proofs.
Three sample inductive properties Consts(t)
size(t) depth(t)
karkare, CSE, IITK/B CS618 13/24
Consts
The set of constants in a term
t
.karkare, CSE, IITK/B CS618 14/24
Consts
The set of constants in a term
t
.Consts(true) = {true}
karkare, CSE, IITK/B CS618 14/24
Consts
The set of constants in a term
t
.Consts(true) = {true}
Consts(false) = {false}
karkare, CSE, IITK/B CS618 14/24
Consts
The set of constants in a term
t
.Consts(true) = {true}
Consts(false) = {false}
Consts(0) = {0}
karkare, CSE, IITK/B CS618 14/24
Consts
The set of constants in a term
t
.Consts(true) = {true}
Consts(false) = {false}
Consts(0) = {0}
Consts(succt) = Consts(t)
karkare, CSE, IITK/B CS618 14/24
Consts
The set of constants in a term
t
.Consts(true) = {true}
Consts(false) = {false}
Consts(0) = {0}
Consts(succt) = Consts(t) Consts(predt) = Consts(t)
karkare, CSE, IITK/B CS618 14/24
Consts
The set of constants in a term
t
.Consts(true) = {true}
Consts(false) = {false}
Consts(0) = {0}
Consts(succt) = Consts(t) Consts(predt) = Consts(t) Consts(iszerot) = Consts(t)
karkare, CSE, IITK/B CS618 14/24
Consts
The set of constants in a term
t
.Consts(true) = {true}
Consts(false) = {false}
Consts(0) = {0}
Consts(succt) = Consts(t) Consts(predt) = Consts(t) Consts(iszerot) = Consts(t) Consts(ift1thent2elset3) = Consts(t1)
∪Consts(t2)
∪Consts(t3)
karkare, CSE, IITK/B CS618 14/24
size
The number of nodes in the abstract syntax tree of a term
t
.karkare, CSE, IITK/B CS618 15/24
size
The number of nodes in the abstract syntax tree of a term
t
.size(true) = 1
karkare, CSE, IITK/B CS618 15/24
size
The number of nodes in the abstract syntax tree of a term
t
.size(true) = 1 size(false) = 1
karkare, CSE, IITK/B CS618 15/24
size
The number of nodes in the abstract syntax tree of a term
t
.size(true) = 1 size(false) = 1 size(0) = 1
karkare, CSE, IITK/B CS618 15/24
size
The number of nodes in the abstract syntax tree of a term
t
.size(true) = 1 size(false) = 1 size(0) = 1
size(succt) = size(t) +1
karkare, CSE, IITK/B CS618 15/24
size
The number of nodes in the abstract syntax tree of a term
t
.size(true) = 1 size(false) = 1 size(0) = 1
size(succt) = size(t) +1 size(predt) = size(t) +1
karkare, CSE, IITK/B CS618 15/24
size
The number of nodes in the abstract syntax tree of a term
t
.size(true) = 1 size(false) = 1 size(0) = 1
size(succt) = size(t) +1 size(predt) = size(t) +1 size(iszerot) = size(t) +1
karkare, CSE, IITK/B CS618 15/24
size
The number of nodes in the abstract syntax tree of a term
t
.size(true) = 1 size(false) = 1 size(0) = 1
size(succt) = size(t) +1 size(predt) = size(t) +1 size(iszerot) = size(t) +1
size(ift1thent2elset3) = size(t1) +size(t2) +size(t3)
karkare, CSE, IITK/B CS618 15/24
depth
The maximum depth of the abstract syntax tree of a term
t
.Equivalently, the smallesti such thatt∈ Si.
karkare, CSE, IITK/B CS618 16/24
depth
The maximum depth of the abstract syntax tree of a term
t
.Equivalently, the smallesti such thatt∈ Si. depth(true) = 1
karkare, CSE, IITK/B CS618 16/24
depth
The maximum depth of the abstract syntax tree of a term
t
.Equivalently, the smallesti such thatt∈ Si.
depth(true) = 1 depth(false) = 1
karkare, CSE, IITK/B CS618 16/24
depth
The maximum depth of the abstract syntax tree of a term
t
.Equivalently, the smallesti such thatt∈ Si.
depth(true) = 1 depth(false) = 1 depth(0) = 1
karkare, CSE, IITK/B CS618 16/24
depth
The maximum depth of the abstract syntax tree of a term
t
.Equivalently, the smallesti such thatt∈ Si.
depth(true) = 1 depth(false) = 1 depth(0) = 1
depth(succt) = depth(t) +1
karkare, CSE, IITK/B CS618 16/24
depth
The maximum depth of the abstract syntax tree of a term
t
.Equivalently, the smallesti such thatt∈ Si.
depth(true) = 1 depth(false) = 1 depth(0) = 1
depth(succt) = depth(t) +1 depth(predt) = depth(t) +1
karkare, CSE, IITK/B CS618 16/24
depth
The maximum depth of the abstract syntax tree of a term
t
.Equivalently, the smallesti such thatt∈ Si.
depth(true) = 1 depth(false) = 1 depth(0) = 1
depth(succt) = depth(t) +1 depth(predt) = depth(t) +1 depth(iszerot) = depth(t) +1
karkare, CSE, IITK/B CS618 16/24
depth
The maximum depth of the abstract syntax tree of a term
t
.Equivalently, the smallesti such thatt∈ Si.
depth(true) = 1 depth(false) = 1 depth(0) = 1
depth(succt) = depth(t) +1 depth(predt) = depth(t) +1 depth(iszerot) = depth(t) +1
depth(ift1thent2elset3) = max(depth(t1) +depth(t2) +depth(t3)) +1
karkare, CSE, IITK/B CS618 16/24
A Simple Property of Terms
The number of distinct constants in a term
t
is no greater than the size oft
.|Consts(t)| ≤size(t)
karkare, CSE, IITK/B CS618 17/24
A Simple Property of Terms
The number of distinct constants in a term
t
is no greater than the size oft
.|Consts(t)| ≤size(t) Proof: Exercise.
karkare, CSE, IITK/B CS618 17/24
The Set of Values
v
:= – valueskarkare, CSE, IITK/B CS618 18/24
The Set of Values
v
:= – valuestrue – value true
karkare, CSE, IITK/B CS618 18/24
The Set of Values
v
:= – valuestrue – value true
false – value false
karkare, CSE, IITK/B CS618 18/24
The Set of Values
v
:= – valuestrue – value true
false – value false
0 – value zero
karkare, CSE, IITK/B CS618 18/24
The Set of Values
v
:= – valuestrue – value true
false – value false
0 – value zero
succ
v
– successor valuekarkare, CSE, IITK/B CS618 18/24
Small-step Operational Semantics
t→t′ denotes “tevaluates tot′ in one step”
karkare, CSE, IITK/B CS618 19/24
Small-step Operational Semantics
t→t′ denotes “tevaluates tot′ in one step”
if true thent2elset3→t2
karkare, CSE, IITK/B CS618 19/24
Small-step Operational Semantics
t→t′ denotes “tevaluates tot′ in one step”
if true thent2elset3→t2 if false thent2elset3→t3
karkare, CSE, IITK/B CS618 19/24
Small-step Operational Semantics
t→t′ denotes “tevaluates tot′ in one step”
if true thent2elset3→t2 if false thent2elset3→t3
t1→t′
1
ift1thent2elset3→ift′
1thent2elset3
karkare, CSE, IITK/B CS618 19/24
Small-step Operational Semantics (contd. . . )
t→t′ denotes “tevaluates tot′ in one step”
t1→t′
1
succt1 →succt′
1
karkare, CSE, IITK/B CS618 20/24
Small-step Operational Semantics (contd. . . )
t→t′ denotes “tevaluates tot′ in one step”
t1→t′
1
succt1 →succt′
1
pred0→0
karkare, CSE, IITK/B CS618 20/24
Small-step Operational Semantics (contd. . . )
t→t′ denotes “tevaluates tot′ in one step”
t1→t′
1
succt1 →succt′
1
pred0→0 pred(succv)→v
karkare, CSE, IITK/B CS618 20/24
Small-step Operational Semantics (contd. . . )
t→t′ denotes “tevaluates tot′ in one step”
t1→t′
1
succt1 →succt′
1
pred0→0 pred(succv)→v
t1→t′
1
predt1 →predt′
1
karkare, CSE, IITK/B CS618 20/24
Small-step Operational Semantics (contd. . . )
t→t′ denotes “tevaluates tot′ in one step”
iszero0→true
karkare, CSE, IITK/B CS618 21/24
Small-step Operational Semantics (contd. . . )
t→t′ denotes “tevaluates tot′ in one step”
iszero0→true iszero(succv)→false
karkare, CSE, IITK/B CS618 21/24
Small-step Operational Semantics (contd. . . )
t→t′ denotes “tevaluates tot′ in one step”
iszero0→true iszero(succv)→false
t1→t′
1
iszerot1 →iszerot′
1
karkare, CSE, IITK/B CS618 21/24
Normal Form
A term istin normal form if no evaluation rule applies to it.
karkare, CSE, IITK/B CS618 22/24
Normal Form
A term istin normal form if no evaluation rule applies to it.
In other words, there is not′ such thatt→t′.
karkare, CSE, IITK/B CS618 22/24
Evaluation Sequence
An evaluation sequence starting from a termtis a (finite or infinite) sequence of termst1,t2, . . ., such that
t→t1 t1→t2 etc.
karkare, CSE, IITK/B CS618 23/24
Stuck Term
A term is said to bestuckif it is a normal form but not a value.
karkare, CSE, IITK/B CS618 24/24
Stuck Term
A term is said to bestuckif it is a normal form but not a value.
A simple notion of “run-time type error”
karkare, CSE, IITK/B CS618 24/24