Top Banner
CPS 506 Comparative Programming L Languages Type Systems Type Systems, Semantics and Data Tps Types
74

CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

May 02, 2018

Download

Documents

trinhkhue
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

CPS 506Comparative Programming

LLanguagesType Systems Type Systems,

Semantics and Data T p sTypes

Page 2: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type SystemsType SystemsA l t l d fi d l • A completely defined language: Defined syntax, semantics and type systemsystem

• Type: A set of values and operations– int

• Values=Z• Operations={+, -, *, /, mod}

– Boolean• Values={true, false} • Operations={AND, OR, NOT, XOR}p

2

Page 3: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type SystemsType SystemsT S st• Type System– A system of types and their associated

variables and objects in a programvariables and objects in a program

To formalize the definition of data types – To formalize the definition of data types and their usage in a programming language

– A bridge between syntax and semantics• Type checked in compile time: a part of Type checked in compile time a part of

syntax analysis• Type checked in run time: a part of semantics

3

Page 4: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Systems (con’t)Type Systems (con t)S i ll T d h i bl i • Statically Typed: each variable is associated with a single type g ypduring its life in run time.

Could be explicit or implicit –Could be explicit or implicit declaration

–Example: C and Java, Perl–Type rules are defined on abstract Type rules are defined on abstract

syntax (Static Semantics)

4

Page 5: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Systems (con’t)Type Systems (con t)D i ll T d i bl t • Dynamically Typed: a variable type can be changed in run time– Example: LISP, JavaScript, PHPJava Script example:Li t [10 2 3 5]List = [10.2 , 3.5]…List = 47List = 47– Less reliable, difficult to debug– More flexible– Fast compilation– Slow execution (Type checking in run-time)

5

Page 6: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Systems (con’t)Type Systems (con t)• Type Error: a non well defined • Type Error: a non well-defined

operation on a variable in run timeExample: union in C– Example: union in C

union flexType {int i;fl ffloat f;

};union flexType u;fl tfloat x;…u.I = 10;x = u f;x = u.f;…

– Another example in C ?

6

Page 7: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Systems (con’t)Type Systems (con t)Strongly Typed: All type errors are detected in compile or • Strongly Typed: All type errors are detected in compile or run time before execution– More reliable

E l J i l t l t d b t C i t– Example: Java is nearly strongly typed, but C is notx+1 regardless of the type x

– Coercion (implicit type conversion) rules have an effect on t t istrong typing

• Weak type exampleyp px = 2;y = “5”;print x+yp y

Visual Basic: 7JavaScript: “25”

7

Page 8: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Systems (con’t)Type Systems (con t)

• Type Safe: A language without type errortype error–Strongly Typed -> Type Safeg y yp yp f–Example: Java, Haskell, and ML

8

Page 9: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type BindingType BindingTh f i ti • The process of associating an attribute, name, location, value, or t t bj ttype, to an object

• ExampleExampleint i; Identifier i is bound to the

integer type and to a location integer type and to a location specified by the underlying compiler

i 10 Identifier i is bound to value 10i = 10; Identifier i is bound to value 10or value 10 is bound to a location

9

Page 10: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Binding (con’t)Type Binding (con t)Bindin time• Binding time– Language definition time

• Java: Integers are bound to int, and real numbers are g ,bound to float

– Language implementation time• Bounding real values to IEEE 754 standardBounding real values to IEEE 754 standard

– Program writing time• Declaration of variables

C il /L d ti– Compile/Load time• Bounding static objects to stack or fixed memory• Execution code is assigned to a memory blockg y

– Run time• Value are bound to variables

10

Page 11: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Binding (con’t)Type Binding (con t)Early bindin• Early binding– An element is bound to a property as early as

possiblep– The earlier the binding the more efficient the

language

• Late Binding– Delay binding until the last possible timey g p– The later the binding the more flexible the language– Supports overloading and overriding in Object

Oriented languagesOriented languages– C++ example ?

11

Page 12: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type CheckingType CheckingT p h kin is th ti it f ns in th t • Type checking is the activity of ensuring that the operands of an operator are of compatible typescompatible types

• A compatible type is one that is either legal for the operator, or is allowed under language

l b l l d b lp g g

rules to be implicitly converted, by compiler-generated code, to a legal typeIf all type bindin s are static nearly all type • If all type bindings are static, nearly all type checking can be static

• If type bindings are dynamic type checking • If type bindings are dynamic, type checking must be dynamic

12

Page 13: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type ConversionType ConversionA i i i th t • A narrowing conversion is one that converts an object to a type that

t i l d ll f th l f j

cannot include all of the values of the original type e.g. float to intg yp g

• A widening conversion is one in which an object is converted to a which an object is converted to a type that can include at least approximations to all of the values approximations to all of the values of the original type e.g. int to float

13

Page 14: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Conversion (con’t)Type Conversion (con t)

• Implicit type conversion (Coercion)(Coercion)–decreases type error detection yp

ability. In most languages, all numeric types are coerced in expressions types are coerced in expressions, using widening conversions. Ada has no i li it C i implicit Conversion

14

Page 15: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Conversion (con’t)Type Conversion (con t)– Cdouble d;

– Javaint x;

long l;int i;

double d;x = 5;d 2…

d = i;l = i;

d = x + 2;

l = i;if (d == l) d = 2 * l;

15

Page 16: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Type Conversion (con’t)Type Conversion (con t)

• Explicit type conversion (Casting)– ( type-name ) cast-expression ( type name ) cast expression

• Cdouble d = 3.14;double d 3.14;int i = (int) d;

• Javaboolean t = true;byte b = (byte) (t ? 1 : 0);

• Ada (similar to function call)3 * Integer(2.0)2 0 Fl t(2)2.0 + Float(2)

16

Page 17: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Semantic DomainsSemantic DomainsS ti D i• Semantic Domain– A set with well-defined properties and

operationsoperations– Environment

• A set of pairs <variable location>• A set of pairs <variable, location>– Memory

• A set of pairs <location value>A set of pairs <location, value>• State

– Product of environment and its memory– Product of environment and its memoryσ = { <Var1, Val1>, <Var2, Val2>,…, <Varn, Valn>}

17

Page 18: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Semantic Domains (con’t)Semantic Domains (con t)Th d fi h • Three ways to define the meaning of a programmeaning of a program–Operational Semantics

• Program is interpreted as a set of sequences of computational stepsq p p

• A set of execution rulesPremise -> ConclusionPremise -> Conclusionσ(x) => 4 and σ(y) => 2 -> σ(x+y) => 6

18

Page 19: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Semantic Domains (con’t)Semantic Domains (con t)Th t d fi th i f • Three ways to define the meaning of a program

O i l i ( ’ )– Operational Semantics (con’t)• Usage

L l d b k– Language manuals and textbooks– Teaching programming languages

• Structural: define program behavior in • Structural: define program behavior in terms of the behavior of its parts

• Natural: define program behavior in terms Natural define program behavior in terms of its overall effects, and not from its single steps

19

Page 20: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Semantic Domains (con’t)Semantic Domains (con t)Axi m ti S m nti s– Axiomatic Semantics• The program does what it is supposed to do• Agreement of the program result and • Agreement of the program result and

specification• Formal verification of a program using logic

p g g g

expressions, assertions• Hoare triple

{Pre condition} s {Post condition}{Pre-condition} s {Post-condition}• Example

{a = 2} b = a; {b = 2}• Weakest Pre-condition

{?} a = b+1; {a > 1}

20

Page 21: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Semantic Domains (con’t)Semantic Domains (con t)Axi m ti S m nti s ( n’t)– Axiomatic Semantics (con t)• Axioms

– Rule of Consequence ,},{}{ QQPPQaP ′⇒⇒′– Rule of Consequence

– Rule of Conjunction}{}{ QaP ′′

}{}{}{}{},{}{

RQaPRaPQaP

– Rule of Assignment (s : b = a)}{}{ RQaP ∧

}{]}\[{ QsbaQtrue

– Rule of sequence

R l f C diti}{}{

}{}{},{}{

21

21

QssPQsRRsP

– Rule of Conditions : if c then a else b

}{}{}{}{},{}{

QsPQbcPQacP ¬∧∧

21

Page 22: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Semantic Domains (con’t)Semantic Domains (con t)A i ti S ti ( ’t)–Axiomatic Semantics (con’t)• Axioms

–Rule of Loops : while c do b end }{}{

}{}{cIsI

IbcI¬∧

–I is loop invariant–Loop Invariant is true before the loop, at p f p,

the bottom of the loop in each iteration, and when the loop is terminated.

–Find the loop invariant to prove the correctness of the loop

22

Page 23: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Semantic Domains (con’t)Semantic Domains (con t)D i l i– Denotational Semantics

• Define the meaning of statement as a state-t sf i th ti l f titransforming mathematical function

• A state of a program indicates the current values of the active objectsvalues of the active objects

• ExampleDenotational semantics of Integer arithmetic – Denotational semantics of Integer arithmetic expressions

» Production rules:Number ::= N D | DDigit ::= 0 | 1 | … | 9Exp ssi n :: E1 E2 | E1 E2 | E1 * E2 | E1 / Expression ::= E1 + E2 | E1 – E2 | E1 * E2 | E1 /

E2| (E) | N23

Page 24: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Semantic Domains (con’t)Semantic Domains (con t)D t ti l S ti s ( ’t)– Denotational Semantics (con’t)

– Semantic domain:Integer = { 1 0 1 }Integer = { …, -1, 0, 1, …}

– Semantic functions:Value: Numner => Numberm mDigit: Digit => NumberExpr: Expression => Integer

l f– Auxiliary functions:plus: Number + Number => Number…

– Semantic equations:Expr[[E1+E2]] = plus(Expr[E1] , Expr[E2])p [[ ]] p ( p [ ] , p [ ])

24

Page 25: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Data TypesData TypesEl m nts f d t t p• Elements of a data type– Set of possible values

Set of operations– Set of operations– Internal representation– External representationExternal representation

• Type information– Implicitp

• 5 is implicitly integer• I is integer, implicitly, in Fortran

Explicit– Explicit• Using variable or function declaration

25

Page 26: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Data Types (con’t)Data Types (con t)D l ifi i• Data type classifications–Built-inBuilt in

• Included in the language definitionPrimitive–Primitive

–CompositeRecursive–Recursive

–User-defined• Data types defined by users• Declared and defined before usageD f f g

26

Page 27: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Primitive Data TypesPrimitive Data TypesU t t d d i di i ibl titi• Unstructured and indivisible entities

• Integer, Real, Boolean, Char g• Depends to the language application

domainm– COBOL: fixed-length strings and

fixed-point numbersf p m– SNOBOL: Strings with different

lengthg– Scheme: integer, rational, real, complex

27

Page 28: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Primitive Data Types (con’t)Primitive Data Types (con t)E l• Example– C

• int, float, char– Java

i t fl t h b l• int, float, char, boolean– Pascal

• Integer Char Real Longint• Integer, Char, Real, Longint– ML

• bool real int word charbool, real, int, word, char– Scheme

• integer?, real?, boolean?, char?g , , ,

28

Page 29: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Primitive Data Types (con’t)Primitive Data Types (con t)

• IntegerAlmost always an exact reflection of –Almost always an exact reflection of the hardware so the mapping is

i i ltrivial–There may be as many as eight There may be as many as eight

different integer types in a language J ’ i d i t i –Java’s signed integer sizes: byte, short, int, long

29

Page 30: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Primitive Data Types (con’t)Primitive Data Types (con t)Fl t• Float– Model real numbers, but only as

i tiapproximations– Languages for scientific use support at

l t t fl ti i t t ( least two floating-point types (e.g., float and double; sometimes moreU ll tl lik th h d b t – Usually exactly like the hardware, but not alwaysIEEE Fl ti P i t– IEEE Floating-Point

– Standard 754

30

Page 31: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Primitive Data Types (con’t)Primitive Data Types (con t)C l• Complex–Some languages support a complex Some languages support a complex

type, e.g., C99, Fortran, and PythonEach value consists of two floats –Each value consists of two floats, the real part and the imaginary part

l ( )–Literal form (in Python):(7 + 3j), where 7 is the real part and (7 3j), where 7 is the real part and 3 is the imaginary part

31

Page 32: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Primitive Data Types (con’t)Primitive Data Types (con t)D i l• Decimal– For business applications (money)

• Essential to COBOL• C# offers a decimal data type

f d f d l d – Store a fixed number of decimal digits, in coded form (BCD) (Binary-Coded D im l)Decimal)

– Advantage: accuracyD d l d – Disadvantages: limited range, wastes memory

32

Page 33: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Primitive Data Types (con’t)Primitive Data Types (con t)

• BooleanSimplest of all–Simplest of all

–Range of values: two elements, Range of values: two elements, one for “true” and one for “false”

–Could be implemented as bits, but often as bytesoften as bytes

33

Page 34: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Primitive Data Types (con’t)Primitive Data Types (con t)Ch t• Character– Stored as numeric codings

M t l d di ASCII– Most commonly used coding: ASCII– An alternative, 16-bit coding: Unicode

(UCS 2) (Universal Character Set)(UCS-2) (Universal Character Set)• Includes characters from most natural

languagesg g• Originally used in Java• C# and JavaScript also support Unicode

– 32-bit Unicode (UCS-4)• Supported by Fortran, starting with 2003

34

Page 35: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data TypesComposite Data TypesSt t d d t• Structured or compound types

• Array, String, Enumeration, Pointer, R d L F

y gRecord, List, Function

• Homogeneous like Arraym g y• Heterogeneous like Record• Fixed size like Array• Fixed size like Array• Dynamic size like Linked List• Inside the core or as a separate

libraryy

35

Page 36: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• ExampleC–C• Array ([]), Pointer (*), Struct, enumy ([]), ( ), ,

–Java• String, Array

Pascal–Pascal• Record, Array, Pointer (^)

36

Page 37: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)St i• String– C and C++

• Not primitiveNot primitive• Use char arrays and a library of functions that provide

operationsSNOBOL4 (a string manipulation language)– SNOBOL4 (a string manipulation language)

• Primitive• Many operations, including elaborate pattern matching

– Fortran and Python• Primitive type with assignment and several operations

Java– Java• Primitive via the String class

– Perl, JavaScript, Ruby, and PHP , p , y,• Provide built-in pattern matching, using regular expressions

37

Page 38: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) l h • String length option

– Static: COBOL, Java’s String classL, J g– Limited Dynamic Length: C and C++

• In these languages a special character is used • In these languages, a special character is used to indicate the end of a string’s characters, rather than maintaining the lengthg g

– Dynamic (no maximum): SNOBOL4, Perl, JavaScriptJavaScript

– Ada supports all three string length options

38

Page 39: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• String Implementation– Static length: compile-time descriptor– Static length: compile-time descriptor– Limited dynamic length: may need a

run-time descriptor for length (but not in C and C++))

– Dynamic length: need run-time descriptor; allocation/de allocation is descriptor; allocation/de-allocation is the biggest implementation problem

39

Page 40: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)E• Enumeration– All possible values, which are named constants, are

d d h d fprovided in the definition– C# exampleenum days {mon, tue, wed, thu, fri, sat, sun};

– Design issues• Is an enumeration constant allowed to appear in more

than one type definition, and if so, how is the type of an occurrence of that constant checked?

• Are enumeration values coerced to integer?• Any other type coerced to an enumeration type?

40

Page 41: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)E ( ’ )• Enumeration (con’t)– Aid to readability, e.g. no need to code a color as a

bnumberenum Colors {Red, Blue, Green, Yellow};

– Aid to reliability, e.g. compiler can check: • operations (don’t allow colors to be added)

N ti i bl b ssi d l tsid • No enumeration variable can be assigned a value outside its defined range

• Ada, C#, and Java 5.0 provide better support for p ppenumeration than C++ because enumeration type variables in these languages are not coerced into integer typestypes

41

Page 42: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)b • Sub-range Types

– An ordered contiguous subsequence of an ordinal type

• Example: 12..18 is a sub-range of integer typeAd ’ d i– Ada’s design

type Days is (mon, tue, wed, thu, fri, sat, sun);

subtype Weekdays is Days range mon..fri;subtype Index is Integer range 1..100;Day1: Days;Day1: Days;Day2: Weekday;Day2 := Day1;

42

Page 43: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)E • Enumeration and Sub-range implementationp– Enumeration types are implemented as

integersintegers– Sub-range types are implemented like the

parent types with code inserted (by the parent types with code inserted (by the compiler) to restrict assignments to sub-range variablesrange variables

43

Page 44: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)• Array

– An array is an aggregate of homogeneous y gg g f m gdata elements in which an individual element is identified by its position in the m f y paggregate, relative to the first element.

– A heterogeneous array is one in which the A heterogeneous array is one in which the elements need not be of the same type

• Supported by Perl Python JavaScript and Ruby• Supported by Perl, Python, JavaScript, and Ruby

44

Page 45: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) d • Array Index Type

– FORTRAN, C: integer only– Ada: integer or enumeration (includes Boolean and

char)– Java: integer types only– Index range checking

• C, C++, Perl, and Fortran do not specify range checking• Java, ML, C# specify range checking

I Ad th d f lt i t i h ki b t it • In Ada, the default is to require range checking, but it can be turned off

45

Page 46: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) l• Array Initialization

– C-based languagesi iint list [] = {1, 3, 5, 7}char *names [] = {“Mike”, “Fred”,“Mary Lou”};

Ada– AdaList : array (1..5) of Integer := (1 => 17, 3 => 34, others => 0);

– PythonList comprehensionsli t [ ** 2 f i (12) if % 3 0]list = [x ** 2 for x in range(12) if x % 3 == 0]

puts [0, 9, 36, 81] in list

46

Page 47: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) • Array Operations

– APL provides the most powerful array processing p p y p goperations for vectors and matrixes as well as unary operators (for example, to reverse column l ts)elements)

Ad ll i t b t l t ti– Ada allows array assignment but also concatenation

P th ’ i t b t th l – Python’s array assignments, but they are only reference changes. Python also supports array concatenation and element membership operationsconcatenation and element membership operations

47

Page 48: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Array Operations (con’t)– Ruby also provides array concatenationRuby also provides array concatenation

F t id l t l ti – Fortran provides elemental operations because they are between pairs of array l telements

– For example, + operator between two arrays results in an array of the sums of y ythe element pairs of the two arrays

48

Page 49: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)l d d • Rectangular and Jagged Arrays

– A rectangular array is a multi-dimensioned array in h h ll f h h h b f which all of the rows have the same number of

elements and all columns have the same number of elementselements

– A jagged matrix has rows with varying number of elementselements

• Possible when multi-dimensioned arrays actually appear as arrays of arrays

– C, C++, and Java support jagged arrays– Fortran, Ada, and C# support rectangular arrays

(C# also supports jagged arrays)49

Page 50: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)l• Slices

– A slice is some substructure of an array; nothing h f hmore than a referencing mechanism

– Slices are only useful in languages that have array tioperations

– Fortran 95I t Di i (10) V tInteger, Dimension (10) :: VectorInteger, Dimension (3, 3) :: MatInteger Dimension (3 3 4) :: CubeInteger, Dimension (3, 3, 4) :: CubeVector (3:6) is a four element array

– Ruby supports slices with the slice methody pplist.slice(2, 2) returns the third and fourth elements of list 50

Page 51: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

51

Page 52: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) • Array Access

– Access function maps subscript expressions to an dd h address in the array

– Access function for single-dimensioned arrays:address(list[k]) = address (list[lower_bound])

+ ((k-lower_bound) * element_size)– Two common ways:

• Row major order (by rows) – used in most languagesl j d (b l ) d i F t• column major order (by columns) – used in Fortran

52

Page 53: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)d• Record

– A record is a possibly heterogeneous aggregate of d l h h h d d l l data elements in which the individual elements are identified by namesCOBOL l l b t h t d d – COBOL uses level numbers to show nested records; others use recursive definition01 EMP REC01 EMP-REC.

02 EMP-NAME.05 FIRST PIC X(20).05 FIRST PIC X(20).05 MID PIC X(10).05 LAST PIC X(20).

02 HOURLY-RATE PIC 99V99.

53

Page 54: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) ( )• Record (con’t)

– Adatype Emp_Rec_Type is recordFirst: String (1..20);Mid: String (1..10);Last: String (1..20);gHourly_Rate: Float;end record;Emp_Rec: Emp_Rec_Type;

54

Page 55: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Record (con’t)– PascalPascalMonthType = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct( , , , p , y, , , g, p,,Nov,Dec);

DateType = recordMonth : MonthType; Day : 1..31;yYear : 1900..2000;

end;;

55

Page 56: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Record (con’t)C– C

struct student_type { char name[20]; int ID; ;

}

56

Page 57: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Record (con’t)– Java: No record in Java It is defined using Java: No record in Java. It is defined using

class.class Person {class Person {

String name;int id number;int id_number;Date birthday;int age;int age;

}

57

Page 58: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) • Pointer and Reference Types

– A pointer type variable has a range of values that p yp gconsists of memory addresses and a special value, nil

– Provide the power of indirect addressing– Provide a way to manage dynamic memory– A pointer can be used to access a location in the

area where storage is dynamically created (usually called a heap)called a heap)

58

Page 59: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) • Pointer Design Issues

– What are the scope and lifetime of a pointer p pvariable?

– Are pointers restricted as to the type of value to which they can point?

– Are pointers used for dynamic storage t i di t dd i b th?management, indirect addressing, or both?

– Should the language support pointer types, reference types or both?reference types, or both?

59

Page 60: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) • Pointer Operations

– Two fundamental operations: assignment and p gdereferencing

– Assignment is used to set a pointer variable’s value lto some useful address

– Dereferencing yields the value stored at the l ti t d b th i t ’ llocation represented by the pointer’s value

• Dereferencing can be explicit or implicit• C++ uses an explicit operation via *• C++ uses an explicit operation via j = *ptr

sets j to the value located at ptrsets j to the value located at ptr

60

Page 61: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) ll• Pointer Illustration

– The assignment operation j = *ptrg p

61

Page 62: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Pointer Problems– Dangling pointers (dangerous)Dangling pointers (dangerous)

• A pointer points to a heap-dynamic variable that has been de-allocated

– Lost heap-dynamic variable• An allocated heap-dynamic variable that is no longer

accessible to the user program (often called garbage)– Pointer p1 is set to point to a newly created heap-dynamic variable– Pointer p1 is later set to point to another newly created heap-p p y p

dynamic variable– The process of losing heap-dynamic variables is called memory

leakage

62

Page 63: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Pointer Problems (con’t)– AdaAda

• Some dangling pointers are disallowed because dynamic objects can be automatically de-dynamic objects can be automatically deallocated at the end of pointer's type scope

– C, C++C, C• Extremely flexible but must be used with care• Pointers can point at any variable regardless of Pointers can point at any variable regardless of

when or where it was allocated• Used for dynamic storage management and Used for dynam c storage management and

addressing63

Page 64: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Pointer Problems (con’t)– C C++C, C++

• Pointer arithmetic is possible• Explicit dereferencing and address-of • Explicit dereferencing and address-of

operators• Domain type need not be fixed (void *) Domain type need not be fixed (void )

void * can point to any type and can be type checked (cannot be de-referenced)checked (cannot be de referenced)

64

Page 65: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Pointer Arithmetics in C, C++float stuff[100];float stuff[100];

float *p;p = stuff;p

*( +5) is equivalent to t ff[5] and [5]*(p+5) is equivalent to stuff[5] and p[5]*(p+i) is equivalent to stuff[i] and p[i]

65

Page 66: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) • Reference Types

– C++ includes a special kind of pointer type called a p p ypreference type that is used primarily for formal parameters

Ad f b h b f d b l • Advantages of both pass-by-reference and pass-by-value – Java extends C++’s reference variables and allows

them to replace pointers entirelythem to replace pointers entirely• References are references to objects, rather than being

addresses– C# includes both the references of Java and the

pointers of C++

66

Page 67: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Heap ManagementA very complex run time process– A very complex run-time process

– Single-size cells vs. variable-size cellsg– Two approaches to reclaim garbage

• Reference counters (eager approach): • Reference counters (eager approach): reclamation is gradualM k (l h) l ti • Mark-sweep (lazy approach): reclamation occurs when the list of variable space b tbecomes empty

67

Page 68: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t)

• Heap Management (con’t)– Reference countersReference counters

• Maintain a counter in every cell that store the number of pointers currently pointing at the cellnumber of pointers currently pointing at the cell

• Disadvantages: space required, execution time required, complications for cells connected q , pcircularly

• Advantage: it is intrinsically incremental, so significant delays in the application execution are avoided

68

Page 69: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Composite Data Types (con’t)Composite Data Types (con t) ( )• Heap Management (con’t)

– Mark-Sweepp• The run-time system allocates storage cells as requested and

disconnects pointers from cells as necessary; mark-sweep then beginsg

• Every heap cell has an extra bit used by collection algorithm • All cells initially set to garbage• All pointers traced into heap and reachable cells marked as not • All pointers traced into heap, and reachable cells marked as not

garbage• All garbage cells returned to list of available cells

D d l f d f l • Disadvantages: in its original form, it was done too infrequently. When done, it caused significant delays in application execution. Contemporary mark-sweep algorithms avoid this by doing it more ft ll d i t l koften—called incremental mark-sweep

69

Page 70: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

Recursive Data TypesRecursive Data Types

• Recursive or circular data typesT d f bj t f • Type composed from objects of the same typethe same type

• Examplep–Linked list in C and Pascal–ML

datatype intlist = nil | cons of int * intlist

70

5 10

Page 71: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

ExercisesExercises1 D h h f h f ll 1. Determine which of the following

programming languages are statically typed (E l b l )or not: (Explain by example)

– Ada– Perl– Python– Haskell– Prolog– Fortran– Rubyy

71

Page 72: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

ExercisesExercisesB h l f 2. Bring another example of type error in C.

3. Show two examples for early and late binding p y gin a language.

4. Is there any programming language which 4. Is there any programming language which does not allow implicit type conversion, say int to float?int to float?

5. Which type of coercions is not safe?6 compute the Weakest Pre condition of6. compute the Weakest Pre-condition of

{?} a = b * -1; {a > 10}

72

Page 73: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

ExercisesExercises l h h l f 2. Using an example, show the rule of

consequence in axiomatic semantic.

}{}{,},{}{

QaPQQPPQaP

′′′⇒⇒′

3. Find the loop invariant of the following while looploop.

i = 1;s = 0;while (i <= 10) {s = s + i;i = i + 1;

}73

Page 74: CPS 506 Comparative Programming Languagesssamet/cps506/CPS 506 (Winter 2010 Ryerson...type, e.g., C99, Fortran, and Python –Each value consists of two floats Each value consists

ExercisesExercisesh h l ( ) d7. Which programming language(s) except Ada

and different versions of C, support pointer?8. What are the rules of call-by-value and call-

by-reference in Pascal? Give examples.y p9. Name two programming languages which have

automatic garbage collection. What are the automatic garbage collection. What are the negative and positive effects of this operation in a language?operation in a language?

74