types different views
play

Types (different views) Value collection values having the same - PowerPoint PPT Presentation

Types (different views) Value collection values having the same properties can be collected together e.g. integers, floating points different representations for different types operations allowed for a type controlling


  1. Types (different views) • Value collection – values having the same properties can be collected together • e.g. integers, floating points – different representations for different types • operations allowed for a type • controlling the way how values are used • Type construction – more complicated types can be created from the primitive ones with a type constructor – value set and operations are seen as natural features for a type • Type abstraction – types as interfaces (modules, classes) • implementation details hidden – values can be accessed via predefined operations TUT Pervasive Computing Principles of programming languages 1 Maarit Harsu / Matti Rintala / Henri Hansen

  2. Example codes written in an imaginary language Desing issues var X: Integer; for types ... type T = array [ 1..X ] of Integer; • Is type a static or dynamic concept? – are the properties of a type known already at compile time or only at run time • Are types of variables bound type T ( Max: Integer ) = 1..Max; statically or dynamically? ... var A: T ( 10 ); • Are parametrized types enabled? – are these parameters static or dynamic procedure P ( X: Integer ); var A: T ( X ); – which parts of a type declaration can be ... parametrized • How to define type equivalence? – on which conditions two variables have the same or compatible types TUT Pervasive Computing Principles of programming languages 2 Maarit Harsu / Matti Rintala / Henri Hansen

  3. Strong Other definitions: • type errors are detected (at compile time or run time) typing • operations not supported by a type are prevented • A programming language is strongly typed if – each data item (constant, variable, field) has a unique type, the identity and properties of which are known at compile time – type conversions are controlled by interpreting the value as a value of another type • not by interpreting the value presentation as a value presentation of another type • legality of type conversions must be known at compile time • Static typing – type of each variable is known at compile time • Strong typing -> static typing TUT Pervasive Computing Principles of programming languages 3 Maarit Harsu / Matti Rintala / Henri Hansen

  4. Type conversions • Allowing implicit type conversions makes the typing of the language less stronger • Type cast int a; float d; – occurs explicitly d = ( float ) 6; • Coersion a = ( int ) 7.5; – occurs implicitly int a, b; float d; What if this is a misspelling? … The programmer intended to a = a + d; write ”a = a + b;” TUT Pervasive Computing Principles of programming languages 4 Maarit Harsu / Matti Rintala / Henri Hansen

  5. Type conversions vs. strong typing • Stronger typing (Ada) − explicit conversions allowed only in a limited sense − exception: Unchecked_conversion • Weaker typing (C) − compiler can make automatic type coersions − lack of consistency • when and what kind of coersions take place • when an explicit conversion is possible TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

  6. Typing in programming languages • Fortran (weak typing) – types are not checked in parameter passing – Equivalence statement • Pascal (nearly strong typing) – variant records are not necessarily safe • Ada (strong typing) – variant records are safe – type conversions are controlled • C and C++ (weak typing) – type checking is not compulsory in parameter passing – unions are not safe – coercions allowed • Java and C# (strong typing) TUT Pervasive Computing Principles of programming languages 6 Maarit Harsu / Matti Rintala / Henri Hansen

  7. Type equivalence (compatibility) • Name type equivalence – the name of the type identifies the type – types with different names are different types – each nameless type is different from other types • Structure type equivalence – the name of the type is a shorthand of the type definition – two types are same, if they have the same structure and each component in the structure has the same type or identifical definition • Both equivalences can be applied to both scalar types and structural types C-code: typedef int weight; typedef int length; TUT Pervasive Computing Principles of programming languages 7 Maarit Harsu / Matti Rintala / Henri Hansen

  8. Examples on type equivalence Examples var X: array [ 1..10 ] of Integer; are written Y: array [ 1..10 ] of Integer; in Pascal var X, Y: array [ 1..10 ] of Integer; type T = array [ 1..10 ] of Integer; U = array [ 1..10 ] of Integer; var X : T; type T = array [ 1..10 ] of Integer; Y: U; U = T; var X: T; Y: U; TUT Pervasive Computing Principles of programming languages 8 Maarit Harsu / Matti Rintala / Henri Hansen

  9. More examples on type equivalence Pascal-code: const C = 10; type T = array [ 1..10 ] of Integer; R = record E: Integer; D: array [ 1..C ] of Integer; end ; var X: record A: Integer; var X: array [ 1..10 ] of Integer; B: T; Y: array [ 2..11 ] of Integer; end ; Z: array [ 1..2*5 ] of Integer; Y: R; TUT Pervasive Computing Principles of programming languages 9 Maarit Harsu / Matti Rintala / Henri Hansen

  10. An algorithm to solve structure type equivalence 1. Take two type definitions. 2. In both type definitions, replace each type name with the corresponding type definition. 3. Repeat step 2 until there are no type names (except standard types like integer, float, and so on) in the type definitions. 4. Compare the resulting type definitions as texts. Identical definitions are equivalent types. TUT Pervasive Computing Principles of programming languages 10 Maarit Harsu / Matti Rintala / Henri Hansen

  11. Benefits of name type equivalence (vs. structure type equivalence) • Easier to implement • Support for safety (error detection) • Support for type abstraction • Increases readability and understandability (named types) TUT Pervasive Computing Principles of programming languages 11 Maarit Harsu / Matti Rintala / Henri Hansen

  12. Type equivalence in programming languages • Name type equivalence – Java – Ada (except for named subtypes) – C++ (except for typedef ) • Structure type equivalence – Algol68 and Modula-3 typedef int weight; typedef int length; – C • e.g. typedef does not create a new type • except for record types ( struct , union ) TUT Pervasive Computing Principles of programming languages 12 Maarit Harsu / Matti Rintala / Henri Hansen

  13. Type inference • Violations of type consistency can be checked at compile time • Consistency checkings (ML): − all occurences of the same identifier must have the same type − if x then E1 else E2 E1: ’a * int E2: string * ’b • type of x is boolean What can • E1 and E2 have the same type be inferred? − type of a function ( ’a -> ’b ) TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

  14. Type inference and polymorphism fun compare ( x, p, q ) = if x = p then if x = q then ”both” else ”first” else if x = q then ”second” else ”neither” Type of (=): ’a * ’a -> bool Type of compare: ’a * ’a * ’a -> string TUT Pervasive Computing Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen

  15. Types in programming languages • Scalar types • Enumerated types • Subrange types, subtypes, derived types • Structured types – formed with a type constuctor (array, record) • Pointer (and reference) types • Set types • Subprogram and function types – will be introduced in the context of subprograms • Task types (Ada) – associated with concurrency, will be introduced in this context TUT Pervasive Computing Principles of programming languages 15 Maarit Harsu / Matti Rintala / Henri Hansen

  16. Scalar types (primitive types, atomic types) Division 1: Division 2: • Numeric types • Ordinal types – integers – each value has a predecessor and successor – floating points (except for the minimum – decimal numbers and maximum values) • Logic type – includes enumerated types – boolean • Other types • Character type – floating points – decimal numbers TUT Pervasive Computing Principles of programming languages 16 Maarit Harsu / Matti Rintala / Henri Hansen

  17. Integers are used as • mathematical value Numeric types • loop counter • array index • Representation of a numeric value – it is most efficient to use the internal representation of a computer – variations in the representation make portability harder • Representation of integers: two’s complement – 2 n-1 ..2 n-1 -1 e.g. 16 bits: -32768..32767 s exponent fraction • Representation of floating points: 1 8 23 – follows a standard (that is a compromise) • adding bits for fraction would increase precision • adding bits for exponent would increase range • Decimal numbers (Cobol, C#) – fixed number of decimal digits, decimal point at a fixed position – precision is high, range is restricted TUT Pervasive Computing Principles of programming languages 17 Maarit Harsu / Matti Rintala / Henri Hansen

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