Design Constraints in Data Implementation data abstraction (abstraction barrier) is good. the performance of the users of the data depends on the implementation/representation.

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

데이타를 여러방식으로 구현할 때 Multiple Representations 데이터 구현방식마다 꼬리표를 붙이고. 데이터 속내용 감추기 원리를 유지하면서. 예를 들어 complex number data 구현방안들을 생각해 보자 – complex number 만들기 make-from-real-imag:
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Type checking © Marcelo d’Amorim 2010.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation.
Termination Analysis Math Foundations of Computer Science.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Aggregate Data Structures COS 441 Princeton University Fall 2004.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
Propositional Calculus CS 680: Formal Methods in Verification Computer Systems Jeremy Johnson.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
PPL Pairs, lists and data abstraction. Compound Data Until now: atomic, unrelated entities Now: organized into structures Why? – Better conceptual level.
Multiplication properties. Multiplication properties.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
SICP Search Algorithms Why search Data structures that support search Breadth first vs. depth first.
D x (c) = 0 D x (x) = 1 D x (y) = 0 for y an independent variable D x (u+v) = D x (u) + D x (v) D x (uv) = u D x (v) + v D x (u) D x (u n ) = nu n-1 D.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
SICP Tagged data Why do we need tags Concept of tags Extended example.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Boolean Expression Evaluation CS 270: Math Foundations of CS Jeremy Johnson.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
SICP Tagged data Why do we need tags Concept of tags Extended example.
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Data Abstraction: Sets
Additional Scheme examples
ML: a quasi-functional language with strong typing
Tagged Data Tag: a symbol in a data structure that identifies its type
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Programming Languages Dan Grossman 2013
CSC 533: Programming Languages Spring 2017
6.001: Structure and Interpretation of Computer Programs
CSC 533: Programming Languages Spring 2016
CSC 533: Organization of Programming Languages Spring 2008
COP4020 Programming Languages
Binomial Priority Queues
Proving Properties of Recursive Functions and Data Structures
Functional Programming
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
Lecture #9 מבוא מורחב.
Functions, Patterns and Datatypes
Mutators for compound data Stack Queue
6.001 SICP Data abstractions
List and list operations (continue).
Functions, Patterns and Datatypes
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Binomial Priority Queues
CSC 533: Organization of Programming Languages Spring 2007
Functions, Patterns and Datatypes
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Lecture 25: The Metacircular Evaluator Eval Apply
Dispatch on Type (one-less x) => x-1 if x is a <number>
Presentation transcript:

Design Constraints in Data Implementation data abstraction (abstraction barrier) is good. the performance of the users of the data depends on the implementation/representation of the data.

Data Abstraction Example 3: Set set 만드는 방법 – emptyset: set – add-elmt:  * set -> set set 사용하는 방법 – emptyset?: set -> bool – get-elmt: set ->  * set – member?:  * set -> bool – union: set * set -> set – intersection: set * set -> set – exclusion: set * set -> set

Implementation Choices for Set choice 1: unordered list choice 2: ordered list choice 3: binary search tree implementation choice can determine the performance

데이터에 꼬리표 붙이기 Tagged data 꼬리표가 붙은 데이터 = 데이타마다 “ 소속 ” 정보가 매달려 있다. 데이터를 이용할 때는 그 소속을 확인하고 그 소속에 맞는 작업을 한다.

데이터에 꼬리표 붙이기: 왜? 하나의 data type을 만드는 다양한 방법이 있다. – tree인 경우: leaf, node – list인 경우: nil, link – symbolic expression인 경우: const, var, sum, product 주어진 data가 어떤 경우인가? 를 알수 있게 하려 면? 꼬리표를 덧붙인다. – leaf tree인가? node tree인가? – nil list인가? link list인가? – const expr인가? var expr인가? sum expr인가? product expr인가?

tree data tree 만들기 – leaf :  -> tree – node : tree list -> tree tree 사용하기 – leaf-val : tree ->  – is-leaf? : tree -> bool – is-empty-tree? : tree -> bool – nth-subtree : tree * nat -> tree

(define leaf-tag ‘ l) (define node-tag ‘ n) (define (leaf v) (cons leaf-tag v)) (define (node lst) (cons node-tag lst)) (define (is-empty-tree? tree) (and (equal? (car tree) node-tag) (null? (cdr tree)) ) (define (is-leaf? tree) (equal? (car tree) leaf-tag) )

boolean circuit data boolean circuit 만들기 – one: circuit – zero: circuit – not : circuit -> circuit – and : circuit * circuit -> circuit – or : circuit * circuit -> circuit boolean circuit 사용하기 – is-one? : circuit -> bool – is-zero? : circuit -> bool – is-not? : circuit -> bool – is-and? : circuit -> bool – is-or? : circuit -> bool – nth-circuit : circuit * nat -> circuit

(define one-tag ‘ one) (define zero-tag ‘ zero) (define not-tag ‘ not) (define and-tag ‘ and) (define or-tag ‘ or) (define one one-tag) (define zero zero-tag) (define (not circuit) (cons not-tag circuit)) (define (and c1 c2) (cons and-tag (cons c1 c2))) (define (or c1 c2) (cons or-tag (cons c1 c2))) (define (is-one c) (equal? one-tag c)) (define (is-zero c) (equal? zero-tag c)) (define (is-and c) (equal? and-tag (car c))) (define (is-or c) (equal? or-tag (car c)))

symbolic expression data 식 만들기 – const : int -> expr – var : string -> expr – sum : expr * expr -> expr – product : expr * expr -> expr 식 사용하기 – is-const? : expr -> bool – is-var? : expr -> bool – is-sum? : expr -> bool – is-product? : expr -> bool – const-val: expr -> int – var-name: expr -> string – addend: expr -> expr – augend: expr -> expr – multiplier: expr -> expr – multiplicand: expr -> expr

(define const-tag ‘ c) (define var-tag ‘ v) (define sum-tag ‘ +) (define prod-tag ‘ *) (define (const n) (cons const-tag n)) (define (var x) (cons var-tag x)) (define (sum e1 e2) (cons sum-tag (cons e1 e2))) (define (product e1 e2) (cons prod-tag (cons e1 e2))) (define (is-const? e) (equal? (car e) const-tag)) (define (is-var? e) (equal? (car e) var-tag)) (define (is-sum? e) (equal? (car e) sum-tag)) (define (is-product? e) (equal (car e ) prod-tag))

꼬리표를 붙이면 또 어떤 잇점이? 안전한 프로그래밍 defensive programming – 함수들이 주어진 데이터가 적절한지 첵크하기 좋다: sanity check, type check – 중요: 최대한 빨리 오류를 감지하기. 데이타가 주도하는 프로그래밍 data- directed programming – 함수들이 주어진 데이터의 소속에 따라서 하는 일을 정한다.

안전한 프로그래밍 defensive programming dynamic type checking: – 함수의 인자가 그 함수가 예상하는 타입의 인자 인가? – 그러하면, 그 중에서 어떤 경우인가? (define (nth-child tree n) (cond ((is-leaf? tree) (error)) ((is-empty-tree? tree) (error)) ((< n 0) (error)) (else … ) )

안전한 프로그래밍 defensive programming 안전수칙 0: 함수를 정의할 때, 함수 타입에 맞는 데이터를 받는지 확인한다. 꼬리표+기타 방식으로. 안전수칙 1: 함수를 정의할 때, 타입에 맞는 데이터 중에서 원하는 경우의 데이터인지를 확인한다. 꼬리 표를 가지고. 안전불감증 vs 안전집착: – 불감 보다는 집착이 좋다. – 실행중에 “ 옳지않은 ” 값이 나도 모르게 흘러다니는 것 보다는 그럴 경우 바로 중단시키는 것이 좋다.

안전수칙을 지키고 있는가? (define (nth-child tree n) (cond ((is-leaf? tree) (error)) ((is-empty-tree? tree) (error)) ((< n 0) (error)) (else … ) ) (nth-child a-node-tree (fac 10)) (nth-child (cons 1 2) 1) 어떻게되나?