Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill.

Similar presentations


Presentation on theme: "1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill."— Presentation transcript:

1 1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill

2 2 Data Types Computers manipulate sequences of bitsComputers manipulate sequences of bits But most programs manipulate more general dataBut most programs manipulate more general data –Numbers –String –Lists –…–…–…–… Programming languages provide data types that raise the level of abstraction from bits to dataProgramming languages provide data types that raise the level of abstraction from bits to data –But computer hardware only knows about bits!

3 3 Data Types The Purpose of Types Types provide implicit contextTypes provide implicit context –Compilers can infer more information, so programmers write less code –E.g. the expression a+b in Java may be adding two integer, two floats or two strings depending on the context Types provides a set of semantically valid operationsTypes provides a set of semantically valid operations –Compilers can detect semantic mistakes –E.g. Python’s lists support append() and pop(), but complex numbers do not.

4 4 Type Systems High-level languages have type systemsHigh-level languages have type systems –All objects and expressions have a type –E.g. void (*)(const int) is the type of a C++ function A type system consists ofA type system consists of –A mechanism for defining types and associating them with certain language constructs –A set of rules for type checking »Type equivalence »Type compatibility »Type inference

5 5 Type Systems Type Checking Type checking is the process of ensuring that a program obeys the language’s type compatibility rulesType checking is the process of ensuring that a program obeys the language’s type compatibility rules Strongly typed languages always detect types errorsStrongly typed languages always detect types errors –Weakly typed languages do not –All expressions and objects must have a type –All operations must be applied in appropriate type contexts Statically typed languages are strongly typed language in which all type checking occurs at compile typeStatically typed languages are strongly typed language in which all type checking occurs at compile type –Dynamically typed languages: some checking at run-time

6 6 What is a type? Three points of view:Three points of view: –Denotational: a set of values –Constructive: a type is built-in type or a composite type »Composite types are created using type constructors »E.g. In Java, boolean is a built-in type, while boolean[] is a composite type –Abstraction-based: a type is an interface that defines a set of consistent operations These points of view complement each otherThese points of view complement each other

7 7 Classification of Types Built-in Types Built-in/Primitive/Elementary typesBuilt-in/Primitive/Elementary types –Mimic hardware units –E.g. boolean, character, integer, real (float) Their implementation varies across languagesTheir implementation varies across languages Characters are traditionally one-byte quantities using the ASCII character setCharacters are traditionally one-byte quantities using the ASCII character set –Early computers had a different byte sizes »Byte = 8 bits standardized by Fred Brooks et al. thanks to the IBM System/360 –Other character sets have also been used

8 8 Built-in Types Unicode Characters Newer languages have built-in characters that support the Unicode character setNewer languages have built-in characters that support the Unicode character set –http://www.unicode.org/unicode/standard/WhatIsUnicode. html http://www.unicode.org/unicode/standard/WhatIsUnicode. htmlhttp://www.unicode.org/unicode/standard/WhatIsUnicode. html –Unicode is implemented using two-byte quantities –E.g. Java »http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html –E.g. Python »u”¡Hola!” »http://www.python.org/doc/current/tut/node5.html#SECTION005130000 000000000000 http://www.python.org/doc/current/tut/node5.html#SECTION005130000 000000000000http://www.python.org/doc/current/tut/node5.html#SECTION005130000 000000000000

9 9 Built-in Types Numeric Types Most languages support integers and floatsMost languages support integers and floats –The range of value is implementation dependent Some languages support other numeric typesSome languages support other numeric types –Complex numbers (e.g. Fortran, Python) –Rational numbers (e.g. Scheme, Common Lisp) –Signed and unsigned integers (e.g. C, Modula-2) –Fixed point numbers (e.g. Ada) Some languages distinguish numeric types depending on their precisionSome languages distinguish numeric types depending on their precision –Single vs. double precision numbers »C’s int (4 bytes) and long (8 bytes)

10 10 Classification of Types Enumerations Enumeration improve program readability and error checkingEnumeration improve program readability and error checking They were first introduced in PascalThey were first introduced in Pascal –E.g. type weekday = (sun, mon, tue, wed, thu, fri, sat); –They define an order, so they can be used in enumeration- controlled loops –The same feature is available in C »enum weekday {sun, mon, tue, wed, thu, fri, sat}; –Other languages use constants to define enumeration –Pascal’s approach is more complete: integers and enumerations are not compatible

11 11 Classification of Types Subranges Subranges improve program readability and error checkingSubranges improve program readability and error checking They were first introduced in PascalThey were first introduced in Pascal –E.g. type test_score = 0..100; workday = mon..fri; workday = mon..fri; –They define an order, so they can be used in enumeration- controlled loops

12 12 Classification of Types Composite Types Composite/Constructed types are created applying a constructor to one or more simpler typesComposite/Constructed types are created applying a constructor to one or more simpler types ExamplesExamples –Records –Variant Records –Arrays –Sets –Pointers –Lists –Files

13 13 Classification of Types Orthogonality Orthogonality is an important property in the design of type systemsOrthogonality is an important property in the design of type systems More orthogonal languages support have more flexible composite typesMore orthogonal languages support have more flexible composite types Scheme is a good exampleScheme is a good example

14 14 Records

15 15 Records Memory Layout Basic layout in 32-bit machinesBasic layout in 32-bit machines –There may be holes in the allocation of memory

16 16 Records Memory Layout Packed layoutPacked layout Rearranging record fieldRearranging record field

17 17 Records Implications of Memory Layout More memory efficient layouts have several drawbacksMore memory efficient layouts have several drawbacks –Assignments require multiple instructions »Masking and shifting operations –Access to record elements requires multiple instructions »Masking and shifting operations Holes complicate record equalityHoles complicate record equality –Requires field by field comparison or default values in holes –Some languages forbid record comparison »E.g. Pascal, C

18 18 Variant Records A variant record provides two or more alternative fields or collections of fields, but only one bit is valid at any given timeA variant record provides two or more alternative fields or collections of fields, but only one bit is valid at any given time –They are called unions in C/C++

19 19 Variant Records Memory Layout Some languages, like C, do not check whether variant records are properly accessedSome languages, like C, do not check whether variant records are properly accessed Other languages keep track of the value in an additional field, increasing safetyOther languages keep track of the value in an additional field, increasing safety

20 20 Arrays Memory Layout Arrays are usually stored in contiguous locationsArrays are usually stored in contiguous locations Two common orders for 2D arraysTwo common orders for 2D arrays

21 21 Arrays Memory Layout Contiguous allocation vs. row pointersContiguous allocation vs. row pointers

22 22 Arrays Address Calculations Code generation for a 3D arrayCode generation for a 3D array –A: array [L 1..U 1 ] of array [L 2..U 2 ] of array [L 3..U 3 ] of elem_type

23 23 Arrays Address Calculations Row-MajorOrderCompile-TimeConstantOptimization

24 24 Pointer Data Type Contains an address or NULLContains an address or NULL Provides indirect addressing and dynamic storage managementProvides indirect addressing and dynamic storage management Operations – assignment and dereferencingOperations – assignment and dereferencing Problems – dangling pointers, memory leaksProblems – dangling pointers, memory leaks Language question – does it provide solutions to these problems? (ex. C++)Language question – does it provide solutions to these problems? (ex. C++)

25 25 Reference Data Type Like a pointer but usually more limitedLike a pointer but usually more limited C++ reference type is constant – can’t be reassignedC++ reference type is constant – can’t be reassigned Java reference type is like a pointer except safer – why?Java reference type is like a pointer except safer – why?


Download ppt "1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill."

Similar presentations


Ads by Google