1 / 38

Unit-II

Unit-II. Structuring of data. Built in and primitive types Data aggregates and type constructors, Cartesian product, Finite mapping User-defined types and abstract data types, Type systems, Static versus dynamic program checking , Strong typing and type checking ,

kennedyj
Download Presentation

Unit-II

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. Unit-II Structuring of data

  2. Built in and primitive types • Data aggregates and type constructors, • Cartesian product, • Finite mapping • User-defined types and abstract data types, • Type systems, • Static versus dynamic program checking, • Strong typing and type checking, • Type compatibility, • Type conversions, • Types and subtypes, • Generic types, • monomorphic versus polymorphic type systems,

  3. Type • Programming languages organize data through the concept of type. • Types are used as a way to classify data according to different categories.

  4. Classification of data types • Data type ADT class

  5. Advantage of built in data type • Built in data type increases readability of a program and modifiability • programmer does not have access to the underlying bit string that represents a value of a certain type. • If correct data type is known then illegal operations can be easily traced out during the program translation. • Operator overloading is possible due to various built in data type etc

  6. Primitive data type • Some types can be called primitive (or elementary). That is, they are not built from other types • Their values are atomic, and cannot be decomposed into simpler constituents. • In most cases, built-in types can be interchangeably used with primitive types, but there are exceptions. • in C enum is primitive data type which is used to define new constants ,one would write:

  7. An enumeration data type that consists of integral constants. • To define an enumeration, keyword enum is used. • enum flag { const1, const2, ..., constN }; • Here, name of the enumeration is flag. • And, const1, const2,...., constN are values of type flag. • By default, const1 is 0, const2is 1 and so on. You can change default values of enum elements during declaration (if necessary)

  8. Data aggregates and type constructors • Programming languages allow the programmer to specify aggregations of elementary data • For aggregating the objects certain constructors are provided • Then resulting objects are called compound objects. • Ex. Array constructor int a[10] • This constructor is aggregation of similar data type elements. • If array is integer then all the integers are bound together under one entity called array

  9. Older programming languages, such as FORTRAN and COBOL, provided only a limited number of constructors. • For example, FORTRAN only provided the array constructor; • COBOL only provided the record constructor.

  10. Cartesian product • The Cartesian product of n sets A1, A2, . . ., An, denoted by • A1 x A2 x . . . x An, is a set whose elements are ordered n • tuples (a1, a2, . . ., an), where each akbelongs to Ak. • For example, regular polygons (ex. Triangle)might be described by an • integer–the number of edges • Real/float–the length of each edge. • A polygon would thus be an element in the Cartesian product integer x real.

  11. Examples of Cartesian product constructors in programming languages are • structuresin C, C++, Algol 68 and PL/I • records in COBOL, P • COBOL was the first language to introduce Cartesian products, which proved to be very useful in data processing applications.

  12. As an example of a Cartesian product constructor, consider the following C declaration, which defines a new type reg_polygon and two objects pol_a and pol_b; • structreg_polygon • { • intno_of_edges; • float edge_size; • }; • structreg_polygonpol_a, pol_b= {3, 3.45};

  13. Finite mapping • A finite mapping is a association list • a function from a finite set of values of a domain type DT (set 1)onto values of a range type RT(set 2) • array constructors are provided to define finite mappings in programming languages.

  14. DT  RT • intm[10] has two sets • 1 Subrange(domain) 2. integer/float • 0..9 (int) intelements • Finite mapping in the simplest form that maps a set of index to any data type

  15. Two types • Intensional mapping • Extensional mapping 

  16. Intensional mapping • is a routine in which the range type values are directly associated with domain type values. • Extensional mapping • is a routine in which the Range type values are associated as data aggregates. • Ex. Int a[5]

  17. In extensional mapping using indexing techniqthe elements are mapped in given subrange • Ex. For(i=0;i<5;i++) • a[i]=i+10 • Elements 10,11,12,13 and 14 are associated with locations a[0],a[1],…..a[4] • Mapping is also done using compound value • int a[5] = { 10,11,12,13,14} is compound value of type • “array of 5 integers “ i.e arrays get initialized using compond values.

  18. C arrays provide only simple types of mappings • by restricting the domain type to be an integer subrangewhose lower bound is zero • Other programming languages, such as Pascal, • require the domain type to be an ordered discrete type. • For example, in Pascal, it is possible to declare • var x: array [2. .5] of integer; • which defines x to be an array whose domain type is the subrange 2. .5.

  19. Different Policies or choices for finite mapping • 1. compile time binding : • When program is written the subset is fixed. • And at translation time it is frozen. • FORTRAN PASCAL and C adopt this • 2. object creation time binding • When and instance of a variable is created. Subset is fixed at runtime, the binding is established at run time.

  20. User defined and abstract data types • Modern programming languages provide a way to define new data types using the built in data type such data types are called as user defined data type. • Ex. Structpoint • { int x; • Int y; • } • point a,b,c • Here a,b,c are of type point each of which contains X and Y coordinate. • By providing appropriate type names, program readability can be improved

  21. Abstract data type • Abstract data types can be defined in C++ through the class construct. • A class can be viewed as an extension of structures (or records), where fields can be both data and routines • The difference is that only some fields (declared public) are accessible from outside the class. • Non-public fields are hidden to the users of the class.

  22. Type systems • Types are a fundamental semantic concept of programming languages. • programming languages differ in the way types are defined and behave, • type system • defined as the set of rules used by the language to structure and organize its collection of types.

  23. Any attempt to manipulate objects with illegal operations is a type error. • A program is said to be type safe (or type secure ) if all operations in the program are guaranteed to alwaysapply to data of the correct type, i.e.,notype errors will ever occur

  24. Static versus dynamic program checking • We will focus on kinds of errors :that may occur in a program • Errors can be classified in two categories: • language errors • application errors. • Language errors :are syntactic and semantic errors in the use of the programming language • Application errors :are deviations of the program • behavior with respect to specifications

  25. The role of The programming language should facilitate both kinds of errors to be identified and removed.

  26. Static program checking • Computer hardware does not contain any type • Information for the representation of data storage and one can not do the type checking • Ex. If computer memory contains bit sequence 11100101100….00110 at specific point in a program execution. We can not claim that this bit sequence is of interger, charachter

  27. If we pass two parameters to the hardware primitive operation for integers then it can not check whether passed arguments are integers or not . • Because arguments are after all just a bit seq. • Hence it is not possible to spot type errors at hardware level

  28. Type checking • Implies inspecting that every operation executed by a program gets the right number of parameters of the right data type. • A=x+y*e • Compiler must find out whether every operation in statement + * = is obtaining correct no. of parameter with right datatypes

  29. In HLL implementation can provide a type checking almost for all operations. • One can perform type checking at compile time and at run time • In dynamic type checking is commonly done by having a type tag stored for every data object that shows the data type of data object. • Prolog supports dynamic type checking.

  30. Strong type and weak type system • Or strongly typed and weekly typed language • A type system is said to be strong if the constraints of type system are followed. It is guaranteed that program will not have any type error. • That language is called strongly typed language

  31. Type conversion • Many programming languages support the conversion of a value into another of a different data type. • This kind of type conversion can be implicitly or explicitly made. • Implicit conversion is also called as coercion., is automatically done. • Explicit conversion is called as casting. Is performed using extra code or routine.

  32. 1. double x,y ; • X=3 // implicit coercion • Y=(double) 5 // type cast explicitly coercion

  33. Monomorphic type system • In a strong type system can be provided where every program object like as constant,variable,routine has specific type, • It has an operation that requires an operand and it is defined by declaration. • Verification at compile time is possible for occurrence of constant ,variable or function type . • Such a system type is known as monomorphic • It means every data object is having only one datatype • Then it is called as monomprphic type system.

  34. Polymorphic type system • Every variable, data object, constant belongs to more than one type.

More Related