Chapter 5 Data Types SANGJI University KO Kwangman

  • Slides: 71
Download presentation
Chapter 5 Data Types SANGJI University KO Kwangman (kkman@sangji. ac. kr)

Chapter 5 Data Types SANGJI University KO Kwangman (kkman@sangji. ac. kr)

1. Introduction o Type supports abstraction Real World Programming Language concrete representation 유한성(finite) 추상화(abstraction)

1. Introduction o Type supports abstraction Real World Programming Language concrete representation 유한성(finite) 추상화(abstraction) Natural Numbers Real Numbers kkman@sangji. ac. kr Programming Language Implementation int data double data binary digits 2's in sizeof(int) complement bytes IEEE 754 Chap. 5 : Data Type binary digits in sizeof(int) bytes 2

5. 3 기본 타입 o 현재 32 -bit 컴퓨터에서 사용되는 용어 ~ ~ ~

5. 3 기본 타입 o 현재 32 -bit 컴퓨터에서 사용되는 용어 ~ ~ ~ Nibble: 4 bits Byte: 8 bits Half-word: 16 bits Word: 32 bits Double word: 64 bits Quad word: 128 bits o 대부분의 언어에서 숫자 타입은 유한한 크기 ~ a + b, Overflow 가능성 ~ 수학과의 차이점 존재: § a + (b + c) (a + b) + c kkman@sangji. ac. kr

5. 4 사용자 정의 자료형 o 열거형(Enumeration) : enum day {Mon, Tue, Wed, Thu,

5. 4 사용자 정의 자료형 o 열거형(Enumeration) : enum day {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; enum day my. Day = Wednesday; ~ C/C++ 에서 위 타입들의 값은 0, . . . , 6이 사용된다. ~ Java는 더 강력한 구조가 사용 for (day d : day. values()) Sytem. out. println(d); kkman@sangji. ac. kr Chap. 5 : Data Type 12

o 포인터의 사용 struct Node { int key; struct Node* next; }; struct Node*

o 포인터의 사용 struct Node { int key; struct Node* next; }; struct Node* head; kkman@sangji. ac. kr Chap. 5 : Data Type 15

o Homeworks ~ ~ ~ 연결 리스트 구조체 유니온 문자열 부분 범위형 kkman@sangji. ac.

o Homeworks ~ ~ ~ 연결 리스트 구조체 유니온 문자열 부분 범위형 kkman@sangji. ac. kr Chap. 5 : Data Type 19

명시적 자료형(Explicit Typing) o 정적 자료형 검사(static type checking) ~ static type checking is

명시적 자료형(Explicit Typing) o 정적 자료형 검사(static type checking) ~ static type checking is still controversial. ~ 유연성(flexibility) § 자료형 결정 및 검사를 느슨 또는 반대 ~ 신뢰성(reliability) 또는 보안(security) § 최대한 제약성(restrictiveness) § 엄격한 번역 시간 자료형 검사 Flexibility type-less language kkman@sangji. ac. kr still controversial Reliability (security) strongly typed language Chap. 5 : Data Type 20

4. 보안과 신뢰성(security & reliability) ~ 실행시 발생 될 수 있는 에러를 사전 차단

4. 보안과 신뢰성(security & reliability) ~ 실행시 발생 될 수 있는 에러를 사전 차단 5. 판독성(readability) ~ 명시적 자료형 선언은 자료형 설계를 문서화 ~ good for documentation 6. 모호성 제거(remove ambiguity) ~ 다중 적재된 명칭(overloaded names)에 대한 모호성 해결 (resolving) 7. … kkman@sangji. ac. kr Chap. 5 : Data Type 22

자료형 정의 o 정의_1 ~ a set of values ~ ex) the data type

자료형 정의 o 정의_1 ~ a set of values ~ ex) the data type int in Java ~ Vint = { x | x ∈ Z, ~ -2, 147, 483, 648 ≤ x ≤ 2, 147, 483, 647 } o 정의_2 ~ a set of values with a set of operations ~ ex) the data type int in Java Vint = { x | x ∈ Z, -2, 147, 483, 648 ≤ x ≤ 2, 147, 483, 647 } with the following set of operations Fint = { +, -, *, … } kkman@sangji. ac. kr Chap. 5 : Data Type 23

Constructing Types o 자료형 구성자(type constructors) ~ 기본 자료형으로 부터 새로운 자료형을 구성 ~

Constructing Types o 자료형 구성자(type constructors) ~ 기본 자료형으로 부터 새로운 자료형을 구성 ~ Derived types are parameterized type constructors. 사용자-정의 자료형 (user-defined data type) Basic Types (primitive types) type constructors int, double, … Derived Types array, structures, … type constructors kkman@sangji. ac. kr Chap. 5 : Data Type 25

o 자료형 선언(type declaration) ~ 새로운 자료형에 이름 부여 (In C example) typedef int

o 자료형 선언(type declaration) ~ 새로운 자료형에 이름 부여 (In C example) typedef int 10[10]; kkman@sangji. ac. kr Chap. 5 : Data Type 26

o 느스한 자료형(weakly-typed) 언어 ~ the languages which detect type errors during translation time

o 느스한 자료형(weakly-typed) 언어 ~ the languages which detect type errors during translation time but allow a few loopholes o 타입 결여(untyped) 언어 ~ 정적 자료형 시스템을 지원하지 않는 언어 ~ 데이터 안전성에 관한 검사가 실행시간에 이루어짐. ~ Scheme, LISP, Smalltalk, Perl, … kkman@sangji. ac. kr Chap. 5 : Data Type 29

Safe Programs o Every well-typed programs are safe but not vice versa. Executable Programs

Safe Programs o Every well-typed programs are safe but not vice versa. Executable Programs Legal Programs (Safe Programs) Well-Typed Programs kkman@sangji. ac. kr Chap. 5 : Data Type contain datacorrupting errors Unsafe Programs 30

단순 타입(Simple Types) o 단순 타입 ~ atomic types ~ the types which contain

단순 타입(Simple Types) o 단순 타입 ~ atomic types ~ the types which contain no other type substructures o 단순 타입의 분류 Simple Types Predefined Simple Types Enumeration Types User-Defined Ordinal Types Defined Operators: • successor • predecessor • comparison kkman@sangji. ac. kr Chap. 5 : Data Type Subrange Types 31

Ordinal Type Example o Enumerated Type in C enum Color { Red, Green, Blue

Ordinal Type Example o Enumerated Type in C enum Color { Red, Green, Blue }; o Enumerated Type in Ada type Color_Type is (Red, Green, Blue); o Enumerated Type in ML datatype Color_Type = Red | Green | Blue; o Java does not have enumerated types. o Subrange Type in Ada type Digit_Type is range 0. . 9; kkman@sangji. ac. kr Chap. 5 : Data Type 32

Some Notes on Simple Types o 순서 타입(ordinal type) ~ 비교 연산자를 항상 지원하지

Some Notes on Simple Types o 순서 타입(ordinal type) ~ 비교 연산자를 항상 지원하지 않음 ~ ex) Subrange of floating-point numbers in Ada. type Unit_Interval is range 0. 0. . 1. 0; o 부분범위타입(subrange type) type Digit_Type is range 0. . 9; ~ 효과 ? o 단순 자료형은 하드웨어상에서 직접 구현 ~ 하드웨어 효율성 중시 kkman@sangji. ac. kr Chap. 5 : Data Type 33

데카르트 곱(cartesian product) o Cartesian product ~ a set of pairs ~ 두 집합

데카르트 곱(cartesian product) o Cartesian product ~ a set of pairs ~ 두 집합 U, V에 대한 모든 원소들의 순서쌍(ordered pair) U ⅹ V = { (u, v) | u∈ U and v ∈ V } with projection functions p 1 and p 2: p 1: U ⅹ V → U p 2: U ⅹ V → V ((u, p v)) = u p v)) = v 2 ((u, 1 o corresponding to the Cartesian products ~ record (structure) constructors kkman@sangji. ac. kr Chap. 5 : Data Type 35

Example: Structure Types in C struct Int. Real // Int. Real = int ⅹ

Example: Structure Types in C struct Int. Real // Int. Real = int ⅹ double { int i; double r; }; struct Int. Real x = {1, 2. 5}; // x = (1, 2. 5) ∈ Int. Real x. i x. r // p 1(x) // p 2(x) kkman@sangji. ac. kr component selector operation (structure member operation) Chap. 5 : Data Type 36

합집합(union) o 합집합 종류 ~ 비구별 합집합(undiscriminated union) § normal set union ~ 구별

합집합(union) o 합집합 종류 ~ 비구별 합집합(undiscriminated union) § normal set union ~ 구별 합집합(discriminated union) § 원소를 구별하기 위한 태그(tag)가 첨부 kkman@sangji. ac. kr Chap. 5 : Data Type 37

o Example: Union in C ~ undiscriminated union: a union itself ~ discriminated union:

o Example: Union in C ~ undiscriminated union: a union itself ~ discriminated union: a union embedded in a structure union Int. Or. Real { int i; double r; }; kkman@sangji. ac. kr enum Disc {Is. Int, Is. Real}; struct Int. Or. Real { enum Disc which; union { int i; double r; } val; }; Chap. 5 : Data Type 38

More Safe Union o Variant Records in Ada ~ the discriminant and the value

More Safe Union o Variant Records in Ada ~ the discriminant and the value should be assigned at once type Disc is (Is. Int, Is. Real); type Int. Or. Real (which: Disc) is record case which is when Is. Int => i: integer; when Is. Real => r: float; end case; end record; . . . x: Int. Or. Real : = (Is. Real, 2. 3); kkman@sangji. ac. kr o Datatype in ML ~ enumerators may include data fields ~ pattern matching using case expressions datatype Int. Or. Real = Is. Int of int | Is. Real of real; . . . fun print. Int. Or. Real x = case x of Is. Int(i) => print. Int i | Is. Real(r) =>print. Real r ; 39

부분집합(subset) o 부분집합 ~ supported by subtypes(부분자료형). o In Ada ~ subtypes and subrange

부분집합(subset) o 부분집합 ~ supported by subtypes(부분자료형). o In Ada ~ subtypes and subrange types are different: ~ subtype Int. Digit_Type is integer range 0. . 9; ~ new type (that is a subrange of an existing type) type Digit_Type is range 0. . 9; ~ fixing the variant part of a variant record type subtype IRInt is Int. Or. Real(Is. Int); subtype IRReal is Int. Or. Real(Is. Real); kkman@sangji. ac. kr Chap. 5 : Data Type 40

Arrays and Functions o Consider a function f: U → V If U is

Arrays and Functions o Consider a function f: U → V If U is an ordinal type, f corresponds to an array: U: index type, V: component type f(i) ∈ V which i ∈ U o Array Size Problem ~ the size of an array may be or may not be the part of an array kkman@sangji. ac. kr Chap. 5 : Data Type 41

Array Example: C/C++ o The size of an array is not part of the

Array Example: C/C++ o The size of an array is not part of the array. ~ When passing array parameters, the sizes of the arrays may have to be passed as separate parameters. int array_max (int a[], int size); ~ In a formal parameter, the array notation = pointer. o Difference between C and C++. ~ 교재가 틀렸다. const int Size = 5; int x[Size]; /* illegal C, ok in C++ */ int x[Size*Size]; /* illegal C, ok in C++ */ kkman@sangji. ac. kr Chap. 5 : Data Type 42

Array Example: Java, Ada o In Java, ~ the size of an array is

Array Example: Java, Ada o In Java, ~ the size of an array is the part of the array ~ array can be dynamically allocated // a part of Figure 6. 3. . . int [] x = new int[u] ; // Dynamic array allocation for (int i = 0; i < x. length; i++) x[i] = i; . . . kkman@sangji. ac. kr o Unconstrained Array in Ada ~ dynamically sized array ~ 배열 선언시 크기 결정 type Int. To. Int is array (INTEGER range <>) of INTEGER; . . . declare x: Int. To. Int(1. . size); begin for i in x'range loop x(i) : = i; 43

Multidimensional Array o Supporting Multidimensional Array ~ Multidimensional array may be simulated by arrays

Multidimensional Array o Supporting Multidimensional Array ~ Multidimensional array may be simulated by arrays of arrays (C, C++, Java) o 기억 장소 할당(memory allocation) ~ Row-Major : natural way for arrays of arrays ~ Column-Major : FORTRAN ~ 특정 원소 접근 공식 ? Row-Major: 1 2 3 Column-Major: 1 3 5 4 5 6 2 4 6 kkman@sangji. ac. kr Chap. 5 : Data Type 44

Passing Multidimensional Array o 배열을 매개변수로 전달 ~ 배열의 크기가 배열의 일부로 간주 §

Passing Multidimensional Array o 배열을 매개변수로 전달 ~ 배열의 크기가 배열의 일부로 간주 § there is no problem of array parameters. ~ 배열의 크기가 배열의 일부로 간주되지 않는 경우 § 매개변수 전달시 배열의 크기를 전달 o passing 2 -dimensional array in C/C++. ~ When the both dimensions are known ~ When the 1 st dimension should be passed ~ When the both dimensions should be passed kkman@sangji. ac. kr Chap. 5 : Data Type 45

Passing no Dimensions #include <iostream> using namespace std; const int R = 3, C

Passing no Dimensions #include <iostream> using namespace std; const int R = 3, C = 3; int sum(int a[R][C]) { int sum = 0; for (int i = 0; i < R; ++i) for (int j = 0; j < C; ++j) sum += a[i][j]; return sum; } main() { int a[R][C] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; cout << "1+2+. . +9 = " << sum(a) << endl; } kkman@sangji. ac. kr Chap. 5 : Data Type 46

Passing the 1 st Dimension #include <iostream> using namespace std; const int R =

Passing the 1 st Dimension #include <iostream> using namespace std; const int R = 3, C = 3; int sum(int a[][C], int row) { int sum = 0; for (int i = 0; i < row; ++i) for (int j = 0; j < C; ++j) sum += a[i][j]; return sum; } main() { int a[R][C] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; cout << "1+2+. . +9 = " << sum(a, R) << endl; } kkman@sangji. ac. kr Chap. 5 : Data Type 47

Passing the Both Dimensions #include <iostream> using namespace std; int sum(int a[], int row,

Passing the Both Dimensions #include <iostream> using namespace std; int sum(int a[], int row, int col) { int sum = 0; for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) sum += a[i*col+j]; return sum; } const int R = 3, C = 3; main() { int a[R][C] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; cout << "1+2+. . +9 = " << sum(&(a[0][0]), R, C) << endl; } kkman@sangji. ac. kr Chap. 5 : Data Type 48

Some Notes on 2 -Dimensional Array o Equivalences and Differences ~ for a constant

Some Notes on 2 -Dimensional Array o Equivalences and Differences ~ for a constant N int a[N] ≡ int a[] ≡ int *a ~ for constants M and N int a[M][N] ≡ int a[][N] ≡ int (*a)[N] ~ beware that int a[][N] ≠ int **a o Declarations of C/C++ ~ declarations implies the usages int *a[N]; int (*a)[N]; kkman@sangji. ac. kr Chap. 5 : Data Type 49

o In C and C++, arrays are constant pointers int a[] = {1, 2,

o In C and C++, arrays are constant pointers int a[] = {1, 2, 3, 4, 5}; int* p = a; printf(“%d n”, *p); *(p+2) ); *(a+2) ); 2[a]); a[2] ≡ *(a + 2) ≡ 2[a] kkman@sangji. ac. kr // Wow! Chap. 5 : Data Type 51

Recursive Types o 순환 타입(recursive types) ~ a type that uses itself in its

Recursive Types o 순환 타입(recursive types) ~ a type that uses itself in its declaration ~ pointers are useful for recursive types o The Size Problem of Recursive Types ~ require that the maximum size of a data type should be determined at translation time. kkman@sangji. ac. kr Chap. 5 : Data Type 52

struct Char. List { char data; struct Char. List next; }; /* illegal! */

struct Char. List { char data; struct Char. List next; }; /* illegal! */ struct Char. List. Node { char data; struct Char. List. Node* next; }; union Char. List { enum { nothing } empty; struct { char data; union Char. List next; } char. List. Node; }; /* still, illegal! */ typedef struct Char. List. Node* Char. List; /* Now, legal */ kkman@sangji. ac. kr 53

Data Types and the Environment Fully Dynamic Environment Traditional Environment (stack & heap) Recursive

Data Types and the Environment Fully Dynamic Environment Traditional Environment (stack & heap) Recursive Types General Function Types Recursive types and function types are implemented by pointers Functional Languages Algol-style Languages kkman@sangji. ac. kr 54

The Type Structure of C kkman@sangji. ac. kr Chap. 5 : Data Type 55

The Type Structure of C kkman@sangji. ac. kr Chap. 5 : Data Type 55

The Type Structure of Java kkman@sangji. ac. kr Chap. 5 : Data Type 56

The Type Structure of Java kkman@sangji. ac. kr Chap. 5 : Data Type 56

Simplified Type Structure of Ada kkman@sangji. ac. kr Chap. 5 : Data Type 57

Simplified Type Structure of Ada kkman@sangji. ac. kr Chap. 5 : Data Type 57

타입 동등성(Type Equivalence) o 타입 동등성이란 ? ~ 두개의 타입이 어느때 같은(=일치=동등)한가 ! ~

타입 동등성(Type Equivalence) o 타입 동등성이란 ? ~ 두개의 타입이 어느때 같은(=일치=동등)한가 ! ~ 종류 § 구조적 동치(structural equivalence) § 이름 동치(name equivalence) § 선언 동치(declaration equivalence) o 구조적 동치 ~ 두개의 타입이 동일한 구조(same structure) ~ 동일한 구조 § the same type constructor and the same component types. ~ 교재 PP. 258 kkman@sangji. ac. kr Chap. 5 : Data Type 58

o 이름 동치 ~ 두 개의 타입은 공일한 이름을 가질 때에만 동등 o Declaration

o 이름 동치 ~ 두 개의 타입은 공일한 이름을 가질 때에만 동등 o Declaration Equivalence ~ Two types are equivalent if they are derived from the same name. kkman@sangji. ac. kr Chap. 5 : Data Type 59

Complicating Factors o Array Size ~ 배열의 크기를 배열의 한 부분으로 간주 ? o

Complicating Factors o Array Size ~ 배열의 크기를 배열의 한 부분으로 간주 ? o Field Names of Record Types ~ The following Rec. A and Rec. B should be structurally equivalent (char ⅹ int) but not in practice because of the field names. struct Rec. A { char x; int y; }; kkman@sangji. ac. kr struct Rec. B { char a; int b; }; Chap. 5 : Data Type 60

Language Example: Ada o Ada ~ pure name equivalence § the variables a and

Language Example: Ada o Ada ~ pure name equivalence § the variables a and b in a, b: array (1. . 10) of integer; are not type equivalent because the above declaration is considered as a shorthand for a: array (1. . 10) of integer; b: array (1. . 10) of integer; ~ the new types and the subtypes are different each other § new type Age is new integer; § subtype: compatible to the original type subtype Age is integer; kkman@sangji. ac. kr Chap. 5 : Data Type 61

Language Examples: Others o Pascal ~ every type constructors introduce a new type ~

Language Examples: Others o Pascal ~ every type constructors introduce a new type ~ new type names for existing type names are equivalent o Java ~ ~ no ‘typedef’ constructs class and interface declarations introduce a new type name inheritance hierarchy (subtype) structural equivalence for arrays kkman@sangji. ac. kr Chap. 5 : Data Type 62

타입 검사(Type Checking) o 타입 검사 종류(categories) ~ Dynamic Type Checking § 타입 정보를

타입 검사(Type Checking) o 타입 검사 종류(categories) ~ Dynamic Type Checking § 타입 정보를 실행시간에 유지하고 검사 ~ Static Type Checking § 느슨한(weakly) 자료형 검사 언어 § Strongly-Typed Languages o Unspecified whether dynamic or static typing is to be used kkman@sangji. ac. kr Chap. 5 : Data Type 63

Type Checking Examples o C/C++ ~ static type checking ~ no a strongly typed

Type Checking Examples o C/C++ ~ static type checking ~ no a strongly typed language: type conversion o Scheme ~ dynamic type checking but rigorous ~ predefined type test functions: number? , symbol? , … o Ada ~ a strongly typed language ~ array bound checks are performed during run-time: exceptions kkman@sangji. ac. kr Chap. 5 : Data Type 64

Type Checking and Type Inference o 타입 추론(inference) ~ the process to infer the

Type Checking and Type Inference o 타입 추론(inference) ~ the process to infer the type of an expression from the types of its subexpressions o Type Checking and Type Inference ~ Type checking rules and type inference rules are intermingled e 1 + e 2 ~ Type checking rules are type inference rules are closely related with the type equivalence algorithm ~ Explicit declarations are helpful for type checking but not mandatory kkman@sangji. ac. kr Chap. 5 : Data Type 65

Type Compatibility o Relaxing the type correctness e 1 + e 2 Type Equivalence

Type Compatibility o Relaxing the type correctness e 1 + e 2 Type Equivalence Type Compatibility e 1 and e 2 should be of equivalent types. of compatible types. For assignment compatibility, information preservation is an important requirement. o Language Examples ~ Ada: subrange types of a base type are compatible ~ C, Java: numeric types are compatible kkman@sangji. ac. kr Chap. 5 : Data Type 66

Implicit Types o Implicit Types ~ The types of entities are not explicitly declared

Implicit Types o Implicit Types ~ The types of entities are not explicitly declared but inferred implicitly o Examples ~ C: the types of variables and functions are implicitly int x; /* implicitly int */ f(int x) { return x + 1; } /* returns int */ ~ Pascal: constants are implicitly typed from the literals const PI = 3. 14159; (* implicitly real *) kkman@sangji. ac. kr Chap. 5 : Data Type 67

Type Conversion o Classifying Type Conversions ~ According to the notation § Implicit Conversions

Type Conversion o Classifying Type Conversions ~ According to the notation § Implicit Conversions (coercion) § Explicit Conversions (casting) ~ According to the sizes of the types involved § Widening Conversions § Narrowing Conversions: may involve a loss of data o C Example int x = 3; x = 2. 3 + x / 2; int promotion (widening) double assignment conversion (narrowing) int kkman@sangji. ac. kr 68

Notes on Type Conversions o Advantages of Explicit Conversions ~ Type conversions are documented

Notes on Type Conversions o Advantages of Explicit Conversions ~ Type conversions are documented precisely. ~ Make it easier for the translator to resolve overloading. double max(int, double); // max #1 double max(double, int); // max #2. . . max(2, 3); // calls what? max(2, (double)3); // calls max #1 o Structure Casting ~ The sizes of the structure types should be identical. ~ The translation merely reinterprets the memory kkman@sangji. ac. kr Chap. 5 : Data Type 69

포괄(generic) Pointers o Generic pointers may point anything. ~ anonymous pointers (void* in C)

포괄(generic) Pointers o Generic pointers may point anything. ~ anonymous pointers (void* in C) char * int *. . . conversion 1 void * conversion 2 ~ C allows the both conversion to be implicit. ~ In C++, the conversion 1 is implicit but the conversion 2 should be explicit. kkman@sangji. ac. kr Chap. 5 : Data Type 70

Library Functions for Conversions o Ada Example ~ attribute functions of the character type

Library Functions for Conversions o Ada Example ~ attribute functions of the character type character'pos('a') -- returns 97 character'val(97) -- returns 'a' o Java Example ~ conversion functions of the Integer class String s = Integer. to. String(12345); int i = Integer. parse. Int("54321"); kkman@sangji. ac. kr Chap. 5 : Data Type 71