assignment 1 — respecting assertions and invariantscs6721/private/paragraph final 200611.pdf ·...

18
University of New South Wales COMP 6721 (In-)Formal Methods: The Lost Art 2020 Term 2 Assignment 1 — Respecting assertions and invariants Due 9:00pm Friday 3 July 2020 (end Week 5) 1 Typsetting a paragraph in L A T E X style L A T E X typesets paragraphs so that they fill the horizontal space exactly, both the left- and right-hand sides (as for this paragraph). It does that by putting just enough blank space between the words to get the right width overall. That’s the program we will be writing in this assignment, more or less — but our program will only approximate L A T E X’s approach, because we will use a fixed-width font. As you will see, however — the assignment is not about how to typeset. It’s about how to write programs using assertions and invariants. Typesetting just happens to be an intricate-enough problem to make it interesting. Figure 1(a) shows how some text in a fixed-width font is made into a para- graph exactly 37 columns wide (except for the last line). 1 The “greedy” type- setting (at left) fills each line as much as possible before moving on to the next. Then enough extra space is added to every line except the last, between words, so that it finishes exactly at the right-hand margin. In general, greedy algorithms take decisions based on the “current” situation, and do not look ahead to see whether a dierent decision now might lead to a better outcome later on. In Fig. 1(b) the same text is typeset (at right), but now using a “clever” algo- rithm (dynamic programming) that optimises (minimises) the average amount of extra space that must be added between words in each line. In this assignment you will program a clever paragraph-maker in Python3. Be sure however to read Sec. 8 below carefully — it describes how the assignment will be marked (including penalties, if applicable, for late submission). 1 The text is the abstract of Donald E. Knuth and Michael F. Plass. Breaking Paragraphs into Lines. Software Practice and Experience 11(1119–84), 1981. which describes the way that T E X and (thus) L A T E X make paragraphs. 1

Upload: others

Post on 15-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

University of New South WalesCOMP 6721

(In-)Formal Methods: The Lost Art2020 Term 2

Assignment 1 —

Respecting assertions and invariants

Due 9:00pm Friday 3 July 2020 (end Week 5)

1 Typsetting a paragraph in LATEX style

LATEX typesets paragraphs so that they fill the horizontal space exactly, both theleft- and right-hand sides (as for this paragraph). It does that by putting justenough blank space between the words to get the right width overall. That’s theprogram we will be writing in this assignment, more or less — but our programwill only approximate LATEX’s approach, because we will use a fixed-width font.

As you will see, however — the assignment is not about how to typeset.It’s about how to write programs using assertions and invariants. Typesettingjust happens to be an intricate-enough problem to make it interesting.

Figure 1(a) shows how some text in a fixed-width font is made into a para-graph exactly 37 columns wide (except for the last line). 1 The “greedy” type-setting (at left) fills each line as much as possible before moving on to the next.Then enough extra space is added to every line except the last, between words,so that it finishes exactly at the right-hand margin.

In general, greedy algorithms take decisions based on the “current” situation,and do not look ahead to see whether a di↵erent decision now might lead to abetter outcome later on.

In Fig. 1(b) the same text is typeset (at right), but now using a “clever” algo-rithm (dynamic programming) that optimises (minimises) the average amountof extra space that must be added between words in each line.

In this assignment you will program a clever paragraph-maker in Python3.

Be sure however to read Sec. 8 below carefully — it describes howthe assignment will be marked (including penalties, if applicable, forlate submission).

1The text is the abstract of

Donald E. Knuth and Michael F. Plass. Breaking Paragraphs into Lines.

Software Practice and Experience 11(1119–84), 1981.

which describes the way that TEX and (thus) LATEX make paragraphs.

1

Page 2: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

How LATEX sets paragraphs

abstract from Donald E. Knuth and Michael F. Plass.

Breaking Paragraphs into Lines.

This paper discusses a new approachto the problem of dividing the textof a paragraph into lines ofapproximately equal length. Insteadof simply making decisions one lineat a time, the method considers theparagraph as a whole, so that thefinal appearance of a given linemight be influenced by the text onsucceeding lines. A system based onthree simple primitive conceptscalled boxes, glue, and penaltiesprovides the ability to dealsatisfactorily with a wide variety oftypesetting problems in a unifiedframework, using a single algorithmthat determines optimum breakpoints.The algorithm avoids backtracking bya judicious use of the techniques ofdynamic programming. Extensivecomputational experience confirmsthat the approach is both efficientand effective in producinghigh-quality output. The paperconcludes with a brief history ofline-breaking methods, and anappendix presents a simplifiedalgorithm that requires comparativelyfew resources.

1234567890123456789012345678901234567

(a) Greedy strategy: cost 14/3 = 4.67

This paper discusses a new approachto the problem of dividing thetext of a paragraph into lines ofapproximately equal length. Insteadof simply making decisions oneline at a time, the method considersthe paragraph as a whole, sothat the final appearance of agiven line might be influencedby the text on succeeding lines.A system based on three simpleprimitive concepts called boxes,glue, and penalties provides theability to deal satisfactorily with awide variety of typesetting problemsin a unified framework, using asingle algorithm that determinesoptimum breakpoints. The algorithmavoids backtracking by a judicioususe of the techniques of dynamicprogramming. Extensive computationalexperience confirms that the approachis both efficient and effectivein producing high-quality output.The paper concludes with a briefhistory of line-breaking methods, andan appendix presents a simplifiedalgorithm that requires comparativelyfew resources.

1234567890123456789012345678901234567

(b) Clever strategy: cost 14/5 = 2.80

The paragraph above is set in a column of 37 spaces in both cases: but a greedystrategy is used on the left and a clever one on the right.

The greedy paragraph took text into the second line “because it could”, butpaid for it later with extra white space in the last ten lines.

The cost of a typesetting is the maximum over all lines (except the last) of theaverage white space between adjacent words on each line separately.

Figure 1: Greedy vs. clever typesetting strategy

Page 3: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

2 Assertions and invariants

Although this assignment asks you to write the clever, dynamic-programmingalgorithm, and test it, it is not about that algorithm itself, nor is itabout dynamic programming. (You will learn more about those things inother courses.)

This assignment is about how to use assertions, invariants and static rea-soning to write complicated programs and get them right. The dynamic pro-gramming here is just an example of where it really helps. Thus there won’t bea “test suite” to check that your program is working correctly, and it won’t berun through an “auto marker”. Instead, it’s your actual source code that willbe marked, as for an an essay, and your mark will depend on how well you havewritten it. (And that is normal for this course.)

Here therefore are some key points in the way the assignment is presented:

(a) Your code will be written in Python3. 2 (Not C, not Java, not anythingelse.) That’s partly to make it feasible to mark, but also because of (b).

(b) You are given (below) an overall program structure to start with (written inPython3), and it will have –as comments– some assertions written already.

(c) Your actual programming job will be to “Fill in the code between the as-sertions.” That’s where you will gain your marks.

(d) Because of (b,c), your program will use the variable names chosen for youalready. If you need extra variables, of course you can name them yourself.(There should be very few.) The principal ones must remain as given.

(e) The code templates that contain questions (Figs. 3,4,5) you should type inyourself — really. They won’t be in a downloadable file. (You don’t have totype in the comments; but you might consider doing some of them anyway.)

That’s not only to ease the version-control problem between any extra filesand this .tex file, but as well for the more important reason that, becausetyping takes time, you will be forced to think about what you are doing.This whole assignment is about how to think e↵ectively as you program;and typing (rather than swiping ,) is one of the tricks.

2Python2 is acceptable; but it is no longer supported. The code here is in Python3. Do not

use anything other than Python.

3

Page 4: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

3 The general strategy of this algorithm

The program will be given a line-width M and a file of blank-separated words,over as many lines as you like. Line-breaks and multiple spaces are treated asblanks. The program will place the words in a sequence wds of character strings.

The output of the program will be a paragraph, built from wds, in which alllines (except possibly the last) are exactly M characters long, achieved by addingblanks between words where necessary. The program prints the paragraph.

And that paragraph will be of optimal (minimum) cost, where the cost of asingle line is the average number of blanks that are needed between each adjacentpair of words and the overall cost is the largest cost of any single line (excludingthe last line). For example, if the words on a line have lengths 4,5,9,1,3,8 (as inthe first line of Fig. 1) and the line-width M is 37, then 37�(4+5+9+1+3+8) = 7blanks must be distributed over the 5 inter-word spaces. Since we can use onlywhole blanks, we might mix 1- and 2-blank spaces in the pattern 1,2,1,2,1. Orperhaps 2,2,1,1,1; or 1,1,1,2,2. But gaps-lengths must not di↵er by morethan one within a single line. Thus 1,1,1,1,3 is not allowed.

The program will be in three principal parts:

Part (1) Starting from the end of the list of words, calculate the minimumpossible cost of making a paragraph for successively longer su�xes.(That’s the dynamic-programming part.)

Part (2) Use the the results of (1), starting from the beginning of the list ofwords, to split the words into lines such that each line fits within theline-width and the cost of that line is no worse than the minimumachievable for the whole paragraph.

Part (3) Use the results of (2) to print out the actual paragraph, complete withthe extra blanks necessary so that each line takes exactly M characters.

And there is a Part (0) part that is just concerned with reading the input.

4 Part (0) — Read the input.

The Python3 code of Fig. 2 reads the list of words from standard input, andthe line width from the command line. Enter it and test it. (You may copy-and-paste this part from the pdf; but you will need to tidy it up.)

Test your code by printing out what (you think) you have read in and havetaken from the command line: wds and wls and M.

You do not have to hand in this part on its own; but you do have to hand itin as part of the larger program, as explained in later parts of the assignment.

4

Page 5: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

from sys import argv,stdin

### Paragraph.py --- Synopsis## % python3 Paragraph.py WIDTH < WORDS## The column WIDTH is given as the only argument; WORDS come from standard# input, over as many lines as it takes. Multiple blanks and linefeeds# are treated as a single blank, and wholly blank lines are ignored.# EOF from the terminal is ^D (as usual).## The program prints WORDS in lines of exactly WIDTH characters, except# possibly the last. It minimises the largest average-per-line white space# between words in order to achieve right justification, that is to make# a straight right margin.## Although the average white space needed in a line might not be a# whole number of blanks, the program simulates that in a fixed-width# font by using a mixture of "just more than" and "just less than"# inter-word gaps.

### Part (0) --- Collect input.

# Get the line-width from command line.assert len(argv)==2, "\n%s: Exactly one argument expected.\n"%argv[0]+ \

"\nusage: python3 %s WIDTH < WORDS"%argv[0]M= int(argv[1])

# Get the words and their lengths from standard input into wds and wls.# Invariant: wds contains all the words read so far.# and wds contains their lengths.wds,wls= [],[]for line in stdin.readlines():

for w in line.split(): wds.append(w); wls.append(len(w))

# Check that the input satisfies the program’s precondition.assert wds!=[], "The list of words may not be empty."for w in wls: assert w<=M, "Each word must be able to fit on a line alone."

N= len(wds) # N words in total.

print("wds is",wds)print("wls is",wls)print("M is",M)print("There are",N,"words in total.")

### Do not hand this file in on its own; DO test it. ###

You may copy-and-paste the code here directly from the pdf — but you will have to

fix some white-space copying errors afterwards. Only a few.

Figure 2: Part (0) — Read the program’s input data

Page 6: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

% If wds[l:h] is a single word, then the line made% from it has either 1 gap (at the end of the word, say)% or no gap at all (if the word is of length M exactly).% In both those cases, the Cost should be the number% of blanks needed to make that single word take up% M spaces.

Ignore the "exactly fits" case in your coding here; that is, act as if an exactly fitting word does have 1 gap.By good fortune, the function will work anyway.

Once the assignment's deadline has passed, I will changethis function body. ("Good fortune" is not good enough for us :-)

Page 7: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

5 Part (1) — Calculate the minimum costs

The dynamic-programming aspect of this algorithm is that in order to minimise(optimise) the cost of the whole paragraph, we actually find the optimum costsfor all its su�xes along the way. Thus we introduce a sequence of real numberscs[0:N] (for costs), and the postcondition of this part will be that

For all n in [:N] we have # [:N] is Python for 0,1,...,.N-1 .

cs[n] == "the optimal achievable cost of typsetttingsuffix wds[n:] in line-width M."

That means that in particular cs[0] will be the optimal achievable cost for thewhole paragraph.

A code skeleton for this is shown in Fig. 3. The blue text is the assertions(including invariants) that you must respect when you write your code. Typingthem in as well will help you to make sure that you have. The red text identifiesthe places where you must supply code.

For Part (1), fill in the missing portions AAA · · · III of the skeleton given inFig. 3; the number after the three letters is the marks available. Type it in; andtest it together with the code you used for Part (0) by including the print csat the end to check those values against your test inputs.

/ 1Put your code for Parts (0,1) into a single (text) file Para1-yourZID.py,where yourZID is your student number. Include your name and student num-ber as a comment at the beginning of the file. That is the first file that youwill submit for marking — see Sec. 8.3. It will be marked based on how wellyour added red code in this part (that replaced AAA · · · III) respects the blueassertions in Fig. 3. If it does, then it should (also) produce the correct outputas shown by the print cs — but do not hand your tests in.

Do make sure your program “so far” compiles and runs, however, since Imight test it myself if I am unsure the assertions are being respected. And–obviously– do not change the assertions or change any variable names.

6 Part (2) — Calculate the paragraph lines

In Part (2) the results of Part (1), the cs sequence, are used to split the originallist wds of words into the separate lines that will achieve the optimal-cost layoutfor the paragraph overall.

This time we start from the front of wls, making the lines of the eventualparagraph one by one. For each of those lines we take as many words as are nec-essary to reach a “rest of paragraph” point where the overall cost is su�cientlylow. (This is a greedy algorithm, not a dynamic-programming algorithm.)

7

Page 8: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

Fill in the missing portions JJJ · · · NNN of the skeleton given in Fig. 4. Typeit in; and test it in combination with your earlier code printing cs and wlssand checking that against your test input.

/ 2Put your code for Parts (0,1,2) into a (text) file Para2-yourZID.py by itself.Include your name and student number as a comment at the beginning of thefile. You will submit that for marking — see Sec. 8.3.

It will be marked based on how well your added red code in Part (3) respectsthe blue assertions in Fig. 4. Do not hand your tests in.

Note in particular that Answer MMM asks not for code, but for an answerin prose: for this one, state why the statement i= i+1 maintains the invariantInv3 and add it as a comment to your program.

7 Part (3) — Print the paragraph

The final part of this program prints out the formatted paragraph, filling everyline (expect the last) so that it takes exactly M spaces. Given that the lengthsof the paragraph’s lines are now available in wlss, that seems an easy job:something like

for ws in wlss:l= len(ws)ls,wds= wds[:l],wds[l:]print ls # Blanks needed here.

would do... sort of. Except for the “blanks needed here” issue. The main jobthis part is the MakeLine function, which inserts those blanks. In that functionthere are, as usual, things to fill in: from OOO to RRR.

An important note: you are not required to fill in the needed blanks in anyparticular order: you can put all longer ones at the front, or at the back. Itdoesn’t matter. But you must not have gaps whose lengths di↵er bymore than one within a single line. Thus for example to get 7 blanks into 3gaps you can have 2,2,3 or 2,3,2 or 3,2,2 — but you cannot have 1,2,4 or similar.

The second half of this part also has a loop, but it’s so trivial –print everyline– that it probably doesn’t need assertions/invariants so much. And so, inthis part only, we have used Meaningful Variable Names.

MVN is indeed good style, but only up to a point. It makes the programmore verbose — and if you have to do calculations with the assertions (especiallyif they’re on paper), then that can become a bit tedious and, indeed, error prone.

8

Page 9: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

The "forall" should not be there.

Page 10: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981
Page 11: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

### Part (3b) --- # Construct the lines of the paragraph,# one-by-one, and print them out.

maxBlanks,lineCount= 0,0 # For reporting only.for ws in wlss:

ls,wds= wds[:len(ws)],wds[len(ws):] # Get the actual words.gapsHere= len(ws)-1if wds==[]: line= MakeLine(ls,gapsHere); print(line) # Last line.else:

blanksNeededHere= M-sum(ws)line= MakeLine(ls,blanksNeededHere)maxBlanks= max(maxBlanks, \

blanksNeededHere/gapsHere \if len(ws)>1 else blanksNeededHere)

print("%s %d/%d = %.2f." % ( \line, blanksNeededHere, \gapsHere,blanksNeededHere/gapsHere \if len(ws)>1 else blanksNeededHere)

)lineCount= lineCount+1

print(" "*M+"^")print(("==> 567890"+("1234567890"*floor(M/10)))[:M]+"|")print("==> The maximum (fractional) white space for column width" + \

" %3d was %.2f over %d lines." % \(M, maxBlanks,lineCount))

### Hand this in, together with the code from Figs. 2,3,4,5### as a single Para3-yourZID.py file.### It is your complete program.

You may copy-and-paste this.

Figure 6: Part (3)b — Make the lines and print them

Page 12: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

8 How this assignment will be marked

Do not spend any time in Secs. 9, 10 unless you have completed the main as-signment. You can get full marks for correct answers to AAA · · · RRR .

8.1 Testing — must be done; but who does it?

With luck, it will be you and not me: you are expected to test your program –ofcourse– but do not submit your test results. Still, if you would like for exampleto see for yourself how your program handles a fixed input over varying columnwidths, use something like

for ((M= 0; M <= maximumWidth; M++))do python3 Para3_z1234567.py $M < inputFile |

tail -1done

in zsh (the “zed shell”). Other shells can do similar.Each of the three .py files still should have in it the test print commands

that you used for testing it yourself. That way I can test it too — if I have to.(See http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD249.PDF on p7.)

8.2 What will get marks — and what won’t

AAA 3BBB 2CCC 1DDD 2EEE 5FFF 2GGG 4HHH 2III 1

JJJ 2KKK 2LLL 6MMM 4NNN 3

OOO 4PPP 2QQQ 2RRR 3

SSS· · · ⇠0ZZZ

P50

Here’s how to get good marks: If you were asked to fill-in AAA and BBB forthe program

# N>=0n,s= N,0while AAA: # INV: s==sum(as[n:])

BBB# s==sum(as)

and you answered

# Answer Prog1# N>=0n,s= N,0while n!=0: # INV: s==sum(as[n:])

n= n-1s= s+as[n]

# s==sum(as)

then you would get full marks. Your answer need not be coloured; but thereplacements n!=0 and n= n-1; s=s+as[n] you gave for AAA and BBB mustrespect the blue assertions that were there already.

12

Page 13: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

Here’s how to lose marks: If on the other hand your answer had been thisprogram (which does correctly calculate the sum)

# Answer Prog2# N>=0n,s= 0,0while n!=N: # INV: s==sum(as[n:])

s,n= s+as[n],n+1# s==sum(as)

then your marks would be considerably less than full. For even though Prog2satisfies the Hoare triple

{N>=0} Prog2 {s==sum(as)}

it does not respect the invariant s==sum(as[n:]) that was supplied.In “real life” you could of course change the invariant to something that

Answer 2 does respect, 4 and indeed in the long run that’s the point: you mustfind your own invariants. But in this assignment you are not allowed tochange the assertions — for learning to respect assertions (ultimately, yourown or your team mates’) is what this assignment is practising.

Thus the program fragments you fill in must respect the blue assertions thatare supplied: if they don’t, you risk losing marks even if your program is correct.

Each three-letter question identifier from AAA up to RRR comes with a smallnumber indicating how many marks it is worth. Their total is 50, and yourmark will contribute 30% to the overall course mark.

The question identifiers from SSS to ZZZ have no mark indicators becausethey are optional, just for those who are interested in looking further. Ex-ceptionally good work there might, however, gain a few marks extra in theassignment overall. But you can get 100% for fully correct work on AAA up toRRR alone.

8.3 Frequently Asked Questions

• What happens if I change the assertions? If you think an assertion iswrong, contact the lecturer (me) privately. If you change the assertion,you risk losing marks.

• What happens if my program works but it does not respect the assertions?If your program does not respect the assertions, then you cannot be sureit is working. At best, your tests have established only that it probablyworks. Any of your answers that don’t respect the assertions that surroundthem risk receiving few marks.

• What happens if my program respects the assertions, but doesn’t work?// Can’t happen. If your program respects the assertions,

then it must work. 5

4What would it be?

13

Page 14: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

• What exactly must I hand in, by when, and how?

/ (1,2,3)You must hand in the files asked for in the marginal indications 1,2,3, that isthree text-files Para1-yourZID.py, Para2-yourZID.py and Para3-yourZID.py.Zip them together into a single zip-file COMP6721-Ass1-yourZID.zip and sub-mit it following the instructions that will be given nearer the time. (It willprobably be via Moodle.)

Be careful to use exactly the file names given, and to make sure that yourzID and name occurs within each file as a Python comment) as well as havingthe zID within the file names exactly as shown — please. Random file namescause confusion, lead to extra work for the marker and can cause errors.

The assignment submission is due at 9:00pm on Friday 3 July 2020, theend of Week 5. Late submissions will be mark-capped at 85% up to one day;for two days at 75%, three days at 65% and four days at 50%. After four dayslate, submissions are no longer accepted.

9 Extras

There are no marks “o�cially” available for this section (or the next); thusdon’t attempt it unless you have completed the assignment proper.Still, there could be a small benefit available if some of this is really well done.

9.1 Greedy typesetting

If you’d like to compare your program with the (much simpler) greedy approach,you can change just your answer to Part (2) so that it fills lines greedily, ignoringPart (1) — which you can leave just sitting there, since it does no harm.

What change would you make? SSS

9.2 Even spacing

In MakeLines the blanks are distributed over the line to make it exactly Mcharacters long. And some white-spaces will be longer than others. How wouldyou make all the longer white-spaces come before the shorter ones? TTT Or alllonger ones come after the shorter ones? UUU

What about making the distribution of longer and shorter white-spaces bemore or less even, as in Fig. 1? VVV

5. . . unless of course the assertions themselves are wrong.

I have tried to make sure they are right.

14

Page 15: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

9.3 Number of lines used

Minimising the white space has a disadvantage, that in some case more linesmight be used overall than for the greedy strategy. (Look ahead to Sec. 10.)That is, a greedy strategy might not be pretty, but it probably does save paper.

Can you see why our current code doesn’t necessarily use as few lines aspossible, even though it minimises average white space? WWW

10 Epilogue: a very long paragraph

As for Sec. 9, there are no marks “o�cially” available for this section; again,don’t attempt it unless you have completed the assignment proper.Still, there could be a small benefit available if some if this is really well done.

The text in the figures below comes from Moby Dick; 6 I found it by lookingfor the longest sentence in English that (1) would still fit on one page and (2)came from a book I had heard of. You might like to try others. 7 In Fig. 7 itis typeset with the greedy strategy, and a large white space occurs (13 blanks)near the end; but at least the fewest number of lines (102) is used. Can youexplain rigorously why the greedy strategy is guaranteed to use the fewest lines?(Of course it’s “obvious” — but can you be rigorous about it?) XXX

In Fig. 8 the code of this assignment is used, in particular Part (2). Ittakes more lines, but the average white space is drastically reduced: the cost isreduced from 13 to 3.50.

In Fig. 9 the code of this assignment is again used, except that Part (2) ismodified to take the longest line possble consistent with minimising the whitespace (which, being minimum, must be the same as for Fig. 8). However it takesfewer lines than Fig. 8 — though still more than Fig. 7. (See Sec. 9.3.)

Can you modify our Part (2) to achieve the e↵ect of Fig. 9? Do it byproposing a revised Inv4 first — of course. YYY What’s your new loop? ZZZ

Carroll Morgan8 June 2020

6Moby-Dick; Or, The Whale. Herman Melville. Harper & Brothers, 1851.

7https://thejohnfox.com/long-sentences/

15

Page 16: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

Though in many naturalobjects, whiteness refininglyenhances beauty, as ifimparting some special virtueof its own, as in marbles,japonicas, and pearls; andthough various nations havein some way recognised acertain royal preeminence in

- this hue; even the barbaric,grand old kings of Peguplacing the title ’Lord ofthe White Elephants’ aboveall their other magniloquentascriptions of dominion; andthe modern kings of Siamunfurling the same snow-whitequadruped in the royalstandard; and the Hanoverian

- flag bearing the one figureof a snow-white charger; andthe great Austrian Empire,Caesarian, heir tooverlording Rome, having forthe imperial colour the sameimperial hue; and though thispre-eminence in it applies tothe human race itself, givingthe white man ideal

- mastership over every duskytribe; and though, besides,all this, whiteness has beeneven made significant ofgladness, for among theRomans a white stone marked ajoyful day; and though inother mortal sympathies andsymbolizings this same hue ismade the emblem of many

- touching, noble things ---the innocence of brides, thebenignity of age; thoughamong the Red Men of Americathe giving of the white beltof wampum was the deepestpledge of honour; though inmany climes, whitenesstypifies the majesty ofJustice in the ermine of the

- Judge, and contributes to thedaily state of kings and

queens drawn by milk-whitesteeds; though even in thehigher mysteries of the mostaugust religions it has beenmade the symbol of the divinespotlessness and power; bythe Persian fire worshippers,the white forked flame beingheld the holiest on the -altar; and in the Greekmythologies, Great Jovehimself being made incarnatein a snow-white bull; andthough to the noble Iroquois,the midwinter sacrifice ofthe sacred White Dog was byfar the holiest festival oftheir theology, thatspotless, faithful creature -being held the purest envoythey could send to the GreatSpirit with the annualtidings of their ownfidelity; and though directlyfrom the Latin word forwhite, all Christian priestsderive the name of one partof their sacred vesture, thealb or tunic, worn beneath -the cassock; and though amongthe holy pomps of the Romishfaith, white is speciallyemployed in the celebrationof the Passion of our Lord;though in the Vision of St.John, white robes are givento the redeemed, and thefour-and-twenty elders standclothed in white before the -great-white throne, and theHoly One that sitteth therewhite like wool; yet for allthese accumulatedassociations, with whateveris sweet, and honourable, andsublime, there yet lurks anelusive something in theinnermost idea of this hue,which strikes more of panic -to the soul than that rednesswhich affrights in blood.

Line-width 29, cost 13, lines used 102.

Figure 7: Greedy strategy

Page 17: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

Though in many naturalobjects, whiteness refininglyenhances beauty, as ifimparting some special virtueof its own, as in marbles,japonicas, and pearls; andthough various nations havein some way recogniseda certain royal preeminence

- in this hue; even thebarbaric, grand old kings ofPegu placing the title ’Lordof the White Elephants’ aboveall their other magniloquentascriptions of dominion;and the modern kings ofSiam unfurling the samesnow-white quadruped in theroyal standard; and the

- Hanoverian flag bearing theone figure of a snow-whitecharger; and the greatAustrian Empire, Caesarian,heir to overlording Rome,having for the imperialcolour the same imperial hue;and though this pre-eminencein it applies to thehuman race itself, giving the

- white man ideal mastershipover every dusky tribe;and though, besides, allthis, whiteness has beeneven made significant ofgladness, for among theRomans a white stone markeda joyful day; and thoughin other mortal sympathiesand symbolizings this same

- hue is made the emblemof many touching, noblethings --- the innocenceof brides, the benignityof age; though among theRed Men of America thegiving of the white beltof wampum was the deepestpledge of honour; thoughin many climes, whiteness

- typifies the majesty ofJustice in the ermine ofthe Judge, and contributesto the daily state of

kings and queens drawnby milk-white steeds; thougheven in the higher mysteriesof the most august religionsit has been made the symbolof the divine spotlessnessand power; by the Persian -fire worshippers, the whiteforked flame being heldthe holiest on the altar;and in the Greek mythologies,Great Jove himself being madeincarnate in a snow-whitebull; and though to thenoble Iroquois, the midwintersacrifice of the sacredWhite Dog was by far -the holiest festival of theirtheology, that spotless,faithful creature being heldthe purest envoy they couldsend to the Great Spiritwith the annual tidingsof their own fidelity; andthough directly from theLatin word for white, allChristian priests derive the -name of one part of theirsacred vesture, the albor tunic, worn beneath thecassock; and though amongthe holy pomps of the Romishfaith, white is speciallyemployed in the celebrationof the Passion of our Lord;though in the Vision ofSt. John, white robes are -given to the redeemed, andthe four-and-twenty eldersstand clothed in white beforethe great-white throne, andthe Holy One that sitteththere white like wool; yetfor all these accumulatedassociations, with whateveris sweet, and honourable,and sublime, there yet lurks -an elusive something in theinnermost idea of this hue,which strikes more of panicto the soul than that rednesswhich affrights in blood.

Line-width 29, cost 3.50, lines used 105.

Figure 8: Simple clever strategy (ours)

Page 18: Assignment 1 — Respecting assertions and invariantscs6721/Private/Paragraph Final 200611.pdf · Assignment 1 — Respecting ... Software Practice and Experience 11(1119–84), 1981

Though in many naturalobjects, whiteness refininglyenhances beauty, as ifimparting some special virtueof its own, as in marbles,japonicas, and pearls; andthough various nations havein some way recognised acertain royal preeminence in

- this hue; even the barbaric,grand old kings of Peguplacing the title ’Lord ofthe White Elephants’ aboveall their other magniloquentascriptions of dominion; andthe modern kings of Siamunfurling the same snow-whitequadruped in the royalstandard; and the Hanoverian

- flag bearing the one figureof a snow-white charger;and the great AustrianEmpire, Caesarian, heir tooverlording Rome, having forthe imperial colour the sameimperial hue; and though thispre-eminence in it applies tothe human race itself,giving the white man ideal

- mastership over every duskytribe; and though, besides,all this, whiteness has beeneven made significant ofgladness, for among theRomans a white stone marked ajoyful day; and though inother mortal sympathies andsymbolizings this same hue ismade the emblem of many

- touching, noble things ---the innocence of brides, thebenignity of age; thoughamong the Red Men of Americathe giving of the white beltof wampum was the deepestpledge of honour; thoughin many climes, whitenesstypifies the majesty ofJustice in the ermine of the

- Judge, and contributes to thedaily state of kings andqueens drawn by milk-white

steeds; though even in thehigher mysteries of the mostaugust religions it has beenmade the symbol of the divinespotlessness and power; bythe Persian fire worshippers,the white forked flamebeing held the holiest on -the altar; and in theGreek mythologies, GreatJove himself being madeincarnate in a snow-whitebull; and though to thenoble Iroquois, the midwintersacrifice of the sacredWhite Dog was by far theholiest festival of theirtheology, that spotless, -faithful creature being heldthe purest envoy they couldsend to the Great Spiritwith the annual tidings oftheir own fidelity; andthough directly from theLatin word for white, allChristian priests derive thename of one part of theirsacred vesture, the alb or -tunic, worn beneath thecassock; and though among theholy pomps of the Romishfaith, white is speciallyemployed in the celebrationof the Passion of our Lord;though in the Vision ofSt. John, white robes aregiven to the redeemed, andthe four-and-twenty elders -stand clothed in white beforethe great-white throne, andthe Holy One that sitteththere white like wool; yetfor all these accumulatedassociations, with whateveris sweet, and honourable,and sublime, there yet lurksan elusive something in theinnermost idea of this hue, -which strikes more of panicto the soul than that rednesswhich affrights in blood.

Line-width 29, cost 3.50, lines used 103.

Figure 9: Even cleverer clever strategy