The purpose of this assignment is to help you get acquainted with pure objectoriented programming. The assignment is divided into two parts.
You will find a uSmalltalk interpreter in
/comp/105/bin/usmalltalk
.
This interpreter treats the variable
&trace
specially; by defining it to be an integer n with val &trace n
,
you can trace message sends and answers to depth n
.
It is an invaluable aid to debugging.
You will find useful sources are in the git repository, which you can clone by
git clone linux.cs.tufts.edu:/comp/105/buildprovecompare
The repository examples
directory includes
copies of the initial basis, collection classes, financial history,
and other examples from the textbook.
protocol
and localProtocol
methods which are defined
on every class, as shown in Figure 10.5 on
page 427.
To solve these problems, you shouldn't need to add or change more than 10 lines of code in total.
My Natural
class is over 100 lines of uSmalltalk code;
my largeinteger classes are 22 lines apiece.
My modifications to predefined number classes are about 25 lines.
Estimated difficulty: ****
You will find bignums and the bignum algorithms discussed at some length in Dave Hanson's book (which should be free online to Tufts students) and in the article by Per Brinch Hansen. Be aware that your assignment below differs significantly from the implementation in Hanson's book.
Natural
.
I recommend that you use the digit:
and digit:put:
methods
to hide the 1based nature of the underlying representation.
decimal
method must return a
list of decimal
digits, even if base 10 is not what is used in the representation.
Suppress leading zeroes
unless the value of Natural
is itself zero.
XP_add
does add with carry.
XP_sub
does subtract with borrow.
XP_mul
does z := z + x * y
, which is useful, but is not what we want unless
z
is zero initially.
Moreover, Hanson has to pass all the lengths explicitly.
Natural
is an immutable type.
Your methods must not mutate existing natural numbers; you
can mutate only a newly allocated number that you are sure has not
been seen by any client.
digit:
method carefully, you'll have to
worry about sizes only when you allocate new results.
SmallInteger
.
In order to make your solution work with an unmodified
usmalltalk,
you must use this technique.
<fact.smt>= (define factorial (n) (if (strictlyPositive n) [(* n (value factorial ( n 1)))] [1])) (class Factorial Object () (classMethod printUpto: (limit) (locals n nfac) (begin (set n 1) (set nfac 1) (while [(<= n limit)] [(print n) (print #!) (print space) (print #=) (print space) (println nfac) (set n (+ n 1)) (set nfac (* n nfac))]))))
You might find it useful to test your implementation with the following table of factorials:
1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 11! = 39916800 12! = 479001600 13! = 6227020800 14! = 87178291200 15! = 1307674368000 16! = 20922789888000 17! = 355687428096000 18! = 6402373705728000 19! = 121645100408832000 20! = 2432902008176640000 21! = 51090942171709440000 22! = 1124000727777607680000 23! = 25852016738884976640000 24! = 620448401733239439360000 25! = 15511210043330985984000000Be warned that this test by itself is inadequate. You will want other tests. Here is some advice
Natural
by generating a long, random
string of digits, then computing the corresponding number using a
combination of addition and multiplication by 10.
(set p n) ; p == n (set p (+ p p)) ; p == 2n (set p (+ p p)) ; p == 4n (set p (+ p n)) ; p == 5n (set p (+ p p)) ; p == 10nThis idea will test only your addition; if you have bugs there, fix them before you go on.
Array
or List
that prints just the digits, with no spaces.
timesRepeat:
method is defined on any integer.
define
syntax is slightly different
from what we use in uScheme.)
We recommend you use ghci on the command line; standard infix
syntax works.
If you want something more elaborate,
use Standard ML
of New Jersey (command sml),
which has an IntInf
module that implements bignums.
Your bigtests.smt file should be formatted as follows:
; Summary: .........(Your summary should be a simple English phrase that describes your test. Examples might be ``Ackermann's function of (1, 1),'' ``sequence of powers of 2,'' or ``combinations of +, *, and  on random numbers.'')
Test105
with a class method run
that actually runs the test.
run
method to the Test105
class, and nothing else.
If the output is a single line, write a oneline comment.
If the output takes multiple lines, put each line of output in a
comment on its own line.
(run Test105)
must take less than 2
CPU seconds to evaluate.
<example bigtests.smt>= ; Summary: 10 to the tenth power (class Test105 Object () (classmethod run () (locals n 10tothen) (set n 0) (set 10tothen 1) (whileTrue: [(< n 10)] [(set n (+ n 1)) (set 10tothen (* 10 10tothen))]) 10tothen) ) ; 10000000000
If this test is run in an unmodified interpreter, it breaks with an arithmetic overflow and a stack trace.
b = 2
and sometimes b = 10
, but when we
want bignums, the choice of b
is
hard to make in the general case:
b
= 10, then converting to decimal representation is trivial, but
storing bignums requires lots of memory.
b
is, the less memory is required, and the more
efficient everything is.
b
is a power of 10, converting to decimal is
relatively easy and is very efficient.
Otherwise it requires (possibly long) division.
(b1) * (b1)
fits in a machine word, than you can implement
multiplication in highlevel languages without difficulty.
(Serious implementations pick the largest b
such that a[i]
is
guaranteed to fit in a machine word, e.g., 2^64
on modern
machines.
Unfortunately, to work with such large values of b
requires
special machine instructions to support ``add with carry'' and 128bit
multiply, so serious implementations have to be written in assembly
language.)
b
is a power of 2, bitshift can be very efficient, but
conversion to decimal is expensive.
Fast bitshift can be important in cryptographic and communications
applications.
b
'scomplement.
Knuth volume 2 is
pretty informative about these topics.
For extra credit,
try the following variations on your implementation of class Natural
:
base
and not any other code.)
Measure the time needed to compute the first 50 factorials.
Note both your measurements and your argument in your README file.
Natural
and for large integers.
If this changes your argument for the largest possible base, explain
how.
Largest base.
Change the base to the largest reasonable base, not necessarily a
power of 10.
You will have to reimplement decimal
using long division.
Measure the time needed to compute and print the first 50 factorials.
Does the smaller number of digits recoup the higher cost of converting
to decimal?
Comparisons.
Make sure comparisons work, even with mixed kinds of integers.
So for example, make sure comparisons such as
(< 5 (* 1000000 1000000))
produce sensible answers.
Space costs.
Instrument your Natural
class
to keep track of the size of numbers, and measure the
space cost of the different bases.
Estimate the difference in garbagecollection overhead for computing
with the different bases, given a fixedsize heap.
Pi (hard). Use a power series to compute the first 100 digits of pi (the ratio of a circle's circumference to its diameter). Be sure to cite your sources for the proper series approximation and its convergence properties. Hint: I vaguely remember that there's a faster convergence for pi over 4. Check with a numerical analyst.
0
''.
finhist.smt
showing your solution to
Exercise 9.
basis.smt
showing whatever changes you had
to make to the initial basis to do Exercise
29a.
Please identify your solutions using conspicuous comments,
e.g.,
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; ;;;; solution to Exercise 4 (class Array ... )
README
file that should include some
indication of how you tested your bignum code for Part II.
Of course we also want the usual stuff about your collaborators and
your time spent.
bignum.smt
showing your solutions to
Exercises
31,
32,
and
33.
This file must work with an unmodified
usmalltalk
interpreter.
Therefore, if for example you use results from Exercises
4,
29a,
or any other problem (e.g., the class method from:
on
the Array
class), you will need to
duplicate those results in bignum.smt
as well as in
basis.smt
above.
bigtests.smt
showing your solutions to Exercise T.
Submit code using submit105smallsolo and submit105smallpair.
Exemplary  Satisfactory  Must improve  

Structure  • The base used for natural numbers appears in exactly one place, and all code that depends on it consults that place. • Or, the base used for natural numbers appears in exactly one place, and code that depends on either consults that place or assumes that the base is some power of 10 • No matter how many bits are used to represent a machine integer, overflow is detected by using appropriate primitive methods, not by comparing against particular integers. • Code uses method dispatch instead of conditionals. • Mixed operations on different classes of numbers are implemented using double dispatch. • Or, mixed operations on different classes of numbers are implemented by arranging for the classes to share a common protocol. • Or, mixed operations on different classes of numbers are implemented by arranging for unconditional coercions. • Code deals with exceptional or unusual conditions by passing a suitable • Code achieves new functionality by reusing existing methods, e.g., by sending messages to • Or, code achieves new functionality by adding new methods to old classes to respond to an existing protocol. • An object's behavior is controlled by dispatching (or double dispatching) to an appropriate method of its class. 
• The base used for natural numbers appears in exactly one place, but code that depends on it knows what it is, and that code will break if the base is changed in any way. • Overflow is detected only by assuming the number of bits used to represent a machine integer, but the number of bits is explicit in the code. • Code contains one avoidable conditional. • Mixed operations on different classes of integers involve explicit conditionals. • Code protects itself against exceptional or unusual conditions by using Booleans. • Code contains methods that appear to have been copied and modified. • An object's behavior is influenced by interrogating it to learn something about its class. 
• The base used for natural numbers appears in multiple places. • Overflow is detected only by assuming the number of bits used to represent a machine integer, and the number of bits is implicit in the value of some frightening decimal literal. • Code contains more than one avoidable conditional. • Mixed operations on different classes of integers are implemented by interrogating objects about their classes. • Code copies methods instead of arranging to invoke the originals. • Code contains case analysis or a conditional that depends on the class of an object. 
Clarity  • Course staff see no more code than is needed to solve the problem. • Course staff see how the structure of the code follows from the structure of the problem. 
• Course staff see somewhat more code than is needed to solve the problem. • Course staff can relate the structure of the code to the structure of the problem, but there are parts they don't understand. 
• Course staff roughly twice as much code as is needed to solve the problem. • Course staff cannot follow the code and relate its structure to the structure of the problem. 
Documentation  • Private methods are documented with contracts. • Or, private methods use exactly the contracts suggested in the bignums handout. 
• Private methods are neither documented nor consistent with the bignums handout. 

Correctness  • Code that works with collections works with any 
• Code that is supposed to works with all collections works only with some subclasses of collections. 