n n n n boolean char integer real
기본 타입 및 유도 타입 n 기본 타입 n n n boolean, char(나열 타입의 하나로 간주할 수 있음); integer; real; 나열 타입(Enumerated type) 부분범위 타입(Subrange) 유도 타입 n n n Array types; Record types; Union types; Set types; Pointer types.
기본 형: 예 n 나열 타입 n n 부분범위 타입(Ada, Pascal) n n type digit is range 0. . 9; 부분범위 타입 없음(C, C++, Java) n n n enum Color {Red, Green, Blue}; byte digit; // -128. . 127 If (digit >9 || digit < 0) thrown new Digit. Exception(); 사전정의 타입은 무엇인가?
데이터 타입의 의미 n n 값들의 집합(a set of values) 그 값에 적용 가능한 연산들의 모음(a group of operations) n 정의 2 A data type is a set of values, together with a set of operations on those values. n 데이터 타입의 수학적 의미 n n an algebra (S, Op) where S is a set of values and Op is a set of operations on S
6. 3 타입 구성자 (Type Constructors)
구조체(레코드) 타입 n C의 예 struct Int. Char. Real { int i; char c; float r; } n Ada의 예 type Int. Char. Real is record i: integer; c: character; r: float; end record;
레코드의 수학적 의미 n Int. Char. Real가 의미하는 값들의 집합은 ? n n Cartesian Product U V = { (a, b) | a U, b V } n n n int: Int char: Char float: Real Cartesian product can be extended to more than two sets What is an operation corresponding to the projection ? ML의 예 type Int. Char. Real = int * char * real;
레코드 타입에 대한 연산들 n 필드 선택 연산(Field selector operation) n n x. i Project of x to the integer
유니온 n 예(C) union Int. Or. Real { int i; float r; }; enum Disc {Is. Int, Is. Real}; struct Int. Or. Real { enum Disc which; union { int i; float r; } val; };
구별 유니온: Ada 예 type Disc is (Is. Int, Is. Real); type Int. Or. Real (which: Disc) is record case which is when Is. Int => i: integer; when Isreal => r: float; end case; end record; x: Int. Or. Real : = (Is. Real, 2. 3);
집합 타입(Set type) n What is an element of the power set of a set S ? n n n type digit = 0. . 9; digit. Set = SET OF digit In Pascal, Modula n SET OF ordinal type
C 예제 int array_max(int a[], int size) { int temp, i; assert(size > 0); temp = a[0]; for (i = 1; i < size; i++) { if (a[i] > temp) temp = a[i]; } return temp; }
Java 예제 class Array. Test { static int array_max(int[] a) { int temp; temp = a[0]; for (i = 1; i < a. length; i++) { if (a[i] > temp) temp = a[i]; } return temp; }
수학적 의미 n 배열 Digit. To. Int의 의미 n n Digit. To. Int인 배열 A가 나타낼 수 있는 값들의 집합 값들의 연속(Sequence) 혹은 튜플(Tuple) (A[0], A[1], A[2], …, A[9]) n 함수 f Digit Int 0 A[0] 1 A[1]. . . 9 A[9] n Digit. To. Int n n the set of all functions from Digit to Int Digit Int
다차원 배열의 메모리 배치 n 예 n n n int x[10][20]; int[][] x = new int [10][20]; /* C 코드 */ /* Java 코드 */ 행우선 할당(Row-major order allocation) x[0, 0] x[0, 1]. . . x[0, 19] x[1, 0] x[1, 1]. . . x[9, 19] n X[i, j]의 주소 = base + i*n*w+ j*w = base + ( i*n + j)*w n 열우선 할당(Column-major order allocation)
순환 타입(Recursive Type) n 포인터는 순환 타입 정의에 유용하다. n 예 struct Char. List. Node { char data; struct Char. List. Node* next; /* legal in C */ }; typedef struct Char. List. Node* Char. List;
C Types n Basic n n void Numeric n Integral n n n Floating n n char, int, short, long enum float, double, long double Derived n n n Pointer Array Function struct union
Java Types n Primitive n n boolean Numeric n Integral n n Floating n n byte, char, short, int, long float, double Reference n n n Array class interface
타입 동등성 규칙의 종류 n 구조 동등성(Structural equivalence) n n 이름 동등성(Name equivalence) n n n Algol, FORTRAN, COBOL, Modula-3 C(구조체 및 유니온 제외) Java의 배열 Java의 class와 interface C의 구조체 및 유니온 선언 동등성(Declaration equivalence) n Pascal
예 struct Rec 1 { char x; int y; char z[10]; }; struct Rec 2 { char x; int y; char z[10]; }; struct Rec 3 { char y; int x; char z[10]; };
예 struct A { char x; int y; }; struct B { char x; int y; }; typedef struct A C; typedef C *P; typedef struct B *Q; tyedef struct A *R; typedef int S[10]; typedef int T[5]; int Age; int (*F)(int); Age (*G)(Age)
6. 6 타입 검사 (Type Checking)
타입 규칙(Typing Rule) n 정형적 표기법 n n n 정규식(regular expression) – 어휘분석 문맥-자유 문법(CFG) – 구분분석 타입 검사 규칙 n n n 논리적 추론 규칙(logical inference rule) “If this expression has this type and that expression has the other type, then this third expression must have another type. ” “if X and Y then Z” computations.
영어를 타입 규칙으로 표현 n n Read “x : T” as “x has type T”. 규칙 예 n n n If e 1 has type Int and e 2 has type Int, then e 1+ e 2 has type Int (e 1 has type Int e 2 has type Int) e 1+ e 2 has type Int (e 1 : Int e 2: Int) e 1+ e 2 : Int
타입 검사 과정 예 n (x+y)*z a[i] +i * int + z int x + int y int int [] i a array of int int
6. 7 타입 변환 (Type Conversion)
타입 변환 n 묵시적 변환(implicit conversion) int x = 3; … x = 2. 3 + x / 2 n 명시적 변환(explicit conversion) x = (int) (2. 3 + (double) (x /2));
- Slides: 61