CS 227 Meeting -*- Outline -*- Note: be careful to distinguish ratl from the built in type rational this will help the students avoid confusion. * Exact Arithmetic and Data Abstraction (Section 3.3) ** context *** boring Suppose you have a Scheme such as a PC Scheme, which does not have exact fractions. Or in some other system. Or your little brother or sister is doing fractions. Want to see how a system like Chez Scheme implements rationals. we'll call the ones we're implementing "ratl" as opposed to Chez Scheme's "rational" (and try to call that fractions) Precise understanding of *how* to do fractions. *** more fun You are Doctor Sevens, chief scientist of Numeric Only Machines (whose motto is "Compute Only Numbers Speedily") TA introduces you with: Greetings to you all, former employees of Symbolic Computers. We here at Numeric Only Machines hope you enjoy working for your new company, and that there are no hard feelings after our hostile takeover of Symbolic Computers. You've all been hired to work on the rational numbers project, code named ratl. (write: "the ratl project" on the board) The ratl is part of our strategic plan to bring advanced numerical computation to the world! To tell you more about the ratl project, it is my pleasure to introduce to you our chief numerical scientist, and head of the ratl project, Dr. Sevens! You say something like... Thank you, and welcome to all you new employees. We're excited about the ratl project, so let's get right to it. Your task will be to implement various procedures for ratls, such as addition, multiplication, etc. (Play with the godelian representation of rationals, using rprint, and then showing them the real output. (load "ch3.ss") (load "ratl4.ss") Lay it on thick that they should understand the details of the representation as numbers.) (Explain that on Numeric only machines, even lists are represented by numbers, give the boxcar diagram explanation, pointing out that large numbers are treated as pointers, smaller ones as numbers. Ask for questions:) TA says (after the explanation of how lists are represented by numbers when questions are asked for): But, Dr. Sevens, aren't numbers just abstractions of of voltages? For example, a 1 is a high voltage, and 0 is a low voltage. Don't you think it's helpful to think about voltages, latches, gates, etc. when you program? I always do, because that's what's really happening--- although it takes me a long time to write programs. Other TA (after the electrical questioner says that it takes a long time to write programs): But, Dr. Sevens, aren't voltages, gates, latches, etc. just abstractions themselves of the underlying physics? In physics we describe reality using elementary particales and the principles of electromagnetic theory. For example, when I program I always think about Maxwell's equations --- although I'm still working on the first programming assignment for this class... (Say, well maybe it will be easiest if we all stick to the level of the ratls, the highest and most abstract level. It may be a bit far from the physics, but I don't think it's necessary to understand quantum mechanics before we can write our programs.) ** Working with Ratls. *** the basics -------------- RATL VALUES (FRACTIONS) def: a ratl value is n/d where n and d are integers, and d is not 0. RATL OBJECTS def: a ratl object is (make-ratl n d) where n and d are integers, and d is not 0. -------------- Our model ----------- BASIC PROCEDURES for RATLS make-ratl : (-> (integer integer) ratl) numr : (-> (ratl) integer) denr : (-> (ratl) integer) EQUATIONS (numr (make-ratl 3 5)) = 3 (denr (make-ratl 3 5)) = 5 ----------- the last 2 equations don't tell the whole story... How would you implement these? (Other ways?) These equations *specify* numr, make-ratl, and denr *** Arithmetic 0/3 = 0 and 0/2 = 0 So rzero? only has to check numerator (show rzero? or write it) **** Addition (a/b) + (c/d) = ((a*d) + (b*c)) / (b*d) So write r+ **** Multiplication (a/b) * (c/d) = (a*c) / (b*d) So write r* **** Subtraction Can do 2 ways: negate numerator (or denominator) and add (do not do this, it is one of the exercises) or define directly Cheaper to define directly Write r- **** Inversion and Division (a/b) inverted is (b/a). But b must not be zero. Write rinvert. (a/b) / (c/d) = (a/b)*(d/c) So here we let the r/ procedure do that. **** Equality test (a/b) = (c/d) iff ad = bc. Write r= **** Test for positive ask them to write **** Comparisons (a/b) > (c/d) iff ad > bc iff (a/b) - (c/d) > 0 the second leads to the book's program. **** Max and Min Write these and note how similar they are. Also write max and min for integers. Note similarities (need slide). Want to make a procedure that captures the essence of these procs, so do not have to write this pattern each time. ----------- (define extreme-value ; TYPE: (-> (T (-> (T) boolean) T T) ; boolean) (lambda (pred x y) (if (pred x y) x y))) (define rmax ; TYPE: (-> (ratl ratl) ratl) (lambda (x y) ; ENSURES: result is the largest arg (extreme-value r> x y))) (define rmin ; TYPE: (-> (ratl ratl) ratl) (lambda (x y) ; ENSURES: result is the smallest arg (extreme-value r< x y))) ----------- **** Printing ----------------- ; prog 3.20 (define rprint (lambda (rtl) (writeln (numr rtl) "/" (denr rtl)))) ----------------- ** Data Abstraction did a lot of things just using make-ratl, numr and denr. - only had to know how they are supposed to work. - our code will work with different representations. this is *representation independence* - what we mean by "too low level" in HW is using the representation in a way that loses representation independence. - Run with 2 representations - lists - cons cells extremely important: allows you to focus on what's important, not details allows you to replace slow impl. with faster one, without changing code ** another example Ask them, in groups, to come up with as many ways to define the variables *yes*, *no*, and *maybe* as possible then for two ways to code the basic procedures. ----------------- THE ANSWERS Def: an answer value is either yes, no, or maybe. Def: an answer object is the value of one of the variables: *yes*, *no*, or *maybe* BASIC PROCEDURES (ans-and *yes* *yes*) = *yes* (ans-and *yes* *maybe*) = *maybe* (ans-and *yes* *no*) = *no* (ans-and *maybe* *maybe*) = *maybe* (ans-and *maybe* *no*) = *no* (ans-and *no* *no*) = *no* (ans-and x y) = (ans-and y x) (ans-or *yes* *yes*) = *yes* (ans-or *yes* *maybe*) = *yes* (ans-or *yes* *no*) = *yes* (ans-or *maybe* *maybe*) = *maybe* (ans-or *maybe* *no*) = *maybe* (ans-or *no* *no*) = *no* (ans-or x y) = (ans-or y x) --------------------