4500/6500 Programming Languages What Are Types? Different Definitions

4500/6500 Programming Languages What Are Types? Different Definitions

What are Types? ! Denotational: Collection of values from domain CSCI: 4500/6500 Programming » integers, doubles, characters ! Abstraction: Collection of operations that can be Languages applied to entities of that type ! Structural: Internal structure of a bunch of data, described down to the level of a small set of Types fundamental types ! Implementers: Equivalence classes 1 Maria Hybinette, UGA Maria Hybinette, UGA 2 Different Definitions: Types versus Typeless What are types good for? ! Documentation: Type for specific purposes documents 1. "Typed" or not depends on how the interpretation of the program data representations is determined » Example: Person, BankAccount, Window, Dictionary » When an operator is applied to data objects, the ! Safety: Values are used consistent with their types. interpretation of their values could be determined either by the operator or by the data objects themselves » Limit valid set of operation » Languages in which the interpretation of a data object is » Type Checking: Prevents meaningless operations (or determined by the operator applied to it are called catch enough information to be useful) typeless languages; ! Efficiency: Exploit additional information » those in which the interpretation is determined by the » Example: a type may dictate alignment at at a multiple data object itself are called typed languages. of 4, the compiler may be able to use more efficient 2. Sometimes it just means that the object does not need machine code instructions to be explicitly declared (Visual Basic, Jscript … ). ! Abstraction: Types allow programmer to think about programs at a higher level ! Polymorphism: when the compiler finds that it does not need to know certain things. Maria Hybinette, UGA 3 Maria Hybinette, UGA 4 What is type again? Weakly typed and Weak typing Looking at an object: ! Weakly typed: Can successfully perform an improper operation to an object ! Syntactically compatible: The object provides all the expected operations (type names, Type ! Weak typing (latent typing): Type constraints are function signatures, interfaces) relaxed to make programming more flexible and powerful (“no type at all typing”) ! Semantically compatible: The object's la·tent Class operations all behave in the expected way (state 1. Present or potential but not evident or active. In existence but semantics, logical axioms, proofs) not manifest. Latent talent. 2. Psychology. Present and accessible in the unconscious mind but not consciously expressed. ! Type: it provides a particular interface » Example: Duck typing: if it walks like a duck … ! Class: describes implementation constraints » Does not imply type safety or unsafety (later) Maria Hybinette, UGA 5 Maria Hybinette, UGA 6 Weak Type & Type Safety Weak Type & Type Safety ! Weak typing implicitly/automatically convert (or allow ! Weak typing implicitly/automatically convert (or allow cast) cast) when used. when used. var x = 5; // (1) var x = 5; // (1) int x = 5; var y = “hi” // (2) var y = “4” // (2) char y[] = “hi”; x + y ; // (3) x + y ; // (3) char *z = x + y; ! z points to a memory address 5 character beyond y (pointer ! Visual Basic (pre.net) arithmetic). » Left yields “5hi” » Content of that location may be outside addressable memory (dereferencing z may terminate program) » Right yields? 9 or “54”? » Unsafe ! Also program does not crash so in this respect Visual Basic appears “type-safe” Maria Hybinette, UGA 7 Maria Hybinette, UGA 8 Static versus Dynamic Type Strongly and Weakly Typed Checking ! Strongly typed typed languages prevents you from ! Static type checking: check constraints at applying an operation on data that is inappropriate compile time. » Ada, ML, Haskell ! Dynamic type checking: check constraints at » Example of not strongly typed: C’s cast operation: forces compiler to accept and allow the conversion at run-time run time even if it is in appropriate » Examples: Java, constrains the types and usage of ! Weakly typed languages: can successfully perform and all the symbols at compile time, but also has improper operation to an object (un-safe) enough runtime support to perform limited dynamic ! Weak (latently) typed languages (different checks. If it can't check something statically (array polymorphism) automatically convert (or casts) types bounds or null pointers, for example), it checks when used them dynamically. » Visual Basic (pre-.Net) Maria Hybinette, UGA 9 Maria Hybinette, UGA 10 Example: Static and Dynamic Type Checking Example Implementation var x = 5; // (1) var x; // (1) var y := “hi”; // (2) x := 5; // (2) var z = x + y; // (3) x := “hi” // (3) ! (1) declares the name x (2) associates integer ! (1) binds 5 to x (2) binds “hi” to y; and (3) value to x and (3) associates string value “hi” attempts to add x to y. to x ! Binding may be represented by pairs » Most statically typed languages illegal because (2) » (integer, 5) and (string, “hi”) and (3) bind x to values of inconsistent types ! Executing line 3 … error… » Pure dynamically typed languages would permit it because the name x would not have to have consistent type. – Dynamic languages catch errors related to misuse of values “type errors” at the time of the computation of the statement/expression. Maria Hybinette, UGA 11 Maria Hybinette, UGA 12 Advantages of Static Typing Implicit and Explicit Types ! More reliable [ ? ] ! Explicit (Manifest) Typing: Types appears as ! more efficient at run time [ yes! ] syntax in the program » int a, char b; C++/C ! Implicit Typing: Can infer type, not part of the text of the program » ML and Scheme Maria Hybinette, UGA 13 Maria Hybinette, UGA 14 Type Equivalence Structural Equivalence struct student { string name; string address;} ! Determining when the types of two values are struct school { string name; string address;} the same struct student { string name; string address;} ! The same component, put together the same struct school { string name; string address;} » Algol, early Pascal, C (with exception) and ML ! Straightforward and easy to implement ! Are these the same? ! Definition varies from language to language » Does ORDER of the fields matter ! Yes! Example is structurally equivalent Maria Hybinette, UGA 15 Maria Hybinette, UGA 16 Strict & Loose Name Name (Nominative) Equivalence Equivalence struct student { string name; string address;} ! Aliasing: Multiple names for the same type struct school { string name; string address;} » typdef in C/C++ ! More popular recently » TYPE new_type = old_type in Modula 2 ! A new name or definition = a new type ! Subtyping: explicit naming of parent » Java, current Pascal, Ada ! Assume that: if programmer wrote two ! Strict: aliases are distinct types different definitions, then wanted two types ! Loose: aliases are equivalent types » Is this a good or bad assumption to make? ! Example: No! not name equivalent Maria Hybinette, UGA 17 Maria Hybinette, UGA 18 Duck Typing Duck typing in Python ! Variable itself determines what it can do, if it ! Pythonic programming style that determines an implements a certain interface. object's type by inspection of its method or attribute signature rather than by explicit relationship to some ! Object is interchangeable with any object that type object implements the same interface, no matter whether the two have related inheritance ! By emphasizing interfaces rather than specific types, hierarchy well-designed code improves its flexibility by allowing polymorphic substitution. » Templates in C++ is similar but cannot do it at run-time ! Duck-typing avoids tests using type() or isinstance(). Instead, it typically employs hasattr() tests or EAFP if it walks like a duck, and talks like a duck, then it might as [Easier to Ask Forgiveness than Permission] well be a duck. One can also say that the language ducks programming. the issue of typing. “duck the issue of typing” Maria Hybinette, UGA 19 Maria Hybinette, UGA 20 Duck Typing: Python's File Like classes Duck typing in Java ! Classes can implement some or all of the methods of ! 2003, Dave Orme, leader of Eclipse's Visual Editor Project file and be used where files would normally be used. » Needed a generic way to bind any SWT control to any » For example, GzipFile implements a file-like object for JavaBeans-style object, accessing gzip-compressed data. » Observed SWT reuses names across its class hierarchy. » cStringIO allows treating a Python string as a file. – For example, to set captions set the “Text property” » Sockets and files share many of the same methods as ! true for an SWT Shell (window), a Text control, a Label, and many more well, however, SWT controls – sockets lack the tell() method and can't be used everywhere » Realized that if he could implement data binding in terms of the a GzipFile can be used. methods that are implemented on a control, » a file-like object can implement only methods it is able to, – could improve re-use instead of implementing separate data and consequently it can be only used in situations where binding for an SWT Shell, Text, Label, and so on. it makes sense. – Flexibility of Duck typing! » Created a class that makes duck typing simple and natural for Java programmers. Maria Hybinette, UGA 21 Maria Hybinette, UGA 22 Static Strong Nominative Language Safety Dynamic Weak Structural Assembly none weak unsafe structural BASIC static weak safe nominative C/C++ static weak unsafe structural Fortran static strong safe nominative Data Types Java static strong safe nominative C# static weak unsafe nominative ML static strong safe structural Scheme dynamic

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us