\chapter{Input}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Configuration file}
The configuration file is a fixed format file. Its name is the runnanme
given on the commandline, plus the ending ``.in''.
The first three lines are comments and ignored. The other lines have
one parameter per line with the parameter as first part, the rest is
ignored. The order is fixed. Below an example is given.
\begin{verbatim}
0.64                s                  - Radial coordinate s
0.036               M_t                - Mach number (for single Mach no. run)
1d0                 qi                 - Particle charge / elem. charge
2.014               mi                 - Particle mass / 1u
1d19                n0                 - particle density
40000000.0          vth                - thermal velocity
1.0                 epsmn              - perturbation amplitude B1/B0 (if pertfile==F)
0                   m                  - poroidal perturbation mode (if pertfile==F)
3                   n                  - toroidal perturbation mode (if pertfile==F, n>0!)
0                   mth                - poloidal canonical mode (for single Mach no. run)
10                  mthnum             - poloidal canonical mode count
-1.0d-1             Mtmin              - minimum Mach number
+1.0d-1             Mtmax              - maximum Mach number
21                  Mtnum              - Mach number count
F                   supban             - calculate superbanana plateau
T                   magdrift           - consider magnetic drift
F                   nopassing          - neglect passing particles
F                   calcflux           - calculate flux directly instead via flux-force relation
F                   noshear            - neglect magnetic shear term with dqds
F                   pertfile           - read perturbation from file with do_magfie_pert
F                   odeint             - use ODE integrator for resonance line
F                   nonlin             - do nonlinear calculation
1.0                 bscale             - scale B field by factor
1.0                 escale             - scale E field by factor
8                   inp_swi            - input switch for Boozer file
0                   orbit_mode_avg     - orbit mode for bounce averaging
0                   orbit_mode_transp  - orbit mode for transport computation
0                   vsteps             - integration steps in velocity space
F                   comptorque         - do torque computation
F                   intoutput          - output details of integrand
\end{verbatim}
The naming of the parameter, and the description is not required, but
for documentation purposes it is recomended to keep them.

\paragraph{s}
Radial coordinate of the flux surface. Note that figures from
publications might use inverse aspect ratio $\epsilon = 1/A$, with
aspect ratio $A$, as radial coordinate. In ``*__magfie_param.out'' all
three values are given (earlier versions might omit ``s'').

\paragraph{M\_t}
The toroidal Mach number. Is only used if ``Mtnum'' is set to $1$.

\paragraph{qi}
Particle charge in units of elementary charge.

\paragraph{mi}
Particle mass in dalton (approximately one proton mass).

\paragraph{n0}
Particle density.

\paragraph{vth}
Thermal velocity.

\paragraph{epsmn}
Perturbation amplitude $\epsilon_{M} = B1/B0$ (if pertfile==F).
Do not mix this up with inverse aspect ratio $\epsilon$.

\paragraph{m}
Poloidal perturbation mode (if pertfile==F)

\paragraph{n}
Toroidal perturbation mode (if pertfile==F, n>0!)

\paragraph{mth}
Poloidal canonical mode (for single Mach no. run)

\paragraph{mthnum}
Poloidal canonical mode count

\paragraph{Mtmin}
Minimum Mach number. See also ``Mtnum''.

\paragraph{Mtmax}
Maximum Mach number. See also ``Mtnum''.

\paragraph{Mtnum}
Mach number count. If equal to one, then the value from ``M\_t'' is
used. For larger values, an equidistant grid from ``Mtmin'' to ``Mtmax''
(including boundaries) is created, and the calculation is done for all
Mach numbers.

\paragraph{supban}
Calculate superbanana plateau.

\paragraph{magdrift}
Consider magnetic drift.

\paragraph{nopassing}
Neglect passing particles.

\paragraph{calcflux}
Calculate flux directly instead via flux-force relation.

\paragraph{noshear}
If true, then neglect magnetic shear term with dqds.

\paragraph{pertfile}
Read perturbation from file with do\_magfie\_pert.

\paragraph{odeint}
Use ODE integrator for resonance line.

\paragraph{nonlin}
Do nonlinear calculation.

\paragraph{bscale}
Scale B field by factor

\paragraph{escale}
Scale E field by factor.

\paragraph{inp\_swi}
Input switch for Boozer file.

\paragraph{orbit\_mode\_avg}
Orbit mode for bounce averaging.

\paragraph{orbit\_mode\_transp}
Orbit mode for transport computation.

\paragraph{vsteps}
If positive, then number of integration steps in velocity space for
midpoint rule. If zero, then an adaptive scheme is used.

\paragraph{comptorque}
Do torque computation.

\paragraph{intoutput}
Output details of integrand.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{plasma.in}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{profile.in}


\chapter{Transfer Learning and Transportability}

05 - Causal Networks/chapters/11_transfer_learning.tex


\section{Theorem proving using Isabelle}
\label{sec:background:theorem_proving_using_isabelle}

As mentioned before in \cref{sec:introduction:validation} all formal proofs within this paper are verified within a theorem prover, sometimes also called a proof assistant. A theorem prover is a software solution to assist with the task of proving mathematical theorems. It achieves this goal by using automated reasoning and mathematical logic to provide the user with information on the correctness of the written proof. Furthermore, a theorem prover might have tools to prove simple theorems automatically.

In this thesis, the Isabelle proof assistant is used to prove the relevant theorems. Isabelle was chosen as theorem prover for several reasons:
\begin{itemize}
    \item Isabelle stays close to mathematical definitions while still maintaining much automation in the process, which is different from other theorem provers. A comparison of theorem provers has shown that most other theorem provers which stay close to the mathematical definition do not have much automation, and vice versa \cite{wiedijk_comparing-provers}.
    \item Isabelle provides a plug-in to jEdit, a text editor, which deeply integrated Isabelle into the jEdit editor. This integration allows for interactively creating theories and checking proofs without the need to use the command-line application for this purpose.
    \item Isabelle has its own proof language Isar, which makes proofs more readable to the human reader, without giving up on automation and functionality for delivering proofs.
    \item One of the supervisors of this thesis has experience with Isabelle, meaning that there is local expertise available in case of problems.
\end{itemize}

The remaining part of this section will discuss different parts of Isabelle and its proof language Isar, to provide some background on theorem proofing in Isabelle.

\subsection{About Isabelle}
\label{subsec:background:theorem_proving_using_isabelle:about_isabelle}
Isabelle \cite{isabelle_2019} is a generic theorem prover written in \textsc{ML}. It was originally developed at the University of Cambridge and Technische Universität München, but now includes numerous contributions from institutions and individuals worldwide. It has been designed to be able to support reasoning in several object-logics, which include but are not limited to:
\begin{itemize}
    \item first-order logic, constructive and classical versions (Isabelle/FOL)
    \item higher-order logic (Isabelle/HOL)
    \item Zermelo-Fraenkel set theory (Isabelle/ZF)
\end{itemize}
Isabelle is distributed for free under a mix of open-source licenses, but the main code-base is subject to BSD-style regulations. More specifically, the binary distributions of Isabelle come with the 3-Clause BSD License \cite{bsd3clause}.

Isabelle has quite a large user base and a well-maintained community. Besides a mailing list for users, there is also a wiki \cite{isabelle_wiki} and active support on StackOverflow, when questions are tagged under `isabelle' \cite{isabelle_stack_overflow}.

The first release of Isabelle is published in 1986. Nowadays, it receives yearly releases with new updates. At the time of writing, Isabelle 2019 is the newest release, which is also the release used to prove the theorems in this thesis.

\subsection{Basics}
\label{subsec:background:theorem_proving_using_isabelle:basics}
This section will discuss some basics on Isabelle that are relevant for this thesis. The constructs mentioned here will not be discussed in much detail, as that would be a thesis on its own. The documentation provided with Isabelle does a decent job explaining all constructs in as much detail as possible.

\subsubsection{Theories}
Each document in Isabelle is a \textit{theory}, which can define a set of definitions, theorems and proofs. A theory can import other theories in order to reuse its definitions, theorems and proofs. An example of a theory is given in \cref{appendix:example_isabelle_theory}. This theory is the actual formalisation of \cref{defin:formalisations:global_definitions:multiplicity} in Isabelle used for this thesis. It will be used as an example throughout the remaining parts of this section.

\subsubsection{Datatypes}
Within an Isabelle theory, it is possible to define inductive datatypes. Inductive datatypes are the most used way to define new types in Isabelle. Famous data structures, such as lists, can be defined using datatypes.

The example provided in \cref{appendix:example_isabelle_theory} defines a new datatype for the set $\mathbb{N} \cup {\mstar}$. The definition is specified within the theory as:
\vspace{1em}\begin{isabellebody}
\isacommand{datatype}\isamarkupfalse%
\ {\isasymM}\ {\isacharequal}\ Star\ {\isacharbar}\ Nr\ nat\isanewline
\end{isabellebody}

This example defines a datatype called \isasymM{}, which can have two values: $Star$ and $Nr$. $Star$ and $Nr$ are called datatype constructors and can get arguments of different types. For example, the $Nr$ value gets an additional type, $nat$, which is Isabelle's type for representing natural numbers. On the other hand, the $Star$ constructor gets no additional arguments and is just a value for \isasymM{} on itself.

The formalisation achieved here should be straightforward. $Star$ is used to denote $\mstar$, the unbounded value, while $Nr\ nat$ is used to denote a bounded value for a multiplicity.

\subsubsection{Record types}
Another way of defining new types within Isabelle is by using record types. A record can be defined using the \textbf{record} keyword. The concept of a record is borrowed from programming languages, but it provides a way to define a named \textit{n}-tuple. Effectively, a record type is a type consisting of multiple named fields that can each have a different type. Each field of a record can be accessed using its name.

Within this thesis, records are actively used to introduce types for type models, type graphs, instance models and instance graphs. These are all named-tuples which are easiest defined using records. Sadly, the example provided in \cref{appendix:example_isabelle_theory} does not define such a record, therefore, we provide the record of an instance model (\cref{defin:formalisations:ecore_formalisation:instance_models:instance_model}) here:

\vspace{1em}\begin{isabellebody}
\isacommand{record}\isamarkupfalse%
\ {\isacharparenleft}{\isacharprime}o{\isacharcomma}\ {\isacharprime}nt{\isacharparenright}\ instance{\isacharunderscore}model\ {\isacharequal}\isanewline
\ \ Tm\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}nt{\isacharparenright}\ type{\isacharunderscore}model{\isachardoublequoteclose}\isanewline
\ \ Object\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}o\ set{\isachardoublequoteclose}\isanewline
\ \ ObjectClass\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}o\ {\isasymRightarrow}\ {\isacharprime}nt\ Id{\isachardoublequoteclose}\isanewline
\ \ ObjectId\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}o\ {\isasymRightarrow}\ {\isacharprime}nt{\isachardoublequoteclose}\isanewline
\ \ FieldValue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}o\ {\isasymtimes}\ {\isacharparenleft}{\isacharprime}nt\ Id\ {\isasymtimes}\ {\isacharprime}nt{\isacharparenright}{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}o{\isacharcomma}\ {\isacharprime}nt{\isacharparenright}\ ValueDef{\isachardoublequoteclose}\isanewline
\ \ DefaultValue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}nt\ Id\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}o{\isacharcomma}\ {\isacharprime}nt{\isacharparenright}\ ValueDef{\isachardoublequoteclose}%
\end{isabellebody}

The record of an instance model directly shows the structure. It has 6 named fields, the corresponding type model and the 5 elements defined in \cref{defin:formalisations:ecore_formalisation:instance_models:instance_model}. Each of these fields has a corresponding type, corresponding to the type described within the definition of an instance model. This way, we have a direct formalisation of an instance graph in Isabelle.

\subsubsection{Type synonyms}
Isabelle can form new types out of existing types by using generic types. For example, \textit{list}s in Isabelle use this functionality. A list was created using a generic type that can be replaced with any concrete type on usage. For example `$nat\ list$' would represent a list of natural numbers, and `\isasymM$\ list$' a list of elements of datatype \isasymM.

To make it more convenient to use these types, it is possible to define these composed types as a type synonym. An example of such a type synonym is given in \cref{appendix:example_isabelle_theory}:

\vspace{1em}\begin{isabellebody}
\isacommand{type{\isacharunderscore}synonym}\isamarkupfalse%
\ multiplicity\ {\isacharequal}\ {\isachardoublequoteopen}{\isasymM}\ {\isasymtimes}\ {\isasymM}{\isachardoublequoteclose}
\end{isabellebody}

This type synonym defines the multiplicity type, effectively the formalisation of $\mathbb{M}$ from \cref{defin:formalisations:global_definitions:multiplicity}. It is a tuple of two elements of datatype \isasymM, thus a tuple $\mathbb{N} \cup \{\mstar\} \times \mathbb{N} \cup \{\mstar\}$. Using this type synonym, it is now possible to refer to the multiplicity type as `multiplicity'.

\subsubsection{Definitions and functions}
In an Isabelle theory, multiple definitions can be provided. The most basic definition can be created using the \textbf{definition} keyword. Effectively, a \textbf{definition} is simply an abbreviation, i.e. a new name for an existing construction.

An example of such a definition is the $upper$ definition in \cref{appendix:example_isabelle_theory}:
\vspace{1em}\begin{isabellebody}
\isacommand{definition}\isamarkupfalse%
\ upper\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}multiplicity\ {\isasymRightarrow}\ {\isasymM}{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
\ \ {\isachardoublequoteopen}upper\ m\ {\isasymequiv}\ snd\ m{\isachardoublequoteclose}
\end{isabellebody}

As can be seen, the upper definition receives one argument $m$ of type `multiplicity', a multiplicity-tuple. It returns the upper bound of the multiplicity. In other words, it returns the second element of the tuple. This behaviour matches what the definition tells us, as $snd$ is the Isabelle function to return the second element of a tuple.

Besides the \textbf{definition} keyword, it is possible to give recursive function definitions using the \textbf{fun} and \textbf{function} keywords. An example of this is given as part of the linear order of \isasymM{} in \cref{appendix:example_isabelle_theory}:
\vspace{1em}\begin{isabellebody}
\isacommand{fun}\isamarkupfalse%
\ less{\isacharunderscore}eq{\isacharunderscore}{\isasymM}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymM}\ {\isasymRightarrow}\ {\isasymM}\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
{\isachardoublequoteopen}less{\isacharunderscore}eq{\isacharunderscore}{\isasymM}\ {\isacharunderscore}\ \isactrlemph \ {\isacharequal}\ True{\isachardoublequoteclose}\ {\isacharbar}\isanewline
{\isachardoublequoteopen}less{\isacharunderscore}eq{\isacharunderscore}{\isasymM}\ {\isacharparenleft}\isactrlbold a{\isacharparenright}\ {\isacharparenleft}\isactrlbold b{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}a\ {\isasymle}\ b{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\ \isanewline
{\isachardoublequoteopen}less{\isacharunderscore}eq{\isacharunderscore}{\isasymM}\ {\isacharunderscore}\ {\isacharunderscore}\ {\isacharequal}\ False{\isachardoublequoteclose}
\end{isabellebody}

The function defined here, `less-eq-\isasymM', defines the less than or equal to ($\leq$) relation for \isasymM. Effectively, the function describes that any value is always smaller or equal to $\mstar$ and that two numbers (two instances of $Nr$) are only less or equal when the first number is less or equal to the second number.

The same function could also have been defined using the \textbf{function} keyword, with the only difference that for the \textbf{function} keyword, a proof for termination of the function must be provided manually. Using the \textbf{fun} keyword, Isabelle will try to automatically proof termination of the function by using the specification. This automation is very powerful and works in a variety of functions (in fact, for all functions defined in this thesis, termination is proven automatically using \textbf{fun}).

\subsubsection{Abbreviations and notation}
As can be seen from the `less-eq-\isasymM' function in \cref{appendix:example_isabelle_theory}, the $Nr$ and $Star$ constructors for \isasymM{} are not used directly. Instead, numbers and a star symbol (\isactrlemph) are used to respectively represent a value of $Nr$ and the $Star$ constructor. The use of these symbols has been achieved using the \textbf{notation} keyword:
\vspace{1em}\begin{isabellebody}
\isacommand{notation}\isamarkupfalse%
\isanewline
\ \ Star\ {\isacharparenleft}{\isachardoublequoteopen}{\isacharparenleft}\isactrlemph {\isacharparenright}{\isachardoublequoteclose}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}{\isadigit{0}}{\isacharparenright}\ \isakeyword{and}\isanewline
\ \ Nr\ {\isacharparenleft}{\isachardoublequoteopen}{\isacharparenleft}\isactrlbold {\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbrackleft}{\isadigit{1}}{\isadigit{0}}{\isadigit{0}}{\isadigit{0}}{\isacharbrackright}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}{\isadigit{0}}{\isacharparenright}
\end{isabellebody}

The notation keyword allows us to introduce a new notation for many different constructs, in this case, the $Star$ and $Nr$ constructors of \isasymM. Custom notations are very powerful, as Isabelle automatically rewrites $Star$ and $Nr$ constructors back to this notation, so introducing this notation works in two ways. It can help to make theorems and proofs more readable, as can also be seen from the example in \cref{appendix:example_isabelle_theory}.

Besides the introduction of an alternative notation for existing constructs, it is also possible to introduce a new notation with the corresponding definition of what the notation means. Such a notation is achieved using the \textbf{abbreviation} keyword. It has the same properties as the \textbf{notation} keyword, but then for a newly defined definition. An example of this is given in \cref{appendix:example_isabelle_theory}:
\vspace{1em}\begin{isabellebody}
\isacommand{abbreviation}\isamarkupfalse%
\ multiplicity{\isacharunderscore}notation\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymM}\ {\isasymRightarrow}\ {\isasymM}\ {\isasymRightarrow}\ multiplicity{\isachardoublequoteclose}\ {\isacharparenleft}{\isachardoublequoteopen}{\isacharparenleft}{\isacharunderscore}{\isacharslash}{\isachardot}{\isachardot}{\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbrackleft}{\isadigit{5}}{\isadigit{2}}{\isacharcomma}\ {\isadigit{5}}{\isadigit{2}}{\isacharbrackright}\ {\isadigit{5}}{\isadigit{1}}{\isacharparenright}\ \isakeyword{where}\isanewline
\ \ {\isachardoublequoteopen}l{\isachardot}{\isachardot}u\ {\isasymequiv}\ {\isacharparenleft}l{\isacharcomma}u{\isacharparenright}{\isachardoublequoteclose}
\end{isabellebody}

This example introduces a new notation for writing down a multiplicity tuple. It does so by writing the newly introduced notation on the left-hand side and writing the corresponding definition on the right-hand side. Although an \textbf{abbreviation} looks the same as a \textbf{definition}, they are different in the sense that \textbf{abbreviation} only introduces a notation. To Isabelle, it is syntactic sugar, as internally, the notation does not exist. It is only used when representing constructs to the user. This behaviour is different from \textbf{definition}, as definitions exist internally and are used by the proof reasoners.

It should be noted that there are shortcuts possible to introduce notations while defining a definition. An example of this is the `within{\isacharunderscore}multiplicity' definition in \cref{appendix:example_isabelle_theory}:
\vspace{1em}\begin{isabellebody}
\isacommand{definition}\isamarkupfalse%
\ within{\isacharunderscore}multiplicity\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ multiplicity\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequoteopen}in{\isachardoublequoteclose}\ {\isadigit{5}}{\isadigit{0}}{\isacharparenright}\ \isakeyword{where}\isanewline
\ \ {\isachardoublequoteopen}n\ in\ m\ {\isasymequiv}\ lower\ m\ {\isasymle}\ \isactrlbold n\ {\isasymand}\ \isactrlbold n\ {\isasymle}\ upper\ m{\isachardoublequoteclose}\isanewline
\end{isabellebody}

This function uses the infix-left (\textbf{infixl}) construction to define an infix notation for the definition. Just like the \textbf{abbreviation} command, it is possible to use this definition on the left-hand side of the definition, for readability.

\subsubsection{Locales}
When defining new types, there is no way to constrain the values for any of its elements. For example, for the `multiplicity' type, there is no way to prevent the second value of the tuple to be 0, since the natural numbers include 0. In Isabelle, functions and types are always total, and there is no way to exclude specific values of a type.

A way to work around this is by using \textbf{locale}s. Locales are Isabelle's approach for dealing with parametric theories. With locales, it is possible to define a context in which specific assumptions hold. An example of a locale is given in \cref{appendix:example_isabelle_theory}:
\vspace{1em}\begin{isabellebody}
\isacommand{locale}\isamarkupfalse%
\ multiplicity\ {\isacharequal}\ \isakeyword{fixes}\ mult\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}multiplicity{\isachardoublequoteclose}\isanewline
\ \ \isakeyword{assumes}\ lower{\isacharunderscore}bound{\isacharunderscore}valid{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}lower\ mult\ {\isasymnoteq}\ \isactrlemph {\isachardoublequoteclose}\isanewline
\ \ \isakeyword{assumes}\ upper{\isacharunderscore}bound{\isacharunderscore}valid{\isacharcolon}\ {\isachardoublequoteopen}upper\ mult\ {\isasymnoteq}\ \isactrlbold {\isadigit{0}}{\isachardoublequoteclose}\isanewline
\ \ \isakeyword{assumes}\ properly{\isacharunderscore}bounded{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}lower\ mult\ {\isasymle}\ upper\ mult{\isachardoublequoteclose}
\end{isabellebody}

This example introduces the multiplicity locale. Within this locale, we introduce a named-construct `mult', which is a multiplicity. Then we make some assumptions which hold in the context of a multiplicity. In this case,  there are three assumptions. First of all, there are assumptions on the lower and upper bound, excluding specific (but invalid) values. The final assumption captures that the lower bound is always smaller or equal to the upper bound.

With this locale in place, it is possible to prove theorems and lemmas within the context of a multiplicity. That means that when proving theorems and lemmas within the multiplicity context, all introduced assumptions for `mult' hold:
\vspace{1em}\begin{isabellebody}
\isacommand{context}\isamarkupfalse%
\ multiplicity\isanewline
\isakeyword{begin}\isanewline
\isanewline
\isacommand{lemma}\isamarkupfalse%
\ upper{\isacharunderscore}bound{\isacharunderscore}valid{\isacharunderscore}alt{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}upper\ mult\ {\isasymge}\ \isactrlbold {\isadigit{1}}{\isachardoublequoteclose}\isanewline
%
\isadelimproof
\ \ %
\endisadelimproof
%
\isatagproof
\isacommand{using}\isamarkupfalse%
\ less{\isacharunderscore}{\isasymM}{\isachardot}elims\ not{\isacharunderscore}less\ upper{\isacharunderscore}bound{\isacharunderscore}valid\ \isacommand{by}\isamarkupfalse%
\ fastforce%
\endisatagproof
{\isafoldproof}%
%
\isadelimproof
\isanewline
%
\endisadelimproof
\isanewline
\isacommand{end}\isamarkupfalse%
\end{isabellebody}

In above example, it is possible to prove that `$upper\ mult \geq 1$ because the assumptions ensure that `$upper\ mult \not= 0$'. Since natural numbers cannot be negative, we have that `$upper\ mult \geq 1$. This theorem can only be proven within the multiplicity context, as otherwise, the assumptions do not hold, and `$upper\ mult$' might be $0$.

Within this thesis, \textbf{locale}s are mostly used to denote valid constructs, such as a valid type graph, type model, instance graph or instance model. These locales limit the respective \textbf{record} types for these constructs by assuming the validity constraints presented in their respective sections.

\subsubsection{Theorems and proofs}
Theorems (also called lemmas) are statements that can be proven correct. For this thesis, all theorems are either defined using the \textbf{theorem} or \textbf{lemma} keywords in Isabelle. A theorem can be defined to be only valid under certain assumptions or can be defined to be true without any assumptions.

An example of a simple \textbf{theorem} can be found in \cref{appendix:example_isabelle_theory}:
\vspace{1em}\begin{isabellebody}
\isacommand{theorem}\isamarkupfalse%
\ mult{\isacharunderscore}zero{\isacharunderscore}unbounded{\isacharunderscore}valid{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}n\ in\ \isactrlbold {\isadigit{0}}{\isachardot}{\isachardot}\isactrlemph {\isachardoublequoteclose}\isanewline
%
\isadelimproof
\ \ %
\endisadelimproof
%
\isatagproof
\isacommand{unfolding}\isamarkupfalse%
\ within{\isacharunderscore}multiplicity{\isacharunderscore}def\isanewline
\ \ \isacommand{by}\isamarkupfalse%
\ simp%
\endisatagproof
{\isafoldproof}%
%
\isadelimproof
\end{isabellebody}

This theorem states that for a multiplicity $0..\mstar$, any natural number is within bounds (any $n$ is in $0..\mstar$). It can easily be proven using the definition of a natural number within a multiplicity.

A proof for a theorem is written directly after the statement. It can either be a short proof using apply-scripts, or a proof within Isabelle's proof language Isar. In the example above, it is a short proof using apply-scripts. In this case, the proof is done within one step: by simplification of the definition.

Once lemmas or theorems are proven, they can be used in the proof of other lemmas and statements. Reusing them is done by referring to them manually, or by adding them to a set of lemmas and theorems that Isabelle will try by default. Adding theorems to a set of default rules is done by adding a specific keyword. For example, add $[simp]$ to add the theorem to the set of simplification rules, or add $[intro]$ or $[elim]$, to specify the theorem to be an introduction rule or elimination rule. Introduction and elimination rules will not be further specified here; more information on these can be found in the Isabelle documentation.

\subsubsection{Isar}
\textit{Isar} stands for \textit{Intelligible semi-automated reasoning}, and is an interpreted language environment for structured formal proof documents. It allows to write down mostly humanly readable proofs in Isabelle while still getting the advantage of semi-automated reasoning. Isar is built on the principle of writing down multiple steps of the proof, doing less automation, in favour of readability. As a consequence, writing Isar proofs is more work for the writer of the proof but eventually results in better humanly readable proofs that also have value without the automated reasoning of a theorem prover.

This section will not discuss the full Isar environment in detail. The Isabelle/Isar Reference Manual \cite{wenzel-isabelle_isar}, included with each copy of Isabelle, already contains a very detailed explanation of all features that Isar has to offer. Instead, we consider a small example of a proof written in Isar, picked directly from \cref{appendix:example_isabelle_theory}:
\vspace{1em}\begin{isabellebody}
\isatagproof
\isacommand{proof}\isamarkupfalse%
\isanewline
\ \ \isacommand{fix}\isamarkupfalse%
\ x\ y\ z\ {\isacharcolon}{\isacharcolon}\ {\isasymM}\isanewline
\ \ \isacommand{show}\isamarkupfalse%
\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isacharless}\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}x\ {\isasymle}\ y\ {\isasymand}\ {\isasymnot}\ y\ {\isasymle}\ x{\isacharparenright}{\isachardoublequoteclose}\isanewline
\ \ \isacommand{proof}\isamarkupfalse%
\ {\isacharparenleft}induction\ x\ arbitrary{\isacharcolon}\ y{\isacharparenright}\isanewline
\ \ \ \ \isacommand{case}\isamarkupfalse%
\ Star\isanewline
\ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ simp{\isacharunderscore}all\isanewline
\ \ \isacommand{next}\isamarkupfalse%
\isanewline
\ \ \ \ \isacommand{case}\isamarkupfalse%
\ {\isacharparenleft}Nr\ x{\isacharparenright}\isanewline
\ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ {\isacharparenleft}cases\ y{\isacharparenright}\ auto\isanewline
\ \ \isacommand{qed}\isamarkupfalse%
\isanewline
\isanewline
\ \ \isacommand{show}\isamarkupfalse%
\ {\isachardoublequoteopen}x\ {\isasymle}\ x{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
\ {\isacharparenleft}induction\ x{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
\ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isachardoublequoteopen}x\ {\isasymle}\ y\ {\isasymLongrightarrow}\ y\ {\isasymle}\ x\ {\isasymLongrightarrow}\ x\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline
\ \ \isacommand{proof}\isamarkupfalse%
\ {\isacharparenleft}induction\ x\ arbitrary{\isacharcolon}\ y{\isacharparenright}\isanewline
\ \ \ \ \isacommand{case}\isamarkupfalse%
\ Star\isanewline
\ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ {\isacharparenleft}cases\ y{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
\ \ \isacommand{next}\isamarkupfalse%
\isanewline
\ \ \ \ \isacommand{case}\isamarkupfalse%
\ {\isacharparenleft}Nr\ x{\isacharparenright}\isanewline
\ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ {\isacharparenleft}cases\ y{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
\ \ \isacommand{qed}\isamarkupfalse%
\isanewline
\isanewline
\ \ \isacommand{show}\isamarkupfalse%
\ {\isachardoublequoteopen}x\ {\isasymle}\ y\ {\isasymLongrightarrow}\ y\ {\isasymle}\ z\ {\isasymLongrightarrow}\ x\ {\isasymle}\ z{\isachardoublequoteclose}\isanewline
\ \ \isacommand{proof}\isamarkupfalse%
\ {\isacharparenleft}induction\ x\ arbitrary{\isacharcolon}\ y\ z{\isacharparenright}\isanewline
\ \ \ \ \isacommand{case}\isamarkupfalse%
\ Star\isanewline
\ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ {\isacharparenleft}cases\ y{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
\ \ \isacommand{next}\isamarkupfalse%
\isanewline
\ \ \ \ \isacommand{case}\isamarkupfalse%
\ {\isacharparenleft}Nr\ x{\isacharparenright}\isanewline
\ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\isanewline
\ \ \ \ \isacommand{proof}\isamarkupfalse%
\ {\isacharparenleft}induction\ y\ arbitrary{\isacharcolon}\ z{\isacharparenright}\isanewline
\ \ \ \ \ \ \isacommand{case}\isamarkupfalse%
\ Star\isanewline
\ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ {\isacharparenleft}cases\ z{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
\ \ \ \ \isacommand{next}\isamarkupfalse%
\isanewline
\ \ \ \ \ \ \isacommand{case}\isamarkupfalse%
\ {\isacharparenleft}Nr\ x{\isacharparenright}\isanewline
\ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ {\isacharparenleft}cases\ z{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
\ \ \ \ \isacommand{qed}\isamarkupfalse%
\isanewline
\ \ \isacommand{qed}\isamarkupfalse%
\isanewline
\isanewline
\ \ \isacommand{show}\isamarkupfalse%
\ {\isachardoublequoteopen}x\ {\isasymle}\ y\ {\isasymor}\ y\ {\isasymle}\ x{\isachardoublequoteclose}\isanewline
\ \ \isacommand{proof}\isamarkupfalse%
\ {\isacharparenleft}induction\ x\ arbitrary{\isacharcolon}\ y{\isacharparenright}\isanewline
\ \ \ \ \isacommand{case}\isamarkupfalse%
\ Star\isanewline
\ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ simp\isanewline
\ \ \isacommand{next}\isamarkupfalse%
\isanewline
\ \ \ \ \isacommand{case}\isamarkupfalse%
\ {\isacharparenleft}Nr\ x{\isacharparenright}\isanewline
\ \ \ \ \isacommand{then}\isamarkupfalse%
\ \isacommand{show}\isamarkupfalse%
\ {\isacharquery}case\ \isacommand{by}\isamarkupfalse%
\ {\isacharparenleft}cases\ y{\isacharparenright}\ auto\isanewline
\ \ \isacommand{qed}\isamarkupfalse%
\isanewline
\isacommand{qed}\isamarkupfalse%
%
\endisatagproof
{\isafoldproof}%
%
\isadelimproof
%
\endisadelimproof
\end{isabellebody}

In this example, we see the proof that proves that type \isasymM{} is an instantiation of a linear order. In order to show that type \isasymM{} gives rise to a linear order, we have to proof multiple subgoals, which are:
\begin{itemize}
    \item Correctness of $<$: $(x < y) = (x \leq y \land \lnot y \leq x)$
    \item Reflexivity of $\leq$: $(x \leq x)$
    \item Transitivity of $\leq$: $x \leq y \land y \leq z \implies x \leq z$
    \item Correctness of the linear order: $x \leq y \lor y \leq x$
\end{itemize}
Each of these subgoals is proven separately within the Isar proof. The proof of each subgoal is defined by the \textbf{show} keyword. Important to see from the example above is that each subgoal is proven using a nested subproof. Such subproofs can be written as apply-scripts, or using a nested Isar proof, as we see in the example above.

\subsubsection{Proof tactics}
In order to deliver proofs, we make use of automated reasoning. Essential aspects of automated reasoning are the different proof tactics in Isabelle. Proof tactics can be applied to a proof goal to either solve the proof goal entirely or to somehow make the goal simpler to solve.

A vital proof tactic shown in the example above is `induction'. This tactic applies mathematical induction to the proof goal, splitting the goal into new subgoals that follow the structure of mathematical induction.

The following proof tactics are extensively used within this thesis:
\begin{itemize}
    \item `induction' (also called `induct'): Applies mathematical induction to the proof goal. Splits the subgoal into two or more subgoals that follow the structure of mathematical induction.
    \item `cases': Applies a case distinction to the proof goal. It will split the proof goal into multiple subgoals, one for each applicable case. This proof tactic works especially well for inductive definitions and datatypes with a finite set of possible values.
    \item `intro': Splits a proof goal and introduces new subgoals based on an introduction rule. An important example of an introduction rule is $conjI$, which splits a proof goal of $A \land B$ into the two subgoals $A$ and $B$.
    \item `elim': Splits a proof goal by eliminating operations and relations and providing smaller subgoals instead of those. For example, the elimination rule $disjE$ splits a proof goal of the form $A \lor B \implies C$ into two subgoals, $A \implies C$ and $B \implies C$.
    \item `simp' (and `simp\_all'): Apply simplification to a proof goal in order to solve the problem completely. It uses simplification rules to rewrite the statement until it arrives at `True', finishing the proof.
    \item `fastforce': Solves the proof goal by using a tactic similar to brute force. It tries all possible outcomes but tries to be smart by excluding similar cases.
    \item `fast': A classical solver which solves the proof goal by structurally checking cases based on a depth-first search algorithm. Not frequently used within this thesis.
    \item `auto': A combination of `simp' and `fastforce', which can also use introduction rules and elimination rules when rewriting. In general, this proof tactic more powerful than `simp' and `fastforce'.
    \item `blast': Solves the proof goal by using a semantic tableau. Frequently used for solving logic problems.
    \item `metis': Solves the proof goal by using resolution. Frequently used for more complex logic problems that cannot be solved by `blast'.
\end{itemize}

Isabelle is not limited to the above-discussed proof tactics, but these tactics are the most important ones for this thesis. Other tactics are not used either because they apply to a different kind of problem (number arithmetic instead of logic, for example) or because they are not transparent in solving their problem. For example, the `smt' proof tactic solves a problem by using an external SMT solver. Although these proof tactics can solve many problems, it is not transparent to the reader what steps the SMT solver has taken to solve the problem, as opposed to the proof tactics described above. Therefore these tactics have been excluded.

\subsection{Archive of Formal Proofs}
Isabelle has an Archive of Formal Proofs (AFP), which is a collection of proof libraries, examples, and larger scientific developments, mechanically checked in the theorem prover Isabelle. All theories within this archive are organised in the way of a scientific journal such that they can be referred to by new theories.

\subsubsection{Graph Theory}
The Isabelle AFP submission Graph Theory \cite{noschinski-graph_theory} is used as part of this thesis. This submission to the Isabelle AFP is a formalization of directed graphs, supporting labelled multi-edges and infinite graphs. Theorems proven for these graphs include, but are not limited to, walks, cyclicity, connectedness and some properties of isomorphisms. All the theorems proven as part of this submission are discussed in \cite{bang-jensen_gutin_2010}.

Within this thesis, the submission is used as part of the GROOVE formalisation within Isabelle. Within the GROOVE formalisation, GROOVE type graphs and instance graphs are extensions of the directed graph introduced by the Graph Theory submission. This allows Isabelle to apply theorems proven for graphs within this submission to GROOVE graphs presented in the theories of this thesis.

Within this thesis, only a small selected set of theorems from the submission is used. This set mostly includes theorems related to walks and cyclicity of graphs. These theorems are used to show the acyclicity of the containment relation for instance graphs.


% Like most advanced LaTeX files, this one begins with a lot of
% boilerplate. You don't need to understand (or even read) most of it.
% All you need to do is fill in your name, UMN ID, email address,
% and the number of the pset. (Search for "METADATA" to find the place
% for this.) Then, you can go straight to the "EXERCISE 1"
% section and start writing your solutions.
% The "VARIOUS USEFUL COMMANDS" section is probably worth taking a
% look at at some point.

%----------------------------------------------------------------------------------------
%	PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------
\documentclass[paper=a4, fontsize=12pt]{scrartcl} % A4 paper and 12pt font size
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm,amssymb} % Math packages
\usepackage{mathrsfs}    % More math packages
\usepackage{sectsty}  % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all section titles centered, the default font and small caps %remove this to left align section tites
\usepackage{hyperref} % Turns cross-references into hyperlinks,
                      % and defines \url and \href commands.
\usepackage{graphicx} % For embedding graphics files.
\usepackage{framed}   % For the "leftbar" environment used below.
\usepackage{ifthen}   % Used for the \powset command below.
\usepackage{lastpage} % for counting the number of pages
\usepackage[headsepline,footsepline,manualmark]{scrlayer-scrpage}
\usepackage[height=10in,a4paper,hmargin={1in,0.8in}]{geometry}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}     % This is a powerful tool to draw vector
                      % graphics inside LaTeX. In particular, you can
                      % use it to draw graphs.
\usepackage{verbatim} % For the "verbatim" environment, in which
                      % special symbols can be used freely without
                      % confusing the compiler. (And it's typeset in
                      % a constant-width font.)
                      % Useful, e.g., for quoting code (or ASCII art).

%\numberwithin{table}{section} % Number tables within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)

\setlength\parindent{20pt} % Makes indentation for paragraphs longer.
                           % This makes paragraphs stand out more.

%----------------------------------------------------------------------------------------
%	VARIOUS USEFUL COMMANDS
%----------------------------------------------------------------------------------------
% The commands below might be convenient. For example, you probably
% prefer to write $\powset[2]{V}$ for the set of $2$-element subsets
% of $V$, rather than writing $\mathcal{P}_2(V)$.
% Notice that you can easily define your own commands like this.
% Caveat: Some of these commands need to be properly "guarded" when
% they occur in subscripts or superscripts. So you should not write
% $K_\CC$, but rather $K_{\CC}$.
\newcommand{\CC}{\mathbb{C}} % complex numbers
\newcommand{\RR}{\mathbb{R}} % real numbers
\newcommand{\QQ}{\mathbb{Q}} % rational numbers
\newcommand{\NN}{\mathbb{N}} % nonnegative integers
\newcommand{\PP}{\mathbb{P}} % positive integers
\newcommand{\KK}{\mathbb{K}} % my favorite notation for a commutative ring
\newcommand{\Z}[1]{\mathbb{Z}/#1\mathbb{Z}} % integers modulo k
                                            % (syntax: "\Z{k}")
\newcommand{\ZZ}{\mathbb{Z}} % integers
\newcommand{\id}{\operatorname{id}} % identity map
\newcommand{\lcm}{\operatorname{lcm}}
% Lowest common multiple. For historical reasons, LaTeX has a \gcd
% command built in, but not an \lcm command. The preceding line
% rectifies that.
\newcommand{\set}[1]{\left\{ #1 \right\}}
% $\set{...}$ compiles to {...} (set-brackets).
\newcommand{\abs}[1]{\left| #1 \right|}
% $\abs{...}$ compiles to |...| (absolute value, or size of a set).
\newcommand{\tup}[1]{\left( #1 \right)}
% $\tup{...}$ compiles to (...) (parentheses, or tuple-brackets).
\newcommand{\ive}[1]{\left[ #1 \right]}
% $\ive{...}$ compiles to [...] (Iverson bracket, aka truth value; also, set of first n integers).
\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}
% $\floor{...}$ compiles to |_..._| (floor function).
\newcommand{\underbrack}[2]{\underbrace{#1}_{\substack{#2}}}
% $\underbrack{...1}{...2}$ yields
% $\underbrace{...1}_{\substack{...2}}$. This is useful for doing
% local rewriting transformations on mathematical expressions with
% justifications. For example, try this out:
% $ \underbrack{(a+b)^2}{= a^2 + 2ab + b^2 \\ \text{(by the binomial formula)}} $
\newcommand{\powset}[2][]{\ifthenelse{\equal{#2}{}}{\mathcal{P}\left(#1\right)}{\mathcal{P}_{#1}\left(#2\right)}}
% $\powset[k]{S}$ stands for the set of all $k$-element subsets of
% $S$. The argument $k$ is optional, and if not provided, the result
% is the whole powerset of $S$.
\newcommand{\calF}{\mathcal{F}}
\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Create horizontal rule command with 1 argument of height
\newcommand{\nnn}{\nonumber\\} % Don't number this line in an "align" environment, and move on to the next line.

%----------------------------------------------------------------------------------------
%	MAKING SUMMATION SIGNS ALWAYS PUT THEIR BOUNDS ABOVE AND BELOW
%	THE SIGN
%----------------------------------------------------------------------------------------
% The following are hacks to ensure that sums (such as
% $\sum_{k=1}^n k$) always put their bounds (i.e., the $k=1$ and the
% $n$) underneath and above the sign, as opposed to on its right.
% Same for products (\prod), set unions (\bigcup) and set
% intersections (\bigcap). Remove the 8 lines below if you do not want
% this behavior.
\let\sumnonlimits\sum
\let\prodnonlimits\prod
\let\cupnonlimits\bigcup
\let\capnonlimits\bigcap
\renewcommand{\sum}{\sumnonlimits\limits}
\renewcommand{\prod}{\prodnonlimits\limits}
\renewcommand{\bigcup}{\cupnonlimits\limits}
\renewcommand{\bigcap}{\capnonlimits\limits}

%----------------------------------------------------------------------------------------
%	ENVIRONMENTS
%----------------------------------------------------------------------------------------
% The incantations below define how theorem environments
% (\begin{theorem} ... \end{theorem}) and their likes will look like.
\newtheoremstyle{plainsl}% <name>
  {8pt plus 2pt minus 4pt}% <Space above>
  {8pt plus 2pt minus 4pt}% <Space below>
  {\slshape}% <Body font>
  {0pt}% <Indent amount>
  {\bfseries}% <Theorem head font>
  {.}% <Punctuation after theorem head>
  {5pt plus 1pt minus 1pt}% <Space after theorem headi>
  {}% <Theorem head spec (can be left empty, meaning `normal')>

% Environments which make the text inside them slanted:
\theoremstyle{plainsl}
  \newtheorem{theorem}{Theorem}[section]
  \newtheorem{proposition}[theorem]{Proposition}
  \newtheorem{lemma}[theorem]{Lemma}
  \newtheorem{corollary}[theorem]{Corollary}
  \newtheorem{conjecture}[theorem]{Conjecture}
% Environments that don't:
\theoremstyle{definition}
  \newtheorem{definition}[theorem]{Definition}
  \newtheorem{example}[theorem]{Example}
  \newtheorem{exercise}[theorem]{Exercise}
  \newtheorem{examples}[theorem]{Examples}
  \newtheorem{algorithm}[theorem]{Algorithm}
  \newtheorem{question}[theorem]{Question}
 \theoremstyle{remark}
  \newtheorem{remark}[theorem]{Remark}
\newenvironment{statement}{\begin{quote}}{\end{quote}}
\newenvironment{fineprint}{\begin{small}}{\end{small}}

%----------------------------------------------------------------------------------------
%	METADATA
%----------------------------------------------------------------------------------------
\newcommand{\myname}{Darij Grinberg} % ENTER YOUR NAME HERE
\newcommand{\myid}{00000000} % ENTER YOUR UMN ID HERE
\newcommand{\mymail}{dgrinber@umn.edu} % ENTER YOUR EMAIL HERE
\newcommand{\psetnumber}{2} % ENTER THE NUMBER OF THIS PSET HERE

%----------------------------------------------------------------------------------------
%	HEADER AND FOOTER
%----------------------------------------------------------------------------------------
\ihead{Solutions to midterm \#\psetnumber} % Page header left
\ohead{page \thepage\ of \pageref{LastPage}} % Page header right
\ifoot{\myname, \myid} % left footer
\ofoot{\mymail} % right footer

%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------
\title{
\normalfont \normalsize
\textsc{University of Minnesota, School of Mathematics} \\ [25pt] % Your university, school and/or department name(s)
\horrule{0.5pt} \\[0.4cm] % Thin top horizontal rule
\huge Math 4281: Introduction to Modern Algebra, \\
Spring 2019:
Midterm \psetnumber\\% The assignment title
\horrule{2pt} \\[0.5cm] % Thick bottom horizontal rule
}
\author{\myname}

\begin{document}

\maketitle % Print the title

\begin{center} % Delete this if you want to save space!
{\large due date: \textbf{Friday, 12 April 2019} at the beginning of class, \\
or before that through Canvas.

\textbf{No collaboration allowed} -- this is a midterm.

Please solve \textbf{at most 3 of the 6 exercises}!}
\end{center}

%----------------------------------------------------------------------------------------
%	EXERCISE 1
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 1: Not-quite-all-rationals}

\subsection{Problem}

Fix an integer $m$.
An \textit{$m$-integer} shall mean a rational number
$r$ such that there exists a $k \in \NN$ satisfying
$m^k r \in \ZZ$.

For example\footnote{You don't need to prove these.}:
\begin{itemize}
 \item Each integer $r$ is an $m$-integer (since $m^k r \in \ZZ$
       for $k = 0$).
 \item The rational number $\dfrac{5}{12}$
       is a $6$-integer (since $6^k \cdot \dfrac{5}{12} \in \ZZ$
       for $k = 2$), but neither a $2$-integer nor a $3$-integer
       (since multiplying it by a power of $2$ will not ``get
       rid of'' the prime factor $3$ in the denominator, and
       vice versa\footnote{You would have to be more rigorous
       than this in your solution, if you were to make an
       argument like this.}).
 \item The $1$-integers are the integers (since $1^k r = r$
       for all $r$).
 \item Every rational number $r$ is a $0$-integer (since
       $0^k r \in \ZZ$ for $k = 1$).
\end{itemize}

Let $R_m$ denote the set of all $m$-integers.
Prove the following:

\begin{enumerate}

\item[\textbf{(a)}] The set $R_m$ (endowed with the usual
addition, the usual multiplication, the usual integer $0$
as zero, and the usual integer $1$ as unity) is a
commutative ring. \\
(You don't need to prove axioms like commutativity of
multiplication, since these follow from the corresponding
facts about rational numbers, which are well-known.
You only need to check that $R_m$ is
closed under addition and multiplication\footnote{This
means that every $a, b \in R_m$ satisfy $a + b \in R_m$
and $a b \in R_m$.}, and contains
additive inverses of all its elements.)

\item[\textbf{(b)}] Let $x \in \QQ$ be nonzero.
Then, $x \in R_m$ if and only if every prime $p$ satisfying
$w_p\tup{x} < 0$ satisfies $p \mid m$.
Here, we are using the notation $w_p\tup{r}$ defined
in Exercise 3.4.1 of the
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.pdf}{class notes}.

\end{enumerate}

\subsection{Remark}

The ring $R_m$ is an example of a ring ``between $\ZZ$ and $\QQ$''.
Note that $R_1 = \ZZ$ and $R_0 = \QQ$, whereas $R_2 = R_4 = R_8 = \cdots$
is the ring of all rational numbers that can be written in the form
$a / 2^k$ with $a \in \ZZ$ and $k \in \NN$.

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 2
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 2: Rings with $x^2 = x$}

\subsection{Problem}

Let $\KK$ be a ring with the property that
\begin{align}
 u^2 = u \qquad \text{ for all } u \in \KK .
 \label{eq.exe.ring.xx=x.cond}
\end{align}

(Examples of such rings are $\ZZ / 2$ as well as the
``power set'' ring $\tup{\powset{S}, \triangle, \cap, \varnothing, S}$
constructed from any given set $S$.)

Prove the following:

\begin{enumerate}

\item[\textbf{(a)}]
We have $2x = 0$ for each $x \in \KK$.

\item[\textbf{(b)}]
We have $-x = x$ for each $x \in \KK$.

\item[\textbf{(c)}]
We have $xy = yx$ for all $x, y \in \KK$.
(In other words, the ring $\KK$ is commutative.)

\end{enumerate}

(As usual, ``$0$'' stands for the zero of the ring $\KK$.)

[\textbf{Hint:}
For part \textbf{(a)},
apply \eqref{eq.exe.ring.xx=x.cond} to $u = x$
but also to $u = 2x = x + x$, and see what comes out.
For part \textbf{(c)},
apply \eqref{eq.exe.ring.xx=x.cond} to $u = x + y$.]

\subsection{Remark}

You might wonder what happens if we replace
\eqref{eq.exe.ring.xx=x.cond} by
\begin{align}
 u^3 = u \qquad \text{ for all } u \in \KK .
 \label{eq.exe.ring.xx=x.cube}
\end{align}
This no longer leads to $2x = 0$ (nor to $3x = 0$ as you
might perhaps expect).
Instead, it can be shown that $6x = 0$ for all $x \in \KK$.
It can also be shown that it leads to $xy = yx$.

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 3
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 3: A matrix of gcds}

\subsection{Problem}

In this exercise, we shall again use
\href{https://en.wikipedia.org/wiki/Iverson_bracket}{the \textit{Iverson bracket notation}}:

Let $n \in \NN$.
Let $G$ be the $n \times n$-matrix
\[
 \tup{ \gcd\tup{i, j} }_{1 \leq i \leq n, \ 1 \leq j \leq n }
 =
 \begin{pmatrix}   % "pmatrix" stands for "parenthesis-bounded matrix".
                   % That's how we write matrices in this class.
  \gcd\tup{1, 1} & \gcd\tup{1, 2} & \cdots & \gcd\tup{1, n} \\
  \gcd\tup{2, 1} & \gcd\tup{2, 2} & \cdots & \gcd\tup{2, n} \\
  \vdots & \vdots & \ddots & \vdots \\
  \gcd\tup{n, 1} & \gcd\tup{n, 2} & \cdots & \gcd\tup{n, n}
 \end{pmatrix} .
\]

Let $L$ be the $n \times n$-matrix
\[
 \tup{ \ive{j \mid i} }_{1 \leq i \leq n, \ 1 \leq j \leq n }
 =
 \begin{pmatrix}
  \ive{1 \mid 1} & \ive{2 \mid 1} & \cdots & \ive{n \mid 1} \\
  \ive{1 \mid 2} & \ive{2 \mid 2} & \cdots & \ive{n \mid 2} \\
  \vdots & \vdots & \ddots & \vdots \\
  \ive{1 \mid n} & \ive{2 \mid n} & \cdots & \ive{n \mid n}
 \end{pmatrix} .
\]

Let $D$ be the $n \times n$-matrix
\[
 \tup{ \ive{i = j} \phi\tup{i} }_{1 \leq i \leq n, \ 1 \leq j \leq n }
 =
 \begin{pmatrix}
  \phi\tup{1} & 0 & 0 & \cdots & 0 \\
  0 & \phi\tup{2} & 0 & \cdots & 0 \\
  0 & 0 & \phi\tup{3} & \cdots & 0 \\
  \vdots & \vdots & \vdots & \ddots & \vdots \\
  0 & 0 & 0 & \cdots & \phi\tup{n}
 \end{pmatrix} .
\]

Prove that\footnote{We are using the standard notation $A^T$ for
the \textit{transpose} of a matrix $A$. This transpose is defined
as follows:
If $A = \tup{ a_{i, j} }_{1 \leq i \leq n, \ 1 \leq j \leq m }$,
then
$A^T = \tup{ a_{j, i} }_{1 \leq i \leq m, \ 1 \leq j \leq n }$.}
$G = LDL^T$.

[\textbf{Hint:} Diagonal matrices are particularly easy
to multiply with other matrices. For example, given a
diagonal matrix
$\mathbf{D} = \tup{ \ive{i = j} d_i }_{1 \leq i \leq n, \ 1 \leq j \leq n }$
(where $d_1, d_2, \ldots, d_n$ are $n$ elements of a ring $\KK$)
and an arbitrary matrix
$\mathbf{A} = \tup{ a_{i, j} }_{1 \leq i \leq n, \ 1 \leq j \leq m }$
(over the same ring $\KK$),
the product $\mathbf{D} \mathbf{A}$ is simply given by
\begin{align}
 \mathbf{D} \mathbf{A} =
 \tup{ d_i a_{i, j} }_{1 \leq i \leq n, \ 1 \leq j \leq m } .
 \label{eq.matrix.gcd-LDU.row-mult}
\end{align}
(That is, multiplying by a diagonal matrix on the left is tantamount
to rescaling each row by the corresponding entry of the diagonal
matrix.)
You can use the formula \eqref{eq.matrix.gcd-LDU.row-mult} without
proof (though its easy proof is instructive).]

\subsection{Remark}

As the names suggest, the matrix $L$ is lower-triangular
(so that the matrix $L^T$ is upper-triangular),
and the matrix $D$ is diagonal.
Thus, $G = LDL^T$ is an instance of an
\href{https://en.wikipedia.org/wiki/LU_decomposition#LDU_decomposition}{\textit{LDU decomposition}}.

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 4
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 4: Idempotent and involutive elements}

\subsection{Problem}

Let $\KK$ be a ring.

An element $a$ of $\KK$ is said to be \textit{idempotent}
if it satisfies $a^2 = a$.

An element $a$ of $\KK$ is said to be \textit{involutive}
if it satisfies $a^2 = 1$.

\begin{enumerate}
 \item[\textbf{(a)}] Let $a \in \KK$.
 Prove that if $a$ is idempotent, then $1 - 2a$ is
 involutive.

 \item[\textbf{(b)}] Now, assume that $2$ is
 \textit{cancellable} in $\KK$; this means that if
 $u$ and $v$ are two elements of $\KK$ satisfying
 $2u = 2v$, then $u = v$.
 Prove that the converse of the claim of part
 \textbf{(a)} holds:
 If $a \in \KK$ is such that $1 - 2a$ is involutive,
 then $a$ is idempotent.

 \item[\textbf{(c)}] Now, let $\KK = \ZZ / 4$.
 Find an element $a \in \KK$ such that $1 - 2a$
 is involutive, but $a$ is not idempotent.
\end{enumerate}

\subsection{Remark}

The idempotent elements of $\RR$ are $0$ and $1$.
The involutive elements of $\RR$ are $1$ and $-1$.
A matrix ring like $\RR^{n \times n}$ usually has
infinitely many idempotent elements (viz., all
projection matrices on subspaces of $\RR^n$) and
infinitely many involutive elements (viz., all
matrices $A$ satisfying $A^2 = I_n$; for instance,
all reflections across hyperplanes are represented
by such matrices).

Part \textbf{(a)} of this exercise assigns an involutive
element to each idempotent element of $\KK$.
If $2$ is invertible in $\KK$ (that is, if the element
$2 \cdot 1_{\KK}$ has a multiplicative inverse), then this
assignment is a bijection (as can be easily derived from
part \textbf{(b)}).
Part \textbf{(c)} shows that we cannot drop the
``$2$ is cancellable'' condition in part \textbf{(b)}.

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 5
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 5: The matrix approach to Fibonacci numbers}

\subsection{Problem}

Let $A$ be the $2 \times 2$-matrix
$\begin{pmatrix}
  0 & 1 \\
  1 & 1
 \end{pmatrix}$
over $\ZZ$.
Consider also the identity matrix $I_2 \in \ZZ^{2 \times 2}$.

Let $\calF$ % I have defined \calF to mean \mathcal{F} for this problem.
be the subset
\[
 \set{ aA + bI_2 \mid a, b \in \ZZ }
 =
 \set{ \begin{pmatrix} b & a \\ a & a+b \end{pmatrix} \mid a, b \in \ZZ }
\]
of the matrix ring $\ZZ^{2 \times 2}$.

\begin{enumerate}

\item[\textbf{(a)}]
Prove that $A^2 = A + I_2$.

\item[\textbf{(b)}]
Prove that the set $\calF$ (equipped with the addition of matrices,
the multiplication of matrices, the zero $0_{2 \times 2}$ and
the unity $I_2$) is a commutative ring. \\
(Again, you don't need to check the ring axioms, as we already
know that they hold for arbitrary matrices and thus all the more
for matrices in $\calF$.
But you do need to check commutativity of multiplication in
$\calF$, since it does not hold for arbitrary matrices.
You also need to check that $\calF$ is closed under addition
and multiplication and has additive inverses.)

\end{enumerate}

Let $\tup{f_0, f_1, f_2, \ldots}$ be the Fibonacci sequence (which
we have already encountered on
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/hw5s.pdf}{homework set \#5}).
Recall that it is defined recursively by
\[
f_0 = 0, \qquad
f_1 = 1, \qquad \text{and} \qquad
f_n = f_{n-1} + f_{n-2} \text{ for all } n \geq 2 .
\]

\begin{enumerate}

\item[\textbf{(c)}]
Prove that $A^n = f_n A + f_{n-1} I_2$ for all positive
integers $n$.

\item[\textbf{(d)}]
Prove that
$f_{n+m} = f_n f_{m+1} + f_{n-1} f_m$ for all positive
integers $n$ and all $m \in \NN$.

\end{enumerate}

Now, define a further matrix $B \in \calF$ by
$B = \tup{-1}A + 1I_2 = I_2 - A$.

\begin{enumerate}

\item[\textbf{(e)}]
Prove that $B^2 = B + I_2$ and
$B^n = f_n B + f_{n-1} I_2$ for all positive
integers $n$.

\item[\textbf{(f)}]
Prove that
$A^n - B^n = f_n \tup{A - B}$ for all $n \in \NN$.

\item[\textbf{(g)}]
Prove (again!) that $f_d \mid f_{dn}$ for any nonnegative
integers $d$ and $n$.

% Prove that
% \[
%  A^n \begin{pmatrix} 0 \\ 1 \end{pmatrix}
%  = \begin{pmatrix} f_n \\ f_{n+1} \end{pmatrix}
%  \qquad \text{ for all } n \in \NN .
% \]
\end{enumerate}

[\textbf{Hint:} One way to prove \textbf{(d)} is by
comparing the $\tup{1, 1}$-th entries of the two
(equal) matrices $A^n A^{m+1}$ and $A^{n+m+1}$, after
first using part \textbf{(c)} to expand these matrices.

For part \textbf{(g)}, compare the $\tup{1, 1}$-th
entries of the matrices $A^d - B^d$ and $A^{dn} - B^{dn}$,
after first proving that $A^d - B^d \mid A^{dn} - B^{dn}$ in
the commutative ring $\calF$.
Note that divisibility is a tricky concept in general
rings, but $\calF$ is a commutative ring, which lets
many arguments from the integer setting go
through unchanged.]

\subsection{Remark}

Contrast the ring $\calF$ with the ring $\ZZ\ive{\phi}$
from Exercise 5 on
\href{http://www.cip.ifi.lmu.de/~grinberg/t/19s/hw5s.pdf}{homework set \#5}.
Both of these rings, as we see, can be used to prove that $f_d \mid f_{dn}$
for any nonnegative integers $d$ and $n$.
What else do these rings have in common?

\subsection{Solution}

[...]

%----------------------------------------------------------------------------------------
%	EXERCISE 6
%----------------------------------------------------------------------------------------
\horrule{0.3pt} \\[0.4cm]

\section{Exercise 6: ISBNs vs. fat fingers}

\subsection{Problem}

An \textit{ISBN} shall mean a $10$-tuple
$\tup{a_1, a_2, \ldots, a_{10}} \in \set{0, 1, \ldots, 10}^{10}$
such that
\[
1 a_1 + 2 a_2 + \cdots + 10 a_{10} \equiv 0 \mod 11 .
\]

\noindent
(For example, the $10$-tuple $\tup{1, 1, \ldots, 1}$ is an ISBN.)

Prove the following:

\begin{enumerate}

\item[\textbf{(a)}]
If $\mathbf{a} = \tup{a_1, a_2, \ldots, a_{10}}$ and
$\mathbf{b} = \tup{b_1, b_2, \ldots, b_{10}}$ are two ISBNs
that are equal in all but one entry (i.e.,
there exists some $k \in \set{1, 2, \ldots, 10}$
such that $a_i = b_i$ for all $i \neq k$),
then $\mathbf{a} = \mathbf{b}$.

\item[\textbf{(b)}]
If an ISBN $\mathbf{a} = \tup{a_1, a_2, \ldots, a_{10}}$ is
obtained from an ISBN
$\mathbf{b} = \tup{b_1, b_2, \ldots, b_{10}}$ by swapping
two entries (i.e., there exist $k, \ell \in \set{1, 2, \ldots, 10}$
such that $a_k = b_\ell$ and $a_\ell = b_k$ and
$a_i = b_i$ for all $i \notin \set{k, \ell}$),
then $\mathbf{a} = \mathbf{b}$.

\end{enumerate}

\subsection{Remark}

What we called ISBN here is essentially the definition
of an \href{https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digits}{ISBN-10}
-- an international standard for book identifiers used from the
1970s until 2007.
For example, the ISBN-10 of the Graham/Knuth/Patashnik
book ``Concrete Mathematics'' is ``0-201-55802-5'', which corresponds
to $\tup{0, 2, 0, 1, 5, 5, 8, 0, 2, 5}$; you can check
that this is indeed an ISBN according to our definition.

(An ``X'' in a real-life ISBN stands for an entry that
is $10$.)

As this exercise shows, ISBNs have an error-detection
property:
If you make a typo in a single digit or accidentally swap
two digits, the result will not be an ISBN, so you will
know that something has gone wrong.
This helps you avoid ordering the wrong book from a bookstore
or library.
\href{https://en.wikipedia.org/wiki/Luhn_algorithm}{Credit card numbers have a similar error-detection feature}.

This is one of the simplest examples of an
\href{https://en.wikipedia.org/wiki/Error_correction_code}{error correction code}.
We may or may not see more of them in class.
For now, you can think about how to define ``ISBNs''
\begin{itemize}
\item in $\set{0, 1, \ldots, 4}^4$;
\item in $\set{0, 1, \ldots, 6}^6$;
\item in $\set{0, 1, \ldots, 8}^8$ (this is harder!).
\end{itemize}

\subsection{Solution}

[...]

\begin{thebibliography}{99999999}                                                                                %

% Feel free to add your sources -- or copy some from the source code
% of the class notes ( http://www.cip.ifi.lmu.de/~grinberg/t/19s/notes.tex ).

\end{thebibliography}

\end{document}


% Author: Howard Liu
\documentclass[a4paper]{article}
\usepackage[
a4paper, left=2cm, right=2cm, top=2cm, bottom=2cm, portrait
]{geometry}
\usepackage{multicol}
\usepackage{ragged2e}
\usepackage{amsmath}

% STYLE DEFINITION %
\pagestyle{empty}
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
	{-1ex plus -.5ex minus -.2ex}%
	{0.5ex plus .2ex}%x
	{\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
	{-1explus -.5ex minus -.2ex}%
	{0.5ex plus .2ex}%
	{\normalfont\normalsize\bfseries}}

\begin{document}
	\pagenumbering{gobble}              % No page numbers
	% TITLE %
	\begin{center}
		{\large CS2100 Computer Organisation Cheatsheet}\\{for Final of AY 19/20 Semester 2, by Howard Liu}
	\end{center}

    \begin{justifying}
    	\setlength\parindent{0em}           % No indents at start of paragraphs
    	\setlength\parskip{0.5em}           % Increase spacing between paragraphs
    	\begin{multicols}{2}
    		\section{IEEE 754 Floating Point}

    		Given a 32-bit single precision IEEE 754 floating point number \textit{A}, we have the three parts:

    		\begin{itemize}
    			\item A[31:31] - Sign bit, 0 for +ve, 1 for -ve;
    			\item A[30:23] - Exponent bits (8 bits)
    			\item A[22:0]  - Mantissa bits (24 bits)
    		\end{itemize}

    	    From a decimal form, we can calculate the IEEE 754 representation in the following steps (taking number \textbf{85.125} as an example):

    	    \begin{enumerate}
    	    	\item Covert integer part and decimal part into binary
    	    	    \begin{enumerate}
    	    	      	\item $85 = (1010101)_2$
    	    	      	\item $.125 = (.001)_2$
    	    	    \end{enumerate}
        	    \item We have $1010101.001$ now. Make it only have one bit in the integer part, hence we do right shift for 6 bits. ($1010101.001 = 1.010101001 * 2^6$)
        	    \item We calculate the exponent bit. For single precision, $\text{exponent} = 127 + \text{power of 2}$ . Hence we have 133 for this example. ($133 = (10000101)_2$)
        	    \item Put everything together with 0 fills at the back, we have the binary representation in IEEE 754 format:\\
        	        (0 1000101 010101001 00000000000000)$_2$ = 0x42AA4000
        	    \item Sometimes numbers cannot be represented precisely in binary (e.g. \textbf{0.3}), we do the binary conversion until we have the full 23-bit Mantissa.
    	    \end{enumerate}

            \section{Binary convertion}

            To convert a decimal point to binary, taking \textbf{0.125} as an example, we do this:

            \begin{center}
            	\begin{tabular}{lll}
            		\hline
            		Decimal Number & Result & Binary \\
            		\hline
            		$0.125 * 2$ & $\textbf{0}.25$ & $0$ \\
            		$0.25 * 2$ & $\textbf{0}.50$ & $0$ \\
            		$0.50 * 2$ & $\textbf{1}.00$ & $1$ \\
            		$0.00 * 2$ & $\textbf{0}.00$ & $0$ \\
            		\hline
            	\end{tabular}
            \end{center}

            * Binary number is read from top to bottom. In this case, $.125 = (.001)_2$ .

            \section{Negating Numbers}

            To negate a binary number, we have
            \begin{itemize}
                \item Sign-and-Magnitude: just flip MSB to negate a number ($0001_2 \rightarrow 1001_2$)
                \item 1s Complement: flip every bits to negate a number ($0001_2 \rightarrow 1110_2$)
                \item 2s Complement: add 1 to \textbf{LSB} of 1s Complement ($0001.01_2 \rightarrow 1110.10_2 \rightarrow 1110.11_2$)
            \end{itemize}

            We can detect overflow if either happens:
            \begin{itemize}
                \item +ve add +ve become -ve
                \item -ve add -ve become +ve
            \end{itemize}

            When performing binary addition on \textbf{1s-complement} numbers, if there is a carry out of the MSB, add 1 to the result and dispose the MSB. We do \textbf{NOT} need to do so for addition on \textbf{2s-complement} numbers.

            \section{Boolean algebra laws and theorems}

            Duality: if the AND/OR operators and identity elements 0/1 in a Boolean equation are interchanged, it remains valid (but brackets are to be added when necessary). (e.g. $X + X \cdot Y = X \rightarrow X \cdot (X + Y) = X$)

            Laws:
            \begin{itemize}
                \item Identity: $A + 0 = 0 + A = A$
                \item Inverse/Complement: $A + A' = 1$
                \item Commutative: $A + B = B + A$
                \item Associative: $A + (B + C) = (A + B) + C$
                \item Distributive: $A + (B \cdot C) = A \cdot B + A \cdot C$
            \end{itemize}

            Theorems:
            \begin{itemize}
                \item Idempotency: $X + X = X$
                \item One element/Zero element: $X + 1 = 1$
                \item Involution: $(X')' = X$
                \item Absorption 1: $X + X \cdot Y = X$
                \item Absorption 2: $X + X' \cdot Y = X + Y$
                \item DeMorgans': $(X + Y)' = X' \cdot Y'$
                \item Consensus: $X \cdot Y + X' \cdot Z + Y \cdot Z = X \cdot Y + X' \cdot Z$
            \end{itemize}

            \section{Flip-flop reference}

            Characteristic Tables:

            \begin{multicols}{2}
                \begin{center}
                    \begin{tabular}{cc|c}
                        J & K & Q$^+$ \\
                        \hline
                        0 & 0 & Q \\
                        0 & 1 & 0 \\
                        1 & 0 & 1 \\
                        1 & 1 & Q(t)' \\
                    \end{tabular}

                    \begin{tabular}{c|c}
                        D & Q$^+$ \\
                        \hline
                        0 & 0 \\
                        1 & 1 \\
                    \end{tabular}

                    \begin{tabular}{cc|c}
                        S & R & Q$^+$ \\
                        \hline
                        0 & 0 & Q \\
                        0 & 1 & 0 \\
                        1 & 0 & 1 \\
                        1 & 1 & Unpredictable \\
                    \end{tabular}

                    \begin{tabular}{c|c}
                        T & Q$^+$ \\
                        \hline
                        0 & Q \\
                        1 & Q' \\
                    \end{tabular}
                \end{center}
            \end{multicols}

            Excitation Tables:

            \begin{multicols}{2}
                \begin{center}
                    \begin{tabular}{cc|cc}
                        Q & Q$^+$ & J & K \\
                        \hline
                        0 & 0 & 0 & X \\
                        0 & 1 & 1 & X \\
                        1 & 0 & X & 1 \\
                        1 & 1 & X & 0 \\
                    \end{tabular}

                    \begin{tabular}{cc|c}
                        Q & Q$^+$ & D \\
                        \hline
                        0 & 0 & 0 \\
                        0 & 1 & 1 \\
                        1 & 0 & 0 \\
                        1 & 1 & 1 \\
                    \end{tabular}

                    D = Q$^+$

                    \begin{tabular}{cc|cc}
                        Q & Q$^+$ & S & R \\
                        \hline
                        0 & 0 & 0 & X \\
                        0 & 1 & 1 & 0 \\
                        1 & 0 & 0 & 1 \\
                        1 & 1 & X & 0 \\
                    \end{tabular}

                    \begin{tabular}{cc|c}
                        Q & Q$^+$ & T \\
                        \hline
                        0 & 0 & 0 \\
                        0 & 1 & 1 \\
                        1 & 0 & 1 \\
                        1 & 1 & 0 \\
                    \end{tabular}

                    T = Q $\oplus$ Q$^+$
                \end{center}
            \end{multicols}

            \section{Notes about pipeline}
            Considering instruction A and B. B is just the instruction executed after A.
            \begin{itemize}
                \item A pipeline stage of B will always behind the same stage for A, even if the resource is not occupied and there is no data dependency / control hazard. (e.g. D stage of instruction B cannot be executed at cycle 3, even D is vacant)
                \begin{center}
                    \begin{tabular}{c|cccccccc}
                        Inst & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
                        \hline
                        A    & F &   &   & D & E & M & W &   \\
                        B    &   & F &   &   & D & E & M & W \\
                    \end{tabular}
                \end{center}
                \item For $N$ instructions, ideal 5-stage pipeline takes $N+2$ cycles to execute.
                \item Delays introduced when no forwarding, no early branching and no branch prediction.
                \begin{itemize}
                    \item Data dependency: \textbf{2 cycles}
                    \item Control hazard (beq/bne, j): \textbf{3 cycles}
                \end{itemize}
                \item Delays introduced when there is forwarding: \textbf{1 cycle} for data dependency after \textbf{lw}.
                \item Data dependency for \textbf{beq/bne} instruction can lead to delay \textbf{when forwarding and early branching is implemented}. This is because data is required in stage 2 (D), not stage 3 (E), of the branching instruction.
                \item When branch prediction is wrong, flush everything including the correct ones to be executed. This is especially true when there is \textbf{no early branching}. Considering the following example: assuming not taken is used, but actually A branched to C. C and D is executed once again after everything flushed.
                \begin{center}
                    \begin{tabular}{l|cccccccc}
                        Inst       & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
                        \hline
                        A (beq)    & F & D & E & M & W &   &   &   \\
                        B          &   & F & D & E & X & X &   &   \\
                        C          &   &   & F & D & X & X & X &   \\
                        D          &   &   &   & F & X & X & X & X \\
                        C (again)  &   &   &   &   & F & D & E & M \\
                    \end{tabular}
                \end{center}
            \end{itemize}

            \section{Cache}

            Average Access Time formula:
            $$ \text{Hit rate} \times \text{Hit time} + (1 - \text{Hit rate}) \times \text{Miss penality}$$

            \section{Quick reference}

            if (a $<$ b) \{...\} else \{...\}

            \begin{center}
                \begin{tabular}{|l|}
                    \hline
                    slt \$t0, \$a, \$b \\
                    beq \$t0, \$zero, else \\
                    \hline
                \end{tabular}
            \end{center}

            if (a $>=$ b) \{...\} else \{...\} : flip a and b in the above example.

            \section{Miscellaneous notes}

            \begin{itemize}
                \item Zero-extension and Sign-extension is different especially in the case that the number starts with 1 in binary.
                \item MIPS does not have \textbf{not} instruction. We use \textbf{nor \$1, \$1, \$zero} to do bitwise not.
            \end{itemize}
    	\end{multicols}
    \end{justifying}
\end{document}


%!TEX root = /home/renaud/Documents/EPL/tfe/latex/tfe.tex
\chapter{Structure of a compartment model for tracer transport} \label{chap:compartment}
% The previous chapters cover the material needed to derive compartments from the dynamics of a tracer transport model. But deciding which subdomains define the compartments is only the first step in building a compartment model.
A relevant compartment model should reproduce the properties covered in section~\ref{sec:propcontinuous} for the corresponding continuous model. In this chapter, a compartment model structure that fulfills that requirement is proposed, and the corresponding matrix form is derived. This chapter is widely inspired from \cite{deleersnijder2014compartment} and \cite{delhez2010compartment}.

\input{inputs/compartments/formulation}
\input{inputs/compartments/propcomp}
\input{inputs/compartments/matrixformulation}
\input{inputs/compartments/discretetime_comp_model}