type checking
play

Type Checking General properties of type systems Types in - PowerPoint PPT Presentation

Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Static Checking (Cont.) Refers to


  1. Outline Type Checking • General properties of type systems • Types in programming languages • Notation for type rules – Logical rules of inference • Common type rules 2 Static Checking Static Checking (Cont.) • Refers to the compile-time checking of Flow-of-control checks: statements that cause flow of control to leave a construct must have some place programs in order to ensure that the semantic where control can be transferred; conditions of the language are being followed e.g., break statements in C Uniqueness checks: a language may dictate that in some Examples of static checks include: contexts, an entity can be defined exactly once; e.g., identifier declarations, labels, values in case – Type checks expressions – Flow-of-control checks Name-related checks: Sometimes the same name must – Uniqueness checks appear two or more times; – Name-related checks e.g., in Ada a loop or block can have a name that must then appear both at the beginning and at the end 3 4

  2. Types and Type Checking Type Expressions and Type Constructors • A type is a set of values together with a set A language usually provides a set of base types of operations that can be performed on them that it supports together with ways to construct other types using type constructors • The purpose of type checking is to verify that operations performed on a value are in fact Through type expressions we are able to permissible represent types that are defined in a program • The type of an identifier is typically available from declarations, but we may have to keep track of the type of intermediate expressions 5 6 Type Expressions Notions of Type Equivalence • A base type is a type expression Name equivalence: In many languages, e.g. Pascal, types can be given names. Name equivalence • A type name (e.g., a record name) is a type expression views each distinct name as a distinct type. • A type constructor applied to type expressions is a type expression. E.g., So, two type expressions are name equivalent – arrays: If T is a type expression and I is a range of if and only if they are identical. integers, then array(I,T) is a type expression Structural equivalence: Two expressions are – records: If T1, …, Tn are type expressions and f1, …, fn structurally equivalent if and only if they have are field names, then record((f1,T1),…,(fn,Tn)) is a type the same structure; i.e., if they are formed by expression – pointers: If T is a type expression, then pointer(T) is a applying the same constructor to structurally type expression equivalent type expressions. – functions: If T1, …, Tn, and T are type expressions, then so is (T1,…,Tn) → T 7 8

  3. Example of Type Equivalence Static Type Systems & their Expressiveness In the Pascal fragment • A static type system enables a compiler to detect many common programming errors • The cost is that some correct programs are type nextptr = ^node; disallowed prevptr = ^node; – Some argue for dynamic type checking instead var p : nextptr; – Others argue for more expressive static type q : prevptr; checking – But more expressive type systems are also more is not name equivalent to q , p complex but p and q are structurally equivalent. 9 10 Compile-time Representation of Types Compile-time Representation of Types (Cont.) • Need to represent type expressions in a way that Example: is both easy to construct and easy to check var x, y : array[1..42] of integer; Approach 1: Type Graphs – Basic types can have predefined “internal values”, x name e.g., small integer values ... type array – Named types can be represented using a pointer type dimensions 1 into a hash table 1 bounds integer elem type 42 – Composite type expressions: the node for f(T1,…,Tn) name contains a value representing the type constructor y f, ... the nodes for the expressions and pointers to T1,…,Tn type 11 12

  4. Compile-Time Representation of Types Compile-Time Representation of Types: Notes Approach 2: Type Encodings • Type encodings are simple and efficient Basic types use a predefined encoding of the low-order bits • On the other hand, named types and type BASIC TYPE ENCODING constructors that take more than one type boolean 0000 expression as argument are hard to represent char 0001 integer 0002 as encodings. Also, recursive types cannot be The encoding of a type expression op(T) is obtained by represented directly. concatenating the bits encoding op to the left of the encoding of T. E.g.: TYPE EXPRESSION ENCODING • Recursive types (e.g. lists, trees) are not a char 00 00 00 0001 problem for type graphs: the graph simply array(char) 00 00 01 0001 ptr(array(char)) 00 10 01 0001 contains a cycle ptr(ptr(array(char))) 10 10 01 0001 13 14 Types in an Example Programming Language Type Checking and Type Inference • Let’s assume that types are: Type Checking is the process of verifying fully typed programs – integers & floats (base types) – arrays of a base type – booleans (used in conditional expressions) Type Inference is the process of filling in missing type information • The user declares types for all identifiers • The two are different, but are often used • The compiler infers types for expressions interchangeably – Infers a type for every expression 15 16

  5. Rules of Inference Why Rules of Inference? • We have seen two examples of formal notation • Inference rules have the form specifying parts of a compiler If Hypothesis is true, then Conclusion is true – Regular expressions (for the lexer) – Context-free grammars (for the parser) • Type checking computes via reasoning If E 1 and E 2 have certain types, • The appropriate formalism for type checking then E 3 has a certain type is logical rules of inference • Rules of inference are a compact notation for “If-Then” statements 17 18 From English to an Inference Rule From English to an Inference Rule (2) • The notation is easy to read (with practice) If e 1 has type int and e 2 has type int, then e 1 + e 2 has type int • Start with a simplified system and gradually add features (e 1 has type int e 2 has type int) ∧ ⇒ e 1 + e 2 has type int • Building blocks (e 1 : int e 2 : int) ⇒ e 1 + e 2 : int ∧ – Symbol ∧ is “and” – Symbol ⇒ is “if-then” – x:T is “x has type T” 19 20

  6. From English to an Inference Rule (3) Notation for Inference Rules The statement • By tradition inference rules are written (e 1 : int e 2 : int) ⇒ e 1 + e 2 : int ∧ is a special case of Hypothesis 1 … Hypothesis n ├ ├ Conclusion Hypothesis 1 . . . ∧ Hypothesis n ⇒ Conclusion ∧ ├ This is an inference rule • Type rules have hypotheses and conclusions of the form: e : T ├ means “it is provable that . . .” • ├ 21 22 Two Rules Two Rules (Cont.) • These rules give templates describing how to type integers and + expressions i is an integer [Int] i : int ├ • By filling in the templates, we can produce complete typings for expressions e 1 : int e 2 : int ├ ├ [Add] e 1 + e 2 : int ├ 23 24

  7. Example: 1 + 2 Soundness • A type system is sound if – Whenever ├ e : T – Then e evaluates to a value of type T 1 is an integer 2 is an integer • We only want sound rules 1 : int 2 : int ├ ├ – But some sound rules are better than others: 1 + 2 : int ├ i is an integer i : number ├ 25 26 Type Checking Proofs Rules for Constants • Type checking proves facts e: T – Proof is on the structure of the AST – Proof has the shape of the AST [Bool] [Bool] – One type rule is used for each kind of AST node false : bool true : bool ├ ├ • In the type rule used for a node e: – Hypotheses are the proofs of types of e’s subexpressions f is a floating point number [Float] – Conclusion is the type of e f : float ├ • Types are computed in a bottom-up pass over the AST 27 28

  8. Two More Rules A Problem • What is the type of a variable reference? e : bool ├ x is an identifier [Not] [Var] not e : bool ├ x : ? ├ • The local, structural rule does not carry e 1 : bool e 2 : T ├ ├ enough information to give x a type [While] while e 1 do e 2 : T ├ 29 30 A Solution Type Environments • Put more information in the rules! Let E be a function from Identifiers to Types • A type environment gives types for free The sentence E ├ e : T variables is read: – A type environment is a function from Identifiers Under the assumption that variables have to Types the types given by E, it is provable that – A variable is free in an expression if it is not the expression e has the type T defined within the expression 31 32

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend