Introduction to Dependent Types with Idris: Encoding Program Proofs in Types 1st Edition Boro Sitnikovski instant download
Introduction to Dependent Types with Idris: Encoding Program Proofs in Types 1st Edition Boro Sitnikovski instant download
https://ebookmeta.com/product/introduction-to-dependent-types-
with-idris-encoding-program-proofs-in-types-1st-edition-boro-
sitnikovski-2/
https://ebookmeta.com/product/introduction-to-dependent-types-
with-idris-encoding-program-proofs-in-types-1st-edition-boro-
sitnikovski/
https://ebookmeta.com/product/gentle-introduction-to-dependent-
types-with-idris-1st-edition-boro-sitnikovski/
https://ebookmeta.com/product/introducing-software-verification-
with-dafny-language-proving-program-correctness-1st-edition-boro-
sitnikovski/
https://ebookmeta.com/product/mountain-landscapes-in-transition-
effects-of-land-use-and-climate-change-1st-edition-udo-
schickhoff-r-b-singh-suraj-mal-2/
Notes on Hamiltonian Dynamical Systems 1st Edition
Antonio Giorgilli
https://ebookmeta.com/product/notes-on-hamiltonian-dynamical-
systems-1st-edition-antonio-giorgilli/
https://ebookmeta.com/product/lost-feather-the-forgotten-angel-
book-1-1st-edition-merri-bright/
https://ebookmeta.com/product/the-event-of-music-history-1st-
edition-j-p-e-harper-scott-2/
https://ebookmeta.com/product/summary-of-at-the-edge-of-the-
orchard-by-tracy-chevalier-instaread/
https://ebookmeta.com/product/academic-writing-a-handbook-for-
international-students-5th-edition-bailey/
A Travel Guide to the Seven Kingdoms of Westeros 1st
Edition Daniel Bettridge
https://ebookmeta.com/product/a-travel-guide-to-the-seven-
kingdoms-of-westeros-1st-edition-daniel-bettridge/
Introduction to
Dependent Types
with Idris
Encoding Program
Proofs in Types
Boro Sitnikovski
Introduction to Dependent Types with Idris: Encoding Program Proofs
in Types
Boro Sitnikovski
Skopje, North Macedonia
Acknowledgments�����������������������������������������������������������������������������xiii
Preface�����������������������������������������������������������������������������������������������xv
Introduction��������������������������������������������������������������������������������������xvii
v
Table of Contents
vi
Table of Contents
Conclusion����������������������������������������������������������������������������������������117
Further Reading��������������������������������������������������������������������������������119
Index�������������������������������������������������������������������������������������������������153
vii
About the Author
Boro Sitnikovski has over ten years of experience working professionally
as a software engineer. He started programming using the Assembly
programming language on an Intel x86 at the age of ten. While in high
school, he won several prizes for competitive programming.
He is an informatics graduate; his bachelor’s thesis was titled
“Programming in Haskell Using Algebraic Data Structures,” and his
master’s thesis was titled “Formal Verification of Instruction Sets in Virtual
Machines.” He has also published papers on software verification. Other
research interests include programming languages, mathematics, logic,
algorithms, and writing correct software.
He is a strong believer in the open-source philosophy and contributes
to various open-source projects.
In his spare time, he enjoys time with his family.
ix
About the Technical Reviewers
Nathan Bloomfield holds a PhD in algebra from the University of Arkansas
and taught mathematics before joining Automattic, Inc. as a programmer.
He enjoys witnessing the intuitionist renaissance and likes how the
boring parts of abstract algebra transform into really interesting computer
science. Nathan usually feels a little out of his depth and prefers it that way.
xi
Acknowledgments
Special thanks to the Haskell community (#haskell@freenode), the Idris
community (#idris@freenode), and the Coq community (#coq@freenode).
Thanks to my family, coworkers, and friends for all the support they
give me.
Finally, thank you for purchasing this book! I hope that you will learn
new techniques in reading this book and that it will spark interest in logic,
dependent types, and type theory.
xiii
Preface
This book aims to be accessible to novices who have no prior experience
beyond high school mathematics. Thus, this book is designed to be
self-contained. No programming experience is assumed. However, having
some kind of programming experience with the functional programming
paradigm will make things easier to grasp in the beginning. After you finish
reading the book, I recommend that you check the “Further Reading”
section if you are interested in diving deeper into some of the topics
discussed.
I have always been curious about understanding how things work. As a
result, I became very interested in mathematics while I was in high school.
One of the reasons for writing this book is that I could not find a book that
explained how things work, so I had to do a lot of research on the Internet
through white papers, forums, and example code in order to come up
with a complete picture of what dependent types are and what they are
good for.
I will consider this book successful if it provides you with some
additional knowledge. I tried to write this book so that the definitions
and examples provided in it show how different pieces of the puzzle are
connected.
Feel free to contact me at [email protected] for any questions you
might have, and I will do my best to answer. You can also access my blog at
bor0.wordpress.com to check out some of my latest work.
xv
Introduction
Writing correct code in software engineering is a complex and expensive
task, and too often our written code produces inaccurate or unexpected
results. There are several ways to deal with this problem. In practice, the
most common approach is to write tests, which means that you are writing
more code to test your original code. However, these tests can only detect
problems in specific cases. As Edsger Dijkstra noted, “testing shows the
presence, not the absence of bugs.” A less common approach is to find
a proof of correctness for your code. A software proof of correctness
is a logical proof that the software is functioning according to given
specifications. With valid proofs, you can cover all possible cases and be
more confident that the code does exactly what it was intended to do.
Idris is a general-purpose functional1 programming language that
supports dependent types. The features of Idris are influenced by Haskell,
another general-purpose functional programming language. Thus, Idris
shares many features with Haskell, especially in the part of syntax and
types, where Idris has a more advanced type system. There are several
other programming languages that support dependent types2; however,
I chose Idris for its readable syntax.
The first version of Idris was released in 2009. It is developed by The Idris
Community and led by Dr. Edwin Brady. Seen as a programming language,
it is a functional programming language implemented with dependent
types. Seen as a logical system, it implements intuitionistic type theory,
which I cover later. Finally, Chapter 4 shows how these two views relate.
1
The core concept of functional programming languages is a mathematical
function.
2
Several other languages with dependent types support are Coq, Agda, Lean.
xvii
Introduction
xviii
CHAPTER 1
Formal Systems
Before you can construct proofs of correctness for software, you need to
understand what a proof is and what it means for a proof to be valid. This
is the role of formal systems. The purpose of formal systems is to let you
reason about reasoning—to manipulate logical proofs in terms of their
form, rather than their content. This level of abstraction makes formal
systems powerful tools.
2
Another Random Scribd Document
with Unrelated Content
PHOTO BY FRITIOF FRYXELL
Early in the morning the dog which was in the cabin with the men
began to bark shrilly, perhaps taking alarm at the scent of the posse.
Spenser got up, dressed, buckled on his revolver, and went out to the
corral.
The corral lay between the cabin and the shed, and after Spenser
had entered it one of the posse called to him to “throw ’em up.”
Instead Spenser drew with lightning speed and fired twice, one bullet
passing between two logs and almost hitting the spokesman, the
other nicking a log near by. The posse returned fire and Spenser fell
to the ground, propping himself up on one elbow and continuing to
shoot until he collapsed.
Meanwhile Burnett had got up, slipped on his overalls and boots, and
fastened on his revolver. Then he picked up his rifle in his right hand
and came out of the cabin. As he stepped forth, one of the men
behind the ridge fired at him. The bullet struck the point of a log next
to the door, just in front of Burnett’s eyes. Burnett swept the splinters
from his face with his right hand as he reached for his revolver with
his left, and fired lefthanded at the top of the gunman’s hat, just
visible over the ridge. The shot was perfect; the bullet tore away the
hat and creased the man’s scalp. He toppled over backwards.
Burnett then deliberately walked over to the corner of the cabin and
stopped, with rifle in hand, in full view of the entire posse, taunting
them to come out and show themselves. From inside the cabin
Jackson pleaded with him to come in or he would get it too. Burnett
finally turned, and as he did so one of the members of the posse shot
him. The bullet killed Burnett instantly, and he pitched forward
toward the cabin, discharging his rifle as he fell.
Now only Jackson was left in the cabin. A big, bumbling man 46
with a knack for trouble, Jackson had once before been taken
by mistake for a horsethief and been scared almost to death; when
he was now ordered to come out and surrender with his hands in the
air he did so immediately.
The work of the posse was done. Mike Burnett lay face down in the
dirt at the corner of the cabin, the bullet from his last shot lodged in
a log beside him; George Spenser, his six-shooter empty, was
sprawled inside the corral with 4 charges of buckshot and 4 or 5
bullets in his body. They were buried in unmarked graves a few
hundred yards southeast of the cabin, on the south side of a draw.
No investigation was ever made, no trial held, and the matter was
hushed up. As years went by the subject of the killing at Spread
Creek became a touchy one, and most of the men directly involved
preferred not to talk about it. Swede Jackson, apparently thoroughly
shaken by the incident, left the valley and did not return. The affair at
Cunningham’s Ranch was a closed story.
Those in the valley who had not been in on the posse were not so
sure of the legality of the shooting. Cunningham said he thought the
leader was not an officer, and reiterated that the posse had been
instructed not to arrest but to kill. He stated that 2 local men had
previously been asked to dispose of the pair, but had refused. When
asked who raised the posse and investigated the killing, Cunningham
laughed and said he could tell but preferred not to; asked if he cared
to state whether the move was local or not, he quickly said, “Oh no—
it wasn’t only local.”
Years later badgers threw out some of their bones into the sunlight.
47
[8]
PROSPECTOR OF JACKSON HOLE
By Fritiof Fryxell
In the 1880’s and 1890’s it was widely supposed that the Snake River
gravels of Jackson Hole, in Wyoming, contained workable deposits of
placer gold, and there were many who came to the region, lured by
such reports and a prospector’s eternal optimism.
Color, indeed, could be struck almost anywhere along the river, but
the gold of which it gave promise proved discouragingly scarce and
elusive. None found what in fairness to the word could be called a
fortune. Few found sufficient gold to maintain for any length of time
even the most frugal living—and who can live more frugally than the
itinerant prospector? So through these decades prospectors quietly
came and sooner or later as quietly left, leaving no traces of their
visit more substantial than the scattered prospect holes still to be
seen along the bars of the Snake River. Even today a prospector
occasionally finds his way into the valley, and, like a ghost out of the
past, may be seen on some river bar, patiently panning. Probably he,
too, will drift on. It is apparent now that the wealth of Jackson Hole
lies not in gold-bearing gravels but in the matchless beauty of its
snow-covered hills and the tonic qualities of its mountain air and
streams.
But one prospector stayed. Mysterious in life, Uncle Jack Davis has
become one of the most shadowy figures in the past of Jackson Hole,
little more than a name except to those few still left of an older
generation who knew him. He deserves to be remembered—deserves
it because of his singular story, and because he has the distinction
historically of having been the only confirmed prospector in Jackson
Hole.
He was “Uncle” only by courtesy for he lived a lonely hermit until his
death; and so far as is known he left no relatives. He first appeared
in 1887 as one of the throng of miners drawn irresistibly into that
maelstrom of the gold excitements, Virginia City, Montana. In a 48
Virginia City saloon he became involved in a brawl and struck a
man down, struck him too hard and killed him. Davis, it should be
remarked, was a man of herculean strength and, at the time of this
accident, he was drunk. Believing himself slated for the usual
treatment prescribed by Montana justice at the time—quick trial and
hanging—he fled the city.
Uncle Jack’s cabin was located on the Snake River near the mouth of
Bailey Creek. The plank structure on the roof is the old sluice box which
was used to make his coffin.
Davis’ solitary habits sprang from a haunting fear of pursuit, not from
dislike of companionship. The presence of a stranger in the region
made him uneasy, and he did not rest until his mission was known,
sometimes pressing a friend into service to ascertain a stranger’s
business. He rarely allowed his photograph to be taken. Apparently
his fears had little foundation, for no one from “outside” ever came in
after him. Very likely Virginia City soon forgot him.
It seems as though for the remainder of his days Uncle Jack did
penance for his one great mistake. He impressed one as trying hard
to do the right thing by everyone and everything. Such was his love
for birds and animals that he would go hungry rather than shoot
them. To callers at his shack he explained the absence of meat from
the table by a stock alibi so lame and transparent that it fooled no
one: “He’d eat so much meat lately that he’d decided to lay off it for
awhile.” His unwillingness to kill turned him into a vegetarian—here in
the midst of the best hunting country in America. A hermit, yet Uncle
Jack was hardly lonely. In birds and beasts of the canyon he found a
substitute for human companionship. The wild creatures about him
soon ceased to be wild. His family of pets included Lucy, a doe who
lived with him for many years; Buster, her fawn, whom the coyotes
finally killed; two cats—Pitchfork Tillman, named for a prominent
political figure of the times, and Nick Wilson, much given to night life,
so named after a prominent pioneer of the valley; and a number of
tame squirrels and bluebirds. Not to mention Dan, the old horse, and
Calamity Jane, the inevitable prospector’s burro, which had
accompanied Jack in his flight to Jackson Hole, where it finally died at
the advanced age of 40 years. Maintaining peace in such a family
kept Uncle Jack from becoming lonely.
Al Austin, who for many years was forest ranger in this region, 50
and who in time came to enjoy Uncle Jack’s closest confidence,
presents an unforgettable picture of the old man and his family.
Dropping in at mealtime for a friendly call, Austin would find Uncle
Jack in his cabin surrounded by his pets, each clamouring to be fed
and each jealous of attention bestowed on any creature other than
itself. If the bluebirds were favored, the squirrels chattered
vociferously. Buster, if irritated, would justify his name by charging
and upsetting the furniture. Add to this the audible impatience of
Pitchfork Tillman and Nick Wilson, Lucy was ladylike but nevertheless
insistent. To this motley circle Uncle Jack would hold forth in
inimitable language, carrying on a running stream of conversation—
scolding, lecturing, admonishing, or when discord became acute,
threatening dire punishment if they did not mend their ways. It is
hardly necessary to add that to Uncle Jack’s awful threats, and the
vivid profanity, which it must be admitted, accompanied them, the
members of the household remained serenely indifferent, and there is
no record that any of the promised disasters ever fell on their furry
heads.
PHOTOGRAPHER UNKNOWN
Uncle Jack Davis, the only confirmed prospector of Jackson Hole, was
tall, broad, of magnificently erect carriage—a blue-eyed, full-bearded
giant. This is a rare photograph taken shortly before his death.
Having no windows, Uncle Jack left his door open during the good
weather. One spring a pair of bluebirds flew through the open door
into the shack and, having inspected the place and found it to their
liking, built their nest behind a triangular fragment of mirror which
Uncle Jack had stuck on the wall. Uncle Jack then cut down the door
from its leather hinges and did not replace it until fall. Six successive
summers the bluebirds returned to the cabin, and, finding the door
removed in anticipation of their coming, built their nest and raised
their young behind Uncle Jack’s mirror.
Nearby Uncle Jack made a little graveyard for his pets, as they left
him one by one. It was lovingly cared for. In the course of the 24
years which he spent there the burial ground came to contain many
neat mounds—mounds of strangely different sizes. But Lucy, Pitchfork
Tillman, and Dan outlived Uncle Jack.
He would not accept charity, even during the last year or two of his
life when he was nearly destitute. Neighbors had to resort to strategy
to get him to accept help.
The old man lay in bed, delirious. The last date checked off on the
wall calendar was February 11. Outside the cabin, elk had eaten all
the hay, and the horse and Lucy were at the point of starvation.
Austin stayed by his bedside for several days, then, finding it
impossible to care for Uncle Jack decently in the dark old cabin,
summoned Counts. Several days later they moved the old man 6
miles up the river, carrying him where they could, most of the way
pulling him along in a boat from the shore. The old trail was one Jack
himself had built many years before. In Count’s cabin, a week later,
Uncle Jack died.
Austin made Uncle Jack’s coffin from one of the old man’s own sluice
boxes. Together the two men carried Uncle Jack to the grave they
had dug for him at Sulphur Springs, nearby in the canyon. A wooden
headboard on which Ranger Austin carved the inscription, “A. L.
Davis, Died March 25, 1911,” marks the grave—there Uncle Jack
sleeps alone.
In Davis’ shack was found the “fortune” which placer mining had
brought him—$12 in cash and about the same value in gold
amalgam.
52
[9]
MOUNTAIN RIVER MEN
THE STORY OF MENOR’S FERRY
By Frances Judge
Both Bill and Holiday carried a mouthful of oaths that spilled out
whenever they spoke. They cursed their friends and neighbors, they
cursed each other, and they cursed themselves. But to lighten this
burden of words when women were around, Holiday would say,
before a sentence, in the middle of a sentence or at the end of one,
“Holy Savior, yes!” or “Holy Savior, no!”
Everyone in Jackson Hole knew Bill and Holiday Menor. They were as
much a part of the country as the Snake River or the Teton
Mountains. The type of men they were brought them here.
The Menor brothers came originally from Ohio. They were tall men.
Bill, 11 years older than his brother, was thin and long-boned. His
nose and sharp eyes were like an old eagle’s. Holiday’s long body
sagged a little. He had a grizzled beard, long, shrewd nose, and
amused, gray eyes. He prospected in Montana before coming to
Jackson Hole. “My partner’s name was Mean, but I was Menor,” he
would say. He claimed to have made over one hundred and twenty
thousand dollars in one prospect. When asked what happened to the
money, he always said, “Wine, women and song.” He talked of going
off to Old Mexico, prospecting, but he never went. There was too
much living to be done on the banks of the Snake River.
Many settlers cut timber on Bill’s side of the river, so the ferry was
welcome. There were times when it was the only crossing within a 40
mile stretch up and down the river. Once in awhile there was no
crossing at all, when the river was “in spate” and Bill refused to risk
the ferry. At such a time people were forced to go up one side of the
river to Moran, cross the toll bridge, and travel down the other side—
80 miles to travel 8.
Menor’s Ferry at about the turn of the Century. Where the mad Snake
rolls by, and the shadow of the great mountains moves over sage, and
building, and river.
In those early days almost everyone who came to cross the ferry
around mealtime was invited to eat. If the river was too high for safe
crossing and the persons who wanted to cross were in no particular
hurry, Bill would keep them 2 or 3 days, bedding them and feeding
them generously until the waters subsided, and charging them only
the slim ferry fee. “When you see them rollers in the middle of the
river, I won’t cross,” he would say, apologizing in his grouchy way for
keeping people around.
Anyone who stayed with Bill had to be washed and combed and
ready to leap at the table at twelve-noon and six-sharp. Early in the
morning, as soon as the fire was built, he yelled at them, saying,
“Come on, get out of bed. Don’t lay there until the flies blow you!”
Nothing angered him more than to have someone late for a meal,
unless it was to put a dish or a pan in the wrong place. Bill had a
place for everything and everything had to be in place. Once the Roy
VanVlecks spent the night with Bill. They washed the morning dishes
before ferrying over the river. Bill, leaning against the kitchen
doorcasing, criticized and cursed because the frying pans shouldn’t
go here and the kettles shouldn’t go there. Yet he did not offer to put
them on their proper nails or even show where they belonged.
That was Bill, and his neighbors understood. He was a man boiled
down to his primary colors.
A huge, uprooted tree swept against the ferry with such force that
the ropes broke and the boat was carried downstream, taking Bill
with it. After a quick trip, the ferry grounded on a submerged
sandbar. Neighbors gathered and conferred and hurried about, trying
to rescue Bill. He stood on the ferry violently cursing the rescue crew
and acting, in general, as though they alone were to blame for the
high water and his predicament.
One Christmas the brothers were invited to the Bar B C Ranch for
dinner. It was Holiday’s birthday. Neither knew the other was to be
there. When each arrived he was given a strong drink of whiskey to
insure amiability. The 2 brothers shook hands over the Christmas
table. Ever after they were on speaking terms.
And sometimes they spoke too freely, shaking fists and cursing each
other over the river. There was much gusto in their living.
Though Bill read hardly more than the daily paper that came to 55
him, Holiday subscribed to a number of magazines. He read 7
long months of the year and “talked it out” the other 5. He argued
politically with everyone, whether they would argue or not. “Now,
mind you, I’m telling you, this ain’t W. D. talking, this is H. H. Menor
talking, by God.” And for emphasis he would bang things with a stick
of stove wood. Once he came down on the red hot stove with his
bare fist and for a short while political views were unimportant.
The Widow down the River laughed every time she looked at
Holiday’s rooster and wanted to take a picture of it. But Holiday said,
“No.”
Fearing Mrs. Dornan would take a picture of the fowl, he killed it,
cooked it, and invited her to eat it with him. He never once thought
that the bird might have been defeathered by disease. Mrs. Dorman
ate rooster and pretended to enjoy it. She was an understanding
neighbor.
“We shot it right in the eye,” one said, holding up what was left of the
rabbit.
That is what gin and wild huckleberry juice did to a rabbit. Holy
Savior, yes! What might Holiday’s wine have done to it?
Though Holiday was more jovial than Brother Bill, his neighbors
steered clear of him when he was in the process of making lime. He
made and sold lime to neighboring ranchers. Some of them, like Bill,
whitewashed their houses inside and out with it. Holiday chinked his
houses with it. He also used it as a cure-all for man and beast. When
he made lime he had to keep a steady fire going for thirty-odd hours
in the kiln just behind the house in the bank. During these hours he
was not fit company for man or beast. But his neighbors accepted his
limy disposition as a necessary part of the process. Holy Savior, yes.
What of it?
When late fall brought bitter winds, heavy fogs, and snow, the ferry
was beached for the winter. From then on all teams had to ford the
river. A little platform was hung from the river cable to accommodate
foot passengers. It would hold 3 or 4 at one time. The passengers
mounted the platform from a ladder and sat down. Bill released the
car; with a quick swoosh it ran down the slack in the cable where it
dipped within 10 feet of the river. Then the frightened passengers
would laboriously haul themselves up the relaxed cable to the
opposite shore.
In later years, when travel became heavier, a winter bridge was flung
across the main channel. Putting in the winter bridge was the