1 / 24

Principles of programming languages 6: Types

This text discusses the principles of programming languages, specifically focusing on static and dynamic typing, type consistency, type checking, and variable declarations in C.

grado
Download Presentation

Principles of programming languages 6: Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. 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.

  4. 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.

  5. Variable declarations in C (ex.) int(*a) [13]; This declares a variable aof 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.

  6. 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.

  7. Variable declarations in C Can 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.

  8. 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

  9. Exercise 1 Rewrite the following variable declaration in C in the postfix notation. char ( * ( * y [3] ) ( ) ) [5] ;

  10. Exercise 2 Rewrite the following variable declarations (1) and (2) in C in the postfix notation. (1) int * z; (2) int c [13];

  11. Exercise 3 Rewrite the following variable declarations (1) and (2) in C in the postfix notation. (1) int (*a) [13]; (2) intb[2][13];

  12. An example Under the variable declaration char ( * ( * y [3] ) ( ) ) [5] ; what type does the expressiony [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] * ( ) *

  13. Exercise 4 Under the declaration int (*a) [13]; what type does the expression*a have?

  14. Inference rules e : [ n ] e [ i ] :  e : ( ) e ( ) :  e : * * e :  0 i < n, where n is a positive integer. We use metavariables e and  for representing expressions and types respectively.

  15. 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]

  16. 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

  17. Exercise 5 Under the declaration intb [2] [13] ; derive the type of the expressionb [1] by applying inference rules to the type of b in the postfix notation.

  18. Exercise 6 Under the declaration int b [2] [13] ; derive the type of the expressionb [1] [4] by applying inference rules to the type of b in the postfix notation.

  19. 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.

  20. 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).

  21. Address operator & We add the following inference rules about the address operator &. e :  &e : & e : & * e :  e : * e’ : & e = e’ : & where the outermost part of  is not &.

  22. The first example Under the following declarations a : int [13] * b : int [13] [2] we can show that the assignment expressiona = 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] &

  23. 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 Cthe 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.

  24. 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.

More Related