-
Notifications
You must be signed in to change notification settings - Fork 0
/
thesis.tex
3329 lines (2848 loc) · 133 KB
/
thesis.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[
10pt, % fontsize
twoside, % symmetric pagination, for digital view
% oneside, % asymmetric pagination, for a nice printed version
a4paper, %
english, %
tikz, % to build images
openright, % start chapters on odd-numbered pages
]{book}
\usepackage{preamble}
\usepackage{graphicx}
\graphicspath{ {./images/} }
\begin{document}
%% config && title page
\frontmatter
%!TEX root = ../dissertation.tex
\newcommand{\myName}{Alessio Ferrarini}
\newcommand{\myTitle}{Abstract Hoare logic}
\newcommand{\myDegree}{Master degree thesis}
\newcommand{\myUni}{University of Padova}
\newcommand{\myFaculty}{Master degree in Computer Science}
\newcommand{\myDepartment}{Department of Mathematics ``Tullio Levi-Civita''}
\newcommand{\profTitle}{Prof.}
\newcommand{\myProf}{Francesco Ranzato}
\newcommand{\myCoProf}{Paolo Baldan}
\newcommand{\myLocation}{Padova}
\newcommand{\myAA}{2023--2024}
\newcommand{\myTime}{April}
\title{\myTitle}
\author{\myName}
% PDF file metadata fields
% when updating them delete the build directory, otherwise they won't change
\begin{filecontents*}{\jobname.xmpdata}
\Title{Abstract Hoare logic}
\Author{Alessio Ferrarini}
\Language{en-EN}
\Subject{Software Verification}
\Keywords{Hoare Logic\sep Software Verification\sep Abstract Interpretation}
\end{filecontents*}
% IMPORTANT: Update the metadata!! Contains the subject and the
% keywords
% Author in metadata must be specified again, otherwise it won't
% compile
\begin{titlepage}
\begin{center}
\begin{figure}[htbp]
\centering
\includegraphics[height=3cm]{unipd-logo}
\end{figure}
\vbox to0pt{
\vbox to\textheight{
\vfill \includegraphics[width=11.5cm]{unipd-light}
\vfill}
\vss}
\begin{huge}
\textbf{\myUni}\\
\end{huge}
\line(1, 0){\textwidth} \\
\begin{Large}
\textsc{\myDepartment}\\
\end{Large}
\vspace{10pt}
\begin{large}
\textsc{\myFaculty}\\
\end{large}
\vspace{30pt}
\begin{LARGE}
{\color{unipdred}\textbf{\myTitle}}\\
\end{LARGE}
%% \vspace{40pt}
\vfill
\begin{large}
\begin{flushleft}
\textit{Supervisor}\\
\vspace{5pt}
\profTitle\ \myProf\\
\vspace{20pt}
\textit{Co. Supervisor}\\
\vspace{5pt}
\profTitle\ \myCoProf\\
\end{flushleft}
% You can tweak the spacing to have professor and student names on the same line
% useful if the page is broken by a long thesis title and you need more space
\vspace{-55pt}
\begin{flushright}
\textit{Candidate}\\
\vspace{5pt}
\myName
\end{flushright}
\end{large}
%% \vspace{40pt}
\line(1, 0){\textwidth} \\
\begin{normalsize}
\textsc{Academic Year \myAA}
\end{normalsize}
\end{center}
\end{titlepage}
\cleardoublepage
\phantomsection
\pdfbookmark{Abstract}{Abstract}
\begingroup
\chapter*{Abstract}
In theoretical computer science, program logics are essential for verifying the
correctness of software. Hoare logic provides a systematic way of reasoning
about program correctness using preconditions and postconditions. This thesis
explores the development and application of an abstract Hoare-like logic framework
that generalizes the traditional Hoare program logic by using arbitrary elements of
complete lattices as the assertion language, extrapolating what makes Hoare
logic sound and complete. We also demonstrate the practical applications of
this framework by systematically deriving a program logic for hyperproperties, thus highlighting
versatility and benefits of our general framework. From the design of Abstract Hoare logic, we
then define Reverse Abstract Hoare logic, which is used to develop a proof system for
backward correctness reasoning on programs.
\vfill
\cleardoublepage
\phantomsection
\pdfbookmark{Acknowledgments}{Acknowledgments}
\begingroup
\chapter*{Acknowledgments}
Innanzitutto, vorrei ringraziare i miei relatori, Prof. Francesco Ranzato e
Prof. Paolo Baldan, per la paziente supervisione durante lo sviluppo di questa
tesi e per i preziosissimi consigli. Vorrei ringraziarli anche, assieme alla
Prof.ssa Maria Emilia Maietti e al Prof. Ingo Blechschmidt, per avermi fatto
scoprire quanto sia profonda e bella l'informatica.
\\
Un grazie speciale va a tutti gli amici che hanno reso indimenticabili i miei
cinque anni a Padova. Arrivando in questa città il primo giorno senza conoscere
nessuno, non avrei mai immaginato di trovare qui una seconda casa grazie a voi.
Ogni momento trascorso insieme è stato un capitolo prezioso e divertente della
mia esperienza universitaria. Anche se le nostre strade si separeranno e ci
porteranno in posti diversi, sono certo che l'amicizia che abbiamo formato
durerà per sempre.
\\
Also, I would like to thank all the friends I made during my exchange in
Amsterdam, who made me feel at home in a place where I did not know anyone.
Thank you for all the beautiful experiences we had together. See you around the
world.
\\
Infine, vorrei dedicare un sentito ringraziamento alla mia famiglia, in
particolare ai miei genitori. Il vostro sostegno incondizionato e la vostra
fiducia in me sono stati la spinta costante che mi ha motivato a perseguire i
miei sogni. I sacrifici che avete fatto per farmi studiare e crescere sono
inestimabili, e questa tesi è il risultato tangibile del vostro amore e
supporto. Dedico a voi questa tesi.
\bigskip
Alessio Ferrarini
\begin{textblock}{1}[1,1](8,4)
\epigraph{\textit{``Progress is possible only if we train ourselves to think
about programs without thinking of them as pieces of executable code.``}}
{Edsger W. Dijkstra}
\end{textblock}
\endgroup
\cleardoublepage
%% TOC
\cleardoublepage
\begingroup
\hypersetup{linkcolor=black}
\tableofcontents
\endgroup
%% Structure
\cleardoublepage
\mainmatter
% This file only puropouse is to organize the chapters
% import the mod file from each directory, it will tell how the
% chapter is organized
\chapter*{Introduction}
The verification of program correctness is a critical and crucial task in
computer science. Ensuring that software behaves as expected under all possible
conditions is fundamental in a society that increasingly relies on computer
programs. Software engineers often reason about the behavior of their programs
at an intuitive level. While this is definitely better than not reasoning at
all, intuition alone becomes insufficient as the size of programs grows.
Writing tests for programs is definitely a useful task, but at best, it can
show the presence of bugs, not prove their absence. We cannot feasibly write
tests for every possible input of the program. To offer a guarantee of the
absence of undesired behaviors, we need sound logical models rooted in logic.
The field of formal methods in computer science aims at developing the logical
tools necessary to prove properties of software systems.
Hoare logic, first introduced by Hoare in the late 60s \cite{Hoare69}, provides
a set of logical rules to reason about the correctness of computer programs.
Hoare logic formalizes, with axioms and inference rules, the relationship
between the initial and final states after executing a program.
Hoare logic, beyond being one of the first program logics, is arguably also one
of the most influential ideas in the field of software verification. It created
the whole field of program logics—systems of logical rules aimed at proving
properties of programs. Over the years, modifications of Hoare logic have been
developed, sometimes to support new language features such as dynamic memory
allocation and pointers, or to prove different properties such as equivalence
between programs or properties of multiple executions. Every time Hoare logic
is modified, it is necessary to prove again that the proof system indeed proves
properties about the program (soundness) and ideally that the proof system is
powerful enough to prove all the properties of interest (completeness).
Most modifications of Hoare logic usually do not alter the fundamental proof
principles of the system. Instead, they often extend the assertion language to
express new properties and add new commands to support new features in
different programming languages.
In this work, we introduce Abstract Hoare Logic, which aims to be a framework
general enough to serve as an extensible platform for constructing new
Hoare-like logics without the burden of proving soundness and completeness
anew. We demonstrate, through examples, how some properties that are not
expressible in standard Hoare logic can be simply instantiated within Abstract
Hoare Logic, while keeping the proof system as simple as possible.
The theory of Abstract Hoare Logic is deeply connected to the theory of
abstract interpretation \cite{Cousot77}. The semantics of the language is
defined as an inductive abstract interpreter, and the validity of the Abstract
Hoare triples depends on it. Since we do not use the strongest postcondition
directly, we are able to reason about properties that are not expressible in
the powerset of the program states, such as hyperproperties.
This thesis is structured as follows:
\begin{itemize}
\item In Chapter 1, we introduce the basic mathematical background of order
theory and abstract interpretation.
\item In Chapter 2, we introduce standard Hoare logic and the general
framework of Abstract Hoare Logic: the extensible language $\lang$, its
syntax and semantics, the generalization of the strongest postcondition,
and finally, Abstract Hoare Logic and its proof system, proving the general
results of soundness and relative completeness.
\item In Chapter 3, we show some notable instantiations of Abstract Hoare
Logic: we demonstrate that it is possible to obtain program logics where
the implication is decidable, thus making the goal of checking a derivation
computable; we show how to obtain a proof system for hyperproperties (and
we introduce the concept of the strongest hyper postcondition); finally, we
show that it is possible to obtain a proof system for partial
incorrectness.
\item In Chapter 4, we show how to enrich the barebones proof system of
Abstract Hoare Logic by adding more restrictions on the assertion language
or the semantics.
\item In Chapter 5, we show how to reuse the idea of Abstract Hoare Logic to
generalize proof systems for backward reasoning.
\item In Chapter 6, we provide a brief summary of the most important
contributions of the thesis. We discuss possible extensions to the
framework of Abstract Hoare Logic and, to conclude, we examine the
relationship of Abstract Hoare Logic with other similar work.
\end{itemize}
\chapter{Background}
\section{Order theory}\label{sec:backround:order_theory}
When defining the semantics of programming languages, the theory of
\textit{partially ordered sets} and \textit{lattices} is fundamental
\cite{Gratzer11, Birkhoff40}. These
concepts are at the core of denotational semantics \cite{Scott70} and
\textit{Abstract Interpretation} \cite{Cousot77}, where the semantics of
programming languages and abstract interpreters are defined as monotone
functions over some complete lattice.
\subsection{Partial Orders}
\begin{definition}[Partial order]
A partial order on a set $X$ is a relation $\leq \subseteq X \times X$
such that the following properties hold:
\begin{itemize}
\item Reflexivity: $\forall x \in X, \; (x, x) \in \; \leq$
\item Anti-symmetry: $\forall x, y \in X, \; (x, y) \in \; \leq \mand
(y, x) \in \; \leq \implies x = y$
\item Transitivity: $\forall x, y, z \in X, \; (x, y) \in \; \leq \mand
(y, z) \in \; \leq \implies (x, z) \in \;\leq$
\end{itemize}
\end{definition}
Given a partial order $\leq$, we will use $\geq$ to denote the converse
relation $\{ (y, x) \mid (x, y) \in \;\leq \}$ and $<$ to denote
$\{ (x, y) \mid (x, y) \in \;\leq \; \text{and} \; x \neq y \}$.
From now on we will use the notation $x R y$ to indicate $(x, y) \in R$.
\begin{definition}[Partially ordered set]
A partially ordered set (or poset) is a pair $(X, \leq)$ in which $\leq$ is a
partial order on $X$.
\end{definition}
We will use partially ordered sets to encode collections of program states.
\begin{definition}[Galois connection]
Let $(C, \sqsubseteq)$ and $(A, \leq)$ be two partially ordered sets, a
Galois connection written $\langle C, \sqsubseteq \rangle
\galois{\alpha}{\gamma} \langle A, \leq \rangle$, are a pair of functions:
$\gamma : A \to D$ and $\alpha : D \to A$ such that:
\begin{itemize}
\item $\gamma$ is monotone
\item $\alpha$ is monotone
\item $\forall c \in C$ $c \sqsubseteq \gamma(\alpha(c))$
\item $\forall a \in A$ $a \leq \alpha(\gamma(a))$
\end{itemize}
\end{definition}
In the context of program analysis, it is common to refer to $C$ as the concrete
domain and to $A$ as the abstract domain, as the idea behind the use of Galois
connections is to relate $C$ to a simpler and approximate representation of
itself.
\begin{definition}[Galois Insertion]
Let $\langle C, \sqsubseteq \rangle \galois{\alpha}{\gamma} \langle A, \leq
\rangle$, be a Galois connection, a Galois insertion written
$\langle C, \sqsubseteq \rangle \galoiS{\alpha}{\gamma} \langle A, \leq \rangle$
are a pair of functions: $\gamma : A \to D$ and $\alpha : D \to A$ such that:
\begin{itemize}
\item $(\gamma, \alpha)$ are a Galois connection
\item $\alpha \circ \gamma = id$
\end{itemize}
\end{definition}
Galois insertions are a refinement of Galois connections. By requiring $\alpha
\circ \gamma = id$, we are ensuring that all the abstract elements $a \in A$
have distinct concrete representations. It is always possible to obtain a
Galois insertion from a Galois connection by simply removing all the elements
$a$ such that $\alpha(\gamma(a)) \neq a$ from $A$.
\subsection{Lattices}
\begin{definition}[Meet-semilattice]
A meet-semilattice is a partially ordered set \((L, \leq)\) such that for
every pair of elements \(a, b \in L\), there exists an element \(c \in L\)
satisfying the following conditions:
\begin{enumerate}
\item \(c \leq a\) and \(c \leq b\)
\item \(\forall d \in L\), if \(d \leq a\) and \(d \leq b\), then \(d \leq c\)
\end{enumerate}
The element \(c\) is called the \emph{meet} of \emph{greatest lower bound} of
\(a\) and \(b\), and is denoted by \(a \wedge b\).
\end{definition}
\begin{definition}[Join-semilattice]
A join-semilattice is a partially ordered set \((L, \leq)\) such that for
every pair of elements \(a, b \in L\), there exists an element \(c \in L\)
satisfying the following conditions:
\begin{enumerate}
\item \(c \geq a\) and \(c \geq b\)
\item \(\forall d \in L\), if \(d \geq a\) and \(d \geq b\), then \(d \geq c\)
\end{enumerate}
The element \(c\) is called the \emph{join} or \emph{least upper bound} of
\(a\) and \(b\), and is denoted by \(a \join b\).
\end{definition}
\begin{observation}
Both join and meet operations are idempotent, associative, and commutative.
\end{observation}
\begin{definition}[Lattice]
A poset $(L, \leq)$ is a lattice if it is both a join-semilattice and a
meet-semilattice.
\end{definition}
\begin{definition}[Complete lattice]
A partially ordered set \((L, \leq)\) is called a \emph{complete lattice} if
for every subset \(S \subseteq L\), there exist elements \(\sup S\) and
\(\inf S\) in \(L\) such that:
\begin{enumerate}
\item \(\sup S\) (the supremum or least upper bound of \(S\)) is an
element of \(L\) satisfying:
\begin{itemize}
\item For all \(s \in S\), \(s \leq \sup S\).
\item For any \(u \in L\), if \(s \leq u\) for all \(s \in S\), then
\(\sup S \leq u\). \end{itemize}
\item \(\inf S\) (the infimum or greatest lower bound of \(S\)) is an
element of \(L\) satisfying:
\begin{itemize}
\item For all \(s \in S\), \(\inf S \leq s\).
\item For any \(l \in L\), if \(l \leq s\) for all \(s \in S\), then
\(l \leq \inf S\).
\end{itemize}
\end{enumerate}
We denote the \textit{least element} or \textit{bottom} as $\bot = \inf \; L$
and the \textit{greatest element} or \textit{top} as $\top = \sup \; L$.
\end{definition}
\begin{observation}
A complete lattice cannot be empty, since it must contain at least
$\sup \emptyset$.
\end{observation}
\begin{definition}[Point-wise lifting]
Given a complete lattice $(L, \leq)$ and a set $A$, the set of all functions
from $A$ to $L$, denoted $L^A$, is usually called the \textit{point-wise
lifting} of $L$.
$(L^A, \sqsubseteq)$ is a complete lattice where $f \sqsubseteq g \iff \forall
a \in A \; f(a) \leq g(a)$.
\end{definition}
\subsection{Fixpoints}
\begin{definition}[Fixpoint]
Given a function $f : X \to X$, a fixpoint of $f$ is an element $x \in X$
such that $x = f(x)$.
We denote the set of all fixpoints of a function as $\fix(f) =
\{ x \mid x \in X \mand x = f(x) \}$.
\end{definition}
Fixpoints of function will be used to describe the semantics of
programs therefore we are interested in which conditions they exists.
\begin{definition}[Monotone function]
Given two ordered sets $(X, \leq)$ and $(Y, \sqsubseteq)$, a function
$f : X \to Y$ is said to be monotone if $x \leq y \implies f(x) \sqsubseteq
f(y)$.
\end{definition}
The representation of program semantics as functions falls naturally in the
category of monotone functions, as different executions of the program are not
supposed to influence each other. If the semantics is not monotone, that would
mean that the program is able to distinguish between the set of executions $x$
and $y$ and perform different actions.
\begin{definition}[Least and Greatest fixpoints]
Given a function $f : X \to X$,
\begin{itemize}
\item We denote the \textit{least fixpoint} as $\lfp(f)$ and is defined as
$lfp(f) = a^* \in \fix(f)$ and $\forall a \in \fix(f) \; a^* \leq a$.
\item We denote the \textit{greatest fixpoint} as $\gfp(f)$ and is defined as
$gfp(f) = a^* \in \fix(f)$ and $\forall a \in \fix(f) \; a^* \geq a$.
\end{itemize}
\end{definition}
\begin{observation}[Point-wise fixpoint]
\label{th:pointfix}
The least-fixpoint and greatest fixpoint on some point-wise lifted lattice on
a monotone function defined point-wise is the point-wise lift of the function.
$$\lfp(\lambda p' a . f(p'(a))) = \lambda a . \lfp(\lambda p' . f(a))$$
$$\gfp(\lambda p' a . f(p'(a))) = \lambda a . \gfp(\lambda p' . f(a))$$
\end{observation}
\begin{theorem}[Knaster-Tarski theorem]
\label{thm:knaster}
Let $(L, \leq)$ be a complete lattice and let $f : L \to L$ be a monotone
function. Then $(\text{fix}(f), \leq)$ is also a complete lattice.
\end{theorem}
We have two direct consequences: both the greatest and the least fixpoint of
$f$ exists as they are respectively top and bottom of $\fix(f)$.
\begin{theorem}[Post-fixpoint inequality]
\label{thm:post-lfp}
Let $f$ be a monotone function on a complete lattice then
$$f(x) \leq x \implies \lfp(f) \leq x$$
\end{theorem}
\begin{proof}
By theorem \ref{thm:knaster} $\lfp(f) = \bigwedge\{ y \mid y \geq f(y) \}$
thus $\lfp(f) \leq x$ since $x \in \{ y \mid y \geq f(y) \}$.
\end{proof}
\begin{theorem}[$\lfp$ monotonicity]
\label{thm:lfp-mono}
Let $L$ be a complete lattice, if $P \leq Q$ and $f$ is monotone then
$$\lfp(\lambda X. P \join f(X)) \leq \lfp(\lambda X. Q \join f(X))$$
\end{theorem}
\begin{proof}
\begin{align*}
P \join f(\lfp(\lambda X . Q \join f(X)))
&\leq Q \join f(\lfp(\lambda X . Q \join f(X)))
&\text{[Since $P \leq Q$]} \\
&= \lfp(\lambda X . Q \join f(X))
&\text{[By definition of fixpoint]} \\
\end{align*}
Thus by Theorem \ref{thm:post-lfp} pick $f = \lambda X . P \join f(X)$ and
$x = \lfp(\lambda X. Q \join f(X))$ it follows that
$\lfp(\lambda X. P \join f(X)) \leq \lfp(\lambda X. Q \join f(X))$.
\end{proof}
\section{Abstract Interpretation}
Abstract interpretation \cite{Cousot77, Cousot21} is the de-facto standard
approach for designing static program analysis. Fixed some representation of the
state of the program usually denoted by $\states$, the specification of a
program can be expressed as a pair of initial and final sets of states,
$\mathit{Init}, \mathit{Final} \in \pow{\states}$, and the task of verifying a
program $C$ boils down to checking if $\sem{C}(\mathit{Init}) \subseteq
\mathit{Final}$.
Clearly, this task cannot be performed in general. The solution proposed by the
framework of abstract interpretation is to construct an approximation of
$\sem{\cdot}$, usually denoted by $\sem{\cdot}^\#$, that is computable.
\subsection{Abstract Domains}
One of the techniques used by abstract interpretation to make the problem of
verification tractable involves representing collections of states with a finite
amount of memory.
\begin{definition}[Abstract Domain]
A poset $(A, \leq)$ is an abstract domain of $\states$ if there exists a
Galois insertion $\langle \pow{\states}, \subseteq \rangle
\galoiS{\alpha}{\gamma} \langle A, \leq \rangle$.
\end{definition}
\begin{example}[Interval Domain]
\label{exmp:interval}
Let $\mathit{Int} = \{ [a, b] \mid a, b \in \mathbb{Z} \cup \{+\infty, -\infty \}, a
\leq b \} \cup \{ \bot \}$ be ordered by inclusion, each element $[a, b]$
represent the set $\{ x \mid a \leq x \leq b \}$ and $\bot$ is used as a
representation of $\emptyset$. The structure of the
lattice can be summarized by the following Hasse diagram:
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node [] (0) at (0, 0) {$\bot$};
\node [] (1) at (0, 1) {$[0, 0]$};
\draw (0) to (1);
\node [] (2) at (2, 1) {$[+1, +1]$};
\draw (0) to (2);
\node [] (3) at (4, 1) {$[+2, +2]$};
\draw (0) to (3);
\node [] (4) at (-2, 1) {$[-1, -1]$};
\draw (0) to (4);
\node [] (5) at (-4, 1) {$[-2, -2]$};
\draw (0) to (5);
\node [] (c1) at (+6, 1) {\color{black!30}$\dots$};
\node [] (c2) at (-6, 1) {\color{black!30}$\dots$};
\node [] (6) at (-3, 2) {$[-2, -1]$};
\draw (5) to (6);
\draw (4) to (6);
\node [] (7) at (-1, 2) {$[-1, -0]$};
\draw (4) to (7);
\draw (1) to (7);
\node [] (8) at (+1, 2) {$[0, 1]$};
\draw (2) to (8);
\draw (1) to (8);
\node [] (9) at (+3, 2) {$[1, 2]$};
\draw (2) to (9);
\draw (3) to (9);
\node [] (10) at (-2, 3) {$[-2, 0]$};
\draw (10) to (6);
\draw (10) to (7);
\node [] (11) at (0, 3) {$[-1, +1]$};
\draw (11) to (7);
\draw (11) to (8);
\node [] (12) at (2, 3) {$[0, +2]$};
\draw (12) to (8);
\draw (12) to (9);
\node [] (13) at (-1, 4) {$[-2, +1]$};
\draw (13) to (10);
\draw (13) to (11);
\node [] (14) at (1, 4) {$[-1, +2]$};
\draw (14) to (11);
\draw (14) to (12);
\node [] (15) at (0, 5) {$[-2, +2]$};
\draw (13) to (15);
\draw (14) to (15);
\node [] (16) at (-7, 3) {$[-\infty, -2]$};
\node [] (17) at (-6, 4) {$[-\infty, -1]$};
\draw (16) to (17);
\node [] (18) at (-5, 5) {$[-\infty, 0]$};
\draw (17) to (18);
\node [] (b1) at (-4, 4) {};
\draw [loosely dotted] (b1) to (18);
\node [] (b2) at (-5, 3) {};
\draw [loosely dotted] (b2) to (17);
\node [] (b3) at (-6, 2) {};
\draw [loosely dotted] (b3) to (16);
\node [] (19) at (+7, 3) {$[+2, +\infty]$};
\node [] (20) at (+6, 4) {$[+1, +\infty]$};
\draw (19) to (20);
\node [] (21) at (+5, 5) {$[0, +\infty]$};
\draw (20) to (21);
\node [] (a1) at (+4, 4) {};
\draw [loosely dotted] (a1) to (21);
\node [] (a2) at (+5, 3) {};
\draw [loosely dotted] (a2) to (20);
\node [] (a3) at (+6, 2) {};
\draw [loosely dotted] (a3) to (19);
\node (22) at (0, 8) {$[-\infty, +\infty]$};
\node (25) at (-5, 2) {};
\draw[loosely dotted] (25) to (5);
\node (26) at (+5, 2) {};
\draw[loosely dotted] (26) to (3);
\node (27) at (-4, 3) {};
\draw[loosely dotted] (27) to (6);
\node (28) at (+4, 3) {};
\draw[loosely dotted] (28) to (9);
\node (29) at (-3, 4) {};
\draw[loosely dotted] (29) to (10);
\node (30) at (+3, 4) {};
\draw[loosely dotted] (30) to (12);
\node (31) at (-2, 5) {};
\draw[loosely dotted] (31) to (13);
\node (32) at (+2, 5) {};
\draw[loosely dotted] (32) to (14);
\node (33) at (-1, 6) {};
\draw[loosely dotted] (33) to (15);
\node (34) at (1, 6) {};
\draw[loosely dotted] (34) to (15);
\node (35) at (0, 7) {};
\draw[loosely dotted] (35) to (22);
\node (a1) at (-1, 7) {};
\draw[loosely dotted] (a1) to (22);
\node (a2) at (+1, 7) {};
\draw[loosely dotted] (a2) to (22);
\node (36) at (-4, 6) {};
\draw[loosely dotted] (36) to (18);
\node (37) at (+4, 6) {};
\draw[loosely dotted] (37) to (21);
\node (38) at (-8, 2) {};
\draw[loosely dotted] (38) to (16);
\node (39) at (+8, 2) {};
\draw[loosely dotted] (39) to (19);
\end{tikzpicture}
\end{center}
Then, there is a Galois insertion from $\mathit{Int}$ to $\pow{\mathbb{Z}}$ defined as:
$$\gamma(A) = \begin{cases}
\{ x \mid a \leq x \leq b \} & \text{if } A = [a, b] \\
\emptyset & \text{otherwise}
\end{cases}$$
$$\alpha(C) = \begin{cases}
[\min C, \max C] & \text{if } C \neq \emptyset \\
\bot & \text{otherwise}
\end{cases}$$
\end{example}
\begin{example}[Complete sign domain]
\label{exmp:sign}
Let $Sign = \{ \bot , <0, >0, =0, \leq 0, \neq 0, \geq 0, \mathbb{Z}\}$ be
ordered by following the Hasse diagram below.
\begin{center}
\begin{tikzpicture}
\node [] (0) at (0, 0) {$\bot$};
\node [] (1) at (-1, 1) {$< 0$};
\draw (0) to (1);
\node [] (2) at (0, 1) {$=0$};
\draw (0) to (2);
\node [] (3) at (+1, 1) {$> 0$};
\draw (0) to (3);
\node [] (4) at (-1, 2) {$\leq 0$};
\draw (1) to (4);
\draw (2) to (4);
\node [] (5) at (0, 2) {$\neq 0$};
\draw (1) to (5);
\draw (3) to (5);
\node [] (6) at (+1, 2) {$\geq 0$};
\draw (2) to (6);
\draw (3) to (6);
\node [] (7) at (0, 3) {$\mathbb{Z}$};
\draw (6) to (7);
\draw (5) to (7);
\draw (4) to (7);
\end{tikzpicture}
\end{center}
Then, there is a Galois insertion from $Sign$ to $\pow{\mathbb{Z}}$ defined as:
$$\gamma(A) = \begin{cases}
\{ x \mid x \;op\; 0 \} & \text{if } A = op\;0 \\
\mathbb{Z} & \text{if } A = \mathbb{Z} \\
\emptyset & \text{otherwise}
\end{cases}$$
$$\alpha(C) = \begin{cases}
\bot & \text{if } C = \emptyset \\
op \; 0 & \text{if } C \subseteq \{ x \mid x \; op \; 0 \}
\mand op \in \{<, >, =, \leq, \geq, \neq \} \\
\mathbb{Z} & \text{otherwise}
\end{cases}$$
\end{example}
The fundamental goal of abstract interpretation is to provide an approximation
of the non-computable aspects of program semantics. The core concept is captured
by the definition of soundness:
\begin{definition}[Soundness]
Given an abstract domain $A$, an abstract function $f^\# : A \to A$ is a
sound approximation of a concrete function $f : \pow{\states} \to
\pow{\states}$ if
$$\alpha(f(P)) \leq f^\#(\alpha(P))$$
\end{definition}
Hence, the goal of abstract interpretation is to construct a sound
over-approximation of the program semantics that is computable (efficiently).
\begin{example}
We can use the sign domain to construct a sound approximation of the
multiplication operation:
\begin{table}[h!]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
$\times^\#$ & $\bot$ & $< 0$ & $> 0$ & $= 0$ & $\le 0$ & $\ne 0$ & $\ge 0$
& $\mathbb{Z}$ \\
\hline
$\bot$ & $\bot$ & $\bot$ & $\bot$ & $\bot$ & $\bot$ & $\bot$ & $\bot$
& $\bot$ \\
\hline
$< 0$ & $\bot$ & $> 0$ & $< 0$ & $= 0$ & $< 0$ & $\ne 0$ & $\le 0$
& $\mathbb{Z}$ \\
\hline
$> 0$ & $\bot$ & $< 0$ & $> 0$ & $= 0$ & $\le 0$ & $\ne 0$ & $\ge 0$
& $\mathbb{Z}$ \\
\hline
$= 0$ & $\bot$ & $= 0$ & $= 0$ & $= 0$ & $= 0$ & $= 0$ & $= 0$ & $= 0$ \\
\hline
$\le 0$ & $\bot$ & $< 0$ & $\le 0$ & $= 0$ & $\le 0$ & $\ne 0$ & $\le 0$
& $\mathbb{Z}$ \\
\hline
$\ne 0$ & $\bot$ & $\ne 0$ & $\ne 0$ & $= 0$ & $\ne 0$ & $\ne 0$
& $\ne 0$ & $\mathbb{Z}$ \\
\hline
$\ge 0$ & $\bot$ & $\le 0$ & $\ge 0$ & $= 0$ & $\le 0$ & $\ne 0$
& $\ge 0$ & $\mathbb{Z}$ \\
\hline
$\mathbb{Z}$ & $\bot$ & $\mathbb{Z}$ & $\mathbb{Z}$ & $= 0$
& $\mathbb{Z}$ & $\mathbb{Z}$ & $\mathbb{Z}$ & $\mathbb{Z}$ \\
\hline
\end{tabular}
\caption{Multiplication table for $Sign$ domain}
\end{table}
\end{example}
\chapter{The abstract Hoare logic framework}
In this chapter, we will develop the basic theory of \textit{Abstract Hoare
Logic}. We will formalize the extensible language $\lang$, a minimal imperative
programming language that is parametric on a set of basic commands to permit
the definition of arbitrary program features, such as pointers, objects, etc.
We will define the semantics of the language, provide the standard definition
of Hoare triples, and introduce the concept of abstract inductive semantics; a
modular approach to express the strongest postcondition of a program, where the
assertion language is a complete lattice. Additionally, we will present a sound
and complete proof system to reason about these properties.
\section{The $\lang$ programming language}
\subsection{Syntax}
The $\lang$ language is inspired by Dijkstra's guarded command languages
\cite{Dijkstra74} with the goal of being as general as possible by being
parametric on a set of \textit{basic commands}. The $\lang$ language is general
enough to describe any imperative non-deterministic programming language.
\begin{definition}[$\lang$ language syntax]
Given a set $\mathit{BCmd}$ of basic commands, the set on valid $\lang$ programs is
defined by the following inductive definition:
\begin{align*}
b \in \mathit{BCmd} \\
\lang \ni C, C_1, C_2 \; & \cceq \sskip & \text{Skip}\\
& \smid b & \text{Basic command}\\
& \smid C_1 \fcmp C_2 & \text{Program composition}\\
& \smid C_1 + C_2 & \text{Non deterministic choice}\\
& \smid C^\fix & \text{Iteration}\\
\end{align*}
\end{definition}
\begin{example} \label{exmp:base-commands-syntax}
Usually the set of basic commands contains a command to perform tests $e ?$
discarding executions that do not satisfy the predicate $e$, and $x \ass v$
to assign the value $v$ to the variable $x$.
\end{example}
\subsection{Semantics}
Fixed a set $\states$ of states (usually a collection of associations between
variables names and values) and a family of partial functions $\bsem{\cdot} :
\mathit{BCmd} \to \states \topartial \states$ we can define the denotational semantics
of programs in $\lang$. The \textit{collecting semantics} is defined as a function
$\sem{\cdot} : \lang \to \pow{\states} \to \pow{\states}$ that associates a
program $C$ and a set of initial states to the set of states reached after
executing the program $C$ from the initial states, this is also know as the
predicate transformer semantics \cite{Dijkstra74}.
\begin{definition}[Denotational semantics]
\label{def:deno}
Given a set $\states$ of states and a family of partial functions
$\bsem{\cdot} : \mathit{BCmd} \to \states \topartial \states$ the denotational
semantics is defined as follows:
\begin{align*}
\sem{\cdot} & \;\;:\; \lang \to \pow{\states} \to \pow{\states} \\
\sem{\sskip} &\defeq id \\
\sem{b} &\defeq \lambda P . \{ \bsem{b}(p)\; \mid
p \in P \mand \bsem{b}(p) \downarrow\} \\
\sem{C_1 \fcmp C_2} &\defeq \sem{C_2} \circ \sem{C_1} \\
\sem{C_1 + C_2} &\defeq \lambda P . \sem{C_1} P \cup \sem{C_2} P \\
\sem{C^\fix} &\defeq \lambda P . \lfp(\lambda P'. P \cup \sem{C} P')
\end{align*}
Where the notation $\bsem{b}(p)\downarrow$ is used to denote that $\bsem{b}$
is defined on input $p$.
\end{definition}
\begin{example}
We can define the semantics of the basic commands introduced in
\ref{exmp:base-commands-syntax} as:
$$\bsem{e ?}(\sigma) \defeq \begin{cases}
\; \sigma & \sigma \models e \\
\; \uparrow & otherwise
\end{cases}$$
Where $\sigma \models e$ means that the state $\sigma$ satisfies the
predicate $e$ and $\uparrow$ is denoting that the function is diverging.
$$\bsem{x \ass e}(\sigma) \defeq \sigma[eval(e, \sigma)/x]$$
Where $eval$ is some evaluate function for the expressions on the left-hand
side of assignments and then is substitute in place of $x$ in the
state $\sigma$.
\end{example}
\begin{theorem}[Monotonicity]
\label{thm:sem-mono}
$\forall \; C \in \lang$ $\sem{C}$ is well-defined and monotone.
\end{theorem}
\begin{proof}
We want to prove that $\forall P, Q \in \pow{\states}$ and $C \in \lang$
$$P \subseteq Q \implies \sem{C}(P) \subseteq \sem{C}(Q)$$
By structural induction on $C$:
\begin{itemize}
\item $\sskip$:
\begin{align*}
\sem{\sskip}(P)
&= P
& \text{[By definition of $\sem{\sskip}$]}\\
&\subseteq Q \\
&= \sem{\sskip}(Q)
& \text{[By definition of $\sem{\sskip}$]}\\
\end{align*}
\item $b$:
\begin{align*}
\sem{b}(P)
&= \{ \bsem{b}(x)\downarrow \; \mid x \in P \}
& \text{[By definition of $\sem{b}$]}\\
&\subseteq \{ \bsem{b}(x)\downarrow \; \mid x \in Q \}
& \text{[Since $P \subseteq Q$]}\\
&= \sem{b}(Q)
& \text{[By definition of $\sem{b}$]}\\
\end{align*}
\item $C_1 \fcmp C_2$:
By inductive hypothesis $\sem{C_1}$ is monotone hence
$\sem{C_1}(P) \subseteq \sem{C_2}(Q)$
\begin{align*}
\sem{C_1 \fcmp C_2}(P)
&= \sem{C_2}(\sem{C_1}(P))
&\text{[By definition of $\sem{C_1 \fcmp C_2}$]}\\
&\subseteq \sem{C_2}(\sem{C_1}(Q))
&\text{[By inductive hypothesis on $\sem{C_2}$]} \\
\end{align*}
\item $C_1 + C_2$:
\begin{align*}
\sem{C_1 + C_2}(P)
&= \sem{C_1}(P) \cup \sem{C_2}(P)
&\text{[By definition of $\sem{C_1 + C_2}$]}\\
&\subseteq \sem{C_1}(Q) \cup \sem{C_2}(P)
&\text{[By inductive hypothesis on $\sem{C_1}$]} \\
&\subseteq \sem{C_1}(Q) \cup \sem{C_2}(Q)
&\text{[By inductive hypothesis on $\sem{C_2}$]} \\
&= \sem{C_1 + C_2}(Q)
&\text{[By definition of $\sem{C_1 + C_2}$]}\\
\end{align*}
\item $C^\fix$:
\begin{align*}
\sem{C^\fix}(P)
&\text{[By definition of $\sem{C^\fix}$]}\\
&= \lfp(\lambda P'. P \cup \sem{C}(P'))
&\subseteq \lfp(\lambda P'. Q \cup \sem{C}(P'))
&\text{[By Theorem \ref{thm:lfp-mono}]}\\
&= \sem{C^\fix}(Q)
&\text{[By definition of $\sem{C^\fix}$]}\\
\end{align*}
Clearly all the $\lfp$ are well-defined since by inductive hypothesis
$\sem{C}$ is monotone and $\pow{\states}$ is a complete from
\ref{thm:knaster} the least-fixpoint exists.
\end{itemize}
\end{proof}
\begin{observation}
As observed in \cite{Fischer79} when the set of basic commands contains a
command to discard executions we can define the usual deterministic control
flow commands as syntactic sugar.
$$\mathbf{if} \; b \; \mathbf{then} \; C_1 \; \mathbf{else} \; C_2 \defeq (b ? \fcmp C_1)
+ (\neg b ? \fcmp C_2)$$
$$\mathbf{while} \; b \; \mathbf{do} \; C \defeq (b? \fcmp C)^\fix \fcmp \neg b ?$$
\end{observation}
\begin{observation}
Regular languages of Kleene algebras \cite{Kozen97} usually provide an
iteration command usually denoted $C^\star$ whose semantics is
$\sem{C^\star}(P) \defeq \bigcup_{n \in \nat} \sem{C}^n(P)$. This is
equivalent to $C^\fix$, the reason why a fixpoint formulation was
chosen will become clear in \ref{obs:abstract-fix}.
\end{observation}
\begin{example}
Let $C \defeq (x \leq 10? \fcmp x := x + 1)^\fix + (x := 55)$ and
$P = \{ x = 1 \}$ then we can compute $\sem{C}(P)$ as:
\begin{align*}
\sem{C}(P)
&= \sem{(x \leq 10? \fcmp x := x +1)^\fix}(P) \cup \sem{x := 55}(P) \\
&= \lfp(\lambda P' . P \cup \sem{x \leq 10? \fcmp x := x + 1}(P'))
\cup \{x = 55\} \\
&= \{ x \in \{1, ..., 10\} \} \cup \{x = 55\} \\
&= \{ x \in \{1, ..., 10, 55\} \}
\end{align*}
\end{example}
\section{Abstract inductive semantics}
From the theory of abstract interpretation we know that the definition of the
denotational semantics can be modified to work on any complete lattice as long
as we provide suitable function for the basic commands. The rationale
behind is the same as in the denotational semantics but instead of representing