chiark / gitweb /
c9647ad7e6ea93d98c08f0177cb709df1c035328
[topbloke-formulae.git] / trav-alg.tex
1 \section{Traversal phase --- algorithm}
2
3 (In general, unless stated otherwise below, when we generate a new
4 commit $C$ using one of the commit kind recipies, we update
5 $W \assign C$.  In any such case where we say we're going to Merge
6 with $L = W$, if $R \ge W$ we do not Merge but instead simply set
7 $W \assign R$.
8
9 For each commit generation operation called for by the traversal
10 algorithms, we prove that the commit generation preconditions are met.)
11
12 For each patch $\pc \in \allpatches$ in topological order by $\hasdep$,
13 lowest first:
14
15 \begin{enumerate}
16
17 \item Optionally, attempt
18  $\alg{Merge-Base}(\pc)$.  This may or may not succeed.
19
20 \item If this didn't succeed, or was not attempted, execute
21  $\alg{Recreate-Base}(\pc)$.
22
23 \item Then in any case, execute
24  $\alg{Merge-Tip}(\pc)$.
25
26 \end{enumerate}
27
28 After processing each $\pc$ we will have created $\tipcn$ and $\tipcy$
29 such that:
30
31 \statement{Correct Base}{
32   \baseof{\tipcy} = \tipcn
33 }
34 \statement{Base Correct Contents}{
35   \tipcn \haspatch \pa E
36    \equiv
37   \pa E \isdep \pc
38 }
39 \statement{Tip Covers Reachable}{
40   \tipcy \ge \pendsof{\allreachof{\pcy}}{\pcy}
41 }
42 \statement{Base Covers Inputs' Bases}{
43   \bigforall_{E \in \pendsof{\allsrcs}{\pcy}} \tipcn \ge \baseof{E}
44 }
45 \statement{Base Covers Base Inputs}{
46   \bigforall_{H \in \set H^{\pn}} \tipcn \ge H
47 }
48
49 \subsection{Reachability and coverage}
50
51 We ensure Tip Covers Reachable as follows:
52
53 \begin{itemize}
54 \item  We do not generate any commits $\in \py$ other than
55        during $\alg{Merge-Tip}(\py)$;
56 \item  So at the start of $\alg{Merge-Tip}(\py)$,
57        $ \pendsof{\allreach}{\py} = \pendsof{\allsrcs}{\py} $
58 \item  $\alg{Merge-tip}$ arranges that when it is done
59        $\tippy \ge \pendsof{\allreach}{\py}$ --- see below.
60 \end{itemize}
61
62 A corrolary is as follows:
63 \statement{Tip Covers Superior Reachable} {
64   \bigforall_{\pd \isdep \pc}
65     \tipdy \ge \pendsof{\allreachof{\pcy}}{\pdy}
66 }
67 \proof{
68   No commits $\in \pdy$ are created other than during
69   $\alg{Merge-Tip}(\pd)$, which runs (and has thus completed)
70   before $\alg{Merge-Tip}(\pcy)$
71   So $\pendsof{\allreachof{\pcy}}{\pdy} =
72       \pendsof{\allreachof{\pdy}}{\pdy}$.
73 }
74
75 \subsection{Traversal Lemmas}
76
77 \statement{Tip Correct Contents}{
78   \tipcy \haspatch \pa E
79     \equiv
80   \pa E = \pc \lor \pa E \isdep \pc
81 }
82 \proof{
83   For $\pc = \pa E$, Tip Own Contents suffices.
84   For $\pc \neq \pa E$, Exclusive Tip Contents
85   gives $D \isin \tipcy \equiv D \isin \baseof{\tipcy}$
86   which by Correct Base $\equiv D \isin \tipcn$.
87 }
88
89 \subsection{$\alg{Merge-Base}(\pc)$}
90
91 This algorithm attempts to construct a suitably updated version of the
92 base branch $\pcn$ using some existing version of $\pcn$ as a starting
93 point.
94
95 It should be executed noninteractively.  Specifically, if any step
96 fails with a merge conflict, the whole thing should be abandoned.
97 This avoids asking the user to resolve confusing conflicts.  It also
98 avoids asking the user to pointlessly resolve conflicts in situations
99 where we will later discover that $\alg{Merge-Base}$ wasn't feasible
100 after all.
101
102 If $\pc$ has only one direct dependency, this algorithm should not be
103 used as in that case $\alg{Recreate-Base}$ is trivial and guaranteed
104 to generate a perfect answer, whereas this algorithm might involve
105 merges and therefore might not produce a perfect answer if the
106 situation is complicated.
107
108 For \alg{Merge-Base} we do not prove that the preconditions are met.
109 Instead, we check them at runtime.  If they turn out not to be met, we
110 abandon \alg{Merge-Base} and resort to \alg{Recreate-Base}.
111
112 Initially, set $W \iassign W^{\pcn}$.
113
114 \subsubsection{Bases and sources}
115
116 In some order, perhaps interleaving the two kinds of merge:
117
118 \begin{enumerate}
119
120 \item For each $\hasdep$-maximal $\pd \isdirdep \pc$, find a merge base
121 $M \le W,\; \le \tipdy$ and merge $\tipdy$ into $W$.
122 That is, use $\alg{Merge}$ with $L = W,\; R = \tipdy$.
123
124 \item For each $S \in S^{\pcn}_i$, merge it into $W$.
125 That is, use $\alg{Merge}$ with $L = W,\; R = S,\; M = M^{\pcn}_i$.
126 (Base Sibling Merge.)
127
128 \end{enumerate}
129
130 \subsubsection{Fixup}
131
132 Execute $\alg{Fixup-Base}(W,\pc)$.
133
134 TODO define Fixup-Base
135
136 \subsubsection{Result}
137
138 If all of that was successful, let $\tipcn = W$.
139
140 \subsection{$\alg{Recreate-Base}(\pc)$}
141
142 \begin{enumerate}
143
144 \item
145
146 Choose a $\hasdep$-maximal direct dependency $\pd$ of $\pc$.
147
148 \item
149
150 Use $\alg{Create Base}$ with $L$ = $\tipdy,\; \pq = \pc$ to generate $C$
151 and set $W \iassign C$.
152
153  \commitproof{
154   Create Acyclic: by Tip Correct Contents of $L$,
155   $L \haspatch \pa E \equiv \pa E = \pd \lor \pa E \isdep \pd$.
156   Now $\pd \isdirdep \pc$,
157   so by Coherence, and setting $\pa E = \pc$,
158   $L \nothaspatch \pc$. I.e. $L \nothaspatch \pq$.  OK.
159
160   That's everything for Create Base.
161  }
162
163 \item
164
165 Execute the subalgorithm $\alg{Recreate-Recurse}(\pc)$.
166
167 \item
168
169 Declare that we contain all of the relevant information from the
170 sources.  That is, use $\alg{Pseudo-Merge}$ with $L = W, \;
171 \set R = \{ W \} \cup \set S^{\pcn}$.
172
173  \commitproof{
174   Base Only: $\patchof{W} = \patchof{L} = \pn$.  OK.
175
176   Unique Tips:
177   Want to prove that for any $\p \isin C$, $\tipdy$ is a suitable $T$.
178   WIP TODO
179
180   WIP TODO INCOMPLETE
181  }
182
183 \end{enumerate}
184
185 \subsubsection{$\alg{Recreate-Recurse}(\pd)$}
186
187 \begin{enumerate}
188
189 \item Is $W \haspatch \pd$ ?  If so, there is nothing to do: return.
190
191 \item TODO what about non-Topbloke base branches
192
193 \item For all $\hasdep$-maximal $\pd' \isdirdep \pd$,
194 execute $\alg{Recreate-Recurse}(\pd')$.
195
196 \item Use $\alg{Pseudo-Merge}$ with $L = W,\; \set R = \{ \tipdn \}$.
197 (Recreate Base Dependency Base Declaration.)
198
199 \item Use $\alg{Merge}$ to apply $\pd$ to $W$.  That is,
200 $L = W, \; R = \tipdy, \; M = \baseof{R} = \tipdn$.
201 (Recreate Reapply.)
202
203 \end{enumerate}
204
205
206 \subsection{$\alg{Merge-Tip}(\pc)$}
207
208 \begin{enumerate}
209
210 \item TODO CHOOSE/REFINE W AND S as was done during Ranking for bases
211
212 \item $\alg{Merge}$ from $\tipcn$.  That is, $L = W, \;
213 R = \tipcn$ and choose any suitable $M$.
214
215  \commitproof{
216   $L = W$, $R = \tipcn$.
217   TODO TBD
218
219   Afterwards, $\baseof{W} = \tipcn$.
220  }
221
222 \item For each source $S \in \set S^{\pcy}$,
223 $\alg{Merge}$ with $L = W, \; R = S$ and any suitable $M$.
224
225  \commitproof{
226   In fact, we do this backwards: $L = S$, $R = W$.
227   Since $S \in \pcy$,
228   the resulting $C \in \pcy$ and the remaining properties of the Merge
229   commit construction are symmetrical in $L$ and $R$ so this is fine.
230
231   By the results of Tip Base Merge, $\baseof{W} = \tipcn$.
232
233   By Base Ends Supreme, $\tipcn \ge \baseof{S}$ i.e.
234   $\baseof{R} \ge \baseof{L}$.
235
236   Either $\baseof{L} = \baseof{M}$, or we must choose a different $M$ in
237   which case $M = \baseof{S}$ will suffice.
238  }
239
240 \end{enumerate}