v v boolean char int float double v
기본 타입: 예 v 사전 정의된 기본 타입 v boolean, char, int, float, double, … v 나열 타입 v enum Color {Red, Green, Blue} v 부분범위 타입(Ada, Pascal) v type digit is range 0. . 9; v 부분범위 타입 없음(C, C++, Java) v v byte digit; // -128. . 127 if (digit >9 || digit < 0) throw new Digit. Exception(); © 숙대 창병모 3
데이터 타입의 의미 v v 값들의 집합(a set of values) 그 값에 적용 가능한 연산들의 모음(a set of operations) v 정의 2 A data type is a set of values + a set of operations on those values. v 데이터 타입의 수학적 의미 v v v an algebra (S, Op) where S is a set of values Op is a set of operations on S © 숙대 창병모 5
구조체(레코드) 타입 v C의 예 struct Int. Char. Real { int i; char c; float r; } v Ada의 예 type Int. Char. Real is record i: integer; c: character; r: float; end record; © 숙대 창병모 7
C 언어의 순환 타입 v 포인터는 순환 타입 정의에 유용하다. v 예 struct Char. List. Node { char data; struct Char. List. Node* next; /* legal in C */ }; typedef struct Char. List. Node* Char. List; © 숙대 창병모 13
C Types v Basic v v void Numeric v v Integral v char, int, short, long v enum Floating v float, double, long double v Derived v v v pointer array function struct union © 숙대 창병모 15
Java Types v Primitive v boolean v Numeric v v Integral v byte, char, short, int, long Floating v float, double v Reference v array v class v interface © 숙대 창병모 16
타입 오류 v let int x = 1 in x = ! x + 2 end v let int x = 1, bool y = true in x = x + y end v let int x = 1, bool y = true in if (…) then x = x + 1 else x = x + y end v let fun int f(int x): x = x + 1 in f = f + 1 end © 숙대 창병모 19
지금까지 한 것/앞으로 할 것! v Topic Logic Implementation --------------------------v Syntax Grammar Parser v Semantics rules Interpreter v Type Typing rules Type checker © 숙대 창병모 26
타입 규칙(Typing Rule) v 타입 검사에 사용되는 규칙 논리적 추론 규칙(logical inference rule) v “If this expression has this type and that expression has the other type, then this third expression must have another type. ” v “if X and Y then Z” v © 숙대 창병모 27
영어를 타입 규칙으로 표현 v Read “x : T” as “x has type T”. v 규칙 예 v If e 1 has type Int and e 2 has type Int, then e 1 + e 2 has type Int v (e 1 has type Int e 2 has type Int) e 1 + e 2 has type Int v (e 1 : Int © 숙대 창병모 e 2 : Int) e 1 + e 2 : Int 28
영어를 타입 규칙으로 표현 v 추론 규칙 예 (e 1 : Int e 2 : Int) e 1+ e 2 : Int v 일반적 형태 Hypothesis 1 … Hypothesisn Conclusion v 추론 규칙 Hypothesis 1 … Hypothesisn Conclusion © 숙대 창병모 29
언어 S v Stmt S x = E | S 1; S 2 | if E then S 1 else S 2 | while E do S | read x | print E | let t x = e in S end | let fun t f(t x) = S 1 in S 2 end | return E v Expr E n | x | true | false | f(E) | E + E | E – E | E * E | E / E | E == E | E < E | E > E | !E © 숙대 창병모 33
Example 1 v let int x = 0 in x = x + 1 end v 타입 검사 {x ↦ int} ⊢ x: int {x ↦ int} ⊢ 1: int {x ↦ int} ⊢ x + 1: int { } ⊢ 0 : int {x ↦ int} ⊢ x = x + 1: void { } ⊢ let int x = 0 in x = x + 1 end: void © 숙대 창병모 39
Example 2 v 다음 함수의 타입은 bool f(int x) : if x > 0 then return true else return false T[x ↦ int] ⊢ x: int T[x ↦ int] ⊢ 0: int T[x ↦ int] ⊢ x > 0: bool T[x ↦ int] ⊢ true: bool . . . T[x ↦ int] ⊢ return true: bool. . . T[x ↦ int] ⊢ if … : bool T ⊢ fun bool f(int x): if … © 숙대 창병모 : int bool 42
타입 안전성: 사례 v Not safe: BCPL family, including C and C++ v Casts, pointer arithmetic v Almost safe: Algol family, Pascal, Ada. v Dangling pointers. v v v Allocate a pointer p to an integer, deallocate the memory referenced by p, then later use the value pointed to by p v Safe: Lisp, ML, Smalltalk, and Java v v Lisp, Smalltalk: dynamically typed ML, Java: statically typed © 숙대 창병모 44
- Slides: 47