<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="https://www.w3.org/2005/Atom">
  <channel>
    <title>rubber duck typing</title>
    <link>https://rubber-duck-typing.com/</link>
    <description>A blog about programming, software engineering, and computer science</description>
    <language>en-us</language>
    <atom:link href="https://rubber-duck-typing.com/rss.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Challenges of specifying and compiling gas-fueled languages</title>
      <link>https://rubber-duck-typing.com/posts/2026-03-14-compiler-correctness-gas-challenge.html</link>
      <guid>https://rubber-duck-typing.com/posts/2026-03-14-compiler-correctness-gas-challenge.html</guid>
      <pubDate>Sat, 14 Mar 2026 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
In the <a href="2024-09-20-bugs-in-compilers.html">previous post on compiler correctness</a> we asked: how do you know a
compiler has a bug? The answer is surprisingly hard. Most languages have no
formal spec. Comparing program behavior is undecidable. Programs may have
multiple possible behaviors, or even no specified behavior at all. Even experts
disagree on what counts as a bug.
</p>

<p>
Some programs run on platforms where every instruction costs money. Solidity
programs, for example, compile to EVM bytecode. Each bytecode instruction
consumes <i>gas</i>. You buy gas with dollars or cryptocurrency, then fuel your
virtual machine sufficiently so it can complete the run. Should you run out of
gas mid-execution, your computations will just heat the atmosphere. Money
wasted, results disappeared.
</p>

<p>
People familiar with PLT know gas by the name <i>fuel</i>.
</p>

<p>
Compilers can't ignore gas. Two problems emerge. First: programs can read their
gas meter, directly or indirectly. Two compilers targeting the same platform
will produce code that burns gas at different rates, the program can see this
and act differently. Second: different platforms measure gas differently. A
compiler targeting multiple platforms must track multiple fuel meters.
</p>

<p>
In this post I'd like to explore the different roles of gas and why it affects
the observable behavior beyond programs running out of gas. For now, we focus on
the proper problem statement and investigate the tradeoffs between the
predictability of gas consumption and the scale of compiler optimizations. The
problems are universal to any language where the program behavior may depend on
the exact gas meter values, but if we speak about Ethereum, the most popular
language for the contracts there is Solidity. So I will use it to illustrate my
points.
</p>
<div id="outline-container-org815e614" class="outline-2">
<h2 id="org815e614">Behavior equivalence and extensional correctness</h2>
<div class="outline-text-2" id="text-org815e614">
<p>
We have to start with the program behaviors in general. How can a program
behave, in general?  It can produce output, then terminate, panic, or loop
forever; or it can keep producing output indefinitely. The common approach,
<i>extensional compiler correctness</i>, says the behavior is a sequence of <i>observable
events</i>, such as external calls or I/O.
</p>

<p>
For something like Solidity, the events are logs, calls, storage writes; the
behavior kinds are, roughly:
</p>

<ul class="org-ul">
<li>success with: a trace of events, some return data, and the leftover gas;</li>
<li>revert with data, and the leftover gas;</li>
<li>panic / OOG (different from revert).</li>
</ul>

<p>
The semantic of Solidity language should define a behavior for a valid Solidity
program. The semantic of its compilation target, EVM, defines behavior for EVM
bytecode. <i>Extensional compiler correctness property</i> says: a compiler is correct
if the behavior of EVM bytecode matches the behavior of Solidity program.
</p>

<p>
If a Solidity program has more than one possible behaviors, the compiler should
select one and make it the behavior of compiled EVM bytecode. It is a
refinement: we may lose some possible Solidity behaviors, but we don't let new
behaviors in.
</p>

<p>
In a more general case, both source and target languages may allow multiple
defined behaviors. Then a compiler is extensionally correct if all behaviors of
compiled program are in the set of behaviors of source program.
A bit simplified:
</p>

<p>
\[\forall P, Behaviors_{target\_language}(compiled(P)) \subset Behaviors_{source\_language}(P)\].
</p>
</div>
<div id="outline-container-org7a1e004" class="outline-3">
<h3 id="org7a1e004">Undefined behavior</h3>
<div class="outline-text-3" id="text-org7a1e004">
<p>
A program may also violate the language rules. Such programs are meaningless.
Compiler might reject them outright or silently compile them into code that does
anything at all.
</p>

<p>
Which programs have a defined behavior, and which do not? In most languages, the
rules are notoriously complex. Worse even: languages lack standards, so these
rules are not even put together in a coherent description.
</p>

<p>
Are two programs with undefined behavior equivalent? Some theorists say yes,
undefined is undefined. Engineers disagree &#x2013; compilers usually produce
something mildly predictable even from programs with undefined behavior. A
program having, on paper, undefined behavior, stays docile for years, until some
edge case drives it crazy and it corrupts your database. Engineers have to work
with languages and codebases where undefined behavior will eventually happen, so
they strongly prefer predictable, not chaotic, undefined behavior.
</p>
</div>
</div>
</div>
<div id="outline-container-org6851282" class="outline-2">
<h2 id="org6851282">Resource-observational equivalence</h2>
<div class="outline-text-2" id="text-org6851282">
<p>
A trace of observable events is too simplified to catch all important properties
of programs. Say, the updated compiler produces functionally equivalent, but
100x slower programs. It reads like a bug  and we will probably call it so &#x2013; so
is this compiler still "correct" in the mundane engineering sense?
Another example: the new optimization pass made it so that most of the time the
code is running 2x faster, but on rare occasions it lags and stops responding
for an hour. Is it acceptable? Now what if the lag is not an hour, just one
extra microsecond, but the attacker can exploit it indefinitely and keep your
code lagging forever? Finally, imagine you are a mobile developer and after
updating your compiler whatever app you build for Android drains the battery in
an hour. Is it a compiler bug?
</p>

<p>
Extensional compiler correctness is oblivious to the resource consumption,
including time and power; it does not understand average or worst case; it can
not capture the time to respond. It is all about "what" and nothing about "how".
</p>

<p>
But it gets worse. If the program can inspect the "how", then optimizations
change semantics.  Imagine a function \(f\) that typically takes 11ms to complete,
and the following code.
</p>

<div class="org-src-container">
<pre class="src src-nil">started = clock()
f();
elapsed = clock() - started;

if (elapsed &lt; 10ms) {
    print "yes"
}
else {
    print "no"
}
</pre>
</div>

<p>
We branched on the timer value: if the program has run quicker than 10ms, do
one thing, otherwise do another. Now suppose we launch compiler with different
optimization options, or we have updated the compiler, or switched to a better
one. Function \(f\) is now optimized and runs under 10ms every time. The
optimization changes the behavior, not just <i>how</i> but <i>what</i>!
</p>
</div>
</div>
<div id="outline-container-org742f1c5" class="outline-2">
<h2 id="org742f1c5">Gas</h2>
<div class="outline-text-2" id="text-org742f1c5">
<p>
Gas is one of exposed resources, and it is hard to ignore. Of course, we care
about compilers producing gas efficient programs that cost less, and we care
about gas attacks too. But if a program can also read the gas meter, the same
way they could read a timer, they can branch on the gas value, producing
different behaviors. Again, the resource is not just affecting "how" the program
runs but also "what" it does.
</p>
</div>
<div id="outline-container-org8df205a" class="outline-3">
<h3 id="org8df205a">Explicit dependency: branching on gas meter</h3>
<div class="outline-text-3" id="text-org8df205a">
<p>
In Solidity, <code>gasleft()</code> reads the gas meter.
</p>

<div class="org-src-container">
<pre class="src src-solidity">if (gasleft() &gt; 100000) {
  assembly {
    invalid()
  }
  // be silly and crash if we have too much gas
}
</pre>
</div>

<p>
The official documentation mentions <code>gasleft()</code> twice: <a href="https://docs.soliditylang.org/en/latest/units-and-global-variables.html">here</a> and <a href="https://docs.soliditylang.org/en/latest/units-and-global-variables.html">there</a>. It is not a
lot of information &#x2013; for example, to which extent can we reorder <code>gasleft()</code>
calls? How do optimizations affect its value, in general? So for now let us
informally interpret <code>gasleft()</code> as a built-in that outputs a numeric value of the
gas meter in a fully deterministic way. This is primitive, but intuitive, and we
need to start somewhere.
</p>
</div>
</div>
<div id="outline-container-orgf4163e2" class="outline-3">
<h3 id="orgf4163e2">Implicit dependency on gas: calls and try/catch</h3>
<div class="outline-text-3" id="text-orgf4163e2">
<p>
Unlike in the timer example, we can diverge on gas values even without reading
the gas meter. Again, two compilers may compile this fragment so that the
resulting code diverges on the same input (and given the same initial gas).
</p>

<div class="org-src-container">
<pre class="src src-solidity">try {
  somecall(); // consume gas
}
catch {
  // executes if `somecall` did not get enough gas
}
</pre>
</div>

<p>
In the current iteration of Ethereum protocol, there are two options regarding
how much gas to forward for a call:
</p>

<ol class="org-ol">
<li>explicitly tell how much gas we forward<sup><a id="fnr.1" class="footref" href="#fn.1" role="doc-backlink">1</a></sup> , or</li>
<li>use the default &#x2013; which is set to 63/64 of the remaining gas.</li>
</ol>

<p>
The unbound calls, like in the example above, will provide a different amount of
gas to the callee depending on the remaining gas at the moment of the call.
The rate of fuel consumption, in turn, depends on the compiler optimizations.
The callee then can also diverge, because the exact value of gas it received is
different.
</p>
</div>
</div>
<div id="outline-container-orgb50d953" class="outline-3">
<h3 id="orgb50d953">Optimization goals</h3>
<div class="outline-text-3" id="text-orgb50d953">
<p>
So, gas is a resource, but it actively bleeds into observable behavior even if
we don't explicitly keep track of the gas gauge. Gas optimizations affect
behavior. Still, one of raisons d'être of compilers and higher level languages
is to optimize programs, and do it better than humans. For most Solidity
programs we want to get away with as little gas as possible, because gas costs
money. Do we want to turn off optimizations to maximize predictability of gas
consumption, then force all compilers to adhere to that? This contradiction is
the core of the problem we uncovered here.
</p>

<p>
To sum up: optimizations alter behaviors that depend on resources, including
gas. As long as you have one compiler, you may get away with hand-waving about
how programs should behave. "Just compile and run and see what's it doing."
With more compilers in play we require a contract between them. This contract
should describe what can we expect from a program, no matter which compiler we
use; it also elucidates how correct optimizations may interact with
gas-dependent behaviors.
</p>
</div>
</div>
</div>
<div id="outline-container-org36482f7" class="outline-2">
<h2 id="org36482f7">Roles of Gas</h2>
<div class="outline-text-2" id="text-org36482f7">
<p>
To make matters more complicated, gas is a beast with not one, but three heads.
Each head bites the compiler correctness issue from a different side.
</p>

<p>
<b>Gas is an observable effect</b>. Publish the value of <code>gasleft()</code> in the storage, or
introduce gas-dependent control flow &#x2013; it is now affecting the trace.
</p>

<p>
<b>Gas is a resource bound.</b> An optimization can make a cold path more expensive to
reduce the average cost. Then unoptimized code may complete when provided the
bare minimum of gas, but optimized code on the cold path will run out of gas.
Most formal specifications of compiler correctness will treat it as a change of
behavior, meaning that such compilation is not correct.
</p>

<p>
<b>Gas is a part of execution environment.</b> A platform upgrade adjusts the cost of
instructions, or changes how gas passes through cross-boundary call. Programs
that were terminating correctly may now run out of gas. In Ethereum ecosystem
this has already happened e.g. EIP-2929 repricing, or EIP-1884.
</p>

<p>
We can classify programs by how gas affects their observable behavior:
</p>

<dl class="org-dl">
<dt>gas-sensitive</dt><dd>behavior depends on exact gas (reading gas meter value,
branching on it, putting it to storage etc.). Gas is an observable effect
here.</dd>
<dt>gas-insensitive</dt><dd>changing gas consumption does not change observable trace,
unless we run OOG. Gas is just a resource bound here, except for potential OOG
that we can observe.</dd>
<dt>gas-fragile</dt><dd>programs break if we change the protocol parameters such as gas
budgets / stipends / EIP-150 gas forwarding 63/64 cap/instruction costs. The
way environment treats gas matter here.</dd>
</dl>

<p>
Note that these are not properties of a single contract; they are properties of
a contract execution, which means <b>they span across the whole call tree</b>. A
contract might be gas-insensitive, but once it calls another, gas-fragile
contract, the whole thing becomes brittle. In an open ecosystem like Ethereum,
this is important, because if you call an upgradable contract, you have to trust
that the upgrade won't break your gas assumptions about it. Of course, the
callee has to be <i>aware</i> of such assumptions in the first place.
</p>
</div>
</div>
<div id="outline-container-orgc033ddd" class="outline-2">
<h2 id="orgc033ddd">Multiple backends give resources different meaning</h2>
<div class="outline-text-2" id="text-orgc033ddd">
<p>
The second problem mirrors the first: compiling the gas-aware language for a
target with different gas consumption. How to compile Solidity for gas-fueled
RISC-V? Maybe we should treat Solidity gas as some abstract resource, the
platform-native gas? Then retrieving the exact gas values should be either
prohibited or made into platform-dependent language extension. Or, perhaps,
Solidity gas should map precisely to EVM gas, its native counterpart, but on
other platforms we will have to simulate it. Then the EVM gas becomes
meaningless on other platforms, because they only accept their proper gas as a
payment for running code. Programs won't survive depleting neither native nor
simulated gas.
</p>

<p>
We circle back to <code>gasleft()</code> here: its intention seem to reflect an approach
"just expose the internals as they are". <code>gasleft()</code> inherits its semantic from
EVM and the protocol in general. The meaning is platform-specific and, for the
higher-level language, almost accidental. Trying to support another compilation
target when Solidity and EVM are tightly coupled together feels like compiling
x86 assembly to ARM, while trying to keep stack usage perfectly aligned.
</p>

<p>
Worse even: in EVM, the cost of an instruction that touches memory depends on
the current memory size (roughly a quadratic law). Storing something outside the
current memory bound triggers expansion and you pay for every new byte. Move a
memory-touching instruction  earlier and you might expand memory earlier. Later
instructions touching memory will do it at a change cost. If we want a perfect
gas virtualization, we have to account for this too. And moving around memory
operations for optimization purposes may affect observable behavior through
meddling with the gas consumption.
</p>

<p>
There is another language that likes to expose features from the lower level of
abstraction &#x2013; the good old C. Writing directly to registers, switching CPU
modes and so on belong to platform-specific extensions, implemented in compilers
supporting the respective platforms. They are not portable by definition, and
portable code is decoupled from them. Macro definitions are often used for that.
Gas is more pervasive; without a gas definition generic enough to describe EVM,
and RISC-V, and other targets, we have to virtualize EVM on other targets.
</p>

<p>
Any gas-aware language will have to deal with both problems: how to support
multiple compilers for the same target, and how to support multiple targets.
My intuition is that to solve both of them we need to figure out a formal gas
consumption model and guarantees that it can realistically provide. Furthermore,
we won't be able to have a singular definition of correctness, because there is
a tradeoff between gas consumption predictability and gas efficiency. These
compromises might be more or less friendly to the portability of contracts. To
advance further, we need to figure out the semantic of gas for the language.
Can we do it for Solidity?
</p>
</div>
</div>
<div id="outline-container-org75b894d" class="outline-2">
<h2 id="org75b894d">Semantic of Solidity</h2>
<div class="outline-text-2" id="text-org75b894d">
<p>
Unfortunately, it seems the most popular language to write contracts in Ethereum
ecosystem is long past the no-return point. Now, chaos dragons live here.
</p>

<p>
First, Solidity does not have a complete specification separated from compiler
implementation. This means that the default compiler <code>solc</code> <b>is</b> the description
of the language. So, if your compiled contract acts weirdly, it is challenging,
if even possible, to diagnose whether it is a compiler bug, a bug in the
language specification (which does not exist), or a correct, albeit unintuitive,
behavior.
</p>

<p>
Second, other Solidity compilers have to reinvent the language specification by
reverse engineering the existing compiler, <code>solc</code>. This includes separating out
the compiler bugs. Without a language standard decoupled from <code>solc</code>,
"correctness" is just "what the other guy did." But we do not have a choice at
this point.
</p>

<p>
Third, <code>solc</code> is written in C++, a language rich in undefined behaviors, which are
sometimes borderline impossible to catch. Therefore, <code>solc</code> itself is likely to
exhibit undefined behavior on some inputs. Undefined behavior in the compiler
may be contagious to the programs it operates on. This makes using <code>solc</code> as a
source of truth for the semantics of Solidity even more dangerous. Tomorrow we
recompile <code>solc</code> with a new version of <code>clang</code> or change the arguments in its
invocation. After that, it will perhaps compile Solidity programs into the
bytecode with diverging behavior.
</p>

<p>
The Gas Dragon enters the club and finds itself at home. The gas consumption is
treated as an implementation detail, the guarantees about it are not documented.
Without an explicitly specified interface, the <a href="https://www.hyrumslaw.com/">Hyrum's law</a> applies even to
smallest user bases.
</p>

<p>
A theorist in me shouts: do not make your programs dependent on exact gas
values! Do not make strong assumptions about gas! Distill a language standard
describing "abstract gas" which is guaranteed to satisfy 42 different formal
properties; your programs should respect the constraints of the standard, or
their behavior won't be well defined. In case of a niche language without an
established codebase, used mostly by programmers well versed in CS,
perhaps. We can even throw in something to provide static guarantees about gas
consumption, maybe linear types, resource monads etc. But can we do that to a
language with millions of lines of code already written and deployed? We know
how well such retrofitting worked out for the mess of C(++), right. Ask a couple
of your fellow C programmers to recite the precise definition of a pointer, then
check how far they diverge from the standard. A pointer is not "just a number",
and null pointer is not zero (if you think of <i>zero</i> as a number with all cleared
bits, not the literal <code>0</code> in a pointer context&#x2026;)
</p>

<p>
So, I don't think we can universally retrofit some abstract gas semantics to
Solidity without dealing with the same problems as early standards of C, which
were trying to harmonize some thousand programmers with diverse backgrounds and
qualifications, and diverging ideas about pointers, memory, type conversions and
so on. But maybe we can contain the damage or at least take lessons because any
future gas-aware language will face the problem of gas-dependent observable
behaviors. Solidity, because of how popular it is, shows us how real people
write contracts, what are the challenges, what do they really want from gas
meter.
</p>
</div>
</div>
<div id="outline-container-orgad086f4" class="outline-2">
<h2 id="orgad086f4">What then?</h2>
<div class="outline-text-2" id="text-orgad086f4">
<p>
I believe we still have to investigate the relationship between gas and program
behavior deeper. It will most certainly be a semantic parameterized with the
protocol version; it may describe several possible tradeoffs between predictable
gas consumption and optimizing the costs; it should account for memory expansion
costs and make clear the possible reorderings and optimizations. Even if we
won't be able to fit any or all of them neatly into Solidity, it will give us
ideas about how to design better gas-aware languages. But I'm leaving this for
the next time.
</p>
</div>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">

<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
In reality, what will be send via a bounded call is <code>min(63/64*gasleft(),provided_gas_value)</code>.
</p></div></div>


</div>
</div>]]></description>
    </item>
    <item>
      <title>Argument from recollection in Phaedo is self-illustratory</title>
      <link>https://rubber-duck-typing.com/posts/2025-12-14-plato-clever-reflexivity.html</link>
      <guid>https://rubber-duck-typing.com/posts/2025-12-14-plato-clever-reflexivity.html</guid>
      <pubDate>Thu, 18 Dec 2025 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
Plato impresses me not just with his ideas, but by his writing craftsmanship.
In line with his definition of philosophy as a practice, a process, both content
and form of his writing contribute to its message. What's said is important,
but how it's said adds another layer to the meaning, sometimes illustrative,
sometimes adding a twist of depth. In this post I'd like to share some of these
findings in Phaedo.
</p>

<p>
Phaedo is a dialogue about the immortality of the soul, capturing the final
hours of Socrates. Within the dramatic time of the dialogue, he is convincing
his friends Simmias and Cebes that philosophy is a "practice for dying",
preparing us to leave our bodies and be released from its inconveniences,
enabling us to finally see things for what they are, clearly.  So, the soul is
immortal, death is not to be feared, but welcomed when it comes (not sooner
though, later Socrates adds that gods should decide when our time is over).
</p>

<p>
Socrates gives three arguments for the immortality of the soul, here I'd like to
focus on the argument from recollection, as it has a fascinating quality of
illustrating itself on three levels. To reveal how exactly, let's start by
recollecting the argument itself.
</p>

<p>
Suppose you are looking at two stones, and they seem so similar to you that you
are inclined to judge them as two equal stones.  However, in other context, from
another angle, you might judge them unequal.  Maybe they have equal weight, but
differ by size, or shape, or color.  In sensible world, comparisons are
inherently imperfect: they are unstable, context-dependent, approximate.
</p>

<p>
But to tell that the equality in sensible world is imperfect means to think
about a standard &#x2013; some perfect equality, compared to which the "real world
equality" falls short. But what is this standard, the perfect equality, and
where did we get an idea of it?
</p>

<p>
Socrates argues that the idea of perfect equality can not be based on
experience, because a perfect equality is an abstraction. It does not exist in
the sensible reality, and it belongs to an entirely different ontological
category. He calls such objects Forms.
</p>

<p>
From this, Socrates concludes, that we should have known equality from before
our birth, but then apparently forgotten. Your soul knows Equality, and your
soul existed before your mortal body. As you live and gather sensory experience,
this experience reminds you of the forgotten knowledge through the similarity &#x2013;
like the lackluster sensory equality reminds you of a perfect equality. This
enables the recollection.
</p>

<p>
The Equality is one of the Forms &#x2013; eternal, unchangeable objects
belonging to the ideal realm of Forms. Each Form is, in layman's terms, like an
essence of a kind of things on Earth. In Plato's writings, Socrates repeatedly
mentions that our souls possess all knowledge of Forms but they have forgotten
it as they got embodied on Earth. So, we need to be reminded of it.
</p>

<p>
Socrates notes, how asking Simmias or Cebes a well formulated question about the
nature of the Form of Equality produced an immmediate answer. He engages them in
Socratic questioning: asking and guiding them to the knowledge of universal
truths they have already had in their souls. When Simmias and Cebes are
performing the recollection themselves, under his guidance, the dialogue
illustrates the point of recollection on the second level. Show, and tell.
</p>

<p>
Funnily enough, before the argument starts, in 73b, Simmias claims that he <i>knows</i>
the argument but asks Socrates directly to <i>remind</i> him of it &#x2013; very tongue in
cheek from Plato. Socrates indeed reminds Simmias of the argument, but in two
different senses: directly, by stating the argument that Simmias heard but
"forgot"; and by teaching it to Simmias, which is equivalent to reminding
Simmias of the truths his soul knew all along. Unfortunately, as I do not read
ancient Greek, I can't be sure to which extent the wordplay was intentional.
</p>

<p>
This second possible meaning of <i>remind</i> raises an interesting question: if
Simmias is recollecting the lost knowledge, it must have to do with one of the
Forms, but which one?  At first I was inclined to think that the knowledge is
about the world of Forms, therefore assuming that the world of Forms is a Form
itself. This, however, seems to be a debatable point among Plato scholars, and
for a good reason. Plato did not leave us a clear account on it, so we would
have to deal with nuances such as: does the Form of Forms contain itself (think
Russel's paradox)? Do we need a Form to explain what's in common between the
Form of the World of Forms and individual forms, and thus start an infinite
regress?
</p>

<p>
A better answer might be to say that Simmias learns something about the world of
Forms, which is related to all Forms, therefore gets into the domain of
knowledge accessible by recollection.
Or, perhaps, it's about the Form of Man or Form of Human Being, specifically,
which was mentioned in another dialogue, Parmenides.  Throughout the entirety of
the dialogue, Simmias and Cebes learn about what it is to be a man, how we are
related to our bodies and souls, what is it to live and to die. The argument
from recollection is a part of the picture, elucidating the mechanism by which
we recollect the forgotten knowledge of Forms, accessible to our soul.  Simmias
and Cebes learn about it, but also experience it firsthand.
</p>


<p>
Through the dialogue form, the readers are witnessing the process, but also
guided through the same recollection process as Simmias and Cebes. You too did,
in your soul, know the argument all along, you just needed a reminder. This is
the third level, and here the text breaks the fourth wall.
</p>

<p>
Indeed, we are unable to engage with the text interactively, as we could with a
teacher. Socrates critiques the stale quality of texts in a different dialogue,
Phaedrus, which is, ironically, a written text itself. In Phaedo, however, we
see clearly, that even a written text can actively engage reader in a process
that leads to interesting outcomes for them, very much like a teacher does.
</p>

<p>
The recollection argument has a unique property: the very act of understanding
this argument is itself an act of recollection.
To grasp why sensible equals are deficient compared to the Form of Equality, the
reader must already possess and use the very concept of perfect Equality that
the argument claims we've recollected from prenatal existence. Within this
framework, the understanding is not about recollection &#x2013; it is recollection.
</p>

<p>
Unlike pedagogical texts that invite optional engagement, or rhetorical texts
that persuade through technique, the Phaedo makes its central claim
self-exemplifying: understanding requires exercising the very capacity for
abstract thought that the argument attributes to recollection. The form doesn't
just illustrate the content &#x2013; it is the content first revealed, and then made
an embodied experience. Perhaps, the highest expression of the art of writing.
</p>
]]></description>
    </item>
    <item>
      <title>Compiler correctness</title>
      <link>https://rubber-duck-typing.com/posts/2024-09-20-bugs-in-compilers.html</link>
      <guid>https://rubber-duck-typing.com/posts/2024-09-20-bugs-in-compilers.html</guid>
      <pubDate>Thu, 19 Sep 2024 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
This post explores the notions of bug identity, bug identification applied to
compilers. I will try to highlight why compilers are so difficult to check for
correctness.
</p>
<div id="outline-container-org0cd8aee" class="outline-2">
<h2 id="org0cd8aee">Context: Verification and Validation of Software</h2>
<div class="outline-text-2" id="text-org0cd8aee">
</div>
<div id="outline-container-org5b22efa" class="outline-3">
<h3 id="org5b22efa">Model of a program</h3>
<div class="outline-text-3" id="text-org5b22efa">
<p>
Most programs can be modeled as a black box that reacts to events. In simple
cases, the only event is feeding the program input data, and the only reaction
is producing output.
</p>

<p>
The choice of events and reactions for the model is contextual. We do not need
to include every aspect of the program's execution in the model.
</p>

<ul class="org-ul">
<li>If only computation results matter, we may limit the model to describing
output files. This is prevalent in non-interactive programs; think scientific
computing.</li>
<li>If the execution speed, or the reaction time matters, we may include events
reflecting <i>how</i> the results are obtained. This way, we can track memory
consumption or response delays. Think high-frequency trading or robotics.</li>
</ul>
</div>
</div>
<div id="outline-container-orgd2d30bf" class="outline-3">
<h3 id="orgd2d30bf">What makes a program <i>correct</i>?</h3>
<div class="outline-text-3" id="text-orgd2d30bf">
<p>
We usually think of program correctness in a vague, informal way. A correct
program does what we expect it to do, but what exactly do we expect?
</p>

<p>
To make the notion of correctness clear for a given program, we should describe
our understanding of its behavior. In other words, we need to make explicit the
expected reactions to the input events. This description is called
a <b>specification</b> and can  be written in plain English, in some formal language,
or a mix of the two.
</p>

<p>
There are usually many ways to write code that satisfies a proposed
specification. First, there exist infinitely many programs with exactly the same
observable behavior. Second, the specification can be quite permissive, which
means many different behaviors are considered correct.
</p>

<p>
To illustrate the permissiveness point, let's take the specification:
</p>

<blockquote>
<p>
The program outputs all odd numbers in range from 0 to 100.
</p>
</blockquote>

<p>
This specification does not impose any order on these numbers, nor does it
prohibit duplicates. Therefore, a program that produces one of the following
outputs will fit this specification:
</p>

<ul class="org-ul">
<li>Outputs <code>[1,3,5,...,99]</code>.</li>
<li>Outputs <code>[1,5,3,...,99]</code>.</li>
<li>Outputs <code>[99,97, ..., 1, 3]</code>, or any other permutation of these elements.</li>
<li>Outputs the list <code>[1,3,5,...,99]</code> twice, and so on.</li>
</ul>

<p>
<b>Verification</b> is making an argument that the program behavior matches the
specification. This argument should be backed by reasoning, tests, and formal
methods.
</p>

<p>
Another step that justifies our belief in the software correctness is called
<b>validation</b>. While verification checks if our understanding of what the program
should do matches the actual program behavior, validation checks if we have the
right idea about what it should do, i.e. if the specification makes sense.
Validation is performed for the real use cases.
</p>

<p>
The meaning of the correctness usually implies that it is both verified and
validated. Bugs are evidence against the system correctness.
</p>
</div>
</div>
</div>
<div id="outline-container-orgbedc2b4" class="outline-2">
<h2 id="orgbedc2b4">How are compilers different from other software?</h2>
<div class="outline-text-2" id="text-orgbedc2b4">
<p>
In the previous section, we provided an example of a program specification: “output
all even numbers in the range from 0 to 100”. If a program outputs a list of numbers
without any special properties, it is easy to verify its output &#x2013; just make sure
it contains all the expected numbers. All outputs that contain the required numbers
are equivalent when we think about the program correctness. This equivalence is
easy to define, and computers can verify it.
</p>

<p>
Compilers are way more complicated. To decide if a compiler is <i>correct</i> for a
certain input program, we need to show that the <i>observable behavior</i> of an
input program written in X is equivalent to the observable behavior of the
output program, written in Y.
</p>

<p>
Unlike comparing two lists of numbers, equivalence of program is not just harder
&#x2013; it is undecidable in a general case. Imagine needing to put programs in every
imaginable context and see how they behave &#x2013; there is no algorithm that can do
that for all programs, in a finite time. This follows from <a href="https://en.wikipedia.org/wiki/Rice%27s_theorem">Rice's theorem</a>.
</p>

<p>
How we define an observable behavior is situational. For example, if we are
interested in how exactly the program is executed, not just its results, we
may include the memory allocation events. Otherwise we can completely ignore all
operational aspects of a program, reducing it to its output.
</p>

<p>
<a href="https://compcert.org/">CompCert</a>, the first verified C compiler, defines an event in a C program as a
system call or read/write to volatile memory; then an observable behavior is
either a finite trace of events, or an infinite trace (repeating the same
sequence of events or diverging), or an error. This approach can be traced to
systems theory:
</p>

<blockquote>
<p>
As we have seen, systems of equations [&#x2026;] may have three different kinds of
solution. The system in question may asymptotically attain a stable stationary
state with increasing time; it may never attain such state; or there may be
periodic oscillations.
</p>

<p>
&#x2014; "General Systems Theory", Ludwig von Bertalanfy
</p>
</blockquote>
</div>
</div>
<div id="outline-container-org1338fc3" class="outline-2">
<h2 id="org1338fc3">Identifying bugs in compilers</h2>
<div class="outline-text-2" id="text-org1338fc3">
<p>
Let us simplify the task: instead of verifying the correctness of the compiler
for all meaningful programs in X, let's focus on identifying specific bugs in the
compiler. While testing it on different programs in X, we may encounter a
program that behaves differently after compilation. This could seem like  like a
strong  evidence for a compiler bug. <b>Surprisingly, this is still not sufficient
to identify a bug in compiler.</b> But why?
</p>

<p>
To compare the behaviors of two programs, we need to answer several complicated
questions.
</p>

<ol class="org-ol">
<li>What is a program’s behavior? This, in turn, requires defining:
<ul class="org-ul">
<li>The language semantic for X;</li>
<li>The language semantic for Y;</li>
<li>Observable behavior i.e. which events are visible.</li>
</ul></li>
<li>Can a program in X exhibit multiple behaviors? What about compiled programs in Y?
<ul class="org-ul">
<li>If yes, is it acceptable to lose some of X’s behaviors in the compiled programs?</li>
</ul></li>
<li>Can a program in X exhibit undefined behavior? Additionally:
<ul class="org-ul">
<li>Can undefined behavior be reliably identified?</li>
<li>Does the compiler guarantee anything about the behavior of the compiled program in such cases?</li>
</ul></li>
</ol>

<p>
These issues hint to the first major problem: <b>bug identification/definition</b>.
Given a compiled program that behaves unexpectedly, what is the underlying cause?
</p>
</div>
<div id="outline-container-orgd1a476a" class="outline-3">
<h3 id="orgd1a476a">No formal language specification</h3>
<div class="outline-text-3" id="text-orgd1a476a">
<p>
Many popular languages lack a formal, unambiguous specification.
In its absence, the compiler effectively becomes the language's de facto
specification and a part of the ultimate source of truth for determining program
behavior.
</p>

<p>
It gets worse: compilers themselves are often written in a language that lacks a
formal specification. For example, suppose we have a compiler written in C++.
The compiler's behavior may then be compromised by:
</p>

<ul class="org-ul">
<li>The quirks of C++ semantics, which make it harder to interpret the compiler's code.</li>
<li>C++'s compile-time non-determinism means that after recompiling the new executable of X's compiler
may compile programs differently.</li>
<li>C++'s many undefined behaviors may carry over into programs in X, even if X is not supposed to have any.</li>
<li>Bugs of the C++ compiler itself may affect the compiler of X.</li>
<li>Implementation bugs of the C++ compiler may introduce bugs in
the specification of language X and creep into the programs written in X.</li>
</ul>

<p>
Thus, different behaviors in a program in X and its compiled version in Y do not necessarily indicate a bug <i>in the compiler</i>.
</p>
</div>
</div>
</div>
<div id="outline-container-org4f6f04e" class="outline-2">
<h2 id="org4f6f04e">Same bug or different bugs?</h2>
<div class="outline-text-2" id="text-org4f6f04e">
<p>
If we are unsure whether we have found a bug or if the unexpected behavior stems from an incomplete language specification, distinguishing one bug from another becomes even more difficult. How can we tell whether two
miscompilations result from the same compiler bug or from different ones?
This is the <b>bug identity</b> problem.
</p>

<p>
Why does bug identity matter? Can't we just fix bugs one by one?
Sometimes, fixing one bug will inadvertently fix another, suggesting that they have a common root cause.
</p>

<p>
First, if you have 100 issues in your bug tracker, it helps to know
whether any of them are related. This aids in planning, as it helps estimate how
many patches do we really need to resolve these 100 issues.
</p>

<p>
Second, there are multiple ways to incentivize the community to report bugs for
some sort of compensation. When two people report the same bug, how do we ensure fair compensation?
It is hard to say whether they have reported the same bug at all!
All reward strategies seem flawed:
</p>


<ul class="org-ul">
<li>A "first-come, first-rewarded" approach does not work because new
reports may be related to already reported but unresolved bugs.</li>
<li>Rewarding both reporters equally is problematic because one can spam the
system by generating numerous programs triggering the same bug, receiving
disproportionately large rewards or reducing the reward for the original
reporter if the total reward pool is fixed.</li>
</ul>

<p>
Futhermore, compiler users are often application developers, not experts in
programming language semantics or programming language theory. They may not
understand why, when they report something that is <i>clearly a bug</i> to them, the
compiler experts scratch their heads and come up with an excuse &#x2013; obviously
because they don't want to pay or are ignorant.
</p>

<p>
Languages like C and C++ are rife of "bugs" of this kind. Many rather arcane
behaviors are only recognized by a selected few, well versed in the language
standard. For instance,  using unions for type punning, where one union field is
written and
another is read, falls into undefined behavior according to the language standard.
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-keyword">union</span> <span class="org-type">array</span> <span class="org-rainbow-delimiters-depth-1">{</span>
        <span class="org-type">char</span><span class="org-rainbow-delimiters-depth-2">[</span><span class="org-highlight-numbers-number">8</span><span class="org-rainbow-delimiters-depth-2">]</span> as_chars;
        <span class="org-type">uint64_t</span> <span class="org-variable-name">as_uint64</span>;
<span class="org-rainbow-delimiters-depth-1">}</span>;


<span class="org-keyword">union</span> <span class="org-type">array</span> <span class="org-variable-name">my_union</span>;
my_union.as_uint64 = <span class="org-highlight-numbers-number">42</span>;

<span class="org-comment-delimiter">// </span><span class="org-comment">accessing the 3rd byte of the variable holding 42.</span>
<span class="org-type">char</span> <span class="org-variable-name">n</span> = my_union.as_chars<span class="org-rainbow-delimiters-depth-1">[</span><span class="org-highlight-numbers-number">3</span><span class="org-rainbow-delimiters-depth-1">]</span>;
</pre>
</div>

<p>
In this case, the C standard deems such practices as undefined behavior, meaning the developer should not expect consistent behavior. Therefore if a
developer finds that the variable <code>n</code> equals 999 in their code, regardless of
the value written to <code>my_union.as_uint64</code>, jokes on them &#x2013; the language standard is on the compiler's side.
</p>

<p>
One potential way to define bug identity is to say that two bugs are the same if one cannot reasonably be fixed without also fixing the other. This definition is imperfect, as the term "reasonably" is vague. In theory, we could patch specific cases without addressing the root cause, but this is impractical for compilers, as similar bugs will continue to arise.
</p>

<p>
In the end, bug identity is hard to formalize. Calling in an expert may be the best option, relying on open criteria to judge whether two inputs trigger the same bug or different ones. Some cases are clearly not bugs, while most fall into a gray area.
</p>
</div>
</div>
<div id="outline-container-org5219243" class="outline-2">
<h2 id="org5219243">Bug severity</h2>
<div class="outline-text-2" id="text-org5219243">
<p>
After distinguishing between two bugs, the next step is to prioritize which one to fix first. This is the <b>bug severity</b> issue.
</p>

<p>
Compilers are usually tested using a code base containing both synthetic tests
and real projects. It makes sense to use the real projects affected by the bug
as a part of work prioritization.
</p>

<p>
However, the identity problem complicates this process: decision-making requires comparing bugs across different input programs. How do we identify duplicates? How do we tell if the same bug is affecting two programs or if two different bugs are at play?
</p>

<p>
As mentioned earlier, automating bug identification and comparison is not
feasible, meaning expert judgment is necessary.  Malicious users who understand
compiler bugs may submit thousands of inputs that trigger the bug, flooding
developers with AI-generated bug reports. This can amount to a real-world
denial-of-service attack on the development team. Fairly rewarding bug reports
under these circumstances becomes nearly impossible.
</p>
</div>
</div>
<div id="outline-container-org3bdbbe8" class="outline-2">
<h2 id="org3bdbbe8">Conclusion</h2>
<div class="outline-text-2" id="text-org3bdbbe8">
<p>
Most programming languages lack formal, unambiguous specifications, making it difficult to define correct program behavior and identify bugs in compilers.
</p>

<p>
Comparing bugs is challenging because they stem from program behavior.
Programs may have multiple possible behaviors and the compiler will select one
of them. Since program equivalence is not decidable for Turing-complete
languages, we
can't reliably detect when the compiler's output changes but the behavior remains the same.
</p>

<p>
In the next post, I will explore the challenges faced by compilers for platforms where computations are paid for with gas, particularly when the compiler has multiple backends with different gas models.
</p>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>My note-taking process</title>
      <link>https://rubber-duck-typing.com/posts/2023-01-19-note-taking.html</link>
      <guid>https://rubber-duck-typing.com/posts/2023-01-19-note-taking.html</guid>
      <pubDate>Thu, 19 Jan 2023 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
I love being efficient and there is nothing like a feeling of wholesomeness at the end of the
day when you realize that you have not wasted it.
I am not incredibly productive, but I am more productive then <i>some</i> people,
including myself for practically my whole previous life.
</p>

<p>
I attribute my recent increase of productivity to my knowledge acquisition
system.
It is not a perfect system indeed, and always a work-in-progress, but it stuck
with me for months now, which is a good sign.
Additionally, it has ordered my life, eliminated much friction and helped systematize
all my intellectual activities.
Maybe this post will inspire you to create a similar system, or improve yours.
</p>
<div id="outline-container-org941470e" class="outline-2">
<h2 id="org941470e">Overview</h2>
<div class="outline-text-2" id="text-org941470e">
<p>
The main components of my system are:
</p>

<ol class="org-ol">
<li>Zettelkasten, stored in a git repository and organized in Emacs with <code>org-mode</code> and <code>org-roam</code>.</li>
<li>Book storage: cloud storage with digital articles and books, and a bookshelf
with physical books.</li>
<li>An Android tablet with a stylus to keep a working set of
documents and annotate them.</li>
<li>An e-ink reader, which mirrors a part of my collection of books.</li>
</ol>

<p>
The main stages of my workflow are:
</p>

<ul class="org-ul">
<li>Reading/watching and making notes as I go.</li>
<li>Revising, copying notes to my long-term note storage.</li>
</ul>

<p>
Now, to the details.
</p>
</div>
</div>
<div id="outline-container-orga59dd9f" class="outline-2">
<h2 id="orga59dd9f">Data flow</h2>
<div class="outline-text-2" id="text-orga59dd9f">
<p>
Most of the time, I study by reading books, articles, and blog posts.
Watching videos is less convenient, but sometimes I have no choice.
I will illustrate the workflow on an example of a physical book.
</p>
</div>
<div id="outline-container-org6fa1e43" class="outline-3">
<h3 id="org6fa1e43">Introduction to the source</h3>
<div class="outline-text-3" id="text-org6fa1e43">
<p>
First, I need to get a general idea of what is happening in the book.
Linear reading is inconvenient for the task, so we need to approach the text structurally.
</p>

<p>
To begin, I am looking at the table of contents, skimming through pages, reading
the section headers, sometimes section introductions.
It is enjoyable doing this with a physical book!
</p>

<p>
Often, the book is either not entirely new for me, or only some chapters are
useful &#x2014;  it is necessary to identify them before reading.
This is most important if the book is not an easy reading: sometimes,
getting through every single page may require an extensive research and an exhausting
thinking.
</p>

<p>
My goals at this stage are:
</p>

<ol class="org-ol">
<li>Familiarize myself with the book structure.</li>
<li>Ask Google/ChatGPT about the meaning of unknown technical terms.</li>
<li>Identify my goals in reading this specific book.</li>
<li>Think for a moment about the questions that appear in my head after skimming
through the book.</li>
</ol>

<p>
Some books are going an extra mile to guide the reader, for example,
<a href="https://thenetworkstate.com/">"The Network State"</a>  starts with sections "The book in one sentence", "The book
in one image", "The book in one thousand words", and "The book in one essay".
This is a didactic approach that I am also using with my students: guide to
ideas through a series of increasingly detailed approximations.
</p>
</div>
</div>
<div id="outline-container-org3f10cc2" class="outline-3">
<h3 id="org3f10cc2">Annotating the source</h3>
<div class="outline-text-3" id="text-org3f10cc2">
<p>
Once I have a general idea of what to expect from the book, I start reading and
annotating it.
As a kid, I was reading fast but memorized little, so now if I am
serious about working through the text, I am annotating it.
I use a set of four pencils and four highlighters of colors: violet, blue, red, and green.
When something picks my interest, I highlight a fragment of text with one of
these colors and write text on margins in the same color. This way a commentary
is matched to a fragment.
</p>


<div id="org6fcd117" class="figure">
<p><img src="../img/book-annotated.jpeg" alt="book-annotated.jpeg" />
</p>
</div>

<p>
There is a logic to my color selection:
</p>

<ul class="org-ul">
<li><p>
<b>Violet</b> is for summarizing. It is well visible on paper and also
distinguishable from black, so it does not fuse with the text.
</p>

<p>
It is <b>not useful</b> to highlight without commenting. Writing summary is
mandatory, highlighting is optional <sup><a id="fnr.1" class="footref" href="#fn.1" role="doc-backlink">1</a></sup>.
</p></li>

<li><b>Blue</b> is for connections. It reminds me of hyperlinks on Web pages.
With blue I mark reasonable connections to other topics, writers, or books.
Connections greatly enhance retention.</li>

<li><b>Red</b> is for questions and critique. It attracts the most attention and is
associated with danger. I use it if I do not agree with the author, or
if I do not understand a fragment.</li>

<li><b>Green</b> is for everything else. It is a safe, a bit disconnected color. If my
curiosity suggests me a possible connection to my previous readings, if I get
an idea on how to solve some problem, an analogy, a metaphor, then it is worth
noting it. Basically, this is the color for everything else.</li>
</ul>

<p>
I am never waiting to finish one book before starting another, so my working set
of sources is usually large.
Reading many books on the same topic helps establishing more connections.
Sometimes, it also enhances understanding, since I am exposed to several
explanations of the same concepts, often from different points of view.
</p>

<p>
Every now and then I revise the notes to add information to my permanent note storage.
</p>
</div>
</div>
<div id="outline-container-orga7c73c5" class="outline-3">
<h3 id="orga7c73c5">Storing notes</h3>
<div class="outline-text-3" id="text-orga7c73c5">
<p>
The core of my system is Zettelkasten.
It is a personal Wiki with small subjective articles and annotated subjective connections between them.
Each "wiki article" is a note on something: a concept, an idea, a book, an article etc.
After some reading, it is time to revise my notes, skim through the book again,
and put the bits of notes into the appropriate notes in Zettelkasten.
</p>

<p>
I like taking notes in two stages:
</p>

<ul class="org-ul">
<li>It forces me to come back to notes in a systemic way.
I re-read, re-discover things that I missed, and memorize information better.</li>
<li>It combines the advantages of hand-writing and digital note-taking.
<ul class="org-ul">
<li>Handwriting is good for retention; sheets are more "personal", it stimulates
thinking.</li>
<li>Digital note-taking is fast, notes are easier to systematize, much easier to
edit and illustrate, they can benefit from the version control systems etc.</li>
</ul></li>
</ul>


<p>
I use <code>emacs</code> with <code>org-roam</code> to keep my Zettelkasten organized, easily search through them and
explore them with <code>org-roam-ui</code>.
</p>


<p>
Here is an example of a note:
</p>


<div id="org55e4752" class="figure">
<p><img src="../img/emacs-zettel-example.png" alt="emacs-zettel-example.png" />
</p>
</div>

<p>
&#x2026; and its source code:
</p>

<div class="org-src-container">
<pre class="src src-org"><span class="org-org-drawer">:PROPERTIES:</span>
<span class="org-org-special-keyword">:ID:</span>       <span class="org-org-property-value">22AF419C-C79F-42A6-B7E9-CF7A27DAD5E0</span>
<span class="org-org-drawer">:END:</span>
<span class="org-org-document-info-keyword">title:</span> <span class="org-org-document-title">System</span>
<span class="org-org-meta-line">filetags: #system-engineering</span>

&#9660;<span class="org-org-level-1"> Definition</span>
A system is something that consists of parts, functions and has emergent properties.

&#9660;<span class="org-org-level-1">  Other definitions</span>

<span class="org-org-modern-block-name"><span class="org-org-block-begin-line">quote</span></span>
<span class="org-org-quote"><span class="org-org-block"><span class="org-font-latex-sedate">\dots</span></span></span><span class="org-org-quote"><span class="org-org-block"> </span></span><span class="org-org-quote">any organized </span><span class="org-org-quote"><span class="org-bold">assembly</span></span><span class="org-org-quote"> of resources and procedures united and regulated by</span>
<span class="org-org-quote"><span class="org-bold">interaction of interdependence</span></span><span class="org-org-quote"> to accomplish a set of specific functions</span>
<span class="org-org-quote">-- DoDAS (Department of Defense Architecture Framework)</span>
<span class="org-org-modern-block-name"><span class="org-org-block-end-line">quote</span></span>


<span class="org-org-modern-block-name"><span class="org-org-block-begin-line">quote</span></span>
<span class="org-org-quote"><span class="org-org-block"><span class="org-font-latex-sedate">\dots</span></span></span><span class="org-org-quote"><span class="org-org-block"> </span></span><span class="org-org-quote">a construct or collection of different entities that together produce results</span>
<span class="org-org-quote">not obtainable by the entities alone.</span>
<span class="org-org-quote">-- INCOSE</span>
<span class="org-org-modern-block-name"><span class="org-org-block-end-line">quote</span></span>



Human-crafted systems have goals.

&#9660;<span class="org-org-level-1"> Related</span>

&#8211;<span class="org-org-indent"> </span>In order to create systems it is necessary to engage in
  "<span class="org-org-link"><a href="id:4C0E5272-CC97-40B5-BA56-FD15F83B23B8">Systems thinking</a></span>"

<span class="org-org-meta-line"> setupfile: org-header.org</span>
<span class="org-org-meta-line"> created:  </span><span class="org-org-modern-date-inactive">2022-11-18 Fri </span>
<span class="org-org-meta-line"> last_modified:  </span><span class="org-org-modern-date-inactive">2022-11-18 Fri </span><span class="org-org-modern-time-inactive"> 12:16 </span>

</pre>
</div>


<p>
The note contains:
</p>

<ol class="org-ol">
<li><p>
An identifier for <code>org-roam</code> (generated automatically)
</p>
<div class="org-src-container">
<pre class="src src-nil">:PROPERTIES:
:ID:       22AF419C-C79F-42A6-B7E9-CF7A27DAD5E0
:END:
</pre>
</div></li>
<li>Title</li>
<li>Tags (we can then search by them)</li>
<li>Sections with content. Text supports markup: underline/bold/italic,
monospace, strike-through and alike; it may contain pictures (there is a
convenient way of including shot of a part of the screen through
<code>org-download-screenshot</code>).</li>
<li>A special section for connections to other notes
Personally, I am also putting links to other notes directly into the body of
the note. The special section is precious to provide subjective annotations for
the links: why do I think there is a connection between this note and another
one.</li>
</ol>

<p>
I used to have two sections: for links to the sources, and for related notes, but now I tend
to just merge both into "Related".
</p>

<p>
I keep most metadata out of the way by putting it in the end of the note.
</p>

<p>
Additionally, emacs lets us explore the connected graph of notes in 3D, filter
them by tags, preview them nicely etc. thanks to <code>org-roam-ui</code>. 
It looks like this:
</p>


<div id="org0b1e121" class="figure">
<p><img src="../img/org-roam-ui.png" alt="org-roam-ui.png" />
</p>
</div>
</div>
</div>
</div>
<div id="outline-container-orgbd77d7c" class="outline-2">
<h2 id="orgbd77d7c">What about digital sources?</h2>
<div class="outline-text-2" id="text-orgbd77d7c">
<p>
Most of my books or articles are in digital format, so I read a lot on my tablet as well.
For that, I have bought Samsung Galaxy Tab7 Fan Edition this summer, and it is
just really good for me:
</p>

<ul class="org-ul">
<li>The screen is big (and I never felt like I have enough screen space for
comfortable handwriting before)</li>
<li>Samsung S-Pen feels good to me, better than Apple pencil. The feel is a bit
closer to the fountain pen, which is my writing tool of preference on paper.</li>
<li>The performance is adequate for everything I need.</li>
</ul>



<div id="orgab9f7bd" class="figure">
<p><img src="../img/tablet-book.jpeg" alt="tablet-book.jpeg" />
</p>
</div>



<p>
As for software, I found that Flexcil works best for me.
It does not choke when I open 40 tabs with documents, stays responsive
and handles even huge books like <a href="http://www.topology.org/tex/conc/dg.html">Differential geometry reconstructed</a>, which has around 2500 pages as of today.
</p>

<p>
The editing capabilities are not great, e.g. you can not rotate your handwritten
text, and the Android version lacks a good synchronization mechanism &#x2013; you
basically have to do backups and restore them.
The backup process is fast thought, so I am just doing backing up my notes in
cloud every day.
Once these features get implemented, Flexcil will become an ideal tool for me.
</p>

<p>
I also read for pleasure, without annotating.
For that I still prefer using my old e-ink reader PocketBook 902
E-ink displays are easier on my eyes.
It has now turned 12 years old, and still works like a charm.
</p>


<div id="orgd8592eb" class="figure">
<p><img src="../img/bookreader.jpeg" alt="bookreader.jpeg" />
</p>
</div>

<p>
At some point I was considering buying an e-ink tablet for annotating, but I find that having
color on annotations is too valuable.
The current e-ink tablets were not convincing to me.
</p>
</div>
</div>
<div id="outline-container-orgdbd72bf" class="outline-2">
<h2 id="orgdbd72bf">Pitfalls</h2>
<div class="outline-text-2" id="text-orgdbd72bf">
<ul class="org-ul">
<li>Do not try to create a hierarchy (taxonomy) for your notes. It is not
scalable and only works for narrow domain. Prefer tags.</li>

<li>I did not perceive this system as too complicated, until I have written this
post. Start small, build a habit, grow as you see fit.</li>

<li>Without a habit, your Zettelkasten will stay empty.
You may need to force yourself to stop reading at some point, revise your notes and put them to the permanent
database.
Do not delay this too much, or you will not be recalling the annotated texts,
but rather reading them anew.</li>

<li>Redundancy is good. You will naturally put the same information in many notes,
and <b>it is all right</b>.</li>

<li>Drawing is great. Emacs and org notes support <code>.svg</code> files, but you can also include
photos or screenshots of your hand-drawn diagrams, why not.</li>

<li><p>
Think carefully which software you will be using, if any.
It is great to have the second brain for life.
My first digital notes were in some note-taking app for Symbian OS, then in Evernote.
These programs are proprietary and do not always have an easy way for you to migrate your notes somewhere else.
Emacs (and text files) is clearly not everyone's darling, but it is free,
open-source, and has been around for more, than
I am alive.
It means that it is flexible enough to adapt to the new technological context:
we are not using the same computers with the same operating systems as in 80s
anymore, and emacs runs fine on basically everything.
I am quite certain that:
</p>
<ul class="org-ul">
<li>either I will be able to keep my notes there for life,</li>
<li>or I will be able to migrate them somewhere, should such need arise.</li>
</ul>


<div id="org9d949e6" class="figure">
<p><img src="../img/pen-of-choice.jpeg" alt="pen-of-choice.jpeg" />
</p>
<p><span class="figure-number">Figure 1: </span>My favorite pen since my teen years is Sheaffer Fashion.</p>
</div></li>
</ul>
</div>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">

<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
Most texts are not too dense and include a fair percentage of redundancy,
so you can summarize them in a useful way. There are, of course, exceptions, for
example, <i>Tractatus Logico-Philosophicus</i> of Ludwig Wittgenstein, where the book
is a structured tree of key points of his word view from that time. In this case
I had to keep the annotations separated from the book.
</p></div></div>


</div>
</div>]]></description>
    </item>
    <item>
      <title>Programming languages and computer systems (1)</title>
      <link>https://rubber-duck-typing.com/posts/2022-07-26-systems-1.html</link>
      <guid>https://rubber-duck-typing.com/posts/2022-07-26-systems-1.html</guid>
      <pubDate>Tue, 26 Jul 2022 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
A couple years ago I have started to write my second book, whose main point is
to teach students elements of computer system design, largely speaking.
Besides discussing the notion of systems, complexity and approaches to managing
systems' complexity in a meaningful way, I want to give special attention to the
programming languages. Source code transformed into a running program is an
important part of system, the language design and the software design affect the
resulting system in a lot of ways.
</p>

<p>
The book remains largely unfinished, but as a mean to push myself to complete it
and to share the first versions of the chapters I will post them here.
</p>

<p>
The first chapters introduce the notions of systems and computer systems.
Software and hardware engineers can greatly benefit from studying systems: 
system design techniques can be applied to designing all kinds of software,
hardware, hybrid systems, distributed systems, and even applied to developer
teams, which are systems themselves.
However, we are not focusing on the systems theory itself, and many ideas can be
expanded or made more precise.
</p>
<div id="outline-container-orgbc031dd" class="outline-2">
<h2 id="orgbc031dd">Systems around us</h2>
<div class="outline-text-2" id="text-orgbc031dd">
<p>
In our lives we encounter multiple collections of objects that we call systems.
<i>System</i> is a collection of components interacting with each other and outside
world, giving the system additional properties, that individual components do
not possess.
Such properties are called <i>systemic</i> or <i>emergent</i>. 
</p>

<p>
This drawing shows a system of three components, denoted A, B, and C.
Component B interacts with A and C, while A and C do not interact directly with each other.
A big circle is separating the system from the outside world.
</p>


<div id="org109340c" class="figure">
<p><img src="../img/system.svg" alt="system.svg" class="org-svg" width="50%" />
</p>
<p><span class="figure-number">Figure 1: </span>A system of three components: A, B, and C.</p>
</div>


<p>
The world around us is filled with systems. Here are some examples:
</p>

<ol class="org-ol">
<li><p>
Cars can transport passengers and objects.
This ability is a systemic property of cars.
No part of a car can transport objects like a whole functioning car.
</p>

<p>
Moreover, when the car is assembled but not running, it stays still.
Only the interaction between the parts of a car allows it to be a transport.
</p></li>

<li>Living humans are systems. Unlike in a corpse, our cells are interacting.
Our ability to walk, think, breathe, being alive and conscious is our
systemic property.</li>

<li>Computers will not perform any work unless powered up and turned on.
The ability to compute is their systemic property.
It is born from interactions of software and CPU, memory, storage, buses, and other hardware components.</li>

<li>A team of software, or hardware developers is unable to produce a complex
piece of software unless all participants interact with each other, making
sure that they are on the same page.
The ability to produce an output together through solo work and interactions
is a systemic property.</li>
</ol>

<p>
In this part we will continuously revisit these three examples: cars, humans,
computers, and teams.
</p>

<p>
There is a saying: "a system is more than a sum of its parts."
Indeed, a system is not only a sum of its parts, but also of interactions between them.
Disregarding the interactions takes the emergent properties out of the picture.
</p>

<p>
Systems emerge in many areas of our lives, e.g., sociology, biology, physics,
economics, computer science, and engineering.
Systems theory has become an interdisciplinary field studying systems models at
a high level of abstraction, disregarding specific domains.
</p>
</div>
</div>
<div id="outline-container-orgc328030" class="outline-2">
<h2 id="orgc328030">Environment</h2>
<div class="outline-text-2" id="text-orgc328030">
<p>
Systems do not exist in the void, they are interacting with the world.
</p>

<ol class="org-ol">
<li>Cars interact with the driver and the road, the trees around them, other cars.</li>
<li>Humans interact with other humans, sounds, visuals, the air we breathe, and the food we consume.</li>
<li>Computers interact with other devices, with humans through screens, keyboards, and other interfaces.</li>
</ol>

<p>
<i>Environment</i> is a collection of objects outside the system with which the system engages in meaningful interactions.   
</p>


<div id="org1f5ac09" class="figure">
<p><img src="../img/system-environment.svg" alt="system-environment.svg" class="org-svg" />
</p>
<p><span class="figure-number">Figure 2: </span>In the same system, B is an interface, and E,F,G are parts of the system environment.</p>
</div>

<p>
The context of our analysis dictates which objects should be part of the environment.
The car might, in some cases, interact with blue whales, sound waves, bacteria,
or asteroids, but most analyses can safely ignore them.
If the system interacts with an object from an outside world, we include this
object in the environment.
The interaction should be essential for our analysis.
</p>

<p>
The environment usually includes an organized part and a chaotic part.
Sometimes it may be viewed as a system on its own, consisting of multiple systems and interactions between them.
If we design our own system, we often want to protect it from interacting with the chaotic part and set up the correct interactions with the organized part.
</p>

<p>
Usually only a handful of the system's components are interacting with the environment directly, and we call them the <i>interface of the system</i>.
Here are some examples:
</p>

<ol class="org-ol">
<li>The interface of cars includes its controls and wheels.</li>
<li>The interface of humans includes their eyes, ears, and skin.</li>
<li>The interface of computers includes external devices, e.g. keyboard, screens, mice, network adapters.</li>
</ol>

<p>
In other words, <i>interface</i> consists of such components that can be viewed both as parts of the system, or of its environment.
</p>

<p>
The interface can vary depending on the system's function: if we consider a verbal conversation between two humans, their interface is a spoken word; however, when these humans dance together, the interfacing happens through visual contact and touch.
</p>

<p>
An interface is like a border between the system and its environment.
The border consists of such parts of the system that can be attributed to either the system itself or its environment.
It is not always easy to define the border well, it may be blurred.
</p>
</div>
</div>
<div id="outline-container-orgba34024" class="outline-2">
<h2 id="orgba34024">Computer systems</h2>
<div class="outline-text-2" id="text-orgba34024">
<p>
In computer systems, we peek through the interface of a computer system and observe the processes inside.
Then we <i>interpret</i> our observations, and this interpretation is what constitutes <i>computations</i>.
</p>
</div>
<div id="outline-container-orge0aedb6" class="outline-3">
<h3 id="orge0aedb6">Examples of computer systems in nature</h3>
<div class="outline-text-3" id="text-orge0aedb6">
<p>
To demonstrate the importance of our interpretation, let us study some systems where computations happen in unorthodox ways.
</p>

<ol class="org-ol">
<li><p>
Computers are deterministic, so they do not have components to generate random numbers.
Unless they incorporate a specific hardware component to generate randomness, they cannot achieve true non-determinism.
Computers can imitate randomness through clever mathematical trickery, but such numbers, although they <i>seem</i> random, are predictable.
However, actual random numbers have numerous uses, especially in cryptography.
</p>

<p>
One good way of generating actual random numbers is an online service located at <code>www.random.org</code>.
It uses electromagnetic noise from the atmosphere of Earth as a seed for the random number generator.
Using such numbers for cryptography is much more adequate than pseudo-random numbers generated by a traditional computer.
</p>

<p>
We can say that the part of <code>random.org</code> that performs computations is the atmosphere of Earth itself.
It may seem strange because computation results come from observing nature, not through computations on a human-made computer.
However, observing atmospheric noise and interpreting our observations, then plugging it into a more conventional computer system, provides us with numbers with very particular properties.
What is it, if not a computing process?
</p></li>

<li>Another example is <i>pulsars</i>.
Pulsars are stars at the late stages of their life, emitting bursts of light with high and very stable frequency.
Therefore, they can be used as a clock: we count the bursts and know how many milliseconds have passed.
Counting time is also computation.</li>

<li><p>
The third example is ant colonies.
Ants are searching for food in a process that can be described as a random walk. As they move, they mark their steps with a scent of pheromones.
</p>

<p>
When an ant worker reaches food or another resource, it goes back to the ant colony, tracing back its steps.
Thanks to the smelly footprints, it can go back following precisely the same route from the ant colony to food.
</p>

<p>
However, its initial path can be far from optimal.
For example, the ant may walk a full circle, so its patch will contain loops.
Alternatively, it can go uphill instead of going around the hill.
</p>

<p>
Nature seeks efficiency, and ants use their capacities to recognize stronger and weaker smells to optimize their paths.
The smell gradually evaporates, so the path to the ant colony has a weaker smell.
Similarly, the path to the food was taken more recently and thus has a stronger smell.
Turns out, this information is sufficient to shorten paths substantially.
</p>

<p>
So, if we put a piece of food in the proximity of an ant colony, we will find an optimal path from this colony to food.
All of it &#x2014; by observing ants, without any circuitry.
This is a kind of computation that does not look different from something computed on your laptop.
Actually, ant colonies have inspired some path search algorithms.
</p></li>
</ol>

<p>
To sum it up, computations can be performed in a lot of ways, and interpretation plays a huge role in it.
Sometimes nature performs computations without computers, and we interpret natural processes as computations.
</p>
</div>
</div>
<div id="outline-container-orgee7f97e" class="outline-3">
<h3 id="orgee7f97e">Programs as systems</h3>
<div class="outline-text-3" id="text-orgee7f97e">
<p>
Writing programs is of particular interest to us.
What is the relationship between programming and computer systems?
</p>

<p>
When we program, the outcome of our work is the <i>source code of a program</i>.
It may be executed through interpretation, it may be compiled and then executed in a different form.
</p>

<p>
Systems are alive, their parts are interacting, and new properties emerge from these interactions.
</p>

<p>
The source code is dead.
It is akin to a manual on how to assemble a piece of furniture, and someone or something else has to do the actual work.
</p>

<p>
However, a running program becomes a part of a hybrid hardware-software computer system.
It is impossible to understand its behaviour without thinking about hardware, software, and their interaction.
By reducing the system to just software or hardware we lose the other part from the sight, and we also lose their interactions.
</p>


<p>
The source code relates to the running program as a manual on how to assemble a piece of furniture relates to a system of furniture assembler, instruction manual, and furniture parts.
This system is performing work on assembling the furniture.
</p>

<p>
The blueprint of an airplane is also like the program source code.
If we load the program in memory and prepare for its execution, it will be an actual plane standing on the ground.
A running program will be a flying airplane.
</p>

<table border="0" cellspacing="0" cellpadding="6" rules="none" frame="none">


<colgroup>
<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Source code</th>
<th scope="col" class="org-left">Program</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">airplane blueprint</td>
<td class="org-left">flying airplane</td>
</tr>

<tr>
<td class="org-left">furniture assembly manual</td>
<td class="org-left">working furniture assembler</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="outline-container-org9077a2f" class="outline-2">
<h2 id="org9077a2f">Functional and structural decomposition</h2>
<div class="outline-text-2" id="text-org9077a2f">
<p>
The system of interest may be very complex.
It may be difficult to <i>decompose</i>, i.e., identify its parts. 
There are several distinct ways of doing it.
</p>
</div>
<div id="outline-container-org70e6d0b" class="outline-3">
<h3 id="org70e6d0b">Structural parts</h3>
<div class="outline-text-3" id="text-org70e6d0b">
<p>
The first way is to decompose the system <i>structurally</i>.
It is useful for the systems assembled from isolated parts, like a car or a house.
</p>

<ol class="org-ol">
<li>Wheels, chairs, gearbox are all parts of a car.</li>
<li>Bones, muscles, internal organs are parts of a human.</li>
<li>Processor, motherboard, memory chips are parts of a computer.</li>
</ol>


<p>
The second, more useful way, is to decompose the system <i>functionally</i>.
Each part is identified by its role: what is its function in a system?
This is not the same as structurally decomposing system, because we only care about the functionality.
</p>
</div>
</div>
<div id="outline-container-org619bae0" class="outline-3">
<h3 id="org619bae0">Functional components</h3>
<div class="outline-text-3" id="text-org619bae0">
<p>
One structural part may participate in multiple interactions and play different roles.
</p>

<ol class="org-ol">
<li><p>
Steering wheel, knobs, car computer, gear box, steering wheel booster and other parts of a car assist its driver in controlling the car; it is their function.
Some of these parts have also other functions, like the car computer, which may regulate the fuel consumption.
So, the car can be functionally decomposed in multiple parts, two of which are:
</p>

<ul class="org-ul">
<li>A part to assist the driver.</li>
<li>A part to regulate the fuel consumption.</li>
</ul>

<p>
It is impossible to isolate these two parts one from another; therefore it is not a structural decomposition.
</p></li>

<li>Bones are structural parts of humans, but their roles are many: they support us, they protect bone marrow, they allow an effective functioning of muscles and tendants.</li>

<li>A hard disk drive may be a structural part of a computer.
It has many roles, e.g., it is a data storage hosting partitions with filesystems; it also participates in the functioning of virtual memory, as we will see in Chapter 6.</li>

<li>Scissors is another good example of a system with different functional and structural decomposition.
It is able to cut paper better than a simple knife because of its systemic properties.
Structurally scissors are composed of two pieces of metal and a screw that connects them together. 
Functionally, the same scissors consist of two parts: one is used to hold them, whereas another cuts paper. Not only do scissors have more structural parts than functional, we can not map structural parts to functional parts.</li>

<li>Consider a team, a system of people working on the same project.
Structurally, every person is a part of such system.
Functionally, the parts of this system are <i>roles</i>: programmers, designers, managers, testers etc.
One of the differences between them is that one person can play several roles (e.g. be a programmer and project manager, or programmer and graphics designer).</li>
</ol>
</div>
</div>
<div id="outline-container-orgc02af01" class="outline-3">
<h3 id="orgc02af01">Functional view of systems</h3>
<div class="outline-text-3" id="text-orgc02af01">
<p>
Functional way of viewing systems is considered more efficient for engineering
computer systems, including programs.
It emphasizes the important aspect of systems &#x2014; their functionality.
Then this functionality may be implemented in software, in hardware, or in a hybrid form.
Later we will study an example of virtual memory; it is a mechanism implemented
by an operating system that leverages certain hardware features. Virtual memory
is not exclusively hardware or software.
</p>


<p>
Imagine we are building a system that requires intensive computations, like ray tracing in computer games.
Its evolution may look like this:
</p>

<ol class="org-ol">
<li>The starting point is a simple single-threaded algorithm to perform ray tracing on a single CPU.
To speed it up, we provide the fastest CPU available.</li>
<li>The algorithm is optimized on multiple levels to work as fast as possible.</li>
<li>The algorithm is parallelized so that ray tracing is performed in multiple threads.</li>
<li>The computations are moved to a dedicated graphics card. This requires redesigning the algorithm.
The graphics card is often a good computation unit to run highly parallelized algorithms.</li>
<li>If a graphics card is not efficient enough, or if it draws too much power, we may design our own chip.
So the whole algorithm will be encoded as a circuit on a FGPA (field-programmable gate array) which will allow to maximize its speed and minimize the energy consumption.</li>
</ol>

<p>
At all these stages of life, the functional decomposition of the system was the same, but its structural decomposition has changed considerably.
It is therefore more effective to think about functionality and roles of system parts, than to limit yourself with the fixed implementation straight away.
The implementation should follow the functionality and various requirements, not the other way around.
</p>
</div>
</div>
<div id="outline-container-orga16c64f" class="outline-3">
<h3 id="orga16c64f">Examples from computer systems</h3>
<div class="outline-text-3" id="text-orga16c64f">
<p>
To get accustomed to structural and functional decompositions we will study examples from computer systems and programming.
</p>

<ol class="org-ol">
<li>The first example is a <i>cursor</i>; we all see it constantly on computer screen as we point at things, drag them or otherwise interact with them.
Cursor is a functional component, not a structural one.
It is not equivalent to mouse, graphical tablet or any other device, because a program can take control over the cursor and move it around.</li>

<li><p>
A lot of programs are being executed simultaneously on your computer.
They coexist and are isolated from one another through <i>virtual memory</i>.
The function of virtual memory is to store data.
The virtual memory space itself consists of areas of forbidden addresses, files, mapped from hard drive, anonymous pages and so on.
The virtual memory is backed by the physical memory, storage (in case of memory swapping or mapping files) and operating system, which ensures transparent operation with it.
</p>

<p>
It does not seem possible to divide computer system into structural parts in a way that isolates one virtual memory space from all others, so it is not a structural component.
Another example is a process &#x2014; we have all seen a list of processes in a task manager. A similar argument is applicable to it: we can not divide computer on parts structurally to isolate one process. A process contains a virtual address space too, parts of which are shared among processes. So a process is also a functional component.
</p></li>

<li>The final example is <i>storage</i> &#x2014; its name alone suggests its function and hence that it is a functional component.
It can be implemented as a piece of hardware, but the data can be stored in a distributed filesystem, in a distributed database, in cloud.
In cloud storage it is not clear where a specific piece of data is stored due to factors like replication blurring it.</li>
</ol>


<p>
Don't think twice about <a href="mailto:igorjirkov@gmail.com">sending me a message</a> if something caught your eye :)
</p>
</div>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>Job offers [RnD, compilers, PLT, systems] are welcome</title>
      <link>https://rubber-duck-typing.com/posts/2022-03-22-job.html</link>
      <guid>https://rubber-duck-typing.com/posts/2022-03-22-job.html</guid>
      <pubDate>Tue, 22 Mar 2022 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
The time has come for me to work in Europe again.
</p>

<p>
For the last couple of years I was residing in Poland and France, but my main occupation was reforming a curriculum for system architects in ITMO university in St.-Petersburg, Russia.
Our idea was to create a novel curriculum in Computer Science and Engineering tying together the science of complex systems, programming, computer system architecture and strong mathematical foundations.
Quite a task if you ask me! The works connecting complex systems and software/system engineering are quite scarce at the moment.
</p>

<p>
We were lucky to start to assemble a great team of young and more seasoned professionals, passionate and smart students.
The level of education went up considerably, we have provided a vision and clearer goals towards a unique curriculum.
Sadly, due to the war I have to limit my engagement in this project and hence I am looking for a work to hone my hard skills.
</p>

<p>
I am looking for a job in Warsaw, France (remotely) or anywhere in EU (also remotely, or mostly).
My main area of interests includes compilers, programming language theory, formal semantics, programming language design, system design.
I like problems related to organizing code, systems, I know and understand the low level reasonably well (and have even written <a href="https://www.amazon.com/Low-Level-Programming-Assembly-Execution-Architecture/dp/1484224027">a book</a> about it).
I would love to do some R&amp;D requiring some interesting mathematics and writing code. I do love maths and studying &#x2013; the big part of my life problems were consequences of poor work/life balance, but thankfully this part is past me.
</p>

<p>
I have studied in ITMO university and Academic University of Saint-Petersburg. Then I have done research in IMT atlantique in Nantes, where I was proving correctness of some refactorings of the C language formally, using the formal semantics of CompCert verified C compiler.
</p>

<p>
[<a href="https://rubber-duck-typing.com/files/cv.pdf">my CV</a>]
</p>

<p>
Write me a <a href="mailto:igorjirkov@gmail.com">mail</a> if you have a project where I could be of a use.
</p>
]]></description>
    </item>
    <item>
      <title>When functions dissolve</title>
      <link>https://rubber-duck-typing.com/posts/2020-12-12-when-functions-dissolve.html</link>
      <guid>https://rubber-duck-typing.com/posts/2020-12-12-when-functions-dissolve.html</guid>
      <pubDate>Sat, 12 Dec 2020 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
In this post we are going to explore how the notion of function loses its
significance as an abstracted module of program logic when we compile a higher
level imperative language to the assembly code.
</p>

<p>
In imperative programming, functions isolate pieces of program logic.
For example, in C-like language, the following function would increase its
argument by one and return its value.
</p>


<div class="org-src-container">
<pre class="src src-C">  <span class="org-type">int</span> <span class="org-function-name">f</span><span class="org-rainbow-delimiters-depth-1">(</span> <span class="org-type">int</span> <span class="org-variable-name">x</span> <span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">{</span>
     <span class="org-keyword">return</span> x + <span class="org-highlight-numbers-number">1</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
Ideally, each function should have a single, well-defined goal and only contain the code both necessary and sufficient to accomplish the said goal.
</p>
<div id="outline-container-org648d8e3" class="outline-2">
<h2 id="org648d8e3">Modularity</h2>
<div class="outline-text-2" id="text-org648d8e3">
<p>
Imagine we have several programs running in parallel on the same computer.
Each program is a component of this computer system.
</p>

<p>
If these programs are threads of the same process, they are sharing the same
address space.
Therefore, one program can affect the functioning of another by corrupting its
memory structures, either deliberately or by mistake.
On the other hand, if the each program is running in a separate process, it has
its own virtual address space.
Then an error in one process will not influence the memory of another process,
provided they are not engaged in any other interactions.
There are of course details to that, like what happens when a process crashes
while another one is waiting for its response, but such situations can be dealt
with in a reasonable way e.g. through timeouts.
</p>


<p>
There is a concept of <b>modularity</b> in system design.
It means that to construct a system we start with smaller building blocks, which are built in isolation.
Then we assemble the bigger system from these blocks, setting up connections
between them.
</p>

<p>
In the example, we see two degrees of modularity.
When programs share their address space, the modularity is <b>softer</b>, it does not
prevent unintended interactions from happening.
An error in one program (module) can then affect other programs, disrupt their
functioning and lead to more errors.
This error propagation is out of control and leads to unpredictable consequences.
</p>

<p>
On the other hand, programs in different processes are less fragile.
Then the modularity is <b>stronger</b>.
</p>

<p>
To sum it up:
</p>

<dl class="org-dl">
<dt>weak, soft modularity</dt><dd>errors inside one module can propagate to other
modules bypassing interfaces.</dd>
<dt>strong modularity</dt><dd>modules are more isolated and the error propagation is
limited to the interfaces.</dd>
</dl>

<p>
These two notions are pure and extreme, real systems often fall somewhere in
between.
</p>
</div>
<div id="outline-container-org056a89f" class="outline-3">
<h3 id="org056a89f">Functions as modules</h3>
<div class="outline-text-3" id="text-org056a89f">
<p>
We are used to think about functions as the building blocks of program code.
Functions abstract away the complexity: a caller provides them with arguments,
and the function yields the computation result plus side-effects, like file
output.
</p>

<p>
In a high level programming language, functions give us some reasonable
modularity (if we do not use global variables or resources).
Any function can communicate failure to its caller through returning errors:
<code>NULL</code>, instances of optional types, or through exceptions.
</p>
</div>
</div>
<div id="outline-container-org6478054" class="outline-3">
<h3 id="org6478054">Higher level functions and lower level subroutines</h3>
<div class="outline-text-3" id="text-org6478054">
<p>
The source code is eventually compiled into machine code.
Without too much oversimplification, functions in higher level languages are translated into
functions in machine code.
To make a distinction between them we will call these lower level assembly functions <b>subroutines</b>.
</p>

<p>
Subroutines should support two kinds of operations:
</p>

<ul class="org-ul">
<li>we should be able to call subroutines from anywhere, and</li>
<li>subroutines should be able to return to the place where they have been called.</li>
</ul>

<p>
On common architectures, there are instructions or instruction patterns that are
used to call subroutines and return from them, like <code>call</code> / <code>ret</code> on Intel 64, or
<code>bl</code> / <code>pop pc</code> on ARM.
</p>

<p>
Subroutines are different from functions in one important way:
subroutines only operate in a common memory space and do not have their own isolated
piece of memory.
Functions have local variables and arguments; subroutines can allocate space on
stack just for themselves, but are forced to use
CPU registers, which are global and shared among them.
</p>

<p>
To make an analogy with higher-level language, imagine that:
</p>

<ul class="org-ul">
<li>all your functions accept zero arguments;</li>
<li>you are not allowed to create local variables;</li>
<li>you have a limited amount of global variables;</li>
<li>you have to repurpose the same global variables in different functions.</li>
</ul>

<p>
Because of this, modularity supported by subroutines is even "softer" than the function modularity.
I wonder if, in this case, we can talk about any modularity at all (except that each
subroutine is written in one place in the source code which makes a subroutine a
structural module).
Maybe the better way to think about subroutines is to accept that they do not exist.
Once we get there, we may better understand how assembly programs are written,
how they function and what possibilities it offers to us.
</p>

<p>
The rest of this post uses the assembly code as a device to tell about two
interesting concepts: tail-call elimination and coroutines.
We are going to optimize the subroutine <code>print_newline</code> in several stages up to
the point that it is going to lose its shape as an isolated subroutine and get
reduced to one instruction.
</p>


<div class="org-src-container">
<pre class="src src-asm"><span class="org-comment-delimiter">; </span><span class="org-comment">Gets symbol code in rdi and writes it in stdout</span>
<span class="org-function-name">print_char</span>:
   ...
   <span class="org-comment-delimiter">; </span><span class="org-comment">the code of print_char is not important to us</span>
   ...
   <span class="org-keyword">ret</span>

<span class="org-comment-delimiter">; </span><span class="org-comment">Prints newline character</span>
<span class="org-function-name">print_newline</span>:
    <span class="org-keyword">mov</span> rdi, 0xA          <span class="org-comment-delimiter">; </span><span class="org-comment">code</span>
    <span class="org-keyword">call</span> print_char
    <span class="org-keyword">ret</span>
</pre>
</div>

<p>
The subroutine <code>print_newline</code> is just an adapter for <code>print_char</code>:
</p>

<div class="org-src-container">
<pre class="src src-c">  <span class="org-type">void</span> <span class="org-function-name">print_newline</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    print_char<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-highlight-numbers-number">0xA</span><span class="org-rainbow-delimiters-depth-2">)</span>;
      <span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orge15749c" class="outline-2">
<h2 id="orge15749c">Tail call optimization</h2>
<div class="outline-text-2" id="text-orge15749c">
<p>
Tail call happens when the subroutine ends with a call to another subroutine.
In the higher level language this happens in cases such as:
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-type">void</span> <span class="org-function-name">g</span><span class="org-rainbow-delimiters-depth-1">()</span>;

<span class="org-type">void</span> <span class="org-function-name">f</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    g<span class="org-rainbow-delimiters-depth-2">()</span>;
    <span class="org-keyword">return</span>;
<span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
This is also a tail call, because we just return what another function returns:
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-type">int</span> <span class="org-function-name">g</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-type">int</span> <span class="org-variable-name">x</span><span class="org-rainbow-delimiters-depth-1">)</span>;

<span class="org-type">int</span> <span class="org-function-name">f</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    <span class="org-keyword">return</span> g<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-2">)</span>;
<span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
This is not a tail call: after calling function we have to multiply its result
to another number.
We wait until <code>fact(n-1)</code> completes its execution, and then use its result in
other computations.
Note that in this example the function calls itself rather than some other
function, but this is unimportant.
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-type">int</span> <span class="org-function-name">fact</span><span class="org-rainbow-delimiters-depth-1">(</span> <span class="org-type">int</span> <span class="org-variable-name">n</span> <span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">{</span>
  <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span>n &lt; <span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">1</span>;
  <span class="org-keyword">return</span> n * fact<span class="org-rainbow-delimiters-depth-2">(</span>n-<span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-2">)</span>;
<span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
On the assembly level, a tail call corresponds to the following pattern of instructions:
</p>

<div class="org-src-container">
<pre class="src src-asm"><span class="org-function-name">f</span>:
  ...
  <span class="org-keyword">call</span> other
  <span class="org-keyword">ret</span>
</pre>
</div>

<p>
This pair of instructions is located in a function which we are going to call <code>f</code>.
The control reaches this function from some other function, say,  <code>f_caller</code>.
</p>

<p>
Let us follow the state of stack through the execution of <code>f_caller</code>, <code>f</code> and <code>other</code>.
For that we expand this code to include <code>f_caller</code> and <code>other</code> functions.
</p>

<div class="org-src-container">
<pre class="src src-asm"><span class="org-function-name">f_caller</span>:

...
   <span class="org-keyword">call</span> f
   <span class="org-keyword">&lt;next</span> instruction&gt; <span class="org-comment-delimiter">; </span><span class="org-comment">mark 4</span>
...

<span class="org-function-name">f</span>:            <span class="org-comment-delimiter">; </span><span class="org-comment">mark 1</span>
  ...
  <span class="org-keyword">call</span> other
  <span class="org-keyword">ret</span>         <span class="org-comment-delimiter">; </span><span class="org-comment">mark 3</span>
...
<span class="org-function-name">other</span>:        <span class="org-comment-delimiter">; </span><span class="org-comment">mark 2</span>
   ...
   <span class="org-keyword">ret</span>
</pre>
</div>

<ul class="org-ul">
<li>When we start executing <code>f</code> the stack holds the return address inside <code>f_caller</code> (we reach  mark 1).</li>
<li>When we call <code>other</code> the stack holds the return addresses for <code>f_caller</code>, then <code>f</code> on top (we reach mark 2).</li>
<li>The subroutine <code>other</code> returns too, in this moment we have only return address for <code>f_caller</code> on top of the stack  (we reach mark 3).</li>
<li>The subroutine <code>f</code> returns, popping return address of <code>f_caller</code> from the stack  (we reach mark 4).</li>
</ul>

<p>
The last two steps were essentially popping two return addresses from stack consecutively.
It suggests that the first one (the return address to <code>f</code>) is useless and we do not need to store it.
We are indeed right.
</p>

<p>
The <code>call other</code> instruction is equivalent to the pair of pseudoinstructions:
</p>

<div class="org-src-container">
<pre class="src src-asm"><span class="org-function-name">push</span> <span class="org-keyword">rip</span>    <span class="org-comment-delimiter">; </span><span class="org-comment">rip is the program counter register</span>
<span class="org-function-name">jmp</span> <span class="org-keyword">other</span>
</pre>
</div>

<p>
If we do not need to push return address to <code>f</code>, then we can just substitute this instruction for <code>jmp</code> and get rid of one <code>ret</code>:
</p>


<div class="org-src-container">
<pre class="src src-asm"><span class="org-function-name">f_caller</span>:

...
   <span class="org-keyword">call</span> f
   <span class="org-keyword">&lt;next</span> instruction&gt;
...

<span class="org-function-name">f</span>:
  ...
  <span class="org-keyword">jmp</span> other
...
<span class="org-function-name">other</span>:
   ...
   <span class="org-keyword">ret</span>     <span class="org-comment-delimiter">; </span><span class="org-comment">will return directly to f_caller</span>
</pre>
</div>

<p>
The subroutine <code>other</code> becomes essentially the continuation of the subroutine <code>f</code>; we pass from <code>f</code> to <code>other</code> via a simple branching instruction.
</p>


<p>
Coming back to our example, we can rewrite it like that:
</p>

<div class="org-src-container">
<pre class="src src-asm"><span class="org-function-name">print_char</span>:
   ...
   <span class="org-keyword">ret</span>

<span class="org-function-name">print_newline</span>:
    <span class="org-keyword">mov</span> rdi, 0xA          <span class="org-comment-delimiter">; </span><span class="org-comment">code</span>
    <span class="org-keyword">jmp</span> print_char
</pre>
</div>
</div>
<div id="outline-container-org48482e9" class="outline-3">
<h3 id="org48482e9">Tail recursion</h3>
<div class="outline-text-3" id="text-org48482e9">
<p>
What if <code>other</code> and <code>f</code> are the same function?
Then the <code>jmp</code> is performed to the start of <code>f</code> making the whole thing into a loop:
</p>


<div class="org-src-container">
<pre class="src src-asm"><span class="org-function-name">f_caller</span>:

...
   <span class="org-keyword">call</span> f
   <span class="org-keyword">&lt;next</span> instruction&gt;
...

<span class="org-function-name">f</span>:
  ...
  <span class="org-keyword">jmp</span> f
</pre>
</div>

<p>
This is what we mean when we say that the compiler optimizes tail recursion into a loop.
</p>
</div>
</div>
</div>
<div id="outline-container-org1a75eee" class="outline-2">
<h2 id="org1a75eee">Coroutines</h2>
<div class="outline-text-2" id="text-org1a75eee">
<p>
Our current example is the implementation of <code>print_newline</code> in two instructions:
</p>

<div class="org-src-container">
<pre class="src src-asm"><span class="org-function-name">print_char</span>:
   ...
   <span class="org-keyword">ret</span>

<span class="org-function-name">print_newline</span>:
    <span class="org-keyword">mov</span> rdi, 0xA          <span class="org-comment-delimiter">; </span><span class="org-comment">code</span>
    <span class="org-keyword">jmp</span> print_char
</pre>
</div>

<p>
When the example is written like this it is easy to guess the next step.
If the execution is sequential why bother with a jump here?
The control routinely falls from one instruction to the next one anyway.
</p>


<div class="org-src-container">
<pre class="src src-asm"><span class="org-function-name">print_newline</span>:
    <span class="org-keyword">mov</span> rdi, 0xA          <span class="org-comment-delimiter">; </span><span class="org-comment">code</span>
<span class="org-function-name">print_char</span>:
   ...
   <span class="org-keyword">ret</span>
</pre>
</div>

<p>
The subroutines have merged here, and it does not look like there are
two separate functions anymore.
We got a subroutine with <b><b>two entry points</b></b> labeled <code>print_newline</code> and <code>print_char</code>.
We call such a thing a <b>coroutine</b>.
</p>

<p>
Coroutines are a generalization of subroutines, they may have a state and
multiple entry points. You may often encounter them or their variations in
modern languages e.g. generators in Python or lazy <code>IEnumerable</code>'s in C#. Our
coroutine does not have a state, however. It is funny to note that coroutines
appeared first as a assembly programming pattern and is not a fancy new thing
like cubical type theory provers.
</p>

<p>
If your language supports continuations, you can implement coroutines easily.
</p>
</div>
</div>
<div id="outline-container-orge76ad56" class="outline-2">
<h2 id="orge76ad56">Conclusion</h2>
<div class="outline-text-2" id="text-orge76ad56">
<p>
I do like to draw connections between seemingly distant notions and areas.
I also enjoy learning new points of view on things I already know.
My students inspired me to write this post because for a lot of them, coming
from higher level languages, the functions were entities with well shaped
bodies, having a visible start and end.
Well, in assembly they do not have to.
</p>

<p>
It also supports a way of thinking about compiled code as a "soup" of sorts,
where everything is blended together, reordered, precomputed, optimized until
only the actual actions and their order suggest how the original program looked
like. I think this is especially useful when reasoning about parallel programs
and lock-free algorithms.
</p>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>Writing essays</title>
      <link>https://rubber-duck-typing.com/posts/2018-09-06-writing-essays.html</link>
      <guid>https://rubber-duck-typing.com/posts/2018-09-06-writing-essays.html</guid>
      <pubDate>Sun, 16 Sep 2018 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
I am not a naturally good writer. It took me writing a 450 pages book and
countless pages of notes, reports, essays and posts to only start building a
sane mechanism to identify the exact flaws in my writing (and in the text
creation process itself). I found an introductory document on writing essays by
J.B.Peterson to be of a great use for me. I think it is valuable for someone
like me who has to write a lot of text for his PhD, albeit my domain is
computer science. These are my notes for it.
</p>
<div id="outline-container-orgf69deb3" class="outline-2">
<h2 id="orgf69deb3">General concerns</h2>
<div class="outline-text-2" id="text-orgf69deb3">
<ul class="org-ul">
<li>Eating well is important. Proteins and fats are good for breakfast.</li>
<li>You have a daily limit of several hours of productive work. This is why working every day is so important.</li>
<li>Try to concentrate for 15 minutes straight, if you succeed the concentration will persist.</li>
<li>If you have writer's block, read. It gives food for thought.</li>
<li>Separate writing (create) from editing (reduce, arrange).</li>
<li>There are rules for writing, which work most of the time. They are dictated by empirical evidence on providing the best reading experience and by the experience of other writers.</li>
<li>First draft should be longer than the final version (probably 25%).</li>
<li>Better grab the reader's attention immediately, so do not forward reference in the beginning.</li>
<li>Write often to structure your thoughts.</li>
</ul>
</div>
</div>
<div id="outline-container-org9a214ba" class="outline-2">
<h2 id="org9a214ba">Levels of perceptions</h2>
<div class="outline-text-2" id="text-org9a214ba">
<p>
There are many choices we make when we write. These choices affect the text
semantics. We can categorize them based on the "level of resolution":
</p>

<ul class="org-ul">
<li>Selection of words.</li>
<li>Sentence structure.</li>
<li>Order of sentences in a paragraph.</li>
<li>Order of paragraphs in an essay.</li>
<li>The essay as a whole.</li>
<li>Essay as seen by a reader (prism of his mind and personal experience).</li>
<li>Essay in a context of culture the reader is in.</li>
</ul>
</div>
</div>
<div id="outline-container-org05c7ceb" class="outline-2">
<h2 id="org05c7ceb">Method</h2>
<div class="outline-text-2" id="text-org05c7ceb">
<ol class="org-ol">
<li>Choose a topic</li>
<li>Make a reading list and read; take notes (probably 2-3 times the amount of
final text you need).</li>
<li>Write an outline (10-15 sentences). Stock introductions/conclusions are ok,
but should be thrown away after.</li>
<li>Write a paragraph per outline heading (10-15 sentences). Do not edit too
much. A paragraph should present a single idea.</li>
<li>For a single paragraph: place each sentence on its own line. Rewrite each
sentence to make it smoother and better. You can read it aloud and listen to
yourself. Try to cut length by 15-25%. Do for all paragraphs.</li>
<li>Repeat by looking at each paragraph as a whole. Sentences that are no longer
necessary are to be eliminated here.</li>
<li>Read your essay. Try to make an outline without looking at the text. It will
help throwing some sentences away, rearrange them etc.</li>
<li>Wait a few days and re-read everything. When you are rewriting something but
you are not sure it becomes better anymore, it is time to stop.</li>
</ol>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>Variance in programming languages</title>
      <link>https://rubber-duck-typing.com/posts/2018-05-01-variance-in-programming-languages.html</link>
      <guid>https://rubber-duck-typing.com/posts/2018-05-01-variance-in-programming-languages.html</guid>
      <pubDate>Tue, 01 May 2018 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
<i>Covariance</i>, <i>invariance</i>, and <i>contravariance</i> are concepts many students have difficulties to grasp. However, the idea behind them is pretty simple. This post will attempt to illustrate that in a shortest and simplest way possible.
</p>

<p>
Take an imaginary object-oriented language with generic types. Java or C# will do. We are going to draw diagrams, where rectangles represent types and arrows represent a relation "is parent of".
</p>

<p>
<code>A</code> \(\rightarrow\) <code>B</code> means "<code>A</code> is a parent of <code>B</code>", or, in other words, "<code>A</code>
is a supertype of <code>B</code>".
</p>

<p>
Let's create two hierarchies: 
</p>
<ul class="org-ul">
<li>one consists of three classes, each with a type parameter: <code>A</code>, <code>B</code> and <code>C</code> ;</li>
<li><p>
the other contains no parameterized types: <code>x</code>, <code>y</code>, <code>z</code>.
</p>


<div id="org4cbcb1c" class="figure">
<p><img src="../img/two-hierarchies.png" alt="two-hierarchies.png" />
</p>
</div></li>
</ul>

<p>
Now we will draw a 3x3 matrix with all possible substitutions of <code>x</code>, <code>y</code> and <code>z</code> as type parameters of <code>A&lt;T&gt;</code>, <code>B&lt;T&gt;</code> and <code>C&lt;T&gt;</code>, like this:
</p>


<div id="org1473e7a" class="figure">
<p><img src="../img/two-hierarchies.png" alt="two-hierarchies.png" />
</p>
</div>

<p>
You already see the arrows like <code>A&lt;x&gt;</code> \(\rightarrow\) <code>B&lt;x&gt;</code>; these are not
going anywhere, they are valid for any value of type argument.
</p>


<p>
What is of interest to us is: for a parameterized type such as <code>A</code>, how will
<code>A&lt;x&gt;</code> and <code>A&lt;y&gt;</code> be related? 
</p>

<p>
There can be three cases:
</p>

<dl class="org-dl">
<dt>Invariance</dt><dd>whatever relation exists between <code>x</code> and <code>y</code>, <code>A&lt;x&gt;</code> and <code>A&lt;y&gt;</code> have no relation whatsoever.</dd>
<dt>Covariance</dt><dd>if <code>x</code> \(\rightarrow\) <code>y</code>, then <code>A&lt;x&gt;</code> \(\rightarrow\) <code>A&lt;y&gt;</code>.</dd>
<dt>Contravariance</dt><dd>if <code>x</code> \(\leftarrow\) <code>y</code>, then <code>A&lt;x&gt;</code> \(\rightarrow\) <code>A&lt;y&gt;</code>.</dd>
</dl>



<div id="org172127f" class="figure">
<p><img src="../img/three.png" alt="three.png" />
</p>
</div>

<p>
It's all on the picture: for invariance, <code>A&lt;x&gt;</code> and <code>A&lt;y&gt;</code> are not connected for
<code>x</code> \(\neq\) <code>y</code>; for covariance, we draw more arrows according to the hierarchy
of type arguments themselves; for contravariance, we invert these arrows.
</p>
<div id="outline-container-org2a927a7" class="outline-2">
<h2 id="org2a927a7">Language prerequisites</h2>
<div class="outline-text-2" id="text-org2a927a7">
<p>
In order for variance to even exist we need the language to have the following
features:
</p>

<ul class="org-ul">
<li>It has to be typed</li>
<li><p>
It should sometimes allow an entity of type <span class="underline">A</span> to be implicitly interpreted
as an entity of type <span class="underline">B</span>.
</p>

<p>
The most common cases are:
</p>
<ul class="org-ul">
<li><p>
<i>Subtyping</i> in class hierarchies. 
</p>

<p>
Basically, everything that has an "Object Oriented Programming" label on
it: C++, C#, Java etc.
</p></li>
<li><p>
Implicit type conversions (<i>coercions</i>).
</p>

<p>
For example, in C/C++/Java, there are implicit conversions from numeric
types into their wider versions, like <code>short</code> to <code>long</code>, or <code>float</code> to
<code>double</code>.
</p></li>
</ul></li>

<li><p>
We also need parameterized types. 
</p>

<p>
It does not mean a presence of generics though, as functions alone are enough
to introduce covariance and contravariance.
</p></li>
</ul>

<p>
In other words, we need to be able to represent types as an oriented graph.
</p>
</div>
</div>
<div id="outline-container-org49774ee" class="outline-2">
<h2 id="org49774ee">Common facts</h2>
<div class="outline-text-2" id="text-org49774ee">
<p>
Here I want to mention a couple of facts it is useful to be aware of.
</p>
</div>
<div id="outline-container-org720aaea" class="outline-3">
<h3 id="org720aaea">Function variance</h3>
<div class="outline-text-3" id="text-org720aaea">
<p>
Functions are contravariant on arguments and covariant on return type. Why so?
</p>

<ul class="org-ul">
<li>A function that returns a <code>Cat</code> can be used to fill a variable of type
<code>Animal</code>. Hence we got the "natural" way: <code>Animal</code> is more generic than <code>Cat</code>,
function returning <code>Animal</code> is more generic than function returning <code>Cat</code>.</li>
<li>If you need a function that can work on <code>Cat</code> (its argument), it is safe to
use a function that can work on <code>Animal</code> instead, or any supertype of <code>Cat</code>.
Hence, the function with a more generic argument type is of a <span class="underline">more specific</span> type itself.</li>
</ul>
</div>
</div>
<div id="outline-container-orgac7563a" class="outline-3">
<h3 id="orgac7563a">Variance and mutability</h3>
<div class="outline-text-3" id="text-orgac7563a">
<p>
As a rule of thumb, <span class="underline">immutable collections can be covariant, mutable collections should be invariant</span>.
</p>

<p>
Imagine, that a mutable <code>List</code> is covariant. Then take a <code>List&lt;String&gt;</code>. You can
reinterpret it as a <code>List&lt;Object&gt;</code>, because <code>Object</code> \(\rightarrow\) <code>String</code>.
That list can store anything, so it is a valid operation to add an integer to
it. From the type perspective, its method <code>set (int idx, T value)</code> will become 
<code>set(int idx, Object value)</code>, so it is valid to give it an integer as a value.
If we do it, we are screwed because we just have added an integer to a list
that assumes it is holding strings, the objects of an incompatible type,
effectively hacking the type system.
</p>

<p>
The next time when we try to use some function like <code>printString</code> on all
elements of the said list, we are up for some surprises, ranging (depending on
the language semantics) from runtime errors, to undefined behavior.
</p>

<p>
In Scala, if you try to define a covariant List, you will get warned on the
definition of its <code>set</code> method, that accepts an argument of type <code>T</code>
(corresponding to the list contents). As an argument of <code>set</code>, <code>T</code> should be
contravariant (as in any function), but as a type parameter of <code>List</code>, it will
be marked as covariant (because we made it so in <code>List</code> definition). Hence the
warning: "Covariant argument in contravariant position".
</p>

<p>
Immutable collections do not have such problem. If we try to replicate that
example, we will just get a new <code>List</code> of objects, for which it is perfectly
fine to store everything you might want it to.
</p>
</div>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>Advice for programming students</title>
      <link>https://rubber-duck-typing.com/posts/2018-03-17-advice-for-programming-students.html</link>
      <guid>https://rubber-duck-typing.com/posts/2018-03-17-advice-for-programming-students.html</guid>
      <pubDate>Sat, 17 Mar 2018 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
There are many things I wish I knew when I started my journey as a programming
student. Almost 10 years have passed since, and, sadly, I can not share my
experience or insights with my past self, only with my younger colleagues. This
post collects some of the most useful bits of advice I wish I heard when I was 18.
</p>
<div id="outline-container-org00941c0" class="outline-2">
<h2 id="org00941c0">Decide who you are</h2>
<div class="outline-text-2" id="text-org00941c0">
<p>
You certainly do not need to be familiar with formal logic or categories if you
want to just know one practical thing (say, frontend) and only do it. There are
two main paths which differ by an effort, duration and outcome.
</p>

<ul class="org-ul">
<li>You can become proficient in one domain relatively fast — say one, two years.
You will not be useless, you will do things and make a living. There are enough
job opportunities (at least, for now) which do not demand much versatility.</li>

<li>You can become a well rounded specialist who invested a lot of time and effort in foundations. Then you will be able to adapt and switching career paths becomes relatively easy. You can do machine learning, then formal verification, than some low-level programming for trading or switch to game dev. That demands time and dedication — I’d estimate a minimum of 6–8 years.</li>
</ul>

<p>
I strongly advocate the second path because it’s more versatile, interesting,
and brings more in the long run. IT is ever-changing so you want to pick up new
technologies fast. You also have more choice. Should you choose the hard way,
the rest of this post should be of a use for you.
</p>
</div>
</div>
<div id="outline-container-orgd554865" class="outline-2">
<h2 id="orgd554865">Learn math because math is useful</h2>
<div class="outline-text-2" id="text-orgd554865">
<p>
I can not stress that enough. When you start,
you might think that you don’t need linear algebra, because you are unaware of
applications. However, for any non-trivial machine learning, you need it. You
need statistics and probability. You need logic, combinatorics, set theory, all
sorts of discrete mathematics, graph theory, computability, formal grammars,
lambda calculus, formal semantics, topology, type theories, a bit of number
theory, groups, rings, fields, categories.
</p>

<p>
New technologies are constantly emerging. Many of them are based on the existing
mathematical models. If you know the underlying mathematics well, you get very
nice perks:
</p>

<ul class="org-ul">
<li>Picking new trendy things is orders of magnitude simpler.</li>
<li>You understand where you can apply new methods and where you should not.</li>
<li>You usually understand why the solutions are the way they are. Then you can tweak them to better suit the context.</li>
</ul>

<p>
For example, I have had an impression, that few people understand, that you
should not always use least squares to evaluate how well your linear regression
fits the data. This is only adequate when the errors are distributed normally
with the appropriate mean value. If it is not the case, you will blindly apply
an inadequate solution without even thinking that a part of the model needs
tweaking.
</p>
</div>
</div>
<div id="outline-container-orgb271f54" class="outline-2">
<h2 id="orgb271f54">Learn math to learn mathematical thinking</h2>
<div class="outline-text-2" id="text-orgb271f54">
<p>
Writing proofs makes you rigorous. You want to always think about all possible
paths of execution your program can take in order to not introduce bugs and
security issues. The clarity of thinking gained from constructing proofs is
precious. It also helps you in writing short, concise code.
</p>
</div>
</div>
<div id="outline-container-org4554d4f" class="outline-2">
<h2 id="org4554d4f">Pick your first language carefully</h2>
<div class="outline-text-2" id="text-org4554d4f">
<p>
It should be well designed, which means:
</p>

<ul class="org-ul">
<li>Consistency.</li>
<li>Small core.</li>
<li>No unnecessary complexity (it often comes from inconsistency: there are things you should just remember or constantly be aware of, that bring nothing useful to the table).</li>
<li>Makes it harder to shoot yourself in the foot.</li>
<li>This should also be a high level language, because <b><b>programming is problem solving</b></b>, not a mastery of a specific language. Knowing all little particularities of your favorite language is not a mastery of programming in itself.</li>
</ul>

<p>
I advise one of these languages:
</p>

<ul class="org-ul">
<li>Scheme (there is a good classical introductory course “Structure and Interpretation of Computer Programs”)</li>
<li>Smalltalk</li>
<li>Eiffel</li>
<li>ML</li>
</ul>

<p>
Don’t be fooled by their seeming unpopularity, in the programming world the popularity does not mean quality.
Do not start with Python, pretty please! It is badly designed, inconsistent, and does not teach you rigorous thinking. No need to get used to "well, it seems to usually work" mentality. Python has its uses, but not as a first language.
</p>
</div>
</div>
<div id="outline-container-org3be04e0" class="outline-2">
<h2 id="org3be04e0">Expose yourself to greatness</h2>
<div class="outline-text-2" id="text-org3be04e0">
<p>
If you get used to crap languages and crap tools, and crap software, and crap solutions, you will inevitably replicate them in your own work. Be critical, question everything, critic everything, search for inconsistencies and flaws.
</p>

<p>
For example, imagine you are learning a new language, Go. Google "Go language sucks" and read why people criticize it. Some of them will be pathetic, but some will actually have a point. It is likely, that you will obtain new knowledge from reading critical remarks and evaluating, whether they actually have a point, or are just there to whine.
</p>
</div>
</div>
<div id="outline-container-org15894ac" class="outline-2">
<h2 id="org15894ac">Think on your own</h2>
<div class="outline-text-2" id="text-org15894ac">
<p>
I am teaching programming (C and assembly) since 2009 to the students in ITMO university in St.Petersburg, Russia. A lot of people have trouble programming and never actually succeed in learning it because of inability of creating code. When they get an assignment, they try to imitate an existing solution, maybe take some snippets from Stack Overflow, tune them to their liking. OK fine, you got your solution, what else do you want?
</p>

<p>
<b>You should learn to write code from scratch</b>. The types of skills needed for that are so different from meddling with existing code!
</p>

<p>
Programming is about making conscientious choices. You are in state A (you have access to a number of language features/libraries and you know how to combine them); you want to get to state B (the language constructions are combined in a way to express a solution). How do you build a route from A to B? Now, that is the real programming, the <b>problem solving</b>.
</p>

<p>
When you start writing programs from scratch, it will be hard, but it is absolutely necessary to learn to build things from zero. To improve your problem solving skills it is crucial that you learn algorithms and data structures. Pick up a good book and solve contests online. I recommend “Algorithms” of Dasgupta for a start, then the classic work of Cormen. This will open a whole new world for you, I promise.
</p>

<p>
The complimentary part of software creation process is designing the software architecture; it is impossible to learn to structure your programs well without building them from 0 to 100.
</p>
</div>
</div>
<div id="outline-container-org6522a04" class="outline-2">
<h2 id="org6522a04">Broaden your horizons</h2>
<div class="outline-text-2" id="text-org6522a04">
<p>
<b>Program everyday, do side projects</b>. There is a very easy (and mostly accurate) way for me as a teacher to understand that my student will succeed as a programmer with a high probability. One question: What are you programming in your free time?
</p>

<p>
There is just not enough time for your teachers to tell you about everything. After all, after you are out of the university, you have to continue to learn on your own, until you retire. If you are passionate about what you are doing, you will explore different types of software just for fun, and that will give you much more experience and skills, than your less motivated peers will have.
</p>

<p>
Ideally, you should touch everything: write your own compiler, maybe a toy OS, http server, database engine, games, ray casting, build some neural networks, fiddle with proof assistants and dependent types, write a simple mobile app, write for embedded … you go on. Place all your projects on GitHub and take pride in them: your future employer might have a look at it. Use this portfolio to your advantage.
</p>

<p>
It is common knowledge, that recruiting a good programmer is extremely hard. Many programmers applying for jobs have trouble writing trivial things like FizzBuzz. If you have existing projects hosted on GitHub, the employer will be more assured that you are the real deal.
</p>
</div>
</div>
<div id="outline-container-org4a1852d" class="outline-2">
<h2 id="org4a1852d">Expose yourself to different tools and languages</h2>
<div class="outline-text-2" id="text-org4a1852d">
<p>
If someone tells you all languages are alike, this is either an oversimplification or a lack of experience. Let me explain that a bit.
</p>

<p>
A <i>model of computation</i> is a set of basic operations and ways of gluing them together in order to build complex algorithms. Some languages have very similar models of computations, but some are very different.
</p>

<p>
Programming is so much bigger than your commonly known C/Python/Java/C++/C#/Go/Javascript, which are all built on the same principles: imperative, structural, with occasional bits of OOP and syntactic sugar to mimic other programming styles. The world of programming languages is huge, here is a little taste of it:
</p>

<ul class="org-ul">
<li>Industrial functional programming languages with complex and well thought out type systems (Haskell, Ocaml)</li>
<li>Functional languages with dependent types, which allow not only to program, but to write proofs of correctness (Coq, Agda, LEAN)</li>
<li>Stack based, concatenative languages? (Forth)</li>
<li>Logic programming (Prolog, Refal)</li>
<li>Finite state machines (regular expressions, Promela)</li>
<li>Heavily extensible languages allowing to implement virtually any syntax constructs, as Lisp, Forth, Camlp4/5 or Rebol allow.</li>
<li>Domain-Specific Language workbenches such as JetBrains MPS or XText</li>
</ul>

<p>
Every new model of computations is hard to learn, because it is a new way of thinking for you. But the investment is worth your time, because once you get familiar with it:
</p>

<ul class="org-ul">
<li>Every language based on it is easy.</li>
<li>Every language whose model of computations has similarities is easier.</li>
<li>As every model of computations is very fitting for specific type of problems to solve, you now have a new powerful tool, whose usage in specific contexts is orders of magnitude more productive.</li>
</ul>
</div>
</div>
<div id="outline-container-org340962b" class="outline-2">
<h2 id="org340962b">Be social</h2>
<div class="outline-text-2" id="text-org340962b">
<p>
I have been very fortunate to know some amazing people. My mates helped me to perfect my skills, to learn something new, to see the world from a different point of view. Isolating yourself will bring you no good in the long run: you need other people to discuss, to see what they are up to, what they think. If your mate has read an interesting article and told you about it, you just saved a lot of your own time, because he spoon-fed you with a processed, crystallized knowledge.
</p>
</div>
</div>
<div id="outline-container-org45e335e" class="outline-2">
<h2 id="org45e335e">Stick with passionate, smart people, and try learning from them</h2>
<div class="outline-text-2" id="text-org45e335e">
<p>
You will be surprised, how much you can learn during a lunch time with your mates, who are eager to share the details of their work or research. This kind of idea cross-pollination is one of the main reasons corporations like Google give you free quality food.
</p>
</div>
</div>
<div id="outline-container-org90a5a9f" class="outline-2">
<h2 id="org90a5a9f">Ask people who are better in coding for code reviews and read their code</h2>
<div class="outline-text-2" id="text-org90a5a9f">
<p>
Looking at someone’s work, given he is better than you, can teach you a lot, in ways you do not expect. Code reviews are even better, because the guy will tell you, how he would have written the same code. This is probably one of the most effective ways to become a better coder very, very fast.
</p>
</div>
</div>
<div id="outline-container-org683d055" class="outline-2">
<h2 id="org683d055">Write tests</h2>
<div class="outline-text-2" id="text-org683d055">
<p>
This is so important, that it has a section on its own. Tests are an integral part of creating software, and even guys like me who are working on formally verified software (which means it should be mathematically proven correct) are writing tests, albeit one might think, that the guarantees given by proofs are strictly stronger.
</p>




<p>
I hope that this might actually help someone to get a bigger picture, learn faster and become a better programmer; should you have any questions, I will be glad to help. Good luck!
</p>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>Why every programming student should learn Coq</title>
      <link>https://rubber-duck-typing.com/posts/2018-03-11-why-every-programming-student-should-learn-coq.html</link>
      <guid>https://rubber-duck-typing.com/posts/2018-03-11-why-every-programming-student-should-learn-coq.html</guid>
      <pubDate>Sun, 11 Mar 2018 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
My personal experience with Coq proof assistant over the last years made me
think, that such tool, as exotic and niché as it might seem, is invaluable in a
programmer's education. Maybe we should include it in common Software
Engineering and Computer Science master programs, so that students prove
theorems using it.
</p>

<p>
When we are learning to program, we are trying to make a piece of code "just
work". It means, that in a certain context it should demonstrate an expected
behavior. Kind of like:
</p>

<ul class="org-ul">
<li>We are currently in a context \(A\)</li>
<li>We want to get to another context \(B\)</li>
<li>How do we get from \(A\) to \(B\)?</li>
</ul>

<p>
Then we are building a system of language constructions in order to be able to
finally combine them into a more or less straight road from \(A\) to \(B\).
</p>

<p>
And here is the source of a vast majority of programming errors: the definition
of a working program. A program "works" when it does not demonstrate an
unexpected behavior in any possible context! So, our roads we have built should
never allow us to get somewhere we do not want to go. Most programmers are
usually OK with a fairly superficial analysis of possible values and program
behaviors. Behaviors in plural is not a mistake here, because many languages are
allowing for a non-deterministic model of computation. It means, for example, that
in C we do not know whether <code>f</code> or <code>g</code> will be called first in this piece of code:
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-type">int</span> <span class="org-variable-name">x</span> = f<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">4</span><span class="org-rainbow-delimiters-depth-1">)</span> + g<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>

<p>
Now add for a total of 10 functions and make sure all of them have side effects (like logging or networking) to make sure a compiler will chose one of \(10!\) possible behaviors:
</p>


<div class="org-src-container">
<pre class="src src-c"><span class="org-type">int</span> <span class="org-variable-name">x</span> = 
  f1<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f2<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f3<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f4<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f5<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f6<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f7<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f8<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f9<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span> +
  f0<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">42</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>


<p>
Most languages used in either industry or for education are giving too much
liberty to a coder, the right to which is usually exercised in the least
convenient places. For example, the <code>switch</code> construction which does zero
reasoning about whether the cases are exhaustive or not.
</p>


<p>
The first step towards more correct programs is learning functional languages
with well thought out type systems, like Ocaml and Haskell. Their syntax and
semantics are implemented in a way that makes you think about all possible
branches of execution in a more concise way. I mean such features as pattern
matching, expressions and statements not being separated into two different
syntactic categories, <code>else</code> branch being mandatory. This make you adapt a
better reflex of thinking about all possible behaviors in a given context and 
cutting unwanted roads right away. This reflex stays with you no matter what
language you are programming in, and will make you a better Java or C programmer.
</p>

<p>
Now, Coq is doing that at an extreme. You have to write proofs. Writing a
non-trivial Coq program and proving it correct is hard and verbose; this is an
excellent exercise of thought discipline. Every inference should be explicitly
stated. 
</p>

<p>
Surely, you might ask, why can't I just do more mathematics? It is indeed right,
that building mathematical proofs as we are doing in e.g. calculus is aimed at
developing the same skills. However, using automated proof assistant makes this
exercise much more efficient because of the feedback look.
Your proof is being constantly checked and the prover does not allow you to
complete it if you forget a corner case here and there. Such proofs are also
much more verbose, because of being highly formal.
</p>
]]></description>
    </item>
    <item>
      <title>Putting a bigger sphere inside a smaller one</title>
      <link>https://rubber-duck-typing.com/posts/2018-01-21-bigger-sphere-inside-smaller-sphere.html</link>
      <guid>https://rubber-duck-typing.com/posts/2018-01-21-bigger-sphere-inside-smaller-sphere.html</guid>
      <pubDate>Sun, 21 Jan 2018 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
Can we put a sphere with a bigger radius inside of a smaller one so that it
would be fully contained? The answer is yes, but we have to select the space we
are working in carefully. I am going to provide two examples here:
</p>
<div id="outline-container-orgd30f8d3" class="outline-2">
<h2 id="orgd30f8d3">First example: metric space</h2>
<div class="outline-text-2" id="text-orgd30f8d3">
<p>
Suppose we are in a metric space with Euclidean metric. The space is a sphere itself, with radius \(r\). Let us denote a closed sphere of radius \(r\) with center at 0 as \(D_r(0)\). This sphere is a subset of \(R^n\).
</p>


<div id="org7b51972" class="figure">
<p><img src="../img/space.png" alt="space.png" />
</p>
</div>

<p>
What is a sphere? By definition it is a point of a space and a set of such points that are no further than the sphere's radius.
What if we try to define a sphere of radius \(R > r\) inside the space we are working in?
</p>


<div id="orgf67ba3f" class="figure">
<p><img src="../img/spheres.png" alt="spheres.png" />
</p>
</div>

<p>
Why does this sphere look as an intersection of two? We are, by definition, effectively selecting a <b><b>subset</b></b> of points of space \(D_r(0)\) whose distance to \(B\) is less or equal to \(r\). Any point of \(R^n\) lying outside of \(D_r(0)\) is out of reach.
</p>

<p>
As long as \(R < 2r\), we can fit a bigger sphere inside of a smaller one. Once we cross the \(2r\) border, even if we pick a center \(B\) on the border of the \(D_r(0)\), the longest distance between two points inside of \(D_r(0)\) can not surpass \(2r\). Thus, all points will be contained inside of \(D_R(B)\).
</p>
</div>
</div>
<div id="outline-container-org6df3275" class="outline-2">
<h2 id="org6df3275">Second example</h2>
<div class="outline-text-2" id="text-org6df3275">
<p>
We can also pick up a less traditional kind of space, for example, a graph. Let us label its edges with numbers and take the shortest path as a metric.
</p>



<div id="orge2f081c" class="figure">
<p><img src="../img/graph.png" alt="graph.png" />
</p>
</div>

<p>
It is easy to see that \(D_{1.5}(D) \subset D_{1}(C)\), for:
</p>


<p>
\[D_{1.5}(D) = \{ D, A, C\}\]
</p>

<p>
\[D_{1}(C) = \{C, D, A, E\}\]
</p>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>Memory in CompCert: overview</title>
      <link>https://rubber-duck-typing.com/posts/2018-01-17-memory-in-compcert.html</link>
      <guid>https://rubber-duck-typing.com/posts/2018-01-17-memory-in-compcert.html</guid>
      <pubDate>Wed, 17 Jan 2018 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
CompCert is a certified C compiler written in Coq. I work with it to create a verified refactorer, that is proven correct w.r.t. operational semantics of C. 
During my journey, I am tackling various parts of CompCert, including its memory model.
It might seem interesting, how they dealt with raw memory and handled things like encoding values.
</p>

<p>
There were several versions of memory specification. This note is describing the second version, as of CompCert 2.6. Some minor details are omitted for brevity (like alignment checks); I will probably make more posts about memory and connected subjects.
But first we have to take a look at Radix trees, a data structure pervasive in CompCert.
</p>
<div id="outline-container-orgd5e8449" class="outline-2">
<h2 id="orgd5e8449">Radix (Patricia) trees in CompCert</h2>
<div class="outline-text-2" id="text-orgd5e8449">
<p>
Radix tree is a data structure to implement partial mapping from integers. It is extensively used to implement all sorts of maps, such as:
</p>

<ul class="org-ul">
<li>Memory: maps block IDs into block contents</li>
<li>Memory block: maps offsets into block elements.</li>
<li>Symbol table: maps global symbol IDs into memory blocks.</li>
<li>Environment: maps local symbol IDs into pairs of block ID and symbol type.</li>
</ul>

<p>
The type is defined in CompCert in <code>lib.Maps.PTree</code>:
</p>

<div class="org-src-container">
<pre class="src src-nil">Inductive tree (A : Type) : Type :=
   | Leaf : tree A 
   | Node : tree A -&gt; option A -&gt; tree A -&gt; tree A

</pre>
</div>

<p>
A node with no children (a leaf in terms of trees) is encoded as <code>Node Leaf val Leaf</code>. Nodes can store values or be empty. The latter is useful because in Radix trees the paths themselves matter. 
</p>
</div>
</div>
<div id="outline-container-org99ae9b7" class="outline-2">
<h2 id="org99ae9b7">Positive integers in Coq</h2>
<div class="outline-text-2" id="text-org99ae9b7">
<p>
Positive integers are represented through their binary encoding:
</p>
<div class="org-src-container">
<pre class="src src-nil">Inductive positive : Set :=
      | xI : positive -&gt; positive 
      | xO : positive -&gt; positive  
      | xH : positive

</pre>
</div>

<p>
Since positive integers always start with leading one, we are using <code>xH</code> to
encode it. Applying <code>xO</code> appends zero, applying <code>xI</code> appends one. The result
looks like the binary representation of a number written from right to left.
</p>

<p>
For example, an integer number 11 is represented as <code>1011</code> in binary form. The
corresponding term in Coq will be:
</p>

<div class="org-src-container">
<pre class="src src-nil">xI (xI (xO xH) )
</pre>
</div>
</div>
<div id="outline-container-org9888117" class="outline-3">
<h3 id="org9888117">Paths are positive integers</h3>
<div class="outline-text-3" id="text-org9888117">
<p>
There is an isomorphism between positive integers and paths in PTrees. 
</p>

<p>
All paths in PTree start at root, just like positive integers start as <code>xH</code>. Then
they either go to the left or to the right, which corresponds to the choice
between applying <code>xO</code> or <code>xI</code>.
</p>
</div>
</div>
</div>
<div id="outline-container-orgba55b36" class="outline-2">
<h2 id="orgba55b36">Encoding maps using Radix trees</h2>
<div class="outline-text-2" id="text-orgba55b36">
<p>
Radix trees are used to encode partial maps from positive integers into some
domain. The domain values are stored into nodes; the path to the node
corresponds to its index. 
</p>

<p>
Let us encode a map:
</p>

<p>
\[\begin{cases}%
1 \mapsto \text{"one"}\\
3 \mapsto \text{"three"}\\
4 \mapsto \text{"four"}
\end{cases}\]
</p>

<p>
As a tree, it will look like this:
</p>


<div id="org216c296" class="figure">
<p><img src="../img/ptree.png" alt="ptree.png" />
</p>
</div>


<p>
As we see, the elements are enumerated according to the breadth-first search order. 
</p>
</div>
</div>
<div id="outline-container-org3992fa1" class="outline-2">
<h2 id="org3992fa1">Other tree-related types</h2>
<div class="outline-text-2" id="text-org3992fa1">
<ul class="org-ul">
<li><code>PTree</code> is a type of a Radix/Patricia tree; <code>PTree.get</code> returns <code>None</code> if no element has a given index.</li>
<li><code>PMap</code>  is a pair of <code>PTree</code> and a default value returned by <code>PMap.get</code> instead of <code>None</code></li>
<li><code>ZMap</code> is <code>PMap</code> where any integer can be used instead of only positive ones. It is done through a bijection between all integers and positive integers.</li>
</ul>
</div>
</div>
<div id="outline-container-orgb3058ef" class="outline-2">
<h2 id="orgb3058ef">Memory: basic notions</h2>
<div class="outline-text-2" id="text-orgb3058ef">
<p>
Memory is a mapping of <i>addresses</i> into <i>block contents</i> coupled with <i>permissions</i> map and some constraints.
</p>

<p>
*Block contents/ maps <i>offsets</i> (integers) into <i>memory values</i>.
</p>

<p>
All pointers are pairs of block index and an offset inside a block, so it is
impossible to jump from one block into another by changing the offset value. In other words, blocks can not overlap by design.
</p>
</div>
</div>
<div id="outline-container-org7d3fbd0" class="outline-2">
<h2 id="org7d3fbd0">Value</h2>
<div class="outline-text-2" id="text-org7d3fbd0">
<p>
Values are encoded as follows:
</p>

<div class="org-src-container">
<pre class="src src-nil">Inductive val : Type :=
    Vundef  : val
  | Vint    : Int.int        -&gt; val
  | Vlong   : Int64.int      -&gt; val
  | Vfloat  : Floats.float   -&gt; val
  | Vsingle : Floats.float32 -&gt; val
  | Vptr    : block          -&gt; Int.int -&gt; val
</pre>
</div>
</div>
</div>
<div id="outline-container-org4bf061b" class="outline-2">
<h2 id="org4bf061b">Memory value</h2>
<div class="outline-text-2" id="text-org4bf061b">
<p>
<i>Memory value</i> is defined as follows:
</p>

<div class="org-src-container">
<pre class="src src-nil">Inductive memval : Type :=
    Undef    : memval
  | Byte     : int   -&gt; memval
  | Fragment : val   -&gt; quantity -&gt; nat -&gt; memval
</pre>
</div>

<p>
Such memory values are assigned to addresses, which means that a block of \(n\)
bytes holds \(n\) such values.
</p>

<ul class="org-ul">
<li><code>Undef</code> is used to mark the cell as uninitialized. All reads involving such</li>
</ul>
<p>
cells are resulted in <code>Vundef</code> value returned.
</p>
<ul class="org-ul">
<li><code>Byte</code> is a concrete 8-bit integer. It is also a type of raw memory. Such raw</li>
</ul>
<p>
values can be taken in a pack and decoded in an architecture-dependent way using <code>decode_val</code>.
</p>
<ul class="org-ul">
<li><code>Fragment</code> is a usual case of storing data. It contains an opaque value, a
quantity and an index, showing where exactly are we in this value.</li>
</ul>

<p>
Additional arguments include:
</p>

<ul class="org-ul">
<li>A quantity is either <code>Q32</code> or <code>Q64</code>. 8-byte values have quantity set to <code>Q64</code>, the other ones are <code>Q32</code>.</li>
<li>An offset in ranges 0..3 or 0..7. As each element of a block represents a single byte, we are storing as many consecutive <code>Fragments</code> as there are bytes in a value. Each <code>Fragment</code> stores its index w.r.t. the value's beginning address.</li>
</ul>

<p>
<b>Note</b> The source [1] states that only pointers are stored inside fragments, while other values are split into bytes according to the architecture specification. The lemmas however never impose such restriction, making cases like <code>Fragment (Vint 4%Z) _ _</code> possible by construction (and appear in proofs).
</p>

<p>
For example, executing this assignment:
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-type">int32_t</span> <span class="org-variable-name">x</span>;
<span class="org-type">int32_t</span>* <span class="org-variable-name">px</span>;
... 

px = &amp;x

</pre>
</div>

<p>
results in the following values being written into memory (assuming <code>x</code> inhabits in the block #4 and a pointer is 32 bits wide).
</p>

<div class="org-src-container">
<pre class="src src-nil">(Fragment ((Vptr 4 0) Q32 0) ::
(Fragment ((Vptr 4 0) Q32 1) ::
(Fragment ((Vptr 4 0) Q32 2) ::
(Fragment ((Vptr 4 0) Q32 3) :: nil
</pre>
</div>
</div>
<div id="outline-container-org9ba38ae" class="outline-3">
<h3 id="org9ba38ae">Permissions</h3>
<div class="outline-text-3" id="text-org9ba38ae">
<p>
Every address has two associated <i>permissions</i> (access rights) . They put constraints on which operations with are allowed on it. 
</p>

<p>
Permissions are shown in the table below:
</p>



<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Permission</th>
<th scope="col" class="org-left">Read</th>
<th scope="col" class="org-left">Write</th>
<th scope="col" class="org-left">Free</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Freeable</td>
<td class="org-left">+</td>
<td class="org-left">+</td>
<td class="org-left">+</td>
</tr>

<tr>
<td class="org-left">Writable</td>
<td class="org-left">+</td>
<td class="org-left">+</td>
<td class="org-left">&#xa0;</td>
</tr>

<tr>
<td class="org-left">Readable</td>
<td class="org-left">+</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">&#xa0;</td>
</tr>

<tr>
<td class="org-left">Nonempty</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">&#xa0;</td>
</tr>
</tbody>
</table>



<p>
Non-allocated and freed cells can have <code>None</code> as permissions. 
</p>

<p>
The first permission value associated with a memory byte is its <i>maximal permission</i>: it is set on allocation and can be lowered during execution using <code>drop_perm</code> operation.
The <i>current permission</i> is varying between <code>Nonempty</code> and maximal permission.
</p>
</div>
</div>
<div id="outline-container-org97ac45b" class="outline-3">
<h3 id="org97ac45b">Operations on memory</h3>
<div class="outline-text-3" id="text-org97ac45b">
<p>
Memory itself is not opaque and we can have easily access to the inner tree
structure of its contents. However, all memory-related lemmas defined in
CompCert rely on specifically crafted memory operations. These are opaque and
can not be unfolded into their exact definitions. For us it means that we can
only prove our own theorems based on such properties of these transformations,
that are already proven in CompCert.
</p>

<div class="org-src-container">
<pre class="src src-nil">alloc       : mem -&gt; Z -&gt; Z -&gt; mem * block
free        : mem -&gt; block -&gt; Z -&gt; Z -&gt; option mem

load        : memory_chunk -&gt; mem -&gt; block -&gt; Z -&gt; option val
store       : memory_chunk -&gt; mem -&gt; block -&gt; Z -&gt; val -&gt; option mem 

loadbytes   : mem -&gt; block -&gt; Z -&gt; Z -&gt; option (list memval)
storebytes  : mem -&gt; block -&gt; Z -&gt; list memval -&gt; option mem

drop_perm   : mem -&gt; block -&gt; Z -&gt; Z -&gt; permission -&gt; option mem

</pre>
</div>


<p>
<b>Note</b> For now, we are only going to study <code>load</code>, <code>store</code>, <code>loadbytes</code> and <code>storebytes</code> operations.
</p>

<ul class="org-ul">
<li><code>load</code> accepts a chunk type (<code>Mint32</code>,=Mint8signed= etc.), source memory, block ID and offset. It returns a decoded value or <code>None</code>.</li>
<li><code>store</code> accepts a chunk type (<code>Mint32</code>,=Mint8signed= etc.), source memory, block ID and offset, and a value. It returns an instance of memory with overwritten cells or <code>None</code>.</li>
<li><code>loadbytes</code> accepts source memory, block ID and offset, and the amount of bytes to load. It returns a list of memory values or <code>None</code></li>
<li><code>storebytes</code> accepts source memory, block ID and offset and a lit of memory values. It returns an instance of memory with overwritten cells or <code>None</code>.</li>
</ul>

<p>
There are lemmas that allow reasoning about <code>load</code> results involving:
</p>

<ul class="org-ul">
<li>Previous <code>store</code> result</li>
<li><code>loadbytes</code></li>
<li><code>extends</code> and <code>injection</code></li>
<li><code>unchanged_on</code></li>
<li><code>decode_val</code> with a direct memory access.</li>
</ul>

<p>
The following lemma allows us to get raw contents from memory and decode them using <code>decode_val</code>
</p>

<div class="org-src-container">
<pre class="src src-nil">load_result:
  forall (chunk : memory_chunk) (m : mem) (b : block) (ofs : Z) (v : val),
  load chunk m b ofs = Some v -&gt;
  v = decode_val chunk
    (getN (size_chunk_nat chunk) ofs (PMap.get b (mem_contents m)))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org1215ed8" class="outline-2">
<h2 id="org1215ed8">Memory: implementation</h2>
<div class="outline-text-2" id="text-org1215ed8">
<p>
Memory is represented as a record:
</p>


<div class="org-src-container">
<pre class="src src-nil">Record mem' : Type := mkmem
  { mem_contents : PMap.t (ZMap.t Memdata.memval);
    mem_access   : PMap.t (Z -&gt; perm_kind -&gt; option permission);
    nextblock    : Values.block;
    access_max   : forall (b : positive) (ofs : Z),
                   perm_order'' (PMap.get b mem_access ofs Max)
                   (PMap.get b mem_access ofs Memtype.Cur);
    nextblock_noaccess : forall (b : positive) (ofs : Z) (k : perm_kind),
                         ~ Plt b nextblock -&gt; PMap.get b mem_access ofs k = None;
    contents_default : forall b : positive,
                       fst (PMap.get b mem_contents) = Undef }

</pre>
</div>

<p>
All mappings are implemented as Patricia Trees.
</p>

<ul class="org-ul">
<li><code>mem_contents</code> maps block IDs (positive integers) into block contents. It is a map implemented on top of a Radix tree. Block contents are maps from offsets (integers, possibly negative) into block elements.</li>
<li><code>mem_access</code> maps block IDs into functions, which accept an offset, a permission type (<code>Max</code> or <code>Cur</code>) and return the actual permissions for this block.</li>
<li><code>nextblock</code> is the maximal block ID. All blocks with ids in range from 1 inclusive to <code>nextblock</code> exclusive should exist.</li>
<li><code>access_max</code> encodes the following property: for all blocks their maximal permissions are higher than their current permissions.</li>
<li><code>nextblock_noaccess</code> encodes the following property: no block has an ID greater or equal to <code>nextblock</code></li>
<li><code>contents_default</code> encodes the following property: for all blocks the default memory cell value is <code>Undef</code>.</li>
</ul>
</div>
</div>
<div id="outline-container-org83bbcac" class="outline-2">
<h2 id="org83bbcac">Useful sources</h2>
<div class="outline-text-2" id="text-org83bbcac">
<ol class="org-ol">
<li>"Program Logic for Certified Compilers". Chapter 32 "The CompCert memory model"</li>
</ol>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>A beautiful intuition on associativity</title>
      <link>https://rubber-duck-typing.com/posts/2018-01-13-associativity-intuition.html</link>
      <guid>https://rubber-duck-typing.com/posts/2018-01-13-associativity-intuition.html</guid>
      <pubDate>Sat, 13 Jan 2018 00:00:00 +0100</pubDate>
      <description><![CDATA[<p>
I found a beautiful explanation about what essential property does the associativity
capture: You can think of each element of a monoid as having two sides. The idea
is that the <i>left side and right side are independent things that don't interfere
with each other</i>.
</p>

<p>
For example, adding an element at the beginning of a list is independent from 
adding something at the end of a list. These actions do not affect each other, and it doesn't
matter which you do first. That's the idea that associativity captures.
</p>

<p>
<a href="https://mail.haskell.org/pipermail/haskell-cafe/2009-January/053798.html">source</a>
</p>
]]></description>
    </item>
    <item>
      <title>On teaching programmers and mathematicians</title>
      <link>https://rubber-duck-typing.com/posts/2017-10-27-on-teaching.html</link>
      <guid>https://rubber-duck-typing.com/posts/2017-10-27-on-teaching.html</guid>
      <pubDate>Fri, 27 Oct 2017 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
I have been lucky to be exposed to some very good teachers with different 
approaches as well as collect my own experience: I am teaching C/Assembly 
since around 2009. I've also taught things like Lambda-calculus and functional
programming, mathematics and playing piano. This post is intended as a summary
of how I see an ideal education in virtually any domain. I will speak about
teaching mathematics and or programming; the principles however are sufficiently
abstract to be applied anywhere.
</p>

<p>
Based on how our memory and perception work, the following points seem to be
generally true:
</p>

<ol class="org-ol">
<li>We remember better when the brain connects information to an emotional event.</li>
<li>We need to connect new knowledge to the knowledge we already have.</li>
<li>We need to repeat new things several times over a short period of time in order to forget them on a much slower pace. See <a href="https://en.wikipedia.org/wiki/Forgetting_curve">Forgetting curve (Wikipedia)</a>.</li>
<li>Different people base on different types of perception: sound, visual, touch.
That's what they remember most easily; it does not mean that even if information is not representable in audible format, it should be presented like this to those who tend towards sound perception.</li>
<li>Some people like the top-down approach (deductive thinking), while others prefer generalize examples (inductive thinking).</li>
</ol>

<p>
Based on them, I think that the ideal way to teach should incorporate the following points:
</p>

<ul class="org-ul">
<li>We need charismatic teachers with great personalities who can inspire emotions. An average teacher in person is worse than a world class teacher on tape. By no means do I mean sweet talking “popular science” guys, they are often superficial and empty.</li>
<li>Every class should draw examples from the previous ones and <b>force students to decide, how to apply the knowledge from the previous courses in the course they are studying</b> (very important!)</li>
<li>A lot of practice, and all exercises should not only be written on paper, but explained by students to the teacher or his assistant to enforce better understanding, enable other types of memorisation and expose the flaws in reasoning.</li>
<li>Use a lot of illustrative material. Slides are mostly useless, but infographics and videos are VERY useful when done correctly. Check, for example, this YouTube channel:</li>
</ul>

<p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/Ip3X9LOh2dk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</p>

<p>
An animation, or, even better, an interactive playground are perfect to provide an intuition about a mathematical notion. This is because we need to connect mathematics to the sensory experiences.
</p>

<ul class="org-ul">
<li>Very few people can memorize a lot of abstract stuff and then deduce everything from it when asked to apply it in practice. Hence we need to provide examples. In my opinion, some nice explanation patterns are:
<ul class="org-ul">
<li>Deductive and inductive: start with definitions, show examples, explain how exactly each example follows from definition</li>
<li>Inductive and deductive: start informal, give intuition, provide examples, then give precise formal definition, show how exactly the given examples are generalized to these definitions. Then give more examples, but derive them from formal definitions.</li>
</ul></li>
</ul>



<ul class="org-ul">
<li>Your students should know, which topics will be covered during the next session. Ideally, they should start studying them on their own. Then when they come they will be more prepared to listen to you and their minds will produce more useful questions. you more prepared.</li>
</ul>
]]></description>
    </item>
    <item>
      <title>Impressions of René Magritte</title>
      <link>https://rubber-duck-typing.com/posts/2017-07-18-rene-magritte.html</link>
      <guid>https://rubber-duck-typing.com/posts/2017-07-18-rene-magritte.html</guid>
      <pubDate>Tue, 18 Jul 2017 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
Recently I've been lucky to spend a couple of days in Belgium. I've come to
Bruxelles, rushed through Brugge and Gant and ended my journey
by visiting René Magritte museum. I was quite impressed, partly because I have
not seen much of Magritte before. This post is intended as a quick review of
my impressions and thoughts on Magritte style and his philosophy.
</p>
<div id="outline-container-org3e3324a" class="outline-2">
<h2 id="org3e3324a">First impressions</h2>
<div class="outline-text-2" id="text-org3e3324a">
<p>
Magritte's style differs from his fellow surrealists such as Dali or Ernst.
I've often got an impression that his approach is quite blunt, a bit like if he
threw objects into my face:
</p>

<ul class="org-ul">
<li>The objects on his pictures are big, take a lot of space on canvas.
Moreover, the background is empty and not detailed.</li>
<li>The palette has usually few colors.
Speaking of modern times, it is the same kind of restriction that exists in
pixel art, and produces a similar effect on me, that I rather like.</li>
<li>The colors are rarely bright, which, in conjunction with the previous point, makes paintings look surreal, mystic and alien.</li>
<li>The shadows and shapes are nicely detailed which makes for a highly contrast image (although you won't see many sharp edges).</li>
</ul>
</div>
</div>
<div id="outline-container-orgd3bedde" class="outline-2">
<h2 id="orgd3bedde">The Riddler</h2>
<div class="outline-text-2" id="text-orgd3bedde">
<p>
Magritte's approach is also quite intellectual because he wants to mess with the
viewers and make them think (almost a quote of his).
Each pair of a picture and its name is a puzzle to solve.
The reward is a better comprehension the message of a picture, which in turn can
bring you some new understanding about the real world.
The name and the picture can be connected in a number of ways, for example:
</p>

<ul class="org-ul">
<li><p>
The name can be a hint to solve the riddle.
The picture named "Explanation" depicts two bottles, one of which looks like
half-bottle, half-carrot.
As we see the image and its title, we come to an understanding of what an
<i>explanation</i> is, its idea: the explanation is a process of changing our
perception and/or understanding of concepts.
This change is the transformation of an image of a bottle into an image of a carrot.
</p>


<div id="org15c7e53" class="figure">
<p><img src="../img/the-explanation-1952.jpg" alt="the-explanation-1952.jpg" />
</p>
</div></li>
</ul>


<ul class="org-ul">
<li><p>
The name can be just something vaguely connected to what's on the painting.
For example, the name "Imp of the Perverse" should make you think
that something wrong is going on, make you uncomfortable, which, in turn,
strengthens the feeling of wrongness coming from the piece of art itself.
In a sense, Magritte wants you to think critically, not make you trying to connect
distantly related terms.
We can find a context to associate any pair of words if we want to, though not
all contexts are of importance to us.
</p>


<div id="org2c6824c" class="figure">
<p><img src="../img/imp-of-the-perverse-1927.jpg" alt="imp-of-the-perverse-1927.jpg" />
</p>
</div></li>

<li><p>
The name might hold an allusion to other works of art Magritte associated his
painting with.
For example, "The Man from the Sea" refers to the film of
Marcel L'Herbier, which tells a story of a lone sailor.
More importantly, it resembles the final scene of "Juve against Fantômas" where this villain in
black prevails by setting off an explosion with a pull of a lever.
</p>


<div id="orga2d69de" class="figure">
<p><img src="../img/the-man-of-the-sea-1927.jpg" alt="the-man-of-the-sea-1927.jpg" />
</p>
</div></li>
</ul>
</div>
</div>
<div id="outline-container-org82b04e8" class="outline-2">
<h2 id="org82b04e8">Objects, their images and words</h2>
<div class="outline-text-2" id="text-org82b04e8">
<p>
What's especially interesting about Magritte's philosophy is his reflections on
images, words and real world objects as well as the relations of the said three
worlds.
He published his famous text "Words and images" in 1929 in the journal La révolution surréaliste as a result to his extensive
experimentation in the late 1920's.
</p>


<div id="orgf5c8dac" class="figure">
<p><img src="../img/words-and-images-1929.gif" alt="words-and-images-1929.gif" />
</p>
</div>

<p>
This text studies how words (e.g. poetry) and paintings as the means of
expressing ideas.
Here is the text in English with a little commentary (it corresponds to the images from up to down, from left to right):
</p>

<ul class="org-ul">
<li>An object and its name are not inseparable. We can find a better name.</li>
<li><p>
Some objects have no name.
</p>

<p>
Words are parts of languages, and there is no a language which includes all
notions from any other one.
Take Dostoevsky's "nadryv" or dozens of words to
describe different shades of snow in the local languages of northern tribes.
We create names based on our everyday needs and what seems important to us.
</p></li>

<li><p>
A word can be used to refer you to itself.
</p>

<p>
This thought is illustrated by a word "sky" inside a closed curve, depicting
the said sky. As the label "sky" is placed directly on the image of sky, it
refers to this exact instance of "sky", which makes for a funny wordplay.
</p></li>

<li><p>
Sometimes a name of an object meets the object depiction.
</p>

<p>
This is illustrated with a labeled image of a forest.
</p></li>

<li><p>
Sometimes, a name is used in place of an image.
</p>

<p>
The picture shows a stub silhouette of an object labeled with its name.
</p></li>

<li>In reality, a word can be used instead of an object.</li>

<li><p>
A word can be substituted by an image.
</p>

<p>
A pictogram of a sun is used to substitute the word "soleil" (fr. sun).
</p></li>

<li>An object can suggest that there are other objects behind.</li>

<li>Everything suggests that there is little common between an object and its representation.</li>

<li>The words used to represent two objects do not suggest what is different between them.</li>

<li>On a painting, words are like images.</li>

<li><p>
The images and words are seen in an unusual way when on a canvas.
</p>

<p>
The illustration shows how a written word "montagne" (fr. mountain) blends
in and becomes a part of the object texture.
</p></li>

<li><p>
An arbitrary figure can replace an image of an object.
</p>

<p>
I am not sure what this means, because the words (labels) are not mentioned.
However the  illustration has all these random objects labeled as the sun.
When the object is labeled (possibly with an unexpected word), that's one
thing, but when we deduce that the object is replacing sun just because he is
in the context (position, effects etc.) we are used to see the sun in,
that's entirely other thing.
</p></li>

<li><p>
The object is never the same thing as its image and/or its name.
</p>

<p>
I can not stress this enough, as this is quite a useful piece of the puzzle.
I will dedicate the next section to this.
</p></li>
</ul>


<ul class="org-ul">
<li><p>
Visible contours of the object form a mosaic in reality.
</p>

<p>
This is a beautiful thing to notice. I can deduce, that every point of the space can be
named based on it being a part of some part of this mosaic.
</p></li>

<li>The vague shapes have the same significance as the well defined ones.</li>

<li>Sometimes the labels define precise things, and the images are not.</li>

<li>Sometimes, it is vice versa.</li>
</ul>
</div>
</div>
<div id="outline-container-orgf1980c6" class="outline-2">
<h2 id="orgf1980c6">Images, words and the real world</h2>
<div class="outline-text-2" id="text-orgf1980c6">
<p>
In this section I want to explore the following thought of Magritte:
The object is never the same thing as its image and/or its name.
</p>
</div>
<div id="outline-container-orgc875b74" class="outline-3">
<h3 id="orgc875b74">The real</h3>
<div class="outline-text-3" id="text-orgc875b74">
<p>
We are not reasoning about the real world, but about its projection in our
head, a kind of a logic system. This is largely used in philosophy, for
example, to construct some theories of causation.
</p>

<p>
We are fundamentally limited like that. It is evident, that our image is
only a part of the big picture. So when we reason about real life, we base on
the information that could have been warped only once: by our perception.
I think, this is also why methods as palpation still hold such an important
place in medicine: there are as little between the doctor and the
patient as possible.
</p>
</div>
</div>
<div id="outline-container-orgf0bbc6d" class="outline-3">
<h3 id="orgf0bbc6d">The image</h3>
<div class="outline-text-3" id="text-orgf0bbc6d">
<p>
If we use an image of reality, someone has to create it first &#x2013; we will
call him Creator, and we are his Observers. Where can we
get disrupted?
</p>

<ul class="org-ul">
<li>Creator's perception of reality is not perfect: for example,
he can have a mild color blindness; he is also unable to perceive all the
infinite details that do exist.</li>
<li>Creator's way of drawing may be flawed.</li>
<li>Observer's perception of an image can be flawed.</li>
<li>Observer's interpretation is flawed.</li>
</ul>
</div>
</div>
<div id="outline-container-org2f9be3b" class="outline-3">
<h3 id="org2f9be3b">The word</h3>
<div class="outline-text-3" id="text-org2f9be3b">
<p>
Words are purer than images in a sense that they represent crystallized
ideas. However, the exact meaning of a word can slightly vary because of
the differences in cultural background of different people. The existence of
dictionaries, however, helps finding a certain common denominator between
them, which would be accepted as a norm by most people. We will call the
agents Speaker and Listener.
</p>

<ul class="org-ul">
<li>Speaker can mistake an object for another.</li>
<li>Speaker can make a bad choice of word.</li>
</ul>

<p>
The word may seem more precise, but it can be ambiguous (multiple meanings in
dictionary). Whether it will hold more or less information than an image is
debatable and varies from case to case.
</p>


<p>
It is important to note, that we are only speaking about singular words. They
can be ambiguous, but interpreting a word differs from interpreting sentences,
which differs from interpretation of texts, which differs from interpretation
of texts in specific contexts.
</p>

<p>
In fact, Magritte's study is about semantics and human interpretation,
concentrating especially on human perception of images and words on them.
One might say that Magritte was one of pioneers of semiotics in scope of art.
</p>
</div>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>The virtues of using goto</title>
      <link>https://rubber-duck-typing.com/posts/2017-04-26-goto-the-marvelous.html</link>
      <guid>https://rubber-duck-typing.com/posts/2017-04-26-goto-the-marvelous.html</guid>
      <pubDate>Wed, 26 Apr 2017 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
IT folks are prone to prejudices, as we all are.
Once a beginner programmer
starts exploring the world of coding, he quickly learns catchy memes from the
more experienced part of the community. One can then easily live by them
without putting much thought in their meaning.
</p>

<p>
In general, this is also how human society works: we adopt elements
of our parents' lifestyle and their world view, we make them parts of ourselves,
usually not questioning the reasons for this specific behavior. However, there
is one fundamental empirical rule: there are no silver bullets &#x2013; rules,
that are always applicable.
</p>

<p>
Thus, each of programming memes requires a certain context to be fully
understood. My opinion is that we should always try to reach these deep
foundations in order to understand when the rule can be thrown away for good.
This post is about an absurd meme "<code>goto</code> is bad". We want to explore what
exactly prevents <code>goto</code> from being used well in some contexts and when its use
is quite reasonable.
</p>
<div id="outline-container-org4bdca59" class="outline-2">
<h2 id="org4bdca59">Why goto is stigmatized in the structured programming</h2>
<div class="outline-text-2" id="text-org4bdca59">
<p>
Structured programming is a programming style that (as each style) implies a
set of rules and constraints to live by. This style implies an extensive usage
of subroutines, loops and code blocks (statement sequences between braces). It
usually goes in par with imperativeness, when each programming statement is
executed sequentially, and mutable addressable memory &#x2013; roughly following the
von Neumann model of computations.  It is safe to say that one of its most
devoted supporters was <a href="https://en.wikipedia.org/wiki/Edsger_W._Dijkstra">Edsger W. Dijkstra</a>.
Object-oriented programming can often be viewed as a slightly tweaked version
of it.  A large slice of modern programming is structured imperative
programming or its variations; moreover, it is what kids are taught in school.
Because of that educational system flaw, most of us are deeply infected with an
imperative thinking and are often emulating other paradigms on top of it in our
heads.
</p>

<p>
Dijkstra considered <code>goto</code> harmful because it makes harder to follow the code
logic. This is usually true, but needs a clarification. What makes <code>goto</code>
harmful is that it is usually paired with <i>assignments</i>.
</p>

<p>
Assignments are changing the abstract machine state. When reasoning about a
typical program, we are usually tracing its execution and mark how the values
of the variables are changing. Throwing <code>goto</code> ’s everywhere makes it notably
harder to follow the program state, because you can jump anywhere, from
anywhere. That makes the trace much harder to untangle.
</p>

<p>
However, throw away the state changes and you will have no problems using
multiple goto’s in an isolated piece of code (e.g. inside a particular
function), because the interesting state will be determined solely by your
current position in code!
</p>
</div>
</div>
<div id="outline-container-orge83b993" class="outline-2">
<h2 id="orge83b993">Finite State Machines</h2>
<div class="outline-text-2" id="text-orge83b993">
<p>
If we want to implement a <a href="https://en.wikipedia.org/wiki/Finite-state_machine">Finite State Machine (FSM)</a>, then <code>goto</code> ’s are
the way to go! Such an abstract machine consists of:
</p>

<ul class="org-ul">
<li>a set of states (C labels). One state is marked as an <i>initial state</i>.</li>
<li>input (sequence of global events, f.e., character input, received network packets, any user actions)</li>
<li>output (sequence of global actions, <i>responses</i> of the system: send packets or control signals to the connected hardware, output etc.)</li>
<li>for each state, a set of rules to jump to other states based on the current input.</li>
</ul>

<p>
We start in the initial state and perform jumps between states based on the
current input value. As you see, this machine has no memory. If we are
implementing them in a language such as C, its state will be characterized
solely by the position in the code we are currently at.
</p>

<p>
Crafting a FSM is equivalent to crafting an algorithm to solve a problem. They
are not expressive enough to solve all problems Turing-machines can digest.
Nevertheless they are not only potent, but very convenient for some tasks such
as template matching in strings, implementing network protocols and robot controlling tasks.
</p>

<p>
Here is a toy example, taken from [my book](<a href="http://www.apress.com/us/book/9781484224021">http://www.apress.com/us/book/9781484224021</a>). This FSM checks whether
the input string containing only characters 0 and 1 contains an even number of
ones. It is common to draw cool looking diagrams for FSM, showing states as
circles and transitions as arrows between them.
</p>


<div id="orga404c6d" class="figure">
<p><img src="../img/fsm-parity-example.png" alt="fsm-parity-example.png" />
</p>
</div>

<p>
Let us take a look at its implementation in C. I have omitted error checks for
brevity.
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-preprocessor">  #include</span> <span class="org-string"><span class="org-rainbow-delimiters-depth-1">&lt;</span></span><span class="org-string">stddef.h</span><span class="org-string"><span class="org-rainbow-delimiters-depth-1">&gt;</span></span>
<span class="org-preprocessor">  #include</span> <span class="org-string"><span class="org-rainbow-delimiters-depth-1">&lt;</span></span><span class="org-string">stdio.h</span><span class="org-string"><span class="org-rainbow-delimiters-depth-1">&gt;</span></span>

  <span class="org-comment-delimiter">/* </span><span class="org-comment">str should only contain 0 and 1</span><span class="org-comment-delimiter"> */</span>
  <span class="org-type">int</span> <span class="org-function-name">even_ones</span><span class="org-rainbow-delimiters-depth-1">(</span> <span class="org-type">char</span> <span class="org-keyword">const</span>* <span class="org-variable-name">str</span> <span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    <span class="org-type">size_t</span> <span class="org-variable-name">i</span> = <span class="org-highlight-numbers-number">0</span>;
    <span class="org-type">char</span> <span class="org-variable-name">input</span>;
   <span class="org-constant">_even</span>:
    input = str<span class="org-rainbow-delimiters-depth-2">[</span>i++<span class="org-rainbow-delimiters-depth-2">]</span>;
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span>input == <span class="org-string">'1'</span><span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">goto</span> <span class="org-constant">_odd</span>;
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span>input == <span class="org-string">'0'</span><span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">goto</span> <span class="org-constant">_even</span>;
    <span class="org-comment-delimiter">/* </span><span class="org-comment">end of string -- null terminator</span><span class="org-comment-delimiter"> */</span>
    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">1</span>;
   <span class="org-constant">_odd</span>:
    input = str<span class="org-rainbow-delimiters-depth-2">[</span>i++<span class="org-rainbow-delimiters-depth-2">]</span>;
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span>input == <span class="org-string">'1'</span><span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">goto</span> <span class="org-constant">_even</span>;
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span>input == <span class="org-string">'0'</span><span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">goto</span> <span class="org-constant">_odd</span>;
    <span class="org-comment-delimiter">/* </span><span class="org-comment">end of string -- null terminator</span><span class="org-comment-delimiter"> */</span>
    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>

  <span class="org-type">void</span> <span class="org-function-name">test</span><span class="org-rainbow-delimiters-depth-1">(</span> <span class="org-keyword">const</span> <span class="org-type">char</span>* <span class="org-variable-name">str</span> <span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    printf<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">"%s\n"</span>, even_ones<span class="org-rainbow-delimiters-depth-3">(</span> str <span class="org-rainbow-delimiters-depth-3">)</span> ? <span class="org-string">"yes"</span> : <span class="org-string">"no"</span> <span class="org-rainbow-delimiters-depth-2">)</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>

  <span class="org-type">int</span> <span class="org-function-name">main</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-type">void</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    test<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">"0101011"</span><span class="org-rainbow-delimiters-depth-2">)</span>;
    test<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">""</span><span class="org-rainbow-delimiters-depth-2">)</span>;
    test<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">"010"</span><span class="org-rainbow-delimiters-depth-2">)</span>;
    test<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">"110"</span><span class="org-rainbow-delimiters-depth-2">)</span>;

    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgaaa5495" class="outline-2">
<h2 id="orgaaa5495">Model checking</h2>
<div class="outline-text-2" id="text-orgaaa5495">
<p>
An additional benefit is that there exist a quite powerful verification
technique called <i>model checking</i> which allows you to reason about the program
properties if the said program is encoded as a finite state machine. You can
reason about them using temporal logic, checking properties such as "If
I got into the state A, I will never reach the state B from there".
The model checkers can often generate C program automatically from a FSM
description.
</p>

<p>
For examples of what model checking tools are capable of, I recommend you this
<a href="http://spinroot.com/spin/Man/Exercises.html">exercise page</a> for SPIN model checker.
</p>
</div>
</div>
<div id="outline-container-orgae14385" class="outline-2">
<h2 id="orgae14385">Deinitializing resources</h2>
<div class="outline-text-2" id="text-orgae14385">
<p>
C++ has a nice feature C lacks. It can automatically call object destructors
whenever the object's lifetime is over. For example, in the following code the
destructor for an object <code>myC</code> will be automatically called after we reach the
closing bracket. But it gets better: every time you are writing a return
statement, everything that exists in the surrent stack frame gets automatically
destroyed in the correct order (reversed initialization order).
Consider this function, which returns the error code and uses three objects:
<code>myA</code>, <code>myB</code> and <code>myC</code>. The respective classes should have defined destructors
which free all associated resources.
</p>


<div class="org-src-container">
<pre class="src src-c"><span class="org-preprocessor">  #include</span> <span class="org-string"><span class="org-rainbow-delimiters-depth-1">&lt;</span></span><span class="org-string">iostream</span><span class="org-string"><span class="org-rainbow-delimiters-depth-1">&gt;</span></span>

  <span class="org-type">int</span> <span class="org-function-name">f</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    <span class="org-type">A</span> <span class="org-variable-name">myA</span>;
    <span class="org-type">B</span> <span class="org-variable-name">myB</span>;
    <span class="org-type">C</span> <span class="org-variable-name">myC</span>;

    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span><span class="org-negation-char">!</span> myA.init<span class="org-rainbow-delimiters-depth-3">()</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span><span class="org-negation-char">!</span> myB.init<span class="org-rainbow-delimiters-depth-3">()</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>; <span class="org-comment-delimiter">// </span><span class="org-comment">myA's destructor is called</span>
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span><span class="org-negation-char">!</span> myC.init<span class="org-rainbow-delimiters-depth-3">()</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>; <span class="org-comment-delimiter">// </span><span class="org-comment">myA and myB's destructors are called</span>

    <span class="org-comment-delimiter">//</span><span class="org-comment">...</span>

    <span class="org-comment-delimiter">// </span><span class="org-comment">Destructors for myA, myB, myC will be called here anyway</span>
    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">1</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
In C we often want to do the same thing, but we do not have that luxury of
automatically calling anything. It is, however, very important do to because
some structures have dynamically allocated fields or are associated with other
resources such as file descriptors. It can easily leak resources. So, to
do things right, we have to produce quite a mess:
</p>

<div class="org-src-container">
<pre class="src src-c">  <span class="org-type">int</span> <span class="org-function-name">f</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    <span class="org-keyword">struct</span> <span class="org-type">sa</span> <span class="org-variable-name">a</span>;
    <span class="org-keyword">struct</span> <span class="org-type">sb</span> <span class="org-variable-name">b</span>;
    <span class="org-keyword">struct</span> <span class="org-type">sc</span> <span class="org-variable-name">c</span>;

    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span> <span class="org-negation-char">!</span> sa_init<span class="org-rainbow-delimiters-depth-3">(</span> &amp;a <span class="org-rainbow-delimiters-depth-3">)</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span> <span class="org-negation-char">!</span> sb_init<span class="org-rainbow-delimiters-depth-3">(</span> &amp;b <span class="org-rainbow-delimiters-depth-3">)</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-rainbow-delimiters-depth-2">{</span> sa_deinit<span class="org-rainbow-delimiters-depth-3">(</span> &amp;a <span class="org-rainbow-delimiters-depth-3">)</span>; <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>; <span class="org-rainbow-delimiters-depth-2">}</span>
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span> <span class="org-negation-char">!</span> sc_init<span class="org-rainbow-delimiters-depth-3">(</span> &amp;c <span class="org-rainbow-delimiters-depth-3">)</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-rainbow-delimiters-depth-2">{</span> sb_deinit<span class="org-rainbow-delimiters-depth-3">(</span> &amp;b <span class="org-rainbow-delimiters-depth-3">)</span>; sa_deinit<span class="org-rainbow-delimiters-depth-3">(</span> &amp;a <span class="org-rainbow-delimiters-depth-3">)</span>; <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>; <span class="org-rainbow-delimiters-depth-2">}</span>


    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">1</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
Imagine you had 5 structures to work with, this straightforward approach is
going to turn your code into nightmare! However, with the help of <code>goto</code>'s we
are going to exploit a nice little trick. It bases on the fact that all such
branches can be ordered by inclusion: each branch looks exactly  like some
other branch preceded by an additional <code>deinit</code>:
</p>

<div class="org-src-container">
<pre class="src src-c">  <span class="org-comment-delimiter">//</span>
  <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
  sa_deinit<span class="org-rainbow-delimiters-depth-1">(</span> &amp;a <span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
  sb_deinit<span class="org-rainbow-delimiters-depth-1">(</span> &amp;b <span class="org-rainbow-delimiters-depth-1">)</span>; sa_deinit<span class="org-rainbow-delimiters-depth-1">(</span> &amp;a <span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
</pre>
</div>

<p>
If we throw labels in between we could jump to any statement in this sequence.
Then all following statements will be executed as well.  This way we are going
to refactor the example above to look like this:
</p>

<div class="org-src-container">
<pre class="src src-c">  <span class="org-comment-delimiter">//</span>
  <span class="org-type">int</span> <span class="org-function-name">f</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    <span class="org-keyword">struct</span> <span class="org-type">sa</span> <span class="org-variable-name">a</span>;
    <span class="org-keyword">struct</span> <span class="org-type">sb</span> <span class="org-variable-name">b</span>;
    <span class="org-keyword">struct</span> <span class="org-type">sc</span> <span class="org-variable-name">c</span>;

    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span> <span class="org-negation-char">!</span> sa_init<span class="org-rainbow-delimiters-depth-3">(</span> &amp;a <span class="org-rainbow-delimiters-depth-3">)</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">goto</span> <span class="org-constant">fail</span>;
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span> <span class="org-negation-char">!</span> sb_init<span class="org-rainbow-delimiters-depth-3">(</span> &amp;b <span class="org-rainbow-delimiters-depth-3">)</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">goto</span> <span class="org-constant">fail_b</span>;
    <span class="org-keyword">if</span> <span class="org-rainbow-delimiters-depth-2">(</span> <span class="org-negation-char">!</span> sc_init<span class="org-rainbow-delimiters-depth-3">(</span> &amp;c <span class="org-rainbow-delimiters-depth-3">)</span> <span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-keyword">goto</span> <span class="org-constant">fail_c</span>;

    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">1</span>;

   <span class="org-constant">fail_c</span>:
    sb_deinit<span class="org-rainbow-delimiters-depth-2">(</span> &amp;b <span class="org-rainbow-delimiters-depth-2">)</span>;
   <span class="org-constant">fail_b</span>:
    sa_deinit<span class="org-rainbow-delimiters-depth-2">(</span> &amp;a <span class="org-rainbow-delimiters-depth-2">)</span>;
   <span class="org-constant">fail</span>:
    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
Isn't it way nicer that what we have seen before? Additionally, no assignments
are performed hence no fuss about <code>goto</code> evilness at all.
</p>
</div>
</div>
<div id="outline-container-orgcdc271b" class="outline-2">
<h2 id="orgcdc271b">Computed goto</h2>
<div class="outline-text-2" id="text-orgcdc271b">
<p>
Computed <code>goto</code>  is a non-standard feature supported by many popular C and C++
compilers. Basically, it allows to store a label into a variable and perform
jumps to it. It differs from calling function by pointer because no return is
ever performed. The simplest case is shown below:
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-preprocessor">  #include</span> <span class="org-string"><span class="org-rainbow-delimiters-depth-1">&lt;</span></span><span class="org-string">stdio.h</span><span class="org-string"><span class="org-rainbow-delimiters-depth-1">&gt;</span></span>

  <span class="org-type">int</span> <span class="org-function-name">main</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    <span class="org-type">void</span>* <span class="org-variable-name">jumpto</span> = &amp;&amp;label;

    <span class="org-keyword">goto</span> *jumpto;
    puts<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">"Did not jump"</span><span class="org-rainbow-delimiters-depth-2">)</span>;
    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;

   <span class="org-constant">label</span>:
    puts<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">"Did jump"</span><span class="org-rainbow-delimiters-depth-2">)</span>;
    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">1</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
We are taking raw label address using an unusual double ampersand syntax and
then perform a <code>goto</code>. Notice the additional asterisk before <code>goto</code>
operand. When launched, this program will output <code>Did jump</code>.
</p>

<p>
Where can we use such a feature? Expressivity wise, it is not very interesting.
However, sometimes we can get a speedup. A case that comes to my mind is a
bytecode interpreter (but I have written hell of a ton of them, so I should be
quite biased towards them). The instruction fetching takes typically no less
than 30% of the execution time, and computed <code>goto</code> allows one to speed it up.
</p>

<p>
Without computed <code>goto</code>:
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-preprocessor">  #include</span> <span class="org-string"><span class="org-rainbow-delimiters-depth-1">&lt;</span></span><span class="org-string">stdio.h</span><span class="org-string"><span class="org-rainbow-delimiters-depth-1">&gt;</span></span>
<span class="org-preprocessor">  #include</span> <span class="org-string"><span class="org-rainbow-delimiters-depth-1">&lt;</span></span><span class="org-string">inttypes.h</span><span class="org-string"><span class="org-rainbow-delimiters-depth-1">&gt;</span></span>

  <span class="org-keyword">enum</span> <span class="org-type">bc</span> <span class="org-rainbow-delimiters-depth-1">{</span> <span class="org-variable-name">BC_PUSH</span>, <span class="org-variable-name">BC_PRINT</span>, <span class="org-variable-name">BC_ADD</span>, <span class="org-variable-name">BC_HALT</span> <span class="org-rainbow-delimiters-depth-1">}</span>;

  <span class="org-type">uint8_t</span> <span class="org-variable-name">program</span><span class="org-rainbow-delimiters-depth-1">[]</span> = <span class="org-rainbow-delimiters-depth-1">{</span> <span class="org-constant">BC_PUSH</span>, <span class="org-highlight-numbers-number">1</span>, <span class="org-constant">BC_PUSH</span>, <span class="org-highlight-numbers-number">41</span>, <span class="org-constant">BC_ADD</span>, <span class="org-constant">BC_PRINT</span>, <span class="org-constant">BC_HALT</span> <span class="org-rainbow-delimiters-depth-1">}</span>;

  <span class="org-type">void</span> <span class="org-function-name">interpreter</span><span class="org-rainbow-delimiters-depth-1">(</span> <span class="org-type">uint8_t</span>* <span class="org-variable-name">instr</span> <span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    <span class="org-type">uint8_t</span> <span class="org-variable-name">stack</span><span class="org-rainbow-delimiters-depth-2">[</span><span class="org-highlight-numbers-number">256</span><span class="org-rainbow-delimiters-depth-2">]</span>;
    <span class="org-type">uint8_t</span>* <span class="org-variable-name">sp</span> = stack;

    <span class="org-keyword">for</span> <span class="org-rainbow-delimiters-depth-2">(</span>;;<span class="org-rainbow-delimiters-depth-2">)</span>
      <span class="org-keyword">switch</span> <span class="org-rainbow-delimiters-depth-2">(</span> *instr <span class="org-rainbow-delimiters-depth-2">)</span>  <span class="org-rainbow-delimiters-depth-2">{</span>
      <span class="org-keyword">case</span> <span class="org-constant">BC_PUSH</span>:
        instr++;
        sp++;
        *sp = *instr;
        instr++;
        <span class="org-keyword">break</span>;

      <span class="org-keyword">case</span> <span class="org-constant">BC_PRINT</span>:
        printf<span class="org-rainbow-delimiters-depth-3">(</span> <span class="org-string">"%"</span> PRId8 <span class="org-string">"\n"</span>, sp<span class="org-rainbow-delimiters-depth-4">[</span><span class="org-highlight-numbers-number">0</span><span class="org-rainbow-delimiters-depth-4">]</span> <span class="org-rainbow-delimiters-depth-3">)</span>;
        instr++;
        <span class="org-keyword">break</span>;

      <span class="org-keyword">case</span> <span class="org-constant">BC_ADD</span>:
        sp--;
        sp<span class="org-rainbow-delimiters-depth-3">[</span><span class="org-highlight-numbers-number">0</span><span class="org-rainbow-delimiters-depth-3">]</span> += sp<span class="org-rainbow-delimiters-depth-3">[</span><span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-3">]</span>;
        instr++;
        <span class="org-keyword">break</span>;

      <span class="org-keyword">case</span> <span class="org-constant">BC_HALT</span>:
        <span class="org-keyword">return</span>;
      <span class="org-rainbow-delimiters-depth-2">}</span>
  <span class="org-rainbow-delimiters-depth-1">}</span>

  <span class="org-type">int</span> <span class="org-function-name">main</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
    interpreter<span class="org-rainbow-delimiters-depth-2">(</span> program <span class="org-rainbow-delimiters-depth-2">)</span>;
    <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
  <span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>


<p>
With computed gotos:
</p>

<div class="org-src-container">
<pre class="src src-c"><span class="org-preprocessor">#include</span> <span class="org-string"><span class="org-rainbow-delimiters-depth-1">&lt;</span></span><span class="org-string">stdio.h</span><span class="org-string"><span class="org-rainbow-delimiters-depth-1">&gt;</span></span>
<span class="org-preprocessor">#include</span> <span class="org-string"><span class="org-rainbow-delimiters-depth-1">&lt;</span></span><span class="org-string">inttypes.h</span><span class="org-string"><span class="org-rainbow-delimiters-depth-1">&gt;</span></span>

<span class="org-keyword">enum</span> <span class="org-type">bc</span> <span class="org-rainbow-delimiters-depth-1">{</span> <span class="org-variable-name">BC_PUSH</span>, <span class="org-variable-name">BC_PRINT</span>, <span class="org-variable-name">BC_ADD</span>, <span class="org-variable-name">BC_HALT</span> <span class="org-rainbow-delimiters-depth-1">}</span>;

<span class="org-type">uint8_t</span> <span class="org-variable-name">program</span><span class="org-rainbow-delimiters-depth-1">[]</span> = <span class="org-rainbow-delimiters-depth-1">{</span> <span class="org-constant">BC_PUSH</span>, <span class="org-highlight-numbers-number">1</span>, <span class="org-constant">BC_PUSH</span>, <span class="org-highlight-numbers-number">41</span>, <span class="org-constant">BC_ADD</span>, <span class="org-constant">BC_PRINT</span>, <span class="org-constant">BC_HALT</span> <span class="org-rainbow-delimiters-depth-1">}</span>;

<span class="org-type">void</span> <span class="org-function-name">interpreter</span><span class="org-rainbow-delimiters-depth-1">(</span> <span class="org-type">uint8_t</span>* <span class="org-variable-name">instr</span> <span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">{</span>
  <span class="org-type">uint8_t</span> <span class="org-variable-name">stack</span><span class="org-rainbow-delimiters-depth-2">[</span><span class="org-highlight-numbers-number">256</span><span class="org-rainbow-delimiters-depth-2">]</span>;
  <span class="org-type">uint8_t</span>* <span class="org-variable-name">sp</span> = stack;

  <span class="org-type">void</span>* <span class="org-variable-name">labels</span><span class="org-rainbow-delimiters-depth-2">[]</span> = <span class="org-rainbow-delimiters-depth-2">{</span> &amp;&amp;label_PUSH, &amp;&amp;label_PRINT, &amp;&amp;label_ADD, &amp;&amp;label_HALT <span class="org-rainbow-delimiters-depth-2">}</span>;

  <span class="org-keyword">goto</span> *labels<span class="org-rainbow-delimiters-depth-2">[</span>*instr<span class="org-rainbow-delimiters-depth-2">]</span>;

  <span class="org-constant">label_PUSH</span>:
  instr++;
  sp++;
  *sp = *instr;
  instr++;
  <span class="org-keyword">goto</span> *labels<span class="org-rainbow-delimiters-depth-2">[</span>*instr<span class="org-rainbow-delimiters-depth-2">]</span>;

  <span class="org-constant">label_PRINT</span>:
  printf<span class="org-rainbow-delimiters-depth-2">(</span> <span class="org-string">"%"</span> PRId8 <span class="org-string">"\n"</span>, sp<span class="org-rainbow-delimiters-depth-3">[</span><span class="org-highlight-numbers-number">0</span><span class="org-rainbow-delimiters-depth-3">]</span> <span class="org-rainbow-delimiters-depth-2">)</span>;
  instr++;
  <span class="org-keyword">goto</span> *labels<span class="org-rainbow-delimiters-depth-2">[</span>*instr<span class="org-rainbow-delimiters-depth-2">]</span>;

  <span class="org-constant">label_ADD</span>:
  sp--;
  sp<span class="org-rainbow-delimiters-depth-2">[</span><span class="org-highlight-numbers-number">0</span><span class="org-rainbow-delimiters-depth-2">]</span> += sp<span class="org-rainbow-delimiters-depth-2">[</span><span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-2">]</span>;
  instr++;
  <span class="org-keyword">goto</span> *labels<span class="org-rainbow-delimiters-depth-2">[</span>*instr<span class="org-rainbow-delimiters-depth-2">]</span>;

  <span class="org-constant">label_HALT</span>:
  <span class="org-keyword">return</span>;
<span class="org-rainbow-delimiters-depth-1">}</span>

<span class="org-type">int</span> <span class="org-function-name">main</span><span class="org-rainbow-delimiters-depth-1">()</span> <span class="org-rainbow-delimiters-depth-1">{</span>
  interpreter<span class="org-rainbow-delimiters-depth-2">(</span> program <span class="org-rainbow-delimiters-depth-2">)</span>;
  <span class="org-keyword">return</span> <span class="org-highlight-numbers-number">0</span>;
<span class="org-rainbow-delimiters-depth-1">}</span>
</pre>
</div>

<p>
We replaced <code>switch</code> with an array storing the address of an instruction
handler. Each time we fetch an instruction, we are using its bytecode as an
offset in this array. After taking an address from there, we jump to it.  The
larger the instruction set and the <code>switch</code> gets, the more noticeable gets the
difference in a real world program.
</p>

<p>
Using <code>switch</code> slow us down for two reasons:
</p>
<ul class="org-ul">
<li>It is forced to perform the bounds check according to C standard. If no
<code>case</code> exists for a <code>switch</code> expression, and the <code>default</code> case is missing as
well, no part of the <code>switch</code> body is executed<sup><a id="fnr.1" class="footref" href="#fn.1" role="doc-backlink">1</a></sup>.
Computed <code>goto</code> will just result in an undefined behavior in case of an invalid opcode.</li>
<li>When using <code>switch</code>, there is a single point where the decision about where
are we going is taken. In case of computed <code>goto</code>, such decisions are taken
at the end of each instruction handler. It makes CPU hold separate prediction
histories for each decision making point, which is good for dynamic
branch-predicting algorithms.</li>
</ul>

<p>
Starting with Haswell architecture the branch prediction algorithms were tuned
so that <code>switch</code> is predicted better, so the performance gain from using
computed <code>goto</code> is not that substantial.
</p>

<p>
P.S. If you really want to make a faster interpreter, consider implementing
indirect threaded code, direct threaded code or write a JIT compiler.
Computed <code>goto</code> is not a magical thing to make your interpreter as fast as
possible.
</p>
</div>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">

<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
C99 standard, Section 6.8.4.2.
</p></div></div>


</div>
</div>]]></description>
    </item>
    <item>
      <title>Proving dependent equalities in Coq with SSReflect</title>
      <link>https://rubber-duck-typing.com/posts/2016-07-29-proving-dependent-equalities-coq.html</link>
      <guid>https://rubber-duck-typing.com/posts/2016-07-29-proving-dependent-equalities-coq.html</guid>
      <pubDate>Fri, 29 Jul 2016 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
Proving dependent equalities in Coq  is boring, but quite frequently done.
I got so annoyed with it that I wrote a little tactic to automatize it a bit.
</p>

<p>
It does use ssreflect routines but it should not be hard to adapt it to vanilla Coq.
</p>

<div class="org-src-container">
<pre class="src src-nil">Ltac depcomp H := apply EqdepFacts.eq_sigT_eq_dep in H; 
apply Coq.Logic.Eqdep.EqdepTheory.eq_dep_eq in H.
Ltac eq_comp c x y := 
  move: (c x y); 
  case; 
  last try do [by [right; case]| 
  right; case; let H' := fresh "H" in move=&gt;H'; by depcomp H'];
  first try let H' := fresh "H" in move=&gt; H'; subst.


Definition eq_dec T := forall x y: T, {x = y} + {~ x = y}.

(* Example *)
Theorem pair_eq_dec T U: eq_dec T -&gt; eq_dec U -&gt; eq_dec (prod T U).
move=&gt; HT HU [x y] [a b].
eq_comp HT x a.
eq_comp HU y b.
by left.
Qed.
</pre>
</div>
]]></description>
    </item>
    <item>
      <title>Causation in modern philosophy of science</title>
      <link>https://rubber-duck-typing.com/posts/2016-07-16-causation-overview.html</link>
      <guid>https://rubber-duck-typing.com/posts/2016-07-16-causation-overview.html</guid>
      <pubDate>Sat, 16 Jul 2016 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
Our minds use the notions of cause and consequences all the time. It is, without
a doubt, one of the fundamentals of human reasoning. However, when observed
closely and formally, it becomes apparent that our concept is based rather on
intuition and lacks strictness and connection to the real word. This post is
intended as a quick introduction to causation from the philosophical point of
view.
</p>
<div id="outline-container-orga56b30c" class="outline-2">
<h2 id="orga56b30c">General thoughts</h2>
<div class="outline-text-2" id="text-orga56b30c">
<ol class="org-ol">
<li>Where to establish the causal relation? We are going to use propositions, which
represent quantitative properties of various systems in a given moment of time,
that is, <i>measurable properties</i>. Events represents the changes in these
properties. These events will be selected as causes and consequences.  This way
we are building an expressive language to construct statements of a scientific
value. Contrary, people often think about events that can either happen or not.
In our system such events are easily modeled as Boolean properties (taking
    value of either 0 or 1).</li>
<li>There exist two seemingly equally justified points of view on the place of causal relations in the world.
<ul class="org-ul">
<li>There are common causation laws that rule the world; then they are
instantiated in different situations with different events.</li>
<li>Causes and consequences are an indispensable part of the world, all 
generalizations are secondary.</li>
</ul></li>
</ol>
</div>
</div>
<div id="outline-container-org0a0c613" class="outline-2">
<h2 id="org0a0c613">Hume's arguments</h2>
<div class="outline-text-2" id="text-org0a0c613">
<p>
The arguments we are going to study can partly be traced to XVIII century! Meet
David Hume, a philosopher.
</p>

<p>
Hume’s account on causes and consequences is highly empiric. What an observer
(even ideal one) can observe about two events A and B is:
</p>

<ul class="org-ul">
<li>\(A\) occured before \(B\).</li>
<li>\(A\) and \(B\) are close in space and time. Or they are connected by a chain of
events, where each link is a relation between two events which satisfies
these properties.</li>
<li>When we observe something resembling \(A\) again, something resembling \(B\) appears
as well.</li>
</ul>

<p>
It is suspicious that these three observations sum up everything an observer
can notice &#x2014; even an ideal observer. There is no way this information might
be of a foundation for a strict causal relation as we usually imagine it.
</p>

<p>
Adding causality changes absolutely nothing here, because it does not give us
anything observable.
</p>

<p>
In fact the pattern above can describe many events not necessarily connected in
any way. Hume’s opinion is a great starting point, but now let us also address
some problems about it.
</p>

<p>
Three properties of \((A,B)\)  pinpointed above occur not only in cases where we
want to establish the relation, but also:
</p>

<ol class="org-ol">
<li>When \(A\) and \(B\) have a common cause</li>
<li>Just by coincidence</li>
<li>By preemption. It means that there is an event \(C\) that occurred before \(A\)</li>
</ol>
<p>
and would have caused \(B\) anyway.
</p>

<p>
These three major problems are addressed differently depending on how one sees
causation in general, which features of the cause-consequence pair are really
key. Let’s now speak about those ways.
</p>
</div>
</div>
<div id="outline-container-org6debc8b" class="outline-2">
<h2 id="org6debc8b">Sufficiency</h2>
<div class="outline-text-2" id="text-org6debc8b">
<p>
To this day people tend to think about causation as a deterministic beast. Our
knowledge about micro world, however, contradicts it (as many things about
micro world contradict common sense). Contrary to some traditional views on
causation based on necessity of the cause to bring about its effect, their
modern counterparts fit mostly in two categories:
</p>

<ol class="org-ol">
<li>Those who base on regular occurrences of \(A\) followed by \(B\).</li>
<li>Those who reconstruct the events of real world in the other, purely logical world.</li>
</ol>

<p>
Let’s take a closer look at the second point. In order to construct our logical
structures we can use certain sets of rules (they can represent f.e. the laws
of nature) of form:
</p>

<p>
\(\forall x, F \ x \Rightarrow G \ x\)
</p>

<p>
Here for an event \(a\) the expression  \(F a\) will be substituted by an event
instance; \(G a\)  is the consequence. As for the arrow, we can substitute it for
either a material conditional (think simple <i>implication</i>) or something
stronger like a <i>subjunctive conditional</i> ("If Oswald hadn’t killed Kennedy,
someone else would have").
</p>
</div>
</div>
<div id="outline-container-orge9dec30" class="outline-2">
<h2 id="orge9dec30">Necessity</h2>
<div class="outline-text-2" id="text-orge9dec30">
<p>
You know, sufficiency is not <i>sufficient</i> for causation. Even if we stick to
determinism, the overdetermination (multiple causes) alone is a valid reason to
question it.  Let us say, \(A\) and \(B\) can both equally cause \(C\) and they occurred
simultaneously. What caused \(C\)?
</p>

<p>
Basing on sufficiency of cause we can deduce:
</p>

<ul class="org-ul">
<li>If \(A\) was the cause, then \(C\) would not have occurred without \(A\). So \(A\) is not the cause.</li>
<li>If \(B\) was the cause, then \(C\) would not have occurred without \(B\). So \(B\) is not the cause.</li>
</ul>

<p>
Some people tend to think that it’s rather the cause’s necessity for the
consequence that forms a casual relation between them. This way \(A\) caused \(B\)
if and only if:
</p>

<ul class="org-ul">
<li>\(A\) and \(B\) occurred.</li>
<li>We can assert:  "If \(A\) had not occurred, than \(B\) would not have occurred" (we will refer to it as <i>sine qua non</i>, because, well, its  what it is).</li>
</ul>

<p>
I guess it should look like: \(A \land B \land ( \neg A \rightarrow \neg B)\).
</p>

<p>
It is but a foundation of a longer talk I intend to give in the next post based
on arguments of Lewis and maybe Mackie (if I do have time for his book).
</p>
</div>
</div>
<div id="outline-container-org3f26e6d" class="outline-2">
<h2 id="org3f26e6d">Probabilistic approach</h2>
<div class="outline-text-2" id="text-org3f26e6d">
<p>
 
Reasoning about <i>sine qua non</i> is not easy as long as you abandon determinism.
Non-determinism, however, goes in pair with probabilities. The general idea of
this approach is that causes increase probabilities of consequences in a large
variety of contexts: 
</p>

<p>
\(P ( B / AZ ) > P (B / \neg AZ )\)
</p>

<p>
Here \(Z\) should take into account:
</p>

<ul class="org-ul">
<li>Common causes of \(A\) and \(B\).</li>
<li>Preemptive causes of \(B\).</li>
</ul>


<p>
This way preemption problem and common cause problem are addressed, and \(A\) and \(B\) 
become probabilistically independent.
</p>

<p>
The hard thing is to choose \(Z\) and a good definition of probability.
</p>

<p>
For example, take relative frequencies for probability. This way we should
exclude from \(Z\) all causal consequences of \(B\) for which \(B\) is necessary. If we do
not do it, we just get a wrong inequality \(1 > 1\) (check it!). However, look at
it again: to calculate \(P\) we need exactly causal data for which we are building
a theory! We have faced a vicious circle that is not easy to break.
</p>
</div>
</div>
<div id="outline-container-org202e341" class="outline-2">
<h2 id="org202e341">Conclusion</h2>
<div class="outline-text-2" id="text-org202e341">
<p>
As we see, there are loads of interesting subtleties when it comes to a closer
study of causation. Numerous attempts were made to exile this relation
completely from scientific thinking, but its roots are so strong it proved
almost impossible to do. Next time I want to talk about Lewis’s very influential
theory on causation, dated 1973, and then about his new causation theory of
early 2000’s.
</p>
</div>
</div>
]]></description>
    </item>
    <item>
      <title>Proving type inequalities in Coq</title>
      <link>https://rubber-duck-typing.com/posts/2016-05-29-how-to-prove-type-inequalities-coq.html</link>
      <guid>https://rubber-duck-typing.com/posts/2016-05-29-how-to-prove-type-inequalities-coq.html</guid>
      <pubDate>Sun, 29 May 2016 00:00:00 +0200</pubDate>
      <description><![CDATA[<p>
Sometimes you just want to prove that nasty   <code>~ T = U</code> for some types <code>T</code> and <code>U</code>. Well, while in general it is not decidable (nor provable), sometimes there is a relatively easy way to do it, when <code>T</code> and <code>U</code> are not both infinite. In other words, either <code>T</code> or <code>U</code>, or both of them should be finite.
</p>
<div id="outline-container-org1898f1e" class="outline-2">
<h2 id="org1898f1e">Simple example: bool is not unit</h2>
<div class="outline-text-2" id="text-org1898f1e">
<p>
The idea is simple: if types are equal, there exists a bijection between their elements. Let us prove a simple lemma <code>exists_bijection</code>: 
</p>


<p>
\[\forall A \ B: Set, A = B \rightarrow \exists f \exists g: \forall x:A \ \forall y : B, (f \ x = y -> g \ y = x )\]
</p>

<div class="org-src-container">
<pre class="src src-nil">Definition id {T} := fun x : T =&gt; x.
 
Lemma exists_bijection (A B: Set):
A = B -&gt; exists f, exists g, forall (x:A) (y:B),
 f x = y -&gt; g y = x.
Proof.
 intro H; subst. 
 exists id. exists id.
 intros. subst. 
 reflexivity.
Qed.
</pre>
</div>

<p>
The proof was a piece of cake. Now to battle!
</p>

<p>
You remember, that <code>unit</code> is a type inhabited by only one element <code>tt</code>?
</p>

<div class="org-src-container">
<pre class="src src-nil">Inductive unit : Set := tt : unit.
</pre>
</div>

<p>
Both <code>f true</code> and <code>f false</code> are evaluated to <code>tt</code>.
There is not much choice here.
However we know, that \(\forall x, (g \cdot f) \ x = x\) .
By instantiating <code>x</code> with <code>true</code> and <code>false</code> we get \(g (f \ true) = true\) and \(g (f \ false) = false\).
Since both \(f \ true\) and \(f\ false\) are equal, we deduce \(g\ tt =true\) and \(g \ tt = false\), contradiction.
</p>


<p>
Now the code:
</p>

<div class="org-src-container">
<pre class="src src-nil">Lemma unit_neq_bool: bool = unit -&gt; False.
Proof.
intro Heq.
destruct (exists_bijection _ _ Heq) as [f [g Hfg]].
destruct (f true) eqn: Hft.
destruct (f false) eqn: Hff.
pose proof (Hfg _ _ Hft) as Hgtt.
pose proof ( Hfg _ _ Hff) as Hgtf.
 
rewrite Hgtf in Hgtt.
inversion Hgtt.
Qed.
</pre>
</div>

<p>
So, the key idea is to enumerate possible bijections.
As at least one of sets is finite, you will eventually enumerate all the candidates, and when the sets are of different size, you will get contradictions because you will run out of distinct functions trying to enumerate all bijections.
</p>
</div>
<div id="outline-container-orgf5c71e3" class="outline-3">
<h3 id="orgf5c71e3">Example: nat is not bool</h3>
<div class="outline-text-3" id="text-orgf5c71e3">
<p>
Let us apply the same principle to prove <code>~ nat = bool</code>.
</p>

<p>
We are going to use a bit of semicolons because otherwise the proof will become very repetitive. Basically where you see an exclamation mark we got 8 goals which contexts include:
</p>

<div class="org-src-container">
<pre class="src src-nil">g x = 0
g y = 1
g z = 2
</pre>
</div>

<p>
where <code>x</code>, <code>y</code>, <code>z</code> all range over \({ true, false}\). There will always be at least two of three boolean values equal to each other (which should follow from Dirichlet principle), which will feed us with nice contradiction to do a rewrite and inversion.
</p>

<div class="org-src-container">
<pre class="src src-nil">Lemma nat_neq_bool: nat = bool -&gt; False.
intro Heq.
destruct (exists_bijection _ _ Heq) as [f [g Hfg]].
 
destruct (f 0) eqn: Hf0;
destruct (f 1) eqn: Hf1;
destruct (f 2) eqn: Hf2;
pose proof (Hfg _ _ Hf0) as Hg0;
pose proof (Hfg _ _ Hf1) as Hg1;
pose proof (Hfg _ _ Hf2) as Hg2.  (* ! *)
rewrite Hg2 in Hg1; inversion Hg1.
rewrite Hg1 in Hg0; inversion Hg0.
rewrite Hg0 in Hg2; inversion Hg2.
rewrite Hg1 in Hg2; inversion Hg2.
rewrite Hg1 in Hg2; inversion Hg2.
rewrite Hg2 in Hg0; inversion Hg0.
rewrite Hg0 in Hg1; inversion Hg1.
rewrite Hg1 in Hg0; inversion Hg0.
Qed.
</pre>
</div>

<p>
An interesting question (the answer to which I do not know) is whether we could automate it inside Coq somehow without writing plugins for it.
</p>
</div>
</div>
</div>
]]></description>
    </item>
  </channel>
</rss>
