Principles of programming languages 6: Types Isao Sasano Department of Information Science and...

24
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering

Transcript of Principles of programming languages 6: Types Isao Sasano Department of Information Science and...

Page 1: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Principles of programming languages 6: Types

Isao Sasano

Department of Information Science and Engineering

Page 2: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Typed languages

• Statically-typed languages– Check the consistency of types in compile time– (ex.) C, Java, Pascal, etc.

• Dynamically-typed languages– Check the consistency of types in runtime– (ex.) Lisp, Emacs Lisp, Scheme, etc.

Page 3: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

The role of types

/* example */

int f ( ) {

int x, y;

x = 4;

y = 3 + x;

return y;

}

Programs in statically-typed languages must have type consistency in compile time.

Page 4: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Type checking

Consistency of programs is checked in compile time with respect to the typing constraint of the language. Type checking partially ensures the correctness of programs and decreases the runtime errors.

Types are static semantics (information that is obtained without executing programs) and type checking is a kind of static program analysis.

Type checking is performed after the parsing in compilers.

Page 5: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Variable declarations in C

(ex.) int (*a) [13];

This declares a variable a of type pointer to array of length 13 of int.

Expressions (*a) [ j ] (0 j < 13) have type of int.

An expression (* a) [0] has type of int.

Page 6: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Variable declarations in C(ex.) int (*a) [13];

int b [2] [13];

Under the above declarations, the assignment

a = b

is consistent with respect to types. b is replaced with &b[0] in compile time. The following equation holds just after the execution of the assignment expression.

(*a) [ j ] = b[0][ j ] (0 j < 13)

This is obtained by adding [ j ] to the equations

*a = *b = *(&b[0]) = b[0].

We study how to check the type consistency of this kind of programs.

Page 7: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Variable declarations in CCan you read the following variable declaration?

char ( * ( * x ( ) ) [3] ) ( ) ;

Read the variable x firstly and then go outside according to the precedence in the next page and then finally read char.

( ) * [3] * ( ) char

By reversing this, we obtain the following.

char ( ) * [3] * ( )

By writing this after x : , we obtain the following.

x : char ( ) * [3] * ( )

We call this a declaration of x in the postfix notation.

Page 8: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Precedence

The precedence is defined as ( ), [ ], * in descending order. We can use parentheses for overriding this precedence. In the declaration char ( * ( * x ( ) ) [3] ) ( ) ; the parentheses for overriding the precedence is in bold font in the following. char ( * ( * x ( ) ) [3] ) ( ) ;By take into consideration the precedence and parentheses, we read the declaration in the following order. ( ) * [3] * ( ) char

Page 9: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Exercise 1

Rewrite the following variable declaration in C in the postfix notation.

char ( * ( * y [3] ) ( ) ) [5] ;

Page 10: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Exercise 2

Rewrite the following variable declarations (1) and (2) in C in the postfix notation.

(1) int * z;

(2) int c [13];

Page 11: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Exercise 3

Rewrite the following variable declarations (1) and (2) in C in the postfix notation.

(1) int (*a) [13];

(2) int b[2][13];

Page 12: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

An exampleUnder the variable declaration

char ( * ( * y [3] ) ( ) ) [5] ;

what type does the expression y [2] have?

By rewriting the declaration in the postfix notation, we obtain

y : char [5] * ( ) * [3]

By removing the outermost [3], we obtain

y [2] : char [5] * ( ) *

Page 13: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Exercise 4

Under the declaration

int (*a) [13];

what type does the expression *a have?

Page 14: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Inference rules

e : [ n ]

e [ i ] : e : ( ) e ( ) :

e : * * e :

We use metavariables e and for representing expressions and types respectively.

0 i < n, where n is a positive integer.

Page 15: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

An example

Under the declaration

int (*a) [13] ;

the expression *a had type of int [13] in postfix notation (cf. exercise 4). We can derive this from the type in postfix notation by applying an inference rule.

a : int [13] * *a : int [13]

Page 16: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

An example

Under the declaration

int (*a) [13] ;

the expression (*a) [3] has type of int. We can derive this from the type in postfix notation by applying two inference rules.

a : int [13] * *a : int [13] (*a) [3] : int

Page 17: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Exercise 5

Under the declaration

int b [2] [13] ;

derive the type of the expression b [1] by applying inference rules to the type of b in the postfix notation.

Page 18: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Exercise 6

Under the declaration

int b [2] [13] ;

derive the type of the expression b [1] [4] by applying inference rules to the type of b in the postfix notation.

Page 19: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Array types

We add the following inference rule about array types.

e : [ n ]

e : &The notation e : & shows that e : * holds and e does not have address (i.e., e is an non-l-value expression.)

The rule means that when the outermost is an array type we can change it to the corresponding pointer type.

Page 20: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Assignment operator =

We add an inference rule about the assignment operator =.

e : e’ : e = e’ :

where e is an l-value expression and is not a constant (i.e., is modifiable).

Page 21: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Address operator &

We add the following inference rules about the address operator &.

where the outermost part of is not &.

e : &e : &

e : & * e :

e : * e’ : &

e = e’ : &

Page 22: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

The first exampleUnder the following declarations

a : int [13] *

b : int [13] [2]

we can show that the assignment expression a = b is consistent with respect to types by applying inrefence rules to the declarations in the postfix notation.

b : int [13] [2] b : int [13] & a : int [13] *

a = b : int [13] &

Page 23: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Notes

In the full set of C, functions may have parameters. The full set have several other constructs such as structures and unions.

Note that in C the type of union is not checked, so programmers have to write programs with taking into account which of the components each union has at every moment.

Page 24: Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.

Exercise 7

Under the following declarations

p : int *

a : int [10]

show that the assignment expression

p = &a[1]

is consistent with respect to types by using the inference rules.