Lecture 30: The More Things Change, the More They are the Same 2

###
Lecture 30: The More Things Change, the More They are the Same 2

1. Summary of set! vs set-xyz-abc!

   Purpose: | assignment to a variable | changing a structure
   --- | --- | ---
   Syntax: |
       (define variable expression)
       ...
       (set! variable expression)
       ...
       (define-struct xyz (... abc ...))
       ...
       (set-xyz-abc! expression expression)
       ...
   Evaluation: |
       (define variable Value1)
       ...
       (set! variable Value2)
       ...

       =

       (define variable Value2)
       ...
       (void)
       ...
       (define-struct xyz (... abc ...))
       ...
       (define xyz-struct (make-xyz ... Value1 ...))

       (set-xyz-abc! xyz-struct Value2)
       ...
       =
       (define-struct xyz (... abc ...))
       ...
       (define xyz-struct (make-xyz ... Value2 ...))

       (void)
       ...
   Examples: | see previous lecture |

2. extensional versus intensional equality

   Take a look at

       (make-phone 'Bumpy 5556666)

   and

       (make-phone 'Bumpy 5556666)

   They are identical expressions.

   Both are values that belong to the class phone-record.

   Are they the same values? It depends on the meaning of "same". (With apologies to the now-infamous "Bills.")

   Here is the extensional meaning of equal. For atomic values, there is an atomic notion of equality: = for numbers, eq? for booleans and symbols. For compound values, we must inspect their constituents and determine whether they are equal. Task:

       ;; phone-equal? : phone-record phone-record -> bool
       (define (phone-equal? pr1 pr2)
         (and (eq? (phone-name pr1) (phone-name pr2))
              (= (phone-number pr1) (phone-number pr2))))

       (define BUMP (make-phone 'Bumpy 5556666))

         ------------------------------------------------------------

       (phone-equal? (make-phone 'Bumpy 5556666) (make-phone 'Bumpy 5556666))
       =
       #t

       (phone-equal? BUMP BUMP)
       =
       #t

       (phone-equal? BUMP (make-phone 'Bumpy 5556666))
       =
       #t

   Here is the intensional meaning of equal. For atomic values, we still use atomic equalities. For compound values, we check whether "hurting" one of the two records also hurts the other one.

       ;; phone-eq? : phone-record phone-record -> bool
       (define (phone-eq? pr1 pr2)
         (begin
           (set-phone-number! pr1 9999999)
           (= (phone-number pr2) 9999999)))

       (define BUMP (make-phone 'Bumpy 5556666))

       > (phone-eq? (make-phone 'Bumpy 5556666) (make-phone 'Bumpy 5556666))
       #f -- and, in addition, we have also modified one of the records.

       > (phone-eq? BUMP BUMP)
       #t -- and, in addition, we have also modified the record.

       > (phone-equal? BUMP (make-phone 'Bumpy 5556666))
       #f -- which used to be, and should be, #t

   Task: modify `phone-eq?` so that it does NOT change the field of one of its records.

       (define (phone-eq? pr1 pr2)
         (local ((define old1 (phone-number pr1)))
           (begin
             (set-phone-number! pr1 9999999)
             (local ((define resylt (= (phone-number pr2) 9999999)))
       	(begin
       	  (set-phone-number! pr1 old1)
       	  result)))))

   There is still a small problem:

       > (phone-eq? (make-phone 'Bumpy 5556666) (make-phone 'Bumper 9999999))
       #t -- why?

   Task: fix it again!

Matthias Felleisen | This page was generated on Mon Apr 12 15:23:18 CDT 1999.