• No results found

Types and Program Analysis

N/A
N/A
Protected

Academic year: 2022

Share "Types and Program Analysis"

Copied!
100
0
0

Loading.... (view fulltext now)

Full text

(1)

CS618: Program Analysis 2016-17 I

st

Semester

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

(2)

Reference Book

Types and Programming Languages by Benjamin C. Pierce

karkare, CSE, IITK/B CS618 2/24

(3)

Type: Definition

karkare, CSE, IITK/B CS618 3/24

(4)

Types in Programming

A collection ofvalues

+

karkare, CSE, IITK/B CS618 4/24

(5)

Types in Programming

A collection ofvalues

The operations that are permitted on these values

+

karkare, CSE, IITK/B CS618 4/24

(6)

Type System

A collection of rules for checking the correctness of usages of types

karkare, CSE, IITK/B CS618 5/24

(7)

Type System

A collection of rules for checking the correctness of usages of types

“Consistency” of programs

karkare, CSE, IITK/B CS618 5/24

(8)

The World of Programming Languages

Typed

karkare, CSE, IITK/B CS618 6/24

(9)

The World of Programming Languages

Typed

C, C++, Java, Python, . . .

karkare, CSE, IITK/B CS618 6/24

(10)

The World of Programming Languages

Typed

C, C++, Java, Python, . . . Untyped

karkare, CSE, IITK/B CS618 6/24

(11)

The World of Programming Languages

Typed

C, C++, Java, Python, . . . Untyped

Assembly,any other?

karkare, CSE, IITK/B CS618 6/24

(12)

The World of Programming Languages

Statically Typed Dynamically Typed Strongly Typed

Weekly Typed

karkare, CSE, IITK/B CS618 7/24

(13)

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

(14)

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

(15)

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

(16)

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

(17)

Applications of Type-based Analyses

Error Detection

karkare, CSE, IITK/B CS618 8/24

(18)

Applications of Type-based Analyses

Error Detection Language Safety

karkare, CSE, IITK/B CS618 8/24

(19)

Applications of Type-based Analyses

Error Detection Language Safety Verification

karkare, CSE, IITK/B CS618 8/24

(20)

Applications of Type-based Analyses

Error Detection Language Safety Verification Abstraction

karkare, CSE, IITK/B CS618 8/24

(21)

Applications of Type-based Analyses

Error Detection Language Safety Verification Abstraction Documentation

karkare, CSE, IITK/B CS618 8/24

(22)

Applications of Type-based Analyses

Error Detection Language Safety Verification Abstraction Documentation Maintenance

karkare, CSE, IITK/B CS618 8/24

(23)

Applications of Type-based Analyses

Error Detection Language Safety Verification Abstraction Documentation Maintenance Efficiency

karkare, CSE, IITK/B CS618 8/24

(24)

Untyped Arithmetic Expression Language

t

:= – terms

karkare, CSE, IITK/B CS618 9/24

(25)

Untyped Arithmetic Expression Language

t

:= – terms

true – constant true

karkare, CSE, IITK/B CS618 9/24

(26)

Untyped Arithmetic Expression Language

t

:= – terms

true – constant true

false – constant false

karkare, CSE, IITK/B CS618 9/24

(27)

Untyped Arithmetic Expression Language

t

:= – terms

true – constant true

false – constant false if

t

then

t

else

t

– conditional

karkare, CSE, IITK/B CS618 9/24

(28)

Untyped Arithmetic Expression Language

t

:= – terms

true – constant true

false – constant false if

t

then

t

else

t

– conditional

0 – constant zero

karkare, CSE, IITK/B CS618 9/24

(29)

Untyped Arithmetic Expression Language

t

:= – terms

true – constant true

false – constant false if

t

then

t

else

t

– conditional

0 – constant zero

succ

t

– successor

karkare, CSE, IITK/B CS618 9/24

(30)

Untyped Arithmetic Expression Language

t

:= – terms

true – constant true

false – constant false if

t

then

t

else

t

– conditional

0 – constant zero

succ

t

– successor

pred

t

– predecessor

karkare, CSE, IITK/B CS618 9/24

(31)

Untyped Arithmetic Expression Language

t

:= – terms

true – constant true

false – constant false if

t

then

t

else

t

– conditional

0 – constant zero

succ

t

– successor

pred

t

– predecessor

iszero

t

– zero test

karkare, CSE, IITK/B CS618 9/24

(32)

Syntax: Inductive Definition

The set oftermsis the smallest setT such that

karkare, CSE, IITK/B CS618 10/24

(33)

Syntax: Inductive Definition

The set oftermsis the smallest setT such that 1. {true,false,0} ⊆ T

karkare, CSE, IITK/B CS618 10/24

(34)

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

(35)

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

(36)

Syntax: Inference Rules

The set ofterms,T is defined by the following rules:

karkare, CSE, IITK/B CS618 11/24

(37)

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

(38)

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

(39)

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

(40)

Concrete Syntax

S0 = ∅

karkare, CSE, IITK/B CS618 12/24

(41)

Concrete Syntax

S0 = ∅

Si+1 = {true,false,0}

karkare, CSE, IITK/B CS618 12/24

(42)

Concrete Syntax

S0 = ∅

Si+1 = {true,false,0}

∪ {succt1,predt1,iszerot1|t1∈ Si}

karkare, CSE, IITK/B CS618 12/24

(43)

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

(44)

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

(45)

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

(46)

Induction on Terms

Anyt∈ T

karkare, CSE, IITK/B CS618 13/24

(47)

Induction on Terms

Anyt∈ T

Either a ground term, i.e. ∈ {true,false,0}

karkare, CSE, IITK/B CS618 13/24

(48)

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

(49)

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

(50)

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

(51)

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

(52)

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

(53)

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

(54)

Consts

The set of constants in a term

t

.

karkare, CSE, IITK/B CS618 14/24

(55)

Consts

The set of constants in a term

t

.

Consts(true) = {true}

karkare, CSE, IITK/B CS618 14/24

(56)

Consts

The set of constants in a term

t

.

Consts(true) = {true}

Consts(false) = {false}

karkare, CSE, IITK/B CS618 14/24

(57)

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

(58)

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

(59)

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

(60)

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

(61)

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

(62)

size

The number of nodes in the abstract syntax tree of a term

t

.

karkare, CSE, IITK/B CS618 15/24

(63)

size

The number of nodes in the abstract syntax tree of a term

t

.

size(true) = 1

karkare, CSE, IITK/B CS618 15/24

(64)

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

(65)

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

(66)

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

(67)

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

(68)

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

(69)

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

(70)

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

(71)

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

(72)

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

(73)

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

(74)

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

(75)

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

(76)

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

(77)

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

(78)

A Simple Property of Terms

The number of distinct constants in a term

t

is no greater than the size of

t

.

|Consts(t)| ≤size(t)

karkare, CSE, IITK/B CS618 17/24

(79)

A Simple Property of Terms

The number of distinct constants in a term

t

is no greater than the size of

t

.

|Consts(t)| ≤size(t) Proof: Exercise.

karkare, CSE, IITK/B CS618 17/24

(80)

The Set of Values

v

:= – values

karkare, CSE, IITK/B CS618 18/24

(81)

The Set of Values

v

:= – values

true – value true

karkare, CSE, IITK/B CS618 18/24

(82)

The Set of Values

v

:= – values

true – value true

false – value false

karkare, CSE, IITK/B CS618 18/24

(83)

The Set of Values

v

:= – values

true – value true

false – value false

0 – value zero

karkare, CSE, IITK/B CS618 18/24

(84)

The Set of Values

v

:= – values

true – value true

false – value false

0 – value zero

succ

v

– successor value

karkare, CSE, IITK/B CS618 18/24

(85)

Small-step Operational Semantics

t→t denotes “tevaluates tot in one step”

karkare, CSE, IITK/B CS618 19/24

(86)

Small-step Operational Semantics

t→t denotes “tevaluates tot in one step”

if true thent2elset3→t2

karkare, CSE, IITK/B CS618 19/24

(87)

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

(88)

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

(89)

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

(90)

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

(91)

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

(92)

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

(93)

Small-step Operational Semantics (contd. . . )

t→t denotes “tevaluates tot in one step”

iszero0→true

karkare, CSE, IITK/B CS618 21/24

(94)

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

(95)

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

(96)

Normal Form

A term istin normal form if no evaluation rule applies to it.

karkare, CSE, IITK/B CS618 22/24

(97)

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

(98)

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

(99)

Stuck Term

A term is said to bestuckif it is a normal form but not a value.

karkare, CSE, IITK/B CS618 24/24

(100)

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

References

Related documents

cbna Program verification 2016 Instructor: Ashutosh Gupta TIFR, India 2.. Where

These slides constitute the lecture notes for CS618 Program Analysis course at IIT Bombay and have been made available as teaching material accompanying the book:.. • Uday

These slides constitute the lecture notes for CS618 Program Analysis course at IIT Bombay and have been made available as teaching material accompanying the book:.. • Uday

cbna Program verification 2016 Instructor: Ashutosh Gupta TIFR, India 1.. Program

cbna Program verification 2016 Instructor: Ashutosh Gupta TIFR, India 1.. Program

cbna Program verification 2019 Instructor: Ashutosh Gupta IITB, India 1.. Program

cbna Program verification 2019 Instructor: Ashutosh Gupta IITB, India 2.. Where are we and where are

Safe approximation of flow-sensitive point-specific information for any point, for any given execution order A statement can not “override” information computed by another