Principles of Programming Languages Practice session 6 Data














![Data Abstraction For all x, y, r: applicative-eval[ (get-radius (make-circle x y r)) ] Data Abstraction For all x, y, r: applicative-eval[ (get-radius (make-circle x y r)) ]](https://slidetodoc.com/presentation_image_h2/fc0cc8399b355a3e649e7d4d5129fb58/image-15.jpg)






- Slides: 21

Principles of Programming Languages Practice session 6


Data Abstraction ( ADT! ) ומה שביניהם Client, Supplier �

Data Abstraction עבור מעגל במישור ADT יצירת : 1 � שאלה . חישוב שטח מעגל , הזזת מעגל : נניח שלקוח מעוניין בפעולות הבאות הבא עבור מעגל במישור )ממשק ADT - הספק יספק ללקוח את ה : הממשק . ( ואינווריאנטות Signature: make-circle(x-center, y-center, radius) Purpose: constructs a circle. Type: [Number * Number -> Circle] Pre-conditions: radius >= 0 Signature: circle? (x) Purpose: returns true iff x is a circle. Type: [T -> Boolean] Signature: get-x-center(circle) Purpose: returns the x coordinate of the circle's center. Type: [Circle -> Number] Signature: circle-equal? (circle 1, circle 2) Purpose: Compares two circles. Type: [Circle * Circle -> Boolean] Signature: get-y-center(circle) Purpose: returns the y coordinate of the circle's center. Type: [Circle -> Number] *Signature: small-enough? (circle, edge) Purpose: Check whether the circle can fit into a square of a given edge length. Type: [Circle * Number -> Boolean]

Data Abstraction עבור מעגל במישור ADT יצירת : 1 � שאלה . חישוב שטח מעגל , הזזת מעגל : נניח שלקוח מעוניין בפעולות הבאות הבא עבור מעגל במישור )ממשק ADT - הספק יספק ללקוח את ה : האינווריאנטות . ( ואינווריאנטות get-x-center(make-circle(x, y, r)) = x get-y-center(make-circle(x, y, r)) = y get-radius(make-circle(x, y, r)) = r circle? (e) ≡ ∃x, y, r: make-circle(x, y, r) = e circle-equal(e 1, e 2) ≡ get-x-center(e 1) = get-x-center(e 2) & get-y-center(e 1) = get-y-center(e 2) & get- radius(e 1) = get- radius(e 2) small-enough(make-circle(x, y, r), a) ≡ 2 r≤a : x, y, r לכל. 1 : e לכל. 2 : e 1, e 2 לכל. 3 : x, y, r, a לכל. 4

Data Abstraction עבור מעגל במישור – צד הלקוח ADT יצירת : 1 שאלה Signature: move-circle(circle, x, y) Purpose: return a circle, with a center point that is transposed by (x, y) (we get a new circle, in Scheme we can't change the existing one) Type: [Circle * Number -> Circle] (define move-circle (lambda (circle x y) (make-circle (+ (get-x-center circle) x) (+ (get-y-center circle) y) (get-radius circle)))) Signature: area-circle(circle) Purpose: Calculate the area of circle Type: [Circle -> Number] (define area-circle (lambda (circle) (* pi (sqr (get-radius circle))))) �


Data Abstraction : ממשק : tagging עבור ADT Signature: attach-tag(x, tag) Purpose: Construct a tagged-data value Type: [T * Symbol -> Tagged-data(T)] Signature: tagged-data? (datum) Purpose: Identify tagged-data values Type: [T -> Boolean] Signature: get-tag(tagged) Purpose: Select the tag from a tagged-data value Type: [Tagged-data(T) -> Symbol] Signature: tagged-by? (tagged, tag) Purpose: Identify tagged-data values Type: [T * Symbol -> Boolean] Signature: get-content(tagged) Purpose: Select the data from a tagged-data value Type: [Tagged-data(T) -> T] �

Data Abstraction : מימוש : tagging עבור ADT (define attach-tag (lambda (x tag) (cons tag x))) (define get-tag (lambda (tagged) (car tagged))) (define get-content (lambda (tagged) (cdr tagged))) � (define tagged-data? (lambda (datum) (and (pair? datum) (symbol? (car datum))))) (define tagged-by? (lambda (x tag) (and (tagged-data? x) (eq? (get-tag x) tag))))

Data Abstraction מימוש - עבור מעגל במישור – צד הספק ADT יצירת : 1 שאלה � Signature: make-circle(x-center, y-center, radius) Type: [Number * Number -> Tagged-data(Pair(Number, Number)))] (define make-circle (lambda (x-center y-center radius) (attach-tag (cons radius (cons x-center y-center)) 'circle))). ללקוח ADT- ולא את הטיפוסים שמספק ה , בהגדרת המימוש מציגים את הטיפוסים של המימוש : במימוש זה הטיפוס של המעגל הינו Circle = Tagged-data(Pair(Number, Number)))

Data Abstraction מימוש - עבור מעגל במישור – צד הספק ADT יצירת : 1 שאלה Signature: get-x-center(circle), get-y-center(circle), get-radius(circle) Type: [Tagged-data(Pair(Number, Number))) -> Number] Pre-condition: (circle? circle) = #t (define get-x-center (lambda (circle) (cadr (get-content circle)))) (define get-y-center (lambda (circle) (cddr (get-content circle)))) (define get-radius (lambda (circle) (car (get-content circle)))) �

Data Abstraction מימוש - עבור מעגל במישור – צד הספק ADT יצירת : 1 שאלה � Signature: circle? (object) Type: [T -> Boolean] (define circle? (lambda (circle) (tagged-by? circle 'circle))) Signature: small-enough? (circle, edge) Type: [Tagged-data(Pair(Number, Number))) * Number -> Boolean] Pre-condition: (circle? circle) = #t (define small-enough? (lambda (circle edge) (<= ((* 2 (get-radius circle)) edge)))) שימו לב כי במסגרת מימוש הפרדיקטים לא השתמשנו בפרטי המימוש של המעגל זה נותן לנו אפשרות לשנות את המימוש של המעגל. אלא רק בסלקטורים , ( )הזוגות . מבלי לשכתב את הפרדיקטים

Data Abstraction מימוש - עבור מעגל במישור – צד הספק ADT יצירת : 1 שאלה � Signature: circle-equal? (circle 1, circle 2) Type: [Tagged-data(Pair(Number, Number)) * Tagged-data(Pair(Number, Number))) -> Boolean] Pre-condition: (and (circle? circle 1) (circle? circle 2)) = #t (define circle-equal? (lambda (circle 1 circle 2) (and (= (get-x-center circle 1) (get-x-center circle 2)) (= (get-y-center circle 1) (get-y-center circle 2)) (= (get-radius circle 1) (get-radius circle 2))))) שימו לב כי במסגרת מימוש הפרדיקטים לא השתמשנו בפרטי המימוש של המעגל זה נותן לנו אפשרות לשנות את המימוש של המעגל. אלא רק בסלקטורים , ( )הזוגות . מבלי לשכתב את הפרדיקטים

![Data Abstraction For all x y r applicativeeval getradius makecircle x y r Data Abstraction For all x, y, r: applicative-eval[ (get-radius (make-circle x y r)) ]](https://slidetodoc.com/presentation_image_h2/fc0cc8399b355a3e649e7d4d5129fb58/image-15.jpg)
Data Abstraction For all x, y, r: applicative-eval[ (get-radius (make-circle x y r)) ] ==>* r (define make-circle (lambda (x-center y-center radius) (attach-tag (cons radius (cons x-center y-center)) 'circle))) (define get-x-center (lambda (circle) (cadr (get-content circle)))) (define get-y-center (lambda (circle) (cddr (get-content circle)))) (define get-radius (lambda (circle) (car (get-content circle)))) (define circle? (lambda (circle) (tagged-by? circle 'circle)))


Data Abstraction עבור מעגל במישור – צד הספק – מימוש חרוץ ADT יצירת : 2 שאלה � Signature: make-circle(x-center, y-center, radius) Type: [Number * Number -> Tagged-data([Symbol -> Number])] (define make-circle (lambda (x-center y-center radius) (attach-tag (lambda (m) (cond ((eq? m 'x) x-center) ((eq? m 'y) y-center) (else radius))) 'circle))) Signature: get-x-center(circle), get-y-center(circle), get-radius(circle) Type: [Tagged-data([Symbol -> Number] ) -> Number] Pre-condition: (circle? circle) = #t (define get-x-center (lambda (circle) ((get-content circle) 'x))) ? השאר ללא שינוי – למה (define get-y-center (lambda (circle) ((get-content circle) 'y))) (define get-radius (lambda (circle) ((get-content circle) 'r)))


Data Abstraction עבור מעגל במישור – צד הספק – מימוש עצל ADT יצירת : 3 שאלה במימוש הזה הבנאי עוטף את המידע בתוך האובייקט באמצעות פרוצדורה אשר מצפה . שתופעל על המידע , כפרמטר לפרוצדורה אחרת � Signature: make-circle(x-center, y-center, radius) Type: [Number * Number -> Tagged-data([[Number * Number -> T])] (define make-circle (lambda (x-center y-center radius) (attach-tag (lambda(sel) (sel x-center y-center radius)) 'circle))) Signature: get-x-center(circle), get-y-center(circle), get-radius(circle) Type: [Tagged-data([[Number * Number->T]) -> Numb Pre-condition: (circle? circle) = #t (define get-x-center (lambda (circle) ((get-content circle) (lambda (x y r) x)))) (define get-y-center (lambda (circle) ((get-content circle) (lambda (x y r) y))))

