Data Abstraction and Modularity abstraction is the key principle to control the complexity electro-physics transitor microprocessor operating system library user program boolean circuits machine instructions assembly language C Java, ML, Haskell B, Z, HOL, Isabelle specification
abstraction: 자세한 속사정을 감춘다는 의미 - fixed interface - implementation/representation-independence Abstraction example: functional abstraction - fixed interface: name and its type - implementation-independence add(x,y) sort(a) Types are “the” language of interface
abstraction: 자세한 속사정을 감춘다는 의미 - fixed interface - implementation/representation-independence Abstraction example: data abstraction interface of int queue exception EmptyQ val emptyqueue: queue val enqueue: queue -> int -> queue val dequeue: queue -> int * queue implementation of int queue exception EmptyQ let emptyqueue =... let enqueue q n =... let dequeue q =...
interface of car val makeit: engine * size -> car val getin: car * animal -> car val getoff: car -> car val drive: car -> car implementation of car let makeit(e,s) =... let getin(c,a) =......
Language Support for Data Abstraction Use type-checking to enforce the separation: - abstract data type - module * Type checking checks if every computation is done through the interface. * Type checking checks if no information other than the interface is assumed.
module Queue = struct type queue = T | N of int * queue let emptyqueue = T let enqueue q n = n::q let dequeue q =... end module type QUEUE = sig type queue val emptyqueue: queue val enqueue: queue -> int -> queue val dequeue: queue -> int * queue end : QUEUE In ML, done by signature matching
In C, pretended by.h and.c files queue.h struct queue {...}; struct nqpair {...}; queue *emptyqueue; queue *enqueue(queue*, int); nqpair *dequeue(queue*); queue.c struct queue {...}; struct nqpair {...}; queue *emptyqueue {... }; queue *enqueue(queue* q, int x) {...}; nqpair *dequeue(queue* q) {...}; How to hide and check? 별드레게 무러바
Modules are more general than abstract data type - Modules are collection of definitions - Signatures are interfaces - Signature matching (wrapping modules by signatures) hides those not in the signature - Modules can be parameterized by modules/types Remember the ML modules and module types
Language support for abstraction: - functional abstraction, data abstraction - abstract data type, module ML: module, module type, functor C++: class, class template Ada: package, generic package - type-system automaticallys checks if the programs violates the principle ML/Haskell: sound C++, Ada: not sound Abstraction is the must in civilization. Also true in computer science. Also true in programming: - interface, implementation-independence
Object-Oriented Programming v.s. Value-Oriented Programming imperative v.s. applicative 물건중심 기계중심 명령형 객체지향 값중심 생각중심 함수형 Two representative programming paradigms. What are they?
a := 1; b := 2; c := a + b; d := a + b; 1. c 의 3 이 d 의 3 과 같은가 ? 2. a 를 바꾸면 c 도 바뀔까 ? 3. d 를 바꾸면 c 도 바뀔까 ? 4. e:=c 를 수행하면 c 를 복사해야하나 ? 5. c 갖고 일 봤으면, 그 3 을 없애도 되나 ? 싱거운 질문들
1. c 의 집합이 d 의 집합과 같은가 ? 2. a 를 바꾸면 c 도 바뀔까 ? 3. d 를 바꾸면 c 도 바뀔까 ? 4. e:=c 를 수행하면 c 를 복사해야하나 ? 5. c 갖고 일 봤으면, 그 집합을 없애도 되나 ? a := set(1,2,3); b := set(4,5,6); c := setUnion(a,b); d := setUnion(a,b); 아직도 싱거울까 ? 정수보다는 복잡한 ( 컴퓨터에 구현할 때 하는일이 많은 ) 경우
항상 공유하도록 구현하는 방안 항상 복사하도록 구현하는 방안 두가지의 짬뽕 - 무조건 복사 - 프로그램이해가 쉽다 - 메모리 소모가 크다 - 얼키고 설키고 - 버그없는 프로그램짜기 ? - 물건이 변하지않게 - 최대한 공유
fun eval(Var x, env, mem) = (entry env x, mem) | eval(Add(e,e’), env, mem) = let val (z, mem’) = eval(e, env, mem) val (z’, mem’’) = eval(e’, env, mem’) in (z+z’, mem’’) end | eval(Assign(x,e), env, mem) = let val (v,mem’) = eval(e, env, mem) val mem’’ = assign(mem’, x, v) in (v,mem’’) end