Get Thinking with Types Type level Programming in Haskell Sandy Maguire PDF ebook with Full Chapters Now
Get Thinking with Types Type level Programming in Haskell Sandy Maguire PDF ebook with Full Chapters Now
com
https://textbookfull.com/product/thinking-with-types-type-
level-programming-in-haskell-sandy-maguire/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/thinking-functionally-with-haskell-
richard-bird/
textboxfull.com
https://textbookfull.com/product/programming-in-haskell-2nd-edition-
graham-hutton/
textboxfull.com
https://textbookfull.com/product/programming-in-haskell-second-
edition-5th-printing-edition-hutton/
textboxfull.com
https://textbookfull.com/product/algebra-driven-design-elegant-
software-from-simple-building-blocks-version-1-1-2-sandy-maguire/
textboxfull.com
Thinking Low Level Writing High Level 2nd Edition Randall
Hyde
https://textbookfull.com/product/thinking-low-level-writing-high-
level-2nd-edition-randall-hyde/
textboxfull.com
https://textbookfull.com/product/haskell-the-ultimate-beginner-s-
guide-to-learn-haskell-programming-step-by-step-1st-edition-claudia-
alves/
textboxfull.com
https://textbookfull.com/product/the-haskell-road-to-logic-maths-and-
programming-kees-doets/
textboxfull.com
https://textbookfull.com/product/algorithm-design-with-haskell-
richard-s-bird/
textboxfull.com
Sandy
Sandy Maguire
Maguire
Copy
Copyrig
right
ht ©201
©2018,
8, Sand
Sandyy Magu
Maguire
ire
First
First Edition
Edition
When people say
“but most business logic bugs
aren’t type errors,”
I just want to show them
how to make bugs
into type errors.
MATT PARSONS
Contents
Preface ix
Acknowledgments xi
Introduction 1
I Fundamentals 5
1 The Algebra Behind Types 7
1.1 Isomorphisms and Cardinalities . . . . . . . . . . . . . . . 7
1.2 Sum, Product and Exponential Types . . . . . . . . . . . . 10
1.3 Example: Tic-Tac-Toe . . . . . . . . . . . . . . . . . . . . . 13
1.4 The Curry–Howard Isomorphism . . . . . . . . . . . . . . 15
1.5 Canonical Representations . . . . . . . . . . . . . . . . . . 16
2 Terms, Types and Kinds 19
2.1 The Kind System . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.1.1 The Kind of “Types” . . . . . . . . . . . . . . . . . . 20
2.1.2 Arrow Kinds . . . . . . . . . . . . . . . . . . . . . . . 20
2.1.3 Constraint Kinds . . . . . . . . . . . . . . . . . . . . 21
2.2 Data Kinds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3 Promotion of Built-In Types . . . . . . . . . . . . . . . . . 25
2.3.1 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.3.2 Natural Numbers . . . . . . . . . . . . . . . . . . . . 27
2.3.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3.4 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.4 Type-Level Functions . . . . . . . . . . . . . . . . . . . . . 30
3 Variance 35
v
vi CONTENTS
II Lifting Restrictions 41
4 Working with Types 43
4.1 Type Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.2 Type Applications . . . . . . . . . . . . . . . . . . . . . . . . 45
4.3 Ambiguous Types and Non-Injectivity . . . . . . . . . . . 47
5 Constraints and GADTs 51
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.2 GADTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.3 Heterogeneous Lists . . . . . . . . . . . . . . . . . . . . . . 55
6 Rank-N Types 61
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
6.2 Ranks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
6.3 The Nitty Gritty Details . . . . . . . . . . . . . . . . . . . . . 65
6.4 The Continuation Monad . . . . . . . . . . . . . . . . . . . 66
7 Existential Types 71
7.1 Existential Types and Eliminators . . . . . . . . . . . . . . 71
7.1.1 Dynamic Types . . . . . . . . . . . . . . . . . . . . . 74
7.1.2 Generalized Constraint Kinded Existentials . . . . 76
7.2 Scoping Information with Existentials . . . . . . . . . . . 79
8 Roles 85
8.1 Coercions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
8.2 Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
IV Appendices 203
Glossary 205
Solutions 211
Bibliography 233
About the Author 235
viii CONTENTS
Preface
Thinking with Types started, as so many of my projects do,
accidentally. I was unemployed, bored, and starting to get tired of
answering the same questions over and over again in Haskell
chat-rooms. And so I started a quick document, jotting down a
bunch of type-level programming topics I thought it’d be fun to
write blog posts about.
This document rather quickly turned into an outline of what those
blog posts might look like, but as I was about to tease it apart into
separate files I stopped myself. Why not turn it into a book instead?
I approached some friends to see if anyone was interested in
writing it with me. A few nibbles, but nobody had time they wanted
to dedicate to such a thing. My excitement subsequently burned out,
and the idea lay dormant on the back-burner for a few months.
But I was still unemployed, and I was still bored, and I found
myself slowly fleshing out chapters regardless. My enthusiasm for
writing a book had died down, but I still felt the urge to write. A
friend caught me writing one day, and dared me to publish what I
had. I acquiesced.
And so on July 8th, 2018, I posted a 37 page document to reddit,
gauging if there was any interest from the community in such a book.
To my continual surprise, there was. The response was about 100x
bigger than I was expecting. Kind words and letters of support rolled
in, many of whom promised to pay me in order to continue writing
it.
That was enough for me. I put together a Patreon, started selling
early access to the book, and was o to the races. The promise was
to publish weekly updates, which—combined with not wanting to
commit fraud—kept me extremely motivated to get this book
finished. It’s a powerful technique to stay focused, and I’d strongly
ix
x CONTENTS
xi
xii CONTENTS
Introduction
Type-level programming is an uncommon calling. While most
programmers are concerned with getting more of their code to
compile, we type-level programmers are trying our best to prevent
code from compiling.
Strictly speaking, the job of types is twinfold—they prevent
(wrong) things from compiling, and in doing so, they help guide us
towards more elegant solutions. For example, if there are ten
solutions to a problem, and nine of them be poorly-typed, then we
need not look very hard for the right answer.
But make no mistake—this book is primarily about reducing the
circumstances under which a program compiles. If you’re a beginner
Haskell programmer who feels like GHC argues with you too often,
who often finds type errors inscrutable, then this book is probably
not for you. Not yet.
So whom is this book for? The target audience I’ve been trying to
write for are intermediate-to-proficient with the language. They’re
capable of solving real problems in Haskell, and doing it without too
much hassle. They need not have strong opinions on ExceptT vs
throwing exceptions in IO , nor do they need to know how to inspect
generated Core to find performance bottlenecks.
But the target reader should have a healthy sense of unease
about the programs they write. They should look at their comments
saying “don’t call this function with n = 5 because it will crash,”
and wonder if there’s some way to teach the compiler about that.
The reader should nervously eyeball their calls to error that they’re
convinced can’t possibly happen, but are required to make the
type-checker happy.
In short, the reader should be looking for opportunities to make
less code compile. This is not out of a sense of masochism, anarchy,
1
2 CONTENTS
Fundamentals
5
Chapter 1
1.1
1.1 Is
Isom
omor
orph
phis
isms
ms and
and Ca
Card
rdin
inal
alit
itie
iess
One of functio
functional
nal program
programmin ming’s
g’s killer
killer featur
features
es is pattern
pattern matchin
matching,g,
as made possible by algebraic data types . But this this term
term isn’t
isn’t just
just a
catchy title for things that we can pattern match on. As their name
sugge
uggessts,
ts, ther
theree is in fact
fact an algebra behind algebraic data types.
Being comfortable understanding and manipulating this algebra
is a mighty superpower—it allows us to analyze types, find more
convenient forms for them, and determine which operations (eg.
type
typecla
class
sses
es)) are
are poss
possibl
iblee to imple
impleme
ment nt..
To start, we can associate each type with its cardinality—the
numb
number er of inha
inhabit
bitan
ants
ts it has,
has, ignori
ignoring ng bottom
bottoms.s. Cons
Consid
ider
er the
followi
following
ng simple
simple type definiti
definitionsons::
data Void
data () = ()
7
8 CHAPTER
CHAPTER 1. THE ALGEBRA
ALGEBRA BEHIND TYPES
|Void| = 0
|()| = 1
|Bool| = 2
Any two types that have the same cardinality will always be
isomorphic
isomorphic to one another. An isomorphism between types s and t is
defin
de fined
ed as a pair
pair of fun
functio
ction
ns to and from:
to :: s -> t
from :: t -> s
such that composing either after the other gets you back where
you started. In other words, such that:
to . from = id
from . to = id
By the
the argu
argume
ment nt abov
above,
e, we shou
should
ld expe
expect
ct Spin to be isom
isomor
orph
phic
ic to
Bool. Ind
Indee
eedd it is:
is:
1.2
1.2 Sum,
Sum, Pr
Prod
oduc
uctt and
and Ex
Exp
ponen
onenti
tial
al Type
Typess
In the language of cardinalities, sum types correspond to addition.
The
The cano
canoni
nica
call exam
examplplee of thes
thesee is Eith er a b, which is either an a or a
Either
b. As a resul
result,
t, the
the cardi
cardina
nalit
lityy (reme
(rememb mber,
er, the
the numb
number
er of inha
inhabit
bitan
ants
ts))
of Eith er a b is the
Either the card
cardininal
alit
ityy of a plus
plus the
the card
cardin
inal
alit
ityy of b .
As you might expect, this is why such things are called sum types.
The intu
intuiti
ition
on behin
behind d addi
adding
ng gene
genera
raliz
lizes
es to any
any data
dataty
type
pe with
with
multiple constructors—the cardinality of a type is always the sum
of the
the card
cardin
inal
aliti
ities
es of its
its const
constru
ructo
ctors
rs..
data Deal a b
= This a
| That b
| TheO
TheOther
ther Bool
Maybe a| = 1 + |a|
|Maybe
Dual to sum types are the so-called product types. Again, we will
loo
look at thethe canonica ical example first—tht—thee pair type
type (a,
(a, b).
Analogously, the
t he cardinality of a product type is the product of their
cardinalities.
|(a,
(a, b)| = |a| × |b|
prodUnitFrom :: (a, ()
())
) -> a
prodUnitFrom (a, ()
())
) = a
The function absurd at 1 has the type Voi Voidd -> a. It’s
It’s a sort
sort of
of
blu saying “if you give me a Void I can can give
give you
you anyt
anythi
hing
ng you
you want
want.”.”
Despite this being a lie, because there are no Voids to be had in the
first
first plac
place,
e, we can’
can’tt disp
dispro
rove
ve it.
it.
Func
Functio
tion
n type
typess also
also have
have an encod encodining
g as stat
stateme
ementntss abou
aboutt
cardina
cardinality
lity—th
—they ey correspo
correspondnd to exponentia
exponentializa lization
tion.. To give an
example, there are exactly four (2 ) inhabitants of the type Boo
2
Bool
l ->
Bool. These functions are id, not, const True and const
const True False. Try as
const False
hard
hard as you
you can,
can, but
but you
you won’
won’tt findfind anyany othe
otherr pure
pure func
functi
tion
onss
between Bools!
More generally, the type a -> - > b has cardinality |b||a| . While
While this
this
migh
mightt be surp
surpri
risi
sing
ng at first
first—i
—itt alwa
alwaysys seem
seemss back
backwaward
rdss to me—t
me—the he
argument is straightforward. For every value of a in the domain, we
need to give back a b. But we can can cho
chose any valvalue of b for
for ever
everyy valu
valuee
of a —res
—resul
ulti
ting
ng in the
the follo
followin
wingg equa
equalitlity.
y.
Exerci
Exercise
se 1.2-i
1.2-i
Determine the cardinality of Eit
Eithe
her
r Bool
Bool (Bool
(Bool,
, Maybe
Maybe Bool
Bool)
) ->
Bool.
The
The inqu
inquisisiti
itive
ve read
reader
er might
might wonde
wonderr wheth
whetherer subtr
subtrac
actio
tion,
n,
division and other mathematical operations have meaning when
applied
applied to types.
types. Indeed
Indeed they do, but such things
things are hard,
hard, if not
impossible,
impossible, to express in Haskell. Subtraction
Subtraction corresponds
corresponds to types
with particular values removed, while division of a type makes some
of its values equal (in the sense of being defined equally—rather
than
than havi
having
ng an Eq inst
instan
ance
ce which
which equa
equatetess them.
them.))
In fact
fact,, even
even thethe noti
notion
on of die
diere
rent
ntia
iati
tion
on in calc
calcul
ulus
us has
has
mean
meaniningg in the domain
domain of types
types.. Thou
Thoughgh we wiwill
ll not discus
discusss it
furt
furthe
her,
r, the
the inte
interes
reste
ted
d read
reader
er is enco
encoururag
aged
ed to refer
refer to Cono
Conorr
McBride’s paper “The Derivative of a Regular Type is its Type of
One-Hole
One-Hole Contexts.”[8
Contexts.”[8]. ].
1.3. EXAMPLE: TIC-TAC-TOE 13
1.3
1.3 Ex
Exam
ampl
ple:
e: Tic-T
Tic-Tac-
ac-To
Toee
I said earlier that being able to manipulate the algebra behind types
is a migh
mighty ty supe
superp
rpow
ower
er.. Le
Let’
t’ss prov
provee it.
it.
Imagine
Imagine we wanted to write a game of tic-tac-toe.
tic-tac-toe. The standard
standard
tic-tac-toeboardhasninespaces,whichwecouldnaivelyimplement
like
like this
this::
On the 9th of October the Division marched out of the rest area to
take over the left sector of the 4th Corps front, held by the New
Zealand Division. After the breaking of the Hindenburg Line the
pursuit had been carried on by the Third Army with such vigour that
the enemy had been unable to make any prolonged stand, and the
front was now at least a dozen miles east of the Couillet Valley.
Throughout these operations the Divisional Artillery had assisted the
New Zealanders. Evidence of the haste with which the enemy had
retired was furnished on the second day’s march, when the
battalions passed through Lesdain, as most of the houses here had
been left standing, though interiors had been looted. While the
unending column of troops and transport moved eastward across the
ridges and valleys of the Somme country, past battered villages and
farmsteads, a stream of old men, women and children poured to the
west. These were the liberated civilians, freed at last from the
Prussian yoke. Though many of them seemed too stunned by ill-
treatment and the loss of all they had possessed to understand or
feel interest in what was passing around them, the greater number
greeted the British soldiers enthusiastically, and urged them to drive
the sales Boches out of their beloved France, whose soil had been
defiled. Ruined and bereft though they were, French pride and
passionate love of their country still burned brightly, and the chief
emotion was joy that the cochons prussiens were being hustled
ignominiously out of France. The second night was passed in Pélu
Wood and Château Briseux, with D.H.Q. at Esnes. Away to the north
Cambrai was burning so fiercely that the spire of one of its churches
stood out black against the background of flame. A semicircle of
fires in the further distance told of the destruction of villages and
homesteads, and the Lancashire men understood something of the
loathing and hatred that animated the homeless French and perhaps
saved them from the deadening effect of utter despair. The third
day’s march brought the Division to Fontaine-au-Pir and Beauvois,
where a halt was made while arrangements were completed for the
relief of the New Zealanders around Briastre, on the western bank of
the River Selle.
The River Selle winds through and about Solesmes, Briastre,
Neuvilly, Le Cateau and other villages east and south-east of
Cambrai, and here the Germans made one of the last desperate
attempts to check the British advance. The line of the Selle was
strategically one of the most important positions along the whole
front, the key position to Maubeuge, Mons, and above all
Valenciennes, the centre of the enemy’s main lines of
communications for all the northern area. The German troops were
ordered to fight to the death in the very strong defences prepared
east of the Selle, and their best troops were put into the line here,
including the 25th German Division, which claimed that it had fought
through the war with an unbroken record of victory. This famous,
hard-fighting division had been held in reserve that it might be
thrust in at the vital moment, fresh, fit and confident, to turn the
scale, and it was now brought into line against the 42nd Division. To
increase their confidence the German Higher Command issued the
following statement: “All past experiences, all prisoners’ statements,
prove that the British infantry does not push on as it meets with
resolute resistance. Only by offering a tough defence shall we
achieve an honourable peace.” General Solly-Flood issued this
statement to his troops with the comment: “The Division, as it has
always done, will overcome the toughest defence the already shaken
Hun can offer, and teach him once more what the Divisional Motto
means.”
The New Zealanders had already secured a precarious footing at
one or two points on the eastern bank of the Selle, opposite
Briastre, and on the night of October 12-13 their front was taken
over by the 125th Brigade, two companies of the 8th L.F. crossing
the river. The enemy made violent attempts to dislodge them, and,
by driving them back across the Selle, prevent or delay the bridging
of the river. The first attempts were on the night of the relief, but
these were repulsed without much trouble. On the afternoon of the
13th the enemy made much more desperate counter-attacks. At
4.15 p.m., after a heavy bombardment, their infantry and machine-
gunners counter-attacked in force. Though the troops of the division
on the right were forced to withdraw their forward posts, the two
companies of the 8th L.F. stood firm, and inflicted very heavy losses,
a Lewis-gun section contributing largely to this. Fresh counter-
attacks, no less violent, were made, and enfilading machine-gun fire
compelled the posts on the extreme right to withdraw a short
distance and take up a defensive flank, which checked the enemy. At
night-fall patrols again worked forward and established posts close
to the original line. In the course of the next few days the brigade
succeeded in establishing posts across the river along the whole
divisional front, between Briastre and the railway triangle just south
of Solesmes.
The enemy now disclosed a greater artillery strength than he had
shown during recent weeks, and on the 13th and 14th Briastre was
heavily shelled and gassed. Captain H. Neame, R.A.M.C.,
accompanied by a French officer, Lieutenant Pinto, and some N.C.O.s
and men of the 2nd and 3rd Field Ambulances, twice entered the
village during the worst of the shelling, and helped to evacuate more
than 150 civilians. Among these were at least thirty infirm persons
and children, who had to be sought out, and then carried into safety.
On the 15th Lance-Corporal W. Armstrong, 5th L.F., being ordered to
reconnoitre a suspected machine-gun post in a house on the
Solesmes—Belle Vue road, led his section so skilfully that they not
only found the post, with a loaded gun in position, but also seized an
opportunity to enter quietly and abstract the gun before the garrison
realized what was taking place. They were then fired upon, but the
withdrawal was managed with equal skill, and the gun carried off
without a casualty to the section.
As the banks of the Selle drop rather sharply to Bridging the Selle,
the water it is not an easy stream to bridge or October 17-19
cross, especially as the enemy held the higher
ground, with direct observation, not more than 400 yards away, and
in some places their machine-gun posts were only 100 and 150
yards from the river. On the nights of the 17th and 18th the
Divisional Engineers erected four footbridges with handrails, and
prepared material for others, and also for two pontoon bridges to
take wheeled traffic and field-guns. About the centre of the sector
was a weir; north of this the width of the stream was from 15 to 20
feet, and here the bridges were made of German telegraph posts, 30
feet in length, with boards nailed across. South of the weir the width
was greater, and the bridges were made of trestles and duckboards.
On the night of the 19th—the assembly for attack taking place at
midnight—four more footbridges and the pontoon bridges were
erected. The pontoon wagons were brought down soon after dusk,
their wheels padded with canvas filled with straw to deaden the
noise. There was a great deal of rain, the river north of the weir rose
nearly four feet, overflowed its banks, and inundated the
surrounding country, so that the bridges already constructed had to
be raised and extended. By the gallantry and good work of the
Engineers all was completed in time, in spite of violent machine-gun
fire and shelling with H.E. and gas. Respirators were worn for
several hours, but there were only six casualties among the sappers.
The route to each bridge was taped out to guide the assembling
troops and ensure speed in getting into the “kicking-off” positions
east of the Selle; and a lantern was placed at the head of each
bridge, which was numbered. Thanks to the efficiency and
thoroughness of the sappers, the infantry were able to cross the
river and deploy for the attack without confusion or delay.[23]
The bridging being completed, the advance of the Third Army was
resumed on October 20, with zero hour at 2 a.m. The direction of
the Division’s advance, which from the opening of the Third Army’s
offensive had been practically due east, was now turned to the
north-east, towards Maubeuge and Mons. The attack was planned in
three phases.
First Phase.—The 126th Brigade, which had relieved the 125th in
the front line, would advance, with the 5th East Lancashires on the
right and the 10th Manchesters on the left, and a company of the
8th Manchesters in support of each battalion, at zero plus three
minutes, under a barrage lifting at the rate of 100 yards every three
minutes. The first objective, termed the Blue Line, was the railway
cutting running southwards from Solesmes—a very strong defensive
system, with deep dug-outs in the cutting; the second objective,
termed the Green Line, was the high ground beyond a sunken road
which ran roughly parallel to the railway, about 1500 yards north-
east of the “kicking-off” positions. Between the Blue and Green Lines
the barrage would lift at the rate of 100 yards every five minutes.
Handshakes would be established with the 5th Division on the right
and the 62nd on the left on the divisional boundary lines at each
objective.
Each battalion would advance in four waves, as follows: On the
right the first wave (“B” Company, 5th E. Lancs.) to go forward with
six sections in arrow-head formation to the railway cutting, while one
platoon would seize Belle Vue Farm, near the cross-roads a few
hundred yards west of the railway, mop up the cross-roads, and then
work from left to right and form a defensive flank on the spur just
beyond the railway; the second wave (“C” and “D” Companies),
advancing in extended order, to carry on the advance to the Green
Line, through the ravine that stretched diagonally between the two
objectives; the third wave (“D” Company) to advance in line of
sections in single file, and, after mopping up the ravine, to extend
either flank of the leading companies and provide fighting patrols to
clear the flanks and find touch with the 10th Manchesters and the
5th Division; the fourth wave (“D” Company, 8th Manchesters) to
form a defensive flank between the spur and the right of the Green
Line. On the left, the first wave of the 10th Manchesters (“C”
Company on the right, “A” Company on the left) to carry both
objectives; the second wave (“B” Company) to reinforce the first,
and mop up and occupy the cutting; the third wave (“D” Company)
to leapfrog over the second and clear the ravine; the fourth wave
(“B” Company, 8th Manchesters) to assist in mopping up and to
support the others as required.
Soon after midday the enemy massed for a counter-attack, but the
assembly was broken up by the artillery and outstanding work by
the machine-guns. About 4 p.m. the 5th Division attacked under a
heavy barrage, the 5th and 7th Manchesters, with companies of the
8th which had been brought forward, co-operating. This advance
was successful and the Red Line was secured. A machine-gun nest
on the right, which had been very troublesome all morning, was
captured by “C” Company, 7th Manchesters. The 6th Manchesters
were now able to resume their attack, which had been postponed,
as the artillery had been required to switch the barrage to other
objectives; and the final objective, the Brown Line—more than 1000
yards beyond Marou—was captured about 9 p.m., some hundreds of
the enemy being driven from the last position, in the sunken cross-
roads half-way between Marou and Vertigneul, with heavy loss.
Privates H. W. Jarvis, M.M., and M. Fearn, 6th Manchester stretcher-
bearers, had displayed great devotion in attending to the wounded
and bringing them into safety at a time when the violence of the
machine-gun fire made it almost certain death to move about.
Throughout the rest of the day and in the evening these two men
worked incessantly in constant peril, and saved many more lives.
The enemy artillery had been active since early morning, their fire
being especially concentrated upon our guns. Battery D/211 was put
out of action gun by gun, until one gun remained, with only
Sergeant W. Ritchie and Gunners J. Willis and H. Watts to work it.
After a time Ritchie was too badly wounded to be able to give any
assistance, but the two gunners continued to fight their gun until at
last it was put out of action by a direct hit. All three received the
D.C.M.
The night of October 20th closed with all the All Objectives
Division’s objectives secured. The Prussians of the Gained
“Invincible” 25th Division had plenty to think about
—both those in the divisional cage and those still at large. They had
been very thoroughly beaten and very roughly handled; and when it
had come to “in-fighting” they had been soundly thrashed again and
again by half their numbers. They had met two of the East
Lancashire brigades to their sorrow, and were soon to experience
similar treatment from the third.
The newly-won positions were consolidated next day, October
21st. In the afternoon there was heavy enemy shelling with H.E. and
gas; and low-flying aeroplanes were active over the front. The
Germans were by no means done for; they meant to resist
stubbornly any further advance, and had brought artillery from other
sectors into position. During the night of the 21st-22nd the 125th
Brigade relieved the 127th, and the enemy shelling continued at
intervals during the day and night of the 22nd. It was unusually
heavy at midnight, and the assembly of the brigade for the attack
was made under most unfavourable conditions, the number of
casualties among the Fusiliers being serious.
The advance of the Third Army was resumed in the early hours of
the 23rd October, the Fourth and First Armies co-operating. The
42nd Division held the left of the 4th Corps front, with the 5th
Division on the right, and the 3rd Division, 6th Corps, on the left. At
3.20 a.m. the Divisional and New Zealand Artillery put down a
barrage which lifted at the rate of 100 yards in six minutes. The
Division’s final objective was, roughly, the line of the River Harpies
from the north of Beaurain on the right to Vertigneul in the centre,
and to the outskirts of Romeries, north-east of Vertigneul, on the
left. The advance was to be made in three bounds, and the New
Zealanders would then pass through the 125th Brigade at 8.40 a.m.
and continue the advance, supported by the divisional artillery and
two companies of the 42nd M.G. Battalion.
At 3.26 a.m. the Fusilier Brigade, with “C” Capture of
Company, M.G. Battalion, moved off, the 7th Vertigneul,
Battalion on the right, the 8th on the left, and the October 23
5th in support. Once again the right flank had the greater difficulty,
and the progress of the 7th was slow. They were enfiladed from
machine-gun posts on the high ground west of Beaurain, from
Beaurain itself, and, later, from the quarries north of Beaurain. These
poured destructive fire upon them as they strove gallantly to go
forward. A couple of tanks were sent to their assistance, and these
eased the situation by destroying the nearer machine-gun nests. But
the enemy, holding Beaurain very strongly, had checked the advance
of the 5th Division, so the flank of the 7th L.F. was in the air, and its
right company had been much weakened by heavy losses. The left
company, however, made progress in the centre by the Marou—
Beaurain road. There were numerous instances of devotion to duty
and disregard of personal safety on the part of all ranks. Lieutenant
W. J. O’Bryen gained a second bar to his M.C. by leading his men
round by a flank against a machine-gun nest which had held them
up at 100 yards’ range, and capturing the position; and C.S.M. Thos.
Roe, M.M., dealt similarly with another nest. Corporal F. Hesford kept
his men together under deadly shell fire, and though knocked down
by a shell which killed the man next to him, Hesford worked his
section steadily through the fire to the flanks of the machine-guns,
and captured all three.
Conditions on the left were much more satisfactory. The 8th L.F.
had advanced steadily, and as the 3rd Division was making
corresponding progress on the left, their flank was not unduly
exposed. Still, they had to fight their way step by step, held up time
after time by the numerous machine-gun nests, but always
overcoming the resistance, and turning the enemy out of his holes at
the bayonet’s point. Soon after the start Captain D. G. Bird’s
company came under fierce fire from machine-gun nests. Led by
their officer, the company rushed the posts and bayoneted the entire
garrison. Pushing on, Bird led his men to the final objective, north of
Beaurain, and held it under very heavy fire. C.S.M. Riley, whose
courage and coolness in the assembly had been conspicuous, made
personal reconnaissances under heavy fire during the attack, and
brought valuable information regarding the situation on the flanks.
He was the first man on the final objective. Sergeant C. Carter,
seeing that the troops on his right were held up, rushed the enemy
post with two men, and captured the gun. He then took nine
prisoners from a dug-out close to the post. Lance-Corporal W.
Walmsley, though wounded in the side by a bayonet, led his section
against a machine-gun nest which had been holding up the advance,
and captured it, all the enemy being killed with the bayonet. Privates
C. H. Perkins and F. Peace worked a Lewis-gun under heavy artillery
and machine-gun fire, and though Perkins was severely wounded in
the knee, the two men continued to fight the gun until they had
killed or captured the whole of the opposing machine-gun crew.
Privates H. A. Jackson and G. H. Robson attacked a machine-gun
nest with a Lewis-gun, killed four men and captured or dispersed the
rest. As similar instances of pluck and confidence were being shown
in all parts of the fighting area between Marou, Beaurain and
Vertigneul, and the trench-mortar crews and machine-gunners
fought with rifle, bayonet and revolver; and stretcher-bearers,
R.A.M.C. and signallers, though not taking part in hand-to-hand
combats with the bayonet, were doing their duty with equal
disregard of their own safety, the final objective was reached, at first
on the left, where Vertigneul was captured by the 8th L.F., and later
on the right flank, where Captain Kirsopp, G.S.O. 3, of the 42nd,
gave valuable assistance by a reconnaissance outside the divisional
boundary, north-west of Beaurain. Here he came across a company
of the Cheshires and led them to their objective, where they linked
up with the 7th L.F., and the front was securely established. The 8th
L.F. had captured more than 100 prisoners, two field-guns, thirty-
four machine-guns, and some trench-mortars. The 7th and 5th had
also made good captures.
This attack of the 125th Brigade was as brilliant an exploit as any
during the war. The difficulty of assembly in the dark had been
augmented by the complicated tactics required by the need to align
the barrage to suit divisions on the flanks. An outward wheel on one
flank of the Brigade and an inward wheel on the other flank had to
be made in pitch darkness, and while in actual conflict with the
enemy, before the whole line could advance. The area was saturated
with enemy gas, and machine-gun nests abounded. Orders had been
frequently changed, and it was only possible to issue final orders to
the Brigade at 2 p.m. at Belle Vue Farm. Company commanders then
had to crawl considerable distances to attend a hastily summoned
conference. This left little time to get the orders down to platoon
commanders, and objectives could not be pointed out in daylight.
But the Fusiliers went over the top as though on parade and took all
objectives. The laurels gained here are worthy of a place beside
those won at Minden.
At the scheduled time, 8.40 a.m., the New Zealanders passed
through the 125th Brigade, and continued the advance beyond the
line of the Harpies, two companies of the 42nd M.G. Battalion going
with them. This was the second flying start given them by the
Division.
Prisoners taken by the 42nd Division during the operations of
October 20-23 numbered 927, and the booty included three field-
guns, 102 machine-guns, nine trench-mortars, and twenty-two anti-
tank rifles. A number of enemy batteries were put out of action by
our artillery. Five hundred and fifteen enemy dead were counted on
the battlefield—men of the shattered and demoralized 25th German
Division, and to a lesser extent of the 18th Division. The 42nd, which
had lost 138 killed and 707 wounded, withdrew west of the Selle,
and on October 24 concentrated in Beauvois, a pleasant little town,
only slightly damaged by the war, where really good billets were
enjoyed for the first time since March. The inhabitants of Beauvois
and Caudry had been in German hands for four years, and their
delight at having Englishmen as guests, instead of Germans as
tyrants, was displayed in a manner rather disconcerting to many
lads, who found it embarrassing to be kissed and fussed over, and to
be called “deliverers” and “saviours.” However, they appreciated the
many evidences of kindliness and goodwill, and helped in their own
way to cement the friendship and understanding between the
nations; and they were truly glad to be once more in an inhabited
country. Everything possible was done to make the men happy and
comfortable. The canteens were well supplied; massed bands gave
enjoyable concerts; and the Divisional Concert Party gave daily
performances of Sweet Fanny Adams and April Fools in a factory that
had been converted into a theatre. The enemy had been pressed so
hard that they had left, almost intact, an excellent bathing
establishment where hot baths could be obtained. The Divisional
Reception Camp was moved up to Aulicourt Farm, and, while training
was by no means neglected, plenty of opportunity was given for
recreation. The Divisional Boxing Tournament was won by the 8th
L.F., as was also the Band Contest. Most popular of all was the
Football Competition, in the final of which the 7th N.F. (Pioneers)
beat the R.A.M.C. by two goals to nil. Ten pleasant days were spent
at Beauvois, and on the night of November 3 the Division began its
move forward to relieve the N.Z. Division in the Forêt de Mormal.
CHAPTER XIV
FORÊT DE MORMAL AND HAUTMONT
(November 3-11, 1918)
An advance along the entire fronts of the Fourth, Third and First
Armies, French armies co-operating, had been planned for the 4th
November. The attack on the 4th Corps front was to be carried out
by the 37th Division on the right and the N.Z. Division on the left;
and the 42nd Division was to pass through the New Zealanders, and
the 5th Division through the 37th, on the night of November 5-6. As
secrecy was essential, movements of troops prior to the opening of
the battle had to be made in darkness.
The 126th Brigade marched out of Beauvois soon after darkness
fell on November 3, the 127th following, and the 125th bringing up
the rear. The roads had been much damaged by heavy traffic and
rain, and the going was bad. The 127th Brigade completed its first
stage—to Viesley—before midnight, and the 126th Brigade arrived at
Solesmes between 2 and 3 a.m. on the 4th, and remained there
until the attack of the 4th Corps was launched, at 5.30 a.m. Secrecy
being no longer necessary the march was then resumed. By 6 p.m.
the New Zealanders reported the capture of Le Quesnoy, with 1000
prisoners, and the 126th and 127th Brigades were now in touch with
the situation on the N.Z. Division’s front, the former having reached
Beaudignies, about 3000 yards south-west of Le Quesnoy.
The 126th Brigade passed through Le Quesnoy to Herbignies on
the morning of the 5th November, keeping about 6000 yards in rear
of the New Zealanders, and the rear brigade, the 125th, reached
Beaudignies. Here D.H.Q. opened during the morning of the 5th, but
on account of the situation moved further forward, to Potelle
Château. In the afternoon the leading brigade entered the extensive
Forêt de Mormal[24] at a point about half a mile east of Herbignies.
Bad as the conditions of the march from Beauvois had been, they
now became far worse. Rain had been falling steadily for some days,
and the roads and forest tracks—bad at the best of times—were
ankle-deep, and in places almost knee-deep in mud and decaying
leaves. At the main cross-roads huge craters had been blown,
measuring from 60 to 80 feet in diameter, with a depth of 30 feet.
One of these was bridged by the sappers, and a corduroy deviation
road made round the biggest—just west of Forester’s House—a way
being cut through the trees for horses and vehicles. Four men in
each platoon had been furnished with hatchets and bill-hooks in
anticipation of obstacles in the forest, and wood-warfare had formed
part of the training given at Beauvois.
Meanwhile the leading brigade pressed forward, leaving its
transport behind. Progress was slow, as machine-guns, Lewis-guns,
munitions and supplies had to be man-handled. The enemy
continued to shell the forest spasmodically, and though casualties
were infrequent, the sound of the shells crashing through and
tearing off the branches was not exhilarating. The long and arduous
passage through the Mormal Forest will long be remembered by all
three brigade groups as a drab and depressing episode, unrelieved
by the excitement of fighting, or even seeing the enemy.
The weather, the congestion, and the condition of the roads had
become worse, and the 127th Brigade, which entered the forest on
the following day, had a more wretched experience even than the
others. For two days they had had no shelter from the bitterly cold
rain, and as they were badly in need of a rest, the 125th Brigade
was ordered up from Herbignies, where shelter and rest had been
obtained, to become the support brigade, the 127th being moved to
billets in Le Carnoy for a couple of days. The central road past
Forester’s House was the widest and best of the routes, but even
here there was barely room for one wagon to pass another. Trees
had been blown across it; there were bogs on either side, and its
eastern part was shelled continually. If, in making way for motor-
traffic, a gun- or wagon-wheel got off the track by so much as a
foot, it was at once bogged up to the axle. The Divisional
Commander therefore issued an order prohibiting the use of motors
beyond a certain point, so Generals and Staff Officers had to
proceed on horseback or on foot. It was a necessary but not a
popular order, particularly with supply officers, but in spite of all
adverse conditions—rain and mud, craters and blown culverts,
congestion of men, horses, guns, ammunition and ration vehicles—
the transport was most efficiently managed; supplies and
ammunition got through, and the troops even received a hot meal
from the field-kitchens in the middle of the forest. The Engineers
and Pioneers worked magnificently, as always.
On the evening of November 5 the command of November 5-6
the left division sector of the 4th Corps front
passed to the G.O.C. 42nd Division, and by 3.30 a.m. on the 6th the
relief of the leading N.Z. Brigade by the 126th Brigade had been
completed. The relief had been peculiarly difficult, on account of the
intense darkness, the uncertainty of the location of the troops to be
relieved, and the state of the forest roads. The front taken over ran
from a point just beyond the forest near Petit Bayay on the right and
along the extreme eastern fringe a little further north, then bent
back through the forest to Les Viviers and La Grande Rue, where the
situation was obscure, the right division of the 6th Corps being
echeloned some 3000 yards to the rear of the 42nd’s front. The 4th
Corps front was, as usual, in advance of the Corps to right and left,
and once again the 42nd Division formed the spearhead of the 4th
Corps offensive, taking this rôle turn and turn about with their New
Zealand comrades. To cover the exposed left flank a second M.G.
Company was allotted to the 126th Brigade.
FORESTER’S HOUSE AND CRATERS IN THE FORÊT DE MORMAL.
FORT EAST OF HAUTMONT, NEAR WHICH THE DIVISION HAD ITS LAST
FIGHTING ON NOV. 8, 1918.
HAUTMONT, CAPTURED BY 126TH BRIGADE ON NOV. 8, 1918. THE BRIDGES
WERE ERECTED BY THE DIVISIONAL ENGINEERS.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com