<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><![CDATA[Life & Computing Science]]></title>
<description><![CDATA[Life & Computing Science]]></description>
<link>https://alhassy.github.io/</link>
<lastBuildDate>Tue, 28 Nov 2023 06:52:09 -0500</lastBuildDate>
<item>
  <title><![CDATA[Java CheatSheet]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-java.html"><img src="https://img.shields.io/badge/-java-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-cheat-sheet.html"><img src="https://img.shields.io/badge/-cheat_sheet-grey?logo=nil"></a></center>
<center><a href="../images/modern-java.png" class="tooltip" title="Image credit “../images/modern-java.png”"><img src="../images/modern-java.png" alt="Article image" style="border: 2px solid black;" width="88%" height="88%" align="top"/></a></center>


<div class="abstract" style="border: 1px solid black;padding: 1%; margin-top: 1%; margin-bottom: 1%;margin-right: 10%; margin-left: 10%; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>
<div class="org-center">
This is a quick reference of concepts in modern Java.

<p>
<a href="https://alhassy.com/java-cheat-sheet.pdf"><img src="https://img.shields.io/badge/PDF-colorful_cheat_sheet-success?logo=read-the-docs"></a>
</p>

<p>
<a href="https://twitter.com/intent/tweet?text=This looks super neat (•̀ᴗ•́)و::&url=https://alhassy.com/java-cheat-sheet"><img src="https://img.shields.io/twitter/url?url=https://alhassy.com/java-cheat-sheet"></a>
<a href="https://www.buymeacoffee.com/alhassy"><img src="https://img.shields.io/badge/-buy_me_a_coffee-gray?logo=buy-me-a-coffee"></a>
<a href="https://github.com/alhassy/alhassy.github.io/issues"><img src="https://img.shields.io/badge/contributions-welcome-green?logo=nil"></a>
</p>
</div>

<p>
Modern Java is a strongly-typed, eagery evaluated, case sensative, yet
whitespace insensative language. It uses hierarchies of classes/types
to structure data, but also has first-class support for
functional-style algebraic datatypes.
</p>

<p>
Java programs are made up of ‘classes’, classes contain methods, and methods contain commands.  To just try out a
snippet of code, we can
</p>
<ul class="org-ul">
<li><p>
Open a terminal and enter <code>jshell</code>; then enter:
</p>
<div class="org-src-container">
<pre class="src src-java">1 + 2 <span style="color: #a89984;">// </span><span style="color: #a89984;">The jshell lets you try things out!</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Say hello in a fancy way</span>
<span style="color: #98971a; font-weight: bold;">import</span> <span style="color: #076678; font-weight: bold;">javax</span>.<span style="color: #076678; font-weight: bold;">swing</span>.*;
JOptionPane.showMessageDialog(<span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">JFrame</span>(), <span style="color: #689d6a;">"Hello, World!"</span>);

</pre>
</div></li>

<li>Alternatively, in IntelliJ, click <i>Tools</i> then <i>Groovy Console</i> to try things out!</li>
<li>Finally, <a href="http://alhassy.com/making-vscode-itself-a-java-repl.html">VSCode</a> allows arbitrary Java code to be sent to a <code>jshell</code>
in the background(!) and it echoes the result in a friendly way.</li>
</ul>

<!--
<p>
To be terse, lots of content is not shown in this <a href="http://alhassy.com/java-cheat-sheet.pdf">PDF</a>, but is shown in the <b><a href="https://alhassy.com/java-cheat-sheet">HTML</a></b>
version.
</p>
-->


</div>

<div id="outline-container-org140d9b7" class="outline-2">
<h2 id="org140d9b7"><span class="section-number-2">1.</span> <a href="#org140d9b7">The Philosophy of Classes &amp; Interfaces</a></h2>
<div class="outline-text-2" id="text-1">
<p>
Real life objects have properties and behaviour.  For example, my cat
has the properties of <i>name</i> and <i>age</i>, and amongst its behaviours are
<i>sleep</i> and <i>meow</i>.  However, an apple does not have such features.
<b>The possible features of an object are understood when we <i>classify</i>
it;</b> e.g., my cat is an animal, whereas an apple is food.  In Java, a
set of features is known as a <code class="src src-java"><span style="color: #98971a; font-weight: bold;">class</span></code> and
objects having those features are called “objects of that class”.
Just as <code>int</code> is the type of the value <code>12</code>, we say a class is a
<i>type</i> of an object.
</p>

<p>
We tend to think in (disjoint, hierarchical) categories; for example,
in my library, a book can be found in one section, either “Sports” or
“History” or “Trees”. So where should books on the history of football
be located? Or books on the history of trees?  My library places such
books under “Sports”, and “Tree” respecively, but then adds a
“historical” <b>tag</b> to them.  Likewise, in Java, <b>to say different
kinds of things have a feature in common, we “tag” them with an</b>
<b><code class="src src-java"><span style="color: #98971a; font-weight: bold;">interface</span></code>.</b> (Real life tagging is a also known
as <i>multi</i>-<code>class</code>-<i>ificiation</i>.)
</p>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Java's Main Organisational Mechanisms</h3>
<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">&#xa0;</th>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">With state</th>
<th scope="col" class="org-left">Without state</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Attributes &amp; properties</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">  <code class="src src-java"><span style="color: #98971a; font-weight: bold;">class</span></code></td>
<td class="org-left">  <code class="src src-java"><span style="color: #98971a; font-weight: bold;">record</span></code></td>
</tr>

<tr>
<td class="org-left">Partial implementations</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">  <code class="src src-java"><span style="color: #98971a; font-weight: bold;">abstract class</span></code></td>
<td class="org-left">  <code class="src src-java"><span style="color: #98971a; font-weight: bold;">interface</span></code></td>
</tr>
</tbody>
</table>

</div>


<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Here is a teaser of nearly all of these packaging mechanisms </font> </strong> </summary>

<div class="org-src-container">
<pre class="src src-java"><span style="color: #98971a; font-weight: bold;">interface</span> <span style="color: #b57614;">Hospitable</span> {
    <span style="color: #b57614;">String</span> <span style="color: #076678;">name</span>();   <span style="color: #a89984;">/* </span><span style="color: #a89984;">unimplemented method signature</span><span style="color: #a89984;"> */</span>
    <span style="color: #98971a; font-weight: bold;">default</span> <span style="color: #b57614;">void</span> <span style="color: #076678;">greet</span>() { <span style="color: #a89984;">/* </span><span style="color: #a89984;">derived, implemented, method</span><span style="color: #a89984;"> */</span>
        System.out.printf(<span style="color: #689d6a;">"%s says &#8220;Welcome!&#8221;"</span>, name());
    }
}

<span style="color: #a89984;">////////////////////////////////////////////////////////////////////////////////</span>

<span style="color: #98971a; font-weight: bold;">class</span> <span style="color: #b57614;">Person</span> <span style="color: #98971a; font-weight: bold;">implements</span> <span style="color: #b57614;">Hospitable</span> {
    <span style="color: #b57614;">String</span> <span style="color: #076678;">name</span>;
    <span style="color: #b57614;">int</span> <span style="color: #076678;">age</span>;
    <span style="color: #98971a; font-weight: bold;">public</span> <span style="color: #b57614;">String</span> <span style="color: #076678;">name</span>() { <span style="color: #98971a; font-weight: bold;">return</span> name; }
}

<span style="color: #a89984;">// </span><span style="color: #a89984;">Actual usage of &#8220;Person&#8221;:</span>
<span style="color: #b57614;">Person</span> <span style="color: #076678;">me</span> = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Person</span>();
me.name = <span style="color: #689d6a;">"Musa"</span>;
me.age = 31;
me.greet();

<span style="color: #a89984;">////////////////////////////////////////////////////////////////////////////////</span>

<span style="color: #b57614;">record</span> <span style="color: #076678;">Person2</span>(<span style="color: #b57614;">String</span> <span style="color: #076678;">name</span>, <span style="color: #b57614;">int</span> <span style="color: #076678;">age</span>) <span style="color: #98971a; font-weight: bold;">implements</span> <span style="color: #b57614;">Hospitable</span> { }

<span style="color: #a89984;">// </span><span style="color: #a89984;">Actual usage of &#8220;Person2&#8221;:</span>
<span style="color: #b57614;">Person2</span> <span style="color: #076678;">me</span> = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Person2</span>(<span style="color: #689d6a;">"Musa"</span>, 31);
me.greet();
</pre>
</div>

</details> </div>
<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">“Interfaces are the types of classes” </font> </strong> </summary>

<p>
A <i>module</i> is
a bunch of utilities that can be defined from some shared set of parameters.
Those utilities can be thought of as an interface 𝑰.
<i>Then a module is a function from parameters to an anonymous implementation of an interface.</i>
However, functions that return implementations are essentially records/classes that implement
the interface; i.e.,
</p>

<div class="org-src-container">
<pre class="src src-java">   <span style="color: #b57614;">&#119920;</span> <span style="color: #076678;">R</span>(<span style="color: #b57614;">params</span>) { <span style="color: #98971a; font-weight: bold;">return</span> <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">&#119920;</span>() { &#119920;_&#119926;&#119933;&#119916;&#119929;&#119920;&#119915;&#119916;&#119930; }; } <span style="color: #a89984;">// </span><span style="color: #a89984;">module-as-function</span>
&#8776;  record R(params) <span style="color: #98971a; font-weight: bold;">implements</span> <span style="color: #b57614;">&#119920;</span> { &#119920;_&#119926;&#119933;&#119916;&#119929;&#119920;&#119915;&#119916;&#119930; }; <span style="color: #a89984;">// </span><span style="color: #a89984;">module-as-record</span>
</pre>
</div>

<p>
This equation justifies the phrase “interfaces are the types of records/classes”
since a record declaration (i.e., the right side of the “≈”)
can be converted to an (abstract) module (of type 𝑰) &#x2014;i.e., the left side of the “≈”.
</p>

</details> </div>
<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Algebraic Data Types :ignore</font> </strong> </summary>

<p>
Finally, suppose there's something you want to do <i>and</i> there are a number of
ways/configurations to get it done.  You could write it as a method in
a <code class="src src-java"><span style="color: #98971a; font-weight: bold;">class</span></code> with a bunch of <code>if</code>'s to account for
all of those ways.  Better would be to create an interface, then have
a bunch of classes that implement it: One class for each possible
implementation. Finally, if you know <i>all</i> configurations, you
can move those classes <i>into</i> the definition of the interface
and make it <i>sealed</i>: This is known as an <i>algebraic data-type</i>,
whose kill-feature is that you can use <code class="src src-java"><span style="color: #98971a; font-weight: bold;">switch</span></code>
to pattern match on instances of the interface.
</p>


</details> </div>
<div><a style="width: 1%;float: left; padding: 0px" id="ADTs" href="#ADTs">🔗</a> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">ADT example </font> </strong> </summary>

<p>
An example 3-level hierarchy that can be easily represented with ADTs
rather than a traditional class hierarchy.
</p>

<center><image src="../images/monsters-adt.png" width="50%" height="50%" /> </center>

<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">sealed</span> <span style="color: #98971a; font-weight: bold;">interface</span> Monster {

    <span style="color: #b57614;">sealed</span> <span style="color: #98971a; font-weight: bold;">interface</span> Flying <span style="color: #98971a; font-weight: bold;">extends</span> <span style="color: #b57614;">Monster</span> { }
    <span style="color: #b57614;">record</span> <span style="color: #076678;">Griffin</span>() <span style="color: #98971a; font-weight: bold;">implements</span> <span style="color: #b57614;">Flying</span> { }
    <span style="color: #b57614;">record</span> <span style="color: #076678;">Pegasus</span>() <span style="color: #98971a; font-weight: bold;">implements</span> <span style="color: #b57614;">Flying</span> { }

    <span style="color: #b57614;">sealed</span> <span style="color: #98971a; font-weight: bold;">interface</span> Ground <span style="color: #98971a; font-weight: bold;">extends</span> <span style="color: #b57614;">Monster</span> { }
    <span style="color: #b57614;">record</span> <span style="color: #076678;">Ogre</span>() <span style="color: #98971a; font-weight: bold;">implements</span> <span style="color: #b57614;">Ground</span> { }
}
</pre>
</div>

<p>
Then we can actually use this new type as follows:
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #98971a; font-weight: bold;">private</span> <span style="color: #98971a; font-weight: bold;">static</span> <span style="color: #b57614;">String</span> <span style="color: #076678;">glare</span>(<span style="color: #b57614;">Monster</span> <span style="color: #076678;">m</span>) {
    <span style="color: #98971a; font-weight: bold;">return</span> <span style="color: #98971a; font-weight: bold;">switch</span> (m) {
        <span style="color: #98971a; font-weight: bold;">case</span> <span style="color: #076678; font-weight: bold;">Monster</span>.Griffin it -&gt; <span style="color: #689d6a;">"Roar"</span>;
        <span style="color: #98971a; font-weight: bold;">case</span> <span style="color: #076678; font-weight: bold;">Monster</span>.Pegasus it -&gt; <span style="color: #689d6a;">"HeeHaw"</span>;
        <span style="color: #98971a; font-weight: bold;">case</span> <span style="color: #076678; font-weight: bold;">Monster</span>.Ogre it -&gt; <span style="color: #689d6a;">"Grrr"</span>;
    };
}

glare(<span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #076678; font-weight: bold;">Monster</span>.<span style="color: #076678; font-weight: bold;">Flying</span>.<span style="color: #b57614;">Griffin</span>()); <span style="color: #a89984;">// </span><span style="color: #a89984;">"Roar"</span>
</pre>
</div>

<p>
Or only look at the <code>Flying</code> sub-type:
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #98971a; font-weight: bold;">private</span> <span style="color: #98971a; font-weight: bold;">static</span> <span style="color: #b57614;">int</span> <span style="color: #076678;">attackDamage</span>(<span style="color: #076678; font-weight: bold;">Monster</span>.<span style="color: #b57614;">Flying</span> <span style="color: #076678;">f</span>) {
    <span style="color: #98971a; font-weight: bold;">return</span> <span style="color: #98971a; font-weight: bold;">switch</span> (f) {
        <span style="color: #98971a; font-weight: bold;">case</span> <span style="color: #076678; font-weight: bold;">Monster</span>.<span style="color: #076678; font-weight: bold;">Flying</span>.Griffin it -&gt; 120;
        <span style="color: #98971a; font-weight: bold;">case</span> <span style="color: #076678; font-weight: bold;">Monster</span>.<span style="color: #076678; font-weight: bold;">Flying</span>.Pegasus it -&gt; 60;
    };
}

attackDamage(<span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #076678; font-weight: bold;">Monster</span>.<span style="color: #b57614;">Pegasus</span>()); <span style="color: #a89984;">// </span><span style="color: #a89984;">60</span>
</pre>
</div>

</details> </div>
<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Reads </font> </strong> </summary>

<ul class="org-ul">
<li><a href="https://www.mooc.fi/en/#courses">“MOOC” Massive Open Online Course - University of Helsinki</a>
<ul class="org-ul">
<li>Useful for learning Java, Python, Haskell, JavaScript.</li>
<li>I highly reccommend their <a href="https://fullstackopen.com/en/">“full stack”</a> course on web development, with JS!</li>
</ul></li>
<li>Effective Java, 3rd Edition by Joshua Bloch</li>
<li>Seriously Good Software Code that Works, Survives, and Wins</li>
<li>Functional Programming in Java Harnessing the Power of Java 8 Lambda Expressions</li>
<li>Java Generics and Collections Speed Up the Java Development Process</li>
<li>Java 8 Lambdas Pragmatic Functional Programming - Richard Warburton</li>
</ul>

</details> </div>
</div>
</div>

<div><a style="width: 1%;float: left; padding: 0px" id="null" href="#null">🔗</a> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Null </font> </strong> </summary>

<p>
There is a special value named <code class="src src-java"><span style="color: #076678; font-weight: bold;">null</span></code> that
denotes the absence of a meaningful value.  Ironically, it is a value
of every type (excluding the primitive types).  <a href="https://funcall.blogspot.com/2007/11/in-kingdom-of-nouns.html?m=1">Here</a> is a neat story
about <code>null</code>.
</p>


</details> </div>

<div id="outline-container-orgbe75c72" class="outline-2">
<h2 id="orgbe75c72"><span class="section-number-2">2.</span> <a href="#orgbe75c72">Primitive Objects</a></h2>
<div class="outline-text-2" id="text-2">
<p>
For performance reasons, there are a handful of types whose values are created
by <i>literals</i>; i.e., “What you see is what you get”.  (As such, primitives are a
basic building block which cannot be broken apart; whereas non-primitives (aka
references) are made-up from primitives and other references.)  For example, to
create a value of <code class="src src-java"><span style="color: #b57614;">int</span></code> we simply write <code>5</code>.
</p>

<p>
<i>There are no instance methods on literals;</i> only a
handful of operator methods.  For example, we cannot write <code>2.pow(3)</code>
to compute 2³, but instead must write <code class="src src-java">Math.pow(2, 3)</code>.
Finally, variables of primitive types have default values when not initialised
whereas object types default to <code class="src src-java"><span style="color: #076678; font-weight: bold;">null</span></code> &#x2014;note: <code>null</code> is a value of all object types, but not of primitive types.
</p>

<div style="column-rule-style: solid black;column-count: 2;">
<div class="org-src-container">
<pre class="src src-java"><span style="color: #a89984;">// </span><span style="color: #a89984;">Declare a new object type</span>
<span style="color: #98971a; font-weight: bold;">class</span> <span style="color: #b57614;">Person</span> { <span style="color: #b57614;">String</span> <span style="color: #076678;">name</span>; }

<span style="color: #b57614;">Person</span> <span style="color: #076678;">obj</span>; <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8776; null (OBJECT)</span>
<span style="color: #b57614;">int</span> <span style="color: #076678;">prim</span>;    <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8776; 0   (PRIMITIVE)</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Primitives are created as literals</span>
prim = 1;    <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8776; 1</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Objects are created with &#8220;new&#8221;</span>
obj = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Person</span>(); <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8776; a reference,</span>
   <span style="color: #a89984;">// </span><span style="color: #a89984;">like: Person@66048bfd</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Primitives are  identified by</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">thier literal shape</span>
<span style="color: #b57614;">assert</span> <span style="color: #076678;">prim</span> == 1;

<span style="color: #a89984;">// </span><span style="color: #a89984;">Objects are identified by</span>
<span style="color: #a89984;">/// </span><span style="color: #a89984;">references to their memory</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">locations (not syntax shape!)</span>
assert obj != <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Person</span>();

<span style="color: #a89984;">// </span><span style="color: #a89984;">Primitives copy values</span>
<span style="color: #b57614;">int</span> <span style="color: #076678;">primCopy</span> = prim;  <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8776; 1</span>

<span style="color: #a89984;">/// </span><span style="color: #a89984;">Objects copy references</span>
<span style="color: #b57614;">Person</span> <span style="color: #076678;">objCopy</span> = obj;
  <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8776; a reference, like: Person@66048bfd</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Changing primitive copy has</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">no impact on original</span>
primCopy = 123;
<span style="color: #b57614;">assert</span> <span style="color: #076678;">prim</span> == 1;

<span style="color: #a89984;">// </span><span style="color: #a89984;">Changing object copy also</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">changes the original!</span>
<span style="color: #b57614;">assert</span> <span style="color: #076678;">obj.name</span> == <span style="color: #076678; font-weight: bold;">null</span>;
objCopy.name = <span style="color: #689d6a;">"woah"</span>;    <span style="color: #a89984;">// </span><span style="color: #a89984;">Alter copy!</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">Original is altered!</span>
<span style="color: #b57614;">assert</span> <span style="color: #076678;">obj.name.equals</span>(<span style="color: #689d6a;">"woah"</span>);
</pre>
</div>

</div>
<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Wrapper Types </font> </strong> </summary>

<p>
Java lets primitives shift back and forth from their literal representations
and the world of reference objects somewhat-harmoniously by automatically
“boxing” them up as objects when need be. This is done by having class
versions of every primitive type; e.g., the primitive <code class="src src-java"><span style="color: #b57614;">int</span></code>
has the class version <code class="src src-java">Integer</code>.
</p>

<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">Integer</span> <span style="color: #076678;">x</span> = 1; <span style="color: #a89984;">// </span><span style="color: #a89984;">auto-boxed to an object</span>
<span style="color: #b57614;">int</span> <span style="color: #076678;">y</span> = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Integer</span>(2); <span style="color: #a89984;">// </span><span style="color: #a89984;">auto-unboxed to a primitive</span>
</pre>
</div>

<p>
<i>Primitives require much less memory!</i>
An <code>int</code> requires 32-bits to represent, whereas an <code>Integer</code> requires 128-bits:
The object requires as much space as 4 primitives, in this case.
</p>

</details> </div>
</div>
</div>

<div id="outline-container-org3799bf8" class="outline-2">
<h2 id="org3799bf8"><span class="section-number-2">3.</span> <a href="#org3799bf8">Properties and methods have <b>separate namespaces</b></a></h2>
<div class="outline-text-2" id="text-3">
<!--
<p>
Properties and methods have <b>separate namespaces</b> ---
</p>
-->

<p>
Below we use the name <code>plus1</code> in two different definitional roles.
Which one we want to refer to depends on whether we use "dot-notation" with <i>or</i> without parenthesis:
The parentheis indicate we want to use the method.
</p>

<div class="org-src-container">
<pre class="src src-java"><span style="color: #98971a; font-weight: bold;">class</span> <span style="color: #b57614;">SameNameNoProblem</span> {
    <span style="color: #98971a; font-weight: bold;">public</span> <span style="color: #98971a; font-weight: bold;">static</span> <span style="color: #b57614;">int</span> <span style="color: #076678;">plus1</span>(<span style="color: #b57614;">int</span> <span style="color: #076678;">x</span>){ <span style="color: #98971a; font-weight: bold;">return</span> x + 1; } <span style="color: #a89984;">// </span><span style="color: #a89984;">Method!</span>
    <span style="color: #98971a; font-weight: bold;">public</span> <span style="color: #98971a; font-weight: bold;">static</span> <span style="color: #b57614;">String</span> <span style="color: #076678;">plus1</span> = <span style="color: #689d6a;">"+1"</span>;             <span style="color: #a89984;">// </span><span style="color: #a89984;">Property!</span>
}

<span style="color: #98971a; font-weight: bold;">class</span> <span style="color: #b57614;">ElseWhere</span> {
    <span style="color: #b57614;">String</span> <span style="color: #076678;">pretty</span> = SameNameNoProblem.plus1;
    <span style="color: #b57614;">Integer</span> <span style="color: #076678;">three</span> = SameNameNoProblem.plus1(2);
}
</pre>
</div>
<p>
The consequence of different namespaces  are
</p>
<ol class="org-ol">
<li>Use <code class="src src-java">apply</code> to call functions bound to variables.</li>
<li>Refer to functions outside of function calls by using a double colon, <code>::</code>.</li>
</ol>
</div>
</div>

<div id="outline-container-org69dccc7" class="outline-2">
<h2 id="org69dccc7"><span class="section-number-2">4.</span> <a href="#org69dccc7">Anonymous <i>functions</i>:    <code>(arg₁, …, argₙ) → bodyHere</code>     </a></h2>
<div class="outline-text-2" id="text-4">
<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Functions are formed with the “→” notation and used with “apply”</h3>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #a89984;">// </span><span style="color: #a89984;">define, then invoke later on</span>
<span style="color: #b57614;">Function</span>&lt;<span style="color: #b57614;">Integer</span>, <span style="color: #b57614;">Integer</span>&gt; <span style="color: #076678;">f</span>  =  x -&gt; x * 2;

f.apply(3) <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; 6</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">f(3)    // invalid!</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">define and immediately invoke</span>
((Function&lt;<span style="color: #b57614;">Integer</span>, Integer&gt;) x -&gt; x * 2).apply(3);

<span style="color: #a89984;">// </span><span style="color: #a89984;">define from a method reference, using &#8220;::&#8221;</span>
<span style="color: #b57614;">Function</span>&lt;<span style="color: #b57614;">Integer</span>, <span style="color: #b57614;">Integer</span>&gt; <span style="color: #076678;">f</span> = SameNameNoProblem::plus1;
</pre>
</div>

</div>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Let's make a method that takes anonymous functions, and use it</h3>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #a89984;">// </span><span style="color: #a89984;">Recursion with the &#8216;tri&#8217;angle numbers: tri(f, n) = &#931;&#8319;&#7522;&#8332;&#8320; f(i).</span>
<span style="color: #98971a; font-weight: bold;">public</span> <span style="color: #98971a; font-weight: bold;">static</span> <span style="color: #b57614;">int</span> <span style="color: #076678;">tri</span>(<span style="color: #b57614;">Function</span>&lt;<span style="color: #b57614;">Integer</span>, <span style="color: #b57614;">Integer</span>&gt; <span style="color: #076678;">f</span>, <span style="color: #b57614;">int</span> <span style="color: #076678;">n</span>) {
    <span style="color: #98971a; font-weight: bold;">return</span> n &lt;= 0 ? 0 : f.apply(n) + tri(f, n - 1);
}

tri(x -&gt; x / 2, 100);  <span style="color: #a89984;">//  </span><span style="color: #a89984;">&#8658;  &#931;&#185;&#8304;&#8304;&#7522;&#8332;&#8320; i/2 = 2500</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Using the standard &#8220;do nothing&#8221; library function</span>
tri(Function.identity(), 100);  <span style="color: #a89984;">//  </span><span style="color: #a89984;">&#8658;  &#931;&#185;&#8304;&#8304;&#7522;&#8332;&#8320; i = 5050</span>
</pre>
</div>

</div>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Exercise! Why does the following code work?</h3>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">int</span> <span style="color: #076678;">tri</span> = 100;
tri(Function.identity(), tri); <span style="color: #a89984;">//  </span><span style="color: #a89984;">&#8658; 5050</span>

<span style="color: #b57614;">Function</span>&lt;<span style="color: #b57614;">Integer</span>, <span style="color: #b57614;">Integer</span>&gt; <span style="color: #076678;">tri</span> = x -&gt; x;
tri(tri, 100); <span style="color: #a89984;">//  </span><span style="color: #a89984;">&#8658; 5050</span>
</pre>
</div>

</div>

<p>
In Java, everything is an object! (Ignoring primitives, which exist for the purposes of efficiency!)
As such, functions are also objects! Which means, they must have a type: Either some class (or some interface), but which
one? The arrow literal notation <code>x -&gt; e</code> <b>is a short-hand</b> for an implementation of an interface with one abstract
method&#x2026;
</p>
</div>
</div>

<div id="outline-container-org959632a" class="outline-2">
<h2 id="org959632a"><span class="section-number-2">5.</span> <a href="#org959632a">Lambdas are a shorthand for classes that implement functional interfaces</a></h2>
<div class="outline-text-2" id="text-5">
<p>
Let's take a more theoretical look at anonymous functions.
</p>
</div>
<div id="outline-container-org171a931" class="outline-3">
<h3 id="org171a931"><span class="section-number-3">5.1.</span> <a href="#org171a931">Functional Interfaces</a></h3>
<div class="outline-text-3" id="text-5-1">
<p>
A <i>lambda expression</i> is a (shorthand) implementation of the only abstract method
in a <i>functional interface</i> ——–which is an interface that has exactly one abstract
method, and possibly many default methods.
</p>

<p>
For example, the following interface is a functional interface: It has only one abstract method.
</p>
<div class="org-src-container">
<pre class="src src-java">  <span style="color: #98971a; font-weight: bold;">public</span> <span style="color: #98971a; font-weight: bold;">interface</span> <span style="color: #b57614;">Predicate</span>&lt;<span style="color: #b57614;">T</span>&gt; {

      <span style="color: #b57614;">boolean</span> <span style="color: #076678;">test</span>(<span style="color: #b57614;">T</span> <span style="color: #076678;">t</span>);  <span style="color: #a89984;">// </span><span style="color: #a89984;">This is the abstract method</span>

      <span style="color: #a89984;">// </span><span style="color: #a89984;">Other non-abstract methods.</span>
      <span style="color: #98971a; font-weight: bold;">default</span> <span style="color: #b57614;">Predicate</span>&lt;<span style="color: #b57614;">T</span>&gt; <span style="color: #076678;">and</span>(<span style="color: #b57614;">Predicate</span>&lt;? <span style="color: #98971a; font-weight: bold;">super</span> <span style="color: #b57614;">T</span>&gt; <span style="color: #076678;">other</span>) { ... }
      <span style="color: #a89984;">// </span><span style="color: #a89984;">Example usage: nonNull.and(nonEmpty).and(shorterThan5)</span>
      <span style="color: #98971a; font-weight: bold;">static</span> &lt;<span style="color: #b57614;">T</span>&gt; <span style="color: #b57614;">Predicate</span>&lt;<span style="color: #b57614;">T</span>&gt; <span style="color: #076678;">isEqual</span>(<span style="color: #b57614;">T</span> <span style="color: #076678;">target</span>) {...}
      <span style="color: #a89984;">// </span><span style="color: #a89984;">Example usage: Predicate.isEqual("Duke") is a new predicate to use.</span>
  }
</pre>
</div>

<p>
Optionally, to ensure that this is indeed a functional interface, i.e., it has
only one abstract method, we can place <code>@FunctionalInterface</code> above its
declaration. Then the complier will check our intention for us.
</p>
</div>
</div>
<div id="outline-container-org3bfa19f" class="outline-3">
<h3 id="org3bfa19f"><span class="section-number-3">5.2.</span> <a href="#org3bfa19f">The Type of a Lambda</a></h3>
<div class="outline-text-3" id="text-5-2">
<p>
Anyhow, since a lambda is a shorthand implementation of an interface, this means
that what you can do with a lambda depenends on the interface it's impementing!
</p>

<p>
As such, when you see a lambda it's important to know it's type is not "just a function"!
This mean <b>to run/apply/execute a lambda variable</b> you need to remember that the variable
is technically an object implementing a specific functional interface, which has a single
<i>named</i> abstract method (which is implemented by the lambda) and so we need to invoke that
method on our lambda variable to actually run the lambda. For example,
</p>
<div class="org-src-container">
<pre class="src src-java">  <span style="color: #b57614;">Predicate</span>&lt;<span style="color: #b57614;">String</span>&gt; <span style="color: #076678;">f</span> = s -&gt; s.length() == 3;   <span style="color: #a89984;">// </span><span style="color: #a89984;">Make a lambda variable</span>
  <span style="color: #b57614;">boolean</span> <span style="color: #076678;">isLength3String</span> = f.test(<span style="color: #689d6a;">"hola"</span>);     <span style="color: #a89984;">// </span><span style="color: #a89984;">Actually invoke it.</span>
</pre>
</div>

<p>
Since different lambdas may implement different interfaces, the actually method
to run the lambda will likely be different! Moreover, you can invoke <i>any</i> method
on the interface that the lambda is implementing. After-all, a lambda is an object; not just a function.
</p>

<p>
Moreover, <code>Function</code> has useful methods: Such as <code>andThen</code> for composing functions sequentially,
and <code>Function.identity</code> for the do-nothing function.
</p>
</div>
</div>
<div id="outline-container-org71b2b9d" class="outline-3">
<h3 id="org71b2b9d"><span class="section-number-3">5.3.</span> <a href="#org71b2b9d">Common Java Functional Types</a></h3>
<div class="outline-text-3" id="text-5-3">
<p>
Anyhow, <a href="https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/function/package-summary.html">Java has ~40 functional interfaces</a>, which are essentially useful variations around the following 4:
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Class</th>
<th scope="col" class="org-left">runner</th>
<th scope="col" class="org-left">Description &amp; example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">  <code class="src src-java"><span style="color: #98971a; font-weight: bold;">Supplier<T></span></code></td>
<td class="org-left"><code>get</code></td>
<td class="org-left">Makes objects for us; e.g.,   <code class="src src-java"><span style="color: #98971a; font-weight: bold;">() -> "Hello"!</span></code>.</td>
</tr>

<tr>
<td class="org-left">  <code class="src src-java"><span style="color: #98971a; font-weight: bold;">Consumer<T></span></code></td>
<td class="org-left"><code>accept</code></td>
<td class="org-left">Does stuff with our objects, returning void;</td>
</tr>

<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">e.g.,   <code class="src src-java"><span style="color: #98971a; font-weight: bold;">s -> System.out.println(s)</span></code>.</td>
</tr>

<tr>
<td class="org-left">  <code class="src src-java"><span style="color: #98971a; font-weight: bold;">Predicate<T></span></code></td>
<td class="org-left"><code>test</code></td>
<td class="org-left">Tests our object for some property, returning a boolean</td>
</tr>

<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">e.g.,   <code class="src src-java"><span style="color: #98971a; font-weight: bold;">s -> s.length() == 3</span></code></td>
</tr>

<tr>
<td class="org-left">  <code class="src src-java"><span style="color: #98971a; font-weight: bold;">Function<T</span></code></td>
<td class="org-left"><code>apply</code></td>
<td class="org-left">Takes our object and gives us a new one; e.g.,   <code class="src src-java"><span style="color: #98971a; font-weight: bold;">s -> s.length()</span></code></td>
</tr>
</tbody>
</table>

<p>
For example, <code class="src src-java">&#119966;::<span style="color: #98971a; font-weight: bold;">new</span></code> is a supplier for the
class 𝒞, and the <a href="https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/Iterable.html#forEach(java.util.function.Consumer)">forEach</a> method on iterables actually uses a consumer
lambda, and a supplier can be used to <a href="https://stackoverflow.com/questions/36255007/is-there-any-way-to-reuse-a-stream">reuse streams</a> (discussed below).
</p>

<p>
The remaining Java functional interfaces are variations on these 4
that are optimised for primitive types, or have different number of
inputs as functions. For example, <code>UnaryOperator&lt;T&gt;</code> is essentially
<code>Function&lt;T, T&gt;</code>, and <code>BiFunction&lt;A, B, C&gt;</code> is essentially
<code>Function&lt;A, Function&lt;B, C&gt;&gt;</code> ———not equivalent, but essentially the
same thing.
</p>

<ul class="org-ul">
<li>As another example, Java has a <code>TriConsumer</code> which is the type of functions that have 3 inputs and no outputs
&#x2014;since <code>Tri</code> means 3, as in <i>tricycle</i>.</li>
</ul>
</div>
</div>
<div id="outline-container-org6bcaf42" class="outline-3">
<h3 id="org6bcaf42"><span class="section-number-3">5.4.</span> <a href="#org6bcaf42">Eta Reduction: Writing Lambda Expressions as Method References</a></h3>
<div class="outline-text-3" id="text-5-4">
<p>
Lambdas can sometimes be simplified by using <i>method reference</i>:
</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" />

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Method type</th>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">&#xa0;</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Static</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">\((x,ys) → τ.f(x, ys)\)</td>
<td class="org-left">≈</td>
<td class="org-left">\(τ::f\)</td>
</tr>

<tr>
<td class="org-left">Instance</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">\((x,ys) → x.f(ys)\)</td>
<td class="org-left">≈</td>
<td class="org-left">\(τ::f\), where τ is the type of \(x\)</td>
</tr>

<tr>
<td class="org-left">Constructor</td>
<td class="org-left">&#xa0;</td>
<td class="org-left"><code>args → new τ&lt;A&gt;(args)</code></td>
<td class="org-left">≈</td>
<td class="org-left"><code>τ&lt;A&gt;::new</code></td>
</tr>
</tbody>
</table>

<p>
For example, <code class="src src-java">(sentence, word) -&gt; sentence.indexOf(word)</code> is the same
as <code class="src src-java">String::indexOf</code>. Likewise, <code class="src src-java">(a, b) -&gt; Integer.max(a, b)</code> is just <code class="src src-java">Integer::max</code>.
</p>

<ul class="org-ul">
<li>Note that a class name τ might be qualified; e.g., <code class="src src-java">x -&gt; System.out.println(x)</code> is just <code class="src src-java">System.out::println</code>.</li>
</ul>
</div>
</div>
</div>

<div id="outline-container-orge747dad" class="outline-2">
<h2 id="orge747dad"><span class="section-number-2">6.</span> <a href="#orge747dad">Variable Bindings</a></h2>
<div class="outline-text-2" id="text-6">
<p>
Let's declare some new names, and assert what we know about them.
</p>
<div class="org-center">
<p>
<code class="src src-java"><span style="color: #b57614;">Integer</span> <span style="color: #076678;">x</span>, <span style="color: #076678;">y</span> = 1, <span style="color: #076678;">z</span>;</code>
</p>

<p>
<code class="src src-java"><span style="color: #b57614;">assert</span> <span style="color: #076678;">x</span> == <span style="color: #076678; font-weight: bold;">null</span> &amp;&amp; y == 1 &amp;&amp; z == <span style="color: #076678; font-weight: bold;">null</span>;</code>
</p>
</div>

<p>

<code>τ x₀ = v₀, …, xₙ = vₙ;</code> introduces 𝓃-new names <code>xᵢ</code> each having value <code>vᵢ</code> of type τ.
</p>
<ul class="org-ul">
<li>The <code>vᵢ</code> are optional, defaulting to <code class="src src-java"> 0, <span style="color: #076678; font-weight: bold;">false</span>,</code> <code>'\000'</code>,
<code class="src src-java"><span style="color: #076678; font-weight: bold;">null</span> </code> for numbers, booleans, characters, and
object types, respectively.</li>
<li>Later we use <code>xᵢ = wᵢ;</code> to update the name <code>xᵢ</code> to refer to a new value
<code>wᵢ</code>.</li>
</ul>


<hr>
<div style="column-rule-style: solid black;column-count: 2;">
<p>
There are a variety of update statements:
Suppose \(τ\) is the type of \(x\) then,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Augment:  <code>x ⊕= y  ≈  x = (τ)(x ⊕ y)</code></td>
</tr>

<tr>
<td class="org-left">Increment:   <code>x++  ≈  x += 1)</code></td>
</tr>

<tr>
<td class="org-left">Decrement:  <code>x--  ≈  x -= 1)</code></td>
</tr>
</tbody>
</table>

<p>
The operators <code>--</code> and <code>++</code> can appear <i>before or after</i> a name:
Suppose \(𝒮(x)\) is a statement mentioning the name \(x\), then
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>𝒮(x++)  ≈  𝒮(x); x += 1</code></td>
</tr>

<tr>
<td class="org-left"><code>𝒮(++x)  ≈  x += 1; 𝒮(x)</code></td>
</tr>
</tbody>
</table>


</div>

<p>
Since compound assignment is really an <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.26.2">update with a <i>cast</i></a>,
there could be unexpected behaviour when \(x\) and \(y\) are not both
ints/floats.
</p>

<hr>

<ul class="org-ul">
<li>If we place the keyword <code class="src src-java"><span style="color: #98971a; font-weight: bold;">final</span></code> before the type τ,
then the names are constant: They can appear only once on the right side of an ‘=’,
and any further occurrences (i.e., to change their values) crash the program.
<code class="src src-java"> <span style="color: #98971a; font-weight: bold;">final</span> <span style="color: #b57614;">int</span> <span style="color: #076678;">x</span> = 1, <span style="color: #076678;">y</span>; y = 3; </code> is fine, but changing the
second <code>y</code> to an <code>x</code> fails.</li>

<li>We may use <code class="src src-java"><span style="color: #b57614;">var</span> <span style="color: #076678;">x</span> = v</code>, for only <i>one</i>
declaration, to avoid writing the name of the type τ (which may be
lengthy). Java then <i>infers</i> the type by inspecting the shape of
<code>v</code>.</li>

<li><p>
Chained assignments associate to the right:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>a += b /= 2 * ++c;</code></td>
<td class="org-left"> ≈ </td>
<td class="org-left"><code>a += (b /= (2 * ++c));</code></td>
</tr>
</tbody>
</table>
<p>
(The left side of an “=”, or “⊕=”, must a single name!)
</p></li>
</ul>
</div>
</div>
<div id="outline-container-orga1624f4" class="outline-2">
<h2 id="orga1624f4"><span class="section-number-2">7.</span> <a href="#orga1624f4">Scope, Statements, and Control Flow</a></h2>
<div class="outline-text-2" id="text-7">
<div style="column-rule-style: none nil;column-count: 2;">
<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">var</span> <span style="color: #076678;">x</span> = 1;

{ <span style="color: #a89984;">// </span><span style="color: #a89984;">new local scope</span>
  <span style="color: #b57614;">var</span> <span style="color: #076678;">x</span> = 200; <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8220;shadows&#8221; top x</span>
  <span style="color: #b57614;">var</span> <span style="color: #076678;">y</span> = 300;
  assert x + y == 500;
}

<span style="color: #a89984;">// </span><span style="color: #a89984;">y is not visible here</span>
<span style="color: #b57614;">assert</span> <span style="color: #076678;">y</span> == 20; <span style="color: #a89984;">// </span><span style="color: #a89984;">CRASH!</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">The top-most x has not changed</span>
<span style="color: #b57614;">assert</span> <span style="color: #076678;">x</span> == 1;
</pre>
</div>

<p>
⊙ Each binding has a scope, which is the part of the program in which
the binding is visible.
</p>

<p>
⊙ <i>local bindings</i> are defined within a block and can only be referenced in it.
</p>


<p>
⊙ Names within a block /shadow//hide bindings with the same name.
</p>

</div>


<p>
Besides the assignment statement, we also have the following statements:
</p>
<ul class="org-ul">
<li>Blocks: If <code>Sᵢ</code> are statements, then <code>{S₀; …; Sₙ;}</code> is a statement.</li>
<li>Conditionals: <code class="src src-java"><span style="color: #98971a; font-weight: bold;">if</span> (condition) S&#8321; <span style="color: #98971a; font-weight: bold;">else</span> S&#8322;</code></li>
<li><p>
The “for-each” syntax applies to iterable structures
&#x2014;we will define our own later.
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #a89984;">// </span><span style="color: #a89984;">Print all the elements in the given list.</span>
<span style="color: #98971a; font-weight: bold;">for</span> (<span style="color: #b57614;">var</span> <span style="color: #076678;">x</span> : List.of(1, 2, 3))
   System.out.printf(<span style="color: #689d6a;">"x &#8776; %s\n"</span>, x);
</pre>
</div></li>

<li><p>
While-Loops <code class="src src-java">&#160;<span style="color: #98971a; font-weight: bold;">while</span> (condition) S&#160;</code> and for-loops
<code class="src src-java">&#160;<span style="color: #98971a; font-weight: bold;">for</span>(init; cond; change) body&#160;</code>.
</p>

<div class="org-src-container">
<pre class="src src-java">   <span style="color: #b57614;">var</span> <span style="color: #076678;">i</span> = 0; <span style="color: #98971a; font-weight: bold;">while</span> (i &lt; 10) System.out.println(Math.pow(2, i++));
&#8776;
   <span style="color: #98971a; font-weight: bold;">for</span>(<span style="color: #b57614;">var</span> <span style="color: #076678;">i</span> = 0; i &lt; 10; i++) System.out.println(Math.pow(2, i));
</pre>
</div>

<p>
Exit the current loop with the <code class="src src-java"><span style="color: #98971a; font-weight: bold;">break</span>;</code>
 statement.  Similarly, the <code class="src src-java"><span style="color: #98971a; font-weight: bold;">continue</span>;</code>
 statement jumps out of the body and continues with the next
 iteration of the loop.
</p></li>
</ul>
</div>
</div>
<div id="outline-container-orga1f6302" class="outline-2">
<h2 id="orga1f6302"><span class="section-number-2">8.</span> <a href="#orgb665556"><code class="src src-java"><span style="color: #98971a; font-weight: bold;">switch</span></code></a></h2>
<div class="outline-text-2" id="text-8">
<p>
Dispatching on a value with switch
</p>

<div style="column-rule-style: none nil;column-count: 2;">
<p>
<b>⟦Switch Statement⟧</b>
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #98971a; font-weight: bold;">switch</span> (x){
  <span style="color: #98971a; font-weight: bold;">case</span> v&#8321;: S&#8321;
  &#8942;
  <span style="color: #98971a; font-weight: bold;">case</span> v&#8345;: S&#8345;
  <span style="color: #98971a; font-weight: bold;">default</span>: S&#8345;
}
</pre>
</div>

<p>
The <code class="src src-java"><span style="color: #98971a; font-weight: bold;">switch</span></code> works as follows:
Find the <i>first</i> 𝒾 with <code>x == vᵢ</code>, then execute
<code>{Sᵢ; ⋯; Sₘ;}</code>, if there is no such 𝒾, execute the
default statement <code>Sₙ</code>. Where <code>Sₘ</code> is the first
statement after <code>Sᵢ</code> that ends with <code>break;</code>.
</p>


</div>

<p>
E.g., <code>case v: S; case w: S′; break</code>
means do <code>S;S′</code> if we see <code>v</code>
but we do <code>S′</code>
when seeing both <code>v</code> and <code>w</code>.
</p>

<div class="org-src-container">
<pre class="src src-java"><span style="color: #98971a; font-weight: bold;">switch</span> (2){
  <span style="color: #98971a; font-weight: bold;">case</span> 0: System.out.println(0);
  <span style="color: #98971a; font-weight: bold;">case</span> 1: System.out.println(1);
  <span style="color: #98971a; font-weight: bold;">case</span> 2: System.out.println(2);
  <span style="color: #98971a; font-weight: bold;">default</span>: System.out.println(-1);
} <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; Outputs: 2 -1</span>
</pre>
</div>

<hr>

<p>
<b>⟦Switch Expression⟧</b>
If we want to perform case analysis <i>without the fall-over behaviour</i>, we use
arrows ‘→’ instead of colons ‘:’.
</p>
<div class="org-src-container">
<pre class="src src-java">   <span style="color: #98971a; font-weight: bold;">switch</span> (2){
     <span style="color: #98971a; font-weight: bold;">case</span> 0 -&gt; 0;
     <span style="color: #98971a; font-weight: bold;">case</span> 1 -&gt; 1;
     <span style="color: #98971a; font-weight: bold;">case</span> 2 -&gt; 2;
     <span style="color: #98971a; font-weight: bold;">default</span> -&gt; -1;
   } <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; 2</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-orgcb213b0" class="outline-2">
<h2 id="orgcb213b0"><span class="section-number-2">9.</span> <a href="#orgcb213b0">Strings</a></h2>
<div class="outline-text-2" id="text-9">
<p>
Any pair of matching double-quotes will produce a string literal
&#x2014;whereas single-quote around a single character produce a
<code class="src src-java"><span style="color: #b57614;">char</span></code>acter value. For multi-line strings, use
triple quotes, <code>"""</code>, to produce <i>text blocks</i>.
</p>

<p>
String interpolation can be done with <code>String.format</code> using <code>%s</code>
placeholders.  For advanced interpolation, such as positional
placeholders, use <a href="https://docs.oracle.com/javase/8/docs/api/java/text/MessageFormat.html">MessageFormat</a>.
</p>

<div class="org-src-container">
<pre class="src src-java">String.format(<span style="color: #689d6a;">"Half of 100 is %s"</span>, 100 / 2) <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; "Half of 100 is 50"</span>
</pre>
</div>

<ul class="org-ul">
<li><code>s.repeat(𝓃)</code> ≈ Get a new string by gluing 𝓃-copies of the string 𝓈.</li>
<li><code>s.toUpperCase()</code> and <code>s.toLowerCase()</code> to change case.</li>
<li>Trim removes spaces, newlines, tabs, and other whitespace from the start and
end of a string.
E.g., <code class="src src-java"><span style="color: #689d6a;">"  okay \n "</span>.trim().equals(<span style="color: #689d6a;">"okay"</span>)</code></li>
<li><code>s.length()</code> is the number of characters in the string.</li>
<li><code>s.isEmpty()  ≡  s.length() == 0</code></li>
<li><code>s.isBlank()  ≡  s.trim().isEmpty()</code></li>
<li><code>String.valueOf(x)</code> gets a string representation of anything <code>x</code>.</li>
<li><code>s.concat(t)</code> glues together two strings into one longer string; i.e., <code>s + t</code>.</li>
</ul>
</div>
</div>

<div id="outline-container-orgb6f0c55" class="outline-2">
<h2 id="orgb6f0c55"><span class="section-number-2">10.</span> <a href="#orgb6f0c55">Equality</a></h2>
<div class="outline-text-2" id="text-10">
<ul class="org-ul">
<li>In general, ‘==’ is used to check two primitives for equality, whereas
<code>.equals</code> is used to check if two objects are equal.</li>

<li>The equality operator ‘==’ means “two things are indistinguishable:
They evaluate to the same literal value, or refer to the same place in memory”.</li>

<li>As a method, <code>.equals</code> can be redefined to obtain a suitable notion
of equality between objects; e.g., “two people are the same if they
have the same name (regardless of anything else)”.  If it's not
redefined, <code>.equals</code> behaves the same as ‘==’.  In contrast, Java
does not support operator overloading and so ‘==’ cannot be
redefined.</li>

<li>For strings, ‘==’ and <code>.equals</code> behave differently:
<code class="src src-java"><span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">String</span>(<span style="color: #689d6a;">"x"</span>) == <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">String</span>(<span style="color: #689d6a;">"x"</span>)</code> is false, but
<code class="src src-java"><span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">String</span>(<span style="color: #689d6a;">"x"</span>).equals(<span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">String</span>(<span style="color: #689d6a;">"x"</span>))</code> is
true!  The first checks that two things refer to the same place
in memory, the second checks that they have the same letters in the
same order.
<ul class="org-ul">
<li>If we want this kind of “two objects are equal when they have the
same contents” behaviour, we can get it for free by using
<code class="src src-java">record</code>s instead of <code class="src src-java"><span style="color: #98971a; font-weight: bold;">class</span></code>es.</li>
</ul></li>
</ul>
</div>
</div>

<div id="outline-container-org74da38d" class="outline-2">
<h2 id="org74da38d"><span class="section-number-2">11.</span> <a href="#org74da38d">Arithmetic</a></h2>
<div class="outline-text-2" id="text-11">
<p>
In addition to the standard arithmetic operations, we have <code class="src src-java">Math.max(<span style="color: #b57614;">x</span>, <span style="color: #b57614;">y</span>)</code> that takes two numbers and gives the largest; likewise
<code class="src src-java">Math.min(<span style="color: #b57614;">x</span>, <span style="color: #b57614;">y</span>)</code>.  Other common functions include
<code class="src src-java">Math.sqrt, Math.ceil, Math.round, Math.abs,</code> and
<code class="src src-java">Math.random()</code> which returns a random number between 0
and 1.  Also, use <code>%</code> for remainder after division; e.g., <code>n % 10</code> is the right-most
digit of integer \(n\), and <code class="src src-java">n % 2 == 0</code> exactly when \(n\) is
even, and <code>d % 1</code> gives the decimal points of a floating point number \(d\), and
finally: If <code>d</code> is the index of the current weekday (0..6), then <code>d + 13 % 7</code> is the
weekday 13-days from today.
</p>

<div style="column-rule-style: solid black;column-count: 2;">
<div class="org-src-container">
<pre class="src src-java"><span style="color: #a89984;">// </span><span style="color: #a89984;">Scientific notation: &#120013;e&#120014; &#8776; &#120013; &#215; 10&#696;</span>
assert 1.2e3 == 1.2 * Math.pow(10, 3)
</pre>
</div>

<div class="org-src-container">
<pre class="src src-java"><span style="color: #a89984;">// </span><span style="color: #a89984;">random integer x with 4 &#8804; x &lt; 99</span>
<span style="color: #b57614;">var</span> <span style="color: #076678;">x</span> = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Random</span>().nextInt(4, 99);
</pre>
</div>
</div>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Sum the digits of the integer $n = 31485$</h3>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">int</span> <span style="color: #076678;">n</span> = 31485;
<span style="color: #b57614;">int</span> <span style="color: #076678;">sum</span> = 0;
<span style="color: #98971a; font-weight: bold;">while</span> (n % 10 != 0) { sum += n % 10; n /= 10; }
<span style="color: #b57614;">assert</span> <span style="color: #076678;">sum</span> == 3 + 1 + 4 + 8 + 5;
</pre>
</div>

<hr />
<p>
A more elegant, “functional style”, solution:
</p>
<div class="org-src-container">
<pre class="src src-java">String.valueOf(<span style="color: #b57614;">n</span>).chars().map(c -&gt; c - <span style="color: #689d6a;">'0'</span>).sum();
</pre>
</div>


<p>
The <code>chars()</code> methods returns a stream of integers (Java
<code class="src src-java"><span style="color: #b57614;">char</span></code>acters are really just integers).
Likewise, <code class="src src-java">IntStream.range(0, 20)</code> makes a
sequence of numbers that we can then <code>map</code> over, then <code>sum, min, max, average</code>.
</p>

</div>
</div>
</div>

<div id="outline-container-orgbac0050" class="outline-2">
<h2 id="orgbac0050"><span class="section-number-2">12.</span> <a href="#orgbac0050">Collections and Streams</a></h2>
<div class="outline-text-2" id="text-12">
<p>
<i>Collections</i> are types that hold a bunch of similar data: Lists,
Sets, and Maps are the most popular. <i>Streams</i> are pipelines for
altering collections: Usually one has a collection, converts it to a
stream by invoking <code>.stream()</code>, then performs <code>map</code> and <code>filter</code>
methods, etc, then “collects” (i.e., runs the stream pipeline to get
an actual collection value back) the result.
</p>

<hr>

<p>
<b>Lists are ordered collections, that care about multiplicity</b>.  Lists
are made with <code>List.of(x₀, x₁, …, xₙ)</code>.  Indexing, <code>xs.get(𝒾)</code>, yields
the 𝒾-th element from the start; i.e., the number of items to skip;
whence <code>xs.get(0)</code> is the first element.
</p>

<p>
<b>Sets are unordered collections, that ignore multiplicity</b>. Sets are
made with <code>Set.of(x₀, x₁, …, xₙ)</code>.
</p>

<p>
<b>Maps are pairs of ‘keys’ along with ‘values’.</b> <code>Map&lt;K, V&gt;</code> is
essentially the class of objects that have no methods but instead have
an arbitary number of properties (the ‘keys’ of type <code>K</code>), where each
property has a value of type <code>V</code>.  Maps are made with <code>Map.of(k₀, v₀,
…, k₁₀, v₁₀)</code> by explicitly declaraing keys and their associated
values.  The method <code>ℳ.get(k)</code> returns the value to which the
specified key <code>k</code> is mapped, or <code>null</code> if the map ℳ contains no
mapping for the key. Maps have an <code>entrySet()</code> method that gives a set
of key-value pairs, which can then be converted to a stream, if need
be.
</p>

<hr>

<p>
Other collection methods include, for a collection instance 𝒞:
</p>
<ul class="org-ul">
<li><code class="src src-java">&#119966;.size()</code> is the number of elements in the collection</li>
<li><code class="src src-java">&#119966;.isEmpty()</code>  ≡  <code class="src src-java">&#119966;.size() == 0</code></li>
<li><code class="src src-java">&#119966;.contains(<span style="color: #b57614;">e</span>)</code>  ≡  <code class="src src-java">&#119966;.stream().filter(x -&gt; x.equals(e)).count() &gt; 0</code></li>
<li><code class="src src-java">Collections.fill(<span style="color: #b57614;">&#8466;</span>, <span style="color: #b57614;">e</span>)</code>  ≅  <code class="src src-java">&#8466;.stream().map(_ -&gt; e).toList()</code>;
i.e., copy list <code>ℒ</code> but replace all elements with <code>e</code>.</li>
<li><code class="src src-java">Collections.frequency(<span style="color: #b57614;">&#119966;</span>, <span style="color: #b57614;">e</span>)</code>
counts how many times <code>e</code> occurs in a collection.</li>
<li><code class="src src-java">Collections.max(<span style="color: #b57614;">&#119966;</span>)</code> is the largest value in a collection; likewise <code>min</code>.</li>
<li><code class="src src-java">Collections.nCopies(<span style="color: #b57614;">n</span>, <span style="color: #b57614;">e</span>)</code> is a list of \(n\) copies of <code>e</code>.</li>
</ul>

<hr>

<p>
<b><code class="src src-java"><span style="color: #b57614;">Stream</span>&lt;<span style="color: #b57614;">&#964;</span>&gt;</code> methods</b>
</p>
<ul class="org-ul">
<li><code class="src src-java">Stream.of(x&#8320;, ..., <span style="color: #b57614;">x&#8345;</span>)</code> makes a stream of data, of type τ, ready to be acted on.</li>
<li><code class="src src-java">s.map(<span style="color: #b57614;">f</span>)</code> changes the elements according to a function \(f : τ → τ′\).
<ul class="org-ul">
<li><code class="src src-java">s.flatMap(<span style="color: #b57614;">f</span>)</code> transforms each element into a stream since \(f : τ → Stream<τ′>\), then the resulting
stream-of-streams is flattened into a single sequential stream.</li>
<li>As such, to merge a streams of streams just invoke <code>.flatMap(s -&gt; s)</code>.</li>
</ul></li>
<li><code class="src src-java">s.filter(<span style="color: #b57614;">p</span>)</code> keeps only the elements that satisfy property <code>p</code></li>
<li><code class="src src-java">s.count()</code> is the number of elements in the stream</li>
<li><code class="src src-java">s.allMatch(<span style="color: #b57614;">p</span>)</code> tests if all elements  satisfy the <a href="https://download.java.net/java/early_access/panama/docs/api/java.base/java/util/function/Predicate.html">predicate</a> <code>p</code></li>
<li><code class="src src-java">s.anyMatch(<span style="color: #b57614;">p</span>)</code> tests if any element satisfies <code>p</code></li>
<li><code class="src src-java">s.noneMatch(<span style="color: #b57614;">p</span>)</code>  ≡  <code class="src src-java">s.allMatch(p.negate())</code></li>
<li><code class="src src-java">s.distinct()</code> drops all duplicates</li>
<li><code class="src src-java">s.findFirst()</code> returns an <code class="src src-java"><span style="color: #b57614;">Optional</span>&lt;<span style="color: #b57614;">&#964;</span>&gt;</code> denoting the first element, if any.</li>
<li><code class="src src-java">s.forEach(<span style="color: #b57614;">a</span>)</code> to loop over the elements and perform action <code>a</code>.
<ul class="org-ul">
<li>If you want to do some action, and get the stream <code>s</code> back for further use,
then use <code class="src src-java">s.peek(<span style="color: #b57614;">a</span>)</code>.</li>
</ul></li>
</ul>
</div>
</div>

<div id="outline-container-org32de5b6" class="outline-2">
<h2 id="org32de5b6"><span class="section-number-2">13.</span> <a href="#org32de5b6">Generics</a></h2>
<div class="outline-text-2" id="text-13">
<p>
Java only lets us return a single value from a method, what if we want
to return a pair of values? Easy, let's declare <code>record Pair(Object
first, Object second) { }</code> and then return <code>Pair</code>. This solution has
the same problem as methods that just return <code>Object</code>: It communicates
essentially no information &#x2014;after all, <i>everything is an object!</i>&#x2014;
and so requires dangerous casts to be useful, and the compiler wont
help me avoid type mistakes.
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">record</span> <span style="color: #076678;">Pair</span>(<span style="color: #b57614;">Object</span> <span style="color: #076678;">first</span>, <span style="color: #b57614;">Object</span> <span style="color: #076678;">second</span>) { }

<span style="color: #a89984;">// </span><span style="color: #a89984;">This should return an integer and a string</span>
<span style="color: #b57614;">Pair</span> <span style="color: #076678;">myMethod</span>() { <span style="color: #98971a; font-weight: bold;">return</span> <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Pair</span>(<span style="color: #689d6a;">"1"</span>, <span style="color: #689d6a;">"hello"</span>); } <span style="color: #a89984;">// </span><span style="color: #a89984;">Oops, I made a typo!</span>

<span style="color: #b57614;">int</span> <span style="color: #076678;">num</span> = (<span style="color: #b57614;">int</span>) (myMethod().first()); <span style="color: #a89984;">// </span><span style="color: #a89984;">BOOM!</span>
</pre>
</div>

<p>
It would be better if we could say “this method returns a pair of an integer and a string”, for example.
We can do just that with <i>generics</i>!
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">record</span> <span style="color: #076678;">Pair&lt;</span><span style="color: #b57614;">A</span><span style="color: #076678;">, </span><span style="color: #b57614;">B</span><span style="color: #076678;">&gt;</span>(<span style="color: #b57614;">A</span> <span style="color: #076678;">first</span>, <span style="color: #b57614;">B</span> <span style="color: #076678;">second</span>) { }

<span style="color: #b57614;">Pair</span>&lt;<span style="color: #b57614;">Integer</span>, <span style="color: #b57614;">String</span>&gt; <span style="color: #076678;">myMethod</span>() { <span style="color: #98971a; font-weight: bold;">return</span> <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Pair</span>&lt;&gt;(1, <span style="color: #689d6a;">"hello"</span>); }

<span style="color: #b57614;">int</span> <span style="color: #076678;">num</span> = myMethod().first();
</pre>
</div>
<p>
This approach <i>communicates to the compiler my intentions</i> and so the compiler ensures I don't make any silly typos.
Such good communication also means no dangerous casts are required.
</p>

<p>
We can use the new type in three ways:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>Pair&lt;A, B&gt;</code></td>
<td class="org-left">explicitly providing the types we want to use <code>Pair</code> with</td>
</tr>

<tr>
<td class="org-left"><code>Pair&lt;&gt;</code></td>
<td class="org-left">letting Java <i>infer, guess,</i> the types for <code>Pair</code> by how we use it</td>
</tr>

<tr>
<td class="org-left"><code>Pair</code></td>
<td class="org-left">defaulting the types to all be <code>Object</code></td>
</tr>
</tbody>
</table>

<p>
The final option is not recommended, since it looses type information. It's only allowed
since older versions of Java do not have type parameters and so, at run time, all type
parameters are ‘erased’. That is, <i>type parameters only exist at compile time and so cannot
be inspected/observed at run-time.</i>
</p>
</div>
]]></description>
  <category><![CDATA[java]]></category>
  <category><![CDATA[cheat-sheet]]></category>
  <link>https://alhassy.github.io/java-cheat-sheet.html</link>
  <guid>https://alhassy.github.io/java-cheat-sheet.html</guid>
  <pubDate>Tue, 28 Nov 2023 06:52:09 -0500</pubDate>
</item>
<item>
  <title><![CDATA[💐 Repl Driven Development: /Editor Integrated REPLs for all languages/ 🔁]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-repl-driven-development.html"><img src="https://img.shields.io/badge/-repl_driven_development-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-vscode.html"><img src="https://img.shields.io/badge/-vscode-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-emacs.html"><img src="https://img.shields.io/badge/-emacs-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-javascript.html"><img src="https://img.shields.io/badge/-javascript-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-java.html"><img src="https://img.shields.io/badge/-java-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-python.html"><img src="https://img.shields.io/badge/-python-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-lisp.html"><img src="https://img.shields.io/badge/-lisp-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-clojure.html"><img src="https://img.shields.io/badge/-clojure-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-haskell.html"><img src="https://img.shields.io/badge/-haskell-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-arend.html"><img src="https://img.shields.io/badge/-arend-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-purescript.html"><img src="https://img.shields.io/badge/-purescript-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-idris.html"><img src="https://img.shields.io/badge/-idris-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-racket.html"><img src="https://img.shields.io/badge/-racket-grey?logo=nil"></a></center>
<center><a href="../images/rdd-benefits.png" class="tooltip" title="Image credit “../images/rdd-benefits.png”"><img src="../images/rdd-benefits.png" alt="Article image" style="border: 2px solid black;" width="350" height="350" align="top"/></a></center>

<div id="table-of-contents" role="doc-toc">
<h2> <a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a> </h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#A-Rapid-Overview-of-RDD">1. <a href="#A-Rapid-Overview-of-RDD">A Rapid Overview of RDD</a></a>
<ul>
<li><a href="#How-do-people-usually-code-️">1.1. <a href="#How-do-people-usually-code-️">How do people usually code? 🌤️</a></a></li>
<li><a href="#If-only-we-could-have-our-cake-and-eat-it-too">1.2. <a href="#If-only-we-could-have-our-cake-and-eat-it-too">If only we could have our cake, and eat it too! 🍰</a></a></li>
<li><a href="#Technically-speaking-how-is-Emacs-itself-the-REPL">1.3. <a href="#Technically-speaking-how-is-Emacs-itself-the-REPL">Technically speaking, how is Emacs <i>itself</i> the REPL? 🤔</a></a></li>
<li><a href="#Summarising-Evaluator-Pitch">1.4. <a href="#Summarising-Evaluator-Pitch">🛗 Summarising Evaluator Pitch ⚾</a></a></li>
<li><a href="#Features-of-RDD-el">1.5. <a href="#Features-of-RDD-el">🤖 💪 🤖 Features of RDD.el 💪 🤖 💪</a></a></li>
</ul>
</li>
<li><a href="#Teaching-a-runtime-green-incrementally-to-be-a-web-server-️">2. <a href="#Teaching-a-runtime-green-incrementally-to-be-a-web-server-️">Teaching a runtime, <b>green:incrementally</b>, to be a web server 🍽️ 🔁 🤖</a></a></li>
<li><a href="#Concluding-Remarks">3. <a href="#Concluding-Remarks">Concluding Remarks</a></a></li>
<li><a href="#Appendix-Interesting-Reads"><a href="#Appendix-Interesting-Reads">Appendix: Interesting Reads</a></a></li>
<li><a href="#Appendix-Recipes-for-a-number-of-languages"><a href="#Appendix-Recipes-for-a-number-of-languages">Appendix: Recipes for a number of languages</a></a></li>
</ul>
</div>
</div>

<div class="abstract" style="border: 1px solid black;padding: 1%; margin-top: 1%; margin-bottom: 1%;margin-right: 10%; margin-left: 10%; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>
<div class="org-center">
<p>
<img src="https://img.shields.io/badge/Warning-Incomplete_DRAFT-red?logo=codeigniter">
</p>
</div>

<p>
The <a href="https://melpa.org/#/repl-driven-development"><img alt="MELPA" src="https://img.shields.io/badge/repl--driven--development-1.0.1-green?logo=Gnu-Emacs"></img></a> package makes the philosophy of REPL Driven
Development (RDD) accessible to any language that has a primitive CLI repl: <i>The
result is an Emacs interface for the language, where code of your choosing is</i>
<i>evaluated, and results are echoed at your cursor in overlays.</i>
</p>

<p>
That is, with Repl <b><span style="color:green;">aided</span></b> development, you make software by starting with
an already working program (i.e., the repl) then <b><span style="color:green;">incrementlly</span></b> “teach it”
to be the program you want, by defining &amp; redefining things.  Until satisfied,
loop: Type/modify code <b><span style="color:green;">in your editor</span></b>, press some keys to evaluate what you
wrote/modified <i>in the currently running system</i>, and explore/test the resulting
runtime.
</p>

<p>
<i>RDD is programming emphasising fast &amp; rich feedback from a running system.</i> RDD
is fantastic for quickly <i>teaching/exploring</i> an idea; as such, the running
example of this article will be on servers &#x2014;no prior experience with servers
is assumed.
The main examples will be in JavaScript, Python, and Java.  (Since <i>JavaScript is
just Lisp in C clothing</i>, we will not discuss Lisp.)  Since Java is verbose, the
power of REPLs really pays off when exploring a new idea. We see how many
imports and setup-code simply disappear in the RDD approach, letting you focus
on the core idea you're exploring/teaching.  For comparison, a traditional
self-contained Java server program is ~30 lines long whereas the focused RDD
approach is ~4 lines long.
</p>

<div class="org-center">
<p>
<a href="https://www.gnu.org/licenses/gpl-3.0.en.html"><img src="https://img.shields.io/badge/license-GNU_3-informational?logo=read-the-docs"></a>
<a href="https://twitter.com/intent/tweet?text=This looks super neat (•̀ᴗ•́)و::&url=https://alhassy.com/repl-driven-development"><img src="https://img.shields.io/twitter/url?url=https://alhassy.com/repl-driven-development"></a>
<a href="https://www.buymeacoffee.com/alhassy"><img src="https://img.shields.io/badge/-buy_me_a_coffee-gray?logo=buy-me-a-coffee"></a>

<a href="https://github.com/alhassy/alhassy.github.io/issues"><img src="https://img.shields.io/badge/contributions-welcome-green?logo=nil"></a>
</p>
</div>

<p>
<i>tdlr:</i> This library provides the Emacs built-in <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-e ∷ eval-last-sexp<br>Evaluate sexp before point; print value in the echo area.<br>Interactively, EVAL-LAST-SEXP-ARG-INTERNAL is the prefix argument.<br>With a non ‘-’ prefix argument, print output into current buffer.<br><br>This commands handles ‘defvar’, ‘defcustom’ and ‘defface’ the<br>same way that ‘eval-defun’ does.&emsp;See the doc string of that<br>function for details.<br><br>Normally, this function truncates long output according to the<br>value of the variables ‘eval-expression-print-length’ and<br>‘eval-expression-print-level’.&emsp;With a prefix argument of zero,<br>however, there is no such truncation.<br>Integer values are printed in several formats (decimal, octal,<br>and hexadecimal).&emsp;When the prefix argument is -1 or the value<br>doesn’t exceed ‘eval-expression-print-maximum-character’, an<br>integer value is also printed as a character of that codepoint.<br><br>If ‘eval-expression-debug-on-error’ is non-nil, which is the default,<br>this command arranges for all errors to enter the debugger.<br><br>(fn EVAL-LAST-SEXP-ARG-INTERNAL)"><kbd style="border-color: red">C-x C-e</kbd></abbr> behaviour for
arbitrary languages, provided they have a primitive cli REPL.
</p>


</div>

<div id="outline-container-A-Rapid-Overview-of-RDD" class="outline-2">
<h2 id="A-Rapid-Overview-of-RDD"><span class="section-number-2">1.</span> <a href="#A-Rapid-Overview-of-RDD">A Rapid Overview of RDD</a></h2>
<div class="outline-text-2" id="text-A-Rapid-Overview-of-RDD">
</div>
<div id="outline-container-How-do-people-usually-code-️" class="outline-3">
<h3 id="How-do-people-usually-code-️"><span class="section-number-3">1.1.</span> <a href="#How-do-people-usually-code-️">How do people usually code? 🌤️</a></h3>
<div class="outline-text-3" id="text-How-do-people-usually-code-️">
<p>
Either you,
</p>

<ol class="org-ol">
<li>Use a code editor and edit multiple lines, then jump into a console to try
out what you wrote⏳, or</li>

<li>You use an interactive command line, and work with one line at a time
&#x2014;continuously editing &amp; evaluating 🔄</li>
</ol>

<p>
The first approach sucks because your code and its resulting behaviour occur in
different places 😢 The second is only realistic for small experimentation
&#x2014;after all, you're in a constrained environment and don't generally have the
same features that your code editor provides 🧟‍♂️
</p>
</div>
</div>

<div id="outline-container-If-only-we-could-have-our-cake-and-eat-it-too" class="outline-3">
<h3 id="If-only-we-could-have-our-cake-and-eat-it-too"><span class="section-number-3">1.2.</span> <a href="#If-only-we-could-have-our-cake-and-eat-it-too">If only we could have our cake, and eat it too! 🍰</a></h3>
<div class="outline-text-3" id="text-If-only-we-could-have-our-cake-and-eat-it-too">
<p>
With an <i>editor-integrated REPL</i>, we get both approaches! No need to switch
between the two any more! For example, Emacs out-of-the-box lets us just select
some code and press <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-e ∷ eval-last-sexp<br>Evaluate sexp before point; print value in the echo area.<br>Interactively, EVAL-LAST-SEXP-ARG-INTERNAL is the prefix argument.<br>With a non ‘-’ prefix argument, print output into current buffer.<br><br>This commands handles ‘defvar’, ‘defcustom’ and ‘defface’ the<br>same way that ‘eval-defun’ does.&emsp;See the doc string of that<br>function for details.<br><br>Normally, this function truncates long output according to the<br>value of the variables ‘eval-expression-print-length’ and<br>‘eval-expression-print-level’.&emsp;With a prefix argument of zero,<br>however, there is no such truncation.<br>Integer values are printed in several formats (decimal, octal,<br>and hexadecimal).&emsp;When the prefix argument is -1 or the value<br>doesn’t exceed ‘eval-expression-print-maximum-character’, an<br>integer value is also printed as a character of that codepoint.<br><br>If ‘eval-expression-debug-on-error’ is non-nil, which is the default,<br>this command arranges for all errors to enter the debugger.<br><br>(fn EVAL-LAST-SEXP-ARG-INTERNAL)"><kbd style="border-color: red">C-x C-e</kbd></abbr> ---<i>E for Evaluate!</i> 😉
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #a89984;">;; </span><span style="color: #a89984;">Press &#8220;control and x, then control and e&#8221;, at the end of the line, to run the following code</span>
(message-box <span style="color: #689d6a;">"hello world"</span>)
</pre>
</div>

<p>
The <a href="https://melpa.org/#/repl-driven-development"><img alt="MELPA" src="https://img.shields.io/badge/repl--driven--development-1.0.1-green?logo=Gnu-Emacs"></img></a> software gives us this feature <i>for any
language</i>! For example, Press <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-e ∷ eval-last-sexp<br>Evaluate sexp before point; print value in the echo area.<br>Interactively, EVAL-LAST-SEXP-ARG-INTERNAL is the prefix argument.<br>With a non ‘-’ prefix argument, print output into current buffer.<br><br>This commands handles ‘defvar’, ‘defcustom’ and ‘defface’ the<br>same way that ‘eval-defun’ does.&emsp;See the doc string of that<br>function for details.<br><br>Normally, this function truncates long output according to the<br>value of the variables ‘eval-expression-print-length’ and<br>‘eval-expression-print-level’.&emsp;With a prefix argument of zero,<br>however, there is no such truncation.<br>Integer values are printed in several formats (decimal, octal,<br>and hexadecimal).&emsp;When the prefix argument is -1 or the value<br>doesn’t exceed ‘eval-expression-print-maximum-character’, an<br>integer value is also printed as a character of that codepoint.<br><br>If ‘eval-expression-debug-on-error’ is non-nil, which is the default,<br>this command arranges for all errors to enter the debugger.<br><br>(fn EVAL-LAST-SEXP-ARG-INTERNAL)"><kbd style="border-color: red">C-x C-e</kbd></abbr> at the end of the following line to get
an Emacs-integrated REPL for Java &#x2014;i.e.,
<abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-j ∷ repl/jshell<br>Executes the selected region, if any or otherwise the entire current line,<br>and evaluates it with the command-line tool “jshell”.<br><br>Output is shown as an overlay at the current cursor position.<br>It is shown for ‘repl-driven-development/echo-duration’ many seconds.<br><br>## C-u Prefix: Insert result ###################################################<br><br>With a “C-u” prefix, the output is inserted at point<br>(and not echoed in an overlay).<br><br>## C-u C-u Prefix: Documentation ##############################################<br><br>With a “C-u C-u” prefix, documentation is looked-up for the word at point.<br><br>This is done using ‘devdocs’, and so the documentation generally provides<br>example uses as well. Visit https://devdocs.io/ to see the list of documented<br>languages and libraries.<br><br>## “C-u 0” Prefix: See associated buffer #####################################<br><br>Sometimes it may be useful to look at a large output in a dedicated buffer.<br><br>## “C-u -1” Prefix: Restart REPL #############################################<br><br>In the event you’ve messed-up your REPL, starting from a blank slate may be<br>helpful.<br><br>## Implementation Notes ########################################################<br><br>The interactive method is asynchronous: Whenever you send text for evaluation,<br>you immediately regain control in Emacs; you may send more text and it will be<br>queued for evaluation. For example, evaluating a sleep command for 3 seconds<br>does not block Emacs.<br><br>## See also ####################################################################<br><br>See ‘repl-driven-development’ for more useful docs.<br><br>See www.alhassy.com/repl-driven-development to learn more about RDD and see<br>examples and many gifs.<br>"><kbd style="border-color: red">C-x C-j</kbd></abbr> will now
evaluate a selection, or the entire line, as if it were Java code.
</p>
<ul class="org-ul">
<li>Why <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-j ∷ repl/jshell<br>Executes the selected region, if any or otherwise the entire current line,<br>and evaluates it with the command-line tool “jshell”.<br><br>Output is shown as an overlay at the current cursor position.<br>It is shown for ‘repl-driven-development/echo-duration’ many seconds.<br><br>## C-u Prefix: Insert result ###################################################<br><br>With a “C-u” prefix, the output is inserted at point<br>(and not echoed in an overlay).<br><br>## C-u C-u Prefix: Documentation ##############################################<br><br>With a “C-u C-u” prefix, documentation is looked-up for the word at point.<br><br>This is done using ‘devdocs’, and so the documentation generally provides<br>example uses as well. Visit https://devdocs.io/ to see the list of documented<br>languages and libraries.<br><br>## “C-u 0” Prefix: See associated buffer #####################################<br><br>Sometimes it may be useful to look at a large output in a dedicated buffer.<br><br>## “C-u -1” Prefix: Restart REPL #############################################<br><br>In the event you’ve messed-up your REPL, starting from a blank slate may be<br>helpful.<br><br>## Implementation Notes ########################################################<br><br>The interactive method is asynchronous: Whenever you send text for evaluation,<br>you immediately regain control in Emacs; you may send more text and it will be<br>queued for evaluation. For example, evaluating a sleep command for 3 seconds<br>does not block Emacs.<br><br>## See also ####################################################################<br><br>See ‘repl-driven-development’ for more useful docs.<br><br>See www.alhassy.com/repl-driven-development to learn more about RDD and see<br>examples and many gifs.<br>"><kbd style="border-color: red">C-x C-j</kbd></abbr>  ?  Well, <kbd style="">C-x C-“e</kbd>” for Emacs Lisp code, and <kbd style="">C-x C-“j</kbd>”
for Java code!</li>
</ul>

<div class="org-src-container">
<pre class="src src-emacs-lisp" id="orge6dc293">  <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-j now evaluates arbitrary Java code</span>
  <span style="color: #a89984;">;; </span><span style="color: #a89984;">&#10214;Emacs spawns a new &#8220;jshell&#8221; process, then &#8220;C-x C-j&#8221; sends text to that process.&#10215;</span>
  (repl-driven-development [C-x C-j] <span style="color: #689d6a;">"jshell"</span> <span style="color: #7c6f64; font-weight: bold;">:prompt</span> <span style="color: #689d6a;">"jshell&gt;"</span>)
</pre>
</div>

<p>
We can now press <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-j ∷ repl/jshell<br>Executes the selected region, if any or otherwise the entire current line,<br>and evaluates it with the command-line tool “jshell”.<br><br>Output is shown as an overlay at the current cursor position.<br>It is shown for ‘repl-driven-development/echo-duration’ many seconds.<br><br>## C-u Prefix: Insert result ###################################################<br><br>With a “C-u” prefix, the output is inserted at point<br>(and not echoed in an overlay).<br><br>## C-u C-u Prefix: Documentation ##############################################<br><br>With a “C-u C-u” prefix, documentation is looked-up for the word at point.<br><br>This is done using ‘devdocs’, and so the documentation generally provides<br>example uses as well. Visit https://devdocs.io/ to see the list of documented<br>languages and libraries.<br><br>## “C-u 0” Prefix: See associated buffer #####################################<br><br>Sometimes it may be useful to look at a large output in a dedicated buffer.<br><br>## “C-u -1” Prefix: Restart REPL #############################################<br><br>In the event you’ve messed-up your REPL, starting from a blank slate may be<br>helpful.<br><br>## Implementation Notes ########################################################<br><br>The interactive method is asynchronous: Whenever you send text for evaluation,<br>you immediately regain control in Emacs; you may send more text and it will be<br>queued for evaluation. For example, evaluating a sleep command for 3 seconds<br>does not block Emacs.<br><br>## See also ####################################################################<br><br>See ‘repl-driven-development’ for more useful docs.<br><br>See www.alhassy.com/repl-driven-development to learn more about RDD and see<br>examples and many gifs.<br>"><kbd style="border-color: red">C-x C-j</kbd></abbr> to execute any Java code, and see results echoed inline in an overlay, such as:
</p>
<div class="org-src-container">
<pre class="src src-java">  1 + 2                     <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; 3</span>
  System.out.println(2 + 3) <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; 5</span>
  <span style="color: #689d6a;">"hello"</span>[0]                <span style="color: #a89984;">// </span><span style="color: #a89984;">&#128683; Type error: Java strings are not arrays</span>

  IntStream.range(0, 100).toArray() <span style="color: #a89984;">// </span><span style="color: #a89984;">A multi-line overlay of numbers 0..99</span>

  <span style="color: #98971a; font-weight: bold;">import</span> <span style="color: #076678; font-weight: bold;">javax</span>.<span style="color: #076678; font-weight: bold;">swing</span>.*;     <span style="color: #a89984;">// </span><span style="color: #a89984;">(No output; this is an effectful operation)</span>

  <span style="color: #b57614;">var</span> <span style="color: #076678;">frame</span> = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">JFrame</span>(){{ setAlwaysOnTop(<span style="color: #076678; font-weight: bold;">true</span>); }};  <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; &#8216;frame&#8217; is defined</span>

  JOptionPane.showMessageDialog(frame, <span style="color: #689d6a;">"Super nice!"</span>);  <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; A GUI appears &#128157;</span>
</pre>
</div>

<center>
<p>
👀
<i>This extension is an easy Emacs-integrated-REPL builder for any language!</i>
😲
</p>
</center>

<p>
Learn more Java from
<a href="https://alhassy.com/java-cheat-sheet.pdf"><img src="https://img.shields.io/badge/Java-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=coffeescript"></a>
.
</p>


<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Say that again, but use Python please! 🐍 </font> </strong> </summary>
<div class="org-center">
<p>
<a href="https://alhassy.github.io/PythonCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Python-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=python"></a>
</p>
</div>

<p>
With an <i>editor-integrated REPL</i>, we get both approaches! No need to switch
between the two any more! For example, Emacs out-of-the-box lets us just select
some code and press <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-e ∷ eval-last-sexp<br>Evaluate sexp before point; print value in the echo area.<br>Interactively, EVAL-LAST-SEXP-ARG-INTERNAL is the prefix argument.<br>With a non ‘-’ prefix argument, print output into current buffer.<br><br>This commands handles ‘defvar’, ‘defcustom’ and ‘defface’ the<br>same way that ‘eval-defun’ does.&emsp;See the doc string of that<br>function for details.<br><br>Normally, this function truncates long output according to the<br>value of the variables ‘eval-expression-print-length’ and<br>‘eval-expression-print-level’.&emsp;With a prefix argument of zero,<br>however, there is no such truncation.<br>Integer values are printed in several formats (decimal, octal,<br>and hexadecimal).&emsp;When the prefix argument is -1 or the value<br>doesn’t exceed ‘eval-expression-print-maximum-character’, an<br>integer value is also printed as a character of that codepoint.<br><br>If ‘eval-expression-debug-on-error’ is non-nil, which is the default,<br>this command arranges for all errors to enter the debugger.<br><br>(fn EVAL-LAST-SEXP-ARG-INTERNAL)"><kbd style="border-color: red">C-x C-e</kbd></abbr> ---<i>E for Evaluate!</i> 😉
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #a89984;">;; </span><span style="color: #a89984;">Press &#8220;control and x, then control and e&#8221; to run the following code</span>
(message-box <span style="color: #689d6a;">"hello world"</span>)
</pre>
</div>

<p>
The <a href="https://melpa.org/#/repl-driven-development"><img alt="MELPA" src="https://img.shields.io/badge/repl--driven--development-1.0.1-green?logo=Gnu-Emacs"></img></a> software gives us this feature <i>for any
language</i>! For example, Press <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-e ∷ eval-last-sexp<br>Evaluate sexp before point; print value in the echo area.<br>Interactively, EVAL-LAST-SEXP-ARG-INTERNAL is the prefix argument.<br>With a non ‘-’ prefix argument, print output into current buffer.<br><br>This commands handles ‘defvar’, ‘defcustom’ and ‘defface’ the<br>same way that ‘eval-defun’ does.&emsp;See the doc string of that<br>function for details.<br><br>Normally, this function truncates long output according to the<br>value of the variables ‘eval-expression-print-length’ and<br>‘eval-expression-print-level’.&emsp;With a prefix argument of zero,<br>however, there is no such truncation.<br>Integer values are printed in several formats (decimal, octal,<br>and hexadecimal).&emsp;When the prefix argument is -1 or the value<br>doesn’t exceed ‘eval-expression-print-maximum-character’, an<br>integer value is also printed as a character of that codepoint.<br><br>If ‘eval-expression-debug-on-error’ is non-nil, which is the default,<br>this command arranges for all errors to enter the debugger.<br><br>(fn EVAL-LAST-SEXP-ARG-INTERNAL)"><kbd style="border-color: red">C-x C-e</kbd></abbr> on the following line to get
an Emacs-integrated REPL for Python:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">    <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-p now evaluates arbitrary Python code</span>
    (repl-driven-development [C-x C-p] <span style="color: #689d6a;">"python3"</span>)
</pre>
</div>

<p>
We can now press <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-p ∷ mark-page<br>Put mark at end of page, point at beginning.<br>A numeric arg specifies to move forward or backward by that many pages,<br>thus marking a page other than the one point was originally in.<br><br>(fn &optional ARG)"><kbd style="border-color: red">C-x C-p</kbd></abbr> to execute any Python code, such as:
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #98971a; font-weight: bold;">import</span> os                       <span style="color: #a89984;"># </span><span style="color: #a89984;">&#8658; Module OS imported</span>
f<span style="color: #689d6a;">"Hello, </span>{os.environ['USER']}<span style="color: #689d6a;">!"</span>  <span style="color: #a89984;"># </span><span style="color: #a89984;">&#8658; &#8220;Hello, musa!&#8221;</span>

</pre>
</div>

<p>
More exciting fun is to produce an increasing family of colourful circles, in a GUI:
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a89984;"># </span><span style="color: #a89984;">Ensure we have some GUI capabilities installed; press &#8220;C-x C-e&#8221;:</span>
<span style="color: #a89984;"># </span><span style="color: #a89984;">(async-shell-command "brew install python-tk")</span>

<span style="color: #98971a; font-weight: bold;">import</span> turtle
<span style="color: #076678;">it</span> = turtle.Turtle()  <span style="color: #a89984;"># </span><span style="color: #a89984;">This opens a new window</span>

<span style="color: #a89984;"># </span><span style="color: #a89984;">The following fragment was &#8220;discovered&#8221; by trying things out repeatedly with &#8220;C-x C-p&#8221;.</span>
<span style="color: #98971a; font-weight: bold;">for</span> i <span style="color: #98971a; font-weight: bold;">in</span> <span style="color: #7c6f64; font-weight: bold;">range</span>(10):
    it.pencolor(<span style="color: #689d6a;">"green"</span> <span style="color: #98971a; font-weight: bold;">if</span> i % 2 == 0 <span style="color: #98971a; font-weight: bold;">else</span> <span style="color: #689d6a;">"red"</span>)
    it.pensize(i / 2)
    it.circle(i * 10)


<span style="color: #a89984;"># </span><span style="color: #a89984;">Note for Python, the above &#8220;for&#8221; loop is &#8220;complete&#8221; if we also send the extra</span>
<span style="color: #a89984;"># </span><span style="color: #a89984;">newline after it.</span>
</pre>
</div>
<p>
Learn more with <a href="https://realpython.com/beginners-guide-python-turtle/">The Beginner's Guide to Python Turtle</a>.
</p>

<p>
TODO: Make this into a Gif, that incrementlly shows the turtle appearing?
ie it starts off with an experiment of the loop body, then it wraps it
in the for, then re-runs and all of this is <span class="underline">discovered live</span>!
</p>


<p>
👀
<i>This extension is an easy Emacs-integrated-REPL builder for any language!</i>
😲
</p>

</details> </div>
</div>
</div>
<div id="outline-container-Technically-speaking-how-is-Emacs-itself-the-REPL" class="outline-3">
<h3 id="Technically-speaking-how-is-Emacs-itself-the-REPL"><span class="section-number-3">1.3.</span> <a href="#Technically-speaking-how-is-Emacs-itself-the-REPL">Technically speaking, how is Emacs <i>itself</i> the REPL? 🤔</a></h3>
<div class="outline-text-3" id="text-Technically-speaking-how-is-Emacs-itself-the-REPL">
<p>
Let's do what math-nerds call <i>proof by definition-chasing:</i>
</p>

<ol class="org-ol">
<li>Definition: REPL is <i>any software</i> that supports a Read-Evaluate-Print-Loop cycle.</li>

<li><abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-e ∷ eval-last-sexp<br>Evaluate sexp before point; print value in the echo area.<br>Interactively, EVAL-LAST-SEXP-ARG-INTERNAL is the prefix argument.<br>With a non ‘-’ prefix argument, print output into current buffer.<br><br>This commands handles ‘defvar’, ‘defcustom’ and ‘defface’ the<br>same way that ‘eval-defun’ does.&emsp;See the doc string of that<br>function for details.<br><br>Normally, this function truncates long output according to the<br>value of the variables ‘eval-expression-print-length’ and<br>‘eval-expression-print-level’.&emsp;With a prefix argument of zero,<br>however, there is no such truncation.<br>Integer values are printed in several formats (decimal, octal,<br>and hexadecimal).&emsp;When the prefix argument is -1 or the value<br>doesn’t exceed ‘eval-expression-print-maximum-character’, an<br>integer value is also printed as a character of that codepoint.<br><br>If ‘eval-expression-debug-on-error’ is non-nil, which is the default,<br>this command arranges for all errors to enter the debugger.<br><br>(fn EVAL-LAST-SEXP-ARG-INTERNAL)"><kbd style="border-color: red">C-x C-e</kbd></abbr> / <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-j ∷ repl/jshell<br>Executes the selected region, if any or otherwise the entire current line,<br>and evaluates it with the command-line tool “jshell”.<br><br>Output is shown as an overlay at the current cursor position.<br>It is shown for ‘repl-driven-development/echo-duration’ many seconds.<br><br>## C-u Prefix: Insert result ###################################################<br><br>With a “C-u” prefix, the output is inserted at point<br>(and not echoed in an overlay).<br><br>## C-u C-u Prefix: Documentation ##############################################<br><br>With a “C-u C-u” prefix, documentation is looked-up for the word at point.<br><br>This is done using ‘devdocs’, and so the documentation generally provides<br>example uses as well. Visit https://devdocs.io/ to see the list of documented<br>languages and libraries.<br><br>## “C-u 0” Prefix: See associated buffer #####################################<br><br>Sometimes it may be useful to look at a large output in a dedicated buffer.<br><br>## “C-u -1” Prefix: Restart REPL #############################################<br><br>In the event you’ve messed-up your REPL, starting from a blank slate may be<br>helpful.<br><br>## Implementation Notes ########################################################<br><br>The interactive method is asynchronous: Whenever you send text for evaluation,<br>you immediately regain control in Emacs; you may send more text and it will be<br>queued for evaluation. For example, evaluating a sleep command for 3 seconds<br>does not block Emacs.<br><br>## See also ####################################################################<br><br>See ‘repl-driven-development’ for more useful docs.<br><br>See www.alhassy.com/repl-driven-development to learn more about RDD and see<br>examples and many gifs.<br>"><kbd style="border-color: red">C-x C-j</kbd></abbr> will echo the results next to your cursor, in your
editor</li>

<li>So it retains each of the read, eval, and print parts of the Read-Evaluate-Print-Loop</li>

<li>Moreover, since the program doesn't terminate, you're still in the <i>loop part</i>
until you close Emacs</li>
</ol>
</div>
</div>

<div id="outline-container-Summarising-Evaluator-Pitch" class="outline-3">
<h3 id="Summarising-Evaluator-Pitch"><span class="section-number-3">1.4.</span> <a href="#Summarising-Evaluator-Pitch">🛗 Summarising Evaluator Pitch ⚾</a></h3>
<div class="outline-text-3" id="text-Summarising-Evaluator-Pitch">
<center>
<p>
<i>Make Emacs itself a REPL for your given language of choice</i>
</p>
</center>

<p>
Suppose you're exploring a Python/Ruby/Java/JS/TS/Haskell/Lisps/etc
API, or experimenting with an idea and want immediate feedback.
You could open a terminal and try things out there; with no editor
support, and occasionally copy-pasting things back into your editor
for future use. Better yet, why not use your editor itself as a REPL.
</p>

<p>
The <a href="https://melpa.org/#/repl-driven-development"><img alt="MELPA" src="https://img.shields.io/badge/repl--driven--development-1.0.1-green?logo=Gnu-Emacs"></img></a> software provides the Emacs built-in
<abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-e ∷ eval-last-sexp<br>Evaluate sexp before point; print value in the echo area.<br>Interactively, EVAL-LAST-SEXP-ARG-INTERNAL is the prefix argument.<br>With a non ‘-’ prefix argument, print output into current buffer.<br><br>This commands handles ‘defvar’, ‘defcustom’ and ‘defface’ the<br>same way that ‘eval-defun’ does.&emsp;See the doc string of that<br>function for details.<br><br>Normally, this function truncates long output according to the<br>value of the variables ‘eval-expression-print-length’ and<br>‘eval-expression-print-level’.&emsp;With a prefix argument of zero,<br>however, there is no such truncation.<br>Integer values are printed in several formats (decimal, octal,<br>and hexadecimal).&emsp;When the prefix argument is -1 or the value<br>doesn’t exceed ‘eval-expression-print-maximum-character’, an<br>integer value is also printed as a character of that codepoint.<br><br>If ‘eval-expression-debug-on-error’ is non-nil, which is the default,<br>this command arranges for all errors to enter the debugger.<br><br>(fn EVAL-LAST-SEXP-ARG-INTERNAL)"><kbd style="border-color: red">C-x C-e</kbd></abbr> behaviour for arbitrary languages, provided they have a primitive
cli REPL.
</p>

<center>
<p>
<b>Benefits</b>
</p>
</center>

<p>
Whenever reading/refactoring some code, if you can make some of it
self-contained, then you can immediately try it out! No need to
load your entrie program; nor copy-paste into an external REPL. The
benefits of Emacs' built-in “C-x C-e” for Lisp, and Lisp's Repl
Driven Development philosophy, are essentially made possible for
arbitrary languages (to some approximate degree, but not fully).
</p>

<p>
Just as “C-u C-x C-e” inserts the resulting expression at the
current cursour position, so too all repl-driven-development
commands allow for a C-u prefix which inserts the result.
This allows for a nice scripting experience where results
are kept for future use &#x2014;e.g., when writing unit tests where you have an
expression but do not know what it results to.
</p>
</div>
</div>

<div id="outline-container-Features-of-RDD-el" class="outline-3">
<h3 id="Features-of-RDD-el"><span class="section-number-3">1.5.</span> <a href="#Features-of-RDD-el">🤖 💪 🤖 Features of RDD.el 💪 🤖 💪</a></h3>
<div class="outline-text-3" id="text-Features-of-RDD-el">
<ul class="org-ul">
<li>👀 Evaluation results are echoed at your cursor, in your editor, by your code, in an overlay</li>
<li>🔑 You can specify whatever keys you want, for evaluating code.  That
keybinding is itself well-documented, just invoke <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-h k ∷ describe-key<br>Display documentation of the function invoked by KEY-LIST.<br>KEY-LIST can be any kind of a key sequence; it can include keyboard events,<br>mouse events, and/or menu events.&emsp;When calling from a program,<br>pass KEY-LIST as a list of elements (SEQ . RAW-SEQ) where SEQ is<br>a key-sequence and RAW-SEQ is its untranslated form.<br><br>While reading KEY-LIST interactively, this command temporarily enables<br>menu items or tool-bar buttons that are disabled to allow getting help<br>on them.<br><br>Interactively, this command can’t describe prefix commands, but<br>will always wait for the user to type the complete key sequence.<br>For instance, entering ''C-x'' will wait until the command has<br>been completed, but ‘M-: (describe-key (kbd ''C-x'')) RET’ will<br>tell you what this prefix command is bound to.<br><br>BUFFER is the buffer in which to lookup those keys; it defaults to the<br>current buffer.<br><br>(fn &optional KEY-LIST BUFFER UP-EVENT)"><kbd style="border-color: red">C-h k</kbd></abbr> then your
keybinding.</li>
<li>🩹 Press <kbd style="">C-u C-x C-j</kbd> to get the results of your evaluated expression
printed inline, at your cursor.</li>
<li>📚 Documentation is supported out of the box: Put the cursor over a function
name (like "print" or "error"). Then press <kbd style="">C-u C-u C-x C-j</kbd> and you get the
documentation of that function.</li>
</ul>
</div>
</div>
</div>

<div id="outline-container-Teaching-a-runtime-green-incrementally-to-be-a-web-server-️" class="outline-2">
<h2 id="Teaching-a-runtime-green-incrementally-to-be-a-web-server-️"><span class="section-number-2">2.</span> <a href="#Teaching-a-runtime-green-incrementally-to-be-a-web-server-️">Teaching a runtime, <b><span style="color:green;">incrementally</span></b>, to be a web server 🍽️ 🔁 🤖</a></h2>
<div class="outline-text-2" id="text-Teaching-a-runtime-green-incrementally-to-be-a-web-server-️">
<center> <em>
<p>
<i>RDD by example</i>: Let's start with a JavaScript runtime and incrementally turn it
into a web server.
</p>
</em> </center>

<p>
<span class="underline">“RDD ≈ Programming as Teaching”:</span> Start from a program that already works and
“teach it” to be the program we actually want. This makes <i>programming a
goal-directed activity</i>.
</p>

<p>
Below we demonstrate this idea by starting a runtime and, like talking to a
person, we teach it new behaviours. Once it has all the desired behaviours, then
we're done and the text we've written (in our editor) is the resulting program.
Most importantly, we <i>actively interact with the running program as it evolves</i>;
where each “teaching step” is influenced by observing the program's reactions
to various stimuli (e.g., how things look, how they function, etc).
</p>


<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">The “𝒳 as teaching” meme </font> </strong> </summary>
<ul class="org-ul">
<li>The “𝒳 as teaching” meme is about <i>accomplishing the goal 𝒳 as if you were
talking to a friend in-person, explaining how to do something.</i></li>

<li>Almost everything in programming can stand-in for 𝒳; e.g., writing a function
or a git commit is a good way to ‘teach’ your colleagues how to improve the
code-base &#x2014;as such, if the function/commit does “too much” then it is a
“poor teacher” and so not ideal.</li>

<li>Related video: <a href="https://www.youtube.com/watch?v=VEXaUHNmpQw&amp;t=1520s">“How to Write a Great Research Paper (7 Excellent Tips)” by Simon Peyton Jones</a>.</li>
</ul>

</details> </div>

<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green"><em>Wait, I already do this RDD stuff everyday, in the shell!</em> </font> </strong> </summary>
<p>
You <i>can</i> <b><span style="color:green;">“discover”</span></b> a bash script by running various incantations at the
terminal, pressing the up-arrow key, tweaking your incantation &#x2014;and repeating
until you're happy with the result. In this way, you are teaching the shell a
new skill &#x2014;by repeatedly checking whether it can perform the skill and if not,
then refining your definitions.
</p>

<center>
<p>
<i>Anytime you execute a query, in some system, you're using a read-evaluate-print-loop!</i>
</p>
</center>

<p>
Examples include: Writing shell &amp; SQL queries, visiting web-pages by writing
URLs, exploring HTTP APIs using curl/<a href="https://httpie.io/docs/cli/json">httpie</a>, and using the JavaScript Console in
your browser.
</p>

<p>
<b><span style="color:red;">Sadly</span>,</b> the <i>interface</i> to such REPLs is generally very limited. There is no
syntax highlighting, no code completion, no linting, it is difficult to work with
multi-line input. This article proposes instead to use <i>your editor as the
interface to a REPL</i>: You write some code in your feature-rich editor then press
some keys to have <i>only the newly written code</i> executed.
</p>

</details> </div>
<details> <summary> <strong style="color:green"> <em> RDD let's you use your favourite language as a Bash replacement scripting language</em></strong></summary>

<blockquote>
<p>
<i>If your code-base is in language 𝐿, might as well write your scripts in 𝐿 as
well!</i>
</p>
</blockquote>

<p>
For example, if you want to say run a simple for-loop on a payload of an
HTTP request then might as well use your favourite language 𝐿 &#x2014;and <i>not</i>
Bash. Likewise, want to run a for-loop on the results of a SQL query: Use your
favourite language 𝐿, not a SQL scripting language that you're not terribly
comfortable with.
</p>

<p>
Why script in your favourite language 𝐿, and not Bash?
</p>
<ol class="org-ol">
<li>If your an 𝐿 language developer, writing scripts in 𝐿 lets you make use of
all of your existing experience, knowledge, and familiar tool-set of 𝐿.</li>
<li>Stay in the comfort of your favourite IDE: Autocomplete, syntax highlighting,
docs, tooltips, linting, etc.</li>
<li>Lots of libraries!</li>
<li>The gain in expressivity &amp; clarity &amp; test-ability.</li>
<li>Rich data structures, error checking, and <i>compositionality</i>.
<ul class="org-ul">
<li>Since Bash only has <i>unstructured data via strings</i>, this means to compose
two different Bash programs you have to get them to “understand a common
structure” and this means you have to convert unstructured data to JSON
somehow (e.g., using <a href="https://github.com/kellyjonbrazil/jc">jc</a>, which <i>JSONifies the output of many CLI tools</i>) or
parse it yourself! Might as well use your favourite language, since it
probably has support for JSON and has real <i>structured</i> objects.</li>
</ul></li>
<li>An 𝐿-REPL is a shell with 𝐿-syntax, and features! &#x2014;Since you're actually using 𝐿.</li>
<li>Bash is imperative, but your favourite language is (probably) multi-paradigm
&#x2014;you can do imperative or more!</li>
<li><i>By trying out API calls in your language 𝐿 instead of Bash, you get working
code in your language right away that you can build an app around &#x2014;no need</i>
<i>to figure out how to do that later on in your language.</i></li>
</ol>

<blockquote>
<p>
<i>The next time you need to write a loop in Bash, consider breaking out your REPL
and seeing what you can come up with instead!</i>
</p>

<p>
Slightly paraphrasing from: <a href="https://www.freecodecamp.org/news/python-for-system-administration-tutorial/">How to Replace Bash with Python as Your Go-To Command Line Language</a>
</p>
</blockquote>

<p>
<b>“Bash ↦ JavaScript” Personal anecdote:</b> One time I automated a bunch of tedious
tasks at work with Bash by using <a href="https://github.com/kellyjonbrazil/jc">jc</a>, which <i>JSONifies the output of many CLI
tools</i>, alongside <a href="https://jqlang.github.io/jq/">jq</a>, a JSON query language; along with a friendly-alternative to
curl known as <a href="https://github.com/httpie/cli">httpie</a>. However, as the Bash incantations grew larger and larger,
it became more practical to switch to JavaScript and read the http payloads as
proper JavaScript objects (rather than use <i>jc</i>), and quickly work with them via
the usual JS methods <i>.map, .filter, .reduce</i>. With Bash, I used <i>jq</i> and it's
special syntax, but with JavaScript I just use JS in both places 💐 Finally,
this automated work required updating JSON configurations, but I wanted the
result to be pretty-printed for future human readers. Since JSON is literally
JS, it's most natural to use JS to work with JSON and so that's what I
did. <i>Below are 2 very useful methods from this Bash↦JavaScript move</i>.
</p>


<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">withJSON: Alter the contents of a JSON file as if it were a JavaScript object </font> </strong> </summary>
<div class="org-src-container">
<pre class="src src-javascript"><span style="color: #689d6a;">/** Alter the contents of a JSON file as if it were a JavaScript object.</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * - `path : string` is a filepath to a `.json` file.</span>
<span style="color: #689d6a;"> * - `callback : function` is a (possibly async) function that mutates a given JS object.</span>
<span style="color: #689d6a;"> * - `newFile : boolean` indicates whether this is a completely new file, in which case `callback` is provided with an empty object.</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * Trying to access a JSON file that does not exist, when not enabling `newFile`, will result in an error.</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * Write the JSON file, and format it nicely.</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * ### Example use</span>
<span style="color: #689d6a;"> * ```</span>
<span style="color: #689d6a;"> * // Add a new `WOAH` key to the configuration file.</span>
<span style="color: #689d6a;"> * withJSON("~/myConfig.json", data =&gt; data.WOAH = 12)</span>
<span style="color: #689d6a;"> * ```</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * ### Warning! ---Also Design Decision Discussion</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * A purely functional approach would require `callback` to have the shape `data =&gt; {...; return data}`.</span>
<span style="color: #689d6a;"> * However, we anticipate that most uses will be to update a field of `data` and so `callback` will</span>
<span style="color: #689d6a;"> * have the shape `data =&gt; {data.x = y; return data}` and we want to reduce the ceremony: We work with mutable references,</span>
<span style="color: #689d6a;"> * so that `data =&gt; data.x = y` is a sufficient shape for `callback`. However, this comes at the cost that we cannot</span>
<span style="color: #689d6a;"> * wholesale alter a JSON file ---which is an acceptable tradeoff, since this is likely a rare use case.</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * ```</span>
<span style="color: #689d6a;"> * withJSON(`~/myfile.json`, data =&gt; data = {x: 1, y: 2})      // BAD! Will not alter the underyling JSON file.</span>
<span style="color: #689d6a;"> * withJSON(`~/myfile.json`, data =&gt; {data.x = 1; data.y = 2}) // GOOD!</span>
<span style="color: #689d6a;"> * ```</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * A program should not just compute, it should also motivate, justify &amp; discuss.</span>
<span style="color: #689d6a;"> * This human nature makes it easier to follow, detect errors, use elsewhere, or extend.</span>
<span style="color: #689d6a;"> * After all, the larger part of the life of a piece of software is maintenance.</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * Flawed programs with good discussion may be of more use in the development of related correct code,</span>
<span style="color: #689d6a;"> * than working code that has no explanation.</span>
<span style="color: #689d6a;"> */</span>
<span style="color: #98971a; font-weight: bold;">function</span> <span style="color: #076678;">withJSON</span>(<span style="color: #076678;">file</span>, <span style="color: #076678;">callback</span>, <span style="color: #076678;">newFile</span>) {
  file = file.replace(<span style="color: #689d6a;">/~/</span>g, process.env.HOME)
  <span style="color: #98971a; font-weight: bold;">try</span> {
    <span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">data</span> = newFile ? {} : JSON.parse(fs.readFileSync(file))
    callback(data)
    fs.writeFileSync(file, JSON.stringify(data, <span style="color: #076678; font-weight: bold;">null</span>, 2))
  } <span style="color: #98971a; font-weight: bold;">catch</span> (error) {
    console.error(<span style="color: #689d6a;">`&#129327; Oh no! ${error}`</span>)
    console.error(callback.toString())
    process.exit(0)
  }
}
</pre>
</div>

</details> </div>

<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">shell: Run a shell command and provide its result as a string; or crash when there's an error </font> </strong> </summary>
<div class="org-src-container">
<pre class="src src-javascript"><span style="color: #689d6a;">/** Run a shell command and provide its result as a string; or crash when there's an error.</span>
<span style="color: #689d6a;"> * This is intentionally synchronous; i.e., everything stops until the command is done executing.</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * @param {string} command - A Unix bash incantation to be executed.</span>
<span style="color: #689d6a;"> * @param {boolean} ignore - Whether to actually avoid doing any execution; useful for testing/experimentation.</span>
<span style="color: #689d6a;"> * @returns {string} The textual stdout result of executing the given command.</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * - </span><span style="color: #cc9393; font-weight: bold;">TODO</span><span style="color: #689d6a;">: Consider switching to ShellJS.</span>
<span style="color: #689d6a;"> * - Why ShellJS? https://julialang.org/blog/2012/03/shelling-out-sucks/</span>
<span style="color: #689d6a;"> * - See also `Executing shell commands from Node.js`, https://2ality.com/2022/07/nodejs-child-process.html</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * ### Examples</span>
<span style="color: #689d6a;"> * ```</span>
<span style="color: #689d6a;"> * // Who is the current user?</span>
<span style="color: #689d6a;"> * console.log( shell('whoami') )</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * // Make me smile!</span>
<span style="color: #689d6a;"> * console.log( shell('fortune') )</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * // See your Git credentials: Name, email, editor, etc.</span>
<span style="color: #689d6a;"> * shell("git config --list")</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * // Crashes if the provided command does not exist</span>
<span style="color: #689d6a;"> * shell(`nonexistentprogram`); console.log(`You wont see this msg!`) // Boom!</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * // Pass non-falsy second argument to invoke as a dry-run only</span>
<span style="color: #689d6a;"> * shell(`nonexistentprogram`, true); console.log(`You WILL see this msg!`)</span>
<span style="color: #689d6a;"> * ```</span>
<span style="color: #689d6a;"> *</span>
<span style="color: #689d6a;"> * Consider a program to be written primarily to explain to another human what it is that we want the computer to do,</span>
<span style="color: #689d6a;"> * how it is to happen, and why we can believe that we have achievied our aim.</span>
<span style="color: #689d6a;"> * (The &#8220;another human&#8221; might be you in a few months time when the details have escaped your mind.)</span>
<span style="color: #689d6a;"> */</span>
<span style="color: #98971a; font-weight: bold;">function</span> <span style="color: #076678;">shell</span>(<span style="color: #076678;">command</span>, <span style="color: #076678;">ignore</span>) {
  <span style="color: #98971a; font-weight: bold;">return</span> ignore
    ? <span style="color: #689d6a;">`\n&#129302; This is a DRY RUN, so I haven't done anything but I would have:\n&#129514;&#129514;&#129514;\n${command}\n&#129514;&#129514;&#129514;`</span>
    : require(<span style="color: #689d6a;">'child_process'</span>).execSync(command).toString().trim()
}

<span style="color: #689d6a;">/** NodeJS dislikes `~` in file paths, so this helper lets you read files with `~` in their path.</span>
<span style="color: #689d6a;"> * @param {string} path - The (possibily relative) path to a file</span>
<span style="color: #689d6a;"> * @returns {string} The contents of the file located at the given path</span>
<span style="color: #689d6a;"> */</span>
<span style="color: #98971a; font-weight: bold;">function</span> <span style="color: #076678;">readFile</span>(<span style="color: #076678;">path</span>) {
  <span style="color: #98971a; font-weight: bold;">return</span> fs.readFileSync(path.replace(<span style="color: #689d6a;">/~/</span>, process.env.HOME))
}
</pre>
</div>

</details> </div>

<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Quick Example of using JS as a command-line-language            </font> </strong> </summary>
<div class="org-src-container">
<pre class="src src-javascript"> <span style="color: #98971a; font-weight: bold;">var</span> <span style="color: #076678;">axios</span> = require(<span style="color: #689d6a;">'axios'</span>)
 <span style="color: #98971a; font-weight: bold;">var</span> { name, blog, bio } = (<span style="color: #98971a; font-weight: bold;">await</span> axios.get(<span style="color: #689d6a;">'https://api.github.com/users/alhassy'</span>)).data
</pre>
</div>

</details> </div>

<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Java: run a shell command and see the output                    </font> </strong> </summary>
<div class="org-src-container">
<pre class="src src-java">execCmd(<span style="color: #689d6a;">"whoami"</span>)  <span style="color: #a89984;">// </span><span style="color: #a89984;">==&gt; "musa\n"</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Source: https://stackoverflow.com/a/20624914/3550444</span>
<span style="color: #98971a; font-weight: bold;">public</span> <span style="color: #98971a; font-weight: bold;">static</span> String execCmd(String cmd) <span style="color: #98971a; font-weight: bold;">throws</span> <span style="color: #076678; font-weight: bold;">java</span>.<span style="color: #076678; font-weight: bold;">io</span>.<span style="color: #b57614;">IOException</span> {
    <span style="color: #076678; font-weight: bold;">java</span>.<span style="color: #076678; font-weight: bold;">util</span>.<span style="color: #b57614;">Scanner</span> <span style="color: #076678;">s</span> = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #076678; font-weight: bold;">java</span>.<span style="color: #076678; font-weight: bold;">util</span>.<span style="color: #b57614;">Scanner</span>(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter(<span style="color: #689d6a;">"\\A"</span>);
    <span style="color: #98971a; font-weight: bold;">return</span> s.hasNext() ? s.next() : <span style="color: #689d6a;">""</span>;
}
</pre>
</div>
</details> </div>
<p>
<b>Further reading</b>:
</p>
<ul class="org-ul">
<li><a href="https://dev.to/taikedz/your-bash-scripts-are-rubbish-use-another-language-5dh7">Your bash scripts are rubbish, use another language</a></li>
<li><a href="https://blog.developer.atlassian.com/scripting-with-node/">Building command line tools with Node.js - Atlassian Developer Blog</a></li>
<li><a href="https://medium.com/capital-one-tech/bashing-the-bash-replacing-shell-scripts-with-python-d8d201bc0989">Bashing the Bash — Replacing Shell Scripts with Python  ⋋⋌  Medium</a></li>
<li><a href="https://www.youtube.com/watch?v=TdkZ216R6Bw">Bashing the bash: Why the shell is awful &amp; what you can do about it  ⋋⋌  YouTube</a></li>
</ul>

</details>
<hr />

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Goal: Make an web server with a route <code>localhost:3030/about</code> that shows information about the user's environment variables.</td>
</tr>
</tbody>
</table>



<div id="org872ad70" class="figure">
<p><img src="../images/rdd-teaching-a-js-runtime-to-be-a-webserver.png" alt="rdd-teaching-a-js-runtime-to-be-a-webserver.png" width="75%" height="75%" />
</p>
</div>

<p>
First,
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">   <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-j now evaluates arbitrary JavaScript code, I'd also like docs for JS and Express</span>
   (repl-driven-development [C-x C-j] <span style="color: #689d6a;">"node"</span> <span style="color: #7c6f64; font-weight: bold;">:docs</span> <span style="color: #689d6a;">"javascript express"</span>)
</pre>
</div>

<p>
Then, here's how we do this &#x2026;
</p>

<p>
Visit <a href="http://localhost:3030/about">http://localhost:3030/about</a>, if that works, then we're done!
</p>

<div class="org-src-container">
<pre class="src src-javascript"><span style="color: #a89984;">// </span><span style="color: #a89984;">First get stuff with C-x C-e:</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">(async-shell-command "npm install -g express axios@0.21.1")</span>

<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">app</span> = require(<span style="color: #689d6a;">'/usr/local/lib/node_modules/express'</span>)()
<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">server</span> = app.listen(3030) <span style="color: #a89984;">// </span><span style="color: #a89984;">&#128218; Press &#8220;C-u C-u C-x C-j&#8221; to see docs about &#8220;listen&#8221; ;-)</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Now visit http://localhost:3030/</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">... and see &#8220;Cannot GET /&#8221;</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">... Neat, it works but it does nothing! Importantly it works!</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Let's add a route...</span>
<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">visited</span> = 1
app.get(<span style="color: #689d6a;">'/hi'</span>, (req, res) =&gt; res.send(<span style="color: #689d6a;">`Hello &#215; ${visited++}`</span>))

<span style="color: #a89984;">// </span><span style="color: #a89984;">Now visit:  http://localhost:3030/hi</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">Refresh the page a few times &#128521;</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Excellent; let's add an end-point to return the variables in scope</span>
app.get(<span style="color: #689d6a;">'/about'</span>, (req, res) =&gt; res.send(html()) )

<span style="color: #a89984;">// </span><span style="color: #a89984;">Whoops, there's no &#8220;html&#8221;! So we see an error!</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">Let's define that!</span>
<span style="color: #98971a; font-weight: bold;">let</span>
html = _ =&gt; <span style="color: #689d6a;">"&lt;div style='color:green; background-color:cyan'&gt;"</span> + info() + <span style="color: #689d6a;">"&lt;/div&gt;"</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Whoops, there's no &#8220;info&#8221;! So we see an error!</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">Let's define that!</span>
<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">info</span> = <span style="color: #98971a; font-weight: bold;">function</span> () { <span style="color: #98971a; font-weight: bold;">return</span> {visited, user: process.env.USER, time: <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Date</span>() } }

<span style="color: #a89984;">// </span><span style="color: #a89984;">Uh-oh, we see &#8220;[object Object]&#8221; since we didn't convert the</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">JS object into a JSON string, so let's fix that!</span>
html = _ =&gt; <span style="color: #689d6a;">"&lt;div style='color:green; background-color:cyan'&gt;"</span> + JSON.stringify(info(), <span style="color: #076678; font-weight: bold;">null</span>, 3 <span style="color: #a89984;">/* </span><span style="color: #a89984;">indentation</span><span style="color: #a89984;"> */</span>) + <span style="color: #689d6a;">"&lt;/div&gt;"</span>

<span style="color: #a89984;">/* </span><span style="color: #a89984;">uh-oh, the output doesn't look good; let's redefine `html` using &lt;pre&gt; tags.</span>

<span style="color: #a89984;">   pre tells the browser engine that the content inside is pre-formatted and it can be displayed without any modification. So browser will not remove white spaces, new lines etc. code is for making it more semantic and denotes that the content inside is a code snippet. It has nothing to with formatting.</span>
<span style="color: #a89984;"> */</span>
html = _ =&gt; <span style="color: #689d6a;">`&lt;h1&gt;Welcome, visitor ${visited++}!&lt;/h1&gt;&lt;pre style='color:green; background-color:cyan'&gt;`</span> + JSON.stringify(info(), <span style="color: #076678; font-weight: bold;">null</span>, 3 <span style="color: #a89984;">/* </span><span style="color: #a89984;">indentation</span><span style="color: #a89984;"> */</span>) + <span style="color: #689d6a;">"&lt;/pre&gt;"</span>


<span style="color: #a89984;">// </span><span style="color: #a89984;">Notice how we built this end-point from the top-down: We knew what we wanted, and saw some</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">errors ---on the client side--- then fixed them right here, with no reloading!</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Actually, let's add more info: It's not enough to see the current user, let's see all environvment variable values</span>
info = _ =&gt; ({user: process.env, time: <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Date</span>(), platform: os.platform(), architecture: os.arch(), home: os.homedir(), user: os.userInfo(), machine: os.machine()})

<span style="color: #a89984;">// </span><span style="color: #a89984;">So cool!</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Eventually, consider closing the server!</span>
server.close()
</pre>
</div>

<p>
TODO: Make the above into a short youtube video/*GIF*, where I keep
“improving” the definition of <code>html</code> / <code>info</code> and see it live!
</p>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>RDD is about Unobtrusive Redefining</h3>
<p>
Notice that our demonstration above is mostly redefining things, making
interactive observations about them, then redefining them to be better.
</p>

<p>
Most importantly, this redefining cycle is not impeded by the need to restart
the program each time.
</p>

<p>
Instead, the already-working program “learns” what we have taught it &#x2014;and
continues to be a working program.
</p>

</div>

<div> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Programming   ≈   Definitions and re-definitions </font> </strong> </summary>
<p>
In the previous section we saw how easy it was to add &amp; redefine things <i>without</i>
having to restart our program; as such we have the motto “RDD ≈ Interactive
Programming”.
</p>

<div class="org-center">
<p>
<i>In RDD, we can <span style="color:green;">re-define</span> functions &amp; types live, as the program is
running! <br> Future uses of the function/type will use the new definition!</i>
</p>
</div>

<p>
In stark contrast, the traditinal approach forces us to restart the whole
program whenever we make a modification, no matter how small!
That's like rebuilding your entire house when you only wanted to put up a shelf!
🤮
</p>

<hr />

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Let's explore the issue of redefinitions a bit more.</td>
</tr>
</tbody>
</table>

<p>
If you define a function \(f\) and declare \(x = f()\), but then decide to redefine
\(f\), what should happen to \(x\)?  Well, \(x\) is already declared and already has a
value, so nothing happens to it!  If you want it to be the result of the
re-defined \(f\), then re-evaluate \(x = f()\). 👍
</p>

<p>
<span style="color:orange;">However</span>, when <i>re-defining a data-type/class/record/struct</i>, languages such
as Java and Common Lisp, will insist that any previously defined instances now
conform to the new data-type formulation! Likewise, for methods whose inputs are
of the old formulation, they need to be updated to the new one.
</p>

<p>
Take a look at this interactive Java session&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">record</span> <span style="color: #076678;">Person</span>(<span style="color: #b57614;">String</span> <span style="color: #076678;">name</span>) { }
<span style="color: #b57614;">var</span> <span style="color: #076678;">me</span> = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Person</span>(<span style="color: #689d6a;">"Musa"</span>); <span style="color: #a89984;">// </span><span style="color: #a89984;">New instance.</span>
 <span style="color: #a89984;">// </span><span style="color: #a89984;">Can operate on it, using a functional *variable* or a *method*</span>
<span style="color: #b57614;">Function</span>&lt;<span style="color: #b57614;">Person</span>, <span style="color: #b57614;">String</span>&gt; <span style="color: #076678;">speak</span> = p -&gt; p.name() + <span style="color: #689d6a;">" says HELLO!"</span>
String greet(Person p) { <span style="color: #98971a; font-weight: bold;">return</span> <span style="color: #689d6a;">"Hello, I'm "</span> + p.name(); }


<span style="color: #a89984;">// </span><span style="color: #a89984;">Redefining our data-type</span>
<span style="color: #b57614;">record</span> <span style="color: #076678;">Person</span>(<span style="color: #b57614;">int</span> <span style="color: #076678;">age</span>) { }
<span style="color: #a89984;">//</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658;</span>
<span style="color: #a89984;">//  </span><span style="color: #a89984;">record Person(int age) { }</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">|  replaced record Person</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">|    update replaced variable me which cannot be referenced until this error is corrected:</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">|      incompatible types: java.lang.String cannot be converted to int</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">|      var me = new Person("Musa");</span>


<span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; As such, since &#8220;me&#8221; cannot be updated to be an instance of the reformulated data-type, it is kicked out of scope!</span>
me    <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; No such variable is declared!</span>
speak <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; No such *variable* is declared!</span>
<span style="color: #b57614;">greet</span> <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; No problem, but can only run it if you define a  Person::name  instance method! &#128562;</span>

<span style="color: #076678;">greet</span>(<span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">Person</span>(12)) <span style="color: #a89984;">// </span><span style="color: #a89984;">&#8658; Attempted to call method greet(Person) which cannot be</span>
                      <span style="color: #a89984;">// </span><span style="color: #a89984;">invoked until method name() is declared</span>
</pre>
</div>

<p>
Whereas Java says you can no longer use stale instances, Common Lisp tries to
“re-initialise” existing instances &#x2014;and prompts the user if it cannot do so
automatically. The Common Lisp approach may have benefits, but it comes at a
dangerous cost: <i>Your runtime is now no longer tied to text you've written!</i> It is
for this reason, that <a href="https://melpa.org/#/repl-driven-development"><img alt="MELPA" src="https://img.shields.io/badge/repl--driven--development-1.0.1-green?logo=Gnu-Emacs"></img></a> intentionally does not allow
users to run code in the <code>*REPL/⋯*</code> buffers: If you want to modify the running
system, write your modification down (in your working buffer, then save,) then
evaluate it.
</p>

</details> </div>
</div>
</div>
<div id="outline-container-Concluding-Remarks" class="outline-2">
<h2 id="Concluding-Remarks"><span class="section-number-2">3.</span> <a href="#Concluding-Remarks">Concluding Remarks</a></h2>
<div class="outline-text-2" id="text-Concluding-Remarks">
<p>
I've found RDD to be a <i><span style="color:green;">fun</span></i> way to code.  I get fast feedback from my code
as I design it, being able to test my assumptions against how the code actually
works.  When I'm satisfied with something, I codify that behaviour via unit
tests so that I'm aware when my evolving design diverges &#x2014;as I continue
iterating on the design in the REPL.
</p>


<div id="orgec887c3" class="figure">
<p><img src="../images/rdd-workflow.png" alt="rdd-workflow.png" width="75%" height="75%" />
</p>
</div>


<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Some languages have tight integration with Emacs!</h3>
<p>
Programs in these languages are essentially “constructed incrementally” by
“interactive conversations” with Emacs (as the REPL).
</p>

<div class="org-center">
<p>
<a href="https://alhassy.github.io/ElispCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Elisp-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=Gnu-Emacs"></a>
</p>

<p>
<a href="https://alhassy.github.io/ClojureCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Clojure-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=awslambda"></a>
</p>

<p>
<a href="https://alhassy.github.io/AgdaCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Agda-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=haskell"></a>
</p>

<p>
<a href="https://alhassy.github.io/CoqCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Coq-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=twitter"></a>
</p>

<p>
<a href="https://alhassy.github.io/OzCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Oz-PDF%C2%A0CheatSheet-success?logo=pastebin"></a>
</p>
</div>

<p>
The first such language is <i>Common Lisp</i>.
Which also inspired a similar setup for Smalltalk &#x2014;e.g., Pharo and Squeak.
</p>

</div>

<p>
I hope you've enjoyed this article!
</p>

<p>
Bye! 👋 🥳
</p>
</div>
</div>

<div id="outline-container-Appendix-Interesting-Reads" class="outline-2">
<h2 id="Appendix-Interesting-Reads"><a href="#Appendix-Interesting-Reads">Appendix: Interesting Reads</a></h2>
<div class="outline-text-2" id="text-Appendix-Interesting-Reads">
<ul class="org-ul">
<li><a href="https://github.com/rabbibotton/clog/blob/main/LEARN.md">CLOG: Learn Common Lisp by building real-world applications</a></li>
<li><a href="https://pythonspeed.com/articles/live-debugging-python/">A Python prompt into a running process: debugging with Manhole</a></li>
</ul>
</div>
</div>

<div id="outline-container-Appendix-Recipes-for-a-number-of-languages" class="outline-2">
<h2 id="Appendix-Recipes-for-a-number-of-languages"><a href="#Appendix-Recipes-for-a-number-of-languages">Appendix: Recipes for a number of languages</a></h2>
<div class="outline-text-2" id="text-Appendix-Recipes-for-a-number-of-languages">
<div><a style="width: 1%;float: left; padding: 0px" id="javascript" href="#javascript">🔗</a> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">JavaScript ---and a minimal server             </font> </strong> </summary>
<div class="org-center">
<p>
<a href="https://alhassy.github.io/JavaScriptCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/JavaScript-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=javascript"></a>
</p>
</div>

<p>
We can set up a JavaScript REPL in the background as follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">   <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-j now evaluates arbitrary JavaScript code</span>
   (repl-driven-development [C-x C-j] <span style="color: #689d6a;">"node -i"</span>)
</pre>
</div>

<p>
That's it! Press <abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-e ∷ eval-last-sexp<br>Evaluate sexp before point; print value in the echo area.<br>Interactively, EVAL-LAST-SEXP-ARG-INTERNAL is the prefix argument.<br>With a non ‘-’ prefix argument, print output into current buffer.<br><br>This commands handles ‘defvar’, ‘defcustom’ and ‘defface’ the<br>same way that ‘eval-defun’ does.&emsp;See the doc string of that<br>function for details.<br><br>Normally, this function truncates long output according to the<br>value of the variables ‘eval-expression-print-length’ and<br>‘eval-expression-print-level’.&emsp;With a prefix argument of zero,<br>however, there is no such truncation.<br>Integer values are printed in several formats (decimal, octal,<br>and hexadecimal).&emsp;When the prefix argument is -1 or the value<br>doesn’t exceed ‘eval-expression-print-maximum-character’, an<br>integer value is also printed as a character of that codepoint.<br><br>If ‘eval-expression-debug-on-error’ is non-nil, which is the default,<br>this command arranges for all errors to enter the debugger.<br><br>(fn EVAL-LAST-SEXP-ARG-INTERNAL)"><kbd style="border-color: red">C-x C-e</kbd></abbr> on the above line so that <kbd style="">C-x C-j</kbd> will now
evaluate a selection, or the entire line, as if it were JavaScript code.
</p>
<ul class="org-ul">
<li>Why <kbd style="">C-x C-j</kbd>  ?  Well, <kbd style="">C-x C-“e</kbd>” for Emacs Lisp code, and <kbd style="">C-x C-“j</kbd>”
for JavaScript code!</li>
<li>For instance, copy-paste the following examples into a JavaScript file &#x2014;or just
press <kbd style="">C-x C-j</kbd> <i>in any buffer</i> to evaluate them!</li>
</ul>

<div class="org-src-container">
<pre class="src src-javascript">1 + 2                                     <span style="color: #a89984;">// </span><span style="color: #a89984;">&#11157; 3</span>

1 + <span style="color: #689d6a;">'2'</span>                                   <span style="color: #a89984;">// </span><span style="color: #a89984;">&#11157; '12'</span>

<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">me</span> = {name: <span style="color: #689d6a;">'Jasim'</span>}; Object.keys(me) <span style="color: #a89984;">// </span><span style="color: #a89984;">&#11157; ['name']</span>

me.doesNotExist(<span style="color: #689d6a;">'whoops'</span>)                 <span style="color: #a89984;">// </span><span style="color: #a89984;">&#11157; Uncaught TypeError</span>
</pre>
</div>

<p>
All of these results are echoed inline in an overlay, by default.
Moreover, there is a <b>REPL</b> buffer created for your REPL so you
can see everything you've sent to it, and the output it sent
back.  This is particularly useful for lengthy error messages,
such as those of Java, which cannot be rendered nicely within an
overlay.
</p>

<p>
How this works is that Emacs spawns a new “node -i” process, then
<abbr class="tooltip" style="border: none; text-decoration: none;" title="C-x C-j ∷ repl/jshell<br>Executes the selected region, if any or otherwise the entire current line,<br>and evaluates it with the command-line tool “jshell”.<br><br>Output is shown as an overlay at the current cursor position.<br>It is shown for ‘repl-driven-development/echo-duration’ many seconds.<br><br>## C-u Prefix: Insert result ###################################################<br><br>With a “C-u” prefix, the output is inserted at point<br>(and not echoed in an overlay).<br><br>## C-u C-u Prefix: Documentation ##############################################<br><br>With a “C-u C-u” prefix, documentation is looked-up for the word at point.<br><br>This is done using ‘devdocs’, and so the documentation generally provides<br>example uses as well. Visit https://devdocs.io/ to see the list of documented<br>languages and libraries.<br><br>## “C-u 0” Prefix: See associated buffer #####################################<br><br>Sometimes it may be useful to look at a large output in a dedicated buffer.<br><br>## “C-u -1” Prefix: Restart REPL #############################################<br><br>In the event you’ve messed-up your REPL, starting from a blank slate may be<br>helpful.<br><br>## Implementation Notes ########################################################<br><br>The interactive method is asynchronous: Whenever you send text for evaluation,<br>you immediately regain control in Emacs; you may send more text and it will be<br>queued for evaluation. For example, evaluating a sleep command for 3 seconds<br>does not block Emacs.<br><br>## See also ####################################################################<br><br>See ‘repl-driven-development’ for more useful docs.<br><br>See www.alhassy.com/repl-driven-development to learn more about RDD and see<br>examples and many gifs.<br>"><kbd style="border-color: red">C-x C-j</kbd></abbr> sends text to that process. Whenever the process emits
any output &#x2014;on stdout or stderr&#x2014; then we emit that to the
user via an overlay starting with “⮕”.
</p>

<p>
Finally, “C-h k  C-x C-j” will show you the name of the function
that is invoked when you press C-x C-j, along with minimal docs.
</p>

<p>
A useful example would be a minimal server, and requests for it.
</p>

<div class="org-src-container">
<pre class="src src-javascript"><span style="color: #a89984;">// </span><span style="color: #a89984;">First get stuff with C-x C-e:</span>
<span style="color: #a89984;">// </span><span style="color: #a89984;">(async-shell-command "npm install -g express axios")</span>

<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">app</span> = require(<span style="color: #689d6a;">'express'</span>)()
<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">clicked</span> = 1
app.get(<span style="color: #689d6a;">'/hi'</span>, (req, res) =&gt; res.send(<span style="color: #689d6a;">`Hello World &#215; ${clicked++}`</span>))

<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">server</span> = app.listen(3000)
<span style="color: #a89984;">// </span><span style="color: #a89984;">Now visit   http://localhost:3000/hi   a bunch of times!</span>

<span style="color: #a89984;">// </span><span style="color: #a89984;">Better yet, see the output programmatically...</span>
<span style="color: #98971a; font-weight: bold;">let</span> <span style="color: #076678;">axios</span> = require(<span style="color: #689d6a;">'axios'</span>)
<span style="color: #a89984;">// </span><span style="color: #a89984;">Press C-x C-j a bunch of times on the following expression &#9829;&#8255;&#9829;</span>
console.log((<span style="color: #98971a; font-weight: bold;">await</span> axios.get(<span style="color: #689d6a;">'http://localhost:3000/hi'</span>)).data)

<span style="color: #a89984;">// </span><span style="color: #a89984;">Consider closing the server when you're done with it.</span>
server.close()
</pre>
</div>

<p>
Just as “Emacs is a Lisp Machine”, one can use “VSCodeJS” to use
“VSCode as a JS Machine”.
See <a href="http://alhassy.com/vscode-is-itself-a-javascript-repl">http://alhassy.com/vscode-is-itself-a-javascript-repl</a>.
</p>

</details> </div>

<div><a style="width: 1%;float: left; padding: 0px" id="python" href="#python">🔗</a> <details class="float-child" style="background-color: #add8e6"><summary> <strong> <font face="Courier" size="3" color="green">Python </font> </strong> </summary>
<div class="org-center">
<p>
<a href="https://alhassy.github.io/PythonCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Python-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=python"></a>
</p>
</div>

<p>
We can set up a Python REPL in the background as follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">    <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-p now evaluates arbitrary Python code</span>
    (repl-driven-development [C-x C-p] <span style="color: #689d6a;">"python3 -i"</span>)
</pre>
</div>

<p>
Example use&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-python">1 + 2             <span style="color: #a89984;"># </span><span style="color: #a89984;">&#11157; 3</span>

<span style="color: #076678;">hello</span> = <span style="color: #689d6a;">'world!'</span>  <span style="color: #a89984;"># </span><span style="color: #a89984;">(No output; this is an effectful operation)</span>

<span style="color: #7c6f64; font-weight: bold;">print</span>(hello)      <span style="color: #a89984;"># </span><span style="color: #a89984;">&#11157; world!</span>

2 + <span style="color: #689d6a;">'hi'</span>          <span style="color: #a89984;"># </span><span style="color: #a89984;">&#128683; TypeError: unsupported operand type(s) for +</span>
</pre>
</div>

<p>
Learn more by reading&#x2026;  <a href="https://cs.lmu.edu/~ray/notes/pythonnetexamples/">Python: A Gentle Introduction to Socket Programming</a>
</p>

</details> </div>

<div><a style="width: 1%;float: left; padding: 0px" id="java" href="#java">🔗</a> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Java                                                        </font> </strong> </summary>
<div class="org-center">
<p>
<a href="https://alhassy.com/java-cheat-sheet.pdf"><img src="https://img.shields.io/badge/Java-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=coffeescript"></a>
</p>
</div>

<p>
We can set up a Java REPL in the background as follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(repl-driven-development [C-x C-j] <span style="color: #689d6a;">"jshell --enable-preview"</span> <span style="color: #7c6f64; font-weight: bold;">:prompt</span> <span style="color: #689d6a;">"jshell&gt;"</span>)
</pre>
</div>

<p>
Now, we can select the following and press <code>C-x C-j</code> to evaluate the Java code:
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #a89984;">// </span><span style="color: #a89984;">Ensure you're not fullscreen, and you'll see a dialog window appear.</span>
<span style="color: #98971a; font-weight: bold;">import</span> <span style="color: #076678; font-weight: bold;">javax</span>.<span style="color: #076678; font-weight: bold;">swing</span>.*;
JOptionPane.showMessageDialog(<span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #b57614;">JFrame</span>(), <span style="color: #689d6a;">"Super nice!"</span>);
</pre>
</div>

<p>
Or doing algebraic datatypes in Java:
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="color: #b57614;">sealed</span> <span style="color: #98971a; font-weight: bold;">interface</span> Maybe {
    <span style="color: #b57614;">record</span> <span style="color: #076678;">None</span>() <span style="color: #98971a; font-weight: bold;">implements</span> <span style="color: #b57614;">Maybe</span> {}
    <span style="color: #b57614;">record</span> <span style="color: #076678;">Just</span>(<span style="color: #b57614;">int</span> <span style="color: #076678;">x</span>) <span style="color: #98971a; font-weight: bold;">implements</span> <span style="color: #b57614;">Maybe</span> {}
}

<span style="color: #b57614;">var</span> <span style="color: #076678;">thisPrettyPrintsNicelyInTheREPL</span> = <span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #076678; font-weight: bold;">Maybe</span>.<span style="color: #b57614;">Just</span>(3);

<span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #076678; font-weight: bold;">Maybe</span>.<span style="color: #b57614;">Just</span>(3).equals(<span style="color: #98971a; font-weight: bold;">new</span> <span style="color: #076678; font-weight: bold;">Maybe</span>.<span style="color: #b57614;">Just</span>(3)) <span style="color: #a89984;">// </span><span style="color: #a89984;">yay</span>
</pre>
</div>

</details> </div>

<div><a style="width: 1%;float: left; padding: 0px" id="clojure" href="#clojure">🔗</a> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Clojure                            </font> </strong> </summary>
<p>
We can set up a REPL in the background as follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">   <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-k now evaluates arbitrary Clojure code</span>
   (repl-driven-development [C-x C-k] <span style="color: #689d6a;">"clojure"</span> <span style="color: #7c6f64; font-weight: bold;">:prompt</span> <span style="color: #689d6a;">"user=&gt;"</span>)
</pre>
</div>

<p>
For example&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-clojure">(+ 1 2) ;; ⮕ 3

(defn square [x] (* x x)) ;; ⮕ #'user/square
(square 3) ;; ⮕ 9
</pre>
</div>

</details> </div>

<div><a style="width: 1%;float: left; padding: 0px" id="haskell" href="#haskell">🔗</a> <details class="float-child" style="background-color: #add8e6"><summary> <strong> <font face="Courier" size="3" color="green">Haskell                                           </font> </strong> </summary>
<div class="org-center">
<p>
<a href="https://alhassy.github.io/HaskellCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Haskell-Colourful%C2%A0PDF%C2%A0CheatSheet-success?logo=awslambda"></a>
</p>
</div>

<p>
We can set up a REPL in the background as follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">   <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-h now evaluates arbitrary Haskell code</span>
   (repl-driven-development [C-x C-h] <span style="color: #689d6a;">"ghci"</span> <span style="color: #7c6f64; font-weight: bold;">:prompt</span> <span style="color: #689d6a;">"ghci&gt;"</span>)
</pre>
</div>

<p>
For example&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #a89984;">-- </span><span style="color: #a89984;">Sum of the first 100 squares</span>
<span style="color: #076678;">sum</span> [ x <span style="color: #076678;">**</span> 2 <span style="color: #076678;">|</span> x <span style="color: #076678;">&lt;-</span> [1<span style="color: #076678;">..</span>100]] <span style="color: #a89984;">-- </span><span style="color: #a89984;">&#8658; 338350.0</span>

<span style="color: #a89984;">-- </span><span style="color: #a89984;">The positive evens at-most 12</span>
[x <span style="color: #076678;">|</span> x <span style="color: #076678;">&lt;-</span> [1<span style="color: #076678;">..</span>12], x <span style="color: #076678;">`mod`</span> 2 <span style="color: #076678;">==</span> 0] <span style="color: #a89984;">-- </span><span style="color: #a89984;">[2,4,6,8,10,12]</span>

<span style="color: #a89984;">-- </span><span style="color: #a89984;">Define a function...</span>
<span style="color: #076678;">myLast</span> <span style="color: #076678;">=</span> head <span style="color: #076678;">.</span> reverse

<span style="color: #a89984;">-- </span><span style="color: #a89984;">Then use it...</span>
<span style="color: #076678;">myLast</span> [1, 2, 3] <span style="color: #a89984;">-- </span><span style="color: #a89984;">&#8658; 3</span>
</pre>
</div>

<p>
Note that Haskell has “typed holes” with the syntax <code>_A</code>:
</p>
<div class="org-src-container">
<pre class="src src-haskell">1 <span style="color: #076678;">+</span> _A  <span style="color: #a89984;">-- </span><span style="color: #a89984;">&#8658; Found hole: _A::a; it :: forall {a}. Num a = a</span>
</pre>
</div>

<p>
Another language with typed holes is Arend&#x2026;
</p>

</details> </div>

<div><a style="width: 1%;float: left; padding: 0px" id="recipe" href="#recipe">🔗</a> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Arend: Quickly making a terse Emacs interface for a language without one  </font> </strong> </summary>
<p>
The <a href="https://arend-lang.github.io/download#console-application">Arend Theorem Prover</a> has an IntelliJ interface (since it's a JetBrains proof
assistant), but no Emacs counterpart &#x2014;which may be annoying for Agda/Coq
programmers accustomed to Emacs but want to experiment with Arend.
</p>

<p>
We can set up an Arend REPL in the background as follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">    <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-a now evaluates arbitrary Arend code</span>
    (repl-driven-development [C-x C-a]
                             (format <span style="color: #689d6a;">"java -jar %s -i"</span>
                                     (f-expand <span style="color: #689d6a;">"~/Downloads/Arend.jar"</span>)))
</pre>
</div>

<p>
Then,
</p>
<div class="org-src-container">
<pre class="src src-arend">1 Nat.+ 1 -- ⇒ 2
:type 4  -- ⇒ Fin 5

-- Declare a constant
\\func f =&gt; 1
:type f -- ⇒ Nat
f -- ⇒ 1

-- Declare a polymorphic identity function, then use it
\\func id {A : \\Type} (a : A) =&gt; a
id 12  -- ⇒ 12

-- Arend has “typed holes”
1 Nat.+ {?}  -- ⇒ Nat.+{?}: Goal: Expectedtype: Nat
</pre>
</div>

</details> </div>

<div><a style="width: 1%;float: left; padding: 0px" id="purescript" href="#purescript">🔗</a> <details class="float-child" style="background-color: #add8e6"><summary> <strong> <font face="Courier" size="3" color="green">PureScript  </font> </strong> </summary>
<p>
First <code>brew install spago</code>, then we can set up a PureScript REPL in the background
as follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">    <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-p now evaluates arbitrary PureScript code</span>
    (repl-driven-development [C-x C-p] <span style="color: #689d6a;">"spago repl"</span>)
</pre>
</div>

<p>
For example&#x2026;.
</p>
<div class="org-src-container">
<pre class="src src-purescript">import Prelude

-- Define a function
add1 = (\x -&gt; x + 1)

-- Use the function
add1 2    -- ⇒ 3

-- Experiment with a typed hole
1 + ?A  -- ⇒ Hole ?A has the inferred type Int
</pre>
</div>

</details> </div>

<div><a style="width: 1%;float: left; padding: 0px" id="idris" href="#idris">🔗</a> <details class="float-child" style="background-color: nil"><summary> <strong> <font face="Courier" size="3" color="green">Idris  </font> </strong> </summary>
<p>
First <code>brew install idris2</code>, then we can set up an Idris REPL in the background as
follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">    <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-i now evaluates arbitrary Idris code</span>
    (repl-driven-development [C-x C-i] <span style="color: #689d6a;">"idris2"</span>)
</pre>
</div>

<p>
Here's some random code&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-purescript">-- Like Lisp, Idris uses “the” for type annotations
the Nat 4  -- ⇒ 4 : Nat

with List sum [1,2,3] -- ⇒ 6

-- defining a new type (REPL specific notation)
:let data Foo : Type where Bar : Foo

:t Bar -- ⇒ Foo

-- Experiment with a typed hole [Same notation as Haskell]
1 + ?A -- prim__add_Integer 1 ?A
</pre>
</div>

</details> </div>

<div><a style="width: 1%;float: left; padding: 0px" id="racket" href="#racket">🔗</a> <details class="float-child" style="background-color: #add8e6"><summary> <strong> <font face="Courier" size="3" color="green">Racket  </font> </strong> </summary>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Racket is a modern programming language in the Lisp/Scheme family.</td>
</tr>
</tbody>
</table>

<p>
First <code>brew install --cask racket</code>, then we can set up an Racket REPL in the
background as follows&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">    <span style="color: #a89984;">;; </span><span style="color: #a89984;">C-x C-i now evaluates arbitrary Racket code</span>
    (repl-driven-development [C-x C-r] <span style="color: #689d6a;">"racket -I slideshow"</span>)
</pre>
</div>

<p>
Here's some random code&#x2026;
</p>
<div class="org-src-container">
<pre class="src src-racket">(define (series mk) (hc-append 4 (mk 5) (mk 10) (mk 20)))

;; Shows 3 circles of increasing radius, in an external window
(show-pict (series circle))
</pre>
</div>

<p>
Meeting Racket for the first time is probably best done with <i>DrRacket</i>.
</p>

</details> </div>
</div>
]]></description>
  <category><![CDATA[repl-driven-development]]></category>
  <category><![CDATA[vscode]]></category>
  <category><![CDATA[emacs]]></category>
  <category><![CDATA[javascript]]></category>
  <category><![CDATA[java]]></category>
  <category><![CDATA[python]]></category>
  <category><![CDATA[lisp]]></category>
  <category><![CDATA[clojure]]></category>
  <category><![CDATA[haskell]]></category>
  <category><![CDATA[arend]]></category>
  <category><![CDATA[purescript]]></category>
  <category><![CDATA[idris]]></category>
  <category><![CDATA[racket]]></category>
  <link>https://alhassy.github.io/repl-driven-development.html</link>
  <guid>https://alhassy.github.io/repl-driven-development.html</guid>
  <pubDate>Fri, 08 Sep 2023 00:00:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[Arabic CheatSheet]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-arabic.html"><img src="https://img.shields.io/badge/-arabic-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-cheat-sheet.html"><img src="https://img.shields.io/badge/-cheat_sheet-grey?logo=nil"></a></center>
<center><a href="../images/arabic-irab.png" class="tooltip" title="Image credit “../images/arabic-irab.png”"><img src="../images/arabic-irab.png" alt="Article image" style="border: 2px solid black;" width="100%" height="100%" align="top"/></a></center>














<div class="abstract" style="border: 1px solid black;padding: 1%; margin-top: 1%; margin-bottom: 1%;margin-right: 10%; margin-left: 10%; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>
<div class="org-center">
This is a quick reference of concepts in the Arabic language.

<p>
<a href="https://alhassy.com/arabic-cheat-sheet.pdf"><img src="https://img.shields.io/badge/PDF-colorful_cheat_sheet-success?logo=read-the-docs"></a>
</p>

<p>
<a href="https://twitter.com/intent/tweet?text=This looks super neat (•̀ᴗ•́)و::&url=https://alhassy.com/arabic-cheat-sheet"><img src="https://img.shields.io/twitter/url?url=https://alhassy.com/arabic-cheat-sheet"></a>
<a href="https://www.buymeacoffee.com/alhassy"><img src="https://img.shields.io/badge/-buy_me_a_coffee-gray?logo=buy-me-a-coffee"></a>
</p>
</div>

</div>

<div id="outline-container-Sentences-without-Verbs" class="outline-2">
<h2 id="Sentences-without-Verbs"><span class="section-number-2">1.</span> <a href="#Sentences-without-Verbs">Sentences without Verbs</a></h2>
<div class="outline-text-2" id="text-Sentences-without-Verbs">
<p>
English sentences are usually of the form <i>Subject + Verb + Object</i>.  Recall that a <i>verb</i> is an “action word”; the <i>subject</i>
is the one doing the action; and the <i>object</i> is the one having the action done to them.
Arabic sentences do not need verbs! These are known as <i>equational, non-verbal, sentences</i>.
</p>

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


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

<col  class="org-left" />

<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-center"><b><span style="color:blue;">I</span></b> am <b><span style="color:orange;">Samir</span></b></td>
<td class="org-left">≈</td>
<td class="org-center">  <span style="color:orange;" dir="ltr">سمير</span>   <span style="color:blue;" dir="ltr">انا</span></td>
</tr>

<tr>
<td class="org-center"><b><span style="color:blue;">He</span></b> is <b><span style="color:orange;">tall</span></b></td>
<td class="org-left">≈</td>
<td class="org-center">  <span style="color:orange;" dir="ltr">طويل</span>   <span style="color:blue;" dir="ltr">هو</span></td>
</tr>
</tbody>
</table>

<p>
Each sentence above has (1) a <i>subject</i> [the topic of the sentence: A noun or
 pronoun]  and (2) a <i><b><span style="color:orange;">predicate</span></b></i> [information about the
 topic; e.g., a noun or an adjective].
</p>
<ul class="org-ul">
<li>Notice the English has “am”, which is not needed in Arabic.</li>
<li>A <i>noun</i> is a person, place, or thing.</li>
<li>A <i><b><span style="color:blue;">pronoun</span></b></i> is a word such as “He, him, her, this, that, &#x2026;”</li>
</ul>

<p>
The predicate can either be a (pro)noun that renames the subject &#x2014;as in
\arb{سميرُ طالبٌ} which "renames" the subject <i>Samir</i> to the subject <i>doctor</i>&#x2014;; or
the predicate can be an adjective that describes the subject &#x2014;as in \arb{سميرُ
طويلٌ}. If the predicate is a <i>definite</i> noun (discussed below!), then it does not
use nunation; e.g., \arb{هو الطالبُ} <i>He is the doctor</i>.
</p>
</div>
</div>

<div id="outline-container-Adjectives" class="outline-2">
<h2 id="Adjectives"><span class="section-number-2">2.</span> <a href="#Adjectives">Adjectives</a></h2>
<div class="outline-text-2" id="text-Adjectives">
<p>
<i>Adjectives</i>, “descriptive words”, follow nouns and must agree with them in gender, number, definiteness, and case.
<i>The agreement is what distinguishes a noun-adjective phrase from an equational sentence!</i>
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">the new book</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">الكتابُ الجديدُ</span></td>
</tr>

<tr>
<td class="org-left">The book is new.</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">الکتابُ جديدُ.</span></td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-Questions" class="outline-2">
<h2 id="Questions"><span class="section-number-2">3.</span> <a href="#Questions">Questions</a></h2>
<div class="outline-text-2" id="text-Questions">
<p>
The <i>question marker</i>   <span dir="ltr">هَلْ</span> is placed at the start of a statement to turn it into a question.
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">You are a student.</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">انت طالب.</span></td>
</tr>

<tr>
<td class="org-left">Are you a student?</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">هل انت طالب؟</span></td>
</tr>
</tbody>
</table>

<ul class="org-ul">
<li><span dir="ltr">ما</span> has <i>many uses</i> in Arabic, one of them being the question word “what” ---<i>which can only be used with things,
not people!</i></li>

<li><span dir="ltr">مَنْ</span> [`men'] means “who” and is used to refer to people.
− (Becareful not to confuse this with the preposition <i>from</i>,   <span dir="ltr">مِنْ</span> [`min']!)</li>

<li><span dir="ltr">أيْنَ</span> means “where”.</li>
</ul>

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


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

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">What is this?</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">ما هٰذا؟</span></td>
</tr>

<tr>
<td class="org-left">Who is this?</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">من هذا؟</span></td>
</tr>

<tr>
<td class="org-left">Where are you from?</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">مِنْ أين أنتَ؟</span></td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-Inflection-Conjugation" class="outline-2">
<h2 id="Inflection-Conjugation"><span class="section-number-2">4.</span> <a href="#Inflection-Conjugation">Inflection &amp; Conjugation</a></h2>
<div class="outline-text-2" id="text-Inflection-Conjugation">
<p>
The “shape” of an Arabic word changes to tell us information about the word.
</p>
<ul class="org-ul">
<li>“Conjugation”: Verbs change with <i>who</i> is doing the action.</li>
<li>“Case”,   <span dir="ltr">الاعراب</span>: Nouns, and adjectives, change to tell us whether they are doing an action, are having
something done to them, or own/possess something.</li>
</ul>

<p>
For example, in English, there are 3 ways to refer to oneself: <b><span style="color:green;">I</span>, <span style="color:red;">me</span>, <span style="color:blue;">my</span></b>.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><i><b><span style="color:blue;">My</span></b> cat saw <b><span style="color:red;">me</span></b>, and <b><span style="color:green;">I</span></b> jumped!</i></td>
</tr>
</tbody>
</table>
<p>
The <b>shape</b> of the word depends on its <b>case</b>. Here's the rules:
</p>
<ul class="org-ul">
<li><i>(Nominative!)</i> When I am doing something, I say: <b><span style="color:green;"> I</span> did it</b></li>
<li><i>(Accusative!)</i> When something is being done to me, I say: <b>It was done to <span style="color:red;">me</span>.</b></li>
<li><i>(Genitive!)</i> When I have an item, I say: <b><span style="color:blue;">My</span> thing&#x2026;.</b></li>
</ul>

<p>
So, in English, the word used to refer to <i>myself</i> changes depending on what is happening <b><span style="color:green;">by</span></b> me, <b><span style="color:red;">to</span></b> me, or
<b><span style="color:blue;">of</span></b> me / what I own.
</p>
</div>
</div>

<div id="outline-container-Nominative-Case" class="outline-2">
<h2 id="Nominative-Case"><span class="section-number-2">5.</span> <a href="#Nominative-Case">Nominative Case</a></h2>
<div class="outline-text-2" id="text-Nominative-Case">
<p>
<i>Case</i> refers to the form a word &#x2014;mostly nouns and adjectives&#x2014; take depending on their function in a sentence.  The
subject of any sentence will always be in the <i>nominative case</i>, which is indicated by placing a   <span dir="ltr">ـُـ</span> at the end of
the word. The only other time a word will be in the nominative is if it is the predicate of a non-verbal sentence.
</p>

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


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

<col  class="org-left" />

<col  class="org-center" />

<col  class="org-left" />

<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-center">He is <i>the</i> student</td>
<td class="org-left">≈</td>
<td class="org-center">  <span dir="ltr">هو الطالبُ</span></td>
<td class="org-left">&#xa0;</td>
<td class="org-center"><i>huwa al-talib-u</i></td>
</tr>

<tr>
<td class="org-center">He is <i>a</i> student</td>
<td class="org-left">≈</td>
<td class="org-center">  <span dir="ltr">هو طالبٌ</span></td>
<td class="org-left">&#xa0;</td>
<td class="org-center"><i>huwa talib-un</i></td>
</tr>
</tbody>
</table>


<p>
Pronouns, such as   <span dir="ltr">انا} and \arb{هذا</span>, do not have case endings.
</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" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">I am the teacher</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">انا المدرسُ</span></td>
<td class="org-left">&#xa0;</td>
<td class="org-left"><i>ana al-mudaras-u</i></td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-arb-Definiteness" class="outline-2">
<h2 id="arb-Definiteness"><span class="section-number-2">6.</span> <a href="#org0cc8a70">  <span dir="ltr">معرفة</span> Definiteness</a></h2>
<div class="outline-text-2" id="text-arb-Definiteness">
<p>
A word is considered <b>definite   <span dir="ltr">معرفة</span></b> when it refers to something specific in
the world, and <b>indefinite   <span dir="ltr">نكرة</span></b> when it does not. For example, “a car” or
“cars” do not refer to anything specific in the world and thus both examples are
indefinite. Conversely, “my car” or “my cars” both refer to specific / known
objects in the world and thus both examples are definite.
</p>

<p>
When is a word definite?
</p>
<ol class="org-ol">
<li>If it is a proper name such as   <span dir="ltr">احمد</span>.</li>
<li>If it has the <i>definite article</i>   <span dir="ltr">ال</span>/“the” in front of it.</li>
<li>If it is a pronoun &#x2014;i.e., it already refers to something.
Such as   <span dir="ltr">هو</span> or   <span dir="ltr">هذا</span>.</li>
<li>If it is owned by something; e.g., <i>book</i> is definite in both <i>John's book</i> (Idaafa) and <i>his book</i> (Possessive pronoun
ending). <i>Both concepts are discussed below!</i></li>
</ol>
</div>
</div>

<div id="outline-container-Nunnation-Tanween" class="outline-2">
<h2 id="Nunnation-Tanween"><span class="section-number-2">7.</span> <a href="#Nunnation-Tanween">Nunnation/Tanween</a></h2>
<div class="outline-text-2" id="text-Nunnation-Tanween">
<p>
Arabic does not have an indefinite article: To make a word indefinite, we double <b>its</b> case ending; with the second
instance pronounced as   <span dir="ltr">ن</span>, “n”.  This doubling of case endings, and adding the sound “n”, is known as <i>Tanween</i>.  For
the nominative case, the   <span dir="ltr">ــُــ</span> is written twice but often written in the shape   <span dir="ltr">ــٌــ</span>.
</p>

<p>
An indefinite adjective (usually one without   <span dir="ltr">ال</span>) will have tanween:
</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" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">The student is new</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">الطالبُ جديدٌ</span></td>
<td class="org-left">&#xa0;</td>
<td class="org-left"><i>al-talib-u jadeed-un</i></td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-Case-endings-of-Equational-Sentences" class="outline-2">
<h2 id="Case-endings-of-Equational-Sentences"><span class="section-number-2">8.</span> <a href="#Case-endings-of-Equational-Sentences">Case endings of Equational Sentences</a></h2>
<div class="outline-text-2" id="text-Case-endings-of-Equational-Sentences">
<p>
From the preceding discussions: <i>Both the subject and predicate of an equational
sentence should be in the nominative!</i> Remember that the subject can be any
(pro)noun and the predicate is any (pro)noun or adjective &#x2014;if it is an
adjective, then it is indefinite and so ends in   <span dir="ltr">ــٌــ</span>.
</p>
</div>
</div>

<div id="outline-container-Helping-Vowels-for-arb" class="outline-2">
<h2 id="Helping-Vowels-for-arb"><span class="section-number-2">9.</span> <a href="#Helping-Vowels-for-arb">Helping Vowels for    <span dir="ltr">أَل</span></a></h2>
<div class="outline-text-2" id="text-Helping-Vowels-for-arb">
<ol class="org-ol">
<li><p>
The hamza-fatha of the definite article   <span dir="ltr">أَل</span> will always be replaced
by the final vowel of the preceding word; thus the two words <i>sound like one word</i>!
</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>
<tbody>
<tr>
<td class="org-left">You (m) are the director</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">انتَ المُدير</span></td>
<td class="org-left"><i>antal-mudiir</i></td>
</tr>

<tr>
<td class="org-left">You (f) are the director</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">انتِ المُديرة</span></td>
<td class="org-left"><i>antil-mudiira</i></td>
</tr>
</tbody>
</table></li>

<li><p>
When   <span dir="ltr">أَل} follows a “sun letter”</span> is also not pronounced.
</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>
<tbody>
<tr>
<td class="org-left">You are the student</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">انتَ الطّالب</span></td>
<td class="org-left"><i>antat-talib</i></td>
</tr>
</tbody>
</table></li>

<li><p>
Most words end in vowels, since Arabic case endings are vowels.
If a word does not end in a vowel, such as   <span dir="ltr">هَلْ</span>, then
we add a <i>helping kasra vowel</i>:
</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>
<tbody>
<tr>
<td class="org-left">Is the director an idiot?</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">هَلِ المُديرُ بليد؟</span></td>
<td class="org-left"><i>halil-mudiiru baled?</i></td>
</tr>
</tbody>
</table>

<p>
The only exception to this rule is the word   <span dir="ltr">مَنْ</span>, which gets a helping
<i>fatha</i> vowel.
</p></li>
</ol>
</div>
</div>

<div id="outline-container-arb-latex-hspace-1em-This-is-a-X-This-X-This-is-the-X" class="outline-2">
<h2 id="arb-latex-hspace-1em-This-is-a-X-This-X-This-is-the-X"><span class="section-number-2">10.</span> <a href="#org0cc8a70">  <span dir="ltr">اسماء الإشارة</span>  &#x2014;“This is a X” &#x2014;“This X” &#x2014;“This is the X”</a></h2>
<div class="outline-text-2" id="text-arb-latex-hspace-1em-This-is-a-X-This-X-This-is-the-X">
<ol class="org-ol">
<li>“This”   <span dir="ltr">هٰذا</span> is used to refer to things that are close by, whereas “that”   <span dir="ltr">ذالِكَ</span> refers to objects that are
distant or is used in a constrast:   <span dir="ltr">هٰذا طالبٌ وذلِكَ مُدرِّسٌ</span>, <i>This is a student and that is a teacher</i>.
The feminine forms of “this” and “that” are
  <span dir="ltr">هٰذِهِ</span> and   <span dir="ltr">تِلْكَ</span>.</li>

<li>Whenever any of these 4 words is followed by a definite noun, we have <b>one unit</b>
meaning “this noun”.
<ul class="org-ul">
<li>Such phrases often serve as the subjects of an equational sentence.</li>
</ul></li>

<li>We can <b>separate</b> this one unit into two pieces by inserting a pronoun
in-the-middle, which gives us “This is the noun”.</li>
</ol>

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


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

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-right">1.</th>
<th scope="col" class="org-left">This is a book.</th>
<th scope="col" class="org-left">≈</th>
<th scope="col" class="org-left">  <span dir="ltr">هذا كتاب.</span></th>
</tr>

<tr>
<th scope="col" class="org-right">2a.</th>
<th scope="col" class="org-left">this book&#x2026;</th>
<th scope="col" class="org-left">≈</th>
<th scope="col" class="org-left">  <span dir="ltr">هذا الكتاب...</span></th>
</tr>

<tr>
<th scope="col" class="org-right">2b.</th>
<th scope="col" class="org-left">This book is heavy.</th>
<th scope="col" class="org-left">≈</th>
<th scope="col" class="org-left">  <span dir="ltr">هذا الكتابُ ثقيلٌ.</span></th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-right">3.</td>
<td class="org-left">This is the book.</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">هذا هو الكتابُ.</span></td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-The-Accusative-Case" class="outline-2">
<h2 id="The-Accusative-Case"><span class="section-number-2">11.</span> <a href="#The-Accusative-Case">The Accusative Case</a></h2>
<div class="outline-text-2" id="text-The-Accusative-Case">
<p>
The Accusative Case is mostly used for the direct objects of verbs:
It is indicated by a fatha.
For example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">I studied the book.</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">دَرَسْتُ الكتابَ.</span></td>
</tr>
</tbody>
</table>

<p>
<b>Notice</b> that above we did not write   <span dir="ltr">أنا</span>, “I”, since verbs change shape to tell us who is doing the action!
(Changes to nouns is called <i>case</i>; changes to verbs is called <i>conjugation!</i>)
</p>

<p>
There is one more rule.  <i>To place an indefinite word not ending in   <span dir="ltr">ة} in the accusative</span>
&#x2014;which makes the sound “an”</i>.  E.g., <i>I studied a book</i> becomes   <span dir="ltr">درستُ کتاباً</span>.
</p>
</div>
</div>

<div id="outline-container-Genitive-Case" class="outline-2">
<h2 id="Genitive-Case"><span class="section-number-2">12.</span> <a href="#Genitive-Case">Genitive Case</a></h2>
<div class="outline-text-2" id="text-Genitive-Case">
<p>
The genitive case is used for a word following a preposition or a word occuring
as the second or later term in an Idaafa construction (discussed below).
</p>

<p>
Prepositions are words like   <span dir="ltr">عن، الی، لِ، بِ، في، علی، مِن، قبل</span>: They are written
“pre”ceeding a word and tell us something about its “position”.
</p>

<p>
The genitive case ending is a final kasra for a definite word and two kasras for
an indefinite word, with the second kasra pronounded as   <span dir="ltr">ن</span> as in the Nominative case.
</p>
<p>
Let's explain the following example.
</p>

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


<colgroup>
<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-center">  <span dir="ltr">انتَ المُديرُ في هٰذا المکتبِ</span></td>
</tr>

<tr>
<td class="org-center">You are the director in this office.</td>
</tr>
</tbody>
</table>

<p>
Here   <span dir="ltr">انت المدير</span> is an equational sentence followed by a prepositional phrase.
Both the subject and predicate of an equational sentence should be in the nominative,
but   <span dir="ltr">انت} is a pronoun and so does not take case. Moreover</span> is
definite, it takes a single dhamma. Finally, since   <span dir="ltr">هذا المكتب</span> is a demonstrative
followed by a definite it is treated grammatically as a single word, which means
the (genitive) case ending goes at the very end of   <span dir="ltr">المكتب</span>.
</p>
</div>
</div>
<div id="outline-container-Idaafa" class="outline-2">
<h2 id="Idaafa"><span class="section-number-2">13.</span> <a href="#Idaafa">Idaafa</a></h2>
<div class="outline-text-2" id="text-Idaafa">
<p>
Idaafa means “addition“, or “annexation“, and it is used to indicate possesion in Arabic &#x2014;just like how English uses <i>'s</i> to indicate possession.
</p>

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


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">John's book</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left">the book of John</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">كتابُ جون</span></td>
</tr>
</tbody>
</table>

<p>
Idaafa, possesion, is formed by putting nouns next to each other &#x2014;to make a super-duper big noun, formally called a
<i>noun-phrase</i>. That is all.
</p>
</div>

<div id="outline-container-Noun-phrases-of-Idaafa" class="outline-3">
<h3 id="Noun-phrases-of-Idaafa"><span class="section-number-3">13.1.</span> <a href="#Noun-phrases-of-Idaafa">Noun-phrases of Idaafa</a></h3>
<div class="outline-text-3" id="text-Noun-phrases-of-Idaafa">
<p>
Noun-phrases are similar to nouns:
</p>
<ul class="org-ul">
<li>This noun-phrase is (in)definite exactly when its <i>final</i>
noun is (in)definite.</li>
<li>This noun-phrase takes case endings on its <i>first</i> noun.
<ul class="org-ul">
<li>All other words in the noun-phrase must be in the genitive case.</li>
<li>Only the <i>final</i> noun can have nunnation.</li>
</ul></li>
</ul>

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


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">This is an office director's car</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left">This is a car of a director of an office</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">هذه سيارةُ مديرِ مکتبٍ</span></td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-Noun-phrases-and-this-that" class="outline-3">
<h3 id="Noun-phrases-and-this-that"><span class="section-number-3">13.2.</span> <a href="#Noun-phrases-and-this-that">Noun-phrases and “this”/“that”</a></h3>
<div class="outline-text-3" id="text-Noun-phrases-and-this-that">
<p>
Remember that demonstratives form noun-phrases and so can be used
in-place of a noun in an Idaafa.
</p>

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


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

<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-center">The director of this office is stupid.</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-center">  <span dir="ltr">مُديرُ هذا المكتبِ بليدٌ</span></td>
</tr>
</tbody>
</table>

<p>
This is an equational sentence. The subject is   <span dir="ltr">مدير هذا المکتب</span>
which needs to be in the nominative case, and it is
definite since the last word is definite, thus only one dhamma needs to be
added (to the first noun; and the last noun gets no nunnation).
The topic is   <span dir="ltr">بليد</span> which must also be in the nominative indefinite.
</p>
</div>
</div>
</div>

<div id="outline-container-Descriptions-for-Idaafa" class="outline-2">
<h2 id="Descriptions-for-Idaafa"><span class="section-number-2">14.</span> <a href="#Descriptions-for-Idaafa">Descriptions for Idaafa</a></h2>
<div class="outline-text-2" id="text-Descriptions-for-Idaafa">
<p>
In English, a descriptive word can come before the owned item: <i>John's heavy book</i>. In Arabic, adjectives must follow the
Idaafa and cannot interrupt it:   <span dir="ltr">کتاب جون الثقيل</span>.  For example, here is an equational sentence whose subject is a
3-term Idaafa followed by the adjective <i>Arabic</i> (remember only the last term in an Idaafa can have   <span dir="ltr">ال</span>):
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><i>The study of Arabic grammar is enjoyable</i></td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">دراسةُ قواعدِ اللغةِ العربية مُمتعةٌ</span></td>
</tr>
</tbody>
</table>
</div>

<div id="outline-container-Agreement" class="outline-3">
<h3 id="Agreement"><span class="section-number-3">14.1.</span> <a href="#Agreement">Agreement</a></h3>
<div class="outline-text-3" id="text-Agreement">
<p>
Since adjectives come after an Idaafa, how do we describe different parts of the Idaafa?
Easy; adjectives must “agree” with the word they describe: They must have the same
gender, number, definiteness, and case as the word being described.
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">The teacher's <i>new book</i> is in the office.</td>
<td class="org-left">≈</td>
<td class="org-left">.  <span dir="ltr">كتابُ المدرسِ الجديدُ في المکتبِ</span></td>
</tr>

<tr>
<td class="org-left">The <i>new teacher</i>'s book is in the office.</td>
<td class="org-left">≈</td>
<td class="org-left">.  <span dir="ltr">کتابُ المدرسِ الجديدِ في المکتبِ</span></td>
</tr>
</tbody>
</table>

<p>
(Usually only the last term of an Idaafa is actually modified by an adjective.)
</p>
</div>
</div>

<div id="outline-container-Multiple-adjectives" class="outline-3">
<h3 id="Multiple-adjectives"><span class="section-number-3">14.2.</span> <a href="#Multiple-adjectives">Multiple adjectives</a></h3>
<div class="outline-text-3" id="text-Multiple-adjectives">
<p>
Of-course you can modify multiple words, or use multiple modifiers on the same word!
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">the <i>new</i> student of the <i>Americian</i> university</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">طالبةُ الجامعةِ الامريکيةِ الجديدةُ</span></td>
</tr>

<tr>
<td class="org-left">the student of the <i>new Americian</i> university</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">طالبةُ الجامعةِ الامريکيةِ الجديدةِ</span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

<div id="outline-container-Sound-Plurals" class="outline-2">
<h2 id="Sound-Plurals"><span class="section-number-2">15.</span> <a href="#Sound-Plurals">Sound Plurals</a></h2>
<div class="outline-text-2" id="text-Sound-Plurals">
<p>
A <i>sound plural</i> is an ending added to a word to make it plural.
The ending communicates gender, case, and definiteness.
</p>

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


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

<col  class="org-center" />

<col  class="org-center" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-center">&#xa0;</th>
<th scope="col" class="org-center">Nominative</th>
<th scope="col" class="org-center">Genitive &amp; Accusative</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-center">Masculine indefinite</td>
<td class="org-center">  <span dir="ltr">ـونَ</span></td>
<td class="org-center">  <span dir="ltr">ـينَ</span></td>
</tr>

<tr>
<td class="org-center">Masculine definite</td>
<td class="org-center">  <span dir="ltr">ـي</span></td>
<td class="org-center">  <span dir="ltr">ـو</span></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-center">Feminine definite</td>
<td class="org-center">  <span dir="ltr">ـاتٌ</span></td>
<td class="org-center">  <span dir="ltr">ـاتٍ</span></td>
</tr>

<tr>
<td class="org-center">Feminine definite</td>
<td class="org-center">  <span dir="ltr">ـاتُ</span></td>
<td class="org-center">  <span dir="ltr">ـاتِ</span></td>
</tr>
</tbody>
</table>

<p>
<i>Notice that the usual small nunation symbols making the   <span dir="ltr">ن</span>-sound actual
become the   <span dir="ltr">ن</span>-letter!</i> <b>As such, the actual   <span dir="ltr">ن</span> is written or not depending
on the general rules of nunnation.</b>
</p>

<p>
In Arabic, you must learn the plural of each word when you learn its singular form.
However, many words referring to <i>human</i> males have sound plurals.
Likewise, many words ending in   <span dir="ltr">ة</span> have a feminine plural
by replacing the final   <span dir="ltr">ة</span> with   <span dir="ltr">ـات</span>.
</p>

<p>
For example,
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">I saw the directors.</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">رَأیْتُ المُدیرينَ.</span></td>
</tr>

<tr>
<td class="org-left">The directors are superb.</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">المُديرونَ ممتازونَ.</span></td>
</tr>

<tr>
<td class="org-left">The (female) directors are superb.</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">المُديراتُ ممتازاتُ.</span></td>
</tr>

<tr>
<td class="org-left">I saw the newspaper reporters.</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">شاهدتُ مراسلي الجريدةِ.</span></td>
</tr>
</tbody>
</table>
</div>

<div id="outline-container-Sound-Plurals-and-Possessive-Endings" class="outline-3">
<h3 id="Sound-Plurals-and-Possessive-Endings"><span class="section-number-3">15.1.</span> <a href="#Sound-Plurals-and-Possessive-Endings">Sound Plurals and Possessive Endings</a></h3>
<div class="outline-text-3" id="text-Sound-Plurals-and-Possessive-Endings">
<p>
Remember: Possessive endings make words genitive &amp; definite, and so nunnation cannot apply.
</p>

<div style="column-rule-style: none nil;column-count: 2;">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">his teacher</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">مُدرسهُ</span></td>
</tr>

<tr>
<td class="org-left">his teachers</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">مُدرسيهِ</span></td>
</tr>
</tbody>
</table>


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


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

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">my teacher</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">مُدرسي</span></td>
</tr>

<tr>
<td class="org-left">my teachers</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">مُدرسيّ</span></td>
</tr>
</tbody>
</table>

</div>

<p>
Let's talk more about possessive endings&#x2026; ;-)
</p>
</div>
</div>
</div>

<div id="outline-container-Pronouns" class="outline-2">
<h2 id="Pronouns"><span class="section-number-2">16.</span> <a href="#Pronouns">Pronouns</a></h2>
<div class="outline-text-2" id="text-Pronouns">
<p>
A <i>pronoun</i> is a word that stands-in for a noun. For example, below we refer to someone
in 3 different ways:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><i><b><span style="color:blue;">His</span></b> cat saw <b><span style="color:red;">him</span></b>, and <b><span style="color:green;">he</span></b> jumped!</i></td>
</tr>
</tbody>
</table>
</div>

<div id="outline-container-Personal-Pronouns" class="outline-3">
<h3 id="Personal-Pronouns"><span class="section-number-3">16.1.</span> <a href="#Personal-Pronouns">Personal Pronouns</a></h3>
<div class="outline-text-3" id="text-Personal-Pronouns">
<p>
A <b><span style="color:green;">personal pronoun</span></b> replaces a noun that refers to a person (e.g., Jasim ate ≈ <i>he</i> ate),
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">singular</th>
<th scope="col" class="org-left">plural</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">1</td>
<td class="org-left">  <span dir="ltr">أنا</span>   I</td>
<td class="org-left">  <span dir="ltr">نَحْنُ</span>   we</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">2m</td>
<td class="org-left">  <span dir="ltr">أَنْتَ</span>  you</td>
<td class="org-left">  <span dir="ltr">انْتُمْ</span>   you</td>
</tr>

<tr>
<td class="org-left">2f</td>
<td class="org-left">  <span dir="ltr">أَنْتِ</span>  you</td>
<td class="org-left">  <span dir="ltr">انتُنَّ</span>   you</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">3m</td>
<td class="org-left">  <span dir="ltr">هُوَ</span>  he/it</td>
<td class="org-left">  <span dir="ltr">هُمْ</span>  they</td>
</tr>

<tr>
<td class="org-left">3f</td>
<td class="org-left">  <span dir="ltr">هِيَ</span>  she/it</td>
<td class="org-left">  <span dir="ltr">هُنَّ</span>  they</td>
</tr>
</tbody>
</table>

<p>
When <b>I</b> am talking, the speaker is the “first person” (“1”); when taking <b>about you</b>, then you are the “second person” and
may be masculine (“2m”) or feminine (“2f”), or a group of you (“plural”); finally, when talking about someone who is <b>not
here</b> in the conversation, they are in the “third person” (“3m, 3f”).
</p>
</div>
</div>

<div id="outline-container-Possessive-Object-Pronouns" class="outline-3">
<h3 id="Possessive-Object-Pronouns"><span class="section-number-3">16.2.</span> <a href="#Possessive-Object-Pronouns">Possessive &amp; Object Pronouns</a></h3>
<div class="outline-text-3" id="text-Possessive-Object-Pronouns">
<p>
A <b><span style="color:blue;">possessive pronoun</span></b> replaces a noun that involves ownership (e.g., Jasim's book ≈ <i>his</i> book), while an
<b><span style="color:red;">object pronoun</span></b> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <i>him</i>.)
</p>

<p>
In Arabic, <i>possessive and object pronouns</i> are <i>attached pronouns</i>; they are <b>joined to the end</b> of a word: For example,
<i>house</i>   <span dir="ltr">بیت</span> becomes <i>my house</i>   <span dir="ltr">بیتِي</span> and from <i>he helped</i>   <span dir="ltr">نَصَرَ</span> we get   <span dir="ltr">نَصَرَني</span> <i>he helped me</i>.
Arabic's object &amp; possessive pronouns are the same, except for the “my/me” case:
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">singular</th>
<th scope="col" class="org-left">plural</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">1</td>
<td class="org-left">  <span dir="ltr">ـِي</span> my;   <span dir="ltr">ـني</span> me</td>
<td class="org-left">  <span dir="ltr">ـنَا</span>   our/us</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">2m</td>
<td class="org-left">  <span dir="ltr">ـكَ</span>  your/you</td>
<td class="org-left">  <span dir="ltr">كُمْ</span>  your/you</td>
</tr>

<tr>
<td class="org-left">2f</td>
<td class="org-left">  <span dir="ltr">ـكِ</span>  your/you</td>
<td class="org-left">  <span dir="ltr">كُنَّ</span>  your/you</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">3m</td>
<td class="org-left">  <span dir="ltr">ـَهُ</span>   his/him</td>
<td class="org-left">  <span dir="ltr">ـهُمْ</span>   their/them</td>
</tr>

<tr>
<td class="org-left">3f</td>
<td class="org-left">  <span dir="ltr">ـَهَا</span>   hers/her</td>
<td class="org-left">  <span dir="ltr">ـهُنَّ</span>    their/them</td>
</tr>
</tbody>
</table>

<p>
The dhamma of the endings   <span dir="ltr">ـهُ ، ـهُم ، ـهُنَّ</span> becomes a kasra whenver these endings come after a kasra or a   <span dir="ltr">ي</span>.
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">an office</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">مکتبٌ</span></td>
</tr>

<tr>
<td class="org-left">in an office</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">في مکتبِ</span></td>
</tr>

<tr>
<td class="org-left">in his office</td>
<td class="org-left">≈</td>
<td class="org-left">  <span dir="ltr">في مکتبِهِ</span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

<div id="outline-container-Kinds-of-Arabic-Verbs" class="outline-2">
<h2 id="Kinds-of-Arabic-Verbs"><span class="section-number-2">17.</span> <a href="#Kinds-of-Arabic-Verbs">Kinds of Arabic Verbs</a></h2>
<div class="outline-text-2" id="text-Kinds-of-Arabic-Verbs">
<dl class="org-dl">
<dt>Sound Verbs</dt><dd>Verbs with no \arb{و} or \arb{ي} as a root.</dd>
<dt>Defective Verbs</dt><dd>Verbs whose last root is either \arb{و} or \arb{ي}.</dd>
<dt>Hollow Verbs</dt><dd>Verbs whose middle root is either \arb{و} or \arb{ي}.</dd>
<dt>Assimilated Verbs</dt><dd>Verbs whose first root is either \arb{و} or \arb{ي}.</dd>
<dt>Doubled Verbs</dt><dd>Verbs whose second and third roots are the same.</dd>
</dl>

<p>
These are not a big deal. They happen often enough to get names.
</p>
</div>
]]></description>
  <category><![CDATA[arabic]]></category>
  <category><![CDATA[cheat-sheet]]></category>
  <link>https://alhassy.github.io/arabic-cheat-sheet.html</link>
  <guid>https://alhassy.github.io/arabic-cheat-sheet.html</guid>
  <pubDate>Wed, 14 Jun 2023 00:00:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[A Brisk Introduction to Karate]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-karate.html"><img src="https://img.shields.io/badge/-karate-grey?logo=nil"></a></center>
<center><a href="https://www.usadojo.com/wp-content/uploads/2013/08/Goju-Ryu-Karate-600x300.png" class="tooltip" title="Image credit “https://www.usadojo.com/wp-content/uploads/2013/08/Goju-Ryu-Karate-600x300.png”"><img src="https://www.usadojo.com/wp-content/uploads/2013/08/Goju-Ryu-Karate-600x300.png" alt="Article image" style="border: 2px solid black;" width="88%" height="88%" align="top"/></a></center>

<div class="abstract" style="border: 1px solid black;padding: 1%; margin-top: 1%; margin-bottom: 1%;margin-right: 10%; margin-left: 10%; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>
<div class="org-center">
<p>
<img src="https://img.shields.io/badge/Warning-Incomplete_DRAFT-red?logo=codeigniter">
</p>
</div>

<p>
What are the basic forms of Karate? What is Karate?
</p>

<p>
<i>“The ultimate aim of karate lies not in victory or defeat but in the perfection
of the character of its participants … to subdue the enemy without fighting is</i>
<i>the highest skill, know your enemy and know yourself, in a hundred battles you
will not be defeated”</i> says Gichin Funakoshi &#x2014;known as The Father of Modern Karate.
</p>

<p>
<i>Karate</i> means “empty hand” and was developed on the island of Okinawa &#x2014;part of
modern-day Japan.  The major styles (“Ryu”) are Shotokan, Wado-ryu,
Shito-ryu, and Goju-ryu &#x2014;many other styles of Karate are derived from these
four. I'm focusing on Goju-Ryu in this article: Goju-Ryu was founded by Chojun
Miyagi; whose colleague, Gichin Funakosi, founded Shotokan-Ryu.
</p>

<div class="org-center">
<p>
<a href="http://www.traditionalshotokankarate.co.uk/kara-te-do.gif" class="tooltip" title="Image credit “http://www.traditionalshotokankarate.co.uk/kara-te-do.gif”"><img src="http://www.traditionalshotokankarate.co.uk/kara-te-do.gif" alt="Article image"
             width="nil" height="nil" align="top"/></a>
</p>
</div>

<p>
Occasionally one sees <i>Karate-Do</i>, which means “the way of the empty hand”.
This usage is a reminder that Karate is not just about fighting, but is also
a spiritual discipline.
</p>

<p>
The basic form of Goju-Ryu karate is Sanchin, “3 battles”: The battles of the
mind, the body, and the spirit.  However, this was considered a bit difficult
for beginners, and so new forms were needed as a way of introducing fundamental
karate forms to a wider audience.  There are the “peaceful and safe” forms known
as Pinan/Heian, the “first course” or Taikyoku forms, the “popularising forms”
known as Fukyugata &#x2014;the second of which was rebranded as “attack &amp; smash”,
Gekaisai&#x2014; and, finally, there is the so-called Dachi-waza form. This last one
is relatively new, and aims to be a smooth introduction to the world of
forms/Kata.
</p>

<p>
In this article, I'd like to discuss the basic forms and their relationships.
</p>
<div class="org-center">
<p>
<a href="https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/saifa.gif" class="tooltip" title="Image credit “https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/saifa.gif”"><img src="https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/saifa.gif" alt="Article image"
             width="nil" height="nil" align="top"/></a>
</p>
</div>


</div>

<div id="outline-container-org3bf4a1a" class="outline-2">
<h2 id="org3bf4a1a"><span class="section-number-2">1.</span> <a href="#org3bf4a1a">Kata: Learning by Patterns</a></h2>
<div class="outline-text-2" id="text-1">
<p>
<i>Kata</i> means “forms”, which are patterns used to learn sequences of techniques.
Besides teaching kicking, strikes, etc; the repetition of patterns/Kata improves
physical conditioning and muscle memory.
</p>
<ul class="org-ul">
<li>Interestingly, the phrase “kata” is used in Software Engineering for the same
goals: To refer to “families of similar (coding) problems” which may be solved
using an existing set of “design patterns”.</li>
</ul>

<p>
In general, repeating a fixed pattern over and over &#x2014;i.e., “doing kata”&#x2014;
reinforces the knowledge of fundamental techniques and stances via repetition.
However, there are numerous other benefits:
</p>

<dl class="org-dl">
<dt>Solo Practice</dt><dd>Training without a partner is important, since you can then
train whenever and wherever you want. E.g., at your lunch break at work.</dd>

<dt>Mucle Memory</dt><dd>Repeatedly practicing kicks, strikes, blocks, etc,
means it will become easier to perform them, and doing them in fixed sequences
means your body will internalise different combinations. E.g., middle-block,
followed by forward-punch, and concluding with a snap-kick.</dd>

<dt>Improved Fitness</dt><dd><p>
Doing a kata, while focusing on your breathing &amp; stances,
is itself a quick workout. Adding weights, or doing the kata on your toes
only, or doing them slowly, or with extra power, not only provide a challenge
but also make the kata into a sort-of <i>moving meditation</i>:
Gichin Funakoshi says,
<i>In traditional karate-do, we always keep in mind that the true opponent is oneself.</i>
</p>
<ul class="org-ul">
<li>Of-course “real opponents move fast”, so one can also try practising the
kata at increased speeds, but ensuring the techniques are performed
correctly.</li>
<li>“Imaginary opponents don't hit back”, so the addition of weights, or the use
of extra power, can be used to compensate.</li>
</ul>

<p>
<i>When you train, do so as if on the battlefield. Your eyes should glare,</i>
<i>shoulders drop, and body harden. You should always train with intensity and
spirit and in this way you will naturally be ready.</i> &#x2014;Anko Itosu
</p></dd>

<dt>Safety</dt><dd><p>
Dangerous techniques can be practised without anyone getting hurt.
E.g., leg breaks, or the use of weapons, can be practised against an imaginary
opponent before actually practising with a real person.
</p>

<p>
<i>Do not think that you have to win, think rather that you do not have to lose.</i> &#x2014;Gichin Funakoshi
</p></dd>

<dt>Application / Bunkai</dt><dd>When fixed patterns are already learned, one can adapt
them to various real-life situations involving another person. I personally
liked it when the teacher would explain immediate applications of a pattern
<b>while</b> I was learning the pattern: It made it easy for me to imagine an
opponent attacking me as I practised the kata. Unfortunately, some teachers
insist on learning the patterns first, then learning the applications.
<ul class="org-ul">
<li>I think having some immediate application removes boredom, for beginners
especially; otherwise, the kata may feel too rigid and vague.</li>
</ul></dd>

<dt>Accessibility</dt><dd><p>
Anyone can join in! E.g., I've done kata with my kids: It's a
fun little dance we do together; the goal is to have fun as a family.
</p>

<p>
<i>The ultimate aim of Karate lies not in victory or defeat, but in the
perfection of the character of its participants.</i> &#x2014;Gichin Funakoshi
</p></dd>
</dl>

<p>
<span class="underline">The <i>benefits</i> of kata depends on your end-goal:</span> Are you looking for a workout?
Practising fundamental kicks &amp; stances? Fighting imaginary opponents? Or is it
just a way to relax from a hectic day. It's important to keep in mind that
Karate is not just about fighting!
</p>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>The different uses of Karate</h3>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Approach</th>
<th scope="col" class="org-left">Goal</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Self-defence</td>
<td class="org-left">Practising with other people; practising real-life dangerous situations</td>
</tr>

<tr>
<td class="org-left">Art-form</td>
<td class="org-left">Like painting, the goal is to relax, get creative, and get disciplined</td>
</tr>

<tr>
<td class="org-left">Sport</td>
<td class="org-left">Like soccer, the goal is to master the patterns and win competitions</td>
</tr>
</tbody>
</table>

</div>

<p>
I like Karate because it's a nice way to stay physically healthy and meet new people.
</p>

<p>
See also:
</p>
<ul class="org-ul">
<li><a href="https://www.youtube.com/watch?v=QL02OKsQgVY&amp;feature=youtu.be&amp;ab_channel=ShotokanSensei">Karate Kata: Why do we study kata? - YouTube</a></li>
</ul>
</div>
</div>

<div id="outline-container-org8f3eb3b" class="outline-2">
<h2 id="org8f3eb3b"><span class="section-number-2">2.</span> <a href="#org8f3eb3b">Succession of Miyagi</a>&#xa0;&#xa0;&#xa0;<span class="tag"><span class="The">The</span>&#xa0;<span class="Five">Five</span>&#xa0;<span class="Taikyokus">Taikyokus</span></span></h2>
<div class="outline-text-2" id="text-2">
<p>
Following the death of Chojun Miyagi Sensei in 1953, four main schools teaching Goju Ryu emerged.
</p>

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


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

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">School</th>
<th scope="col" class="org-left">Founder</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Meibukan, <i>The House of the pure-minded warrior</i></td>
<td class="org-left">Meitoku Yagi</td>
</tr>

<tr>
<td class="org-left">Jundokan, <i>House in which we follow in the master’s footsteps</i></td>
<td class="org-left">Eiichi   Miyazato</td>
</tr>

<tr>
<td class="org-left">Shoreikan, <i>House of politeness and respect</i></td>
<td class="org-left">Seikichi Toguchi</td>
</tr>

<tr>
<td class="org-left">Goju Kai, <i>The Japanese Goju Association</i></td>
<td class="org-left">Gogen Yamaguchi</td>
</tr>
</tbody>
</table>

<p>
The graphic of this article, the closed fist, is the symbol of Goju Kai which
was popularised in Ontario, Canada, by <a href="https://www.warrenerentertainment.com/fighting-spirit-blog/don-warrener/">Don Warrener</a>.  Initially, Miyagi tasked
<a href="https://blackbeltmag.com/black-belt-flashback-gogen-the-cat-yamaguchi-head-of-goju-karate">Gogen “The Cat” Yamaguchi</a> with spreading Goju Ryu in mainland Japan, outside of
Okinawa: Yamaguchi designed the closed fist symbol, based on the right hand of
Miyagi; he unified all karate schools in Japan; he propagated Goju all over the
world. Moreover, in this Goju Kai style, Yamaguchi added to the Goju system the
“Taikyoku Katas”, <i>First-course formations</i>, which consists of 5 formations all
making an “I”-shape in movement: The student starts at the bottom-middle of the
“I”, moves left, then right, then up, and eventually back down to the starting
position. Each formation reinforces basic principles, such as a certain block
and stance. These are intended as training methods for the beginner students to
prepare them for the more advanced kata.
</p>

<div class="org-center">
<ol class="org-ol">
<li><a href="https://youtu.be/5dCu6C6pTF0"><img src="https://img.shields.io/badge/Taikyoku_Gedan-First--course_Low_Block-green?logo=youtube"></a></li>
<li><a href="https://youtu.be/C08R9PEQRqY"><img src="https://img.shields.io/badge/Taikyoku_Chudan-First--course_Middle_Block-green?logo=youtube"></a></li>
<li><a href="https://youtu.be/g_ggx2Ywwrc"><img src="https://img.shields.io/badge/Taikyoku_Jodan-First--course_High_Block-green?logo=youtube"></a></li>
<li><a href="https://youtu.be/FSeGrXQs3sI"><img src="https://img.shields.io/badge/Taikyoku_Kake_Uke-First--course_Hooking_Block-blue?logo=youtube"></a></li>
<li><a href="https://youtu.be/gDaWBIfs-G8"><img src="https://img.shields.io/badge/Taikyoku_Mawashi_Uke-First--course_Roundhose_Block-blue?logo=youtube"></a></li>
</ol>
</div>

<p>
Other schools skip these “school-children” fundamentals and start-off with
Gekisai Ich, <i>Attack and Destroy</i>, which is known as Goju Karate's first Kata.
However, the Jundokan school works up to this Kata a different way&#x2026;
</p>
</div>
</div>

<div id="outline-container-org5f19a0b" class="outline-2">
<h2 id="org5f19a0b"><span class="section-number-2">3.</span> <a href="#org5f19a0b">From Standing to Destroying</a>&#xa0;&#xa0;&#xa0;<span class="tag"><span class="TeshiWaza">TeshiWaza</span>&#xa0;<span class="FukyuKata">FukyuKata</span>&#xa0;<span class="Gekisai">Gekisai</span></span></h2>
<div class="outline-text-2" id="text-3">
<p>
Jundokan starts with these
</p>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">TeshiWaza, <i>Stances, or Formation-11</i></td>
</tr>

<tr>
<td class="org-left">Fukyu Kata 1, <i>to Spread or to Make Popular formation</i></td>
</tr>

<tr>
<td class="org-left">Fukyu Kata 2, <i>to Spread or to Make Popular formation</i></td>
</tr>
</tbody>
</table>

<p>
The Fukyu Kata were made to popularise Karate and make it accessible.
Miyagi renamed Fukyu Kata to <i>Gekisai Dai Ichi</i>, “Attach and Destroy 1”
&#x2014;of-course the second kata of Goju Karate is known as “Attack and Destroy 2”
or <i>Gekisai Dai Ni</i>.
</p>
<ul class="org-ul">
<li><i>Geki</i> ≈ <i>to attack</i></li>
<li><i>Sai</i> ≈ <i>to smash, break, crush</i></li>
<li><i>Gekisai</i> ≈ <i>to pulverise, to attack and destroy</i></li>
</ul>

<p>
The aggressive renaming of the ‘popularising’ kata, in the 1940s, may have been due
to Japan's war-time efforts.
</p>



<div id="org7e8362b" class="figure">
<p><img src="../images/tachi-fukyu-gekai-sai.png" alt="tachi-fukyu-gekai-sai.png" />
</p>
</div>
</div>
</div>

<div id="outline-container-Tachi-Waza-Kata" class="outline-2">
<h2 id="Tachi-Waza-Kata"><span class="section-number-2">4.</span> <a href="#Tachi-Waza-Kata">Jundokan: Standing in Style</a>&#xa0;&#xa0;&#xa0;<span class="tag"><span class="Tachi_Waza_Kata">Tachi_Waza_Kata</span></span></h2>
<div class="outline-text-2" id="text-Tachi-Waza-Kata">
<p>
<a href="http://www.ogkk.eu/historymaster/masters/eiichi_miyazato.htm">Eiichi Miyazato</a> was a Judo champion and a student of Miyagi; he wanted to pass
on Miyagi's legacy and so opened his own training-hall: The Jundokan &#x2014;which
literally means <i>House of Father's Way</i> or <i>House for Following in the Father's
Footsteps</i>. Among his famous students is Teruo Chinen, who introduced
“Tachi-Waza Kata” into the Jundokan Goju Kata syllabus.
</p>

<p>
<i>Note:</i> Tachi and Dachi are the same word, in Japanese, meaning <i>stance</i>. The sound
changes depending on if the letter is the start of a word.
</p>

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

<p>
Above is “Formation 11”, a slight variation of “Dachi-Waza Kata” which is
performed as follows.
</p>

<p>
<center> <a href="../images/tachi-waza.png" class="tooltip" title="Image credit “Musa Al-hassy”"><img src="../images/tachi-waza.png" alt="Article image"
             width="600" height="600" align="top"/></a> </center>
</p>

<p>
Starting with heels touching, toes pointing out, and hands to the side.
</p>

<ol class="org-ol">
<li><a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-musubi-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-musubi-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-musubi-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
<a href="https://upload.wikimedia.org/wikipedia/commons/5/5c/Musubidachi.svg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/commons/5/5c/Musubidachi.svg”"><img src="https://upload.wikimedia.org/wikipedia/commons/5/5c/Musubidachi.svg" alt="Article image"
             width="50" height="50" align="top"/></a>
Musubi-dachi, <i>Joining/United stance; Formal Attention stance</i>
<ul class="org-ul">
<li>Heels together, toes open at about 45 degrees;</li>
<li>Hands move up to waist: <b>Hands remain on the sides of the waist through-out!</b></li>
<li>In this stance, the body should be straight, knees are slightly bent, heels are touching and feet are pointing out making a 45° angle.</li>
</ul></li>

<li><a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-heiko-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-heiko-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-heiko-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
<a href="https://upload.wikimedia.org/wikipedia/commons/3/32/Heikoudachi.svg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/commons/3/32/Heikoudachi.svg”"><img src="https://upload.wikimedia.org/wikipedia/commons/3/32/Heikoudachi.svg" alt="Article image"
             width="50" height="50" align="top"/></a>
Heiko-dachi, <i>Parallel stance; Attention Stance</i>
<ul class="org-ul">
<li>The feet open to shoulder width apart, and their outer edges are parallel.</li>
<li>In this stance, the feet are shoulder-width apart, the big toes and the
second toes should face forward, the inner edges of the feet are parallel,
and the center of gravity is at the mid-point between the two feet.</li>
</ul></li>

<li><a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-sagiashi-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-sagiashi-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-sagiashi-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
Sagi-ashi-dachi, <i>Heron-foot stance</i>
<ul class="org-ul">
<li>left leg steps to the left, right leg follows then upward with the knee</li>
<li>In this stance, one leg is raised and bent while the other leg is slightly
bent and supports the whole body weight The toe of the raised leg points
downward.</li>
<li>This is also known as Tsuru-ashi-dachi, <i>Crane-foot stance</i>.</li>
</ul></li>

<li>Sagi-ashi-dachi - to the right</li>

<li><p>
<a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-zenkutsu-dachi.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-zenkutsu-dachi.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-zenkutsu-dachi.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
<a href="https://upload.wikimedia.org/wikipedia/commons/9/96/Zenkutsudachi.svg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/commons/9/96/Zenkutsudachi.svg”"><img src="https://upload.wikimedia.org/wikipedia/commons/9/96/Zenkutsudachi.svg" alt="Article image"
             width="50" height="50" align="top"/></a>
Zenkutsu-dachi, <i>forward stance</i> - to the left
</p>

<ul class="org-ul">
<li>This is a long frontal stance where the weight is mostly on the front leg.</li>
<li>It has exactly the same height as shiko-dachi (below), but the rear leg is
completely straight at the knee and extended back.</li>
<li>The front foot is placed frontal (toes facing forward), the rear foot is
turned out 30 degrees, but never 90 degrees as seems natural to new practitioners
because this precludes any forward motion.</li>
<li>The heel of the rear foot rests on the ground.</li>
</ul>

<p>
Zenkutsu is performed as follows:
</p>
<ol class="org-ol">
<li>From the natural stance, step forward so that the distance between the back foot and the front foot is roughly about one and a half to two shoulder width</li>
<li>The feet are one shoulder width apart</li>
<li>The front foot points forward and the back foot points diagonally at about 30 degree angle</li>
<li>The front knee is bent, turned slightly inward, and should be forward enough that you are not able to see the toes</li>
<li>The back leg is naturally straight but not locked</li>
<li>Most of the body weight is placed on the front leg</li>
<li>The heel of the back leg should be placed firmly on the ground.</li>
</ol></li>

<li><a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-kokutsu-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-kokutsu-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-kokutsu-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
<a href="https://upload.wikimedia.org/wikipedia/commons/2/2a/Koukutsudachi.svg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/commons/2/2a/Koukutsudachi.svg”"><img src="https://upload.wikimedia.org/wikipedia/commons/2/2a/Koukutsudachi.svg" alt="Article image"
             width="50" height="50" align="top"/></a>
Kokutsu-dachi, <i>Back Long stance</i> - to the right, but head still facing to the left

<ul class="org-ul">
<li>This is a back stance derived from the zenkutsu dachi stance.</li>
<li>Start with zenkutsu dachi, move your back leg across so that the front leg
and the back leg are on the same line.</li>
</ul></li>

<li>Zenkustu Dachi - back to the left</li>

<li><p>
<a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-sanchin-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-sanchin-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-sanchin-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
<a href="https://upload.wikimedia.org/wikipedia/commons/7/78/Sanchindachi.svg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/commons/7/78/Sanchindachi.svg”"><img src="https://upload.wikimedia.org/wikipedia/commons/7/78/Sanchindachi.svg" alt="Article image"
             width="50" height="50" align="top"/></a>
Sanchin-dachi, <i>Three Battle stance</i> - take a step into sanchin, facing leftwards
</p>

<p>
This is the most difficult stance to master and probably the most important
stance in Goju Ryu.  It is performed as follows:
</p>
<ol class="org-ol">
<li>Begin with heiko dachi, step one foot forward</li>
<li>The heel of the front foot should be on the same line as the toes of the back foot</li>
<li>The toes of both feet should turn inward slightly</li>
<li>The front foot is turned inward at about 20° angle</li>
<li>Tense your tandien, buttocks and thigh muscles and then pull the hips upwards</li>
<li>The knees should bend and turn inward</li>
<li>The feet should be placed firmly on the ground with the toes gripping the ground</li>
<li>The center of gravity should be at the midpoint between the two feet</li>
<li>Keep your back straight and your chin tucked in.</li>
</ol>

<p>
Sanchin kata, considered the core and most difficult kata in Goju Ryu is done entirely in the sanchin dachi stance.
</p></li>

<li>Zenkusti Dachi - Look right, then with right leg move into Zenkutsu, then
end-up facing rightwards with right leg at the front. Through-out the left
remains in-place, just pivoting.</li>

<li>Kokustu Dachi - to the left, but head still facing to the right</li>

<li>Zenkustu dachi - back to the right</li>

<li>Sanchin Dachi - take a step into sanchin, facing rightwards</li>

<li><p>
<a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-heisoku-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-heisoku-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-heisoku-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
<a href="https://upload.wikimedia.org/wikipedia/commons/3/33/Heisokudachi.svg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/commons/3/33/Heisokudachi.svg”"><img src="https://upload.wikimedia.org/wikipedia/commons/3/33/Heisokudachi.svg" alt="Article image"
             width="100" height="100" align="top"/></a>
Hesoku-dachi, <i>feet together stance; informal attention stance</i> - right moves up to touch the left, then head faces to the
front center
</p>

<p>
In this stance, your back is straight and relaxed, your feet are placed
together, and the weight is equally distributed between the two feet.
</p></li>

<li>Zenkustu dachi - to the front center, with left leg leading</li>

<li><a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-hachiji-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-hachiji-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-hachiji-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
Hachiji-dachi, <i>Natural stance</i> - right leg takes a step forward, left follows, to end-up in a
should-width stance

<ul class="org-ul">
<li>This stance is close to the natural way people stand.</li>
<li>The feet are shoulder width apart, the toes point out at about 45°, the
body is relaxed and the knees are slightly bent.</li>
</ul></li>

<li>Zenkustu Dachi - look over the left-shoulder, turn with left leg; end-up in
left leading zenkustu facing the back right corner</li>

<li><p>
<a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-neko-ashi-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-neko-ashi-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-neko-ashi-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
<a href="https://upload.wikimedia.org/wikipedia/commons/a/a5/Nekoashidachi.svg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/commons/a/a5/Nekoashidachi.svg”"><img src="https://upload.wikimedia.org/wikipedia/commons/a/a5/Nekoashidachi.svg" alt="Article image"
             width="50" height="50" align="top"/></a>
Neko Ashi Dachi, <i>Cat stance</i> - Bring the left back, with toes on ground, heel up.
</p>

<p>
To assume neko ashi dachi:
</p>
<ol class="org-ol">
<li>Start with musubi dachi (formal attention stance) and step forward for a distance of about one foot</li>
<li>Lower the hips deeply and transfer most of the body weight to the back leg</li>
<li>The front leg is bent and the heel of the front leg is raised slightly with only the toes and the ball of the front foot touches the ground</li>
<li>The back foot points outward at about 30 to 45 degree angle</li>
<li>About 90% of the body weight is placed on the back foot.</li>
</ol>

<p>
Note:
</p>
<ul class="org-ul">
<li>All weight rests on the back leg, which is bent at the knee.</li>
<li>The rear foot is turned at about 20-30 degrees out and the knee sits at
the same angle.</li>
<li>Only the toes of the front foot rest on the ground, positioned in front of
the back heel.</li>
<li>There is no weight on the front foot, and there is no bent in the ankle
joint - front knee, front shin, and the rise of the foot (but not the
toes) form a single line.</li>
</ul></li>

<li><p>
<a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-bensoku-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-bensoku-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-bensoku-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
Bensoku-dachi, <i>Cross-legged stance</i> - Drop the left down, toes pointing to the right. Right steps
towards the back right corner of the room. Finally, left leg slides behind
the right leg, ending with the heel up and the toes planted and facing the
right leg.
</p>

<p>
To assume bensoku dachi:
</p>
<ol class="org-ol">
<li>Cross one leg behind the other</li>
<li>Bend both knees</li>
<li>The front foot is placed firmly on the ground but only the ball of the back foot touches the ground</li>
<li>The knee of the back foot is nested against the back of the front knee.</li>
</ol>

<p>
Bensoku dachi is a transitional stance that is used when one needs to change
direction. It appears in kata like Seiyunchin and Sepai.
</p></li>

<li>Zenkustu with the right leg towards the back left of the room; the left leg
only pivots.</li>

<li>Neko Ashi Dachi - Bring the right leg back into a cat stance</li>

<li>Drop the right down and do a Bensoku Dachi</li>

<li><p>
<a href="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-shiko-dachi-Copy.jpg" class="tooltip" title="Image credit “https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-shiko-dachi-Copy.jpg”"><img src="https://www.karatephilosophy.com/wp-content/uploads/2022/08/Goju-ryu-stance-shiko-dachi-Copy.jpg" alt="Article image"
             width="50" height="50" align="top"/></a>
<a href="https://upload.wikimedia.org/wikipedia/commons/b/b0/Shikodachi.svg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/commons/b/b0/Shikodachi.svg”"><img src="https://upload.wikimedia.org/wikipedia/commons/b/b0/Shikodachi.svg" alt="Article image"
             width="50" height="50" align="top"/></a>
Shiko-dachi, <i>Square Stance, Horse Stance, Straddle Leg Stance</i>
</p>

<p>
The left leg moves towards the back of the room, ending in a shiko dachi;
right leg remains where it is.
</p>

<p>
The toes face out at about 45 degrees. Knees point outward, and stance is often low.
</p>

<p>
To assume shiko dachi, start with hachiji dachi stance, turn the heels to
point the toes outward at about 45 degrees and lower the hips.
</p>

<p>
In this stance:
</p>
<ul class="org-ul">
<li>The feet are about two shoulder width apart</li>
<li>The big toes point outward diagonally at about 45 degrees</li>
<li>The knees are turned outward</li>
<li>The back is straight</li>
<li>The hips are lower than in kiba dachi and the thighs are almost parallel to the ground</li>
<li>The body weight is evenly distributed between the two legs</li>
<li>The soles of the feet are firmly in contact with the ground.</li>
</ul>

<p>
Shiko dachi is a great stance for developing lower body strength and stability.
</p></li>

<li>Look rightwards towards the center of the room, then do a shiko dachi
&#x2014;ending with body facing the right side of the room; i.e., right leg is in
the back.</li>

<li>Bring the back leg, the right leg, up to the front leg into a Musubu Dachi.</li>
</ol>
</div>
</div>

<div id="outline-container-org8fd8793" class="outline-2">
<h2 id="org8fd8793"><span class="section-number-2">5.</span> <a href="#org8fd8793">Fukyu Kata Ichi</a></h2>
<div class="outline-text-2" id="text-5">
<iframe width="560" height="315" src="https://www.youtube.com/embed/xmCz7F06DLs" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

<div class="org-center">
<p>
<a href="https://youtu.be/UpfuGcwRYzI"><img src="https://img.shields.io/badge/Fukyu_Kata_Ichi-Step--by--Step_Instructions-green?logo=youtube"></a>
</p>

<p>
<a href="https://youtu.be/M-VC1BGtRYM"><img src="https://img.shields.io/badge/Fukyu_Kata_Ichi-With_Japanese_Names-green?logo=youtube"></a>
</p>

<p>
<a href="https://youtu.be/-eBWPK45q1w"><img src="https://img.shields.io/badge/Fukyu_Kata_Ichi-Kids_Instructional-green?logo=youtube"></a>
</p>

<ul class="org-ul">
<li><a href="https://www.youtube.com/watch?v=-vekmSkNOGw">Done slowly</a></li>
<li><a href="https://www.youtube.com/watch?v=GmQGY10xID4&amp;ab_channel=GojuRyuKarateCentre">Learn Fukyu Kata for Goju Ryu</a></li>
<li><a href="https://www.youtube.com/watch?v=FFEgCWfelE0">Walkthrough</a></li>
<li><a href="https://youtu.be/g32NRLwZEsA">Kids Class - Fukyu Kata Dai Ichi walkthrough - Joey Jackson</a></li>
</ul>
</div>

<ol class="org-ol">
<li value="0"><i>Kyotsukei</i>, Attention: Palms at sides, arms straight</li>
<li value="0"><i>Rei</i>, Bow: Eyes slightly down, hands still at sides</li>
<li value="0"><i>Kamae</i>, Ready stance: heels together Palms down in front of body, L hand over R hand</li>

<li>Turn left into a left forward stance; down-block with left-hand</li>

<li>Step forward with right foot into Right forward stance;
right middle (solar plexus) punch</li>

<li>Turn around into a right forward stance, while doing a right down-block

<ol class="org-ol">
<li>Move right foot behind body and to the left</li>
<li>Extend left arm and chamber with right fist at left elbow</li>
<li>Pivot 180°-rigtwards, on the left-foot, into right forward stance</li>
<li>Right down block</li>
</ol></li>

<li>Step forward with left foot into left forward stance;
left middle (solar plexus) punch</li>

<li>Turn 90°-leftward, pivoting on the right foot, to face the front;
enter into a left Zenkustu; down-block with left</li>

<li>Step forward with R foot into R forward stance (R foot straight, L foot diagonal)
<ul class="org-ul">
<li>R middle (solar plexus) punch</li>
</ul></li>

<li>Step forward with L foot into L forward stance (L foot straight, R foot 45 diagonal)
<ul class="org-ul">
<li>L middle (solar plexus) punch</li>
</ul></li>

<li>Step forward with R foot into R forward stance (R foot straight, L foot 45 diagonal)
<ul class="org-ul">
<li>R middle (solar plexus) punch</li>
</ul></li>

<li>Move L foot behind body and to the R (move beyond where you extended in step 3 - this time you will end up 225 degrees from start)
<ul class="org-ul">
<li>Keeping R arm extended, chamber with L fist on R elbow</li>
</ul></li>

<li>Pivot (on R foot) 225 L into L forward stance -
you are now facing 45 L from back of dojo
<ul class="org-ul">
<li>Down block L</li>
</ul></li>

<li>Step forward with R foot into R forward stance (R foot straight, L foot 45 diagonal)
<ul class="org-ul">
<li>High block R</li>
</ul></li>

<li>Pivot 90 R (on L foot) into R forward stance -
you are now facing 45 R from back of dojo
<ol class="org-ol">
<li>Down block R</li>
</ol></li>

<li>Step forward with L foot into L forward stance (L foot straight, R foot 45 diagonal)
<ul class="org-ul">
<li>High block L</li>
</ul></li>

<li>Pivot 45 L (on R foot) into L forward stance - you are now facing back of dojo
<ul class="org-ul">
<li>R reverse middle punch</li>
</ul></li>

<li>Step forward with R foot into R forward stance (R foot straight, L foot 45 diagonal)
<ul class="org-ul">
<li>L reverse middle punch</li>
</ul></li>

<li>Step forward with L foot into L forward stance
(L foot straight, R foot 45 diagonal)
<ul class="org-ul">
<li>R reverse middle punch</li>
</ul></li>

<li>Step forward with R foot into R forward stance (R foot straight, L foot 45 diagonal)
<ul class="org-ul">
<li>L reverse middle punch</li>
</ul></li>

<li>Move L foot behind body and to the R (move beyond where you extended in step 3 - you will end up turning 225 degrees to the left)
<ul class="org-ul">
<li>Extend R arm and chamber with L fist at R elbow</li>
</ul></li>

<li>Pivot 225 L (both feet) into L forward stance, facing 45 L of dojo front
<ul class="org-ul">
<li>Down block L</li>
</ul></li>

<li>Step forward with R foot into R forward stance (R foot straight, L foot 45 diagonal)
<ul class="org-ul">
<li>High punch R</li>
</ul></li>

<li>Pivot 90 R (on L foot) into R forward stance Extend L arm and chamber with R fist at L elbow</li>

<li>Facing 45 R of dojo front Down block R</li>

<li>Step forward with L foot into L forward stance (L foot straight, R foot 45 diagonal)
<ul class="org-ul">
<li>High punch L</li>
</ul></li>

<li>L foot pulls back to starting position, heels together
<ul class="org-ul">
<li>Palms down in front of body, L hand over R hand</li>
</ul></li>
</ol>

<p>
Note: This is also known as “Kihon Kata Ichi”, <i>Basics Form One</i>.
</p>
<ul class="org-ul">
<li><a href="https://www.youtube.com/watch?v=tgdIVEmnwdc&amp;ab_channel=EastlakeDojo">https://www.youtube.com/watch?v=tgdIVEmnwdc&amp;ab_channel=EastlakeDojo</a></li>
</ul>
</div>
</div>
<div id="outline-container-org2d42c48" class="outline-2">
<h2 id="org2d42c48"><span class="section-number-2">6.</span> <a href="#org2d42c48">Gekisai Dai Ichi &#x2014;“Attack &amp; Destroy One”</a></h2>
<div class="outline-text-2" id="text-6">
<div class="org-center">
<p>
<a href="https://youtu.be/xtOl_XULS48"><img src="https://img.shields.io/badge/Gekisai_Dai_Ichi-Attack_%26_Destroy_One-green?logo=youtube"></a>
(Side View)
</p>

<p>
<a href="https://youtu.be/YtcRO7zceIg"><img src="https://img.shields.io/badge/Gekisai_Dai_Ichi-Attack_%26_Destroy_One-green?logo=youtube"></a>
(Front View)
</p>

<ul class="org-ul">
<li><a href="https://youtu.be/i7LyUfjiT3g">Powerful! By Sandra Sanchez</a></li>
<li><a href="https://www.youtube.com/shorts/yqEDJ8lKrlg">#shorts</a></li>
</ul>
</div>

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

<ol class="org-ol">
<li>Attention stance</li>

<li>Left foot steps out into Yoi (ready stance) shoulder width apart

<ol class="org-ol">
<li>Alternatively: Yoi with feet together at attention left hand over right several inches away from the groin</li>
<li>the left hand pushes down while the right hand pushes up creating
(explosive) tension</li>
</ol></li>

<li>From either Yoi right foot steps forward and you turn left 90 degrees into hourglass stance
<ol class="org-ol">
<li>High block with left hand</li>
<li>If feet are apart in Yoi when the right foot steps forward you pivot on the center (ball and heel) of the foot so that the stance is even</li>
<li>If feet are together in Yoi left foot pivots on the heel and the right on the ball so the stance is even</li>
<li>if hands and feet are together in Yoi the right arm explodes from underneath almost like a fanning block before turning</li>
</ol></li>

<li>Step forward into hourglass stance with the right foot
<ul class="org-ul">
<li>High punch with the right hand</li>
</ul></li>

<li>Step back into square/horse stance so that the body is facing the direction of the attention stance
<ul class="org-ul">
<li>Low block with the left</li>
</ul></li>

<li>Turn the body 90 degrees and left foot slides behind the right into hourglass stance
<ul class="org-ul">
<li>High block with the right hand</li>
</ul></li>

<li>Step forward into hourglass stance with the left leg
<ul class="org-ul">
<li>High punch with the left hand</li>
</ul></li>

<li>Step back into square/horse stance so that the body is facing the direction of the attention stance
<ul class="org-ul">
<li>Low block with the right</li>
</ul></li>

<li>Left leg steps in and then forward into hourglass stance
<ul class="org-ul">
<li>Middle block with the left</li>
</ul></li>

<li>Step forward into hourglass stance with the right
<ul class="org-ul">
<li>Middle block with the right</li>
</ul></li>

<li>Left front kick and land in a front stance
<ol class="org-ol">
<li>Left elbow strike as blocking right hand pulls back into chamber</li>
<li>Left downward back fist face level</li>
<li>Left hand transitions to low block</li>
<li>And right hand punches with a “Kiai”</li>
</ol></li>

<li>Turn right 90 degrees into a ready stance (with head facing to right)
<ol class="org-ol">
<li>Right knife hand strike to the side of the temple</li>
<li>Alternatively: during this transition the back leg of the previous front stance lifts up as if avoiding a sweep</li>
</ol></li>

<li>Turn right 90 degrees to right, step forward with the left into hourglass stance
<ul class="org-ul">
<li>Left hand middle block</li>
</ul></li>

<li>Right front kick and land in a front stance
<ol class="org-ol">
<li>Right elbow strike as blocking left hand pulls back into chamber</li>
<li>Right downward back fist face level</li>
<li>Right hand transitions to low block</li>
<li>Left hand punches with a “Kiai”</li>
</ol></li>

<li>Turn left 90 degrees into a ready stance (with head facing to left)
<ol class="org-ol">
<li>Left knife hand strike to the side of the temple</li>
<li>Alternatively: during this transition the back leg of the previous front stance lifts up as if avoiding a sweep</li>
</ol></li>

<li>Turn left 90 degrees and left leg steps back into front stance
<ol class="org-ol">
<li>As stepping back left hand pulls back as if pulling the arm of an opponent</li>
<li>Left hand in chamber palm down, right in chamber palm up</li>
<li>Double punch with left hand to the lungs and right to the stomach</li>
<li>Alternatively: when stepping back the left hand turns in like an open handed fanning block</li>
<li>Right hand in chamber comes out and performs a middle block</li>
<li>Then both hands pull back to chamber with left palm down and right palm up</li>
<li>Then double punch</li>
</ol></li>

<li>Step forward into ready stance
<ol class="org-ol">
<li>Reverse fist orientation</li>
<li>Alternatively: Step forward feet together, both knees bent</li>
<li>Right arm turns in as if for a fanning block</li>
<li>Left arm arcs around in front for a middle block</li>
<li>Then both hands are pulled back into chamber in the revers orientation they were before in the prior step</li>
</ol></li>

<li>Step back with the right leg into front stance
<ul class="org-ul">
<li>Double punch, right hand to the heart and left to the liver</li>
</ul></li>

<li>Step forward into attention stance
<ol class="org-ol">
<li>Alternatively: open left hand</li>
<li>Turn right fist to palm facing up and place it in the open hand</li>
<li>Open the right hand</li>
<li>Step forward to attention as the hands turn staying left over right and
return to the beginning position</li>
</ol></li>

<li>Bow</li>
</ol>

<p>
Geki Sai Ni is very similar&#x2026;
</p>
<div class="org-center">
<p>
<a href="https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/gekisai_ni.gif" class="tooltip" title="Image credit “https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/gekisai_ni.gif”"><img src="https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/gekisai_ni.gif" alt="Article image"
             width="nil" height="nil" align="top"/></a>
</p>
</div>
</div>
</div>

<div id="outline-container-org44aaef3" class="outline-2">
<h2 id="org44aaef3"><span class="section-number-2">7.</span> <a href="#org44aaef3">Closing: Possibly Interesting Reads</a></h2>
<div class="outline-text-2" id="text-7">
<div class="org-center">
<p>
<a href="https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/seipai.gif" class="tooltip" title="Image credit “https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/seipai.gif”"><img src="https://www.sullivanskarateschool.com/wp-content/uploads/2019/07/seipai.gif" alt="Article image"
             width="nil" height="nil" align="top"/></a>
</p>
</div>

<ul class="org-ul">
<li><a href="https://wayofmartialarts.com/is-goju-ryu-karate-any-good/">Is Goju-Ryu Karate Any Good? The Answer May Surprise You</a></li>
<li><a href="https://www.shuriway.co.uk/fukyugata-ichi.html">Shorin Ryu Fukyugata Ichi</a></li>
<li><a href="https://www.usadojo.com/goju-ryu-karate/">Goju-ryu Karate - USAdojo.com</a></li>
<li><a href="https://www.warrenerentertainment.com/fighting-spirit-blog/don-warrener/">Don Warrener - Warrener Entertainment</a></li>
<li><a href="https://www.jundokan-international.org/publications">Publications — Jundokan International</a></li>
<li><a href="https://jundokannz.org/the-jundokan">Jundokan So-Honbu — Jundokan New Zeland</a></li>
<li><a href="https://www.google.com/search?q=fukyu+kata+ichi&amp;client=ms-android-samsung-ss&amp;sxsrf=AJOqlzV7SR1htX8kO_e5kgiPQ7-dtcH5ng:1674874206234&amp;ei=Xo3UY47qDfq1ptQPrsqWuAY&amp;start=10&amp;sa=N&amp;ved=2ahUKEwiOkezQoOn8AhX6mokEHS6lBWcQ8tMDegQIBhAE&amp;biw=1440&amp;bih=796&amp;dpr=2">fukyu kata ichi - Google Search</a></li>
<li><a href="http://www.jundokan-hb.jp/english/declaration.htm">JUNDOKAN -</a></li>
<li><a href="https://www.google.com/search?q=what+does+JUNDOKAN+mean&amp;oq=what+does+JUNDOKAN+mean&amp;aqs=chrome..69i57j33i160l2j33i21.2954j0j4&amp;sourceid=chrome&amp;ie=UTF-8">what does JUNDOKAN mean - Google Search</a></li>
<li><a href="https://blackbeltwiki.com/goju-ryu-katas">List of Goju-Ryu Katas - Black Belt Wiki</a></li>
<li><a href="https://youtu.be/B0KJ1GLUH6I">Jundokan Volume 4 - YouTube</a> [Goju documentary]</li>
<li><a href="https://www.youtube.com/watch?v=jolyGrH447g&amp;ab_channel=ArtofOneDojo">The History of Goju Ryu Karate | ART OF ONE DOJO - YouTube</a></li>
<li><a href="https://www.youtube.com/watch?v=DS5nSNwQ5Os&amp;ab_channel=practicalkatabunkai">The Pinan Heian Kata are NOT Children’s Kata! - YouTube</a></li>
<li><a href="https://www.triangledojo.com/blog/5-ways-to-improve-your-motivation-for-training/">5 Ways To Improve Your Motivation For Training | Innovative Martial Arts Academy</a></li>
<li><a href="https://youtu.be/PZ7lDrwYdZc">How to become 37.78 times better at anything | Atomic Habits summary (by James Clear) - YouTube</a></li>
<li><a href="https://www.google.com/search?q=what+is+the+aim+of+karate&amp;oq=what+is+the+aim+of+karate&amp;aqs=chrome..69i57j0i22i30l9.3752j0j7&amp;sourceid=chrome&amp;ie=UTF-8">what is the aim of karate - Google Search</a></li>
<li><a href="https://www.karatephilosophy.com/">Karate Philosophy</a></li>
<li><a href="https://www.ftmaagojuryu.com/tachi-waza---standing-techniques-stances.html">Standing Techniques (Stances) - Fierce Tiger Martial Arts Association Budokai - Okinawan Goju Ryu</a></li>
<li><a href="https://youtu.be/sZ86wr2WT0w">Geki Sai Dai Ichi Kata (And Ni!) - Step By Step - YouTube</a></li>
<li><a href="https://youtu.be/GmQGY10xID4">Learn Fukyu Kata for Goju Ryu - YouTube</a></li>
<li><a href="https://www.amazon.co.uk/exec/obidos/ASIN/0978576322/rivermeadkara-21">Perfection of Character: Guiding Principles for the Martial Arts &amp; Everyday Life: Amazon.co.uk: Okazaki, Teruyuki: 9780978576325: Books</a></li>
</ul>
</div>
]]></description>
  <category><![CDATA[karate]]></category>
  <link>https://alhassy.github.io/karate.html</link>
  <guid>https://alhassy.github.io/karate.html</guid>
  <pubDate>Thu, 02 Feb 2023 00:00:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[My Family Tree]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-family.html"><img src="https://img.shields.io/badge/-family-grey?logo=nil"></a></center>
<center><a href="../images/family-tree.png" class="tooltip" title="Image credit “../images/family-tree.png”"><img src="../images/family-tree.png" alt="Article image" style="border: 2px solid black;" width="88%" height="88%" align="top"/></a></center>
<div id="outline-container-The-tree" class="outline-2">
<h2 id="The-tree"><span class="section-number-2">1.</span> <a href="#The-tree">The tree</a></h2>
<div class="outline-text-2" id="text-The-tree">

<div id="orgca8bd22" class="figure">
<p><img src="../images/family-tree.png" alt="family-tree.png" />
</p>
</div>
</div>
]]></description>
  <category><![CDATA[family]]></category>
  <link>https://alhassy.github.io/family-tree.html</link>
  <guid>https://alhassy.github.io/family-tree.html</guid>
  <pubDate>Thu, 02 Feb 2023 00:00:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[A Brisk Introduction to the Fundamentals of Arabic Grammar, نحو]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-arabic.html"><img src="https://img.shields.io/badge/-arabic-grey?logo=nil"></a></center>
<center><a href="../images/arabic-irab.png" class="tooltip" title="Image credit “../images/arabic-irab.png”"><img src="../images/arabic-irab.png" alt="Article image" style="border: 2px solid black;" width="100%" height="100%" align="top"/></a></center>

<div class="abstract" style="border: 1px solid black;padding: 1%; margin-top: 1%; margin-bottom: 1%;margin-right: 10%; margin-left: 10%; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>

<p>
In short: In English sometimes we mess-up between “I/me/my”, likewise in Arabic we might mess up with “ابو / ابا / ابي”:
These are just اب followed by one of ا/ي/و (which are the pronounced case endings!)
</p>

<div class="org-center">
<p>
~ ~ ~ ~ ~ ~ ~
</p>
</div>

<p>
How do Arabs say the English “a/an/the”, as in “an apple” or “the chair”? Easy! By default, all words are <i>indefinite</i>
(“a/an”); and made <i>definite</i> (“the”) by adding الـ to the front of the word.
</p>

<p>
But&#x2026; there's some subtleties, which first require us to discuss vowel markings&#x2026; which also change if the <i>feminine
marker</i> ة is used, so we also need to briefly discuss gender.
</p>

<p>
English relies on <i>word order</i> for meaning; for example, <i>Jim hit Bob</i> is a sentence where the person doing the action is
<i>Jim</i> and we know it has to be <i>Jim</i>, and not <i>Bob</i>, since <i>Jim</i> is the word <i>before</i> the action <i>hit</i>. However, in Arabic words can
be ordered in almost any way you like! Then how do we identifiy who does an action? We use <b><span style="color:green;">case markings</span></b>: We add small
symbols to the end of words to indiciate the role they play in a sentence.
</p>

<p>
With vowel markings, we can finally flesh-out the nature of
“a/an/the” in Arabic&#x2026; but then something wild happens if we stick
an (in)definite <i>followed by</i> a definite! We get the concepts of ownership and complete sentences that don't need a verb!
</p>

<p>
Finally, we conclude with an explanation of why in the world English Qurans use the single word <i>muslim</i> where's Arabic
Qurans use both مسلمون and مسلمين.
</p>


</div>

<p>
Some <b><span style="color:green;">interesting</span></b> concepts will also be mentioned, for those curious, but should be ignored on a first
reading. These will be hidden away in <i>clickable/foldable</i> regions.
</p>

<p>
These are notes of things that I'm learning; there's likely errors.
</p>

<p>
<a href="https://en.wiktionary.org/wiki/%D9%86%D8%AD%D9%88">نحو</a> literally means <i>direction</i>; but <a href="https://altaysir.wordpress.com/2010/09/22/pkik9-ce/">when talking about Arabic grammar</a>, it means <i>syntax</i> (how words are combined to form
phrases) and, in-particular, case endings / إعراب.
</p>

<div id="outline-container-Al-I'rab-Case-Endings-الاعراب" class="outline-2">
<h2 id="Al-I'rab-Case-Endings-الاعراب"><span class="section-number-2">1.</span> <a href="#Al-I'rab-Case-Endings-الاعراب">Al-I'rab: Case Endings, الاعراب</a></h2>
<div class="outline-text-2" id="text-Al-I'rab-Case-Endings-الاعراب">
<p>
One day, my wife and I were looking at the word خبز, and we each read
it differently: I read it as خَبَزَ “he baked”, but she read it as خُبْزْ
“bread”. Without a context, we each guessed different <abbr class="tooltip" title="Arabs <em>infer</em> vowels from context, otherwise words alone such as حمل are ambigious: It could mean حَمَلَ “he carried” or حُمِلْ<br>“he was carried”.<br><br>As an example sentence with vowels written, Prophet Muhammad is known to have said:<br>| أنَا مَدِينَةُ الْعِلْمِ وَعَلَيٌ بَابُهَا&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br>| <em>I am the city of knowledge and Ali is its gate</em> |<br><br>Incidentally, Ali was the one who commissioned the system of vowels.<br>https://blogs.transparent.com/arabic/the-beginning-of-dotting-and-diacritics-in-arabic/<br><br><hr><br>Arabic has only three short vowels, or حركات (literally: “movements”), which are written as small symbols above/below<br>letters.<br><br>| Vowel name&emsp;| Vowel sound | Arabic | English example |<br>| Fatha;&emsp;فتحة&emsp;| <em>a</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـَ&emsp;&emsp;&emsp; | <em>mat</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br>| Dhamma; ظمّة&emsp;| <em>u</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـُ&emsp;&emsp;&emsp; | <em>sugar</em>&emsp;&emsp;&emsp;&emsp;&emsp; |<br>| Kasra; كسرة&emsp;| <em>i</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـِ&emsp;&emsp;&emsp; | <em>bit</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br><br>The “no vowel” marker is suukun/سكون: While هههه has its vowels guessed to be هَهَهَهَ “hahahah”, we obtain “hhhh” by using<br>sukkun, هْهْهْهْ. It is important for consonant-vowel-consonant syllables, such as بَابْ “bab” which means <em>door.</em><br><br>Incidentally, when a sound needs to be repeated twice, it is usually written once with a <em>Shadda</em> ـّـ to indicate the<br>doubling.&emsp;For example, فَهِمَ <em>fahima</em> “he understood” but فَهَّمَ <em>fahhama</em> “he explained”. Shadda is used with الـ + ‘sun<br>letters’. Unlike the other short vowels, the Shadda is usually written even in informal Arabic, to avoid ambiguity.<br><br><br>Arabic has 3 long vowels, which are formed using specific letters <em>after</em> the short vowels:<br> | Long vowel&emsp;sound | Arabic | English example |<br> | <em>aa</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـَا&emsp;&emsp;&emsp;| <em>far</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br> | <em>ii</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـِي&emsp;&emsp;&emsp;| <em>meet</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br> | <em>uu</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـُو&emsp;&emsp;&emsp;| <em>boot</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br><br>Since short vowels are normally not written, letters ا ي و play two roles: They behave as long vowels <em>aa,ii,uu</em> (when<br>preceded by short vowels) and also behave as consonant sounds <em>a,y,w</em>.<br> + For example, as a consonant, ي (https://arabic.fi/letters/74) makes an English “y” sound; but as a long vowel it makes an “ii” sound.<br> + Occasionally, <em>aa</em> is written using ی (which is like ي but without the dots), or یٰ, rather than an<br>&emsp; <em>alif</em>. This always happens at the end of a word and is called <em>alif maqsuura</em><br>&emsp; “broken alif”; for example علی “on” and موسیٰ “Musa”.<br><br>The following video reads all Arabic letters, where each letter is vowelised by one of the 3 short vowels. It's a really<br>nice video: https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6.">short vowels</abbr>!
</p>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Tell me more about vowels!
                      </font>
                    </strong>
                  </summary>
<p>
Arabs <i>infer</i> vowels from context, otherwise words alone such as حمل are ambigious: It could mean حَمَلَ “he carried” or حُمِلْ
“he was carried”.
</p>

<p>
As an example sentence with vowels written, Prophet Muhammad is known to have said:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">أنَا مَدِينَةُ الْعِلْمِ وَعَلَيٌ بَابُهَا</td>
</tr>

<tr>
<td class="org-left"><i>I am the city of knowledge and Ali is its gate</i></td>
</tr>
</tbody>
</table>

<p>
Incidentally, Ali was the one who commissioned the system of vowels.
<a href="https://blogs.transparent.com/arabic/the-beginning-of-dotting-and-diacritics-in-arabic/">https://blogs.transparent.com/arabic/the-beginning-of-dotting-and-diacritics-in-arabic/</a>
</p>

<hr />
<p>
Arabic has only three short vowels, or حركات (literally: “movements”), which are written as small symbols above/below
letters.
</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>
<tbody>
<tr>
<td class="org-left">Vowel name</td>
<td class="org-left">Vowel sound</td>
<td class="org-left">Arabic</td>
<td class="org-left">English example</td>
</tr>

<tr>
<td class="org-left">Fatha;  فتحة</td>
<td class="org-left"><i>a</i></td>
<td class="org-left">ـَ</td>
<td class="org-left"><i>mat</i></td>
</tr>

<tr>
<td class="org-left">Dhamma; ظمّة</td>
<td class="org-left"><i>u</i></td>
<td class="org-left">ـُ</td>
<td class="org-left"><i>sugar</i></td>
</tr>

<tr>
<td class="org-left">Kasra; كسرة</td>
<td class="org-left"><i>i</i></td>
<td class="org-left">ـِ</td>
<td class="org-left"><i>bit</i></td>
</tr>
</tbody>
</table>

<p>
The “no vowel” marker is suukun/سكون: While هههه has its vowels guessed to be هَهَهَهَ “hahahah”, we obtain “hhhh” by using
sukkun, هْهْهْهْ. It is important for consonant-vowel-consonant syllables, such as بَابْ “bab” which means <i>door.</i>
</p>

<p>
Incidentally, when a sound needs to be repeated twice, it is usually written once with a <i>Shadda</i> ـّـ to indicate the
doubling.  For example, فَهِمَ <i>fahima</i> “he understood” but فَهَّمَ <i>fahhama</i> “he explained”. Shadda is used with الـ + ‘sun
letters’. Unlike the other short vowels, the Shadda is usually written even in informal Arabic, to avoid ambiguity.
</p>


<p>
Arabic has 3 long vowels, which are formed using specific letters <i>after</i> the short vowels:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Long vowel  sound</td>
<td class="org-left">Arabic</td>
<td class="org-left">English example</td>
</tr>

<tr>
<td class="org-left"><i>aa</i></td>
<td class="org-left">ـَا</td>
<td class="org-left"><i>far</i></td>
</tr>

<tr>
<td class="org-left"><i>ii</i></td>
<td class="org-left">ـِي</td>
<td class="org-left"><i>meet</i></td>
</tr>

<tr>
<td class="org-left"><i>uu</i></td>
<td class="org-left">ـُو</td>
<td class="org-left"><i>boot</i></td>
</tr>
</tbody>
</table>

<p>
Since short vowels are normally not written, letters ا ي و play two roles: They behave as long vowels <i>aa,ii,uu</i> (when
preceded by short vowels) and also behave as consonant sounds <i>a,y,w</i>.
</p>
<ul class="org-ul">
<li>For example, as a consonant, <a href="https://arabic.fi/letters/74">ي</a> makes an English “y” sound; but as a long vowel it makes an “ii” sound.</li>
<li>Occasionally, <i>aa</i> is written using ی (which is like ي but without the dots), or یٰ, rather than an
<i>alif</i>. This always happens at the end of a word and is called <i>alif maqsuura</i>
“broken alif”; for example علی “on” and موسیٰ “Musa”.</li>
</ul>

<p>
The following video reads all Arabic letters, where each letter is vowelised by one of the 3 short vowels. It's a really
nice video: <a href="https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6">https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6</a>.
</p>


</details>

<p>
Here's another one..
</p>
</div>

<div id="outline-container-One-word-many-readings" class="outline-3">
<h3 id="One-word-many-readings"><span class="section-number-3">1.1.</span> <a href="#One-word-many-readings">One word, many readings</a></h3>
<div class="outline-text-3" id="text-One-word-many-readings">
<p>
What does حملت mean? Since ح−م−ل means “carrying”, and ـت is the past tense suffix, we have <i>at least</i> the following meanings:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">حَمَلْتُ</td>
<td class="org-left">I carried</td>
</tr>

<tr>
<td class="org-left">حَمَلْتَ</td>
<td class="org-left">You (masculine) carried</td>
</tr>

<tr>
<td class="org-left">حَمَلْتِ</td>
<td class="org-left">You (feminine) carried</td>
</tr>

<tr>
<td class="org-left">حَمَلَتْ</td>
<td class="org-left">She carried</td>
</tr>

<tr>
<td class="org-left">حُمِلتْ</td>
<td class="org-left">She was carried</td>
</tr>
</tbody>
</table>

<p>
Without the short symbols, the only way to distinguish the intended
meaning is for the word to be contextually located within a sentence
&#x2014;and even then, this would require experience.
</p>
</div>
</div>

<div class="outline-text-2" id="text-Al-I'rab-Case-Endings-الاعراب">
<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Tell me more about how verbs change, conjugate!
                      </font>
                    </strong>
                  </summary>
<p>
Arabic verbs are conjugated in the past tense by adding suffixes to the stem of the verb.
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">singular</td>
<td class="org-left">plural</td>
</tr>

<tr>
<td class="org-left">1</td>
<td class="org-left">ـْتُ</td>
<td class="org-left">ـْنا</td>
</tr>

<tr>
<td class="org-left">2m</td>
<td class="org-left">ـْتَ</td>
<td class="org-left">ـْتُمْ</td>
</tr>

<tr>
<td class="org-left">2f</td>
<td class="org-left">ـْتِ</td>
<td class="org-left">ـْتُنَّ</td>
</tr>

<tr>
<td class="org-left">3m</td>
<td class="org-left">ـَـ</td>
<td class="org-left">ـُوا</td>
</tr>

<tr>
<td class="org-left">3f</td>
<td class="org-left">ـَتْ</td>
<td class="org-left">ـْنَّ</td>
</tr>
</tbody>
</table>

<p>
For example, “they (feminine) studied” is هُنَّ دَرَسْنَّ.
</p>

<ul class="org-ul">
<li>Exercise: Conjugate <i>to study</i> دَرَسَ for each subject above.
( <a href="http://allthearabicyouneverlearnedthefirsttimearound.com/p1/p1-ch3/verbs-past-tense-and-the-accusative-case/">Answer</a> )</li>
<li>Exercise: Conjugate <i>to be generous</i> كَرُمَ and <i>to drink</i> شَرِبَ.</li>
<li>Exercise: What does درست mean?
<ul class="org-ul">
<li><i>Trick question!</i> You need the context, sentence, to <i>infer</i> the required
conjugation.</li>
</ul></li>
</ul>

<hr />

<p>
Note that the conjugation for the third-person masculine, هُمْ/they,
is <i>not phonetic</i>: The ending ـُوا has the long vowel ـُو pronounced, but the alif is silent. E.g., <i>they studied</i> is هُمْ دَرَسُوا
and is read “hum darasuu”.
</p>

<hr />

<p>
The personal pronouns (<i>I, you, they, etc</i>) are not usually used, since the verb conjugation tells us who the
subject is.  Sometimes they are used for emphasis.
E.g., <i>they studied</i> is دَرَسُوا
and is read “darasuu”.
</p>

<hr />

<p>
Arabic has no “to X” form, as in English <i>to eat, to drink, etc</i>.  Instead, it uses the <i>he</i> form of a verb when referring
to a verb in-general.  For example, <i>he studied</i> دَرَسَ is used to mean <i>to study</i> when we are taking about how the verb <i>to
study</i> changes depending on who is doing the studying. This form is chosen since it is the <b>simplest form</b>: It's the main 3
root consontants of the verb, followed by a fatHa.
</p>




</details>
</div>

<div id="outline-container-Word-Order" class="outline-3">
<h3 id="Word-Order"><span class="section-number-3">1.2.</span> <a href="#Word-Order">Word Order</a></h3>
<div class="outline-text-3" id="text-Word-Order">
<p>
Likewise, what does نصرت فاطمة mean? Does it mean “Fatimah helped (someone)”? Or does it mean “Fatimah was helped (by
someone)”?
</p>


<p>
One English sentence can be written a number of ways in Arabic:
</p>

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


<colgroup>
<col  class="org-center" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-center">Fatima helped Zaynab</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-center">نصرت فاطمةُ  زينبَ</td>
</tr>

<tr>
<td class="org-center">نصرت زينبَ فاطمةُ</td>
</tr>

<tr>
<td class="org-center">فاطمةُ نصرت زینبَ</td>
</tr>
</tbody>
</table>


<p>
The way the listener knows what’s the subject and what’s the object is quite literally carried around with the nouns
themselves. The endings make all the difference.
</p>
<div class="org-center">
<span style="color:green;">
<p>
<b>This is <a href="http://ejtaal.net/aa/#hw4=715,ll=2080,ls=5,la=2863,sg=701,ha=473,br=634,pr=104,vi=257,mgf=593,mr=420,mn=911,aan=407,kz=1608,uqq=237,ulq=1202,uqa=286,uqw=1064,umr=711,ums=591,umj=524,bdw=587,amr=425,asb=640,auh=1033,dhq=365,mht=591,msb=159,tla=70,amj=516,ens=28,mis=1439">اعراب</a>, <i>I'rab</i>, which <a href="https://en.wiktionary.org/wiki/%D8%B9_%D8%B1_%D8%A8#Derived_terms">literally</a> means “to Arabize” or
“to make elegant/clarify”.</b>
</p>

</span>
</div>

<p>
The second instance above might seem weird at first, since the object comes before the subject, but it is more common
when the object is an attached <abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronoun</abbr>:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">نصرتْها فطمةُ</td>
<td class="org-left">Fatima helped her.</td>
</tr>
</tbody>
</table>

<p>
This is a common example of the verb-object-subject word order!
</p>

<p>
Anyhow, in general, in Arabic the action word, <b>the verb</b>, can occur in the first
or second positions within a sentence whereas <b>the subject</b> (the person doing the
action) and <b>the object</b> (the thing/person being acted upon) can occur anywhere!
Here's another example:
</p>
<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Verbal sentences can have 4 different word orders</h3>
<p>
Below are 4 ways to say <b>Muhammad wrote the lesson</b> in Arabic!
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Sentence</th>
<th scope="col" class="org-left">Order of words</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">كَتَبَ مُحَمَّدٌ الدَّرسَ</td>
<td class="org-left">object ⇷ subject ⇷ verb</td>
</tr>

<tr>
<td class="org-left">كَتَبَ الدَّرسَ مُحَمَّدٌ</td>
<td class="org-left">subject ⇷ object ⇷ verb</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">مُحَمَّدٌ كَتَبَ الدَّرسَ</td>
<td class="org-left">object ⇷ verb ⇷ subject</td>
</tr>

<tr>
<td class="org-left">الدَّرسَ كَتَبَ مُحَمَّدٌ</td>
<td class="org-left">subject ⇷ verb ⇷ object</td>
</tr>
</tbody>
</table>

<p>
I personally have never encountered the fourth/last form above when actually
talking Arabic to other people.
</p>

</div>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Um actually, it's more accurate to say: Verbs must be singular before subjects
                      </font>
                    </strong>
                  </summary>

<ul class="org-ul">
<li>Verbs can come before or after the subject, and the choice is largely a matter
of emphasis/level of formality.</li>

<li>However, Arabic verbs change according to the subject, such as whether it's
one person or multiple people doing the action.</li>

<li>If the subject is a group of people, the verb will be <i>singular</i> if it comes
<i>before</i> the subject. It will only change according to whether the subject is
masculine or feminine.</li>
</ul>

<p>
For example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-center">. كَتَبَتْ البَنات خِطابات، ثُمَّ خَرَجْنَ</td>
</tr>

<tr>
<td class="org-center">The girls wrote letters and then went out.</td>
</tr>
</tbody>
</table>

<p>
Here كَتَبَتْ/wrote is a <i>singular feminine</i> form of the verb كَتَبَ/to-write since <i>it
comes before the subject</i>, which is البَنات/the-girls.  Likewise, خَرَجْنَ/went-out
is a <i>plural feminine</i>, since it comes after the subject.
</p>


</details>

<p>
Finally, as we will encounter later in this article, Arabic also has <i>sentences
without verbs</i>! These are just the subject followed by <b>the predicate</b>, which is
what is happening to the subject. Since these non-verbal sentences are only two
pieces (possibly complicated pieces), there are two ways to order them and both
are valid!
</p>
<div style="column-rule-style: none nil;column-count: 2;">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">The man died.</td>
</tr>

<tr>
<td class="org-left">≈ مَاتَ الرَّجلُ</td>
</tr>

<tr>
<td class="org-left">≈ الرَّجلُ مَاتَ</td>
</tr>
</tbody>
</table>


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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><span class="underline">The tall man</span> died.</td>
</tr>

<tr>
<td class="org-left">≈ مَاتَ                  <span class="underline">الرَّجلُ الطَّویلُ</span></td>
</tr>

<tr>
<td class="org-left">≈ <span class="underline">الرَّجلُ الطَّویلُ</span>        مَاتَ</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>


<div id="outline-container-Where-is-this-case-stuff-in-English" class="outline-3">
<h3 id="Where-is-this-case-stuff-in-English"><span class="section-number-3">1.3.</span> <a href="#Where-is-this-case-stuff-in-English">Where is this case stuff in English!?</a></h3>
<div class="outline-text-3" id="text-Where-is-this-case-stuff-in-English">
<p>
This <i>words-changing-due-to-role</i> behaviour also happens in English, but mostly with <abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronouns</abbr>: For example, <i><span style="color:purple;">He</span> saw
<span style="color:orange;">me</span>   ≈   <span style="color:orange;">I</span> was seen by <span style="color:purple;">him</span></i>.
</p>

<p>
In English, there are 3 ways to refer to oneself: <b><span style="color:green;">I</span>, <span style="color:red;">me</span>, <span style="color:blue;">my</span></b>.
For example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><b><span style="color:blue;">My</span></b> cat saw <b><span style="color:red;">me</span></b>, and <b><span style="color:green;">I</span></b> jumped!</td>
</tr>
</tbody>
</table>
<p>
Here's the rules:
</p>
<ul class="org-ul">
<li><i>(Nominative!)</i> When I am doing something, I say: <b><span style="color:green;"> I did it.</span></b></li>
<li><i>(Accusative!)</i> When something is being done to me, I say: <b><span style="color:red;"> It was done to me.</span></b></li>
<li><i>(Genitive!)</i> When I have an item, I say: <b><span style="color:blue;"> My thing....</span></b></li>
</ul>

<p>
So the word used to refer to <i>myself</i> changes depending on what is happening <b><span style="color:green;">by</span></b> me, <b><span style="color:red;">to</span></b> me, or <b><span style="color:blue;">of</span></b> me / what I
own.
</p>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Um, actually there's a 4th way: myself!
                      </font>
                    </strong>
                  </summary>
<p>
<i>Myself</i> is the forth way to refer to oneself in English. Like <b><span style="color:red;">me</span></b>, it is used when something is being done to me <i>such that</i>
the person doing the action is also me &#x2026;err, myself.
</p>

<p>
Here are some examples,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">I care for myself, by running everyday.</td>
</tr>

<tr>
<td class="org-left">I describe myself as happy.</td>
</tr>

<tr>
<td class="org-left">I like myself.</td>
</tr>
</tbody>
</table>

<p>
As a rule of thumb, <i>myself</i> should only be used if <i>I</i> is used in the same sentence. Otherwise, just use <i>me</i>.
</p>


</details>
</div>
</div>
<div id="outline-container-So-what's-the-deal" class="outline-3">
<h3 id="So-what's-the-deal"><span class="section-number-3">1.4.</span> <a href="#So-what's-the-deal">So, what's the deal?</a></h3>
<div class="outline-text-3" id="text-So-what's-the-deal">
<p>
Just as people dress according to roles or occassions (such as a
suit at work and pajamas in bed), so too Arabic words have
different case endings, التشكيلُ, to show their roles within a
sentence.
</p>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Roles are indicated by the vowel sign on the final letter of a word</h3>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Role</th>
<th scope="col" class="org-left">Ending Vowel</th>
<th scope="col" class="org-left">Case (Grammatical Name)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><span style="color:green;">Subject; the one doing an action</span></td>
<td class="org-left">ـُــ</td>
<td class="org-left">مرفوع / <span style="color:green;">Nominative</span></td>
</tr>

<tr>
<td class="org-left"><span style="color:red;">Object; the one being acted upon</span></td>
<td class="org-left">ـَــ</td>
<td class="org-left">منصوب / <span style="color:red;">Accusative</span></td>
</tr>

<tr>
<td class="org-left"><span style="color:blue;">Owner of a thing</span></td>
<td class="org-left">ـِــ</td>
<td class="org-left">مجرور / <span style="color:blue;">Genitive</span></td>
</tr>
</tbody>
</table>

</div>

<p>
More accurately, <span style="color:blue;">the genitive case</span> is used when a word follows a preposition <b>and</b>
it is used for all words after the first word in a possessive phrase &#x2014;which is
known as <abbr class="tooltip" title="Idafa, إظافة, is Arabic's version of the English <em>possessive 's</em>, a way of quickly<br>introducing the preposition “of”: English has <em>Jim's apple</em>, whereas Arabic would<br>say <em>the apple (of) Jim</em>.<br><br>Incidentally, English also simply places two nouns next to each other to form<br>possession: “the kitchen table” ---or طبلةُ المطبخِ. This is because English's<br>possessive <em>'s</em> is only used with animate non-things.<br><br>Idafa is a noun phrase, and so can be iterated onto itself to create complex<br>possessions: For example,<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; سَرْقةُ جَوازِ سَفَرِ أِحَدِ اللاعِبِينَ<br><em>the theft of the passport [literally ''license of journey''] of one of the athletes</em><br><br><hr><br><strong>Case endings for Idafa: Only one of “a/the/my”!</strong><br><br>The first term of an Idafa will be in any case the sentence requires; and <em>only<br>the last term</em> in an Idafa (however complex) can have the definite article <em>or</em><br>nunation <em>or</em> an attached possessive pronoun ---this is like English, <em>“a X's Y”</em> or<br><em>“the X's Y”</em> or <em>“my X's Y”</em>--- and this choice determines the definiteness of the<br>Idafa. Also, all terms of an Idafa, other than the first term, <strong>must</strong> be in the<br>genitive case: Ending with ـِـ or ـٍـ .<br><br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; | <strong>a</strong> nurse's book&emsp; | كتابُ مُمرّضةٍ&emsp;|<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; | <strong>the</strong> nurse's book | كتابُ ٱلْمُمرظةِ |<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; | <strong>my</strong> nurse's book&emsp;| كتابُ مُمرّضتي |<br><br>Note that nunnation is indefinite, whereas الـ and possessive-pronouns are definite.">إظافة</abbr> in Arabic, covered below. [Idafa is Arabic's way of quickly
introducing possession without the preposition “of”: English has <i>Jim's apple</i>,
whereas Arabic would say <i>(the) apple (of) Jim</i>, تفاحة جیم .]
</p>

<p>
<b>Nunation/تنوين/Tanween:</b> When the word is indefinite, one “doubles” the symbols, which causes an extra <i>-n</i> sound to each
vowel: u ـُـ, a ـَـ, i ـِـ are replaced by un ـٌـ, an ـًـ, in ـٍـ. (If a word does not end in ة, the ـًـ ending is written اً.)
<i>This is all covered below.</i>
For example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><span style="color:green;">Muhammad</span> was present.</td>
<td class="org-left">حَضَرَ مُحَمَّدٌ</td>
</tr>

<tr>
<td class="org-left">I saw <span style="color:red;">Muhammad</span>.</td>
<td class="org-left">رَأیْتُ مُحَمَّداً</td>
</tr>

<tr>
<td class="org-left">I passed by <span style="color:blue;">Muhmmad</span>.</td>
<td class="org-left">مَرَرْتُ بمُحَمَّدٍ</td>
</tr>
</tbody>
</table>


<div class="org-center">
<p>
<i>The accusative alif, اً, is our first example of Arabic case endings, اعراب, affecting the basic spelling and
pronunciation of words.</i>
</p>
</div>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         When speaking, endings are ignored in natural pauses
                      </font>
                    </strong>
                  </summary>
<p>
The case endings on the last word in a sentence are <b>not</b> pronounced. Nor are they pronounced before any natural
pause. For example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-center">.هذا طالبُ جدیدٌ</td>
</tr>

<tr>
<td class="org-center"><i>hatha talib-un jadded</i></td>
</tr>
</tbody>
</table>
<p>
The last word is read <i>jadded</i> and not <i>jadded-un</i> (with the case ending).
</p>

<p>
Likewise, any ة is not voiced as ت in such natural pauses. More on ة below.
</p>


</details>


<hr />

<p>
Anyhow, with the little we now know about these case endings, let's do one more
puzzle!
</p>
<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>What does      كتاب المدرس الجدید في المكتب     mean?</h3>
<p>
Hopefully by the end of this article, you'll be able to guess the missing
short vowels. However, the word الجديد could be correctly vowelised in 2 ways,
according to the 2 nouns we have (<i>teacher</i> and <i>book</i>) and it's not clear which one
is the correct one!
</p>

<p>
What do the following sentences mean?
</p>
<style> #g750 {color: orange; background-color:orange;}
       #g750:hover {color: black; background-color:white;} </style>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">. كتابُ المُدرّسِ الجدیدُ في المكتبِ</td>
<td class="org-left"><span id="g750">  A teacher's <b>new book</b> is in the office.  </span></td>
</tr>

<tr>
<td class="org-left">. كتابُ المُدرّسِ الجدیدِ في المكتبِ</td>
<td class="org-left"><span id="g750">  A <b>new teacher</b>'s book is in the office.  </span></td>
</tr>
</tbody>
</table>



<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Explain these interpretations please!
                      </font>
                    </strong>
                  </summary>
<p>
Here is a vague explanation, that can only make sense when the concepts
mentioned have already been understood &#x2014;and they are covered later on in this article.
</p>

<ol class="org-ol">
<li>In the first sentence we have a <b>new book</b> since the word <b>new</b> الجدیدُ has the same
case ending as كتابُ: This is called nominative or مرفو and denoted by ـُـ.
(Note: <b>new</b> starts with الـ since كتاب is definite, since it the start of an Idaafa.)</li>

<li>In the second sentence we have a <b>new teacher</b> since the word <b>new</b> الجدیدِ has the same case ending as المُدرّسِ: This is
called genitive or مجرور and denoted by ـِـ.</li>
</ol>

<p>
Without the case markings, you'll have to rely on context and common sense.
</p>

<p>
Otherwise, the number and plurality of the adjective (if any) would match
that of the word it is describing. For example, in مقالة المدرس الجدیدة we know to
read this as <i>the teacher's new article</i> since the words <i>new</i> and <i>article</i> both have the
same feminine gender.
</p>


</details>

</div>
</div>
</div>

<div id="outline-container-Formality-When-do-we-see-these-markings" class="outline-3">
<h3 id="Formality-When-do-we-see-these-markings"><span class="section-number-3">1.5.</span> <a href="#Formality-When-do-we-see-these-markings">Formality: When do we see these markings?</a></h3>
<div class="outline-text-3" id="text-Formality-When-do-we-see-these-markings">
<p>
Depending on the formality of some Arabic text, such as Classical Arabic or Quranic Arabic, you might see and hear
additional grammatical endings.
</p>

<p>
In this article, we'll see that these endings &#x2014;even when not explicitly written as markings&#x2014;
do alter the writing of words in certain situations. For example,
درست كتابا has its markings guessed to be
دَرَسْتُ كِتَاباً <b><i>“<span style="color:green;">I</span> studied <span style="color:red;">a book</span>”</i></b> &#x2014;the extra alif is really the alif-tanween of the accusative case, اً.
</p>
</div>
</div>

<div id="outline-container-I-can't-live-without-vowels-Yes-you-can" class="outline-3">
<h3 id="I-can't-live-without-vowels-Yes-you-can"><span class="section-number-3">1.6.</span> <a href="#org28f6d99"><i>I can't live without vowels!</i> Yes, you can! 💪</a></h3>
<div class="outline-text-3" id="text-I-can't-live-without-vowels-Yes-you-can">
<p>
What do you think the following English sentences say?
</p>

<style> #g751 {color: orange; background-color:orange;}
       #g751:hover {color: black; background-color:white;} </style>
<blockquote>
<ul class="org-ul">
<li><p>
Y cn prbbly rd ths sly dspt th lck f vwls!
</p>

<p>
<span id="g751"> You can probably read this easily despite the lack of vowels! </span>
</p></li>

<li><p>
Ys, y cn lv wtht vwls! Y cn vn wrt nglsh wtht thm; t nly nds sm prctc nd th rslt s drstclly shrtr sntncs! f nd b, lk Arbc, s vwls nly whn thr s mbguty.
</p>

<p>
<span id="g751"> Yes, you can live without vowels! You can even write English without them; it only needs some practice and the result is drastically shorter sentences! If need be, like Arabic, use vowels only when there is ambiguity. </span>
</p></li>
</ul>
</blockquote>



<p>
It might seem weird, for an English speaker, for vowels to be left-out, but conversely an Arabic speaker might think it's
extra effort in English to write out every vowel. It's different cultures, and traditions.
</p>

<p>
Just as it's a bit funny to drop the vowels in English, we can drop the dots in Arabic and the result is still somewhat
readable! In-fact, old Arabic did not have dots written down!
</p>
<center> <img src="https://qph.cf2.quoracdn.net/main-qimg-f160b4120fb65f79b12bb123b2530e45-pjlq">
<br><small> Translation: <em> Do you know that you can read complete passage without points? Because you are able to understand words through the context of the sentences, and the proof is that you have just read this passage.</em> </small>
<center> <small> <a href="https://qr.ae/pvlDtg"> Source </a> </small> </center>
</div>
</div>
</div>

<div id="outline-container-ة-Gender-and-tied-up-t-ت" class="outline-2">
<h2 id="ة-Gender-and-tied-up-t-ت"><span class="section-number-2">2.</span> <a href="#ة-Gender-and-tied-up-t-ت">ة &#x2014;Gender and “tied-up t/ت”</a></h2>
<div class="outline-text-2" id="text-ة-Gender-and-tied-up-t-ت">
<p>
Arabic nouns (words that name people, objects, or ideas) are classified as
<i>masculine</i> مُذَكَّر (“mudhakkar”) or <i>feminine</i> مُؤَنَّث (“mu'annath”).
This classification affects how other words in a sentence are written, such as action words or descriptive words.
</p>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Arabic Gender Rule</h3>

<p>
In general, <i>if a word ends in ة or refers to a female <span class="underline">person</span>, then it is a feminine word; otherwise it is a masculine
word.</i>
</p>

<p>
In more detail:
</p>
<ol class="org-ol">
<li>Words that end with the “feminine ending marker” ة are مُؤَنَّث.
<ul class="org-ul">
<li>The ة is known as the <i>Taa Marbuta</i> (literally: “tied-up ت”) and it is pronounced as a short vowel <i>a</i> sound.</li>
</ul></li>

<li>Words referring to female <i>people</i> but not ending in ة are مُؤَنَّث.</li>

<li>Most country names, natural features, and parts of the body that come in pairs are مُوَّنَّث.</li>

<li>Everything else is مُذَكَّر</li>
</ol>

</div>

<p>
<a href="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png" class="tooltip" title="Let's take a break"><img src="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png" height=50></a> Using the above rules, guess the genders of the following words. <i>Hover/click on the
<b><span style="color:orange;">orange</span></b> box to show the answer</i>.
</p>
<style> #g752 {color: orange; background-color:orange;}
       #g752:hover {color: black; background-color:white;} </style>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Word</th>
<th scope="col" class="org-left">Gender</th>
<th scope="col" class="org-left">Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">سيّارة <i>car</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث  </span></td>
<td class="org-left"><span id="g752">  See Rule-1 above  </span></td>
</tr>

<tr>
<td class="org-left">حقيبة <i>bag</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث  </span></td>
<td class="org-left"><span id="g752">  See Rule-1 above  </span></td>
</tr>

<tr>
<td class="org-left">خالة <i>aunt</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث  </span></td>
<td class="org-left"><span id="g752">  See Rule-1 above  </span></td>
</tr>

<tr>
<td class="org-left">بنت <i>girl</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث   </span></td>
<td class="org-left"><span id="g752">  See Rule-2 above  </span></td>
</tr>

<tr>
<td class="org-left">اُّمّ <i>mother</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث   </span></td>
<td class="org-left"><span id="g752">  See Rule-2 above  </span></td>
</tr>

<tr>
<td class="org-left">رجل <i>leg</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث   </span></td>
<td class="org-left"><span id="g752">  See Rule-3 above  </span></td>
</tr>

<tr>
<td class="org-left">شمس <i>sun</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث   </span></td>
<td class="org-left"><span id="g752">  See Rule-3 above  </span></td>
</tr>

<tr>
<td class="org-left">صحراء <i>desert</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث   </span></td>
<td class="org-left"><span id="g752">  See Rule-3 above  </span></td>
</tr>

<tr>
<td class="org-left">مصر <i>Egypt</i></td>
<td class="org-left"><span id="g752">  مُؤَنَّث   </span></td>
<td class="org-left"><span id="g752">  See Rule-3 above  </span></td>
</tr>

<tr>
<td class="org-left">أب <i>father</i></td>
<td class="org-left"><span id="g752">  مُذَكَّر   </span></td>
<td class="org-left"><span id="g752">  See Rule-4 above  </span></td>
</tr>

<tr>
<td class="org-left">بيت <i>house</i></td>
<td class="org-left"><span id="g752">  مُذَكَّر   </span></td>
<td class="org-left"><span id="g752">  See Rule-4 above  </span></td>
</tr>

<tr>
<td class="org-left">كتاب  <i>book</i></td>
<td class="org-left"><span id="g752">  مُذَكَّر   </span></td>
<td class="org-left"><span id="g752">  See Rule-4 above  </span></td>
</tr>
</tbody>
</table>



<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Quranic Quandary: خَلِیفَة
                      </font>
                    </strong>
                  </summary>
<p>
There are a few masculine words with the ة ending, but the only common on is خَلِيفَة “khalifa”. In the Quran this word has
the strict seance of <i>successor</i> or <i>viceroy</i>. In later times, this was generalised to <i>caliph</i>.
</p>


</details>
</div>

<div id="outline-container-On-the-nature-of-tied-up-t" class="outline-3">
<h3 id="On-the-nature-of-tied-up-t"><span class="section-number-3">2.1.</span> <a href="#On-the-nature-of-tied-up-t">On the nature of <i>tied-up-t</i></a></h3>
<div class="outline-text-3" id="text-On-the-nature-of-tied-up-t">
<p>
Taa Marbuta ة is a formed by taking the ends of ت and tying-them together to get ة.
(<i>Note: ت is also known as “ta mabsuta”, which literally means the “happy t” since the letter ت looks like a smiling face “🙂”)</i>
</p>


<p>
Examples:
</p>

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


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

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-right">0.</td>
<td class="org-left">grandfather</td>
<td class="org-left">جَدّ</td>
<td class="org-left">“jadd”</td>
</tr>

<tr>
<td class="org-right">1.</td>
<td class="org-left">grandmother</td>
<td class="org-left">جَدَّة</td>
<td class="org-left">“jadda”</td>
</tr>

<tr>
<td class="org-right">2.</td>
<td class="org-left">a grandmother</td>
<td class="org-left">جَدَّةً</td>
<td class="org-left">“jaddatan”</td>
</tr>

<tr>
<td class="org-right">3.</td>
<td class="org-left">my grandmother</td>
<td class="org-left">جَدَّتي</td>
<td class="org-left">“jaddaty”</td>
</tr>

<tr>
<td class="org-right">4.</td>
<td class="org-left">grandmothers</td>
<td class="org-left">جَدَّات</td>
<td class="org-left">“jaddaat”</td>
</tr>

<tr>
<td class="org-right">5.</td>
<td class="org-left">the boy's grandmother</td>
<td class="org-left">جَدَّةُ الولد</td>
<td class="org-left">“jaddatu al-walad”</td>
</tr>
</tbody>
</table>


<dl class="org-dl">
<dt>Example #1</dt><dd>The Taa Marbuta is special in contrast to the other letters: It can only be written at the end of a word, either
unjoined as ة or joined as ـَـة:
<ul class="org-ul">
<li>It is purely a grammatical letter, it has no sound!
<ul class="org-ul">
<li>It is the ending of most singluar feminine nouns/adjectives, or nouns referring to female people.</li>
</ul></li>
<li>It <i>always</i> follows a Fatha vowel, as in جدَة or غرفَة, and so people would say ة makes a short <i>a</i> sound &#x2014;but this is
really due to the vowel that always comes before ة!</li>
</ul></dd>

<dt>Example #0 changes to #1</dt><dd><b><span style="color:green;">As a suffix, ـَـة / ة is used to make feminine adjectives or nouns from masculine ones.</span></b></dd>

<dt>Examples #2 and #3</dt><dd>It becomes “untied/opened ت” when suffixes/endings are added.
<ul class="org-ul">
<li>The formal indefinite, Example #2, is discussed below.</li>
</ul></dd>

<dt>Example #4</dt><dd>A feminine word, ending in ـَـة is made plural by extending the Fatha into a long vowel ـَـا and opening
the Taa Marbuta into ت.</dd>

<dt>Example #5</dt><dd>When it is followed by another word, the pronunciation of ة is <i>t</i> −-−though the spelling remains
unchanged. Putting two words beside each other is known as <i>possession, addition, إظافة</i>, and it's covered below.</dd>
</dl>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         ة has a number of other interesting uses
                      </font>
                    </strong>
                  </summary>

<dl class="org-dl">
<dt>It forms singulatives from collectives</dt><dd><p>
From a word that refers to a collection of things, we can refer to
one of those things by adding ة.
</p>

<p>
For example, we get <i>cow</i> بَقَرَة‎ “baqara” from <i>cows</i> بَقَر‎ “baqar”; and we get <i>tree</i> شَجَرَة‎ “shajara” from <i>trees</i> شَجَر‎ “shajar”.
</p>

<p>
It is used this way to indicate <i>one of something</i>.  For example, from <i>watermelon</i> بطيخ and <i>carrot</i> جزر we obtain <i>one
watermelon</i> بطيخة and <i>one carrot</i> جزرة.
</p></dd>

<dt>It forms instances from general verbal nouns</dt><dd><p>
We can refer to a single instance of an action by adding ة.
</p>

<p>
For example, we get <i>a smile</i> اِبْتِسَامَة‎ “ibtisama” from <i>smiling</i> اِبْتِسَام‎ “ibtisam”;
and <i>an uprising</i>  اِنْتِفَاضَة‎ “intifatha” from <i>rising up</i> اِنْتِفَاض‎ “intifith”.
</p></dd>

<dt>It forms nouns referring to devices from occupational/characteristic nouns and adjectives</dt><dd>For example, <i>tank</i> دبابة “dabbaba” from <i>crawler</i> دباب “dabab”; and <i>printer (device)</i> طَابِعَة‎ “tabi'a” from <i>printer (person)</i>
طَابِع‎ “tabi'”.</dd>
</dl>


</details>
</div>
</div>


<div id="outline-container-Grab-a-snack-and-watch-these-helpful-videos-card-Yes" class="outline-3">
<h3 id="Grab-a-snack-and-watch-these-helpful-videos-card-Yes"><span class="section-number-3">2.2.</span> <a href="#Grab-a-snack-and-watch-these-helpful-videos-card-Yes">Grab a snack and watch these helpful videos, <a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png" class="tooltip" title="Yes"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png" height=50></a>!</a></h3>
<div class="outline-text-3" id="text-Grab-a-snack-and-watch-these-helpful-videos-card-Yes">
<div style="column-rule-style: none nil;column-count: 3;">

<center>What is ة<iframe width="70%" src="https://www.youtube.com/embed/dymgNFPsm8Y" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></center>

<center>Everything about ة<iframe width="70%" src="https://www.youtube.com/embed/nuX9tK6vV84" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></center>

<center>Body parts in Arabic, fun!<iframe width="70%" src="https://www.youtube.com/embed/VBjlmwF99OI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></center>


</div>
</div>
</div>
</div>

<div id="outline-container-When-do-you-really-know-a-thing" class="outline-2">
<h2 id="When-do-you-really-know-a-thing"><span class="section-number-2">3.</span> <a href="#When-do-you-really-know-a-thing">When do you really know a thing?</a></h2>
<div class="outline-text-2" id="text-When-do-you-really-know-a-thing">
<p>
You and your friends are talking, and someone says the word <i>bag</i> حقیبة.
Is it a random bag (nonspecific, general, “indefinite”, نَكِرَة), or is it one <b>you know something</b> about it (specific,
“definite”, مَعْرِفَة)?
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">An item</th>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">Do we know to whom it belongs?</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">a bag</td>
<td class="org-left">حقیبة</td>
<td class="org-left">🤷 <i>It's a random bag!</i></td>
</tr>

<tr>
<td class="org-left">the bag</td>
<td class="org-left">الحقیبة</td>
<td class="org-left">😎 <i>It's the one we're already talking about!</i></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">her bag</td>
<td class="org-left">حقیبتها</td>
<td class="org-left">😎 <i>It belongs to someone we've mentioned already!</i></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">Zaynab's bag</td>
<td class="org-left">حقيبة زینب</td>
<td class="org-left">😎 <i>It belongs to Zaynab!</i></td>
</tr>

<tr>
<td class="org-left">the teacher's bag</td>
<td class="org-left">حقیبة المُدرّسة</td>
<td class="org-left">😎 <i>It belongs to the teacher!</i></td>
</tr>

<tr>
<td class="org-left">a teacher's bag</td>
<td class="org-left">حقيبة مُدرّسة</td>
<td class="org-left">🤷 <i>It's a bag that belongs to a random teacher!</i></td>
</tr>
</tbody>
</table>

<br>
<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3></h3>
<p>
So, it seems a word can have <b>exactly one</b> of “a/the/my”, that is, it can be
either indefinite <i>with</i> tanween, definite with <i>al</i>, or possessed (by a pronoun or an Idafa, covered below).
</p>

</div>

<p>
In the rest of this section, we will talk about the first pair of examples.
</p>

<ul class="org-ul">
<li>The last group will be covered later on in this article.</li>
<li><p>
The middle group, حقیبتها, is just <i>bag</i> along with the <abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronoun</abbr> <i>her</i> added to the end, and the ة opens-up into a ت as
discussed already. There's not much here, besides reviewing Arabic <abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronouns</abbr>.
</p>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Tell me more about pronouns!
                      </font>
                    </strong>
                  </summary>

<p>
Personal pronouns are the equivalent of the English <i>I, we, you she, he, &#x2026;</i>.
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">singular</td>
<td class="org-left">plural</td>
</tr>

<tr>
<td class="org-left">1</td>
<td class="org-left">أنا     <i>I</i></td>
<td class="org-left">نَحْن   <i>we</i></td>
</tr>

<tr>
<td class="org-left">2m</td>
<td class="org-left">أَنْتَ    <i>you</i></td>
<td class="org-left">أَنْتُم   <i>you</i></td>
</tr>

<tr>
<td class="org-left">2f</td>
<td class="org-left">أَنْتِ    <i>you</i></td>
<td class="org-left">أَنتُن   <i>you</i></td>
</tr>

<tr>
<td class="org-left">3m</td>
<td class="org-left">هُوَ     <i>he/it</i></td>
<td class="org-left">هُم    <i>they</i></td>
</tr>

<tr>
<td class="org-left">3f</td>
<td class="org-left">هِيَ     <i>she/it</i></td>
<td class="org-left">هُنَّ     <i>they</i></td>
</tr>
</tbody>
</table>

<p>
When <b>I</b> am talking, the speaker is the “first person” (“1”);
when taking <b>about you</b>, then you are the “second person”
and may be masculine (“2m”) or feminine (“2f”), or a group of you (“plural”);
finally, when talking about someone who is <b>not here</b> in the conversation,
they are in the “third person” (“3m, 3f”).
</p>

<hr />

<p>
Possessive pronouns are the equivalent of the English <i>my, his, ours, &#x2026;</i>.
In Arabic, they are <b>joined to the end</b> of a word: For example,
<i>house</i> بیت becomes <i>my house</i> بیتِي.
</p>

<p>
Here are the attached possessive pronouns:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">singular</td>
<td class="org-left">plural</td>
</tr>

<tr>
<td class="org-left">1</td>
<td class="org-left">ـِي</td>
<td class="org-left">ـنَا</td>
</tr>

<tr>
<td class="org-left">2m</td>
<td class="org-left">ـكَ</td>
<td class="org-left">ـكُمْ</td>
</tr>

<tr>
<td class="org-left">2f</td>
<td class="org-left">ـكِ</td>
<td class="org-left">ـكُنَّ</td>
</tr>

<tr>
<td class="org-left">3m</td>
<td class="org-left">ـَهُ</td>
<td class="org-left">ـهُمْ</td>
</tr>

<tr>
<td class="org-left">3f</td>
<td class="org-left">ـَهَا</td>
<td class="org-left">ـهُنَّ</td>
</tr>
</tbody>
</table>

<p>
Exercise: Add these endings to the word <i>house</i>; for example, <i>my house</i> بیتِي.
</p></li>
</ul>


</details>
</div>
<div id="outline-container-Tanween-Formally-Indefinite-نَكِرَة-a-an-or-un-ـٌــ" class="outline-3">
<h3 id="Tanween-Formally-Indefinite-نَكِرَة-a-an-or-un-ـٌــ"><span class="section-number-3">3.1.</span> <a href="#Tanween-Formally-Indefinite-نَكِرَة-a-an-or-un-ـٌــ">Tanween, <i>Formally</i> Indefinite, نَكِرَة: “a/an” or “un” ـٌــ</a></h3>
<div class="outline-text-3" id="text-Tanween-Formally-Indefinite-نَكِرَة-a-an-or-un-ـٌــ">
<p>
Technically, Arabic does not have an indefinite article like English's <i>a/an</i>.  Instead, indefininte/nonspecific words
have <i>doubled case markings</i>: So instead of ـِـ ـُـ ـَـ we have ـٍـ ـٌـ ـًـ, where the second marking is pronounced as a ن/n
sound.  This is known as تنوین/Tanween, or <i>nunation</i>, which means <i>pronouncing the letter ن at the end of a word</i>, or <i>putting a nun/ن on</i>.
(Often the double ـُـ is written as one ـُـ with a tail: ـٌـــ.)
</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">English</th>
<th scope="col" class="org-left">Arabic</th>
<th scope="col" class="org-left">Transliteration</th>
<th scope="col" class="org-left">Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">a boy</td>
<td class="org-left">ولدٌ</td>
<td class="org-left">walad-un</td>
<td class="org-left">Nomative ـٌـ; a boy is doing something</td>
</tr>

<tr>
<td class="org-left">a book</td>
<td class="org-left">كتابٍ</td>
<td class="org-left">kitaab-in</td>
<td class="org-left">Genitive ـٍـ; a book is being owned</td>
</tr>

<tr>
<td class="org-left">a car</td>
<td class="org-left">سيّارةً</td>
<td class="org-left">sayarat-an</td>
<td class="org-left">Accusative ـًـ; something is happening to a car</td>
</tr>

<tr>
<td class="org-left">a book</td>
<td class="org-left">كتاباً</td>
<td class="org-left">kitaab-an</td>
<td class="org-left">Accusative ـاً; something is happening to a book</td>
</tr>
</tbody>
</table>

<ul class="org-ul">
<li>Notice that if a noun ends in ة “tied-up t”, the <i>t</i> is actually pronounced before the Tanween.</li>
<li>Secondly, unless a word ends in ة or ی or ـاء, then double-fatha ـًـ has to be written on an alif as اً. This alif is a
spelling convention; it is not pronounced; unlike case markings, it is always written (e.g., كتابا possibly without
the ـًـ).</li>
</ul>
</div>
</div>

<div id="outline-container-Definite-مَعْرِفَة-the-or-al-الـــ" class="outline-3">
<h3 id="Definite-مَعْرِفَة-the-or-al-الـــ"><span class="section-number-3">3.2.</span> <a href="#Definite-مَعْرِفَة-the-or-al-الـــ">Definite, مَعْرِفَة - “the” or “al”  الـــ</a></h3>
<div class="outline-text-3" id="text-Definite-مَعْرِفَة-the-or-al-الـــ">
<p>
There is no indefinite article equivalent to the English “a/an”.  However, the large majority of nouns and adjectives
have <i>tanween</i> (the addition of the sound <i>n</i>) to the final vowel of a word) to indicate that the word is indefinite:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">a reward</td>
<td class="org-left">أجْرٌ</td>
<td class="org-left">ajurn</td>
</tr>

<tr>
<td class="org-left">a sign/verse</td>
<td class="org-left">آيْةٌ</td>
<td class="org-left">ayatun</td>
</tr>

<tr>
<td class="org-left">a recitation</td>
<td class="org-left">قُرْآنٌ</td>
<td class="org-left">qur'anun</td>
</tr>
</tbody>
</table>

<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         What is آ ?
                      </font>
                    </strong>
                  </summary>
<p>
It has become standard for a hamza followed by a long <i>aa</i> sound to be written as two <i>alifs</i>, over vertical and on
horizontal: آ. This is known as the <b>alif madda</b>.
</p>

<p>
This was already discussed in: <a href="http://alhassy.com/arabic-roots#Arabic-has-112-symbols-and-112-sounds">http://alhassy.com/arabic-roots#Arabic-has-112-symbols-and-112-sounds</a>
</p>


</details>

<p>
However, in everyday, non-vowelised, Arabic there is no separate word/marking for “a/an”, as in “a chair” or “an apple”.
</p>
<ul class="org-ul">
<li>By default, words are <i>indefinite</i>: For example, مكتب means “an office”, even though there is no separate word for the
“an”.</li>
<li><p>
To make a noun <i>definite</i> we add الـ “al” <i>joined</i> to it, which means “the”.
For example:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">  “the office”</td>
</tr>

<tr>
<td class="org-left">≈ “the” ⇸ “office”</td>
</tr>

<tr>
<td class="org-left">≈  ال ⇷ مكتب</td>
</tr>

<tr>
<td class="org-left">≈  المكتب</td>
</tr>
</tbody>
</table>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         What are directed additions ⇸ and ⇷?
                      </font>
                    </strong>
                  </summary>
<p>
I will use <i>directed addition symbols</i> ⇸ and ⇷ to mean the same
as “+” but also to indicate the direction one should read it.
For example, <i>X + Y</i> could mean <i>X Y</i> in English's left-to-right reading, but it could also mean <i>Y X</i> in Arabic's
right-to-left reading. Whereas <i>X ⇷ Y</i> can only mean <i>X Y (read right-to-left)</i>.
</p></li>
</ul>


</details>

<p>
Frequently, the sound of الـ <i>al</i> may have both the <i>a</i> sound, the <i>l</i> sound, or both sounds change!
The rules are pretty simple.
</p>
<ul class="org-ul">
<li>These are changes in pronunciation <i>only</i>, the spelling of “al” الـ doesn't change.</li>
</ul>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         ٱلْـ / Elision: The “a” of “al” الـ is silent if the previous word ends in a vowel
                      </font>
                    </strong>
                  </summary>

<p>
If الـ “al” comes directly after a vowel, the “a” of “al” الـ will drop out, or elide, leaving just the “l” sound. This
only affects pronunciation and not the spelling.
</p>

<p>
For example,
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">the house</td>
<td class="org-left">البيت</td>
<td class="org-left">“al-bayt”</td>
</tr>

<tr>
<td class="org-left">in the house</td>
<td class="org-left">في البيت</td>
<td class="org-left">“fi l-bayt”</td>
</tr>
</tbody>
</table>


</details>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Assimilation: The Sun Letters Assimilate the “l” of “al” الـ
                      </font>
                    </strong>
                  </summary>

<p>
Nouns starting with certain letters of the Arabic alphabet cause the pronunciation of “al” الـ to change: The “l” sound
becomes the same as the first sound of the noun. This double-pronunciation of the first letter of the noun is indicated
with a Shadda ـّـ symbol, if vowel marks are written.
</p>

<p>
For example,
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">a car</td>
<td class="org-left">سيّارة</td>
<td class="org-left">“sayyara”</td>
</tr>

<tr>
<td class="org-left">the car</td>
<td class="org-left">السّيّارة</td>
<td class="org-left">“as-sayyara”</td>
</tr>
</tbody>
</table>

<p>
Notice that السّيّارة is <b>not</b> read “al-sayyara”! The “l” sound changes to be the sound of the first letter of سيّارة, namely
“s”.
</p>

<p>
Likewise, <i>a river</i> is نهر whereas <i>the river</i> is النّهر “an-nahr”.
</p>

<hr />

<p>
The letters which cause this pronunciation assimilation are called
<b>sun letters</b>, الحروف الشمسية “al-huruf ash-shamsiyya”, as ش/sh is itself an assimilating letter. Half of Arabic's 28 letters are Sun Letters.
The remaining half of the letters are called Moon Letters,
الحروف القمرية “al-huruf al-gamariyya”, as ق/G is not an assimilating letter.
</p>

<dl class="org-dl">
<dt>☀️ Sun Letters</dt><dd>ت ث د ذ ر ز س ش ص ض ط ظ ل ن</dd>
<dt>🌙 Moon Letters</dt><dd>ا ب ج ح خ ع غ ف ق ك م ه و ي</dd>
</dl>

<p>
Just as we use a shadda ـّـ on a sun letter, we place a sukoon ـْـ on the ل when it comes after moon letters: For example,
اَلْقمر “al-qamar” &#x2014;the sukoon gives us a slight pause after the “l” sound.
</p>


</details>

<p>
The above two rules are explained by the following theoretical justification.
</p>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="blue">
                         Um, actually the definite article is really just لْ
                      </font>
                    </strong>
                  </summary>
<p>
In fact, the definite article is in essence simply a لْ, an “l” sound. But as Arabic phonetic theory holds that words
cannot begin with an unvowelled consonant, the vowel <i>a</i> (Fatha) is added to the لْ to give اَلْ, <i>al</i>. Theory holds that this
<i>a</i> vowel is not an integral part of the definite article and is required when no other vowel precedes the article
<i>l</i>. In effect this means that the added vowel is only at the beginning of a sentence. In other places, the vowel Fatha is
replaced by a “joining sign” (<i>wasla</i>) to obtain ٱلـ, which tells you to link the <i>l</i> of the definite article to the final
vowel of the preceding word.
</p>

<p>
In short, you will find اَلْـ at the beginning, and ٱلْـ elsewhere in the sentence. The use of the two can be seen as
follows:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">the clear book</td>
<td class="org-left">اَلْكِتَابُ ٱلْمُبِينُ</td>
<td class="org-left">al-kitab-u l-mubeen-u</td>
</tr>
</tbody>
</table>

<p>
Note: In front of Sun Letters, اَلـ is written with <i>no sukkun</i> on the ل, since there is no pause on the ل; in-fact the ل
is assimilated and makes a different sound altogether.
</p>

<p>
We will get to sentence formation, later below.
</p>


</details>


<p>
Exercise: From your knowledge of <i>pronunciation</i> of ة and the two special pronunciation rules of الـ, guess how the following
would be read.
</p>
<style> #g753 {color: green; background-color:green;}
       #g753:hover {color: black; background-color:white;} </style>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">السَّيَّارَةُ الجَدِيْدَة</td>
</tr>

<tr>
<td class="org-left"><span id="g753">  as-sayyara-tu l-jadded-a  </span></td>
</tr>
</tbody>
</table>



<hr />

<p>
Remember: Since tanween indicates indefiniteness, a definite word <i>cannot</i> have tanween!
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">a boy</td>
<td class="org-left">ولدٌ</td>
<td class="org-left">“walad-un”</td>
</tr>

<tr>
<td class="org-left">the boy</td>
<td class="org-left">الولدُ</td>
<td class="org-left">“al-walad-u”</td>
</tr>
</tbody>
</table>

<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Quranic Quandary: اَل ⇷ ل = اَلّ
                      </font>
                    </strong>
                  </summary>
<p>
In the Quran, when the definite article is prefixed to a word
beginning with ل, only one ل is written. For example,
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">the night</td>
<td class="org-left">اَلَّیْلُ</td>
<td class="org-left">al-laylu</td>
</tr>
</tbody>
</table>

<p>
This is not normally the case in modern Arabic.
</p>


</details>

<hr />

<p>
😉 <b>Arabic is so simple!</b>
Unlike other languages, Arabic has الـ for <i>the</i> regardless of whether we're talking about
one person, or many! For example, in French, we have 3-ways to say <i>the</i>: <i>le, la, les</i>.
</p>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         الـ is usually part of Arabic names!
                      </font>
                    </strong>
                  </summary>
<p>
For example, a person named <i>John</i> who happens to be a smith, a worker in metal, might be referred to as <i>John the
smith</i>. In English, this became <i>John Smith</i>, and in Arabic it becomes <i>جون الحداد</i> “john al-hadad” &#x2014;where حداد means
<i>smith</i>.
</p>

<p>
A more important figure would be <a href="https://en.wikipedia.org/wiki/Hasan_al-Askari">حسن العسكري</a>: A person named حسن/Hassan who lived in a town of soldiers/عسكر. He is a
great grandson of Prophet Muhammad.
</p>

<p>
This <i>name-followed-by-profession/town</i> construction is known as Idafa/إظافة and it is discussed later in this article.
</p>


</details>
</div>
</div>
</div>

<div id="outline-container-Idafa-In-definite-followed-by-definite" class="outline-2">
<h2 id="Idafa-In-definite-followed-by-definite"><span class="section-number-2">4.</span> <a href="#Idafa-In-definite-followed-by-definite">Idafa: (In)definite <i>followed</i> by definite</a></h2>
<div class="outline-text-2" id="text-Idafa-In-definite-followed-by-definite">
<div class="org-center">
<p>
<i>What happens when you stick two nouns together? What about definiteness?</i>
</p>
</div>
</div>
<div id="outline-container-Possession-Addition-إظـافـة-idafa" class="outline-3">
<h3 id="Possession-Addition-إظـافـة-idafa"><span class="section-number-3">4.1.</span> <a href="#Possession-Addition-إظـافـة-idafa">Possession: <i>Addition</i> إظـافـة “idafa”</a></h3>
<div class="outline-text-3" id="text-Possession-Addition-إظـافـة-idafa">
<p>
Arabic expresses possession by placing two nouns next to each other: <span style="color:red;">possessor</span>  ⇷ <span style="color:green;">possessed</span> (read <i>right-to-left</i>).
This is إظـافـة, which literally means <i>addition</i>.
</p>

<div style="column-rule-style: none nil;column-count: 3;">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">1. <span style="color:red;">Anwar's</span> <span style="color:green;">book</span></td>
</tr>

<tr>
<td class="org-left">≈  <span style="color:green;">كتاب</span> <span style="color:red;">أنور</span></td>
</tr>
</tbody>
</table>

<hr style="border:none; height:40px">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">2. <span style="color:green;">the manager</span> of <span style="color:red;">the department</span></td>
</tr>

<tr>
<td class="org-left">≈ <span style="color:red;">the department's</span> <span style="color:green;">manager</span></td>
</tr>

<tr>
<td class="org-left">≈ <span style="color:green;">مدير</span> <span style="color:red;">القسم</span></td>
</tr>
</tbody>
</table>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">3. <span style="color:green;">the manager</span> of <span style="color:red;"><em>a</em> department</span></td>
</tr>

<tr>
<td class="org-left">≈  <span style="color:red;"><em>a</em> department's</span> <span style="color:green;">manager</span></td>
</tr>

<tr>
<td class="org-left">≈  <span style="color:green;">مدير</span> <span style="color:red;">قسم</span></td>
</tr>
</tbody>
</table>


</div>

<p>
Since <b>the</b> <span style="color:green;">possessed</span> item is <i>known</i> to belong to the <span style="color:red;">possessor</span>, the English translations all use “the” before
the <span style="color:green;">possessed</span> item: <i>The <span style="color:green;">X</span> of <span style="color:red;">Y</span></i>. That is, just as in English <i><span style="color:red;">Y's</span> <span style="color:green;">X</span></i> means <i>X</i> is known to
<span class="underline">definitely</span> belong to <i>Y</i>, Arabic treats the <span style="color:green;">possessed</span> word in an إظافة as “definite in meaning” (even though it is
not “definite in form; has no الـ”); see example #3 above. We can summarise this
observation as follows.
</p>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Case endings for Idaafa: Only one of “a/the/my”!</h3>

<p>
The first term of an Idafa will be in any case the sentence requires; and <i>only the last term</i> in an Idafa (however
complex) can have the definite article <i>or</i> nunation <i>or</i> an attached possessive <abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronoun</abbr> &#x2014;this is like
English, <i>“a X's Y”</i> or <i>“the X's Y”</i> or <i>“my X's Y”</i>&#x2014; and this choice determines the definiteness of the Idafa. Also, all terms of
an Idafa, other than the first term, <b>must</b> be in the genitive case.
</p>

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


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><b>a</b> nurse's book</td>
<td class="org-left">كتابُ مُمرّضةٍ</td>
</tr>

<tr>
<td class="org-left"><b>the</b> nurse's  book</td>
<td class="org-left">كتابُ ٱلْمُمرظةِ</td>
</tr>

<tr>
<td class="org-left"><b>my</b> nurse's  book</td>
<td class="org-left">كتابُ مُمرّضتي</td>
</tr>
</tbody>
</table>

<p>
Note that nunnation is indefinite, whereas الـ and possessive-pronouns are definite.
</p>

</div>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         What's the deal with pronoun suffixes?
                      </font>
                    </strong>
                  </summary>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">her book</td>
<td class="org-left">كتابها</td>
</tr>
</tbody>
</table>
<p>
Technically, we can treat “her book” as an Idaafa construction, with the pronoun
“her”, written in Arabic as an attached ending ـها, being the second term in the
Idafa. Since pronouns are technically
definite, they will always end an Idafa whenever they are used.
Here's another example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">my brother's book</td>
<td class="org-left">كتاب اخي</td>
</tr>
</tbody>
</table>


</details>


<p>
The <i>result</i> of an addition, إظافة, is noun phrase which itself can be the <span style="color:red;">possessor</span> of something else. Whether this
<i>result</i> is definite or not is determined by whether the <i>final</i> noun in the إظافة is definite or not; see example #2 above.
</p>


<p>
The <i>result</i> of an addition, إظافة, is noun phrase which itself can be the <span style="color:red;">possessor</span> of something else. As such, we
can repeat the إظافة construction onto itself a number of times:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">  the son of the manager of the sales department</td>
</tr>

<tr>
<td class="org-left">≈ <span style="color:green;">the son</span> of <span style="color:red;">(the manager of (the department of sales))</span></td>
</tr>

<tr>
<td class="org-left">≈ <span style="color:green;">ابن</span> <span style="color:red;">مدير قسم المبيعات </span></td>
</tr>
</tbody>
</table>

<hr />
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Idiomatic uses of Idafa
                      </font>
                    </strong>
                  </summary>

<ol class="org-ol">
<li><p>
The use of 2 nouns in an idafa to represent an idea that has to be translated as a noun and a qualifying
adjective. Duh, that's the whole point of idafa with adjectives, to created qualified nouns!
</p>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">قَوْمُ سَوْءٍ</td>
</tr>

<tr>
<td class="org-left">≈ people of evil</td>
</tr>

<tr>
<td class="org-left">≈ an evil people</td>
</tr>
</tbody>
</table></li>

<li><p>
The use of certain words, such as <i>umm, ab, ibn, ahl, saahib, dhu</i>
(accusative dhaa, genitive dhii found only with a following genitive) to represent a single idea. lol see #1 above.
</p>

<div style="column-rule-style: none nil;column-count: 3;">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">اِبْنُ ٱلسّبِیلِ</td>
</tr>

<tr>
<td class="org-left">≈ son of the road</td>
</tr>

<tr>
<td class="org-left">≈ traveller</td>
</tr>
</tbody>
</table>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">أَهْلُ بَیْتٍ</td>
</tr>

<tr>
<td class="org-left">≈ family of a house</td>
</tr>

<tr>
<td class="org-left">≈ a household</td>
</tr>
</tbody>
</table>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">ذُو ٱلْفَضْلِ</td>
</tr>

<tr>
<td class="org-left">≈ possessed of bounty</td>
</tr>

<tr>
<td class="org-left">≈ bountiful</td>
</tr>
</tbody>
</table></li>
</ol>


</div>


</details>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Exercise: Form the Idafa with the correct case endings!
                      </font>
                    </strong>
                  </summary>
<p>
Write the Arabic Idafa for each English phrase and, for simplicity, place the first term of the Idafa in the nominative case.
</p>

<style> #g754 {color: orange; background-color:orange;}
       #g754:hover {color: black; background-color:white;} </style>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Colloquial phrase</th>
<th scope="col" class="org-left">Unfolding into using “of”</th>
<th scope="col" class="org-left">Arabic</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">A university professor</td>
<td class="org-left">≈ a professor (of) a university</td>
<td class="org-left">≈  استاذُ جامعةٍ</td>
</tr>

<tr>
<td class="org-left">The office director</td>
<td class="org-left">≈ the director (of) the office</td>
<td class="org-left">≈ <span id="g754">  مُدیرُ المكتبِ       </span></td>
</tr>

<tr>
<td class="org-left">the house yard</td>
<td class="org-left">≈ the yard (of) the house</td>
<td class="org-left">≈ <span id="g754">  سَاحَةُ البَيْتِ         </span></td>
</tr>

<tr>
<td class="org-left">the bedroom</td>
<td class="org-left">≈ the room (of) sleeping</td>
<td class="org-left">≈ <span id="g754">  غُرْفَةُ النَّوْمِ         </span></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">A teacher's house</td>
<td class="org-left">≈ a house (of) a teacher</td>
<td class="org-left">≈ <span id="g754">  بیتُ مُدرّسٍ       </span></td>
</tr>

<tr>
<td class="org-left">The teacher's house</td>
<td class="org-left">≈ the house (of) the teacher</td>
<td class="org-left">≈ <span id="g754">  بیتُ المُدرّسِ      </span></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">An office director's car</td>
<td class="org-left">≈ a car (of) a director (of) an office</td>
<td class="org-left">≈ <span id="g754">  سیّارةُ مُدیرِ مكتبٍ   </span></td>
</tr>

<tr>
<td class="org-left">The office director's car</td>
<td class="org-left">≈ the car (of) the director (of) the office</td>
<td class="org-left">≈ <span id="g754">  سیّارةُ مُدیرِ المكتبِ    </span></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">Allah's mercy</td>
<td class="org-left">≈ the mercy (of) Allah</td>
<td class="org-left">≈ <span id="g754">  رَحْمَةُ ٱللَّهِ             </span></td>
</tr>

<tr>
<td class="org-left">the people of the book</td>
<td class="org-left">⟦Quranic reference to people who obtained revelation⟧</td>
<td class="org-left">≈ <span id="g754">  اَّهْلُ ٱلْكِتَابِ        </span></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">my brother's watch</td>
<td class="org-left">≈ the watch (of) my brother</td>
<td class="org-left">≈ <span id="g754">   سَاعَةُ أخِي           </span></td>
</tr>

<tr>
<td class="org-left">my mum's aunt</td>
<td class="org-left">≈ the aunt (of) my mother</td>
<td class="org-left">≈ <span id="g754">   خَالَةُ أُمِّي           </span></td>
</tr>
</tbody>
</table>



<p>
🤷 Personally I'm not sure when proper names should have the case markings. So any
guidance regarding the following examples would be welcomed! Inshallah I will
keep learning and eventually figure this out. 🤷
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Mohammed's car</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">≈ سَيَّارَةُ مُحَمَّد</td>
</tr>

<tr>
<td class="org-left">Ali's table</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">≈ طَاوِلَةُ عَلِي</td>
</tr>

<tr>
<td class="org-left">Sarah's job</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">≈ وَظِيْفَةُ سَارَة</td>
</tr>

<tr>
<td class="org-left">Yemen's map</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">≈ خَرِيْطَةُ اليَمَن</td>
</tr>
</tbody>
</table>


</details>

<hr />
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         “This book!” Demonstratives make definites!
                      </font>
                    </strong>
                  </summary>

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


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

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">“this/these”</td>
<td class="org-left">“that/those”</td>
</tr>

<tr>
<td class="org-left">masculine singular</td>
<td class="org-left">هٰذا     (<i>haathaa</i>)</td>
<td class="org-left">ذٰلِكَ  (<i>thaalika</i>)</td>
</tr>

<tr>
<td class="org-left">feminine singular</td>
<td class="org-left">هٰذِهِ     (<i>haathihi</i>)</td>
<td class="org-left">تِلْكَ  (<i>tilka</i>)</td>
</tr>

<tr>
<td class="org-left">plural</td>
<td class="org-left">هؤَلاءِ    (<i>haa'ulaa'i</i>)</td>
<td class="org-left">أُولئكَ (<i>'ulaa'ika</i>)</td>
</tr>
</tbody>
</table>

<p>
Often non-verbal sentences are formed using demonstratives:
</p>

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


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">This <i>is a</i> book.</td>
<td class="org-left">.هذا كتاب</td>
</tr>

<tr>
<td class="org-left">This <i>is</i> my sister.</td>
<td class="org-left">.هذهِ أُخْتي</td>
</tr>

<tr>
<td class="org-left">That <i>is</i> my mother.</td>
<td class="org-left">.تِلْكَ أُمّي</td>
</tr>
</tbody>
</table>

<p>
As always, definitness in an Idafa distinguishes between a complete sentence and an adjectival phrase:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">This <i>is a</i> book.</td>
<td class="org-left">.هذا كتاب</td>
</tr>

<tr>
<td class="org-left">This book …</td>
<td class="org-left">… هذا الكتاب</td>
</tr>

<tr>
<td class="org-left">(This book is heavy.</td>
<td class="org-left">.هذا الكتاب ثقیل )</td>
</tr>

<tr>
<td class="org-left">This <i>is</i> <span class="underline">the</span> book.</td>
<td class="org-left">.هذا هو الكتاب</td>
</tr>
</tbody>
</table>

<p>
The first is a sentence, the second is not.  Finally, notice that you need to add the appropriate noun, in this case هو,
if you want to say the sentence <i>This is <span class="underline">the</span> book.</i>
(This is the <i>Pronoun of Separation</i>, discussed below.)
</p>


</details>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         The Pronoun of Separation
                      </font>
                    </strong>
                  </summary>

<p>
Above we declared
</p>
<div class="org-center">
<p>
<b>Non-verbal Sentences      ≈    <span style="color:red;">indefinite description</span> ⇷ <span style="color:green;">defininite noun</span></b>
</p>
</div>
<p>
However, we can have definite predicates in a sentence such as <i>God is the truth</i>. To separate a definite predicate from a definite subject, a <span class="underline">third person</span> <abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronoun</abbr> (known as <i>thamir al-fasl</i>, the
pronoun of separation) is inserted between subject and predicate.
</p>
<div class="org-center">
<p>
<b>Non-verbal Sentences      ≈    <span style="color:red;">definite description</span> ⇷ <span style="color:blue;">pronoun</span> ⇷ <span style="color:green;">defininite noun</span></b>
</p>
</div>

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


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">The unbelievers are wrongdoers.</td>
<td class="org-left">.اَلْكٰفِرُون ظٰلِمُون</td>
</tr>

<tr>
<td class="org-left">The wrongdoing unbelievers …</td>
<td class="org-left">… اَلْكٰفِرُون ٱلظّٰلِمُن</td>
</tr>

<tr>
<td class="org-left">The unbelievers are <i>the</i> wrongdoers.</td>
<td class="org-left">.اَلْكٰفِرُون هُمُ ٱلظّٰلِمُونَ</td>
</tr>
</tbody>
</table>

<p>
Here is another sentence:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-center">.اَللّٰهُ هُوَ ٱلتَّوَّابُ</td>
</tr>

<tr>
<td class="org-center">≈ (Allah) (He) (the relenting).</td>
</tr>

<tr>
<td class="org-center">≈ Allah is the Relenting one.</td>
</tr>
</tbody>
</table>


</details>
</div>
</div>

<div id="outline-container-Sentences-without-verbs-Replacing-a-noun-with-an-adjective" class="outline-3">
<h3 id="Sentences-without-verbs-Replacing-a-noun-with-an-adjective"><span class="section-number-3">4.2.</span> <a href="#Sentences-without-verbs-Replacing-a-noun-with-an-adjective">Sentences without verbs: Replacing a noun with an adjective</a></h3>
<div class="outline-text-3" id="text-Sentences-without-verbs-Replacing-a-noun-with-an-adjective">
<p>
The Idafa construction is about two nouns next to each other; however Arabic has only 3 kinds of words &#x2014;in contrast to
English's 8.
</p>

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


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

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Kind</th>
<th scope="col" class="org-left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">اسم</td>
<td class="org-left">Nouns, adjectives, adverbs, etc</td>
</tr>

<tr>
<td class="org-left">فعل</td>
<td class="org-left">Verbs: action words</td>
</tr>

<tr>
<td class="org-left">حرف</td>
<td class="org-left">Particles, such as prepositions في and علی</td>
</tr>
</tbody>
</table>

<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         What's an Adjective? Some common adjectives
                      </font>
                    </strong>
                  </summary>

<p>
Descriptive words such as “beautiful, new, heavy” are known
as <i>adjectives</i> in English.
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">&#xa0;</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">beautiful</td>
<td class="org-left">جميل</td>
<td class="org-left">jamiil</td>
</tr>

<tr>
<td class="org-left">ugly</td>
<td class="org-left">قبيح</td>
<td class="org-left">GabeH</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">new</td>
<td class="org-left">جديد</td>
<td class="org-left">jaded</td>
</tr>

<tr>
<td class="org-left">old</td>
<td class="org-left">قديم</td>
<td class="org-left">Gadeem</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">heavy</td>
<td class="org-left">ثغيل</td>
<td class="org-left">thaGeel</td>
</tr>

<tr>
<td class="org-left">light</td>
<td class="org-left">خفيف</td>
<td class="org-left">khafeef</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">big / large</td>
<td class="org-left">كبير</td>
<td class="org-left">kabeer</td>
</tr>

<tr>
<td class="org-left">small</td>
<td class="org-left">صغير</td>
<td class="org-left">sagheer</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">tall / long</td>
<td class="org-left">طويل</td>
<td class="org-left">Taweel</td>
</tr>

<tr>
<td class="org-left">short</td>
<td class="org-left">قصير</td>
<td class="org-left">Gaseer</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">broken</td>
<td class="org-left">مكسور</td>
<td class="org-left">maksuur</td>
</tr>

<tr>
<td class="org-left">happy</td>
<td class="org-left">مسرور</td>
<td class="org-left">masruur</td>
</tr>

<tr>
<td class="org-left">famous</td>
<td class="org-left">مشهور</td>
<td class="org-left">mashHur</td>
</tr>

<tr>
<td class="org-left">married</td>
<td class="org-left">متزوج</td>
<td class="org-left">mitazawwij</td>
</tr>

<tr>
<td class="org-left">suitable</td>
<td class="org-left">موناسب</td>
<td class="org-left">munasib</td>
</tr>
</tbody>
</table>


</details>

<p>
So, what kind of meaning do we get if we replace one of the nouns
in an Idafa construction with an adjective, a descriptive word?
<b>We get sentences!</b>
</p>
<div class="org-center">
<p>
<b>Descriptive Phrases      ≈    <span style="color:red;">description</span> ⇷ <span style="color:green;">noun</span></b>
</p>
</div>

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


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

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-right">1.</td>
<td class="org-left">(a) beautiful girl</td>
<td class="org-left">بنت جميلة</td>
</tr>

<tr>
<td class="org-right">2.</td>
<td class="org-left">(a) beautiful river</td>
<td class="org-left">نهر جميل</td>
</tr>

<tr>
<td class="org-right">3.</td>
<td class="org-left">a beautiful river</td>
<td class="org-left">نهرٌ جميلٌ</td>
</tr>

<tr>
<td class="org-right">4.</td>
<td class="org-left">the beautiful river</td>
<td class="org-left">النهر الجميل</td>
</tr>

<tr>
<td class="org-right">5.</td>
<td class="org-left">the beautiful river</td>
<td class="org-left">النهرُ الجميلُ</td>
</tr>

<tr>
<td class="org-right">6.</td>
<td class="org-left">their beautiful river</td>
<td class="org-left">نهرهم الجميل</td>
</tr>
</tbody>
</table>

<p>
Notice that the description جميل changes according to what is being described: The first has an extra ة since it's
describing a female, the third (and fifth) has markings that match the markings of what's being described, the fourth (and sixth!)
is definite since it's describing something definite.
</p>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Adjective Agreement</h3>
<p>
Adjectives are placed after the noun they describe, and agree with
the noun in gender, definiteness, number, and case endings.
(<i>Number</i>, or <i>plurality</i>, is the last thing covered in this article.)
</p>

</div>

<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         What if I have multiple adjectives?
                      </font>
                    </strong>
                  </summary>

<p>
Just place them after the noun, as usual, and seperate them with <i>and</i> وَ “wa”. Here's two examples, one definite and the
other indefinite.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">a large new school</td>
<td class="org-left">مدرسة كبيرة وَجديدة</td>
</tr>

<tr>
<td class="org-left">the beautiful old chair</td>
<td class="org-left">الكرسي الجميل وَالقديم</td>
</tr>
</tbody>
</table>

<p>
In the English <b>sentence</b> <i>Allah is powerful and mighty.</i>, it is necessary to link the adjectives by using <i>and</i>. This is
not necessary in Arabic &#x2014;even though وَ could be used&#x2014;, especially when tanween is fully pronounced. For example:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">.اَللّٰهُ قَوِيٌّ عَزِیزٌ</td>
</tr>

<tr>
<td class="org-left">Allah is powerful and mighty.</td>
</tr>
</tbody>
</table>


</details>

<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         An Exception: Colours as adjectives
                      </font>
                    </strong>
                  </summary>

<p>
The (masculine) colours are as follows:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">red</td>
<td class="org-left">أحمَر</td>
<td class="org-left">ahmar</td>
</tr>

<tr>
<td class="org-left">blue</td>
<td class="org-left">أزرَق</td>
<td class="org-left">azraG</td>
</tr>

<tr>
<td class="org-left">green</td>
<td class="org-left">أخضَر</td>
<td class="org-left">akhthar</td>
</tr>

<tr>
<td class="org-left">yellow</td>
<td class="org-left">أصفَر</td>
<td class="org-left">asfar</td>
</tr>

<tr>
<td class="org-left">black</td>
<td class="org-left">أسوَد</td>
<td class="org-left">aswad</td>
</tr>

<tr>
<td class="org-left">white</td>
<td class="org-left">أبيَض</td>
<td class="org-left">abyath</td>
</tr>
</tbody>
</table>

<ol class="org-ol">
<li>Notice that all colours start with أ and have a Fatha ـَـ on the next-to-last letter.</li>
<li>The feminine versions of colours are formed by pushing the أ to the end، dropping the ء to the floor, and bringing the
Fatha to the first letter.
<ul class="org-ul">
<li>For example, masculine أحمَر has corresponding feminine
حَمراء.
Likewise, we have زَرقاء ، خَضراء ، صَفراء ، سَوداء ، بَيضاء .</li>
</ul></li>
<li><p>
Even though adjectives must agree with their nouns in case endings, colours are an exception: They always have the ـُـ
ending, (for both definite <i>and</i> indefinite).
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">a beautiful pen</td>
<td class="org-left">قلمٌ جميلٌ</td>
<td class="org-left">Galam-<i>un</i> jameel-<i>un</i></td>
</tr>

<tr>
<td class="org-left">a red pen</td>
<td class="org-left">قلمٌ أحمرُ</td>
<td class="org-left">Galam-<i>un</i> ahmar-<i>u</i></td>
</tr>

<tr>
<td class="org-left">the red pen</td>
<td class="org-left">القلمُ الأحمرُ</td>
<td class="org-left">al-Galam-<i>u</i> al-ahmar-<i>u</i></td>
</tr>
</tbody>
</table></li>
</ol>


</details>

<p>
The rule about agreement in definiteness is crucial, because a definite noun followed by an indefinite adjective is a
complete sentence, not requiring a verb.  That is, mixing definiteness results in <i>sentences</i>, complete thoughts.
</p>
<div class="org-center">
<p>
<b>Non-verbal Sentences      ≈    <span style="color:red;">indefinite description</span> ⇷ <span style="color:green;">defininite noun</span></b>
</p>
</div>
<p>
For example,
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">The river <i>is</i> beautiful.</td>
<td class="org-left">≈</td>
<td class="org-left">.النهر جميل</td>
</tr>

<tr>
<td class="org-left">The river <i>is</i> beautiful.</td>
<td class="org-left">≈</td>
<td class="org-left">.النهرُ جميلٌ</td>
</tr>

<tr>
<td class="org-left">Allah is might.</td>
<td class="org-left">≈</td>
<td class="org-left">.اَللّٰهُ عَزِیزٌ</td>
</tr>
</tbody>
</table>

<p>
Again, since Arabic's word classes put adjectives and nouns in the same group, اسم, we can replace the adjective with a
noun. For example,
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Yusuf <i>is</i> beautiful.</td>
<td class="org-left">≈</td>
<td class="org-left">.يوسف جميل</td>
</tr>

<tr>
<td class="org-left">Yusuf <i>is a</i> teacher.</td>
<td class="org-left">≈</td>
<td class="org-left">.يوسفُ مُدَرّسٌ</td>
</tr>
</tbody>
</table>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Equational Sentences: Do It Yourself Examples!
                      </font>
                    </strong>
                  </summary>
<p>
These kind of no-verb sentences are also known as <i>equational sentences</i>: For example, <i>You are Muhammad</i> is written أنتَ
مهمد and “could be thought of as” <i>you = Muhammad</i> &#x2014;an equation!  Likewise, <i>Muhammad = student</i> is another equation,
which is written مهمد طالب.
</p>

<hr />

<p>
<b>Exercise 1:</b> (1) pick any <abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronoun</abbr> you like, (2) any name you like, then (3) stick them beside each other
to make more example sentences!
</p>

<p>
Here are a few to get you started!
</p>
<ul class="org-ul">
<li>هم مُدرّسون  &#x2014; <i>They are teachers.</i></li>
<li>انا سمير &#x2014; <i>I am Samer.</i></li>
<li>انا سميرة &#x2014; <i>I am Samera</i>.</li>
</ul>


<hr />

<p>
<b>Exercise 2:</b>
(1) Pick any name you like, (2) pick any job/adjective you like, then (3)
stick them beside each other to make more example sentences!
</p>

<p>
Here are a few to get you started!
</p>
<ul class="org-ul">
<li>زينب كاتبة &#x2014; <i>Zaynab is a writer.</i></li>
<li>سارة طويلة &#x2014; <i>Sarah is tall.</i></li>
<li>بنيامين مسرور &#x2014; <i>Benjamin is happy.</i></li>
</ul>


</details>

<p>
Notice how cool that is! Arabic let's us create sentences without an equivalent for <i>am, is, are</i> &#x2014;the subject is just
followed by the rest of the sentence. Moreover, notice that since <b>subjects must have the nominative ending ـُـ/ـٌـ, the
rest of the sentence matches in case</b> &#x2014;this is the same rule of matching for adjectives! (For this reason, non-verbal
sentences are also called <i>nominal sentences</i>.)
</p>

<p>
Here are some more examples:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">I <i>am</i> busy today.</td>
<td class="org-left">.انا مَشْغولٌ الیوم</td>
</tr>

<tr>
<td class="org-left">The window <i>is</i> broken.</td>
<td class="org-left">.الشُّباكُ مَكْسورٌ</td>
</tr>
</tbody>
</table>

<p>
Notice that pronouns, such as انا, do not get case markings.
And we use the indefinite ـٌـ marking for adjectives.
</p>

<p>
<b>Exercise:</b> Write the correct case markings!
</p>
<style> #g755 {color: orange; background-color:orange;}
       #g755:hover {color: black; background-color:white;} </style>
<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">Sentence</th>
<th scope="col" class="org-left">&#xa0;</th>
<th scope="col" class="org-left">Translation</th>
<th scope="col" class="org-left">Correct Case Markings</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">الطالب جديد</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">The student is new.</td>
<td class="org-left">الطالبُ جديدٌ</td>
</tr>

<tr>
<td class="org-left">الكتاب جديد</td>
<td class="org-left">&#xa0;</td>
<td class="org-left"><span id="g755"> The book is new.         </span></td>
<td class="org-left"><span id="g755">    الكتابُ جديدٌ            </span></td>
</tr>

<tr>
<td class="org-left">الطالب جميل</td>
<td class="org-left">&#xa0;</td>
<td class="org-left"><span id="g755"> The student is beautiful. </span></td>
<td class="org-left"><span id="g755">    الطالبُ جميلٌ           </span></td>
</tr>

<tr>
<td class="org-left">المدير طالب</td>
<td class="org-left">&#xa0;</td>
<td class="org-left"><span id="g755"> The manager is a student. </span></td>
<td class="org-left"><span id="g755">  المُديرُ طالبٌ  </span></td>
</tr>

<tr>
<td class="org-left">انتَ مُدير</td>
<td class="org-left">&#xa0;</td>
<td class="org-left"><span id="g755"> You are a manager.       </span></td>
<td class="org-left"><span id="g755">    انتَ مُديرٌ               </span></td>
</tr>

<tr>
<td class="org-left">انا المُدرس</td>
<td class="org-left">&#xa0;</td>
<td class="org-left"><span id="g755"> I am the teacher.        </span></td>
<td class="org-left"><span id="g755">   انا المُدرسُ             </span></td>
</tr>
</tbody>
</table>



<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Questions: Stick a “question marker” in front of a sentence! Also اعرب affecting writing!
                      </font>
                    </strong>
                  </summary>
<p>
English makes sentences <i>into</i> question by moving words around:
The sentence <i>You are a student.</i> becomes the question
<i>Are you a student?</i>
</p>

<p>
Arabic just adds هَلْ or اَ to a non-verbal sentence to turn it into a question:
For example, <i>You are a student</i> is انتَ طالب, and we turn it into a question as follows.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-center">Are you a student?</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-center">هَلْ انتَ طالب؟</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-center">اانتَ طالب؟</td>
</tr>
</tbody>
</table>

<p>
Here are more question words:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">where</td>
<td class="org-left">أینَ</td>
<td class="org-left">ayna</td>
</tr>

<tr>
<td class="org-left">who</td>
<td class="org-left">مَنْ</td>
<td class="org-left">men (like <i>gentlemen</i>)</td>
</tr>

<tr>
<td class="org-left">what <i>+ noun</i></td>
<td class="org-left">ما</td>
<td class="org-left">ma</td>
</tr>

<tr>
<td class="org-left">what <i>+ verb</i></td>
<td class="org-left">ماذا</td>
<td class="org-left">matha</td>
</tr>

<tr>
<td class="org-left">why</td>
<td class="org-left">لِماذا</td>
<td class="org-left">limatha</td>
</tr>

<tr>
<td class="org-left">when</td>
<td class="org-left">مَتی</td>
<td class="org-left">mataa</td>
</tr>

<tr>
<td class="org-left">which</td>
<td class="org-left">أيّ</td>
<td class="org-left">ayy</td>
</tr>

<tr>
<td class="org-left">how</td>
<td class="org-left">كَیفَ</td>
<td class="org-left">kayfa</td>
</tr>

<tr>
<td class="org-left">how many</td>
<td class="org-left">كَم</td>
<td class="org-left">kam</td>
</tr>

<tr>
<td class="org-left">how much (<i>price!</i>)</td>
<td class="org-left">بِكم</td>
<td class="org-left">bikam</td>
</tr>
</tbody>
</table>

<p>
There are three important things to note here:
</p>

<dl class="org-dl">
<dt>ما/ماذا</dt><dd><p>
There are 2 words for <i>what</i>, one is used for a following noun and the other for a
following verb.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">What's your address?</td>
<td class="org-left">ما عنوانك؟</td>
</tr>

<tr>
<td class="org-left">What are you doing?</td>
<td class="org-left">ماذا تفعل؟</td>
</tr>
</tbody>
</table>

<p>
Also,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">What's this?</td>
<td class="org-left">ما هٰذا؟</td>
</tr>
</tbody>
</table></dd>
</dl>


<dl class="org-dl">
<dt>كم</dt><dd><p>
كم is followed by an indefinite <i>singular</i> noun that must be in the accusative case; i.e., it ends with ـًـ if the word
ends with ة/ی/اء and otherwise ends with اً.
</p>

<p>
This is another example of Arabic case endings, اعراب, affecting the basic spelling
and pronunciation.
</p>

<p>
For example, كم ولداَ؟ <i>How many boys?</i>
</p></dd>

<dt>من</dt><dd><p>
The question word <i>who</i> مَنْ  “men” (as in “gentlemen”) can be easily confused
with the preposition <i>from</i> مِنْ. “min” (as in “minimum”). Unless the vowels are written, it's the context that
distinguishes them apart.
</p>

<p>
For example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-center" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-center">Where are you from?</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-center">From where are you?</td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-center">مِنْ أَیْنَ انتَ؟</td>
</tr>
</tbody>
</table>

<p>
Likewise, من انت؟ only means <i>Who are you?</i> (“men anta”),
since the phrase <i>From you?</i> would be written with
an attached <abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronoun</abbr> as مِنْك؟ “min-ak”.
</p></dd>
</dl>


</details>
</div>
</div>

<div class="outline-text-2" id="text-Idafa-In-definite-followed-by-definite">
<hr>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Laysa: Not-to-be
                      </font>
                    </strong>
                  </summary>
<p>
If you want to make a nominal sentence negative, you need to use the special verb Laysa.
</p>

<p>
While Arabic doesn't use a verb “to be” (<i>is/am/are</i>) in simple non-verbal sentences,
it does have a verb “to <i>not</i> be”!
</p>

<p>
We make a sentence, such as <i>Haani is a doctor</i> هاني طبیب, negative by adding
لَیْسَ (and concluding the sentence in the accusative case)
or by adding لِیْسَ…بِـ (and concluding in the genitive case).
</p>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Haani isn't a doctor.</td>
</tr>

<tr>
<td class="org-left">. لِیْسَ هاني طبيباً</td>
</tr>

<tr>
<td class="org-left">. لِيْسَ هاني بِطبيب</td>
</tr>
</tbody>
</table>

<p>
لِيْسَ is unusual because it looks like a past verb, but always has a present meaning:
<i>Haani <span class="underline">wasn't</span> a doctor</i> would be لم یكن هاني طبیباً.
</p>

<p>
However, لِیْسَ does change according to the subject:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">singular</td>
<td class="org-left">plural</td>
</tr>

<tr>
<td class="org-left">1</td>
<td class="org-left">لَسْتُ     <i>I'm not</i></td>
<td class="org-left">لَسْنا     <i>we're not</i></td>
</tr>

<tr>
<td class="org-left">2m</td>
<td class="org-left">لَسْتَ     <i>you're not</i></td>
<td class="org-left">لَسْتُمْ     <i>you're not</i></td>
</tr>

<tr>
<td class="org-left">2f</td>
<td class="org-left">لَسْتِ     <i>you're not</i></td>
<td class="org-left">لَسْتُمْ     <i>you're not</i></td>
</tr>

<tr>
<td class="org-left">3m</td>
<td class="org-left">لَیْسَ     <i>he's not</i></td>
<td class="org-left">لَیْسوا     <i>they're not</i></td>
</tr>

<tr>
<td class="org-left">3f</td>
<td class="org-left">لَیْسَتْ    <i>she's not</i></td>
<td class="org-left">لَسْنَ     <i>they're not</i></td>
</tr>
</tbody>
</table>

<p>
Just as in elementary school, we memorised multiplication times in math class;
when learning a new language there are various conjugation tables that must simply
be memorised.
</p>

<p>
Here's a final example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">. لَستُ بِمُدرّس</td>
</tr>

<tr>
<td class="org-left">. لَستُ مُدرساً</td>
</tr>

<tr>
<td class="org-left">I'm not a teacher.</td>
</tr>
</tbody>
</table>


</details>
</div>

<div id="outline-container-Describing-Possession" class="outline-3">
<h3 id="Describing-Possession"><span class="section-number-3">4.3.</span> <a href="#Describing-Possession">Describing Possession</a></h3>
<div class="outline-text-3" id="text-Describing-Possession">
<p>
Adjectives, descriptive words, come at the end of an Idafa &#x2014;even if they describe the first word in an Idafa.
</p>

<p>
The adjective will match the gender of the noun it is describing,
  and will have الـ if the noun is definite. For example, the presence of ة below is what decides which noun of the
  Idafa is being described.
</p>

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


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">the town's beautiful river</td>
<td class="org-left">نهر المدينة الجميل</td>
</tr>

<tr>
<td class="org-left">the beautiful town's river</td>
<td class="org-left">نهر المدينة الجميلة</td>
</tr>
</tbody>
</table>

<hr />

<p>
Here's a puzzler for you! What does the following sentence mean?
</p>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">   شباك البيت السغير</td>
</tr>

<tr>
<td class="org-left">≈?  the window of the <i>small</i> house</td>
</tr>

<tr>
<td class="org-left">≈?  the <i>small</i> window of the house</td>
</tr>
</tbody>
</table>

<p>
Answer&#x2026;
</p>

<style> #g756 {color: orange; background-color:orange;}
       #g756:hover {color: black; background-color:white;} </style>
<p>
<span id="g756"> It's not clear! Such ambiguities also exist in English! For example, “the boy touched the girl with the flower”: Does this mean the boy used a flower to touch the girl, or does it mean the boy touched the specific girl who had a flower with her. </span>
</p>



<p>
However&#x2026; Arabic has markings, or اعراب which literally means
“to make clear, eloquent”. As such, if we use markings, we can remove the ambiguity.
</p>
<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Adjective Agreement</h3>
<p>
Adjectives are placed after the noun they describe, and agree with
the noun in gender, definiteness, and case endings.
</p>

</div>

<p>
The <i>second</i> noun in an Idafa (and any subsequent nouns) will have
the genetive case ending: Either ـِـ if definite, or ـٍـ if indefinite.
(The case of the first noun will vary depending on the role it plays within the sentence.)
</p>

<p>
As such, we have:
</p>

<div style="column-rule-style: none nil;column-count: 2;">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">  شباك البيتِ السغيرِ</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">≈  the window of the <i>small</i> house</td>
</tr>
</tbody>
</table>


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


<colgroup>
<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">شباكُ البيتِ السغيرُ</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">≈  the <i>small</i> window of the house</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</div>

<div id="outline-container-Plurals-Seeing-إعراب-in-the-main-script" class="outline-2">
<h2 id="Plurals-Seeing-إعراب-in-the-main-script"><span class="section-number-2">5.</span> <a href="#Plurals-Seeing-إعراب-in-the-main-script">Plurals: <i>Seeing إعراب in the main script!</i></a></h2>
<div class="outline-text-2" id="text-Plurals-Seeing-إعراب-in-the-main-script">
<p>
In English to talk about <i>many</i> instance of a “house” or a “mouse” we use the words “houses” and “mice”. In Arabic, one
has to generally learn the plural when learning a word. However, there are two kinds of words that we just add an ending
to form the plural.
</p>
</div>

<div id="outline-container-Sound-Feminine-Plurals" class="outline-3">
<h3 id="Sound-Feminine-Plurals"><span class="section-number-3">5.1.</span> <a href="#Sound-Feminine-Plurals">Sound Feminine Plurals</a></h3>
<div class="outline-text-3" id="text-Sound-Feminine-Plurals">
<p>
For groups of females, or (female or male) <i>non-human</i> nouns, we form the plural by adding ـَـات at the end of a word &#x2014;which is essentially just <i>expanding</i> any existing ـَـة.
</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>
<tbody>
<tr>
<td class="org-left"><i>(female)</i></td>
<td class="org-left">teacher</td>
<td class="org-left">مُدرّسة</td>
<td class="org-left">mudarrisa</td>
</tr>

<tr>
<td class="org-left"><i>(female)</i></td>
<td class="org-left">teachers</td>
<td class="org-left">مُدرّسَات</td>
<td class="org-left">mudarrisaat</td>
</tr>

<tr>
<td class="org-left"><i>(male)</i></td>
<td class="org-left">animal</td>
<td class="org-left">حيوان</td>
<td class="org-left">Haywaan</td>
</tr>

<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">animals</td>
<td class="org-left">حيوانَات</td>
<td class="org-left">Haywaanaat</td>
</tr>
</tbody>
</table>

<p>
Notice that the Arabic word for <i>animals</i> is grammatically feminine.
In-general, the plurals of all non-humans are treated grammatically as <i>feminine singular</i> 🤯 As such, for example,
descriptive words are singular for <i>animals</i>, but plural for <i>teachers</i>.
</p>
<div style="column-rule-style: none nil;column-count: 2;">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">The <i>(female)</i> teachers are beautiful.</td>
</tr>

<tr>
<td class="org-left">≈ المُدرّسَات جميلَات</td>
</tr>
</tbody>
</table>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">The animals are beautiful.</td>
</tr>

<tr>
<td class="org-left">≈ الحيوانَات جميلة</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>


<div id="outline-container-Sound-Masculine-Plural-SMP" class="outline-3">
<h3 id="Sound-Masculine-Plural-SMP"><span class="section-number-3">5.2.</span> <a href="#Sound-Masculine-Plural-SMP">Sound Masculine Plural (SMP)</a></h3>
<div class="outline-text-3" id="text-Sound-Masculine-Plural-SMP">
<p>
For groups of males, or groups of mixed males &amp; females, we form the plural by adding ـُـونَ at the end of a word when the
word is doing the action (i.e., it's in the <i>nomiative</i> case) and otherwise we add ـِـينَ.
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">teacher</th>
<th scope="col" class="org-left">مُدرّس</th>
<th scope="col" class="org-left">mudarris</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">The teacher is here.</td>
<td class="org-left">المُدرّسُ هُنا</td>
<td class="org-left">al-mudarris-u huna</td>
</tr>

<tr>
<td class="org-left">The teachers are here.</td>
<td class="org-left">المُدرّسُونَ هُنا</td>
<td class="org-left">al-mudarris-uuna huna</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">I saw the teacher.</td>
<td class="org-left">رأیتُ ٱلمُدرّسِ</td>
<td class="org-left">r'aytu al-mudrarris-i</td>
</tr>

<tr>
<td class="org-left">I saw the teachers.</td>
<td class="org-left">رأيتُ المُدرّسِينَ</td>
<td class="org-left">r'aytu al-mudarris-iina</td>
</tr>
</tbody>
</table>


<p>
Notice that the Irab in the singular are stretched out in the plural! Super cool stuff!
</p>
<ul class="org-ul">
<li>We see this often in the Quran, where God talks about
مسلمون (Muslims doing something) and
مسلمين (Muslims having something done to them, or owning something).</li>
</ul>


<p>
The sound masculine plural is one of the few instances of the
case ending being written as part of the main script and universally pronounced.
</p>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Here's another somewhat common one!
                      </font>
                    </strong>
                  </summary>

<p>
A few nouns have long final vowels when they are the first element in an Idafa.
</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">&#xa0;</th>
<th scope="col" class="org-left">nomative</th>
<th scope="col" class="org-left">accusative</th>
<th scope="col" class="org-left">genitive</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">father اب</td>
<td class="org-left">أَبُو</td>
<td class="org-left">أَبَا</td>
<td class="org-left">أَبِي</td>
</tr>

<tr>
<td class="org-left">brother اخ</td>
<td class="org-left">أَخُو</td>
<td class="org-left">أَخَا</td>
<td class="org-left">أَخِي</td>
</tr>
</tbody>
</table>



</details>


<p>
Since this plural explicitly indicates a case, either nomative
with  ـُـونَ and otherwise with ـِـينَ;
but the <i>second</i> noun (and any subsequent nouns) in an Idafa
must be in the genetive case. As such, in an Idafa whose <i>final</i> word is a
sound masculine plural, the ـِـينَ ending is always used.
Moreover, when this plural is the <i>first</i> word in an Idafa,
it loses the shared ending ـنَ.
For example,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">The boy's teachers are here.</td>
<td class="org-left">مُدرّسو الولدِ هُنا</td>
</tr>

<tr>
<td class="org-left">I saw the boy's teachers.</td>
<td class="org-left">رأيتُ مُدرّسي الولدِ</td>
</tr>
</tbody>
</table>

<br>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Tell me more about why we lose ـنَ at the start of an Idafa
                      </font>
                    </strong>
                  </summary>
<p>
In an Idafa, the first noun is definite (even when it does not start with الـ): In <i>X's Y</i>, we know that <i>Y</i> belongs to <i>X</i>,
and so it's not some arbitrary unknown <i>Y</i>. As such, the first noun in an Idafa can only have the defininte case endings ـَ
ـِ ـُ and not the indefinite ones ـَ ـٍ ـٌ . It is for this reason that the final ـنَ ending of the SMP must be dropped when a
SMP is the first noun in an Idafa.
</p>


<p>
Here's some more examples:
</p>
<div style="column-rule-style: none nil;column-count: 2;">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">أُولُو ٱلْأَلبَابِ</td>
</tr>

<tr>
<td class="org-left">≈ <i>(literally!)</i> possessors of hearts</td>
</tr>

<tr>
<td class="org-left">≈ men of understanding</td>
</tr>
</tbody>
</table>


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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">أُولِي ٱلْأَبْصَارِ</td>
</tr>

<tr>
<td class="org-left">≈ <i>(literally!)</i> possessors of sight</td>
</tr>

<tr>
<td class="org-left">≈ men of insight</td>
</tr>
</tbody>
</table>

</div>

<p>
Another example is <i>Children of Israel</i> placed in an Idafa to get
<i>banu israil</i> in the nomiative, and <i>banii israil</i> in the genitive.
</p>



</details>

<p>
So, the way sound masculine plurals are written is due to the إعراب rules. We started with I'rab and ended with it;
we've come full circle 😊
</p>
</div>
</div>


<div class="outline-text-2" id="text-Plurals-Seeing-إعراب-in-the-main-script">
<hr>
<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Bonus: The Dual Also Shows-off إعراب in the main script!
                      </font>
                    </strong>
                  </summary>

<p>
Arabic has three notions of <i>number</i>:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Singular</td>
<td class="org-left">When talking about <i>one</i> thing,</td>
</tr>

<tr>
<td class="org-left">Dual</td>
<td class="org-left">When talking about <i>two</i> things,</td>
</tr>

<tr>
<td class="org-left">Plural</td>
<td class="org-left">When talking about <i>three or more</i> things.</td>
</tr>
</tbody>
</table>

<p>
The dual is used for both masculine and feminine,
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">you (two)</td>
<td class="org-left">أَنْتُما</td>
</tr>

<tr>
<td class="org-left">they (two)</td>
<td class="org-left">هُما</td>
</tr>
</tbody>
</table>

<p>
<b><span style="color:green;">If you want to refer to two people or things (/nouns/), you add the dual ending ـانِ (“aani”) in the normative case and ـَینِ (“ayni”) in the accusative & genitive cases.</span></b>
</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>
<tbody>
<tr>
<td class="org-left">book كتاب</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">two books</td>
<td class="org-left">كتابانِ</td>
</tr>

<tr>
<td class="org-left">city مدینة</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">two cities</td>
<td class="org-left">مدينتانِ</td>
</tr>
</tbody>
</table>

<p>
The dual ending is also added to adjectives:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">هُناكَ مُمَرّضَتانِ جدیدتانِ في المُسْتشْفَی</td>
</tr>

<tr>
<td class="org-left">There are two new nurses in the hospital.</td>
</tr>
</tbody>
</table>


</details>
</div>
]]></description>
  <category><![CDATA[arabic]]></category>
  <link>https://alhassy.github.io/arabic-word-order.html</link>
  <guid>https://alhassy.github.io/arabic-word-order.html</guid>
  <pubDate>Thu, 03 Nov 2022 00:00:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[Arabic Roots: The Power of Patterns]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-arabic.html"><img src="https://img.shields.io/badge/-arabic-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-javascript.html"><img src="https://img.shields.io/badge/-javascript-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-emacs.html"><img src="https://img.shields.io/badge/-emacs-grey?logo=nil"></a></center>
<center><a href="https://unsplash.com/photos/Ejdemp9O7Po" class="tooltip" title="Image credit “https://unsplash.com/photos/Ejdemp9O7Po”"><img src="https://source.unsplash.com/Ejdemp9O7Po/350x350" alt="Article image" style="border: 2px solid black;" width="350" height="350" align="top"/></a></center>

<div class="abstract" style="border: 1px solid black;padding: 10px; margin-top: 50px; margin-bottom: 50px;margin-right: 150px; margin-left: 80px; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>

<p>
I want to quickly introduce the Arabic language, through its “root system” &#x2014;i.e., most words have 3-letters at their
core&#x2014; and how these roots can be placed in “patterns” to obtain new words.
</p>

<p>
I'd like to take a glance at Arabic's Verb Forms: These give you 10 words for each root!
</p>

<p>
Some <b><span style="color:green;">interesting</span></b> concepts will also be mentioned, for those curious, but should be ignored on a first
reading. These will be hidden away in <i>clickable/foldable</i> regions.
</p>

<p>
These are notes of things that I'm learning; there's likely errors.
</p>


</div>



<div id="outline-container-The-Arabic-Root-System" class="outline-2">
<h2 id="The-Arabic-Root-System"><span class="section-number-2">1.</span> <a href="#The-Arabic-Root-System">The Arabic Root System</a></h2>
<div class="outline-text-2" id="text-The-Arabic-Root-System">
<p>
Most Arabic words are derived from a three-letter <abbr class="tooltip" title="The Arabic language is based on “roots” that link words of related meanings.<br><br>An Arabic “root” is the sequence of (usually 3) Arabic letters that carry the underlying meaning of a word, for example<br>ش−ر−ب “to drink” and ح−م−ل “to carry”.<br><br>Vowels and consonants are added around the roots to create related words.<br><em>Roots are the building blocks of the Arabic language and are helpful for guessing the meaning of vocabulary.</em><br><br>Generally foreign loan words, such as <em>internet</em> انترنت, fall outside the root system.">root</abbr> &#x2014;notable exceptions are words like “and” وَ
and “on” علی.  A root, or مصدر “masdar”, refers to the core meaning of a word.  Simply put, roots are identified by
ignoring all non-vowel letters, resulting in usually 3 letters &#x2014;and, rarely, 4 letters.  <i>Roots are the building
blocks of the Arabic language and are helpful for guessing the meaning of vocabulary.</i>
</p>

<p>
For example, the sequence of letters س−ف−ر (read right-to-left as s-f-r) carries the meaning of “travel”.
Any word containing these letters, in this order, likely has something to do with travel. For example:
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">English</th>
<th scope="col" class="org-left">Arabic</th>
<th scope="col" class="org-left">Transliteration</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">journey</td>
<td class="org-left">سفر</td>
<td class="org-left">safar</td>
</tr>

<tr>
<td class="org-left">he travels</td>
<td class="org-left">يسافر</td>
<td class="org-left">yusafir</td>
</tr>

<tr>
<td class="org-left">amabassdor</td>
<td class="org-left">سفير</td>
<td class="org-left">safeer</td>
</tr>

<tr>
<td class="org-left">traveller</td>
<td class="org-left">مسافر</td>
<td class="org-left">musafir</td>
</tr>

<tr>
<td class="org-left">embassy</td>
<td class="org-left">سفارة</td>
<td class="org-left">sifara</td>
</tr>
</tbody>
</table>

<p>
All of these words are derived from the root س−ف−ر, s-f-r, in this order.
<i>Much of Arabic grammar is concerned with how the root is manipulated to create different related meanings:
Additional letters, or vowels, modify the meaning according to different general patterns.</i>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png" class="tooltip" title="Yes"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png" height=50></a>
</p>

<p>
Likewise, ك−ت−ب is a root regarding “writing”, from which we can obtain numerous words:
</p>
<center><a href="http://oerabic.llc.ed.ac.uk/"><img src="http://roots.oerabic.llc.ed.ac.uk/wp-content/uploads/sites/8/2020/02/1a1.png" height=500></a></center>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Can't we simply just stick the roots together?
                      </font>
                    </strong>
                  </summary>
<p>
<b>No.</b>
</p>

<p>
For example the letters ح−م−ل have the meaning of “to carry” but naively connecting the letters gives us حمل, which,
without any context, could be read as
</p>
<ul class="org-ul">
<li>حَمَلَ “he carried”, or</li>
<li>حُمِلْ “he was carried”!
<ul class="org-ul">
<li>(<abbr class="tooltip" title="Arabs <em>infer</em> vowels from context, otherwise words alone such as حمل are ambigious: It could mean حَمَلَ “he carried” or حُمِلْ<br>“he was carried”.<br><br>As an example sentence with vowels written, Prophet Muhammad is known to have said:<br>| أنَا مَدِينَةُ الْعِلْمِ وَعَلَيٌ بَابُهَا&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br>| <em>I am the city of knowledge and Ali is its gate</em> |<br><br>Incidentally, Ali was the one who commissioned the system of vowels.<br>https://blogs.transparent.com/arabic/the-beginning-of-dotting-and-diacritics-in-arabic/<br><br><hr><br>Arabic has only three short vowels, or حركات (literally: “movements”), which are written as small symbols above/below<br>letters.<br><br>| Vowel name&emsp;| Vowel sound | Arabic | English example |<br>| Fatha;&emsp;فتحة&emsp;| <em>a</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـَ&emsp;&emsp;&emsp; | <em>mat</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br>| Dhamma; ظمّة&emsp;| <em>u</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـُ&emsp;&emsp;&emsp; | <em>sugar</em>&emsp;&emsp;&emsp;&emsp;&emsp; |<br>| Kasra; كسرة&emsp;| <em>i</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـِ&emsp;&emsp;&emsp; | <em>bit</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br><br>The “no vowel” marker is suukun/سكون: While هههه has its vowels guessed to be هَهَهَهَ “hahahah”, we obtain “hhhh” by using<br>sukkun, هْهْهْهْ. It is important for consonant-vowel-consonant syllables, such as بَابْ “bab” which means <em>door.</em><br><br>Incidentally, when a sound needs to be repeated twice, it is usually written once with a <em>Shadda</em> ـّـ to indicate the<br>doubling.&emsp;For example, فَهِمَ <em>fahima</em> “he understood” but فَهَّمَ <em>fahhama</em> “he explained”. Shadda is used with الـ + ‘sun<br>letters’. Unlike the other short vowels, the Shadda is usually written even in informal Arabic, to avoid ambiguity.<br><br><br>Arabic has 3 long vowels, which are formed using specific letters <em>after</em> the short vowels:<br> | Long vowel&emsp;sound | Arabic | English example |<br> | <em>aa</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـَا&emsp;&emsp;&emsp;| <em>far</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br> | <em>ii</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـِي&emsp;&emsp;&emsp;| <em>meet</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br> | <em>uu</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـُو&emsp;&emsp;&emsp;| <em>boot</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br><br>Since short vowels are normally not written, letters ا ي و play two roles: They behave as long vowels <em>aa,ii,uu</em> (when<br>preceded by short vowels) and also behave as consonant sounds <em>a,y,w</em>.<br> + For example, as a consonant, ي (https://arabic.fi/letters/74) makes an English “y” sound; but as a long vowel it makes an “ii” sound.<br> + Occasionally, <em>aa</em> is written using ی (which is like ي but without the dots), or یٰ, rather than an<br>&emsp; <em>alif</em>. This always happens at the end of a word and is called <em>alif maqsuura</em><br>&emsp; “broken alif”; for example علی “on” and موسیٰ “Musa”.<br><br>The following video reads all Arabic letters, where each letter is vowelised by one of the 3 short vowels. It's a really<br>nice video: https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6.">Vowel signs ـْ ـَ ـِ ـُ</abbr> are discussed below.)</li>
</ul></li>
</ul>

<p>
Both of these words are specific to
</p>
<ol class="org-ol">
<li>a male person,</li>
<li>in the past, and</li>
<li>it's not clear whether the person is doing the carrying or the one being carried.</li>
</ol>

<p>
Thus حمل is far more specific than the general meaning of ح−م−ل “to carry” &#x2014;which is not itself a word, but an abstract
sequence of letters.
</p>



</details>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Irregular Roots
                      </font>
                    </strong>
                  </summary>
<p>
Irregular roots do not consist of 3 different consonants; instead, they fall into three categories:
</p>

<dl class="org-dl">
<dt>Doubled roots</dt><dd><p>
where the second and third root letters are the
same.
</p>

<p>
− If a doubled verb is put into a form involving a sukkun ـْـ
  on the 3rd root, then the 2nd and 3rd letters are written
  separately; otherwise, the 2nd and 3rd letters are written together
  with a shadda ـّـ.
</p>

<p>
− For example, ر−د−د “to reply” becomes رَدَّ “he replied” when placed in the
  masculine-second-person-past-tense form فَعَلَ, but becomes رَدَدْتُ “I replied” when placed in the
  first-person-past-tense form فَعَلْتُ. <span style="color:red;">More on forms below!</span>
</p>

<ul class="org-ul">
<li>In doubt: Doubled roots are usually written together.</li>
</ul></dd>

<dt>Weak roots</dt><dd><p>
where one of the three root letters is و or ي; for example ق−و−ل “to speak”.
</p>
<ul class="org-ul">
<li>These letters are “so weak” that they change from the constant sounds و/w and ي/y to vowel
sounds or disappear entirely, depending on the pattern the root is placed in.</li>
</ul>
<p>
− If و‌/ي is the first root, then it almost always drops out in the present tense.
  For example, و−ص−ل “to arrive” becomes أَصِلُ “I arrive” in the first-person-present-tense
  أَفْعُلُ form. Contrast this with the regular root ك−ت−ب “to write” becoming أَكْتُبُ “I write”.
</p></dd>

<dt>Hamzated roots</dt><dd>where one of the root letters is hamza ء; for example ق−ر−ء “to read”.</dd>
</dl>


</details>
</div>

<div id="outline-container-Meanings-of-roots" class="outline-3">
<h3 id="Meanings-of-roots"><span class="section-number-3">1.1.</span> <a href="#Meanings-of-roots">Meanings of roots</a>&#xa0;&#xa0;&#xa0;<span class="tag"><span class="Interactive">Interactive</span></span></h3>
<div class="outline-text-3" id="text-Meanings-of-roots">
<p>
<a href="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png" class="tooltip" title="Let's take a break"><img src="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png" height=50></a>
Enter 3 letters to get a link to Arabic words that are derived from that root:
</p>
<p hidden> See: https://alhassy.github.io/AngularJSCheatSheet/ </p>
<center ng-app> <!-- ⟨0⟩ AngularJS is active for this div -->
    <!-- ⟨1⟩ Actually load AngularJS -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"> </script>

      <!-- ⟨2⟩ The *value* of these text boxes are named “first, second, third” -->
      <input type="text" maxlength="1" size="1" ng-model="third" ng-init="third ='ب'">
      <input type="text" maxlength="1" size="1" ng-model="second" ng-init="second ='ت'">
      <input type="text" maxlength="1" size="1" ng-model="first" ng-init="first ='ك'">
      ⇨
      <!-- ⟨3⟩ Actually use the “first, second, third” values here, *whenever* they are updated! -->
      <a href="https://en.wiktionary.org/wiki/{{first}}_{{second}}_{{third}}#Derived_terms">Derived words from {{first}}-{{second}}-{{third}}</a>
</center>

<p>
Alternatively, <a href="https://arabic.fi/roots/67-42-40">this link</a> provides more information, but has less roots.
</p>
</div>
</div>
</div>
<div id="outline-container-Arabic-has-112-symbols-and-112-sounds" class="outline-2">
<h2 id="Arabic-has-112-symbols-and-112-sounds"><span class="section-number-2">2.</span> <a href="#Arabic-has-112-symbols-and-112-sounds">Arabic has 112 symbols and 112 sounds</a></h2>
<div class="outline-text-2" id="text-Arabic-has-112-symbols-and-112-sounds">
<p>
<i>What are the vowels that can be added to roots to make new words?</i>
</p>

<p>
Arabic has 28 letters, and like cursive English, it is written with letters connected.
</p>

<p>
Each letter has 4 forms: The isolated form, the form where the letter starts a word, the form where the letter is in the
middle of a word, and the form where a letter is at the end of a word. So, Arabic has 28 * 4 = 112 distinct shapes for
its alphabet.  (Since some letters do not connect forwards, the isolated form actually does appear in written text.  For
example, او means “or” and it consists of two isolated letters.)  For example, the letter <i>ha</i> has the following forms:
</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">isolated</th>
<th scope="col" class="org-left">initial</th>
<th scope="col" class="org-left">medial</th>
<th scope="col" class="org-left">final</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">ه</td>
<td class="org-left">هـ</td>
<td class="org-left">ـهـ</td>
<td class="org-left">ـه</td>
</tr>
</tbody>
</table>

<p>
If a friend texts you something funny, you reply with ههههه −−− “hahaha”.
</p>

<p>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png" class="tooltip" title="I have a question"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png" height=50></a>
But, where are the short vowels “a”? <b>Arabic short vowels are generally not written!</b>
</p>

<p>
There are only three short vowels in Arabic: <i>a</i>, <i>i</i> and <i>u</i>.
They are denoted by small symbols above/below letters, for example:
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Vowel</th>
<th scope="col" class="org-left">Example</th>
<th scope="col" class="org-left">English reading</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">ـَـ</td>
<td class="org-left">هَهَهَهَ</td>
<td class="org-left">hahahaha</td>
</tr>

<tr>
<td class="org-left">ـُـ</td>
<td class="org-left">هُهُهُهُ</td>
<td class="org-left">huhuhuhu</td>
</tr>

<tr>
<td class="org-left">ـِـ</td>
<td class="org-left">هِهِهِهِ</td>
<td class="org-left">hihihihi</td>
</tr>
</tbody>
</table>

<p>
Incidentally, the sound “h” is obtained by using the “no vowel” marker: هْـ.  So with the 3 short vowels and the fourth
symbol to indicate the absence of a vowel, there are a total of 4 * 28 = 112 sounds in Arabic.
</p>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Tell me more about Arabic Vowels!
                      </font>
                    </strong>
                  </summary>
<p>
Arabs <i>infer</i> vowels from context, otherwise words alone such as حمل are ambigious: It could mean حَمَلَ “he carried” or حُمِلْ
“he was carried”.
</p>

<p>
An example sentence with vowels written:
</p>

<div id="org23f41fa" class="figure">
<p><img src="../images/arabic-irab.png" alt="arabic-irab.png" width="90%" height="200px" />
</p>
</div>

<hr />
<p>
Arabic has only three short vowels, or حركات (literally: “movements”), which are written as small symbols above/below
letters.
</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">Vowel name</th>
<th scope="col" class="org-left">Vowel sound</th>
<th scope="col" class="org-left">Arabic</th>
<th scope="col" class="org-left">English example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Fatha / فتحة</td>
<td class="org-left"><i>a</i></td>
<td class="org-left">ـَ</td>
<td class="org-left"><i>mat</i></td>
</tr>

<tr>
<td class="org-left">Dhamma / ظمّة</td>
<td class="org-left"><i>u</i></td>
<td class="org-left">ـُ</td>
<td class="org-left"><i>sugar</i></td>
</tr>

<tr>
<td class="org-left">Kasra / كسرة</td>
<td class="org-left"><i>i</i></td>
<td class="org-left">ـِ</td>
<td class="org-left"><i>bit</i></td>
</tr>
</tbody>
</table>

<p>
The “no vowel” marker is suukun/سكون: While هههه has its vowels guessed to be هَهَهَهَ “hahahah”, we obtain “hhhh” by using
sukkun, هْهْهْهْ.
</p>

<p>
Arabic has 3 long vowels, which are formed using specific letters <i>after</i> the short vowels:
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Long vowel  sound</th>
<th scope="col" class="org-left">Arabic</th>
<th scope="col" class="org-left">English example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><i>aa</i></td>
<td class="org-left">ـَا</td>
<td class="org-left"><i>far</i></td>
</tr>

<tr>
<td class="org-left"><i>ii</i></td>
<td class="org-left">ـِي</td>
<td class="org-left"><i>meet</i></td>
</tr>

<tr>
<td class="org-left"><i>uu</i></td>
<td class="org-left">ـُو</td>
<td class="org-left"><i>boot</i></td>
</tr>
</tbody>
</table>

<p>
Since short vowels are normally not written, letters ا ي و play two roles: They behave as long vowels <i>aa,ii,uu</i> (when
preceded by short vowels) and also behave as consonant sounds <i>a,y,w</i>.
</p>
<ul class="org-ul">
<li>For example, as a consonant, <a href="https://arabic.fi/letters/74">ي</a> makes an English “y” sound; but as a long vowel it makes an “ii” sound.</li>
<li>Occasionally, <i>aa</i> is written using ی (which is like ي but without the dots), or یٰ, rather than an
<i>alif</i>. This always happens at the end of a word and is called <i>alif maqsuura</i>
“broken alif”; for example علی “on” and موسیٰ “Musa”.</li>
</ul>

<p>
The following video reads all Arabic letters, where each letter is vowelised by one of the 3 short vowels. It's a really
nice video: <a href="https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6">https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6</a>.
</p>


</details>

<p>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-23.png" class="tooltip" title="Disagree"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-23.png" height=50></a>
<i>Of-course, there is more to the story!</i> There is the “glottal stop”, Hamza ـٔ , and other special characters and symbols
above/below letters.  So the counts of 112 are not exact.  For example, some letters, like alif ا, have the same shape
for different forms, but sometimes it can be written as ی (such as علی “on”) یٰ (such as موسیٰ “Musa”).
</p>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         The Arabic Hamza ـٔ is like the English Apostrophe ـ'
                      </font>
                    </strong>
                  </summary>

<ol class="org-ol">
<li>In both cases there is uncertaininty as to when and how to use it, even among native speakers.</li>
<li>Whereas in English we ask ourselves: <i>Should the apostrophe come before the “s” or after the “s”?</i>, in Arabic the
question becomes: <i>Which letter should carry the hamza?</i>.</li>
<li>The hamza itself is considered a consonant, not a vowel, pronounced as a short pause.</li>
<li>Like the apostrophe, the rules for hamza are more concerned with where to place it than how to pronounce it.</li>
<li>General rules:
<ul class="org-ul">
<li>At the start of a word, hamza is written on an alif: أ</li>
<li>This might result in two alifs side-by-side, if so then merge them
into <i>alif madda</i> آ, which is read as a long <i>aa</i> sound.</li>
<li>Otherwise, the letter carrying the hamza tends to relate to the vowel <i>before</i> the hamza:
If we have ـُـ ، ـِـ ، ـَـ before the hamza, then the hamza is written ؤ ، یٔ/ـٔـ ، أ respectively.
<ul class="org-ul">
<li>If we have ـْـ before the hamza, we write ؤ ، یٔ/ـٔـ ، أ
depending on the vowel the hamza root should be taking.
For example, س−ء−ل “to ask” becomes يسْأَل “he asks” in the
masculine-second-person-present-tense (يَفْعَلُ form, for this
particular root).</li>
</ul></li>
</ul></li>
</ol>


</details>

<p>
The following video reads all Arabic letters, where each letter is vowelised by one of the 3 short vowels. It's a really
nice video.
</p>
<center><iframe width="560" height="315" src="https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></center>
</div>
</div>
<div id="outline-container-ف-ع-ل-The-template-for-any-3-core-root-letters" class="outline-2">
<h2 id="ف-ع-ل-The-template-for-any-3-core-root-letters"><span class="section-number-2">3.</span> <a href="#ف-ع-ل-The-template-for-any-3-core-root-letters">ف−ع−ل : The template for any 3 core root letters</a></h2>
<div class="outline-text-2" id="text-ف-ع-ل-The-template-for-any-3-core-root-letters">
<p>
As a symbol to represent the three root letters of any word, Arabic grammar uses the roots of the prototypical verb فعل
“to do”, read <i>fa'al</i>.
</p>

<p>
For example, the root ك−ت−ب is associated with “writing”.  The word for “office” مَكْتَب is the مَفْعَل-pattern: The root
letters have مَـ before them, a sukkun ـْـ over the first root letter, and a fatha ـَـ over the second root letter. In the
same way, “books” كُتُب is the فُعُل-pattern.
</p>


<p>
<a href="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png" class="tooltip" title="Let's take a break"><img src="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png" height=50></a>
Below are some example patterns.  <i>If you are faced with an Arabic word that you have never heard before, you can guess
the meaning by its root and pattern.</i>
</p>
</div>

<div id="outline-container-The-فَعَّال-pattern-the-person-whose-job-is-X" class="outline-3">
<h3 id="The-فَعَّال-pattern-the-person-whose-job-is-X"><span class="section-number-3">3.1.</span> <a href="#The-فَعَّال-pattern-the-person-whose-job-is-X">The فَعَّال-pattern: “the person whose job is X”</a></h3>
<div class="outline-text-3" id="text-The-فَعَّال-pattern-the-person-whose-job-is-X">
<p>
This pattern gives the profession associated with a core root. Here's some examples:
</p>

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


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

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Profession</th>
<th scope="col" class="org-left">Core meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">كَتَّاب</td>
<td class="org-left">ك−ت−ب</td>
</tr>

<tr>
<td class="org-left">Scribe</td>
<td class="org-left">to write</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">فَنَّان</td>
<td class="org-left">ف−ن−ن</td>
</tr>

<tr>
<td class="org-left">Artist</td>
<td class="org-left">to be artistic</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">خَبَّاز</td>
<td class="org-left">خ−ب−ز</td>
</tr>

<tr>
<td class="org-left">Baker</td>
<td class="org-left">to bake</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">عَطَّار</td>
<td class="org-left">ع−ط−ر</td>
</tr>

<tr>
<td class="org-left">Perfume vendor</td>
<td class="org-left">to perfume</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">رَكَّاض</td>
<td class="org-left">ر−ك−ظ</td>
</tr>

<tr>
<td class="org-left">Runner</td>
<td class="org-left">to run</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">جَرَّاح</td>
<td class="org-left">ج−ر−ح</td>
</tr>

<tr>
<td class="org-left">Surgeon</td>
<td class="org-left">to cut</td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-The-مَفعَل-pattern-the-place-where-X-is-done" class="outline-3">
<h3 id="The-مَفعَل-pattern-the-place-where-X-is-done"><span class="section-number-3">3.2.</span> <a href="#The-مَفعَل-pattern-the-place-where-X-is-done">The مَفعَل-pattern: “the place where X is done”</a></h3>
<div class="outline-text-3" id="text-The-مَفعَل-pattern-the-place-where-X-is-done">
<p>
This pattern gives the place associated with a core root. Here's some examples:
</p>

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


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

<col  class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Place</th>
<th scope="col" class="org-left">Core meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">مَسكَن</td>
<td class="org-left">س−ك−ن</td>
</tr>

<tr>
<td class="org-left">home</td>
<td class="org-left">to live</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">مَكتَب</td>
<td class="org-left">ك−ت−ب</td>
</tr>

<tr>
<td class="org-left">office</td>
<td class="org-left">to write</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">مَدخَل</td>
<td class="org-left">د−خ−ل</td>
</tr>

<tr>
<td class="org-left">entrance</td>
<td class="org-left">to enter</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">مَخبَز</td>
<td class="org-left">خ−ب−ز</td>
</tr>

<tr>
<td class="org-left">bakery</td>
<td class="org-left">to bake</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">مَعبَر</td>
<td class="org-left">ع−ب−ر</td>
</tr>

<tr>
<td class="org-left">crossing point</td>
<td class="org-left">to cross</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">مَسبَح</td>
<td class="org-left">س−ب−ح</td>
</tr>

<tr>
<td class="org-left">swimming pool</td>
<td class="org-left">to swim</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

<div id="outline-container-org7d59307" class="outline-2">
<h2 id="org7d59307"><span class="section-number-2">4.</span> <a href="#org7d59307">Verb Forms: The True Power of Arabic's Form System</a></h2>
<div class="outline-text-2" id="text-4">
<p>
The richness of Arabic is based on its system of word roots, and nowhere is this more evident than in the verb system. <a href="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-22.png" class="tooltip" title="Agree"><img src="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-22.png" height=50></a>
</p>

<p>
In English we can add extra letters to form different but connected meanings &#x2014;for example: <i>value, revalue, validate</i>.
Arabic takes this principle much farther with many different patterns that add meaning to the origninal root form.
These <i>derived</i> forms are the major way in which Arabic achieves its richness of vocabulary. For example, from ق−ت−ل “to
kill”, we can obtain
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">he killed</td>
<td class="org-left">قتل</td>
<td class="org-left">qatala</td>
</tr>

<tr>
<td class="org-left">he massacred (“killed intensely”)</td>
<td class="org-left">قتّل</td>
<td class="org-left">qattala</td>
</tr>

<tr>
<td class="org-left">he battled (“tried to kill”)</td>
<td class="org-left">قاتل</td>
<td class="org-left">qaatala</td>
</tr>

<tr>
<td class="org-left">they fought each other</td>
<td class="org-left">تقاتلوا</td>
<td class="org-left">taqaataluu</td>
</tr>
</tbody>
</table>

<p>
Here are the significant verb forms. For simplicitly, I'm presenting them in the <i>past tense</i> using the root ف−ع−ل “to
do”.
</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" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Form</th>
<th scope="col" class="org-left">Common Meanings</th>
<th scope="col" class="org-left">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">1.  <a href="https://arabic.tripod.com/Verbs01.htm#:~:text=or%20has%20done.-,1)%20Fa%22al(a),-The%20first%20structure">فَعَلَ</a></td>
<td class="org-left">“doing an action X”; (this is the most basic form)</td>
<td class="org-left">كَتَبَ “he wrote” from ك−ت−ب “to write”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">2.  <a href="https://arabic.tripod.com/VerbForms1.htm#:~:text=Form%20II%20of%20Arabic%20Verbs">فَعَّلَ</a></td>
<td class="org-left">“doing X to another”; “making another do X”</td>
<td class="org-left">خَرّجَ “he made someone go-out/graduate” from خ−ر−ج “to go out”</td>
</tr>

<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">“doing X intensely/repeatedly”</td>
<td class="org-left">كَسَّرَ “he smashed” from ك−س−ر “to break”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">3.  <a href="https://arabic.tripod.com/VerbForms1.htm#:~:text=Form%20III%20of%20Arabic%20Verbs">فَاعَلَ</a></td>
<td class="org-left">“doing X with someone else”</td>
<td class="org-left">جَالَسَ “he sat with (someone)” from ج−ل−س “to sit”</td>
</tr>

<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">“trying to do X”</td>
<td class="org-left">سَابَقَ “he raced” from س−ب−ق “to come before”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">4.  <a href="https://arabic.tripod.com/VerbForms2.htm#:~:text=Form%20IV%20of%20Arabic%20Verbs">أَفْعَلَ</a></td>
<td class="org-left"><abbr class="tooltip" title="A “transitive” verb is a verb that requires an object to express a complete thought, otherwise it is “intransitive”.<br>Some verbs are both transitive and intransitive.<br><br>A “transitive” verb needs to <em>transfer its action</em> to something or someone ---the object.<br>In essence, transitive means “to affect something else.”<br><br>For example, “Please bring coffee.” would not be a complete thought without the object “coffee”.<br>That is, “Please bring.” is an incomplete thought: What or whom should we bring? As such, “bring” is a transitive verb.<br>In contrast, “Please sing.” is a complete thought, and so “sing” is an intransitive verb ---actually, it's also transitive.<br><br>In Arabic, the Form-4 أفْعَلَ pattern turns intransitive verbs into transitive ones; and turns transitive verbs into<br>doubly-transitive verbs ---which means it takes two objects: E.g., “I gave the boy the ball”, here “gave” is<br>doubly-transitive. E.g., in Form-4, ر−س−ل “to send” gives the transitive verb أرْسَلَ which means it can be followed by two<br>objects: أرْسَلَ الولد لكتاب “The boy sent the book”.">Transitive</abbr> meaning: “doing X to another”; like Form-2</td>
<td class="org-left">أَسْخَنَ “he heated (something)” from س−خ−ن “to be hot”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">5.  <a href="https://arabic.tripod.com/VerbForms4.htm#:~:text=Form%20V%20of%20Arabic%20Verbs">تَفَعَّلَ</a></td>
<td class="org-left">“doing X to yourself”; this is Form-2 + تَـ</td>
<td class="org-left">تَذَكَّرَ “he remembered” from ذ−ك−ر “to remind”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">6.  <a href="https://arabic.tripod.com/VerbForms4.htm#:~:text=Form%20VI%20of%20Arabic%20Verbs">تَفَاعَلَ</a></td>
<td class="org-left">“doing X together (as a group)”; this is Form-3 + تَـ</td>
<td class="org-left">تَعَاوَنَ “he cooperated” from ع−و−ن “to help”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">7.  <a href="https://arabic.tripod.com/VerbForms2.htm#:~:text=Form%20VII%20of%20Arabic%20Verbs">اِنْفَعَلَ</a></td>
<td class="org-left"><abbr class="tooltip" title="A “passive” verb is one where the subject undergoes the action of the verb rather than carries out the action, for<br>example حُملت “she was carried” and يُستخدم “it is used”.">Passive</abbr> meaning: “to be X-ed”. This is Form-1 + اِنْـ</td>
<td class="org-left">اِنْحَمَلَ “he was carried” from ح−م−ل “to carry”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">8.  <a href="https://arabic.tripod.com/VerbForms3.htm">اِفْتَعَلَ</a></td>
<td class="org-left">No consistent meaning;  “to make yourself do X”</td>
<td class="org-left">اِفْتَعَلَ “he incited” from ف−ع−ل “to do”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">9.  <a href="https://arabic.tripod.com/VerbForms5.htm#:~:text=Form%20IX%20of%20Arabic%20Verbs">اِفْعَلَّ</a></td>
<td class="org-left">‌used for changing colours: “to turn colour X”</td>
<td class="org-left">اِحْمَرَّ “he blushed / turned-red” from أحمر “red”</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left">10.  <a href="https://arabic.tripod.com/VerbForms5.htm#:~:text=Form%20X%20of%20Arabic%20Verbs">اِسْتَفْعَلَ</a></td>
<td class="org-left">“asking for X”; this is nearly Form-1 + اِسْتَـ</td>
<td class="org-left">اِسْتَعْلَمَ “he inquired” from ع−ل−م “to know”</td>
</tr>

<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">“to consider or find something to have quality X”</td>
<td class="org-left">اِسْتَحْسَنَ “he admired” from ح−س−ن “to be beautiful”</td>
</tr>
</tbody>
</table>

<ul class="org-ul">
<li><b>Exercise!</b> Place the roots ع−م−ل into all of these patterns, except form-9; then guess their meanings!  ( <a href="https://en.wiktionary.org/wiki/%D8%B9_%D9%85_%D9%84#Derived_terms">Solution</a> )</li>
<li><a href="https://www.almaany.com/en/dict/ar-en/%D8%AC%D9%8E%D8%A7%D9%84%D9%8E%D8%B3%D9%8E">AlManny.com</a> is an excellent online dictionary to finding out the meanings of words when placing them in these forms.</li>
</ul>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         All the derived forms do not exist for all roots, but most roots have at least one or two forms in general circulation.
                      </font>
                    </strong>
                  </summary>

<ol class="org-ol">
<li>You'll need to look in a dictionary, or the above root-meaning tool, to know exactly which forms exist.</li>

<li>There are an additional 5 forms, but they are super rare in usage.</li>

<li>In addition, Arabic speakers will sometimes make up new verbs from existing roots, either as a joke or in an
effort to be creative or <i>✨poetic💐</i>.</li>
</ol>


</details>
</div>
</div>
<div id="outline-container-useful-resource-https-arabic-fi" class="outline-2">
<h2 id="useful-resource-https-arabic-fi"><span class="section-number-2">5.</span> <a href="#useful-resource-https-arabic-fi">Closing &amp; Useful Resources</a></h2>
<div class="outline-text-2" id="text-useful-resource-https-arabic-fi">
<p>
I've often seen introductions to Arabic mention the power of roots &amp; patterns, but one usually has to work through a
host of fundamental topics before actually seeing some of these patterns.
</p>

<p>
I've written this brief introduction so that one can actually see some of these patterns in action.
</p>

<p>
It's been a lot of fun &#x2014;I had to learn a lot more than I thought I knew to make this happen.
<i>It seems writing about things forces you to understand them better!</i>
</p>

<p>
Anyhow, I'm going to keep writing about Arabic since it seems fun and I'd like to have a way to quickly review my notes
on what I'm learning.
</p>
</div>

<div id="outline-container-org472d672" class="outline-3">
<h3 id="org472d672"><span class="section-number-3">5.1.</span> <a href="#org472d672">Resources</a></h3>
<div class="outline-text-3" id="text-5-1">
<ul class="org-ul">
<li><p>
<a href="https://www.amazon.ca/Mastering-Arabic-Grammar-Mahmoud-Wightwick/dp/1403941092">Mastering Arabic Grammar</a> by Jane Wightwick &amp; Mahmoud Gaafar
</p>

<p>
Perhaps the most accessible book I've seen on Arabic grammar.
</p>

<p>
It's a small book, whose chapters are also small/focused and digestible.
</p>

<p>
It assumes you're familiar with the Arabic alphabet and takes you to forming
full sentences, and reading short stories.
</p></li>

<li><p>
<a href="https://arabic.fi/">https://arabic.fi/</a>
</p>

<p>
Almost every word in every sentence and phrase on this website is
clickable, and takes you to a page with generous information about the
word, along with audio clips. It's a free, beautiful, interactive website.
</p></li>

<li><p>
<a href="http://allthearabicyouneverlearnedthefirsttimearound.com/wp-content/uploads/2014/03/All-The-Arabic-Searchable-PDF.pdf">All The Arabic You Never Learned The First Time Around (PDF)</a>
</p>

<p>
This seems like a very good book.
</p></li>

<li><p>
<a href="http://oerabic.llc.ed.ac.uk/?p=2756">OERabic</a>
</p>

<p>
OERabic is an ambitious initiative that aims to enhance the mastering of Arabic by creating bespoke creative learning
(and teaching) resources.
</p></li>
</ul>
</div>
</div>
</div>

<div id="outline-container-Arabic-Input-Setup" class="outline-2">
<h2 id="Arabic-Input-Setup"><span class="section-number-2">6.</span> <a href="#Arabic-Input-Setup">Appendix: Arabic Input Setup</a></h2>
<div class="outline-text-2" id="text-Arabic-Input-Setup">
<p>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png" class="tooltip" title="I have a question"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png" height=50></a> How was this article written?  <a href="https://www.spacemacs.org/">Emacs</a>!
</p>

<p>
On the <i>left</i> below is what I type, and on the <i>right</i> is what you see in this article (which include hover/tooltips for the
cards).
</p>

<hr />
<div style="column-rule-style: none nil;column-count: 2;">
<div style="padding: 1em;background-color: white;border-radius: 15px;font-size: 0.9em;"> <h3>Source</h3>
<div ><pre class="src src-org">[[card:I have a question]] How was this article written? green:Emacs!

card:Yes With Emacs, I type /phonetically/ (based on sounds) to get Arabic; e.g.,
typing  *musy$ alHsaIY* gives me *موسیٰ الحسائي*, my name /Musa Al-hassy/.
</pre></div>
</div>
<div style="padding: 1em;background-color: white;border-radius: 15px;font-size: 0.9em;"> <h3>Result</h3>
<p>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png" class="tooltip" title="I have a question"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png" height=50></a> How was this article written? <span style="color:green;">Emacs</span>!
</p>

<p>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png" class="tooltip" title="Yes"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png" height=50></a> With Emacs, I type <i>phonetically</i> (based on sounds) to get Arabic; e.g.,
typing  <b>musy$ alHsaIY</b> gives me <b>موسیٰ الحسائي</b>, my name <i>Musa Al-hassy</i>.
</p>

</div>
</div>
<p>
Moreover, this is how Arabic looks like within Emacs:
</p>


<div id="org691cc97" class="figure">
<p><img src="../images/arabic-irab.png" alt="arabic-irab.png" width="90%" height="200px" />
</p>
<p><span class="figure-number">Figure 1: </span>This is how Arabic looks like within Emacs. (Old Arabic did not have any of the coloured symbols; not even the dots!)</p>
</div>
<hr />

<p>
The rest of this section details my <a href="https://www.spacemacs.org/">Emacs</a> setup.
</p>
</div>

<div id="outline-container-The-look-within-Emacs" class="outline-3">
<h3 id="The-look-within-Emacs"><span class="section-number-3">6.1.</span> <a href="#The-look-within-Emacs">The <i>look</i> within Emacs</a></h3>
<div class="outline-text-3" id="text-The-look-within-Emacs">
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #a89984;">;; </span><span style="color: #a89984;">Makes all dots, hamza, diadiract marks coloured!</span>
(set-fontset-font <span style="color: #689d6a;">"fontset-default"</span> '(#x600 . #x6ff) <span style="color: #689d6a;">"Amiri Quran Colored"</span>)
</pre>
</div>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         How did I find this font?
                      </font>
                    </strong>
                  </summary>

<ol class="org-ol">
<li>Look for a font I like on  <a href="https://fonts.google.com/?subset=arabic">https://fonts.google.com/?subset=arabic</a></li>
<li><code>brew search amiri</code>
<ul class="org-ul">
<li>Look to see if there is a font associated with it</li>
</ul></li>
<li><code>brew install font-amiri</code>
<ul class="org-ul">
<li>Install the likely candidate</li>
</ul></li>
<li><code>(set-fontset-font "fontset-default" '(#x600 . #x6ff) "Amiri Quran Colored")</code>
<ul class="org-ul">
<li>Get the full name by: Emacs -&gt; Options -&gt; Set Default Font</li>
</ul></li>
</ol>


</details>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         Why even bother with this line?
                      </font>
                    </strong>
                  </summary>

<p>
I found that I personally need the above <abbr class="tooltip" title="Modify fontset NAME to use FONT-SPEC for TARGET characters.<br><br>NAME is a fontset name (a string), nil for the fontset of FRAME,<br>or t for the default fontset.<br><br>TARGET may be a single character to use FONT-SPEC for.<br><br>TARGET may be a cons (FROM . TO), where FROM and TO are characters.<br>In that case, use FONT-SPEC for all the characters in the range<br>between FROM and TO (inclusive).<br><br>TARGET may be a script symbol.&emsp;In that case, use FONT-SPEC for<br>all the characters that belong to the script.&emsp;See the variable<br>‘script-representative-chars’ for the list of known scripts.<br><br>TARGET may be a charset.&emsp;In that case, use FONT-SPEC for all<br>the characters in the charset.&emsp;See ‘list-character-sets’ and<br>‘list-charset-chars’ for the list of character sets and their<br>characters.<br><br>TARGET may be nil.&emsp;In that case, use FONT-SPEC for any character for<br>which no font-spec is specified.<br><br>FONT-SPEC may one of these:<br> <strong> A font-spec object made by the function ‘font-spec’ (which see).<br> </strong> A cons (FAMILY . REGISTRY), where FAMILY is a font family name and<br>&emsp; REGISTRY is a font registry name.&emsp;FAMILY may contain foundry<br>&emsp; name, and REGISTRY may contain encoding name.<br> <strong> A font name string.<br> </strong> nil, which explicitly specifies that there’s no font for TARGET.<br><br>Optional 4th argument FRAME is a frame, or nil for the selected frame,<br>to be considered in the case that NAME is nil.<br><br>Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC<br>to the previously set font specifications for TARGET.&emsp;If it is<br>‘prepend’, FONT-SPEC is prepended.&emsp;If it is ‘append’, FONT-SPEC is<br>appended.&emsp;By default, FONT-SPEC overrides the previous settings.<br><br>(fn NAME TARGET FONT-SPEC &optional FRAME ADD)">set-fontset-font</abbr> line, since
I was typing the phrase
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">اهلاً وسهلاً</td>
</tr>

<tr>
<td class="org-left">“Hello, and welcome”</td>
</tr>

<tr>
<td class="org-left">Literally: ‌/Be with family, and at ease/</td>
</tr>
</tbody>
</table>

<p>
Yet I could not see the Fatha Tanween, ـًـ, on the Lam-Alif لا.  This issue was only within Emacs: When I exported to
HTML via <kbd style="">C-c C-e h o</kbd> then لاً would render with the tanween.
</p>

<p>
Anyhow, here are some other fun fonts to try out.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"> <span style="color: #689d6a;">"Times New Roman"</span>    <span style="color: #a89984;">;; </span><span style="color: #a89984;">Default?</span>
 <span style="color: #689d6a;">"Libian SC"</span>          <span style="color: #a89984;">;; </span><span style="color: #a89984;">Default?</span>
 <span style="color: #689d6a;">"Noto Sans Arabic"</span>   <span style="color: #a89984;">;; </span><span style="color: #a89984;">Also good! -- brew install  font-noto-sans-arabic  --cask</span>
 <span style="color: #689d6a;">"Sana"</span>               <span style="color: #a89984;">;; </span><span style="color: #a89984;">is super fun!</span>
 <span style="color: #689d6a;">"Al Bayan"</span>
 <span style="color: #689d6a;">"Baghdad"</span>
 <span style="color: #689d6a;">"Damascus"</span>           <span style="color: #a89984;">;; </span><span style="color: #a89984;">Thin</span>
 <span style="color: #689d6a;">"Beirut"</span>             <span style="color: #a89984;">;; </span><span style="color: #a89984;">Super thick!</span>
 <span style="color: #689d6a;">"KufiStandardGK"</span>     <span style="color: #a89984;">;; </span><span style="color: #a89984;">Reasonable bold</span>
 <span style="color: #689d6a;">"Diwan Kufi"</span>         <span style="color: #a89984;">;; </span><span style="color: #a89984;">fancy, almost calligraphic</span>
 <span style="color: #689d6a;">"DecoType Naskh"</span>     <span style="color: #a89984;">;; </span><span style="color: #a89984;">Tight; looks like handwritten; does not support `___` elongations.</span>
 <span style="color: #689d6a;">"Farah"</span>              <span style="color: #a89984;">;; </span><span style="color: #a89984;">sloppy handwritten</span>
 <span style="color: #689d6a;">"Waseem"</span>             <span style="color: #a89984;">;; </span><span style="color: #a89984;">handwritten</span>
 <span style="color: #689d6a;">"Farisi"</span>             <span style="color: #a89984;">;; </span><span style="color: #a89984;">Persian-style: Super thin and on an angle</span>
 <span style="color: #689d6a;">"Noto Nastaliq Urdu"</span> <span style="color: #a89984;">;; </span><span style="color: #a89984;">Like Farisi, but a bit larger &amp; thicker</span>
 <span style="color: #689d6a;">"Noto Kufi Arabic UI"</span>
 <span style="color: #689d6a;">"Geeza Pro"</span>          <span style="color: #a89984;">;; </span><span style="color: #a89984;">nice and thick</span>
 <span style="color: #689d6a;">"DecoType Naskh"</span>
</pre>
</div>


</details>
</div>
</div>

<div id="outline-container-Actually-typing-Arabic" class="outline-3">
<h3 id="Actually-typing-Arabic"><span class="section-number-3">6.2.</span> <a href="#Actually-typing-Arabic">Actually typing Arabic</a></h3>
<div class="outline-text-3" id="text-Actually-typing-Arabic">
<p>
The <code>"arabic"</code> input method (via <code>C-\</code>, which is <abbr class="tooltip" title="Enable or disable multilingual text input method for the current buffer.<br>Only one input method can be enabled at any time in a given buffer.<br><br>The normal action is to enable an input method if none was enabled,<br>and disable the current one otherwise.&emsp;Which input method to enable<br>can be determined in various ways--either the one most recently used,<br>or the one specified by ‘default-input-method’, or as a last resort<br>by reading the name of an input method in the minibuffer.<br><br>With a prefix argument ARG, read an input method name with the minibuffer<br>and enable that one.&emsp;The default is the most recent input method specified<br>(not including the currently active input method, if any).<br><br>When called interactively, the optional argument INTERACTIVE is non-nil,<br>which marks the variable ‘default-input-method’ as set for Custom buffers.<br><br>(fn &optional ARG INTERACTIVE)">toggle-input-method</abbr>) just changes my English QWERTY keyboard into an
Arabic keyboard &#x2014;useful if one has already mastered touch typing in Arabic!
</p>

<p>
In contrast, the Perso-Arabic input method (known as <code>farsi-transliterate-banan</code>) uses a system of transliteration: ASCII
keys are phonetically mapped to Arabic letters.
</p>
<ul class="org-ul">
<li><code>C-\ farsi-transliterate-banan RET M-x describe-input-method</code> to enter this method and to learn more about it.
<ul class="org-ul">
<li>For example, <code>wrb</code> ≈ عرب and <code>alwrbYTh</code> ≈ العربية</li>
</ul></li>
<li>When you're done writing in Arabic, just press <code>C-\</code> to toggle back to English.</li>
</ul>

<details class="code-details"
                 style ="padding: 1em;
                          background-color: #e5f5e5;
                          /* background-color: pink; */
                          border-radius: 15px;
                          color: hsl(157 75%);
                          font-size: 0.9em;
                          box-shadow: 0.05em 0.1em 5px 0.01em  #00000057;">
                  <summary>
                    <strong>
                      <font face="Courier" size="3" color="green">
                         M-x describe-input-method
                      </font>
                    </strong>
                  </summary>
<pre class="example" id="orga88c630">
Input method: farsi-transliterate-banan (mode line indicator:ب)

Intuitive transliteration keyboard layout for persian/farsi.
  See http://www.persoarabic.org/PLPC/120036 for additional documentation.


KEYBOARD LAYOUT
---------------
This input method works by translating individual input characters.
Assuming that your actual keyboard has the ‘standard’ layout,
translation results in the following "virtual" keyboard layout
(the labels on the keys indicate what character will be produced
by each key, with and without holding Shift):

     +----------------------------------------------------------+
      | ۱ ! | ۲ ْ | ۳ ً | ۴ ٰ | ۵ ٪ | ۶ َ | ۷ &amp; | ۸ * | ۹ ( | ۰ ) | − ـ‎ | = + | ٔ ّ |
     +----------------------------------------------------------+
        | غ‎ ق‎ | ع‎ ء‎ | ِ ٍ | ر‎ R | ت‎ ط‎ | ی‎ ي‎ | و‎ ٓ | ی‎ ئ‎ | ُ ٌ | پ‎ P | [ { | ] } |
       +---------------------------------------------------------+
         | ا‎ آ‎ | س‎ ص‎| د‎ ٱ‎ | ف‎ إ‎ | گ‎ غ‎ | ه‎ ح‎ | ج‎ ‍ | ک‎ ك‎ | ل‎ L | ؛‎ : | ' " | \ | |
        +--------------------------------------------------------+
           | ز‎ ذ‎ | ض‎ ظ‎ | ث‎ ٕ | و‎ ؤ‎ | ب‎ B | ن‎ « | م‎ » | ، &lt; | . &gt; | ‌ ؟‎ |
          +-----------------------------------------------+
                    +-----------------------------+
                    |          space bar          |
                    +-----------------------------+

KEY SEQUENCE
------------
You can also input more characters by the following key sequences:

Th ة   kh خ   sh ش   ch چ
</pre>


</details>

<p>
Also watch <a href="https://emacsconf.org/2021/talks/bidi/">Perso-Arabic Input Methods And BIDI Aware Apps</a> (also on <a href="https://www.youtube.com/watch?v=kqIZb80OIKE&amp;ab_channel=EmacsConfandEmacshangouts">youtube</a>).
</p>

<div style="padding: 1em;background-color: #CCFFFF;border-radius: 15px;font-size: 0.9em;"> <h3>Vowel me to the moon!</h3>
<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" />

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">fatha</td>
<td class="org-left">fathaTan</td>
<td class="org-left">kasrah</td>
<td class="org-left">kasrahTan</td>
<td class="org-left">dhama</td>
<td class="org-left">dhamTan</td>
<td class="org-left">sukun</td>
<td class="org-left">hamza</td>
<td class="org-left">alif</td>
<td class="org-left">madda</td>
<td class="org-left">shadda</td>
</tr>

<tr>
<td class="org-left">  بَ</td>
<td class="org-left">بً</td>
<td class="org-left">بِ</td>
<td class="org-left">بٍ</td>
<td class="org-left">بُ</td>
<td class="org-left">بٌ</td>
<td class="org-left">بْ</td>
<td class="org-left">بٔ</td>
<td class="org-left">بٰ</td>
<td class="org-left">بٓ</td>
<td class="org-left">بّ</td>
</tr>

<tr>
<td class="org-left">  ^</td>
<td class="org-left">#</td>
<td class="org-left">e</td>
<td class="org-left">E</td>
<td class="org-left">o</td>
<td class="org-left">O</td>
<td class="org-left">@</td>
<td class="org-left">`</td>
<td class="org-left">$</td>
<td class="org-left">U</td>
<td class="org-left">~</td>
</tr>
</tbody>
</table>

</div>

<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>Arabic Tatweel: Stretching out the handwritten text for beauty!</h3>
<p>
We can obtain elongation by pressing underscore: <code>j_______w________f_______r</code> gives us جـــــــعـــــــفــــــــر
</p>
<ul class="org-ul">
<li>جعفر is read <i>Jaafar</i>; it is a <a href="https://en.wikipedia.org/wiki/Ja%CA%BDfar">popular name</a> that also means <i>small stream</i>.</li>
</ul>

</div>
</div>
</div>

<div id="outline-container-org716bffe" class="outline-3">
<h3 id="org716bffe"><span class="section-number-3">6.3.</span> <a href="#org716bffe">Typing <i>outside</i> of Emacs</a></h3>
<div class="outline-text-3" id="text-6-3">
<p>
<a href="https://www.typingclub.com/sportal/program-115.game">TypingClub ~ Arabic</a> is a fun &amp; free website to learn how to type Arabic on a standard keyboard. I highly recommend it.
</p>

<p>
Being able to type Arabic while <i>thinking</i> in Arabic, instead of thinking of sounds using the English alphabet to approximate
Arabic sounds, may be helpful in actually learning Arabic.
</p>
</div>
</div>

<div id="outline-container-org716e36f" class="outline-3">
<h3 id="org716e36f"><span class="section-number-3">6.4.</span> <a href="#org716e36f">Using Images as Emojis</a></h3>
<div class="outline-text-3" id="text-6-4">
<p>
I found some images online, from <a href="http://oerabic.llc.ed.ac.uk/?p=2756">OERabic​</a>, that I thought could be used to enhance my prose.
</p>
<ul class="org-ul">
<li>I want to treat them like emojis, as such they're  intentionally small, so that they can more-or-less be used inline
within sentences.</li>
<li>When you click on them, they take you to the actual image source.
<ul class="org-ul">
<li>This is better than me downloading the images <i>then</i> having to host them somewhere <i>then</i> linking back to the source to
credit the people who made the images.</li>
</ul></li>
<li>When you hover over them, you see the translation.</li>
</ul>

<p>
The docstring of <abbr class="tooltip" title="Make a new Org-link NAME that exports using form BODY.<br><br>Since Org links are essentially string-valued functions,<br>a function ‘org-link/NAME’ is created.<br><br>DOCSTRING is optional; it is visible with<br>&emsp; (documentation ’org-link/NAME)<br><br>BODY is a string-valued expression, that may make use of the names<br>o-label, o-description, o-backend. The final one refers to the export<br>backend, such as ’html or ’latex. The first two are obtained from uses:<br><br>&emsp; Y (name:o-label][o-description]]<br><br>In particular, the use case “name:o-label” means that o-description is nil.<br><br><hr><br><br>Example use:<br><br>&emsp; ;; In a Lisp buffer, press “C-x C-e” to load this definition<br>&emsp; (org-deflink shout (upcase (or o-description o-label)))<br><br>&emsp; ;; In an Org-buffer, press “C-c C-e h o” to see how this exports<br>&emsp; <shout: hello world!><br><br>&emsp; ;; Or using the bracket format<br>&emsp; [[shout:][hello world!]]<br>&emsp; [[shout: hello world!]]<br><br>&emsp; ;; or using the plain format<br>&emsp; shout:hello_world<br><br>Here is a more complex, involved, example that makes use of<br>‘:let’ for local declarations. For instance, “define:hello”<br>renders as the word “hello” with a tooltip defining the word; the<br>definition is obtained from the command line tool ‘wn’.<br><br>&emsp;(org-deflink define<br>&emsp;&emsp;''Define the given word using WordNet, along with synonyms and coordinate terms.''<br>&emsp;&emsp;[:let (definition (shell-command-to-string (format ''wn %s -over -synsn -coorn'' o-label)))<br>&emsp;&emsp; :help-echo definition]<br>&emsp;&emsp;(--> definition<br>&emsp;&emsp;&emsp;(s-replace-regexp ''\\\'''' ''’’'' it) ;; The presence of ‘\''’ in tooltips breaks things, so omit them.<br>&emsp;&emsp;&emsp;(s-replace-regexp ''\n'' ''<br>'' it)<br>&emsp;&emsp;&emsp;(format ''<abbr class=\''tooltip\'' title=\''%s\''>%s</abbr>'' it o-label)))<br><br>For HTML tooltips, see ‘org-ospe-html-export-preserving-whitespace’.<br><br>More generally, org-special-block-extra’s “doc” link type<br>supports, in order of precedence: User definitions, Emacs Lisp<br>documentation of functions & variables, and definitions of<br>English words. For example, “doc:existential_angst” for an entry<br>‘existential_angst’ whose associated documentation-glossary is<br>user-defined in a ‘#+documentation’ Org-block, or<br>“doc:thread-first” for the Emacs Lisp documentation of the<br>function ‘thread-first’, or “doc:user-mail-address” for the Emacs<br>Lisp documentation of the variable ‘user-mail-address’, or<br>“doc:hello” for the definition of the English word ‘hello’.<br><br>DISPLAY is a vector consisting of key-value pairs that affects how the link<br>is displayed in Emacs Org buffers. The keys are as follows.<br><br>+ :help-echo is a string-valued expression of the tooltip that should accompany<br>&emsp;the new link in Org buffers. It has access to o-format being one of ‘plain’,<br>&emsp;‘angle’, ‘bracket’ which indicates the format of the link, as shown above.<br>&emsp;It also has access to o-label and o-description.<br><br>&emsp;By default, the tooltip is the link name followed by the documentation<br>&emsp;of the link, and, finally, the HTML export of the link.<br>&emsp;That way, upon hover, users can visually see the link contents,<br>&emsp;know what/how the link exports, and actually see the HTML export.<br><br>&emsp;That is to say, for the ‘shout’ example aboce, the default display is essentially:<br>&emsp;[:help-echo (org-link/shout o-label o-description ’html)]<br><br>&emsp;You may want to add the following to your Emacs init file:<br><br>&emsp;&emsp;;; Nearly instantaneous display of tooltips.<br>&emsp;&emsp;(setq tooltip-delay 0)<br>&emsp;&emsp;;; Give user 30 seconds before tooltip automatically disappears.<br>&emsp;&emsp;(setq tooltip-hide-delay 300)<br><br>+ :face specifies how should these links be displayed within Emacs.<br>&emsp; It is a list-valued expression.<br>&emsp; As usual, it may make use of O-LABEL (but O-DESCRIPTION has value nil).<br>&emsp; Example:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; :face ’(:underline ''green'')<br><br>&emsp; See https://www.gnu.org/software/emacs/manual/html_node/elisp/Face-Attributes.html<br><br>+ [:display full] if you do not want bracket links to be<br>&emsp;folded away in Org buffers; i.e., “[[X)” does not render as just “Y”.<br><br>+ :follow is a form that is executed when you click on such links; e.g., to open<br>&emsp; another buffer, browser, or other action. It makes use of (an implicit argument) ‘o-label’.<br>&emsp; Be aware that ‘o-label’ is a string that may contain spaces; e.g., when the action is to open<br>&emsp; a URL in a browser.<br><br>&emsp; If you are in need of providing similar, related, actions on a single link<br>&emsp; then your :follow can condition on the current prefix argument via ‘o-prefix’<br>&emsp; (which is essentially ‘current-prefix-arg’).<br>&emsp; For instance, a user presses “C-u RET” on your link to do one thing<br>&emsp; but “C-u 72 RET” to do another action.<br><br>+ :keymap is an alternating list of keys and actions to be<br>&emsp;performed when those keys are pressed while point is on the link.<br>&emsp;For example:<br>&emsp;&emsp;&emsp;[:keymap (C-h (message-box ''hola''))]<br><br>&emsp;By default, C-n and C-p are for moving to next and previous occruances of the same link type.<br><br>+ :let is a list of alternating variable symbol name and value, which are then used to form<br>&emsp;a concrete ‘let*’ clause. This is useful for introducing local variables for use in the DISPLAY<br>&emsp;as well as in the CONTENTS. Such local declarations may make use of O-LABEL and O-DESCRIPTION, as usual.<br><br>(fn NAME &optional DOCSTRING DISPLAY &rest BODY)">org-deflink</abbr> has nice examples, so we quickly adapt the very first example for our needs: We look at
the label given to the link, then depending on that, we show an clickable image along with a tooltip.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp" id="org85db79d">(<span style="color: #98971a; font-weight: bold;">org-deflink</span> card
 <span style="color: #689d6a;">"Show one of 6 hardcoded phrases as a small inline image."</span>
 (<span style="color: #98971a; font-weight: bold;">-let</span> [url
        (<span style="color: #98971a; font-weight: bold;">pcase</span> o-label
          (<span style="color: #689d6a;">"Let's take a break"</span> <span style="color: #689d6a;">"https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png"</span>)
          (<span style="color: #689d6a;">"Yes"</span> <span style="color: #689d6a;">"https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png"</span>)
          (<span style="color: #689d6a;">"No"</span> <span style="color: #689d6a;">"https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-21.png"</span>)
          (<span style="color: #689d6a;">"Agree"</span> <span style="color: #689d6a;">"https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-22.png"</span>)
          (<span style="color: #689d6a;">"Disagree"</span> <span style="color: #689d6a;">"https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-23.png"</span>)
          (<span style="color: #689d6a;">"I have a question"</span> <span style="color: #689d6a;">"https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png"</span>))]
   (format <span style="color: #689d6a;">"&lt;a href=\"%s\" class=\"tooltip\" title=\"%s\"&gt;&lt;img src=\"%s\" height=50&gt;&lt;/a&gt;"</span> url o-label url)))
</pre>
</div>


<p>
Below is how I would go about actually using this new link type.  The left shows what I would write <i>within</i> Emacs, and
the right is the resulting HTML (which I also see <i>within</i> Emacs, and the Chrome Browser.)
</p>
<div style="column-rule-style: none nil;column-count: 2;">
<div style="padding: 1em;background-color: white;border-radius: 15px;font-size: 0.9em;"> <h3>Source</h3>
<div ><pre class="src src-org">[[card:Let's take a break]]
[[card:Yes]]
[[card:No]]
[[card:Agree]]
[[card:Disagree]]
[[card:I have a question]]
</pre></div>
</div>
<div style="padding: 1em;background-color: white;border-radius: 15px;font-size: 0.9em;"> <h3>Result</h3>
<p>
<a href="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png" class="tooltip" title="Let's take a break"><img src="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-19.png" height=50></a>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png" class="tooltip" title="Yes"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-20.png" height=50></a>
<a href="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-21.png" class="tooltip" title="No"><img src="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-21.png" height=50></a>
<a href="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-22.png" class="tooltip" title="Agree"><img src="https://i0.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-22.png" height=50></a>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-23.png" class="tooltip" title="Disagree"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-23.png" height=50></a>
<a href="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png" class="tooltip" title="I have a question"><img src="https://i1.wp.com/oerabic.llc.ed.ac.uk/wp-content/uploads/2020/09/Visual-Communication-Signs-IRAQI-35.png" height=50></a>
</p>

</div>
</div>
</div>
</div>
]]></description>
  <category><![CDATA[arabic]]></category>
  <category><![CDATA[javascript]]></category>
  <category><![CDATA[emacs]]></category>
  <link>https://alhassy.github.io/arabic-roots.html</link>
  <guid>https://alhassy.github.io/arabic-roots.html</guid>
  <pubDate>Wed, 02 Nov 2022 00:00:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[Glossary of Arabic Linguistic Terms]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-arabic.html"><img src="https://img.shields.io/badge/-arabic-grey?logo=nil"></a></center>
<center><a href="../images/arabic-irab.png" class="tooltip" title="Image credit “../images/arabic-irab.png”"><img src="../images/arabic-irab.png" alt="Article image" style="border: 2px solid black;" width="100%" height="100%" align="top"/></a></center>

<div class="abstract" style="border: 1px solid black;padding: 10px; margin-top: 50px; margin-bottom: 50px;margin-right: 150px; margin-left: 80px; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>
<p>
Definitions, and discussions, of jargon relating to learning the Arabic language.
</p>

</div>

<div id="outline-container-org94f5602" class="outline-2">
<h2 id="org94f5602"><span class="section-number-2">1.</span> <a href="#org94f5602">Noun</a></h2>
<div class="outline-text-2" id="text-1">
<p>
<abbr class="tooltip" title="A word naming a person, object, or idea; for example: <em>House, boy, freedom.</em><br><br>In Arabic, words are classified into 3 categories ---in contrast to English's 8.<br><br>| <strong>Kind</strong> | <strong>Description</strong>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br>| اسم&emsp; | Nouns, adjectives, adverbs, etc&emsp;&emsp;&emsp;&emsp;&emsp;|<br>| فعل&emsp; | Verbs: action words&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br>| حرف&emsp;| Particles, such as prepositions في and علی |<br><br>As such, the word “noun” when talking about Arabic will sometimes mean the more general category اسم.">noun</abbr>
</p>
<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>noun</h3>
<p>
A word naming a person, object, or idea; for example: <i>House, boy, freedom.</i>
</p>

<p>
In Arabic, words are classified into 3 categories &#x2014;in contrast to English's 8.
</p>

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


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

<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><b>Kind</b></td>
<td class="org-left"><b>Description</b></td>
</tr>

<tr>
<td class="org-left">اسم</td>
<td class="org-left">Nouns, adjectives, adverbs, etc</td>
</tr>

<tr>
<td class="org-left">فعل</td>
<td class="org-left">Verbs: action words</td>
</tr>

<tr>
<td class="org-left">حرف</td>
<td class="org-left">Particles, such as prepositions في and علی</td>
</tr>
</tbody>
</table>

<p>
As such, the word “noun” when talking about Arabic will sometimes mean the more general category اسم.
</p>

</div>
</div>
</div>

<div id="outline-container-orgb155751" class="outline-2">
<h2 id="orgb155751"><span class="section-number-2">2.</span> <a href="#orgb155751">Roots</a></h2>
<div class="outline-text-2" id="text-2">
<p>
<abbr class="tooltip" title="The Arabic language is based on “roots” that link words of related meanings.<br><br>An Arabic “root” is the sequence of (usually 3) Arabic letters that carry the underlying meaning of a word, for example<br>ش−ر−ب “to drink” and ح−م−ل “to carry”.<br><br>Vowels and consonants are added around the roots to create related words.<br><em>Roots are the building blocks of the Arabic language and are helpful for guessing the meaning of vocabulary.</em><br><br>Generally foreign loan words, such as <em>internet</em> انترنت, fall outside the root system.<br><br>For more, see www.alhassy.com/arabic-roots">root</abbr>
</p>
<div style="padding: 1em;background-color: #CCFFFF;border-radius: 15px;font-size: 0.9em;"> <h3>root</h3>

<p>
The Arabic language is based on “roots” that link words of related meanings.
</p>

<p>
An Arabic “root” is the sequence of (usually 3) Arabic letters that carry the underlying meaning of a word, for example
ش−ر−ب “to drink” and ح−م−ل “to carry”.
</p>

<p>
Vowels and consonants are added around the roots to create related words.
<i>Roots are the building blocks of the Arabic language and are helpful for guessing the meaning of vocabulary.</i>
</p>

<p>
Generally foreign loan words, such as <i>internet</i> انترنت, fall outside the root system.
</p>

<p>
For more, see www.alhassy.com/arabic-roots
</p>

</div>
</div>
</div>

<div id="outline-container-orgf1d1fb3" class="outline-2">
<h2 id="orgf1d1fb3"><span class="section-number-2">3.</span> <a href="#orgf1d1fb3">Vowels, Short &amp; Long</a></h2>
<div class="outline-text-2" id="text-3">
<p>
<abbr class="tooltip" title="Arabs <em>infer</em> vowels from context, otherwise words alone such as حمل are ambigious: It could mean حَمَلَ “he carried” or حُمِلْ<br>“he was carried”.<br><br>As an example sentence with vowels written, Prophet Muhammad is known to have said:<br>| أنَا مَدِينَةُ الْعِلْمِ وَعَلَيٌ بَابُهَا&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br>| <em>I am the city of knowledge and Ali is its gate</em> |<br><br>Incidentally, Ali was the one who commissioned the system of vowels.<br>https://blogs.transparent.com/arabic/the-beginning-of-dotting-and-diacritics-in-arabic/<br><br><hr><br>Arabic has only three short vowels, or حركات (literally: “movements”), which are written as small symbols above/below<br>letters.<br><br>| Vowel name&emsp;| Vowel sound | Arabic | English example |<br>| Fatha;&emsp;فتحة&emsp;| <em>a</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـَ&emsp;&emsp;&emsp; | <em>mat</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br>| Dhamma; ظمّة&emsp;| <em>u</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـُ&emsp;&emsp;&emsp; | <em>sugar</em>&emsp;&emsp;&emsp;&emsp;&emsp; |<br>| Kasra; كسرة&emsp;| <em>i</em>&emsp;&emsp;&emsp;&emsp;&emsp; | ـِ&emsp;&emsp;&emsp; | <em>bit</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br><br>The “no vowel” marker is suukun/سكون: While هههه has its vowels guessed to be هَهَهَهَ “hahahah”, we obtain “hhhh” by using<br>sukkun, هْهْهْهْ. It is important for consonant-vowel-consonant syllables, such as بَابْ “bab” which means <em>door.</em><br><br>Incidentally, when a sound needs to be repeated twice, it is usually written once with a <em>Shadda</em> ـّـ to indicate the<br>doubling.&emsp;For example, فَهِمَ <em>fahima</em> “he understood” but فَهَّمَ <em>fahhama</em> “he explained”. Shadda is used with الـ + ‘sun<br>letters’. Unlike the other short vowels, the Shadda is usually written even in informal Arabic, to avoid ambiguity.<br><br><br>Arabic has 3 long vowels, which are formed using specific letters <em>after</em> the short vowels:<br> | Long vowel&emsp;sound | Arabic | English example |<br> | <em>aa</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـَا&emsp;&emsp;&emsp;| <em>far</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |<br> | <em>ii</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـِي&emsp;&emsp;&emsp;| <em>meet</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br> | <em>uu</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| ـُو&emsp;&emsp;&emsp;| <em>boot</em>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;|<br><br>Since short vowels are normally not written, letters ا ي و play two roles: They behave as long vowels <em>aa,ii,uu</em> (when<br>preceded by short vowels) and also behave as consonant sounds <em>a,y,w</em>.<br> + For example, as a consonant, ي (https://arabic.fi/letters/74) makes an English “y” sound; but as a long vowel it makes an “ii” sound.<br> + Occasionally, <em>aa</em> is written using ی (which is like ي but without the dots), or یٰ, rather than an<br>&emsp; <em>alif</em>. This always happens at the end of a word and is called <em>alif maqsuura</em><br>&emsp; “broken alif”; for example علی “on” and موسیٰ “Musa”.<br><br>The following video reads all Arabic letters, where each letter is vowelised by one of the 3 short vowels. It's a really<br>nice video: https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6.">vowels</abbr>
</p>
<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>vowels</h3>
<p>
Arabs <i>infer</i> vowels from context, otherwise words alone such as حمل are ambigious: It could mean حَمَلَ “he carried” or حُمِلْ
“he was carried”.
</p>

<p>
As an example sentence with vowels written, Prophet Muhammad is known to have said:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">أنَا مَدِينَةُ الْعِلْمِ وَعَلَيٌ بَابُهَا</td>
</tr>

<tr>
<td class="org-left"><i>I am the city of knowledge and Ali is its gate</i></td>
</tr>
</tbody>
</table>

<p>
Incidentally, Ali was the one who commissioned the system of vowels.
<a href="https://blogs.transparent.com/arabic/the-beginning-of-dotting-and-diacritics-in-arabic/">https://blogs.transparent.com/arabic/the-beginning-of-dotting-and-diacritics-in-arabic/</a>
</p>

<hr />
<p>
Arabic has only three short vowels, or حركات (literally: “movements”), which are written as small symbols above/below
letters.
</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>
<tbody>
<tr>
<td class="org-left">Vowel name</td>
<td class="org-left">Vowel sound</td>
<td class="org-left">Arabic</td>
<td class="org-left">English example</td>
</tr>

<tr>
<td class="org-left">Fatha;  فتحة</td>
<td class="org-left"><i>a</i></td>
<td class="org-left">ـَ</td>
<td class="org-left"><i>mat</i></td>
</tr>

<tr>
<td class="org-left">Dhamma; ظمّة</td>
<td class="org-left"><i>u</i></td>
<td class="org-left">ـُ</td>
<td class="org-left"><i>sugar</i></td>
</tr>

<tr>
<td class="org-left">Kasra; كسرة</td>
<td class="org-left"><i>i</i></td>
<td class="org-left">ـِ</td>
<td class="org-left"><i>bit</i></td>
</tr>
</tbody>
</table>

<p>
The “no vowel” marker is suukun/سكون: While هههه has its vowels guessed to be هَهَهَهَ “hahahah”, we obtain “hhhh” by using
sukkun, هْهْهْهْ. It is important for consonant-vowel-consonant syllables, such as بَابْ “bab” which means <i>door.</i>
</p>

<p>
Incidentally, when a sound needs to be repeated twice, it is usually written once with a <i>Shadda</i> ـّـ to indicate the
doubling.  For example, فَهِمَ <i>fahima</i> “he understood” but فَهَّمَ <i>fahhama</i> “he explained”. Shadda is used with الـ + ‘sun
letters’. Unlike the other short vowels, the Shadda is usually written even in informal Arabic, to avoid ambiguity.
</p>


<p>
Arabic has 3 long vowels, which are formed using specific letters <i>after</i> the short vowels:
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">Long vowel  sound</td>
<td class="org-left">Arabic</td>
<td class="org-left">English example</td>
</tr>

<tr>
<td class="org-left"><i>aa</i></td>
<td class="org-left">ـَا</td>
<td class="org-left"><i>far</i></td>
</tr>

<tr>
<td class="org-left"><i>ii</i></td>
<td class="org-left">ـِي</td>
<td class="org-left"><i>meet</i></td>
</tr>

<tr>
<td class="org-left"><i>uu</i></td>
<td class="org-left">ـُو</td>
<td class="org-left"><i>boot</i></td>
</tr>
</tbody>
</table>

<p>
Since short vowels are normally not written, letters ا ي و play two roles: They behave as long vowels <i>aa,ii,uu</i> (when
preceded by short vowels) and also behave as consonant sounds <i>a,y,w</i>.
</p>
<ul class="org-ul">
<li>For example, as a consonant, <a href="https://arabic.fi/letters/74">ي</a> makes an English “y” sound; but as a long vowel it makes an “ii” sound.</li>
<li>Occasionally, <i>aa</i> is written using ی (which is like ي but without the dots), or یٰ, rather than an
<i>alif</i>. This always happens at the end of a word and is called <i>alif maqsuura</i>
“broken alif”; for example علی “on” and موسیٰ “Musa”.</li>
</ul>

<p>
The following video reads all Arabic letters, where each letter is vowelised by one of the 3 short vowels. It's a really
nice video: <a href="https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6">https://www.youtube.com/embed/U1Cl6W8EEBQ?start=6</a>.
</p>

</div>
</div>
</div>

<div id="outline-container-orgdeeed75" class="outline-2">
<h2 id="orgdeeed75"><span class="section-number-2">4.</span> <a href="#orgdeeed75">Pronoun</a></h2>
<div class="outline-text-2" id="text-4">
<p>
<abbr class="tooltip" title="A <em>pronoun</em> is a word that stands-in for a noun. For example, below we refer to someone<br>in 3 different ways:<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <em>“His” cat saw “him”, and “he” jumped!</em><br><br>+ A <em>personal pronoun</em> replaces a noun that refers to a person (e.g., Jasim ate ≈ <em>he</em> ate),<br>+ while a <em>possessive pronoun</em> replaces a noun that involves ownership (e.g., Jasim's book ≈ <em>his</em> book),<br>+ and an <em>objective pronoun</em> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <em>him</em>.)<br><br><hr> Below are Arabic's <em>personal pronouns</em> alongside their English translations.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;&emsp;| plural&emsp;&emsp;&emsp;|<br>| 1&emsp;| أنا&emsp; &emsp;I&emsp;&emsp;&emsp; | نَحْن &emsp;we&emsp;&emsp; |<br>| 2m | أَنْتَ&emsp;  you&emsp;&emsp; | أَنْتُم &emsp;you&emsp;&emsp;|<br>| 2f | أَنْتِ&emsp;  you&emsp;&emsp; | أَنتُن &emsp;you&emsp;&emsp;|<br>| 3m | هُوَ&emsp;   he/it&emsp;| هُم&emsp;  they&emsp;|<br>| 3f | هِيَ&emsp;   she/it | هُنَّ&emsp;   they |<br><br><hr> In Arabic, <em>possessive and object pronouns</em> are <em>attached pronouns</em>; they are <strong>joined to the end</strong> of a word: For example,<br><em>house</em> بیت becomes <em>my house</em> بیتِي, and from <em>he helped</em> نَصَرَ we get نَصَرَني <em>he helped me</em>.<br>Arabic's object & possessive pronouns are the same, except for the “my/me” case.<br><br>|&emsp;&emsp;| singular&emsp;&emsp;| plural&emsp;&emsp;&emsp;&emsp; |<br>| 1&emsp;| ـِي&emsp;  &emsp;my&emsp; | ـنَا&emsp;&emsp; &emsp;our&emsp;&emsp;|<br>| 2m | ـكَ&emsp;   your | ـكُمْ&emsp;&emsp;   your&emsp;|<br>| 2f | ـكِ&emsp;   your | ـكُنَّ&emsp;&emsp;  your&emsp;|<br>| 3m | ـَهُ&emsp;  &emsp;his&emsp;| ـهُمْ&emsp;&emsp; &emsp;their |<br>| 3f | ـَهَا&emsp; &emsp; hers | ـهُنَّ&emsp;  &emsp; their |">pronoun</abbr>
</p>
<div style="padding: 1em;background-color: #CCFFFF;border-radius: 15px;font-size: 0.9em;"> <h3>pronoun</h3>
<p>
A <i>pronoun</i> is a word that stands-in for a noun. For example, below we refer to someone
in 3 different ways:
                   <i>“His” cat saw “him”, and “he” jumped!</i>
</p>

<ul class="org-ul">
<li>A <i>personal pronoun</i> replaces a noun that refers to a person (e.g., Jasim ate ≈ <i>he</i> ate),</li>
<li>while a <i>possessive pronoun</i> replaces a noun that involves ownership (e.g., Jasim's book ≈ <i>his</i> book),</li>
<li>and an <i>objective pronoun</i> replaces a noun that is having an action done to it (e.g., I saw Jasim ≈ I saw <i>him</i>.)</li>
</ul>

<p>
&lt;hr&gt; Below are Arabic's <i>personal pronouns</i> alongside their English translations.
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">singular</td>
<td class="org-left">plural</td>
</tr>

<tr>
<td class="org-left">1</td>
<td class="org-left">أنا     I</td>
<td class="org-left">نَحْن   we</td>
</tr>

<tr>
<td class="org-left">2m</td>
<td class="org-left">أَنْتَ    you</td>
<td class="org-left">أَنْتُم   you</td>
</tr>

<tr>
<td class="org-left">2f</td>
<td class="org-left">أَنْتِ    you</td>
<td class="org-left">أَنتُن   you</td>
</tr>

<tr>
<td class="org-left">3m</td>
<td class="org-left">هُوَ     he/it</td>
<td class="org-left">هُم    they</td>
</tr>

<tr>
<td class="org-left">3f</td>
<td class="org-left">هِيَ     she/it</td>
<td class="org-left">هُنَّ     they</td>
</tr>
</tbody>
</table>

<p>
&lt;hr&gt; In Arabic, <i>possessive and object pronouns</i> are <i>attached pronouns</i>; they are <b>joined to the end</b> of a word: For example,
<i>house</i> بیت becomes <i>my house</i> بیتِي, and from <i>he helped</i> نَصَرَ we get نَصَرَني <i>he helped me</i>.
Arabic's object &amp; possessive pronouns are the same, except for the “my/me” case.
</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" />
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left">singular</td>
<td class="org-left">plural</td>
</tr>

<tr>
<td class="org-left">1</td>
<td class="org-left">ـِي      my</td>
<td class="org-left">ـنَا       our</td>
</tr>

<tr>
<td class="org-left">2m</td>
<td class="org-left">ـكَ     your</td>
<td class="org-left">ـكُمْ       your</td>
</tr>

<tr>
<td class="org-left">2f</td>
<td class="org-left">ـكِ     your</td>
<td class="org-left">ـكُنَّ      your</td>
</tr>

<tr>
<td class="org-left">3m</td>
<td class="org-left">ـَهُ      his</td>
<td class="org-left">ـهُمْ       their</td>
</tr>

<tr>
<td class="org-left">3f</td>
<td class="org-left">ـَهَا      hers</td>
<td class="org-left">ـهُنَّ       their</td>
</tr>
</tbody>
</table>


</div>
<p>
When <b>I</b> am talking, the speaker is the “first person” (“1”); when taking <b>about you</b>, then you are the “second person” and
may be masculine (“2m”) or feminine (“2f”), or a group of you (“plural”); finally, when talking about someone who is <b>not
here</b> in the conversation, they are in the “third person” (“3m, 3f”).
</p>
</div>
</div>

<div id="outline-container-orga302fd3" class="outline-2">
<h2 id="orga302fd3"><span class="section-number-2">5.</span> <a href="#orga302fd3">Passive</a></h2>
<div class="outline-text-2" id="text-5">
<p>
<abbr class="tooltip" title="A “passive” verb is one where the subject undergoes the action of the verb rather than carries out the action, for<br>example حُملت “she was carried” and يُستخدم “it is used”.">passive</abbr>
</p>
<div style="padding: 1em;background-color: #CCFFFF;border-radius: 15px;font-size: 0.9em;"> <h3>passive</h3>
<p>
A “passive” verb is one where the subject undergoes the action of the verb rather than carries out the action, for
example حُملت “she was carried” and يُستخدم “it is used”.
</p>

</div>
</div>
</div>

<div id="outline-container-orgdc37d54" class="outline-2">
<h2 id="orgdc37d54"><span class="section-number-2">6.</span> <a href="#orgdc37d54">Transitive</a></h2>
<div class="outline-text-2" id="text-6">
<p>
<abbr class="tooltip" title="A “transitive” verb is a verb that requires an object to express a complete thought, otherwise it is “intransitive”.<br>Some verbs are both transitive and intransitive.<br><br>A “transitive” verb needs to <em>transfer its action</em> to something or someone ---the object.<br>In essence, transitive means “to affect something else.”<br><br>For example, “Please bring coffee.” would not be a complete thought without the object “coffee”.<br>That is, “Please bring.” is an incomplete thought: What or whom should we bring? As such, “bring” is a transitive verb.<br>In contrast, “Please sing.” is a complete thought, and so “sing” is an intransitive verb ---actually, it's also transitive.<br><br>In Arabic, the Form-4 أفْعَلَ pattern turns intransitive verbs into transitive ones; and turns transitive verbs into<br>doubly-transitive verbs ---which means it takes two objects: E.g., “I gave the boy the ball”, here “gave” is<br>doubly-transitive. E.g., in Form-4, ر−س−ل “to send” gives the transitive verb أرْسَلَ which means it can be followed by two<br>objects: أرْسَلَ الولد لكتاب “The boy sent the book”.">transitive</abbr>
</p>
<div style="padding: 1em;background-color: #CCFFCC;border-radius: 15px;font-size: 0.9em;"> <h3>transitive</h3>
<p>
A “transitive” verb is a verb that requires an object to express a complete thought, otherwise it is “intransitive”.
Some verbs are both transitive and intransitive.
</p>

<p>
A “transitive” verb needs to <i>transfer its action</i> to something or someone &#x2014;the object.
In essence, transitive means “to affect something else.”
</p>

<p>
For example, “Please bring coffee.” would not be a complete thought without the object “coffee”.
That is, “Please bring.” is an incomplete thought: What or whom should we bring? As such, “bring” is a transitive verb.
In contrast, “Please sing.” is a complete thought, and so “sing” is an intransitive verb &#x2014;actually, it's also transitive.
</p>

<p>
In Arabic, the Form-4 أفْعَلَ pattern turns intransitive verbs into transitive ones; and turns transitive verbs into
doubly-transitive verbs &#x2014;which means it takes two objects: E.g., “I gave the boy the ball”, here “gave” is
doubly-transitive. E.g., in Form-4, ر−س−ل “to send” gives the transitive verb أرْسَلَ which means it can be followed by two
objects: أرْسَلَ الولد لكتاب “The boy sent the book”.
</p>

</div>
</div>
]]></description>
  <category><![CDATA[arabic]]></category>
  <link>https://alhassy.github.io/arabic-glossary.html</link>
  <guid>https://alhassy.github.io/arabic-glossary.html</guid>
  <pubDate>Tue, 01 Nov 2022 00:00:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[Dora, The Arab Explorer]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-family.html"><img src="https://img.shields.io/badge/-family-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-arabic.html"><img src="https://img.shields.io/badge/-arabic-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-javascript.html"><img src="https://img.shields.io/badge/-javascript-grey?logo=nil"></a></center>
<center><a href="https://upload.wikimedia.org/wikipedia/en/6/64/Dora_and_Boots.jpg" class="tooltip" title="Image credit “https://upload.wikimedia.org/wikipedia/en/6/64/Dora_and_Boots.jpg”"><img src="https://upload.wikimedia.org/wikipedia/en/6/64/Dora_and_Boots.jpg" alt="Article image" style="border: 2px solid black;" width="350" height="300" align="top"/></a></center>

<div class="abstract" style="border: 1px solid black;padding: 1%; margin-top: 1%; margin-bottom: 1%;margin-right: 10%; margin-left: 10%; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>

<p>
A simple interface to watch the engaging children's show “Dora the Explorer” in Arabic
</p>

</div>

<p style="margin-bottom: 1cm;"></p>
<p hidden> See: https://alhassy.github.io/AngularJSCheatSheet/ </p>

<center ng-app="myDoraApp">

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
    </script>

  <div ng-controller="MyVideoController">
    <!-- <h1 display=none>  مغامرات دورا  </h1> -->
    <h1> {{selectedEpisode.title}} </h1>

    <select ng-model="selectedEpisode" ng-options="show.title for show in shows"></select>

      <iframe src="{{ selectedEpisode.url | trustAsResourceUrl}}" allowfullscreen="true" width="100%" height="500"></iframe>

    <script>
      var app = angular.module("myDoraApp", [])

      app.filter('trustAsResourceUrl', ['$sce', function ($sce) {
            return function (val) {
                return $sce.trustAsResourceUrl(val);
            };
        }]);

      app.controller("MyVideoController",
         async function($scope, $http){

         $scope.shows = [
  {
    "title": "بوكيمون الجزء 1 الحلقة 1",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=NVBzNnNXVGpsMDBJc284MGJWVjJ5Zz09&id=3468&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 2",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=MnQ4RHN0ZkM0aGF4ZUhJVWFRMklDZz09&id=3469&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 3",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=aFprQituRGdiODYwejEwY1RacUR1UT09&id=3470&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 4",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=OWhZazF0UDQ3Yjh5eDFET21DeXNzdz09&id=3471&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 5",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=WDU2d1dPQzZqQWJla1FoOU5tMU9YUT09&id=3472&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 6",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=clo4bkxZdDJ4Ti9WTk9FQitaZHo4UT09&id=3473&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 7",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=WFZhY3FsY1BHRS9BWUtJakVndHBzdz09&id=3474&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 8",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=V0FiaE03Vk10S2tiRFUyTklYbG9Vdz09&id=3475&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 9",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=QXdhWjdvM2xLUmN0bUYvWXN5RWJ3UT09&id=3476&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 10",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=T25iaDBTZ2JMbVdNa2JMcHNobG1XUT09&id=3477&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 11",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=VzMvNVBkVENEeG1URzhwMDNPaFRuUT09&id=3478&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 12",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=eEdVVHlabzJUOUszNjhaY1dNS1o1UT09&id=3479&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 13",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=cnpUMnRpOGxpSjdBU2l5bnlXaWhWUT09&id=3480&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 14",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=TkFBVGxZVGVkdm1LeTJaQk9OcndFUT09&id=3481&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 15",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=eXE3QWt4MWY2eEttdlJrUm51aDJPUT09&id=3482&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 16",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=LzhvOHJFZGIxaWNlYi80VjFCOXVHQT09&id=3483&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 17",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=K3pQK0VlQXdZM2V0YmZJeWdiUFhPUT09&id=3484&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 18",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=STdNc3NKSzNGWldHdFh1MXlSNnltQT09&id=3485&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 19",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=emI2VS9wbGpvZHJScmNkbW8wK2pSQT09&id=3486&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 20",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=ZEpxRXIvR3VhV2N5d1VjMlBJek1IUT09&id=3487&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 21",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=c3R2d044WHR0Szk0YTZZQloyVVhmZz09&id=3488&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 22",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=eFV3dVV5MTZvZVBCZG05b3lGZkVXUT09&id=3489&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 23",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=QWlUc0pLNDlNSndxU0E0LytqeXpjdz09&id=3490&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 24",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=eSs4ZHJmR1JmZEh3L002Y20rbkg0UT09&id=3491&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 25",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=VDBOM1lleVdKRmt6dENXMG5JRFpzdz09&id=3492&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 26",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=aEdqcVMyTmllalEvTkwvVmNqbFN4Zz09&id=3493&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 27",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=dVp0K3FxSDBlQ1hUQ1VFSldNMlhaUT09&id=3494&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 28",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=Qnp3YnR6NGpMVUo2bmxycjNtSlROQT09&id=3495&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 29",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=UVRGL2IxVkhMT3VRRTdhdmVqc3E5UT09&id=3496&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 30",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=ZlkwYVFKcmdRTnJmKy8vMDd2SllWQT09&id=3497&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 31",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=RzNGbkRkMkVOVUlRMWxORVh6VTI5Zz09&id=3498&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 32",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=cmNBM29rTCtsSW1rYXBSRHk1Tk0rZz09&id=3499&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 33",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=dWZQRUFCM3FMaHQxQjY3UWxSa1hvUT09&id=3500&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 34",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=VTNEeTVCRW5EUGlnaCtKRmJDRDRWZz09&id=3501&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 35",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=K1JaZnBXalVOcWlxSXcwdzV1eU40QT09&id=3502&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 36",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=cnd5T3RwZWVPeEdSbDNiMC9KMStjQT09&id=3503&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 37",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=L0hiTjFHeXNIelRWNU0zM3AzcXpVUT09&id=3504&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 38",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=bHdZNGlZOVZFbXgxQXc3TWp4OHk1dz09&id=3505&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 39",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=eUtNZnphRGNMRVVPb0NlQ01nVC9ldz09&id=3506&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 40",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=dlNZQlVqSjF5eUU2M05mdmxOSE95QT09&id=3507&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 41",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=RlBqZzJQZVBIVEFLK0ZTcTIxOHRqQT09&id=3508&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 42",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=cllKSks5cklJOTltUmxWTmlmdm90dz09&id=3509&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 43",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=MHQxNHdQY0xLTmRmZTlxak1kbTFIQT09&id=3510&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 44",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=YXdIZmJ1ZzBDQzlteFA1YVBVUTl1QT09&id=3511&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 45",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=cEYvYzNqM1lTWGsrM1dZWVFSY1kvQT09&id=3512&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 46",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=SEFKb05NSmlGZE1MSTQveDl1eHN0UT09&id=3513&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 47",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=Sm43YU83cldVNXJTNmlwOEZEVURjZz09&id=3514&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 48",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=S3lQSnVKbkJ4akY1ckFLWit3aXhwdz09&id=3515&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 49",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=a3JUMGY0YnUvOVZyTFA0N0Q3dThPUT09&id=3516&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 50",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=UmIybHdURk0vYTFUZDRsZFpqRzFFdz09&id=3517&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 51",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=ZXRFeDkraTViWFpZNG9JdXRyRC9xZz09&id=3518&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 52",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=QnlQb3dEN2VVaVZRYkZFN1VLY3lZUT09&id=3519&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 53",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=Z3dYbDUreXEzZ3RiQlFERkVZaFJsUT09&id=3520&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 54",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=ZEZ2ZnN2TStYUjJuU2RZQlp3c3JWdz09&id=3521&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 55",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=SzNtMnExM3lQUzZFYnlIa3NKNEpFdz09&id=3522&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 56",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=THA1Q2hxY1Zza3k0K1lwWVB5Vm0xUT09&id=3523&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 57",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=anEzVkhLUVAzUEFrbmgzN0t3V0lxdz09&id=3524&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 58",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=c3lYZGg0M2F6Z1p3QXlSaHFYUFBrdz09&id=3525&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 59",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=UlZldmh3Z2RzaCswK3Q0QmRCVE0zdz09&id=3526&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 60",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=VkRNZVJvZ0ZJeEdXRk9ydU1sYkl5UT09&id=3527&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 61",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=NytsTnJyUFNEdXNJM0o5R0ZnNnI4UT09&id=3528&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 62",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=R3ltcGpEQnhnNEVoekRHS1pmZFlLUT09&id=3529&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 63",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=b0p4ZHdhQ216WEhzcTVpVGNjdWRJQT09&id=3530&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 64",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=VnNPMTlhODV6S2hjaTI0MVhsWW1idz09&id=3531&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 65",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=bEcyK0hncWMwRlNmVUtlS2lEY2ZyZz09&id=3532&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 66",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=VDg4ZDRlVjRMSzhreW5HOXJHN1E4UT09&id=3533&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 67",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=dXdBbG1vcG5ESjNldks0WUtYUDE4UT09&id=3534&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 68",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=c3IvckdwbkhpQ1FMNkVLY2VlTU9sdz09&id=3535&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 69",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=clRHaDVoanM0VXJmMmJ2aVRiaXVDdz09&id=3536&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 70",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=Vm5STVo0ZEJHd0I3T2phclB0amxuQT09&id=3537&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 71",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=MEpFdkQyN1hwZDN5OHpCYUkrQ0k3dz09&id=3538&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 72",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=OFhiaFhzbVU2dm84S1JDMVEyaEVZUT09&id=3539&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 73",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=WFpPcElFYm1jVExla1VHV1RxZlg3dz09&id=3540&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 74",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=amR0NmlYb2hEWllPZ1hDVk1WSEMzdz09&id=3541&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 75",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=UVVWcW5jY1RuZDdIdlM2SDRLT1h0UT09&id=3542&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 76",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=MU5FOE1USGQ0SEpjUFhQeU0rb1BRdz09&id=3543&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 77",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=Z2ZibFppUEJNZ040WkdNWi9DNkREUT09&id=3544&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 78",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=OEV2T25QRlhJdDdraG1KbEJxK1dLdz09&id=3545&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 79",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=MzlMaUpITis2Q1BrMHpUd0dKdWhLdz09&id=3546&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 80",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=c2VlT0dzNXE3Q1BMcnRMdm9ZS05vdz09&id=3547&y=1"
  },
  {
    "title": "بوكيمون الجزء 1 الحلقة 81",
    "url": "https://www.arteenz.com/plugins/server6/embed.php?url=Qkd6YnN3WGwyVnIxeWtQLzUreE5Bdz09&id=3548&y=1"
  }
         ]

             let rndmEpisodeIndex = Math.floor(Math.random() * $scope.shows.length) + 1
             $scope.selectedEpisode = $scope.shows[rndmEpisodeIndex]
       })
    </script>
    ]]></description>
  <category><![CDATA[family]]></category>
  <category><![CDATA[arabic]]></category>
  <category><![CDATA[javascript]]></category>
  <link>https://alhassy.github.io/dora.html</link>
  <guid>https://alhassy.github.io/dora.html</guid>
  <pubDate>Fri, 21 Oct 2022 11:20:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[💐 Making VSCode itself a Java REPL 🔁]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-repl-driven-development.html"><img src="https://img.shields.io/badge/-repl_driven_development-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-vscode.html"><img src="https://img.shields.io/badge/-vscode-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-emacs.html"><img src="https://img.shields.io/badge/-emacs-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-javascript.html"><img src="https://img.shields.io/badge/-javascript-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-java.html"><img src="https://img.shields.io/badge/-java-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-python.html"><img src="https://img.shields.io/badge/-python-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-ruby.html"><img src="https://img.shields.io/badge/-ruby-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-clojure.html"><img src="https://img.shields.io/badge/-clojure-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-typescript.html"><img src="https://img.shields.io/badge/-typescript-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-haskell.html"><img src="https://img.shields.io/badge/-haskell-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-lisp.html"><img src="https://img.shields.io/badge/-lisp-grey?logo=nil"></a></center>
<center><a href="https://github.com/alhassy/easy-extensibility/blob/main/graphics/repl-java.gif?raw=true" class="tooltip" title="Image credit “https://github.com/alhassy/easy-extensibility/blob/main/graphics/repl-java.gif?raw=true”"><img src="https://github.com/alhassy/easy-extensibility/blob/main/graphics/repl-java.gif?raw=true" alt="Article image" style="border: 2px solid black;" width="90%" height="90%" align="top"/></a></center>

<div class="abstract" style="border: 1px solid black;padding: 1%; margin-top: 1%; margin-bottom: 1%;margin-right: 10%; margin-left: 10%; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>

<p>
VSCode evaluates Java code wherever it sees it, by sending it to a JShell in the background, and echos the results in a
friendly way!
</p>

<p>
This is achieved with a <a href="https://github.com/alhassy/easy-extensibility">meta-extension for VSCode</a> that makes VSCode into a living, breathing, JS interpreter: It can
execute arbitrary JS that alters VSCode on-the-fly.  <i>(Inspired by using Emacs and Lisp!)</i>
</p>

<p>
The relevant docs show how to make a similar REPL for <a href="https://github.com/alhassy/easy-extensibility/blob/65572a283e4864d9bef157b438c33f4e47276e3a/vscodejs/index.js#L1417-L1420">Python</a>, <a href="https://github.com/alhassy/easy-extensibility/blob/65572a283e4864d9bef157b438c33f4e47276e3a/vscodejs/index.js#L1459-L1462">Ruby</a>, <a href="https://github.com/alhassy/easy-extensibility/blob/65572a283e4864d9bef157b438c33f4e47276e3a/vscodejs/index.js#L1475-L1479">Clojure</a>, <a href="https://github.com/alhassy/easy-extensibility/blob/65572a283e4864d9bef157b438c33f4e47276e3a/vscodejs/index.js#L1490-L1494">Common Lisp</a>, <a href="https://github.com/alhassy/easy-extensibility/blob/65572a283e4864d9bef157b438c33f4e47276e3a/vscodejs/index.js#L1509-L1515">JavaScript</a>, <a href="https://github.com/alhassy/easy-extensibility/blob/65572a283e4864d9bef157b438c33f4e47276e3a/vscodejs/index.js#L1527-L1536">Typescript</a>,
<a href="https://github.com/alhassy/easy-extensibility/blob/65572a283e4864d9bef157b438c33f4e47276e3a/vscodejs/index.js#L1553-L1557">Haskell</a>, and of-course <a href="https://github.com/alhassy/easy-extensibility/blob/65572a283e4864d9bef157b438c33f4e47276e3a/vscodejs/index.js#L1575-L1585">Java</a>.
</p>


</div>

<div id="outline-container-org3b19677" class="outline-2">
<h2 id="org3b19677"><span class="section-number-2">1.</span> <a href="#org3b19677">How do people usually code?</a></h2>
<div class="outline-text-2" id="text-1">
<p>
Either you,
</p>

<ol class="org-ol">
<li>Use a code editor and edit multiple lines, then jump into a console to try
out what you wrote⏳, or</li>

<li>You use an interactive command line, and work with one line at a time
&#x2014;continuously editing &amp; evaluating 🔄</li>
</ol>

<p>
The first approach sucks because your code and its resulting behaviour occur in different places 😢 The second is only
realistic for small experimentation &#x2014;after all, you're in a constrained environment and don't generally have the same
features that your code editor provides 🧟‍
</p>
</div>
</div>

<div id="outline-container-org59c44fa" class="outline-2">
<h2 id="org59c44fa"><span class="section-number-2">2.</span> <a href="#org59c44fa">If only we could have our cake, and eat it too! 🍰</a></h2>
<div class="outline-text-2" id="text-2">
<p>
With a few lines of JavaScript, to alter VSCode, we can get both approaches! No need to switch between the two anymore!
Just select some code and press <code>Ctrl+X Ctrl+E</code> ---<i>E for Evaluate!</i> 😉
</p>

<ol class="org-ol">
<li>Install the meta-extension <a href="https://marketplace.visualstudio.com/items?itemName=alhassy.easy-extensibility">Easy-Extensibility</a>.

<ul class="org-ul">
<li>Easy-Extensibility makes VSCode into a living JavaScript interpreter; that can execute arbitrary JS to alter VSCode
on-the-fly: Just press “⌘+E” to “E”valuate code! 😲</li>

<li>You can save and reuse your code by placing it in an ~/init.js file.</li>

<li>Video Intro to Easy-Extensibility: <a href="https://youtu.be/HO2dFgisriQ">https://youtu.be/HO2dFgisriQ</a></li>
</ul></li>

<li>Make a <code>~/init.js</code> file &#x2014;or just invoke <code>Cmd+h Find user's init.js file, or provide a template</code>.</li>

<li><p>
Stick the following code in there; this code will be run every time you open VSCode.
</p>

<ul class="org-ul">
<li>Restart or run <code>Cmd+h Reload ~/init.js file</code>.</li>
</ul>

<div class="org-src-container">
<pre class="src src-javascript">commands[<span style="color: #689d6a;">'Evaluate: Java'</span>] = {
 <span style="color: #689d6a;">'ctrl+x ctrl+e'</span>: E.REPL({
   command: <span style="color: #689d6a;">'/usr/bin/jshell'</span>,     <span style="color: #a89984;">// </span><span style="color: #a89984;">Ensure this points to wherever you have JShell</span>
   prompt: <span style="color: #689d6a;">'jshell&gt;'</span>,
   errorMarkers: [<span style="color: #689d6a;">/Error:/</span>, <span style="color: #689d6a;">/\|\s*\^-*/</span>, <span style="color: #689d6a;">/\|\s+Exception/</span>, <span style="color: #689d6a;">/\|\s+at/</span>, <span style="color: #689d6a;">/cannot find symbol/</span>, <span style="color: #689d6a;">/symbol:/</span>],
   echo: E.overlay,   <span style="color: #a89984;">// </span><span style="color: #a89984;">For notification-style output, change this to:  E.message</span>
   stdout: result =&gt; result.includes(<span style="color: #689d6a;">' ==&gt; '</span>) ? result.replace(<span style="color: #689d6a;">'==&gt;'</span>, <span style="color: #689d6a;">'&#11157;'</span>) : <span style="color: #689d6a;">`&#11157; ${result} `</span>
 })}
</pre>
</div></li>

<li><p>
Now you can select any Java code and press Ctrl+X Ctrl+E to evaluate it! 🥳
</p>

<p>
👀 Take another look at the above gif for what's possible 🔼
</p></li>
</ol>
</div>
</div>

<div id="outline-container-org0dbf0c0" class="outline-2">
<h2 id="org0dbf0c0"><span class="section-number-2">3.</span> <a href="#org0dbf0c0">Wait, how does the above JavaScript work?</a></h2>
<div class="outline-text-2" id="text-3">
<p>
Look at <a href="https://github.com/alhassy/easy-extensibility/blob/main/vscodejs/index.js#L1404-L1438">the docs</a>!
</p>

<p>
<center> <a href="https://raw.githubusercontent.com/alhassy/easy-extensibility/main/graphics/repl-docs.jpeg" class="tooltip" title="Image credit “https://raw.githubusercontent.com/alhassy/easy-extensibility/main/graphics/repl-docs.jpeg”"><img src="https://raw.githubusercontent.com/alhassy/easy-extensibility/main/graphics/repl-docs.jpeg" alt="Article image"
             width="700" height="600" align="top"/></a> </center>
</p>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">🤯 The docs are longer than the code itself 😼</td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-org9e92539" class="outline-2">
<h2 id="org9e92539"><span class="section-number-2">4.</span> <a href="#org9e92539">Technically speaking, how is VSCode <i>itself</i> the REPL?</a></h2>
<div class="outline-text-2" id="text-4">
<p>
Let's do what math-nerds call <i>proof by definition-chasing:</i>
</p>

<ol class="org-ol">
<li>Definition: REPL, an alias for <i>command line</i>, stands for Read-Evaluate-Print-Loop</li>

<li><code>Ctrl+X Ctrl+E</code> will echo the results in an overlay at the end of the selection &#x2014;or at the end of the line, when no explicit selection is made</li>

<li>So it retains each of the read, eval, and print parts of the Read-Evaluate-Print-Loop</li>

<li>Moreover, since the program doesn't terminate, you're still in the <i>loop part</i> until you close VSCode</li>
</ol>

<p>
Bye! 👋 🥳
</p>
</div>
]]></description>
  <category><![CDATA[repl-driven-development]]></category>
  <category><![CDATA[vscode]]></category>
  <category><![CDATA[emacs]]></category>
  <category><![CDATA[javascript]]></category>
  <category><![CDATA[java]]></category>
  <category><![CDATA[python]]></category>
  <category><![CDATA[ruby]]></category>
  <category><![CDATA[clojure]]></category>
  <category><![CDATA[typescript]]></category>
  <category><![CDATA[haskell]]></category>
  <category><![CDATA[lisp]]></category>
  <link>https://alhassy.github.io/making-vscode-itself-a-java-repl.html</link>
  <guid>https://alhassy.github.io/making-vscode-itself-a-java-repl.html</guid>
  <pubDate>Mon, 05 Sep 2022 00:00:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[💐 VSCode is itself a JavaScript REPL 🔁]]></title>
  <description><![CDATA[</center> <script> window.parent.document.title =  window.parent.document.title.replace(/@@.*@@/, "") </script>
</p>

<center><a href="https://alhassy.github.io/tags.html"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 16" width="15" height="16"><path fill-rule="evenodd" d="M7.73 1.73C7.26 1.26 6.62 1 5.96 1H3.5C2.13 1 1 2.13 1 3.5v2.47c0 .66.27 1.3.73 1.77l6.06 6.06c.39.39 1.02.39 1.41 0l4.59-4.59a.996.996 0 000-1.41L7.73 1.73zM2.38 7.09c-.31-.3-.47-.7-.47-1.13V3.5c0-.88.72-1.59 1.59-1.59h2.47c.42 0 .83.16 1.13.47l6.14 6.13-4.73 4.73-6.13-6.15zM3.01 3h2v2H3V3h.01z"></path></svg> </a><a href="https://alhassy.github.io/tag-repl-driven-development.html"><img src="https://img.shields.io/badge/-repl_driven_development-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-vscode.html"><img src="https://img.shields.io/badge/-vscode-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-emacs.html"><img src="https://img.shields.io/badge/-emacs-grey?logo=nil"></a> <a href="https://alhassy.github.io/tag-javascript.html"><img src="https://img.shields.io/badge/-javascript-grey?logo=nil"></a></center>
<center><a href="https://raw.githubusercontent.com/alhassy/easy-extensibility/main/graphics/repl.gif" class="tooltip" title="Image credit “https://raw.githubusercontent.com/alhassy/easy-extensibility/main/graphics/repl.gif”"><img src="https://raw.githubusercontent.com/alhassy/easy-extensibility/main/graphics/repl.gif" alt="Article image" style="border: 2px solid black;" width="90%" height="90%" align="top"/></a></center>

<div class="abstract" style="border: 1px solid black;padding: 1%; margin-top: 1%; margin-bottom: 1%;margin-right: 10%; margin-left: 10%; background-color: lightblue;"><center> <strong class="tooltip"title="What's the goal of this article?"> Abstract </strong> </center>

<p>
A meta-extension for VSCode that makes VSCode into a living, breathing, JS interpreter: It can execute arbitrary JS that
alters VSCode on-the-fly. A gateway into the world of Editor Crafting!
</p>

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


<colgroup>
<col  class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><i>(Inspired by using Emacs and Lisp!)</i></td>
</tr>
</tbody>
</table>


</div>

<div id="outline-container-org1b42e68" class="outline-2">
<h2 id="org1b42e68"><span class="section-number-2">1.</span> <a href="#org1b42e68">How do people usually code?</a></h2>
<div class="outline-text-2" id="text-1">
<p>
Either you,
</p>

<ol class="org-ol">
<li>Use a code editor and edit multiple lines, then jump into a console to try
out what you wrote⏳, or</li>

<li>You use an interactive command line, and work with one line at a time
&#x2014;continuously editing &amp; evaluating 🔄</li>
</ol>

<p>
The first approach sucks cause your code and its resulting behaviour occur in
different places 😢 The second is only realistic for small experimentation
&#x2014;after all, you're in a constrained environment and don't generally have the
same features that your code editor provides 🧟‍♂️
</p>
</div>
</div>

<div id="outline-container-orgdd0ac18" class="outline-2">
<h2 id="orgdd0ac18"><span class="section-number-2">2.</span> <a href="#orgdd0ac18">If only we could have our cake, and eat it too! 🍰</a></h2>
<div class="outline-text-2" id="text-2">
<p>
With the VSCode <a href="https://marketplace.visualstudio.com/items?itemName=alhassy.easy-extensibility">Easy-Extensibility</a> extension, we get both approaches! No need to
switch between the two any more! Just select some code and press <code>Cmd+E</code> ---<i>E for
Evaluate!</i> 😉
</p>

<p>
Why the strange name? Why isn't this extension called something more
informative, such as <code>JS-repl-to-the-moon</code>? 👀 Take another look at the above gif
🔼 Besides plain old JavaScript, this extension let's us alter VSCode itself!!
(It's a <i>meta</i>-extension! 😲)
</p>

<center> <br> <em>Intro to Easy-Extensibility</em> <br> <iframe width="360" height="215" src="https://www.youtube.com/embed/HO2dFgisriQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe></center>
</div>
</div>

<div id="outline-container-orgb29a634" class="outline-2">
<h2 id="orgb29a634"><span class="section-number-2">3.</span> <a href="#orgb29a634">Technically speaking, how is VSCode <i>itself</i> the REPL?</a></h2>
<div class="outline-text-2" id="text-3">
<p>
Let's do what math-nerds call <i>proof by definition-chasing:</i>
</p>

<ol class="org-ol">
<li>Definition: REPL, an alias for <i>command line</i>, stands for Read-Evaluate-Print-Loop</li>

<li><code>Cmd+E</code> will echo the results in the notification area, in the bottom right-corner of VSCode</li>

<li>So it retains each of the read, eval, and print parts of the Read-Evaluate-Print-Loop</li>

<li>Moreover, since the program doesn't terminate, you're still in the <i>loop part</i> until you close VSCode</li>
</ol>

<p>
Bye! 👋 🥳
</p>
</div>
]]></description>
  <category><![CDATA[repl-driven-development]]></category>
  <category><![CDATA[vscode]]></category>
  <category><![CDATA[emacs]]></category>
  <category><![CDATA[javascript]]></category>
  <link>https://alhassy.github.io/vscode-is-itself-a-javascript-repl.html</link>
  <guid>https://alhassy.github.io/vscode-is-itself-a-javascript-repl.html</guid>
  <pubDate>Wed, 17 Aug 2022 00:00:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[Typed Lisp, A Primer]]></title>
  <description><![CDATA[</div><div style="text-align: center;">21 Aug 2019</div><center> <img src="images/emacs-birthday-present.png" alt="Article image"
            style="border: 2px solid black;" width="350" height="350" align="top" /> </center><br><center><strong>Abstract</strong></center><nav id="table-of-contents">
<h2><a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a></h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#inconsistent-love">1. “Loving Haskell &amp; Lisp is Inconsistent”</a></li>
<li><a href="#terse-types-tutorial">2. Why Bother with Types? A Terse Tutorial on Type Systems</a>
<ul>
<li><a href="#type-checking">2.1. Obtaining &amp; Checking Types</a></li>
<li><a href="#lisp-is-eval">2.2. Statics &amp; Dynamics of Lisp</a></li>
<li><a href="#lisp-is-dynamic">2.3. Variable Scope</a></li>
<li><a href="#lisp-is-strong">2.4. Casts &amp; Coercions</a></li>
<li><a href="#type-annotations">2.5. Type Annotations</a></li>
<li><a href="#typecase">2.6. Type-directed Computations</a></li>
<li><a href="#type-specifiers">2.7. Type Specifiers: On the nature of types in Lisp</a></li>
<li><a href="#deftype">2.8. Making New Types with <code>deftype</code></a></li>
<li><a href="#adts">2.9. Algebraic Data Types a la Haskell</a></li>
</ul>
</li>
<li><a href="#why-dynamic">3. In Defence of Being Dynamically Checked</a></li>
<li><a href="#lisp-funny-history">4. With its hierarchy of types, why isn't Lisp statically typed?</a></li>
<li><a href="#lisp-is-typed">5. Lisp Actually Admits Static Typing!</a></li>
<li><a href="#elisp-types">6. ELisp's Type Hierarchy</a>
<ul>
<li><a href="#Number">6.1. Number</a></li>
<li><a href="#Character">6.2. Character</a></li>
<li><a href="#Symbol">6.3. Symbol</a></li>
<li><a href="#Sequence">6.4. Sequence</a></li>
<li><a href="#Function">6.5. Function</a></li>
<li><a href="#Macro">6.6. Macro</a></li>
<li><a href="#Record">6.7. Record</a></li>
</ul>
</li>
<li><a href="#typing-via-macros">7. Typing via Macros &amp; Advice</a></li>
<li><a href="#Closing">8. Closing</a></li>
<li><a href="#references">9. References</a></li>
</ul>
</div>
</nav>

<nav id="table-of-contents">
<h2><a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a></h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#inconsistent-love">1. “Loving Haskell &amp; Lisp is Inconsistent”</a></li>
<li><a href="#terse-types-tutorial">2. Why Bother with Types? A Terse Tutorial on Type Systems</a>
<ul>
<li><a href="#type-checking">2.1. Obtaining &amp; Checking Types</a></li>
<li><a href="#lisp-is-eval">2.2. Statics &amp; Dynamics of Lisp</a></li>
<li><a href="#lisp-is-dynamic">2.3. Variable Scope</a></li>
<li><a href="#lisp-is-strong">2.4. Casts &amp; Coercions</a></li>
<li><a href="#type-annotations">2.5. Type Annotations</a></li>
<li><a href="#typecase">2.6. Type-directed Computations</a></li>
<li><a href="#type-specifiers">2.7. Type Specifiers: On the nature of types in Lisp</a></li>
<li><a href="#deftype">2.8. Making New Types with <code>deftype</code></a></li>
<li><a href="#adts">2.9. Algebraic Data Types a la Haskell</a></li>
</ul>
</li>
<li><a href="#why-dynamic">3. In Defence of Being Dynamically Checked</a></li>
<li><a href="#lisp-funny-history">4. With its hierarchy of types, why isn't Lisp statically typed?</a></li>
<li><a href="#lisp-is-typed">5. Lisp Actually Admits Static Typing!</a></li>
<li><a href="#elisp-types">6. ELisp's Type Hierarchy</a>
<ul>
<li><a href="#Number">6.1. Number</a></li>
<li><a href="#Character">6.2. Character</a></li>
<li><a href="#Symbol">6.3. Symbol</a></li>
<li><a href="#Sequence">6.4. Sequence</a></li>
<li><a href="#Function">6.5. Function</a></li>
<li><a href="#Macro">6.6. Macro</a></li>
<li><a href="#Record">6.7. Record</a></li>
</ul>
</li>
<li><a href="#typing-via-macros">7. Typing via Macros &amp; Advice</a></li>
<li><a href="#Closing">8. Closing</a></li>
<li><a href="#references">9. References</a></li>
</ul>
</div>
</nav>

<p>
Let's explore Lisp's fine-grained type hierarchy!
</p>

<p>
We begin with a shallow comparison to Haskell, a rapid tour of type theory,
try in vain to defend dynamic approaches, give a somewhat humorous account of history,
note that you've been bamboozled &#x2014;type's have always been there&#x2014;,
then go into technical details of some Lisp types, and finally conclude by showing
how <i>macros permit typing</i>.
</p>

<p>
Goals for this article:
</p>

<ol class="org-ol">
<li>Multiple examples of type constructions in Lisp.</li>
<li>Comparing Lisp type systems with modern languages, such as Haskell.</li>
<li>Show how algebraic <a href="#orged0cebc">polymorphic</a> types like <code>Pair</code> and <code>Maybe</code> can be defined in Lisp.
Including heterogeneously typed lists!</li>
<li>Convey a passion for an elegant language.</li>
<li>Augment Lisp with functional Haskell-like type declarations ;-)</li>
</ol>

<p>
Unless suggested otherwise, the phrase “Lisp” refers to
<a href="https://www.gnu.org/software/emacs/manual/html_mono/cl.html#index-cl_002ddeftype-14">Common Lisp as supported by Emacs Lisp</a>. As such, the resulting discussion
is applicable to a number of Lisp dialects
&#x2014;I'm ignoring editing types such as buffers and keymaps, for now.
</p>

<small> <center>
<p>
( Original print by Baneen Al-hassy as a birthday present to me. )
</p>
</center> </small>
<style>

.figure-number {
    display: none;
}

.table-number {
    display: none;
}

/* Using source blocks “math” as aliaas for haskell */
pre.src-math:before { content: 'Mathematical! Algebraic! Axiomatic!'; }
/* Execute this for alias: (add-to-list 'org-src-lang-modes '("math" . haskell)) */

</style>

<div id="outline-container-org42e78cf" class="outline-2">
<h2 id="inconsistent-love"><span class="section-number-2">1</span> “Loving Haskell &amp; Lisp is Inconsistent”</h2>
<div class="outline-text-2" id="text-inconsistent-love">
<p>
I have convinced a number of my peers to use Emacs/Spacemacs/Doom-emacs,
but my efforts to get them to even consider trying Lisp have been met with
staunch rejection. These peers are familiar with Haskell, and almost all know Agda,
so you'd think they'd be willing to try Lisp &#x2014;it's there, part of their editor&#x2014;
but the superficial chasm in terms of syntax and types is more than enough apparently.
In this article, I aim to explore the type system of (Emacs) Lisp and occasionally
make comparisons to Haskell. Perhaps in the end some of my Haskell peers would be
willing to try it out.
</p>


<figure>
<img src="https://imgs.xkcd.com/comics/lisp_cycles.png" alt="lisp_cycles.png">

<figcaption><span class="figure-number">Figure 1: </span>xkcd - Lisp is a language of timeless elegance</figcaption>
</figure>

<ul class="org-ul">
<li><p>
↯ I almost never use Haskell for any day-to-day dealings.
</p>

<p>
✓ The ideas expressed by its community are why I try
       to keep updated on the language.
</p></li>

<li><p>
↯ No one around me knows anything about Lisp,
but they dislike it due to the parens.
</p>

<p>
✓ I love it and use it for Emacs configuration and recently
       to prototype my PhD research.
</p></li>
<li>⇅ I love that I can express a complicated procedure compactly in both
by using zips, unzips, filters, and maps <code>(งಠ_ಠ)ง</code>
<ul class="org-ul">
<li><p>
Lately, in Lisp, I'll write a nested loop (gasp!)
then, for fun, try to make it a one-liner!
Sometimes, I actually think the loop formulation is clearer
and I leave it as a loop &#x2014;Breaking news: Two Haskell readers just died.
</p>


<figure>
<img src="https://i.stack.imgur.com/jvSOG.png" alt="jvSOG.png">

<figcaption><span class="figure-number">Figure 2: </span>From the awesome “Land of Lisp” book</figcaption>
</figure></li>
</ul></li>
</ul>

<ul class="org-ul">
<li><p>
<b>What I like and why:</b>
</p>
<table>


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

<col  class="org-left">

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left">Haskell</td>
<td class="org-left">⇒</td>
<td class="org-left">Executable category theory; compact &amp; eloquent</td>
</tr>

<tr>
<td class="org-left">Lisp</td>
<td class="org-left">⇒</td>
<td class="org-left">Extensible language; malleable, uniform, beautiful</td>
</tr>
</tbody>
</table></li>

<li><p>
<b>Documentation?</b>
</p>
<table>


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

<col  class="org-left">

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left">Haskell</td>
<td class="org-left">⇒</td>
<td class="org-left">Hoogle; can search by type alone!</td>
</tr>

<tr>
<td class="org-left">Emacs Lisp</td>
<td class="org-left">⇒</td>
<td class="org-left">Self-documenting; <code>M-x apropos</code></td>
</tr>
</tbody>
</table></li>

<li><p>
<b>How has using the language affected me?</b>
</p>
<table>


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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left">Haskell</td>
<td class="org-left">I almost always think in-terms compoistionality, functors, &amp; currying</td>
</tr>

<tr>
<td class="org-left">Lisp</td>
<td class="org-left">Documentation strings, units tests, and metaprogramming are second nature</td>
</tr>
</tbody>
</table></li>
</ul>

<p>
It may not be entirely accurate to say that
Lisp's type system is more expressive than Haskell's
as it's orthogonal in many respects; although it is closer to that of <a href="https://liquid.kosmikus.org/01-intro.html#/what-is-liquid-haskell">Liquid Haskell</a>.
</p>
</div>
</div>

<div id="outline-container-org3491fb5" class="outline-2">
<h2 id="terse-types-tutorial"><span class="section-number-2">2</span> Why Bother with Types? A Terse Tutorial on Type Systems</h2>
<div class="outline-text-2" id="text-terse-types-tutorial">
<p>
<i>Types allow us to treat objects according a similar structure
or interface.</i>
Unlike Haskell and other statically typed systems, in Lisp we have
that types can overlap.
As such, here's our working definition.
</p>
<div class="org-center">
<p>
A <b>type</b> is a collection of possible objects.
</p>

<p>
To say “\(e\) has type \(τ\)” one writes \(e : τ\), or in Lisp: <code>(typep e 'τ)</code>.
</p>
</div>

<p>
Haskellers and others may append to this definition the following,
which we will not bother with:
<i>Type membership is determined by inspecting
syntactic structure and so is decidable.</i>
</p>

<blockquote>
<p>
✓ Typing is one of the simplest forms of “assertion-comments”:
Documenting a property of your code in a way that the machine can verify.
</p>

<p>
If you're gonna comment on what kind of thing you're working with, why not have the
comment checked by the machine.
</p>
</blockquote>

<table>
<caption class="t-bottom"><span class="table-number">Table 1:</span> Lisp's type hierarchy is a “complemented lattice” ♥‿♥</caption>

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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left">Common types</td>
<td class="org-left"><code>integer, number, string, keyword, array, cons, list, vector, macro, function, atom</code></td>
</tr>

<tr>
<td class="org-left">Top</td>
<td class="org-left"><code>t</code> has everything as an element</td>
</tr>

<tr>
<td class="org-left">Unit</td>
<td class="org-left"><code>null</code> has one element named <code>nil</code></td>
</tr>

<tr>
<td class="org-left">Bottom</td>
<td class="org-left"><code>nil</code> has no elements at all</td>
</tr>

<tr>
<td class="org-left">Union</td>
<td class="org-left"><code>(or τ₀ τ₁ … τₙ)</code>  has elements any element in any type <code>τᵢ</code></td>
</tr>

<tr>
<td class="org-left">Intersection</td>
<td class="org-left"><code>(and τ₀ τ₁ … τₙ)</code> has elements that are in all the types <code>τᵢ</code></td>
</tr>

<tr>
<td class="org-left">Complement</td>
<td class="org-left"><code>(not τ)</code> has elements that are <i>not</i> of type <code>τ</code></td>
</tr>

<tr>
<td class="org-left">Enumeration</td>
<td class="org-left"><code>(member x₀ … xₙ)</code> is the type consisting of only the elements <code>xᵢ</code></td>
</tr>

<tr>
<td class="org-left">Singleton</td>
<td class="org-left"><code>(eql x)</code> is the type with only the element <code>x</code></td>
</tr>

<tr>
<td class="org-left">Comprehension</td>
<td class="org-left"><code>(satisfies p)</code> is the type of values that satisfy predicate <code>p</code></td>
</tr>
</tbody>
</table>

<p>
Let's see some examples:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">The universal type &#8220;t&#8221;, has everything as its value.</span>
(<span style="color: #b58900;">typep</span> 'x '<span style="color: #268bd2;">t</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> 12 '<span style="color: #268bd2;">t</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">The empty type: nil</span>
(<span style="color: #b58900;">typep</span> 'x 'nil) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false; nil has no values.</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">The type &#8220;null&#8221; contains the one value &#8220;nil&#8221;.</span>
(<span style="color: #b58900;">typep</span> nil '<span style="color: #b58900;">null</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> () '<span style="color: #b58900;">null</span>)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8220;(eql x)&#8221; is the singelton type consisting of only x.</span>
(<span style="color: #b58900;">typep</span> 3 '(<span style="color: #b58900;">eql</span> 3)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> 4 '(<span style="color: #b58900;">eql</span> 3)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8220;(member x&#8320; &#8230; x&#8345;)&#8221; denotes the enumerated type consisting of only the x&#7522;.</span>
(<span style="color: #b58900;">typep</span> 3 '(<span style="color: #b58900;">member</span> 3 x <span style="color: #2aa198;">"c"</span>))  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> 'x '(<span style="color: #b58900;">member</span> 3 x <span style="color: #2aa198;">"c"</span>)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> 'y '(<span style="color: #b58900;">member</span> 3 x <span style="color: #2aa198;">"c"</span>)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8220;(satisfies p)&#8221; is the type of values that satisfy predicate p.</span>
(<span style="color: #b58900;">typep</span> 12 '(satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (x) (<span style="color: #b58900;">oddp</span> x)))) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
(<span style="color: #b58900;">typep</span> 12 '(satisfies <span style="color: #b58900;">evenp</span>) )                <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Computation rule for comprehension types.</span>
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">(typep x '(satisfies p)) &#8776; (if (p x) t nil)</span>
</pre>
</div>

<p>
Here's a convenient one: <code>(booleanp x) ≈ (typep x '(member t nil))</code>.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">booleanp</span> 2)   <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
(<span style="color: #b58900;">booleanp</span> nil) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
</pre>
</div>

<p>
Strongly typed languages like Haskell allow only a number of the type formers listed
above. For example, Haskell does not allow unions but instead offers so-called sum
types. Moreover, unlike Haskell, Lisp is non-parametric:
We may pick a branch of computation according to the type of a value.
Such case analysis is available in languages such as C# &#x2014;c.f.,
<a href="https://blogs.msdn.microsoft.com/ericlippert/2010/09/16/is-is-as-or-is-as-is/">is is as or is as is</a>. Finally, it is important to realise that <code>cons</code> is a monomorphic
type
&#x2014;it just means an (arbitrary) element consisting of two parts called <code>car</code> and <code>cdr</code>&#x2014;
we show how to form a <a href="#orged0cebc">polymorphic</a> product type below.
</p>

<p>
We may ask for <i>the</i> ‘primitive type’ of an object;
which is the simplest built-in type that it belongs to,
such as integer, string, cons, symbol, record, subr, and a few others.
As such, <i>Lisp objects come with an intrinsic primitive type</i>;
e.g., <code>'(1 "2" 'three)</code> is a list and can only be treated as a value of
another type if an explicit coercion is used.
In Lisp, rather than variables, it is values that are associated with a type.
One may optionally declare the types of variables, like in OCaml.
</p>
<div class="org-center">
<p>
<i>Lisp (primitive) types are inferred!</i>
</p>

<p>
“Values have types, not variables.” &#x2014;Paul Graham, ANSI Common Lisp
</p>
</div>

<p>
Let's review some important features of type systems and how they manifest themselves
in Lisp.
</p>
</div>

<div id="outline-container-org8b06411" class="outline-3">
<h3 id="type-checking"><span class="section-number-3">2.1</span> Obtaining &amp; Checking Types</h3>
<div class="outline-text-3" id="text-type-checking">
<p>
The typing relationship “:” is usually deterministic in its second argument for
static languages: <code>e : τ  ∧  e : τ′  ⇒  τ ≈ τ′</code>. However this is not the case with
Lisp's <code>typep</code>.
</p>

<table>
<caption class="t-bottom"><span class="table-number">Table 2:</span> Where are the types &amp; <i>when</i> are they checked?</caption>

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

<col  class="org-left">

<col  class="org-left">
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Style</th>
<th scope="col" class="org-left">Definition</th>
<th scope="col" class="org-left">Examples</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Static</td>
<td class="org-left">Variables have a fixed type; compile time</td>
<td class="org-left">Haskell &amp; C#</td>
</tr>

<tr>
<td class="org-left">Dynamic</td>
<td class="org-left">Values have a fixed type; runtime</td>
<td class="org-left">Lisp &amp; Smalltalk</td>
</tr>
</tbody>
</table>

<p>
In some sense, dynamic languages make it easy to produce <a href="#orged0cebc">polymorphic</a> functions.
Ironically, the previous sentences is only meaningful if you acknowledge the importance
of types and type variables.
</p>

<p>
In Lisp, types are inferred and needn't be declared.
However, the declaration serves as a nice documentation to further readers ;-)
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">setq</span> ellew 314)
(<span style="color: #b58900;">type-of</span> ellew) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; integer</span>

(<span style="color: #859900; font-weight: bold;">setq</span> ellew <span style="color: #2aa198;">"oh my"</span>)
(<span style="color: #b58900;">type-of</span> ellew) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; string</span>
</pre>
</div>
<ul class="org-ul">
<li>The <code>type-of</code> function returns the type of a given object.</li>
<li>Re variables: Static ⇒ only values can change; dynamic ⇒ both values and types change.</li>
</ul>

<p>
We may check the type of an item using <code>typep</code>, whose second argument
is a “type specifiers”
 &#x2014;an expressions whose value denotes a type; e.g., the <code>or</code> expression below
 forms a ‘union type’.
</p>

<p>
There's also <code>check-type</code>: It's like <code>typep</code> but instead of yielding true or
false, it stays quiet in the former and signals a type error in the latter.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">check-type</span> 12 integer)               <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil, i.e., no error</span>
(<span style="color: #859900; font-weight: bold;">check-type</span> 12   (<span style="color: #859900; font-weight: bold;">or</span> symbol integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">nil; i.e., no error</span>
(<span style="color: #859900; font-weight: bold;">check-type</span> <span style="color: #2aa198;">"12"</span> (<span style="color: #859900; font-weight: bold;">or</span> symbol integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Crash: Type error!</span>
</pre>
</div>

<p>
In summary:
</p>
<table>


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

<col  class="org-left">

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>(equal τ (type-of e))</code></td>
<td class="org-left"><code>≈</code></td>
<td class="org-left"><code>(typep e τ)</code></td>
</tr>

<tr>
<td class="org-left"><code>(check-type e τ)</code></td>
<td class="org-left"><code>≈</code></td>
<td class="org-left"><code>(unless (typep e 'τ) (error "⋯"))</code></td>
</tr>
</tbody>
</table>

<p>
( Note: (<code>unless x y) ≈ (when (not x) y)</code> .)
</p>
</div>
</div>
<div id="outline-container-org7dd53ce" class="outline-3">
<h3 id="lisp-is-eval"><span class="section-number-3">2.2</span> Statics &amp; Dynamics of Lisp</h3>
<div class="outline-text-3" id="text-lisp-is-eval">
<blockquote>
<p>
Types are the central organising principle of the theory of programming languages.
Language features are manifestations of type structure.
The syntax of a language is governed by the constructs that define its types, and
its semantics is determined by the interactions among those constructs.
</p>

<p>
&#x2014; Robert Harper, Practical Foundations for Programming Languages
</p>
</blockquote>

<p>
Besides atoms like numbers and strings,
the only way to form new terms in Lisp is using “modus ponens”,
or “function application”. Here's a first approximation:
</p>
<div class="org-src-container">
<pre class="src src-math">f : τ₁ → ⋯ → τₙ → τ   e₁ : τ₁  …  eₙ : τₙ
-----------------------------------------------------------------------------------------
           (f e₁ … eₙ) : τ
</pre>
</div>
<p>
One reads such a fraction as follows: If each part of the numerator &#x2014;the ‘hypothesises’&#x2014; is true, then so is the denominator &#x2014;the ‘conclusion’.
</p>

<p>
An <i>abstract syntax tree</i>, or ‘AST’, is a tree with operators for branches
and arguments for children. A tree is of kind τ if the topmost branching operator has τ as its resulting type. Here's an improved rule:
</p>
<div class="org-src-container">
<pre class="src src-math">f : τ₁ → ⋯ → τₙ → τ   e₁ : AST τ₁  …  eₙ : AST τₙ
-----------------------------------------------------------------------------------------
              (f e₁ … eₙ) : AST τ
</pre>
</div>

<p>
A Lisp top-level then may execute or interpret such a form to obtain a value:
When we write <code>e</code> at a top-level, it is essentially <code>(eval e)</code> that is invoked.
</p>
<div class="org-src-container">
<pre class="src src-math">   e : AST τ
-----------------------------------------------------------------------------------------
  (eval e) : τ
</pre>
</div>

<p>
However, we may also protect against evaluation.
</p>
<div class="org-src-container">
<pre class="src src-math">     e : AST τ
-----------------------------------------------------------------------------------------
  (quote e) : AST τ
</pre>
</div>

<p>
We have the following execution rules, where ‘⟿’ denotes “reduces to”.
</p>
<div class="org-src-container">
<pre class="src src-math">(eval a)         ⟿ a                        ;; for atom ‘a’
(eval (quote e))   ⟿ e
(eval (f e₁ … eₙ)) ⟿ (f (eval e₁) ⋯ (eval eₙ)) ;; Actually invoke ‘f’
</pre>
</div>

<div class="org-center">
<p>
<i>A conceptual model of Lisp is <code>eval</code>.</i>
</p>
</div>
</div>
</div>

<div id="outline-container-org48c022b" class="outline-3">
<h3 id="lisp-is-dynamic"><span class="section-number-3">2.3</span> Variable Scope</h3>
<div class="outline-text-3" id="text-lisp-is-dynamic">
<p>
There's also the matter of “scope”, or ‘life time’, of a variable.
</p>

<table>
<caption class="t-bottom"><span class="table-number">Table 3:</span> Local variables temporarily mask global names …</caption>

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

<col  class="org-left">

<col  class="org-left">
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Style</th>
<th scope="col" class="org-left">Definition</th>
<th scope="col" class="org-left">Examples</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Lexical</td>
<td class="org-left">… only in visible code</td>
<td class="org-left">Nearly every language!</td>
</tr>

<tr>
<td class="org-left">Dynamic</td>
<td class="org-left">… every place imaginable</td>
<td class="org-left">Bash, Perl, &amp; allowable in some Lisps</td>
</tr>
</tbody>
</table>

<p>
That is, dynamic scope means a local variable not only acts as a global variable
for the rest of the scope but it does so even in the definitions of pre-defined methods
being invoked in the scope.
</p>
<div class="org-src-container">
<pre class="src src-elisp">(<span style="color: #859900; font-weight: bold;">setq</span> it <span style="color: #2aa198;">"bye"</span>)
(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">go</span> () it)
(<span style="color: #859900; font-weight: bold;">let</span> ((it 3)) (go)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 3; even though &#8220;it&#8221; does not occur textually!</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Temporarily enable lexical binding in Emacs Lisp</span>
(<span style="color: #859900; font-weight: bold;">setq</span> <span style="color: #268bd2;">lexical-binding</span> <span style="color: #268bd2;">t</span>)
(<span style="color: #859900; font-weight: bold;">let</span> ((it 3)) (go)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; bye; as most languages would act</span>
</pre>
</div>

<div class="org-center">
<p>
<i>Dynamic scope lets bindings leak down into all constituents in its wake.</i>
</p>
</div>

<p>
That is fantastic when we want to do <a href="https://nullprogram.com/blog/2012/08/15/">unit tests</a> involving utilities with side-effects:
We simply locally re-define the side-effect component to, say, do nothing. (─‿‿─)
</p>
</div>
</div>

<div id="outline-container-org21ae3d3" class="outline-3">
<h3 id="lisp-is-strong"><span class="section-number-3">2.4</span> Casts &amp; Coercions</h3>
<div class="outline-text-3" id="text-lisp-is-strong">
<table>
<caption class="t-bottom"><span class="table-number">Table 4:</span> The frequency of implicit type coercions</caption>

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

<col  class="org-left">

<col  class="org-left">
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Style</th>
<th scope="col" class="org-left">Definition</th>
<th scope="col" class="org-left">Examples</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">Strong</td>
<td class="org-left">Almost never</td>
<td class="org-left">Lisp &amp; Haskell</td>
</tr>

<tr>
<td class="org-left">Weak</td>
<td class="org-left">Try as best as possible</td>
<td class="org-left">JavaScript &amp; C</td>
</tr>
</tbody>
</table>

<p>
<i>Strong systems will not accidentally coerce terms.</i>
</p>

<p>
Lisp has a <a href="http://www.lispworks.com/documentation/lw51/CLHS/Body/f_coerce.htm#coerce">coerce</a> form; but coercion semantics is generally unsound
in any language and so should be used with tremendous caution.
( Though Haskell has some sensible coercions as well as unsafe one. )
</p>
<div class="org-src-container">
<pre class="src src-math">     e : α
----------------------------------------------------------------------------------------
(coerce e β) : β
</pre>
</div>
<p>
We have a magical way to turn elements of type α to elements of type β.
Some languages call this <i>type casting</i>.
</p>

<p>
Here's a cute example.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">coerce</span> '(76 105 115 112) '<span style="color: #b58900;">string</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; Lisp</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org768ee6d" class="outline-3">
<h3 id="type-annotations"><span class="section-number-3">2.5</span> Type Annotations</h3>
<div class="outline-text-3" id="text-type-annotations">
<p>
We may perform type annotations using the form <code>the</code>; e.g.,
the Haskell expression <code>(1 :: Int) + 2</code> checks the type annotation,
and, if it passes, yields the value and the expression is computed.
Likewise, <code>(the type name)</code> yields <code>name</code> provided it has type <code>type</code>.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">+</span> (<span style="color: #859900; font-weight: bold;">the</span> integer 1)
   (<span style="color: #859900; font-weight: bold;">the</span> integer 2)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 3</span>

(<span style="color: #b58900;">+</span> (<span style="color: #859900; font-weight: bold;">the</span> integer 1)
   (<span style="color: #859900; font-weight: bold;">the</span> integer <span style="color: #2aa198;">"2"</span>)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; Type error.</span>
</pre>
</div>

<p>
Computationally, using <code>or</code> as a control structure for lazy sequencing with left-unit <code>nil</code>:
</p>
<table>


<colgroup>
<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>(the τ e) ≈ (or (check-type e τ) e)</code></td>
</tr>
</tbody>
</table>
</div>
</div>

<div id="outline-container-orgdc8226f" class="outline-3">
<h3 id="typecase"><span class="section-number-3">2.6</span> Type-directed Computations</h3>
<div class="outline-text-3" id="text-typecase">
<p>
Sometimes a value can be one of several types.
This is specified using union types; nested unions are essentially flattened
&#x2014;which is a property of ‘or’, as we shall come to see.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span> 12 'integer)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
(<span style="color: #b58900;">typep</span> 'a 'symbol)   <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

(<span style="color: #859900; font-weight: bold;">setq</span> woah 12)
(<span style="color: #b58900;">typep</span> woah '(<span style="color: #859900; font-weight: bold;">or</span> integer symbol)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

(<span style="color: #859900; font-weight: bold;">setq</span> woah 'nice)
(<span style="color: #b58900;">typep</span> woah '(<span style="color: #859900; font-weight: bold;">or</span> integer symbol)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div>

<p>
When given a union type, we may want to <i>compute according to the type of a value.</i>
</p>
<ul class="org-ul">
<li>Case along the possible types using <code>typecase</code>.</li>
<li>This returns a <code>nil</code> when no case fits; use <code>etypecase</code> to have an error instead of <code>nil</code>.</li>
</ul>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">typecase</span> woah
  (integer  (+1 woah))
  (symbol  'cool)
  (<span style="color: #268bd2;">t</span>       <span style="color: #2aa198;">"yikes"</span>))
</pre>
</div>
</div>
</div>

<div id="outline-container-orgcc35fca" class="outline-3">
<h3 id="type-specifiers"><span class="section-number-3">2.7</span> Type Specifiers: On the nature of types in Lisp</h3>
<div class="outline-text-3" id="text-type-specifiers">
<blockquote>
<p>
Types are not objects in Common Lisp. There is no object that corresponds to the type
<code>integer</code>, for example. What we get from a function like <code>type-of</code>, and give as an argument
to a function like <code>typep</code>, is not a type, but a type specifier.
A type specifier is the name of a type. &#x2014;Paul Graham, ANSI Common Lisp
</p>
</blockquote>

<p>
Type specifiers are essentially transformed into predicates as follows.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span> x '&#964;)                &#8776; (&#964;p x)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">E.g., &#964; &#8776; integer</span>
(<span style="color: #b58900;">typep</span> x '(<span style="color: #859900; font-weight: bold;">and</span> &#964;&#8321; &#8230; &#964;&#8345;))    &#8776; (<span style="color: #859900; font-weight: bold;">and</span> (<span style="color: #b58900;">typep</span> x &#964;&#8321;) &#8230; (<span style="color: #b58900;">typep</span> x &#964;&#8345;))
(<span style="color: #b58900;">typep</span> x '(<span style="color: #859900; font-weight: bold;">or</span> &#964;&#8321; &#8230; &#964;&#8345;))     &#8776; (<span style="color: #859900; font-weight: bold;">or</span> (<span style="color: #b58900;">typep</span> x &#964;&#8321;) &#8230; (<span style="color: #b58900;">typep</span> x &#964;&#8345;))
(<span style="color: #b58900;">typep</span> x '(<span style="color: #b58900;">not</span> &#964;))          &#8776; (<span style="color: #b58900;">not</span> (<span style="color: #b58900;">typep</span> x &#964;))
(<span style="color: #b58900;">typep</span> x '(<span style="color: #b58900;">member</span> e&#8321; &#8230; e&#8345;)) &#8776; (<span style="color: #859900; font-weight: bold;">or</span> (<span style="color: #b58900;">eql</span> x e&#8321;) &#8230; (<span style="color: #b58900;">eql</span> x e&#8345;))
(<span style="color: #b58900;">typep</span> x '(satisfies p))    &#8776; (p x)
</pre>
</div>

<p>
Type specifiers are thus essentially ‘characteristic functions’ from mathematics.
</p>
</div>
</div>

<div id="outline-container-orgc13bb34" class="outline-3">
<h3 id="deftype"><span class="section-number-3">2.8</span> Making New Types with <code>deftype</code></h3>
<div class="outline-text-3" id="text-deftype">
<p>
If we use a type specifier often, we may wish to abbreviate it using
the <a href="https://www.gnu.org/software/emacs/manual/html_mono/cl.html#index-cl_002ddeftype-14">deftype</a> macro &#x2014;it is like <code>defmacro</code> but expands into a type specifier
instead of an expression.
</p>

<p>
We can define new types that will then work with <code>typecase</code> and friends
as follows:
</p>
<ol class="org-ol">
<li>Define a predicate <code>my-type-p</code>.</li>
<li>Test it out to ensure only the elements you want satisfy it.</li>
<li><p>
Register it using <a href="https://www.gnu.org/software/emacs/manual/html_mono/cl.html#index-cl_002ddeftype-14">deftype</a>.
</p>

<p>
You could just do number 3 directly, but it's useful to have the
predicate form of a <a href="#org9adf3f1">type descriptor</a>.
</p></li>
</ol>

<p>
<a href="https://lispcookbook.github.io/cl-cookbook/type.html">For example,</a> here's the three steps for a type of lists of numbers drawn from <code>(-∞..9]</code>.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Make the predicate</span>
(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">small-number-seq-p</span> (thing)
  (<span style="color: #859900; font-weight: bold;">and</span> (<span style="color: #b58900;">sequencep</span> thing)
       (<span style="color: #b58900;">every</span> #'<span style="color: #b58900;">numberp</span> thing)
       (<span style="color: #b58900;">every</span> (<span style="color: #859900; font-weight: bold;">lambda</span> (x) (<span style="color: #b58900;">&lt;</span> x 10)) thing)))

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Test it</span>
(<span style="color: #859900; font-weight: bold;">setq</span> yes '(1 2  4))
(<span style="color: #859900; font-weight: bold;">setq</span> no  '(1 20 4))
(small-number-seq-p yes) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Register it</span>
(<span style="color: #859900; font-weight: bold;">deftype</span> small-number-seq ()
  '(satisfies small-number-seq-p))

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Use it</span>
(<span style="color: #b58900;">typep</span> yes 'small-number-seq) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> no 'small-number-seq)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
</pre>
</div>

<p>
Arguments are processed the same as for <code>defmacro</code> except that optional
arguments without explicit defaults use <code>*</code> instead of <code>nil</code> as the default value.
<a href="https://www.gnu.org/software/emacs/manual/html_mono/cl.html#index-cl_002ddeftype-14">From the deftype docs, here are some examples:</a>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">cl-deftype</span> <span style="color: #b58900; font-style: italic;">null</span> () '(satisfies <span style="color: #b58900;">null</span>))    <span style="color: #96A7A9; font-style: italic;">; </span><span style="color: #96A7A9; font-style: italic;">predefined</span>
(<span style="color: #859900; font-weight: bold;">cl-deftype</span> <span style="color: #b58900; font-style: italic;">list</span> () '(<span style="color: #859900; font-weight: bold;">or</span> <span style="color: #b58900;">null</span> <span style="color: #b58900;">cons</span>))      <span style="color: #96A7A9; font-style: italic;">; </span><span style="color: #96A7A9; font-style: italic;">predefined</span>

(<span style="color: #859900; font-weight: bold;">cl-deftype</span> <span style="color: #b58900; font-style: italic;">unsigned-byte</span> (<span style="color: #b58900; font-style: italic;">&amp;optional</span> bits)
  (<span style="color: #b58900;">list</span> 'integer 0 (<span style="color: #859900; font-weight: bold;">if</span> (<span style="color: #b58900;">eq</span> bits '<span style="color: #b58900;">*</span>) bits (<span style="color: #b58900;">1-</span> (<span style="color: #b58900;">lsh</span> 1 bits)))))

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Some equivalences</span>
(unsigned-byte 8)  &#8801;  (integer 0 255)
(unsigned-byte)    &#8801;  (integer 0 <span style="color: #b58900;">*</span>)
unsigned-byte      &#8801;  (integer 0 <span style="color: #b58900;">*</span>)
</pre>
</div>

<ul class="org-ul">
<li>Notice that type specifiers essentially live in their own namespace; e.g., <code>null</code> is the
predicate that checks if a list is empty yet <code>null</code> is the type specifying such lists.</li>
</ul>

<p>
Let's form a type of pairs directly &#x2014;which is not ideal!
This is a <a id="orged0cebc">polymorphic</a> datatype: It takes two type arguments, called <code>a</code> and <code>b</code> below.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">deftype</span> pair (a b <span style="color: #b58900; font-style: italic;">&amp;optional</span> type)
  `(satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (x) (<span style="color: #859900; font-weight: bold;">and</span>
      (<span style="color: #b58900;">consp</span> x)
      (<span style="color: #b58900;">typep</span> (<span style="color: #b58900;">car</span> x) (<span style="color: #859900; font-weight: bold;">quote</span> ,a))
      (<span style="color: #b58900;">typep</span> (<span style="color: #b58900;">cdr</span> x) (<span style="color: #859900; font-weight: bold;">quote</span> ,b))))))

(<span style="color: #b58900;">typep</span> '(<span style="color: #2aa198;">"x"</span> . 2) '(pair <span style="color: #b58900;">string</span> integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> '(<span style="color: #2aa198;">"x"</span> . 2) '(pair symbol integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
(<span style="color: #b58900;">typep</span> nil '(pair integer integer))       <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
(<span style="color: #b58900;">typep</span> 23 '(pair integer integer))        <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>

(<span style="color: #859900; font-weight: bold;">setq</span> ss <span style="color: #2aa198;">"nice"</span> nn 114)
(<span style="color: #b58900;">typep</span> `(,ss . ,nn) '(pair <span style="color: #b58900;">string</span> integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> (<span style="color: #b58900;">cons</span> ss nn) '(pair <span style="color: #b58900;">string</span> integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">The following are false since ss and nn are quoted symbols!</span>
(<span style="color: #b58900;">typep</span> '(ss . nn)    '(pair <span style="color: #b58900;">string</span> integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
(<span style="color: #b58900;">typep</span> `(<span style="color: #b58900;">cons</span> ss nn) '(pair <span style="color: #b58900;">string</span> integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
</pre>
</div>

<p>
<b>Exercise:</b> Define the <a href="#orged0cebc">polymorphic</a> <code>maybe</code> type
such that <code>(maybe τ)</code> has elements being either <code>nil</code> or a value of <code>τ</code>.
</p>
<p>
Let's define type <code>list-of</code> such that <code>(list-of τ)</code> is the type of lists
whose elements are all values of type <code>τ</code>.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Make the predicate</span>
(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">list-of-p</span> (&#964; thing)
  (<span style="color: #859900; font-weight: bold;">and</span> (<span style="color: #b58900;">listp</span> thing) (<span style="color: #b58900;">every</span> (<span style="color: #859900; font-weight: bold;">lambda</span> (x) (<span style="color: #b58900;">typep</span> x &#964;)) thing)))

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Test it</span>
(list-of-p 'integer '(1 2   3)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(list-of-p 'integer '(1 two 3)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
(list-of-p '<span style="color: #b58900;">string</span> '())         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(list-of-p '<span style="color: #b58900;">string</span> '(no))       <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Register it</span>
(<span style="color: #859900; font-weight: bold;">deftype</span> list-of (&#964;)
  `(satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (thing) (list-of-p (<span style="color: #859900; font-weight: bold;">quote</span> ,&#964;) thing))))

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Use it</span>

(<span style="color: #b58900;">typep</span> '(1 2  ) '<span style="color: #b58900;">list</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> '(1 two) '<span style="color: #b58900;">list</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

(<span style="color: #b58900;">typep</span> '(1 2)   '(list-of integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> '(1 <span style="color: #2aa198;">"2"</span>) '(list-of <span style="color: #b58900;">string</span>))  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
(<span style="color: #b58900;">typep</span> '(1 <span style="color: #2aa198;">"2"</span>) '(list-of (<span style="color: #859900; font-weight: bold;">or</span> integer <span style="color: #b58900;">string</span>)))  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
</pre>
</div>

<p>
Notice that by the last example we can <b>control the degree of heterogeneity</b> in our lists!
So cool!
</p>

<p>
Here's some more exercises. The first should be nearly trivial, the second a bit more
work, and the last two have made me #sad.
</p>

<ol class="org-ol">
<li>Define a type <code>(rose τ)</code> whose elements are either τ values or rose trees of type τ.</li>

<li>Define a type <code>record</code> so that <code>(record τ₁ … τₙ)</code> denotes a record type whose iᵗʰ
component has type <code>τᵢ</code>.</li>

<li><p>
Define a type constructor <code>∃</code> such that, for example, <code>(∃ τ (pair integer τ)</code>
denotes the type of pairs where the first components are integers and the second
components all have the same type <code>τ</code>, but we do not know which one.
</p>

<p>
My idea was to let <code>τ</code> denote the type of the first occurrence of a value
at that location, then all subsequent checks now refer to this value of <code>τ</code>.
</p>

<p>
Sadly, I could not define this type :'(
</p>

<p>
Upon further reading, this may be doable using a <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Watching-Variables.html#Watching-Variables">variable watcher</a>.
</p></li>

<li><p>
Produce a record for monoids and keep-track of the monoid instances produced.
Define a the predicate <code>(monoid τ)</code> to check if any of the monoid instances
has <code>τ</code> as its carrier type. In this way we could simulate Haskell typeclasses.
</p></li>
</ol>

<p>
Let me know if you do cool things!
</p>
</div>
</div>
<div id="outline-container-orgdaad8d9" class="outline-3">
<h3 id="adts"><span class="section-number-3">2.9</span> Algebraic Data Types a la Haskell</h3>
<div class="outline-text-3" id="text-adts">
<p>
Consider the Haskell expression type, example, and integer evaluator.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">data</span> <span style="color: #b58900; font-style: italic;">Expr</span> a <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Var</span> a <span style="color: #268bd2;">|</span> <span style="color: #b58900; font-style: italic;">Expr</span> a <span style="color: #b58900; font-style: italic;">:+:</span> <span style="color: #b58900; font-style: italic;">Expr</span> a <span style="color: #268bd2;">|</span> <span style="color: #b58900; font-style: italic;">Neg</span> (<span style="color: #b58900; font-style: italic;">Expr</span> a) <span style="color: #859900; font-weight: bold;">deriving</span> <span style="color: #b58900; font-style: italic;">Show</span>

<span style="color: #b58900;">ex</span> <span style="color: #268bd2;">::</span> <span style="color: #b58900; font-style: italic;">Expr</span> <span style="color: #b58900; font-style: italic;">Int</span>
<span style="color: #b58900;">ex</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Var</span> 5 <span style="color: #b58900; font-style: italic;">:+:</span> (<span style="color: #b58900; font-style: italic;">Var</span> 6 <span style="color: #b58900; font-style: italic;">:+:</span> <span style="color: #b58900; font-style: italic;">Neg</span> (<span style="color: #b58900; font-style: italic;">Var</span> 7))

<span style="color: #b58900;">int</span> <span style="color: #268bd2;">::</span> <span style="color: #b58900; font-style: italic;">Expr</span> <span style="color: #b58900; font-style: italic;">Int</span> <span style="color: #268bd2;">-&gt;</span> <span style="color: #b58900; font-style: italic;">Int</span>
<span style="color: #b58900;">int</span> (<span style="color: #b58900; font-style: italic;">Var</span> n)    <span style="color: #268bd2;">=</span> n
<span style="color: #b58900;">int</span> (l <span style="color: #b58900; font-style: italic;">:+:</span> r)  <span style="color: #268bd2;">=</span> int l <span style="color: #268bd2;">+</span> int r
<span style="color: #b58900;">int</span> (<span style="color: #b58900; font-style: italic;">Neg</span> e)    <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">-</span> (int e)

<span style="color: #96A7A9; font-style: italic;">{- </span><span style="color: #96A7A9; font-style: italic;">int ex &#8658; 4 -}</span>
</pre>
</div>

<p>
If we view a constructor declaration <code>C a₁ … aₙ</code> with superfluous parenthesis
as <code>(C a₁ … aₙ)</code>, then a translation to Lisp immediately suggests itself:
</p>
<div class="org-center">
<p>
<i>Haskell constructors ≅ Lisp lists whose <code>car</code> are constructor names</i>
</p>
</div>

<p>
A nearly direct translation follows.
</p>
<div class="org-src-container">
<pre class="src src-lisp">(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">exprp</span> (&#964; thing)
    (pcase thing
       (`(var ,n)    (typep n &#964;))
       (`(add ,l ,r) (and (exprp &#964; l) (exprp &#964; r)))
       (`(neg ,e)    (exprp &#964; e))))

(setq ex '(add (var 5) (add (var 6) (neg (var 7)))))
(exprp 'integer ex) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

<span style="color: #96A7A9; font-style: italic;">; </span><span style="color: #96A7A9; font-style: italic;">This declarion &#8220;declare-type&#8221; is defined near the end of this article.</span>
(declare-type int : (expr-of integer) integer)
(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">int</span> (thing)
    (pcase thing
       (`(var ,n)    n)
       (`(add ,l ,r) (+ (int l) (int r)))
       (`(neg ,e)    (- (int e)))))

(int ex) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 4</span>
</pre>
</div>

<p>
There are of-course much better ways to do this in Lisp; e.g.,
use <code>identity, +, -</code> in-place of the <code>var, add, neg</code> tags
to produce “syntax that carries its semantics”
or express the interpreter <code>int</code> as a one liner
by replacing the formal tags with their interpretations then
invoking Lisps <code>eval</code>. I doubt either of these are new ideas,
but the merit of the former seems neat &#x2014;at a first glance, at least.
</p>

<p>
Support for ADTs in Common Lisp along with seemingly less clunky pattern matching
can be found <a href="https://github.com/stylewarning/cl-algebraic-data-type">here</a> &#x2014;which I have only briefly looked at.
</p>

<p>
The Haskell presentation has type-checking baked into it, yet our
Lisp interpreter <code>int</code> does not! This seems terribly worrying, but
that <a href="#org84d2c90"><code>declare-type</code></a> declaration actually handles type checking for us!
</p>
<div class="org-src-container">
<pre class="src src-lisp"><span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Register the type</span>
(<span style="color: #859900; font-weight: bold;">deftype</span> <span style="color: #b58900; font-style: italic;">expr-of</span> (&#964;)
  `(satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (thing) (exprp (quote ,&#964;) thing))))

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Try it out</span>
(typep '(1 2)   '(expr-of integer)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>
(typep ex   '(expr-of integer))     <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">This invocation, for example, now yields a helpful error message.</span>
(int '(var 6 4))
<span style="color: #96A7A9; font-style: italic;">;;</span>
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; int: Type mismatch! Expected (expr-of integer) for argument 0 &#8800; Given cons (var 6 4).</span>
<span style="color: #96A7A9; font-style: italic;">;;</span>
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Which is reasonable since the &#8216;</span><span style="color: #6c71c4; font-weight: bold; font-style: italic;">var</span><span style="color: #96A7A9; font-style: italic;">&#8217; constructor only takes a single argument.</span>
</pre>
</div>
<p>
Notice that invalid cases yield a helpful (run-time) error message!
</p>
</div>
</div>
</div>

<div id="outline-container-org714b51d" class="outline-2">
<h2 id="why-dynamic"><span class="section-number-2">3</span> In Defence of Being Dynamically Checked</h2>
<div class="outline-text-2" id="text-why-dynamic">
<div class="org-center">
<p>
<i>Lisp gets a bad rap for being untyped; let's clarify this issue further!</i>
</p>
</div>

<p>
It is important to realise that nearly every language is typed &#x2014;albeit the checking
may happen at different stages&#x2014; and so, as <a href="http://www.cis.upenn.edu/~bcpierce/tapl/index.html">Benjamin Pierce</a> says:
<i>Terms like “dynamically typed” are arguably misnomers and should probably be replaced by “dynamically checked,” but the usage is standard.</i>
</p>

<p>
In particular, dynamically typed is <i>not</i> synonymous with untyped, though some people use
it that way since nearly <a href="https://www.williamjbowman.com/blog/2018/01/19/untyped-programs-don-t-exist/#related">every language is typed</a> &#x2014;possibly with a single anonymous
type.
</p>

<p>
Some people in the Haskell community, which I love, say things like
<i>“if it typechecks, ship it”</i> which is true more often than not, but it leads some
people to avoid producing unit tests. For example, the following type checks but
should be unit tested.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900;">mcbride</span> <span style="color: #268bd2;">::</span> [<span style="color: #b58900; font-style: italic;">Int</span>] <span style="color: #268bd2;">-&gt;</span> <span style="color: #b58900; font-style: italic;">Int</span>
<span style="color: #b58900;">mcbride</span> xs <span style="color: #268bd2;">=</span> <span style="color: #859900; font-weight: bold;">if</span> null xs <span style="color: #859900; font-weight: bold;">then</span> head xs <span style="color: #859900; font-weight: bold;">else</span> 666
</pre>
</div>

<p>
Regardless, I love static type checking and static analysis in general.
However, the shift to a dynamically checked setting has resulted in greater
interest in unit testing. For example, Haskell's solution to effectful computation
is delimited by types, as any Haskeller will proudly say (myself included);
but ask how are such computations unit tested and the room is
silent (myself included).
</p>

<p>
Interestingly some unit tests check the typing of inputs and output, which is
a mechanical process with no unknowns and so it should be possible to produce a syntax
for it using Lisp macros. This is one of the goals of this article and we'll return to
it later.
</p>

<p>
Even though I like Lisp, I'm not sure why dynamic typing is the way to go
&#x2014;c.f. <a href="https://existentialtype.wordpress.com/2011/03/19/dynamic-languages-are-static-languages/">Dynamic Languages are Static Languages</a> which mentions the unjust tyranny of
unityped systems.
Below are two reasons why people may dislike static types.
</p>

<p>
<b>First</b>: The de-facto typing rule do binary choice is usually:
</p>
<div class="org-src-container">
<pre class="src src-math">     T : 𝔹     E : α     B : α -----------------------------------------------------------------------------------------
     if T then E else B : α
</pre>
</div>

<p>
That means valid programs such as <code>if True then 1 else "two"</code> are rejected;
even though the resulting type will always be an integer there is no way to know
that statically &#x2014;the choice needs to be rewritten, evaluated at run time.
</p>

<p>
Indeed, in Haskell, we would write
<code>if True then Left 1 else Right "two"</code> which has type <code>Either Int String</code>,
and to use the resulting value means we need to pattern match or use
the eliminator <code>(|||</code>) &#x2014;from Haskell's <code>Control.Arrow</code>.
</p>

<p>
<b>Second:</b>
Some statically typed languages have super weak type systems and ruin the rep
for everyone else.
For example, <code>C</code> is great and we all love it of-course, but it's a shame that we can only
express the <a href="#orged0cebc">polymorphic</a> identity function \(id : ∀{α}. α → α \;=\; λ x → x\),
by using the C-preprocessor &#x2014;or dismiss the types by casting pointers around.
</p>

<p>
Maybe this video is helpful, maybe not:
<a href="https://games.greggman.com/game/dynamic-typing-static-typing/">The Unreasonable Effectiveness of Dynamic Typing for Practical Programs</a>
</p>

<blockquote>
<p>
( For the algebraist: Dynamic typing is like working in a monoid whose
composition operation is partial and may abruptly crash; whereas static typing
is working in a category whose composition is proudly typed. )
</p>
</blockquote>

<p>
Overall I haven't presented a good defence for being dynamically checked, but you
should ignore my blunder and consider trying Lisp yourself to see how awesome it is.
</p>
</div>
</div>

<div id="outline-container-orgbe67c8a" class="outline-2">
<h2 id="lisp-funny-history"><span class="section-number-2">4</span> With its hierarchy of types, why isn't Lisp statically typed?</h2>
<div class="outline-text-2" id="text-lisp-funny-history">
<div class="org-center">
<p>
<i>I haven't a clue. Here are two conjectures.</i>
</p>
</div>

<p>
<b>First</b>: Code that manipulates code is difficult to type.
</p>

<p>
Is the type of <code>'(+ x 2)</code> a numeric code expression?
Or just an arbitrary code expression? Am I allowed to “look inside”
to inspect its structure or is it a black box? What about the nature of
its constituents? If I'm allowed to look at them, can I ask if they're even defined?
</p>

<p>
What if <code>c</code> is a code element that introduces an identifier, say <code>it</code>.
What is type of <code>c</code>? What if it doesn't introduce and thus avoids accidentally
capturing identifiers? Are we allowed only one form or both? Which do we select
and why?
</p>

<p>
I may be completely wrong, but below I mention a <a href="#orgd4b9b6a">bunch of papers</a> that suggest
it's kind hard to type this stuff.
</p>

<p>
<b>Second</b>: The type theory just wasn't in place at the time Lisp was created.
</p>

<p>
Here's a probably wrong account of how it went down.
</p>

<dl class="org-dl">
<dt>1913ish</dt><dd>Bertrand Russel introduces a hierarchy of types to avoid barber trouble;
e.g., <code>Typeᵢ : Typeᵢ₊₁</code>.</dd>
<dt>1920s</dt><dd>A Polish guy &amp; British guy think that's dumb and collapse the hierarchy.</dd>
<dt>1940s</dt><dd>Alonzo Church says arrows are cool.</dd>
<dt>1958 </dt><dd><p>
With his awesome hairdo, John McCarthy gifts the world an elegant
piece of art: Lisp (•̀ᴗ•́)و
</p>
<ul class="org-ul">
<li>Lisp is currently the 2ⁿᵈ oldest high-level language still
in use after Fortran.</li>
<li>Maxwell's equations <a href="https://queue.acm.org/detail.cfm?id=1039523">get</a> <a href="http://www.michaelnielsen.org/ddi/lisp-as-the-maxwells-equations-of-software/">jealous</a>.</li>
</ul>

<p>
Lisp introduces a bunch of zany ideas to CS:
</p>
<ul class="org-ul">
<li>Introduced if-then-else “McCarthy's Conditional”; 1ˢᵗ class functions &amp; recursion</li>
<li>macros ≈ compiler plugins</li>
<li>symbols ≈ raw names which needn't have values</li>
<li>variables ≈ pointers</li>
<li>code ≈ data; statements ≈ expressions</li>
<li><code>read, eval, load, compile, print</code> are all functions!</li>
</ul></dd>

<dt>1959</dt><dd>My man JM thinks manual memory is lame &#x2014;invents garbage collection!
<ul class="org-ul">
<li>Later, 2001, he writes <a href="https://web.archive.org/web/20130814213421/http://www-formal.stanford.edu/jmc/robotandbaby/robotandbaby.html">The Robot &amp; The Baby</a>.</li>
</ul></dd>
<dt>1960s</dt><dd>Simula says OOPs!</dd>
<dt>1970s</dt><dd>Smalltalk popularises the phrase “oop”. ( B has a child named C. )</dd>
<dt>1970s</dt><dd>Simple λ-calculus is a fashion model for sets and functions.</dd>
<dt>1970s</dt><dd>Milner and friends demand
<i>variables are for types too, not just terms!</i></dd>
<dt>1970s</dt><dd>Per Martin-Löf tells us it's okay to depend on one another; <code>Π, Σ</code> types.</dd>
<dt>1982 </dt><dd>A Lisp <a href="https://en.wikipedia.org/wiki/Ummah">ummah</a> is formed: <a href="http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node1.html">“Common Lisp the Language”</a> ♥‿♥
<ul class="org-ul">
<li>In order to be hip &amp; modern, it's got <a href="https://extravagaria.com/Files/LASC-Overview.pdf">class</a> with <a href="https://en.wikipedia.org/wiki/Common_Lisp_Object_System">CLOS</a>.</li>
<li>Other shenanigans: Scheme 1975, Elisp 1985, Racket 1995, Clojure 2007</li>
</ul></dd>
<dt>1984</dt><dd>A script of sorcerous schemes lords lisp over mere mortals</dd>
<dt>1990s</dt><dd>A committee makes a sexy <a href="https://en.wiktionary.org/wiki/a_camel_is_a_horse_designed_by_a_committee">camel</a> named Haskell; Professor X's school make their own camel.
<ul class="org-ul">
<li>Their kids get on steroids and fight to this day; Agda ↯↯↯ Coq.</li>
</ul></dd>
<dt>2000s</dt><dd><p>
X's camel .&lt;becomes .~(self .&lt;aware&gt;.)&gt;.
&#x2014;the other camel [does| the same].
</p>
<ul class="org-ul">
<li>In 2015, the cam ls married Lisp and <a href="https://luxlang.gitbooks.io/the-lux-programming-language/content/">Lux</a> was born.</li>
<li>In 2016, Haskell &amp; Lisp get involved with Prolog; <a href="https://shen-language.github.io/">Shen</a> is born.</li>
</ul>

<p>
2019: Coq is <a href="https://github.com/MetaCoq/metacoq">self-aware</a>; Agda is <a href="https://github.com/alhassy/gentle-intro-to-reflection">playing</a> <a href="https://alhassy.github.io/next-700-module-systems-proposal/prototype/PackageFormer.html">catch-up</a>.
</p></dd>
</dl>

<p>
A more informative historical account of Lisp &amp; its universal reverence can be read at:
<a href="https://twobithistory.org/2018/10/14/lisp.html">How Lisp Became God's Own Programming Language</a>.
</p>

<figure>
<img src="https://imgs.xkcd.com/comics/lisp.jpg" alt="lisp.jpg">

<figcaption><span class="figure-number">Figure 3: </span>xkcd</figcaption>
</figure>
</div>
</div>

<div id="outline-container-orge703a21" class="outline-2">
<h2 id="lisp-is-typed"><span class="section-number-2">5</span> Lisp Actually Admits Static Typing!</h2>
<div class="outline-text-2" id="text-lisp-is-typed">
<p>
Besides Common Lisp, “Typed Lisps” include <a href="https://github.com/clojure/core.typed">an optional type system for Clojure</a>
&#x2014;see also <a href="https://circleci.com/blog/why-were-no-longer-using-core-typed/">Why we're no longer using Core.typed</a>&#x2014;
<a href="https://docs.racket-lang.org/ts-guide/">Typed Racket</a>, and, more recently, <a href="https://github.com/LuxLang/lux">Lux</a> ≈ Haskell + ML + Lisp
and  <a href="https://shen-language.github.io/">Shen</a> ≈ Haskell + Prolog + Lisp.
</p>

<p>
<a href="https://news.ycombinator.com/item?id=8593261">For example,</a> Common Lisp admits strong static typing, in <a href="http://www.lispforum.com/viewtopic.php?f=2&amp;t=191">SBCL</a>, as follows.
</p>
<div class="org-src-container">
<pre class="src src-common-lisp">  <span style="color: #96A7A9; font-style: italic;">; Type declaration then definition.</span>
  (<span style="color: #859900; font-weight: bold;">declaim</span> (ftype (function (fixnum)) num-id))
  (<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">num-id</span> (n) n)

  (<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">string-id</span> (s) (<span style="color: #859900; font-weight: bold;">declare</span> (string s)) (num-id s))
  <span style="color: #96A7A9; font-style: italic;">; in: DEFUN STRING-ID</span>
  <span style="color: #96A7A9; font-style: italic;">;     (NUM-ID S)</span>
  <span style="color: #96A7A9; font-style: italic;">;</span>
  <span style="color: #96A7A9; font-style: italic;">; caught WARNING:</span>
  <span style="color: #96A7A9; font-style: italic;">;   Derived type of S is</span>
  <span style="color: #96A7A9; font-style: italic;">;     (VALUES STRING &amp;OPTIONAL),</span>
  <span style="color: #96A7A9; font-style: italic;">;   conflicting with its asserted type</span>
  <span style="color: #96A7A9; font-style: italic;">;     FIXNUM.</span>
</pre>
</div>

<p>
Such annotations mostly serve as compiler optimisation annotations and,
unfortunately, Emacs Lisp <a href="https://www.gnu.org/software/emacs/manual/html_node/cl/Declarations.html">silently ignores Common Lisp declarations such as ftype</a>
&#x2014;which provides <a href="http://www.lispworks.com/documentation/lw51/CLHS/Body/d_ftype.htm#ftype">function type</a> declarations.
However,
Emacs Lisp does provide a method of <a href="http://www.p-cos.net/documents/filtered-dispatch.pdf">dispatch</a> filtered by <a href="http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html">classes</a> rather than by
simple types. Interestingly, Lisp methods are more like Haskell typeclass constituents
or C# extensible methods
rather than like Java object methods &#x2014;in that, <i>Lisp methods specialise on classes</i>
whereas Java's approach is <i>classes have methods</i>.
</p>

<p>
Here's an example.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">defmethod</span> <span style="color: #b58900;">doit</span> ((n integer)) <span style="color: #35a69c; font-style: italic;">"I'm an integer!"</span>)
(<span style="color: #859900; font-weight: bold;">defmethod</span> <span style="color: #b58900;">doit</span> ((s <span style="color: #b58900;">string</span>)) <span style="color: #35a69c; font-style: italic;">"I'm a string!"</span>)
(<span style="color: #859900; font-weight: bold;">defmethod</span> <span style="color: #b58900;">doit</span> ((type (<span style="color: #b58900;">eql</span> <span style="color: #d33682; font-style: italic;">:hero</span>)) thing) <span style="color: #35a69c; font-style: italic;">"I'm a superhero!"</span>)

(doit 2)             <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; I'm an integer!</span>
(doit <span style="color: #2aa198;">"2"</span>)           <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; I'm a string!</span>
(doit 'x)            <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; Error: No applicable method</span>
(doit <span style="color: #d33682; font-style: italic;">:hero</span> 'bobert) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; I'm a superhero!</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">C-h o cl-defmethod &#8658; see extensible list of specialisers Elisp supports.</span>
</pre>
</div>

<p>
We can of-course make our own classes:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">defclass</span> <span style="color: #b58900; font-style: italic;">person</span>  () ((name)))
(<span style="color: #859900; font-weight: bold;">defmethod</span> <span style="color: #b58900;">speak</span> ((x person)) (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"My name is %s."</span> (<span style="color: #b58900;">slot-value</span> x 'name)))
(<span style="color: #859900; font-weight: bold;">setq</span> p (<span style="color: #b58900;">make-instance</span> 'person))
(<span style="color: #859900; font-weight: bold;">setf</span> (<span style="color: #b58900;">slot-value</span> p 'name) <span style="color: #2aa198;">"bobert"</span>)
(speak p) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; My name is bobert.</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Inherits from &#8216;</span><span style="color: #6c71c4; font-weight: bold; font-style: italic;">person</span><span style="color: #96A7A9; font-style: italic;">&#8217; and has accessor &amp; constructor methods for a new slot</span>
(<span style="color: #859900; font-weight: bold;">defclass</span> <span style="color: #b58900; font-style: italic;">teacher</span> (person) ((topic <span style="color: #d33682; font-style: italic;">:accessor</span> teacher-topic <span style="color: #d33682; font-style: italic;">:initarg</span> <span style="color: #d33682; font-style: italic;">:studying</span>)))

(<span style="color: #859900; font-weight: bold;">defmethod</span> <span style="color: #b58900;">speak</span> ((x teacher))
  (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"My name is %s,and I study %s."</span> (<span style="color: #b58900;">slot-value</span> x 'name) (teacher-topic x)))

(<span style="color: #859900; font-weight: bold;">setq</span> ins (<span style="color: #b58900;">make-instance</span> 'teacher <span style="color: #d33682; font-style: italic;">:studying</span> <span style="color: #2aa198;">"mathematics"</span>))
(<span style="color: #859900; font-weight: bold;">setf</span> (<span style="color: #b58900;">slot-value</span> ins 'name) <span style="color: #2aa198;">"Robert"</span>)
(speak ins) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; My name is Robert, and I study mathematics.</span>
</pre>
</div>

<p>
Later in this article, we'll make something like the <code>declaim</code> above
but have it be effectful at run-time. <i>Typing as Macros!</i>
</p>

<blockquote>
<p>
(
If you happen to be interested in looking under the hood to see what compiler generated
code looks like use <code>disassemble</code>. For example, declare <code>(defun go (x) (+ 1 x) 'bye)</code>
then invoke <code>(disassemble 'go)</code> to see something like
<code>varref x⨾ add1⨾ discard ⨾ constant bye⨾ return</code>.
)
</p>
</blockquote>
</div>
</div>

<div id="outline-container-org6ab0d5e" class="outline-2">
<h2 id="elisp-types"><span class="section-number-2">6</span> ELisp's Type Hierarchy</h2>
<div class="outline-text-2" id="text-elisp-types">
<p>
⇨ Each primitive type has a corresponding Lisp function that checks whether an object is a
  member of that type. Usually, these are the type name appended with <code>-p</code>, for multi-word
  names, and <code>p</code> for single word names. E.g., <code>string</code> type has the predicate <code>stringp</code>.
</p>

<dl class="org-dl">
<dt><a id="org9adf3f1">Type Descriptor</a></dt><dd><p>
Objects holding information about types.
</p>

<p>
This is a <code>record</code>; the <code>type-of</code> function returns the first slot of records.
</p></dd>
</dl>

<p>
This section is based <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Lisp-Data-Types.html#Lisp-Data-Types">GNU Emacs Lisp Reference Manual</a>, §2.3 “Programming Types”.
</p>
</div>

<div id="outline-container-org81d2acf" class="outline-3">
<h3 id="Number"><span class="section-number-3">6.1</span> Number</h3>
<div class="outline-text-3" id="text-Number">
<p>
<i>Numbers, including fractional and non-fractional types.</i>
</p>

<table>


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

<col  class="org-left">

<col  class="org-left">

<col  class="org-left">

<col  class="org-left">

<col  class="org-left">

<col  class="org-left">

<col  class="org-left">

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>integer</code></td>
<td class="org-left"><code>float</code></td>
<td class="org-left"><code>number</code></td>
<td class="org-left"><code>natnum</code></td>
<td class="org-left"><code>zero</code></td>
<td class="org-left"><code>plus</code></td>
<td class="org-left"><code>minus</code></td>
<td class="org-left"><code>odd</code></td>
<td class="org-left"><code>even</code></td>
</tr>
</tbody>
</table>

<p>
The relationships between these types are as follows:
</p>
<table>


<colgroup>
<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>(numberp x) ≈ (or (integerp x) (floatp x))</code></td>
</tr>

<tr>
<td class="org-left"><code>(natnump x) ≈ (and (integerp x) (≤ 0 x))</code></td>
</tr>

<tr>
<td class="org-left"><code>(zerop   x) ≈ (equal 0 x)</code></td>
</tr>

<tr>
<td class="org-left"><code>(plusp   x) ≈ (&lt; 0 x)</code></td>
</tr>

<tr>
<td class="org-left"><code>(minusp  x) ≈ (&gt; 0 x)</code></td>
</tr>

<tr>
<td class="org-left"><code>(evenp    x) ≈ (zerop (mod x 2))</code></td>
</tr>

<tr>
<td class="org-left"><code>(oddp     x) ≈ (not (oddp x))</code></td>
</tr>
</tbody>
</table>

<ul class="org-ul">
<li><p>
<b>Integer</b>: Numbers without fractional parts.
</p>

<p>
There is no overflow checking.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">expt</span> 2 60) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 1,152,921,504,606,846,976</span>
(<span style="color: #b58900;">expt</span> 2 61) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; -2,305,843,009,213,693,952</span>
(<span style="color: #b58900;">expt</span> 2 62) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 0</span>
</pre>
</div>

<p>
Numbers are written with an optional sign ‘+’ or ‘-’ at the beginning and
  an optional period at the end.
</p>
<table>


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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>-1 ≈ -1.</code></td>
<td class="org-left"><code>1 ≈ +1 ≈ 1.</code></td>
</tr>
</tbody>
</table>

<p>
They may also take <i>inclusive</i> (and exclusive) ranges:
The type list <code>(integer LOW HIGH)</code> represents all integers between
 <code>LOW</code> and <code>HIGH</code>, inclusive.  Either bound may be a list of a single
 integer to specify an exclusive limit, or a <code>*</code> to specify no
 limit.  The type <code>(integer * *)</code> is thus equivalent to <code>integer</code>.
 Likewise, lists beginning with <code>float</code>, <code>real</code>, or <code>number</code>
 represent numbers of that type falling in a particular range.
 ( <a href="https://www.gnu.org/software/emacs/manual/html_mono/cl.html#Predicates">The Emacs Common Lisp Documentation</a> )
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">    (<span style="color: #b58900;">typep</span> 4 '(integer 1 5)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true since 1 &#8804; 4 &#8804; 5.</span>
    (<span style="color: #b58900;">typep</span> 4 '(integer 1 3)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil  since 1 &#8804; 4 &#8816; 3.</span>

    (<span style="color: #b58900;">typep</span> 12 'integer) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
    (<span style="color: #b58900;">typep</span> 12 'number) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

    (<span style="color: #b58900;">typep</span> 23 'odd)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

    (<span style="color: #b58900;">typep</span> 12 '(integer <span style="color: #b58900;">*</span> 14)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t, since 12 &#8804; 14, but no lower bound.</span>
    (<span style="color: #b58900;">typep</span> 12 '(integer 0 <span style="color: #b58900;">*</span>)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t; the &#8216;*&#8217; denotes a wild-card; anything.</span>

   (<span style="color: #b58900;">typep</span> -1 '(<span style="color: #b58900;">not</span> (integer 0 <span style="color: #b58900;">*</span>))) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
   (<span style="color: #b58900;">typep</span>  1 '(<span style="color: #b58900;">not</span> (integer 0 <span style="color: #b58900;">*</span>))) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>

   (<span style="color: #b58900;">typep</span> 1 '(integer  1 2))   <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t, including lower bound</span>
   (<span style="color: #b58900;">typep</span> 1 '(integer (1) 2))  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil, excluding lower bound</span>

   (<span style="color: #b58900;">typep</span> 1.23 '(<span style="color: #b58900;">float</span> 1.20 1.24)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

   <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Here's a slighly organised demonstration:</span>

   (<span style="color: #b58900;">typep</span> 1.23 'number) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
   (<span style="color: #b58900;">typep</span> 123  'number) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
   (<span style="color: #b58900;">typep</span> 1.23 'real) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
   (<span style="color: #b58900;">typep</span> 123  'real) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

   (<span style="color: #b58900;">typep</span> 1.23 'integer) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>
   (<span style="color: #b58900;">typep</span> 123  'integer) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
   (<span style="color: #b58900;">typep</span> 1.23 'fixnum) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>
   (<span style="color: #b58900;">typep</span> 123  'fixnum) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

   (<span style="color: #b58900;">typep</span> 1.23 '<span style="color: #b58900;">float</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
   (<span style="color: #b58900;">typep</span> 123 '<span style="color: #b58900;">float</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>
   (<span style="color: #b58900;">typep</span> 123.0 '<span style="color: #b58900;">float</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div></li>

<li><b>Floating-Point</b>: Numbers with fractional parts; expressible using scientific notation.
For example, <code>15.0e+2 ≈ 1500.0</code> and <code>-1.0e+INF</code> for negative infinity.</li>

<li><b>Aliases:</b>
The type symbol <code>real</code> is a synonym for <code>number</code>, <code>fixnum</code> is a
 synonym for <code>integer</code>, and <code>wholenum</code> is a synonym for <code>natnum</code>.</li>

<li><p>
The smallest and largest values <i>representable</i> in a Lisp integer are in the
constants <code>most-negative-fixnum</code> and <code>most-postive-fixnum</code>
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Relationship with infinities</span>
(<span style="color: #b58900;">&lt;</span> -1e+INF <span style="color: #268bd2;">most-negative-fixnum</span> <span style="color: #268bd2;">most-positive-fixnum</span> 1e+INF) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div></li>
</ul>
</div>
</div>

<div id="outline-container-orgae1a42f" class="outline-3">
<h3 id="Character"><span class="section-number-3">6.2</span> Character</h3>
<div class="outline-text-3" id="text-Character">
<p>
<i>Representation of letters, numbers, and control characters.</i>
</p>

<p>
A character is just a small integers, up to 22 bits;
e.g., character <code>A</code> is represented as the integer 65.
</p>

<p>
One writes the character ‘A’ as <code>?A</code>, which is identical to 65.
Punctuations <code>()[]\;"|'`#</code> must be &#x00ad;escaped; e.g.,
</p>
<table>


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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>?\( ≈ 40</code></td>
<td class="org-left"><code>?\\ ≈ 92</code></td>
</tr>
</tbody>
</table>
<p>
Whereas <code>?. ≈ 46</code>.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">characterp</span> ?f) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
(<span style="color: #b58900;">characterp</span> <span style="color: #268bd2;">t</span>)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>
</pre>
</div>

<p>
Emacs specfic characters control-g <code>C-g</code>, backspace <code>C-h</code>, tab <code>C-i</code>, newline <code>C-j</code>, space,
return, del, and escape are expressed by ?\a, ?\b, ?\t, ?\n, ?\s, ?\r, ?\d, ?\e.
</p>

<p>
Generally, control characters can be expressed as <code>?\^𝓍 ≈ ?\C-𝓍</code>,
and meta characters by <code>?\M-𝓍</code>; e.g., <code>C-M-b</code> is expressed
<code>?\M-\C-b ≈ ?\C-\M-b</code>.
</p>

<p>
Finally, <code>?\S-𝓍</code> denotes shifted-𝓍 characters.
There are also <code>?\H-𝓍, ?\A-𝓍, ?\s-𝓍</code> to denote Hyper- Alt- or Super-modified keys;
note that lower case ‘s’ is for super whereas capital is for shift,
and lower case with no dash is a space character.
</p>
</div>
</div>

<div id="outline-container-org3f5e753" class="outline-3">
<h3 id="Symbol"><span class="section-number-3">6.3</span> Symbol</h3>
<div class="outline-text-3" id="text-Symbol">
<p>
<i>A multi-use object that refers to functions and variables, and more.</i>
</p>

<p>
A symbol is an object with a name; different objects have different names.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span> 'yes 'symbol) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">symbolp</span> 'yes)       <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

(<span style="color: #b58900;">typep</span> 12   'symbol) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
(<span style="color: #b58900;">symbolp</span> 12)         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
</pre>
</div>

<table>


<colgroup>
<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>symbol</code> ≈ Is it a symbol?</td>
</tr>

<tr>
<td class="org-left"><code>bound</code>  ≈ Does it refer to anything?</td>
</tr>
</tbody>
</table>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span> 'xyz 'bound) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>

(<span style="color: #859900; font-weight: bold;">setq</span> xyz 123)
(<span style="color: #b58900;">typep</span> 'xyz 'bound) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div>
<p>
See this short <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Void-Variables.html#Void-Variables">docs</a> page for more info on when a variable is void.
</p>

<p>
<span class="underline">Names have a tremendously flexible syntax.</span>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">setq</span> +*/-_~!<span style="color: #b58900;">@$%^&amp;</span>:&lt;&gt;{}? 23)
(<span style="color: #859900; font-weight: bold;">setq</span> \+1            23) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Note +1 &#8776; 1, a number.</span>
(<span style="color: #859900; font-weight: bold;">setq</span> \12            23)
(<span style="color: #859900; font-weight: bold;">setq</span> this\ is\ woah 23) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Escaping each space!</span>
(<span style="color: #b58900;">+</span> this\ is\ woah 1)     <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 24</span>
</pre>
</div>

<p>
If the symbbol name starts with a colon ‘:’, it's called a keyword symbol
     and automatically acts as a constant.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span> <span style="color: #d33682; font-style: italic;">:hello</span> 'keyword) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div>

<p>
Symbols generally act as names for variables and functions, however there are
some names that have <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Variables-with-Restricted-Values.html#Variables-with-Restricted-Values">fixed values</a> and any attempt to reset their values signals an error.
Most notably, these include <code>t</code> for true or the top-most type,
<code>nil</code> for false or the bottom-most type, and keywords.
These three evaluate to themselves.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #268bd2;">t</span>      <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
nil    <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>
<span style="color: #d33682; font-style: italic;">:hello</span> <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; :hello</span>

(<span style="color: #859900; font-weight: bold;">setq</span> <span style="color: #268bd2;">t</span>   12) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; Error: Attempt to set a constant symbol</span>
(<span style="color: #859900; font-weight: bold;">setq</span> nil 12) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; Error: Attempt to set a constant symbol</span>
(<span style="color: #859900; font-weight: bold;">setq</span> <span style="color: #d33682; font-style: italic;">:x</span>  12) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; Error: Attempt to set a constant symbol</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">:x &#8800; 'x</span>
(<span style="color: #b58900;">set</span> 'x 12) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 12</span>
x           <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 12</span>

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">They're self-evaluating</span>
(<span style="color: #b58900;">equal</span> <span style="color: #268bd2;">t</span>   '<span style="color: #268bd2;">t</span>)   <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
(<span style="color: #b58900;">equal</span> nil 'nil) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
(<span style="color: #b58900;">equal</span> <span style="color: #d33682; font-style: italic;">:x</span>  '<span style="color: #d33682; font-style: italic;">:x</span>)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>

(<span style="color: #b58900;">equal</span> <span style="color: #d33682; font-style: italic;">:x</span> 'x)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; nil</span>
</pre>
</div>

<p>
In particular, <code>:x ≠ 'x</code>!
</p>
</div>
</div>

<div id="outline-container-org8b356ff" class="outline-3">
<h3 id="Sequence"><span class="section-number-3">6.4</span> Sequence</h3>
<div class="outline-text-3" id="text-Sequence">
<p>
<i>The interface for ordered collections</i>; e.g.,
the <code>(elt sequence index)</code> function can be applied to any sequence
to extract an element at the given index.
</p>

<div class="org-center">
<table>


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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>sequence</code></td>
<td class="org-left"><code>seq</code></td>
</tr>
</tbody>
</table>
</div>

<p>
The latter is an extensible variant of the former
&#x2014;for when we declare our own sequential data types.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span> '(1 2 3) 'sequence) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div>

<p>
There are two immediate subtypes: <code>array</code> and <code>cons</code>, the latter has <code>list</code>
as a subtype.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span>  [1 2 3] 'array)       <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
(<span style="color: #b58900;">typep</span> '(1 2 3) '<span style="color: #b58900;">cons</span>)        <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
(<span style="color: #b58900;">typep</span> '(1 <span style="color: #2aa198;">"2"</span> 'three) '<span style="color: #b58900;">list</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div>

<dl class="org-dl">
<dt>Array</dt><dd><p>
Arrays include strings and vectors.
</p>
<dl class="org-dl">
<dt>Vector</dt><dd>One-dimensional arrays.</dd>
<dt>Char-Table</dt><dd>One-dimensional sparse arrays indexed by characters.</dd>
<dt>Bool-Vector</dt><dd>One-dimensional arrays of <code>t</code> or <code>nil</code>.</dd>
<dt>Hash Table</dt><dd>Super-fast lookup tables.</dd>
</dl>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span> <span style="color: #2aa198;">"hi"</span> '<span style="color: #b58900;">string</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> 'hi  '<span style="color: #b58900;">string</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
</pre>
</div></dd>

<dt>Cons cell type</dt><dd><p>
Cons cells and lists, which are chains of cons cells.
</p>

<p>
These are objects consisting of two Lisp objects, called <code>car</code> and <code>cdr</code>.
That is they are pairs of Lisp objects.
</p>

<div class="org-src-container">
<pre class="src src-math">      '(x₀ x₁ x₂)
    ≈ '(x₀ . (x₁ . (x₂ . nil)))
    ≠ '(x₀ x₁ . x₂)
    ≈ '(x₀ . (x₁ . x₂))
</pre>
</div>

<p>
Notice that when there is no ‘.’, then a list
is just a nested cons chain ending in ‘nil’.
Note that <code>'(x₀ . x₁ . x₂)</code> is meaningless.
</p>

<p>
Cons cells are central to Lisp and so objects which are not a cons
cell are called <i>atoms</i>.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">An atom is not a cons.</span>
(<span style="color: #b58900;">typep</span> 123 '<span style="color: #b58900;">atom</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
(<span style="color: #b58900;">typep</span> 'ni '<span style="color: #b58900;">atom</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div>

<p>
Computationally:
</p>
<table>


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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left"><code>(atom x)</code></td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left"><code>(typep x 'atom)</code></td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left"><code>(not (consp x))</code></td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left"><code>(not (typep x 'cons))</code></td>
</tr>

<tr>
<td class="org-left">≈</td>
<td class="org-left"><code>(typep x '(not cons))</code></td>
</tr>
</tbody>
</table>

<p>
Interestingly, one writes <code>atom</code>, not <code>atomp</code>.
</p></dd>
</dl>
</div>
</div>

<div id="outline-container-org77bfe7c" class="outline-3">
<h3 id="Function"><span class="section-number-3">6.5</span> Function</h3>
<div class="outline-text-3" id="text-Function">
<p>
<i>Piece of executable code.</i>
</p>

<p>
A non-compiled function in Lisp is a lambda expression: A list whose
first element is the symbol <code>lambda</code>.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">consp</span>     (<span style="color: #859900; font-weight: bold;">lambda</span> (x) x))        <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">functionp</span> (<span style="color: #859900; font-weight: bold;">lambda</span> (x) x))        <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>

(<span style="color: #b58900;">functionp</span> (<span style="color: #859900; font-weight: bold;">lambda</span> is <span style="color: #b58900;">the</span> <span style="color: #b58900;">first</span>)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> (<span style="color: #859900; font-weight: bold;">lambda</span> stuff) '<span style="color: #b58900;">function</span>)  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
</pre>
</div>

<p>
It may help to know that a <code>defun</code> just produces an alias for a function:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">  (<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">name</span> (args) <span style="color: #35a69c; font-style: italic;">"docs"</span> body)
&#8776; (<span style="color: #859900; font-weight: bold;">defalias</span> (<span style="color: #859900; font-weight: bold;">quote</span> name) (<span style="color: #859900; font-weight: bold;">function</span> (<span style="color: #859900; font-weight: bold;">lambda</span> (args) docs body)))
</pre>
</div>

<p>
Here's some more examples.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">typep</span> #'<span style="color: #b58900;">+</span>   '<span style="color: #b58900;">function</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">typep</span> 'nice '<span style="color: #b58900;">function</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>

(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">it</span> (x) (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"%s"</span> (+1 x)))
(<span style="color: #b58900;">typep</span> #'it   '<span style="color: #b58900;">function</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">functionp</span> #'it)         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-org63a064a" class="outline-3">
<h3 id="Macro"><span class="section-number-3">6.6</span> Macro</h3>
<div class="outline-text-3" id="text-Macro">
<p>
<i>A method of expanding an expression into another expression.</i>
</p>

<p>
Like functions, any list that begins with <code>macro</code>, and whose <code>cdr</code>
is a function, is considered a macro as long as Emacs Lisp is concerned.
</p>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">macrop</span> '(macro (<span style="color: #859900; font-weight: bold;">lambda</span> (x) x))) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
</pre>
</div>

<p>
Since <code>defmacro</code> produces an alias, as follows,
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">  (<span style="color: #859900; font-weight: bold;">defmacro</span> <span style="color: #b58900;">name</span> (args) <span style="color: #35a69c; font-style: italic;">"docs"</span> body)
&#8776; (<span style="color: #859900; font-weight: bold;">defalias</span> (<span style="color: #859900; font-weight: bold;">quote</span> name) (<span style="color: #b58900;">cons</span> (<span style="color: #859900; font-weight: bold;">quote</span> macro) (<span style="color: #859900; font-weight: bold;">function</span> (<span style="color: #859900; font-weight: bold;">lambda</span> (args) docs body))))
</pre>
</div>

<p>
You may be concerned that <code>(macrop x) ≟ (equal 'macro (car x))</code>, and so if a user
gives you a macro you might think its a cons cell of data.
Fortunately this is not the case:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">defmacro</span> <span style="color: #b58900;">no-op</span> () )

(<span style="color: #b58900;">macrop</span> #'no-op)    <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; true</span>
(<span style="color: #b58900;">consp</span>  #'no-op)    <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false; whence it's also not a list.</span>
(<span style="color: #b58900;">functionp</span> #'no-op) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>

(<span style="color: #b58900;">typep</span> #'no-op '
       (satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (x) (<span style="color: #859900; font-weight: bold;">and</span> (<span style="color: #b58900;">listp</span> x) (<span style="color: #b58900;">equal</span> 'macro (<span style="color: #b58900;">car</span> x)))))) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; false</span>
</pre>
</div>

<p>
Why not? Well, you could think of a macro as a ‘record’ whose label is <code>macro</code> and
its only element is the associated function.
</p>
</div>
</div>

<div id="outline-container-org5590899" class="outline-3">
<h3 id="Record"><span class="section-number-3">6.7</span> Record</h3>
<div class="outline-text-3" id="text-Record">
<p>
<i>Compound objects with programmer-defined types.</i>
</p>

<p>
They are the underlying representation of <code>defstruct</code> and <code>defclass</code> instances.
</p>

<p>
For example:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">defstruct</span> person
  name age)
</pre>
</div>

<p>
The <code>type-of</code> operator yields the <code>car</code> of instances of such declartions.
</p>
<table>


<colgroup>
<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>(record τ e₀ … eₙ) ≈ #s(τ e₀ … eₙ)</code></td>
</tr>
</tbody>
</table>

<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">setq</span> bobert (make-person <span style="color: #d33682; font-style: italic;">:name</span> <span style="color: #2aa198;">"bobby"</span> <span style="color: #d33682; font-style: italic;">:age</span> 'too-much))
(<span style="color: #b58900;">type-of</span> bobert) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; person</span>
</pre>
</div>

<p>
Componenets may be indexed with <code>aref</code>.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">aref</span> bobert 1)      <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; bobby</span>
(person-name bobert) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; bobby</span>
</pre>
</div>

<p>
A record is considered a constant for evaulation: Evaluating it yields itself.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">type-of</span> #s(person <span style="color: #2aa198;">"mark"</span> twelve)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; person</span>
(<span style="color: #b58900;">recordp</span> #s(nice))                 <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; t</span>
</pre>
</div>
</div>
</div>
</div>

<div id="outline-container-org77b37c4" class="outline-2">
<h2 id="typing-via-macros"><span class="section-number-2">7</span> Typing via Macros &amp; Advice</h2>
<div class="outline-text-2" id="text-typing-via-macros">
<p>
Checking the type of inputs is tedious and so I <a href="https://www.reddit.com/r/emacs/comments/cct5hp/functional_type_declarations_in_elisp/">guessed</a> it could be done using
macros and advice. Looking at <a href="https://docs.racket-lang.org/ts-guide/types.html">Typed Racket</a> for inspiration, the following
fictitious syntax would add advice to <code>f</code> that checks the optional arguments <code>xᵢ</code>
have type <code>σᵢ</code> and the mandatory positional arguments have type <code>τᵢ</code> according
to position, and the result of the computation is of type <code>τ</code>.
To the best of my knowledge, no one had done this for Emacs Lisp &#x2014;I don't know why.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type 'f ((<span style="color: #d33682; font-style: italic;">:x&#8321;</span> &#963;&#8321;) &#8230; (<span style="color: #d33682; font-style: italic;">:x&#8344;</span> &#963;&#8344;)) (&#964;&#8321; &#8230; &#964;&#8345; &#964;))
</pre>
</div>

<p>
To modify a variable, or function, we may simply redefine it; but a much more elegant and powerful
approach is to <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html">“advise”</a> the current entity with some new behaviour. In our case of interest, we will
<i>advise functions to check their arguments before executing their bodies</i>.
</p>

<p>
Below is my attempt: <a id="org84d2c90"><code>declare-type</code></a>. Before you get scared or think it's horrendous, be charitable and
note that about a third of the following is documentation and a third is local declarations.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">cl-defmacro</span> <span style="color: #b58900;">declare-type</span> (f key-types <span style="color: #b58900; font-style: italic;">&amp;rest</span> types)
  <span style="color: #35a69c; font-style: italic;">"Attach the given list of types to the function &#8216;f&#8217;</span>
<span style="color: #35a69c; font-style: italic;">   by advising the function to check its arguments&#8217; types</span>
<span style="color: #35a69c; font-style: italic;">   are equal to the list of given types.</span>

<span style="color: #35a69c; font-style: italic;">   We name the advice &#8216;&#10218;f&#10219;-typing-advice&#8217; so that further</span>
<span style="color: #35a69c; font-style: italic;">   invocations to this macro overwrite the same advice function</span>
<span style="color: #35a69c; font-style: italic;">   rather than introducing additional, unintended, constraints.</span>

<span style="color: #35a69c; font-style: italic;">   Using type specifiers we accommodate for unions of types</span>
<span style="color: #35a69c; font-style: italic;">   and subtypes, etc &#9829;&#8255;&#9829;.</span>

<span style="color: #35a69c; font-style: italic;">   &#8216;</span><span style="color: #6c71c4; font-weight: bold; font-style: italic;">key-types</span><span style="color: #35a69c; font-style: italic;">&#8217; should be of the shape (:x&#8320; t&#8320; &#8943; :x&#8345; t&#8345;);</span>
<span style="color: #35a69c; font-style: italic;">    when there are no optional types, use symbol &#8220;:&#8221;.</span>

<span style="color: #35a69c; font-style: italic;">    E.g., (declare-type my-func (:z string :w integer) integer symbol string)</span>
<span style="color: #35a69c; font-style: italic;">  "</span>

  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Basic coherency checks. When there aren't optional types, key-types is the &#8220;:&#8221; symbol.</span>
  (should (<span style="color: #859900; font-weight: bold;">and</span> (<span style="color: #b58900;">listp</span> types) (<span style="color: #859900; font-weight: bold;">or</span> (<span style="color: #b58900;">listp</span> key-types) (<span style="color: #b58900;">symbolp</span> key-types))))

  (<span style="color: #859900; font-weight: bold;">letf*</span> ((pairify (<span style="color: #859900; font-weight: bold;">lambda</span> (xs) (<span style="color: #859900; font-weight: bold;">loop</span> for i in xs by #'<span style="color: #b58900;">cddr</span>         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Turn a list of flattenned pairs</span>
                                      for j in (<span style="color: #b58900;">cdr</span> xs) by #'<span style="color: #b58900;">cddr</span>   <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">into a list of explicit pairs.</span>
                                      collect (<span style="color: #b58900;">cons</span> i j))))         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">MA: No Lisp method for this!?</span>
         (result-type  (<span style="color: #b58900;">car</span> (<span style="color: #b58900;">-take-last</span> 1 types)))
         (types        (<span style="color: #b58900;">-drop-last</span> 1 types))
         (num-of-types (<span style="color: #b58900;">length</span> types))
         (key-types-og (<span style="color: #859900; font-weight: bold;">unless</span> (<span style="color: #b58900;">symbolp</span> key-types) key-types))
         (key-types    (<span style="color: #b58900;">funcall</span> pairify key-types-og))
         (advice-name  (<span style="color: #b58900;">intern</span> (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"%s-typing-advice"</span> f)))
         (notify-user  (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"%s now typed %s &#8594; %s &#8594; %s."</span>
                               `,f key-types-og types result-type)))

      `(<span style="color: #859900; font-weight: bold;">progn</span>
         (<span style="color: #859900; font-weight: bold;">defun</span> ,advice-name (orig-fun <span style="color: #b58900; font-style: italic;">&amp;rest</span> args)

           <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Split into positional and key args; optionals not yet considered.</span>
           (<span style="color: #859900; font-weight: bold;">letf*</span> ((all-args
                     (<span style="color: #b58900;">-split-at</span>
                       (<span style="color: #859900; font-weight: bold;">or</span> (<span style="color: #859900; font-weight: bold;">--find-index</span> (<span style="color: #b58900;">not</span> (<span style="color: #b58900;">s-blank?</span> (<span style="color: #b58900;">s-shared-start</span> <span style="color: #2aa198;">":"</span> (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"%s"</span> it)))) args) ,num-of-types)
                        args)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">The &#8220;or&#8221; is for when there are no keywords provided.</span>
                  (pos-args  (<span style="color: #b58900;">car</span> all-args))
                  (key-args  (<span style="color: #b58900;">funcall</span> ,pairify (<span style="color: #b58900;">cadr</span> all-args)))
                  (fun-result nil)
                  ((<span style="color: #b58900;">symbol-function</span> 'shucks)
                     (<span style="color: #859900; font-weight: bold;">lambda</span> (e&#964; e g)
                       (<span style="color: #859900; font-weight: bold;">unless</span> (<span style="color: #b58900;">typep</span> g e&#964;)
                         (<span style="color: #b58900;">error</span> <span style="color: #2aa198;">"%s: Type mismatch! Expected %s %s &#8800; Given %s %s."</span>
                                (<span style="color: #859900; font-weight: bold;">function</span> ,f) e&#964; e (<span style="color: #b58900;">type-of</span> g) (<span style="color: #b58900;">prin1-to-string</span> g))))))

         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Check the types of positional arguments.</span>
         (<span style="color: #859900; font-weight: bold;">unless</span> (<span style="color: #b58900;">equal</span> ,num-of-types (<span style="color: #b58900;">length</span> pos-args))
           (<span style="color: #b58900;">error</span> <span style="color: #2aa198;">"%s: Insufficient number of arguments; given %s, %s, but %s are needed."</span>
                  (<span style="color: #859900; font-weight: bold;">function</span> ,f) (<span style="color: #b58900;">length</span> pos-args) pos-args ,num-of-types))
         (<span style="color: #859900; font-weight: bold;">loop</span> for (ar ty pos) in (<span style="color: #b58900;">-zip</span> pos-args (<span style="color: #859900; font-weight: bold;">quote</span> ,types) (<span style="color: #b58900;">number-sequence</span> 0 ,num-of-types))
               <span style="color: #b58900;">do</span> (shucks ty (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"for argument %s"</span> pos) ar))

         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Check the types of *present* keys.</span>
         (<span style="color: #859900; font-weight: bold;">loop</span> for (k . v) in key-args
               <span style="color: #b58900;">do</span> (shucks (<span style="color: #b58900;">cdr</span> (<span style="color: #b58900;">assoc</span> k (<span style="color: #859900; font-weight: bold;">quote</span> ,key-types))) k v))

         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Actually execute the orginal function on the provided arguments.</span>
         (<span style="color: #859900; font-weight: bold;">setq</span> fun-result (<span style="color: #b58900;">apply</span> orig-fun args))
         (shucks (<span style="color: #859900; font-weight: bold;">quote</span> ,result-type) <span style="color: #2aa198;">"for the result type (!)"</span> fun-result)

         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Return-value should be given to caller.</span>
         fun-result))

      <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Register the typing advice and notify user of what was added.</span>
      (<span style="color: #b58900;">advice-add</span> (<span style="color: #859900; font-weight: bold;">function</span> ,f) <span style="color: #d33682; font-style: italic;">:around</span> (<span style="color: #859900; font-weight: bold;">function</span> ,advice-name))
      ,notify-user )))
</pre>
</div>

<pre class="example">
declare-type
</pre>


<p>
There are some notable shortcomings: Lack of support for type variables and, for now, no support for
optional arguments. Nonetheless, I like it &#x2014;of course.
( Using <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Watching-Variables.html#Watching-Variables">variable watchers</a> we could likely add support for type variables as well as
function-types. )
</p>

<p>
<b>We accidentally forgot to consider an argument.</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type f&#8321; (<span style="color: #d33682; font-style: italic;">:z</span> <span style="color: #b58900;">string</span> <span style="color: #d33682; font-style: italic;">:w</span> <span style="color: #b58900;">list</span>) integer symbol <span style="color: #b58900;">string</span>)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8321; now typed (:z string :w integer) &#8594; (integer symbol) &#8594; string.</span>

(<span style="color: #859900; font-weight: bold;">cl-defun</span> <span style="color: #b58900;">f&#8321;</span> (x y <span style="color: #b58900; font-style: italic;">&amp;key</span> z w) (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"%s"</span> x))
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8321; now defined</span>

(f&#8321; 'x) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8321;: Insufficient number of arguments; given 2, (x), but 3 are needed.</span>
</pre>
</div>
<p>
The type declaration said we needed 3 arguments, but we did not consider one of them.
</p>

<p>
<b>We accidentally returned the wrong value.</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type f&#8322; (<span style="color: #d33682; font-style: italic;">:z</span> <span style="color: #b58900;">string</span> <span style="color: #d33682; font-style: italic;">:w</span> <span style="color: #b58900;">list</span>) integer symbol <span style="color: #b58900;">string</span>)
(<span style="color: #859900; font-weight: bold;">cl-defun</span> <span style="color: #b58900;">f&#8322;</span> (x y <span style="color: #b58900; font-style: italic;">&amp;key</span> z w) x)

(f&#8322; 144 'two)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8322;: Type mismatch! Expected string for the result type (!) &#8800; Given integer 144.</span>
</pre>
</div>

<p>
<b>We accidentally forgot to supply an argument.</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type f&#8323; (<span style="color: #d33682; font-style: italic;">:z</span> <span style="color: #b58900;">string</span> <span style="color: #d33682; font-style: italic;">:w</span> <span style="color: #b58900;">list</span>) integer symbol <span style="color: #b58900;">string</span>)
(<span style="color: #859900; font-weight: bold;">cl-defun</span> <span style="color: #b58900;">f&#8323;</span> (x y <span style="color: #b58900; font-style: italic;">&amp;key</span> z w) (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"%s"</span> x))

(f&#8323; 144)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8323;: Insufficient number of arguments; given 1, (144), but 2 are needed.</span>
</pre>
</div>

<p>
<b>A positional argument is supplied of the wrong type.</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(f&#8323; 'one <span style="color: #2aa198;">"two"</span>)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658;  f&#8323;: Type mismatch! Expected integer for argument 0 &#8800; Given symbol one.</span>

(f&#8323; 144 <span style="color: #2aa198;">"two"</span>)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8323;: Type mismatch! Expected symbol for argument 1 &#8800; Given string "two".</span>
</pre>
</div>
<p>
Notice: When multiple positional arguments have type-errors, the errors are reported one at a time.
</p>

<p>
<b>A keyword argument is supplied of the wrong type.</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(f&#8323; 1 'two <span style="color: #d33682; font-style: italic;">:z</span> 'no&#8320; <span style="color: #d33682; font-style: italic;">:w</span> 'no&#8321;)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8323;: Type mismatch! Expected string :z &#8800; Given symbol no&#8320;.</span>

(f&#8323; 1 'two <span style="color: #d33682; font-style: italic;">:z</span> <span style="color: #2aa198;">"ok"</span> <span style="color: #d33682; font-style: italic;">:w</span> 'no&#8321;)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8323;: Type mismatch! Expected string :w &#8800; Given symbol no&#8321;.</span>

(f&#8323; 1 'two <span style="color: #d33682; font-style: italic;">:z</span> <span style="color: #2aa198;">"ok"</span> <span style="color: #d33682; font-style: italic;">:w</span> 23)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8323;: Type mismatch! Expected string :w &#8800; Given integer 23.</span>

(f&#8323; 1 'two <span style="color: #d33682; font-style: italic;">:z</span> <span style="color: #2aa198;">"ok"</span> <span style="color: #d33682; font-style: italic;">:w</span> '(a b 1 2)) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; okay; no type-error.</span>
</pre>
</div>

<p>
<b>We have no optional arguments.</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type f&#8324; : integer symbol <span style="color: #b58900;">string</span>)
(<span style="color: #859900; font-weight: bold;">cl-defun</span> <span style="color: #b58900;">f&#8324;</span> (x y <span style="color: #b58900; font-style: italic;">&amp;key</span> z w) (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"%s"</span> x))

(f&#8324; 144 'two <span style="color: #d33682; font-style: italic;">:z</span> <span style="color: #2aa198;">"bye"</span>)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658;  f&#8324;: Type mismatch! Expected nil :z &#8800; Given string "bye".</span>
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">( We shouldn't have any keyword :z according to the type declaration! )</span>

(f&#8324; 144 'two) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; "144"</span>
</pre>
</div>

<p>
<b>We can incorporate type specfiers such as unions!</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type f&#8325; : (<span style="color: #859900; font-weight: bold;">or</span> integer <span style="color: #b58900;">string</span>) <span style="color: #b58900;">string</span>)
(<span style="color: #859900; font-weight: bold;">cl-defun</span> <span style="color: #b58900;">f&#8325;</span> (x) (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"%s"</span> x))

(f&#8325; 144)     <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; "144"</span>
(f&#8325; <span style="color: #2aa198;">"neato"</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; "neato"</span>

(f&#8325; 'shaka-when-the-walls-fell)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8325;: Type mismatch! Expected (or integer string) for argument 0</span>
<span style="color: #96A7A9; font-style: italic;">;;       </span><span style="color: #96A7A9; font-style: italic;">&#8800; Given symbol shaka-when-the-walls-fell.</span>
</pre>
</div>

<p>
<b>No positional arguments but a complex optional argument!</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type f&#8326; (<span style="color: #d33682; font-style: italic;">:z</span> (satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (it) (<span style="color: #859900; font-weight: bold;">and</span> (<span style="color: #b58900;">integerp</span> it) (<span style="color: #b58900;">=</span> 0 (<span style="color: #b58900;">mod</span> it 5))))))
                 character)
(<span style="color: #859900; font-weight: bold;">cl-defun</span> <span style="color: #b58900;">f&#8326;</span> (<span style="color: #b58900; font-style: italic;">&amp;key</span> z) ?A)

(f&#8326; 'hi)     <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658;  Keyword argument 144 not one of (:z)</span>
(f&#8326;)         <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 65; i.e., the character &#8216;A&#8217;</span>
(f&#8326; <span style="color: #d33682; font-style: italic;">:z</span> 6)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658;  f&#8326;: Type mismatch!</span>
<span style="color: #96A7A9; font-style: italic;">;;    </span><span style="color: #96A7A9; font-style: italic;">Expected (satisfies (lambda (it) (and (integerp it) (= 0 (mod it 5))))) :z</span>
<span style="color: #96A7A9; font-style: italic;">;;    </span><span style="color: #96A7A9; font-style: italic;">&#8800; Given integer 6.</span>

(f&#8326; <span style="color: #d33682; font-style: italic;">:z</span> 10) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 65; i.e., the expected output since 10 mod 5 &#8776; 0 &amp; so 10 is valid input.</span>
</pre>
</div>

<p>
<b>Preconditions!</b> The previous example had a complex type on a keyword, but that was
essentially a pre-condition; we can do the same on positional arguments.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type f&#8327; : (satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (it) (<span style="color: #b58900;">=</span> it 5)))
                   integer)
(<span style="color: #859900; font-weight: bold;">cl-defun</span> <span style="color: #b58900;">f&#8327;</span> (n) n)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">The identity on 5 function; and undefined otherwise.</span>

(f&#8327; 4)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; f&#8327;: Type mismatch! Expected (satisfies (lambda (it) (= it 5))) for argument 0</span>
<span style="color: #96A7A9; font-style: italic;">;;       </span><span style="color: #96A7A9; font-style: italic;">&#8800; Given integer 4.</span>

(f&#8327; 5) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 5</span>
</pre>
</div>

<p>
<b>Postconditions!</b>
Given an integer greater than 5, we present an integer greater than 2; i.e.,
this is a constructive proof that \(∀ n • n > 5 ⇒ n > 2\).
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(declare-type f&#8328; : (satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (in)  (<span style="color: #b58900;">&gt;</span> in 5)))
                   (satisfies (<span style="color: #859900; font-weight: bold;">lambda</span> (out) (<span style="color: #b58900;">&gt;</span> out 2))))
(<span style="color: #859900; font-weight: bold;">cl-defun</span> <span style="color: #b58900;">f&#8328;</span> (n) n)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">The identity on 5 function; and undefined otherwise.</span>

(f&#8328; 4)
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658;  f&#8328;: Type mismatch! Expected (satisfies (lambda (in) (&gt; in 5))) for argument 0</span>
<span style="color: #96A7A9; font-style: italic;">;;        </span><span style="color: #96A7A9; font-style: italic;">&#8800; Given integer 4.</span>

(f&#8328; 72) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#8658; 72; since indeed 72 &gt; 5 for the input, and clearly 72 &gt; 2 for the output.</span>
</pre>
</div>

<p>
As it currently stands we cannot make any explicit references between the inputs
and the output, but that's an easy fix: Simply add a local function <code>old</code> to the
<a href="#org84d2c90"><code>declare-type</code></a> macro which is intentionally exposed so that it can be used in the
type declarations to refer to the ‘old’, or initial, values provided to the function.
Additionally, one could also add keyword arguments <code>:requires</code> and <code>:ensures</code>
for a more sophisticated pre- and post-condition framework.
<a href="https://github.com/sellout/quid-pro-quo">Something</a> along these lines is implemented for Common Lisp.
</p>

<p>
Here's a fun exercise: Recast the <a href="https://liquid.kosmikus.org/01-intro.html#/what-is-liquid-haskell">Liquid Haskell</a> examples in Lisp using this
<a href="#org84d2c90"><code>declare-type</code></a> form.
</p>
</div>
</div>

<div id="outline-container-orgb3de774" class="outline-2">
<h2 id="Closing"><span class="section-number-2">8</span> Closing</h2>
<div class="outline-text-2" id="text-Closing">
<blockquote>
<p>
<i>I have heard more than one LISP advocate state such subjective comments as, "LISP is the most powerful and elegant programming language in the world" and expect such comments to be taken as objective truth. I have never heard a Java, C++, C, Perl, or Python advocate make the same claim about their own language of choice.</i>
</p>

<p>
---<a href="http://www.paulgraham.com/quotes.html">A guy on slashdot</a>
</p>
</blockquote>

<p>
I learned a lot of stuff, hope you did too ^_^
</p>
</div>
</div>

<div id="outline-container-org1471d5d" class="outline-2">
<h2 id="references"><span class="section-number-2">9</span> References</h2>
<div class="outline-text-2" id="text-references">
<p>
Neato web articles:
</p>
<ul class="org-ul">
<li><a href="http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html">What to know before debating type systems</a>
<ul class="org-ul">
<li>Debunks a number of fallacies such as
“dynamic typing provides no way to find bugs” and
“static types need type declarations”.</li>
</ul></li>
<li><a href="http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html">Dynamic Languages Strike Back</a>
<ul class="org-ul">
<li>Everything you might wanna know about dynamically checked languages.</li>
</ul></li>
<li><a href="http://www.ai.sri.com/~delacaze/alu-site/alu/table/contents.htm">The Association of Lisp Users</a>
<ul class="org-ul">
<li>Abundant resource relating to Lisp.</li>
</ul></li>
<li><a href="https://www.williamjbowman.com/blog/2018/01/19/untyped-programs-don-t-exist/#related">Untyped Programs Don’t Exist</a>
<ul class="org-ul">
<li>It's not a matter of typing but of pragmatics.</li>
</ul></li>
<li><a href="http://homes.sice.indiana.edu/jsiek/what-is-gradual-typing/">What is Gradual Typing</a>:
<ul class="org-ul">
<li>Discusses how static and dynamic typing can be used together hamroniously.</li>
</ul></li>
<li><a href="https://www.cliki.net/">CLiki &#x2014; The Common Lisp Wiki</a>
<ul class="org-ul">
<li>Contains resources for learning about
and using the   programming language Common Lisp.</li>
<li>The humour section is delightful.</li>
</ul></li>
<li><a href="http://www.cs.utexas.edu/users/boyer/ftp/diss/akers.pdf">Strong Static Type Checking for Functional Common Lisp</a>
<ul class="org-ul">
<li>PhD thesis regarding strong static type checking in an applicative subset of CL.</li>
</ul></li>
<li><a href="http://www.paulgraham.com/avg.html">Beating the Averages</a>
<ul class="org-ul">
<li>Paul Graham discusses “the most powerful language available” &#x2014;Lisp.</li>
<li>Other articles he's written about Lisp can be found <a href="http://www.paulgraham.com/lisp.html">here</a>.</li>
</ul></li>
<li><a href="http://www.marktarver.com/bipolar.html">The Bipolar Lisp Programmer</a>
<ul class="org-ul">
<li>“Lisp is, like life, what you make of it.”
Lisps attract a certain kind of personality.</li>
</ul></li>

<li>A <a id="orgd4b9b6a">bunch of papers</a> on <a href="#orged0cebc">polymorphic</a> (modal) type systems
for Lisp-like multi-staged languages:
<a href="http://ropas.snu.ac.kr/lib/dock/KiYiCa2005.pdf">This</a> is generic, <a href="https://www.iro.umontreal.ca/~monnier/typer-jfla2019.pdf">this</a> is ML + Scheme, <a href="https://link.springer.com/chapter/10.1007/978-1-4615-2836-4_8">this</a> for compile-time typing,
and <a href="https://hal.archives-ouvertes.fr/hal-01380792/document">this</a> one “allows the programmer to declaratively express the types of
 heterogeneous sequences in a way which is natural in the Common Lisp language.”</li>

<li><a href="http://lambda-the-ultimate.org/node/5426">Type Systems as Macros</a>
<ul class="org-ul">
<li><p>
After defining <a href="#org84d2c90"><code>declare-type</code></a> I thought the slogan “types by macros” sounded nifty;
Googling it led me to this paper where the Racket is endowed with types.
</p>

<p>
Lisp is great lol.
</p></li>
</ul></li>

<li><a href="https://twobithistory.org/2018/10/14/lisp.html">How Lisp Became God's Own Programming Language</a>
<ul class="org-ul">
<li>History and venerance of Lisp.</li>
</ul></li>

<li><a href="http://www.lispworks.com/documentation/lw51/CLHS/Body/04_bc.htm">Common Lisp HyperSpec</a> &#x2013; Type Specifiers</li>
</ul>
</div>
</div>
<div class="taglist"><a href="https://alhassy.github.io/tags.html">Tags</a>: <a href="https://alhassy.github.io/tag-types.html">types</a> <a href="https://alhassy.github.io/tag-lisp.html">lisp</a> <a href="https://alhassy.github.io/tag-program-proving.html">program-proving</a> <a href="https://alhassy.github.io/tag-emacs.html">emacs</a> </div><center><strong> Generated by Emacs and Org-mode (•̀ᴗ•́)و </strong></center><center><a href="TypedLisp.org.html"><img
   src="https://img.shields.io/badge/-Source-informational?logo=read-the-docs"></a>&emsp;<a href="https://github.com/alhassy/alhassy.github.io/commits/master/posts/TypedLisp.org"><img
   src="https://img.shields.io/badge/-History-informational?logo=github"></a><br><a href="https://www.buymeacoffee.com/alhassy"><img src="https://img.shields.io/badge/-buy_me_a%C2%A0coffee-gray?logo=buy-me-a-coffee"></a></center>]]></description>
  <category><![CDATA[types]]></category>
  <category><![CDATA[lisp]]></category>
  <category><![CDATA[program-proving]]></category>
  <category><![CDATA[emacs]]></category>
  <link>https://alhassy.github.io/TypedLisp.html</link>
  <guid>https://alhassy.github.io/TypedLisp.html</guid>
  <pubDate>Wed, 21 Aug 2019 19:29:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[Have you ever packaged anything?]]></title>
  <description><![CDATA[</div><div style="text-align: center;">12 Mar 2019</div><center> <img src="images/packages.png" alt="Article image"
            style="border: 2px solid black;" width="250" height="250" align="top" /> </center><br><center><strong>Abstract</strong></center>
<nav id="table-of-contents">
<h2><a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a></h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgc4fbc71">1. Part I ─Getting the mail</a></li>
<li><a href="#org1c5096c">2. Part II ─Building blocks</a></li>
<li><a href="#orga8c2f25">3. Part III ─Arithmetic</a></li>
</ul>
</div>
</nav>

<p>
Herein I try to make my current doctoral research accessible to the average person:
Extending dependently-typed languages to implement module system features in the core
language. It's something I can direct my family to, if they're inclined to know what it is
I've been doing lately.
</p>

<p>
The technical matter can be seen at the associated website
─<a href="https://alhassy.github.io/next-700-module-systems-proposal/">The Next 700 Module Systems</a>─ which includes a poster, slides, and a demo.
</p>

<p>
Excluding the abstract, this is my thesis proposal in <i>three minutes</i> (•̀ᴗ•́)و
</p>

<small> <center>
<p>
( Photo by
<a href="https://unsplash.com/@vtrsnts">Vitor Santos</a>
on <a href="https://unsplash.com/">Unsplash</a> )
</p>
</center> </small>

<div id="outline-container-orgc4fbc71" class="outline-2">
<h2 id="orgc4fbc71"><span class="section-number-2">1</span> Part I ─Getting the mail</h2>
<div class="outline-text-2" id="text-1">
<ul class="org-ul">
<li>Have you ever been confused by packages?</li>

<li><p>
There are so many parts when it comes to packaging:
</p>
<ol class="org-ol">
<li>There's the stamps,</li>
<li>the addresses,</li>
<li>the contents,</li>
<li>the damage insurance,</li>
<li>the package weight,</li>
<li>customs declarations!</li>
</ol>

<p>
There's so many parts!
</p></li>

<li>Look at them! ⟪ gesture to screen ⟫
<ul class="org-ul">
<li>There's so many! The place is a mess!</li>
<li>This could be you parents' basement!</li>
<li>It could be your work desk!</li>
<li>In computing, this is your code! Mish-mashed from smaller pieces!</li>
<li>Eek!</li>
</ul></li>

<li>Unlike the day-to-day mail we receive, in computing, packages take the form of modules
and each of these separate, yet crucial, aspect of packaging corresponds to a different
<i>language</i>! ⟪Pause⟫</li>

<li><p>
Think about it: On average, a ‘coding language’ actually consists of at least 4 different
languages! That's like if your package in the mail had
</p>
<ul class="org-ul">
<li>a stamp in English,</li>
<li>an address in Spanish,</li>
<li>contents in Latin,</li>
<li>the insurance policy in German,</li>
<li>package weight in Gaelic.</li>
</ul>

<p>
What a headache!
</p></li>

<li>You shouldn't have to go to college to understand how your mail works!</li>
</ul>
</div>
</div>

<div id="outline-container-org1c5096c" class="outline-2">
<h2 id="org1c5096c"><span class="section-number-2">2</span> Part II ─Building blocks</h2>
<div class="outline-text-2" id="text-2">
<ul class="org-ul">
<li>Just as a clingy boyfriend, or a puppy that follows you around, is called a ‘dependent type’
we have those in computing too and they're essentially the same thing!

<ul class="org-ul">
<li>Software that uses dependent types lets us treat the different pieces
as if they were the same &#x2013;except packages, they still live in their own language.</li>
</ul></li>

<li>Packages are a building block of computing, like Lego(!):
You can build better, compact,
software using pre-built blocks. ⟪ Gesture hands together. ⟫</li>

<li>Since computing is so young as a science,
we don't really know how pieces should be glued together.

<ul class="org-ul">
<li>You'd think otherwise since you have a brick in your pocket
that can access all knowledge of humanity.</li>
</ul></li>

<li>For the most part, we cope by ad hoc methods &#x2013; “it works good enough”.

<ul class="org-ul">
<li>Unfortunately this does not scale!</li>
<li>⟪wave at slide⟫ package maintenance can get out of hand
rather quickly for any large piece of software!</li>
</ul></li>
</ul>
</div>
</div>

<div id="outline-container-orga8c2f25" class="outline-2">
<h2 id="orga8c2f25"><span class="section-number-2">3</span> Part III ─Arithmetic</h2>
<div class="outline-text-2" id="text-3">
<ul class="org-ul">
<li>Just as the clingy boyfriend and the puppy dog that follows you around can be seen as just
more family; with dependent types, packages too can be treated as just normal blocks.</li>

<li><p>
I'm researching the arithmetic of packages
to help software developers manage large projects and reduce complexity &amp; maintenance costs.
</p>

<p>
My research suggests that packaging can be treated in the same way we
treat numbers.
</p>

<ul class="org-ul">
<li>We can combine numbers by adding them, likewise we can combine packages by
putting their pieces together in a new package!</li>

<li>In grade school, “2 + 3 and 3 + 2 are both 5”,
yet with packages “2 + 3 might be 5, but 3 + 2 might not even exist!”</li>
</ul></li>
</ul>

<p>
So next time you send a package, think of what it means to me and how many
languages are involved!
</p>
</div>
</div>
<div class="taglist"><a href="https://alhassy.github.io/tags.html">Tags</a>: <a href="https://alhassy.github.io/tag-packages.html">packages</a> <a href="https://alhassy.github.io/tag-dependent-types.html">dependent-types</a> </div><center><strong> Generated by Emacs and Org-mode (•̀ᴗ•́)و </strong></center><center><a href="three-minute-thesis.org.html"><img
   src="https://img.shields.io/badge/-Source-informational?logo=read-the-docs"></a>&emsp;<a href="https://github.com/alhassy/alhassy.github.io/commits/master/posts/three-minute-thesis.org"><img
   src="https://img.shields.io/badge/-History-informational?logo=github"></a><br><a href="https://www.buymeacoffee.com/alhassy"><img src="https://img.shields.io/badge/-buy_me_a%C2%A0coffee-gray?logo=buy-me-a-coffee"></a></center>]]></description>
  <category><![CDATA[packages]]></category>
  <category><![CDATA[dependent-types]]></category>
  <link>https://alhassy.github.io/three-minute-thesis.html</link>
  <guid>https://alhassy.github.io/three-minute-thesis.html</guid>
  <pubDate>Tue, 12 Mar 2019 19:29:00 -0400</pubDate>
</item>
<item>
  <title><![CDATA[An Interactive Way To C]]></title>
  <description><![CDATA[</div><div style="text-align: center;">12 Jan 2019</div><center> <img src="images/interactive_way_to_c.png" alt="Article image"
            style="border: 2px solid black;" width="450" height="450" align="top" /> </center><br><center><strong>Abstract</strong></center>
<nav id="table-of-contents">
<h2><a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a></h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org47e2274">1. Introduction</a>
<ul>
<li><a href="#orgdb057a4">1.1. Getting Started</a></li>
<li><a href="#orgf1e46b3">1.2. A Prelude</a></li>
</ul>
</li>
<li><a href="#org93bf90a">2. Basic Constructs</a>
<ul>
<li><a href="#org72726a6">2.1. Assignment</a></li>
<li><a href="#org8424d18">2.2. Sequence</a></li>
<li><a href="#org637ffa8">2.3. Skip</a></li>
<li><a href="#org51b507c">2.4. Examples Using the Assignment, Sequence, Skip Rules</a></li>
<li><a href="#org5739048">2.5. Test; Conditional</a></li>
<li><a href="#org6d00c87">2.6. Loop</a></li>
</ul>
</li>
<li><a href="#orga9bfe17">3. ACSL Properties</a>
<ul>
<li><a href="#org44e8a5d">3.1. Predicates</a></li>
<li><a href="#orgecebd71">3.2. Logic Functions</a></li>
<li><a href="#orge283388">3.3. Axiomatic Definitions</a></li>
</ul>
</li>
<li><a href="#org6551851">4. Functions</a>
<ul>
<li><a href="#orgd5a0501">4.1. What are Functions?</a></li>
<li><a href="#orgf5ac87b">4.2. An Example Functional Contract</a></li>
<li><a href="#org8f973ed">4.3. Proving is Programming</a></li>
<li><a href="#org666258d">4.4. Maintaining The Sequence Rule</a></li>
<li><a href="#orgbc9010b">4.5. Passing Arguments by Value and by Reference</a></li>
<li><a href="#orgb3a9763">4.6. Side-effects: <code>assigns</code></a></li>
<li><a href="#orgb4dc9d3">4.7. Pointer Aliasing: <code>separated</code></a></li>
<li><a href="#org97e45ef">4.8. Functional Composition</a></li>
</ul>
</li>
<li><a href="#orge8a3822">5. Records</a>
<ul>
<li><a href="#org5481f16">5.1. Allocation of a Record</a></li>
<li><a href="#orgec54702">5.2. The Four Constructs of Records</a></li>
<li><a href="#orgf2627f4">5.3. Sharing, Equality, &amp; Garbage</a></li>
<li><a href="#orgfc2132a">5.4. Arrays</a></li>
</ul>
</li>
<li><a href="#orgdb5f42b">6. Arrays &#x2013; <code>\forall, \exists</code></a></li>
<li><a href="#org6f07fb1">7. Recursion</a>
<ul>
<li><a href="#org7a76a2e">7.1. Recursive Definitions and Fixed Point Equations</a></li>
<li><a href="#org4736a67">7.2. Programming without Assignment &#x2013;the Functional Core</a></li>
</ul>
</li>
<li><a href="#orgc9ce1d2">8. Hehner's Problem</a></li>
<li><a href="#org8c13011">9. Advanced Data Structures</a></li>
<li><a href="#orgcb55316">10. The Underlying Elisp</a></li>
</ul>
</div>
</nav>

<p>
Do you know what the above program accomplishes?
If you do, did you also spot a special edge case?
</p>

<p>
We aim to present an approach to program proving in C using a minimal Emacs setup
so that one may produce literate C programs and be able to prove them correct
&#x2013;or execute them&#x2013; using a single button press; moreover the output is again in Emacs.
</p>

<p>
The goal is to learn program proving using the Frama-C tool
&#x2013;without necessarily invoking its gui&#x2013; by loading the source of this file into
Emacs then editing, executing, &amp; proving as you read along.
One provides for the formal specification of properties of C programs &#x2013;e.g., using ACSL&#x2013;
 which can then be verified for the implementations using tools that interpret such annotation
&#x2013;e.g., Frama-C invoked from within our Emacs setup.
</p>

<p>
Read on, and perhaps you'll figure out how to solve the missing <code>FixMe</code> pieces 😉
</p>

<p>
The intent is for rapid editing and checking.
Indeed, the Frama-c gui does not permit editing in the gui, so one must switch between
their text editor and the gui.
<a href="https://orgmode.org/worg/org-tutorials/org4beginners.html">Org mode beginning at the basics</a> is a brief tutorial that covers a lot of Org and,
from the get-go, covers “the absolute minimum you need to know about Emacs!”
</p>

<p>
If anything, this effort can be construed as a gateway into interactive theorem proving
such as with Isabelle, Coq, or Agda.
</p>

<p>
The article <i>aims</i> to be self-contained &#x2013;not even assuming familiarity with any C!
</p>


<blockquote>
<p>
The presentation and examples are largely inspired by
</p>

<ul class="org-ul">
<li>Gilles Dowek's exquisite text <a href="https://www.springer.com/gp/book/9781848820319">Principles of Programming Languages</a>.
<ul class="org-ul">
<li>It is tremendously accessible!</li>
</ul></li>

<li>Allan Blanchard's excellent tutorial
<a href="https://allan-blanchard.fr/publis/frama-c-wp-tutorial-en.pdf">Introduction to C Program Proof using Frama-C and its WP Plugin</a>.</li>
</ul>

<p>
Another excellent and succinct tutorial is Virgile Prevosto's <a href="https://frama-c.com/download/acsl-tutorial.pdf">ACSL Mini-Tutorial</a>.
In contrast, the tutorial <a href="https://www.cs.umd.edu/class/spring2016/cmsc838G/frama-c/ACSL-by-Example-12.1.0.pdf">ACSL By Example</a> aims to provide a variety of algorithms
rendered in ACSL.
</p>
</blockquote>

<p>
There are no solutions since it's too easy to give up and look at the solutions that're
nearby. Moreover, I intend to use some of the exercises for a class I'm teaching ;-)
</p>

<div id="outline-container-org47e2274" class="outline-2">
<h2 id="org47e2274"><span class="section-number-2">1</span> Introduction</h2>
<div class="outline-text-2" id="text-1">
<p>
Despite its age, C is a widely used language and so is available on many platforms.
Moreover, many systems rely on code historically written in C that needs to be maintained.
</p>

<p>
The traditional way to obtain confidence that a program correctly works is to provide
inputs we believe to be representative of the actual use of the program and
verify the results we get are correct. Incidentally, the unexpected cases are often not
considered whereas they are generally the most dangerous ones.
Since we cannot test everything, we need to employ great care in selecting good tests.
</p>

<p>
Since it is hard to answer “Is our software tested enough?”, we consider mathematically
proving that there cannot be problems at runtime. That is, a specification of the expected
behaviour is provided, which is satisfied by the <i>resulting</i> program
&#x2013;note the order: Specify <i>then obtain</i> code! This two-stage process can produce errors
in either stage, yet whereas testing ensures “the program avoids the bugs we tested against”
this approach is a big step ensuring “the program doesn't contain bugs that don't exist in
the specification.”
</p>

<p>
The goal here is to use a tool to learn the basics of C program proof
&#x2013;Frama-C: FRAmework for Modular Analysis of C code.
In particular, to demonstrate the ability to write programs without any error
by emphasising the simple notions needed to write programs more confidently.
</p>

<p>
Testing is ‘dynamic analysis’ since it requires the actual execution of programs,
whereas our program proof approach is ‘static’ since no execution is performed but instead
we reason on a semantic model of the reachable states.
The semantics associated with the C language control structures and statements we will
use is known as Hoare Logic. One writes a “Hoare Triple” <i>{G} S {R}</i> to express that
“Starting in a given state (satisfying) <i>G</i>, the execution of <i>S</i> terminates such that the
resulting state (satisfies) R”.
</p>

<p>
How does one <i>prove</i> such a triple? Programs are constructed from a variety of pieces,
so it suffices to look at each of those. That is, <i>{G} S {R}</i> has the code <i>S</i> transform
the required predicate <i>R</i> to the given <i>G</i> &#x2013;since we usually know what is required
and it is the goal of the program. In general, we find the <i>weakest</i> precondition that
works since then it allows the largest possible number of inputs;
if <i>G</i> is at least as strong as the weakest precondition, then our program is correct but
may allow less then the largest amount of possible inputs.
</p>

<p>
For example, <i>{5 ≤ x ≤ 10} x ≔ x + 1 { 3 ≤ x ≤ 11}</i> is a correct program that ends in state
<i>3 ≤ x ≤ 11</i>, however its precondition could be weakened to <i>2 ≤ x ≤ 10</i> thereby allowing
many more  valid inputs.
</p>

<p>
In Frama-C, we do not use curly braces like this for such assertions but instead
express our properties as code annotations using the ANSI C Specification Language
&#x2013;ACSL or “axel”. The weakest precondition plugin uses the annotation and the code to
automatically ensure the program is correct according to the Hoare Logic fashion
mentioned earlier.
</p>
</div>

<div id="outline-container-orgdb057a4" class="outline-3">
<h3 id="orgdb057a4"><span class="section-number-3">1.1</span> Getting Started</h3>
<div class="outline-text-3" id="text-1-1">
<p>
The aim of this section is to introduce the Emacs controls that bring C to life within
literate code.
</p>

<table>


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

<col  class="org-left">

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><span class="underline">Key Press</span></td>
<td class="org-left"><span class="underline">Elisp Command</span></td>
<td class="org-left"><span class="underline">Description</span></td>
</tr>

<tr>
<td class="org-left">Enter <code>&lt;s</code> then <code>TAB</code></td>
<td class="org-left">─</td>
<td class="org-left">Produces a “Hello World” C template program.</td>
</tr>

<tr>
<td class="org-left"><code>F6</code></td>
<td class="org-left"><code>(interpret)</code></td>
<td class="org-left">Execute currently focused code blocks in a new buffer.</td>
</tr>

<tr>
<td class="org-left"><code>F7</code></td>
<td class="org-left"><code>(show-code)</code></td>
<td class="org-left">Shows currently focused code blocks in a new buffer.</td>
</tr>

<tr>
<td class="org-left"><code>F8</code></td>
<td class="org-left"><code>(frama-c)</code></td>
<td class="org-left">Open the Frama-C gui on the currently focused code blocks.</td>
</tr>

<tr>
<td class="org-left"><code>F9</code></td>
<td class="org-left"><code>(frama-c-no-gui)</code></td>
<td class="org-left">Invoke Frama-C on the currently focused code blocks in a new buffer.</td>
</tr>
</tbody>
</table>

<p>
Which code blocks are currently under focus is controlled by the command
<code>(currently-working-with "nameHere")</code>, which produces a file <code>nameHere.c</code> that is used
for the utility functions. If multiple blocks use the same filename, then the file is
appended to.
While reading this tutorial, bring/take code blocks in/out of focus by
toggling between <code>(currently-working-with "nameHere")</code> and
<code>(not-currently-working-with "nameHere")</code> &#x2014;that is, simply prepend <code>not-</code>.
Remember to undo such a toggle when you're done with a code block.
</p>

<p>
When no name is provided to <code>[not-]currently-working-with</code>, the name of the buffer is used.
</p>
</div>

<div id="outline-container-org45c83fa" class="outline-4">
<h4 id="org45c83fa"><span class="section-number-4">1.1.1</span> Exercise: Hello World</h4>
<div class="outline-text-4" id="text-1-1-1">
<ol class="org-ol">
<li>Insert the text <code>&lt;s</code> then press the <code>TAB</code> key.</li>
<li>A new C program template has appeared.</li>
<li>Press <code>F6</code> to execute the code in another buffer.</li>
<li>Press <code>F7</code> to inspect the code in another buffer.</li>
<li>Alter the program to output your name, press <code>F6</code>.</li>
</ol>

<p>
Now toggle that code block so that you are <code>not</code> currently working with it.
</p>
<ul class="org-ul">
<li>Make a change to the code block, such as printing 42. Notice that <code>F6</code> refers
to the old file on disk since there is no currently focused block.</li>
</ul>
</div>
</div>

<div id="outline-container-org7d3e587" class="outline-4">
<h4 id="org7d3e587"><span class="section-number-4">1.1.2</span> Exercise: Verifying Swap</h4>
<div class="outline-text-4" id="text-1-1-2">
<p>
Consider the fully annotated <code>swap</code> algorithm, i.e., remove the <code>not-</code> prefix,
</p>
<div class="org-src-container">
<pre class="src src-c" id="orgcf78824"><span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">#include&lt;stdio.h&gt; // for IO</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@</span>
<span style="color: #96A7A9; font-style: italic;">requires \valid(a) &amp;&amp; \valid(b);</span>
<span style="color: #96A7A9; font-style: italic;">assigns *a, *b;</span>
<span style="color: #96A7A9; font-style: italic;">ensures *a == \old(*b);</span>
<span style="color: #96A7A9; font-style: italic;">ensures *b == \old(*a);</span>
<span style="color: #96A7A9; font-style: italic;">*/</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">swap</span>(<span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">a</span>, <span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">b</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">tmp</span> = *a;
  *a = *b;
  *b = tmp;
}

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">printf("Hello World!\n");</span>

  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span> = 42;
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">b</span> = 37;

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert Before_Swap</span><span style="color: #96A7A9; font-style: italic;">: a == 442 &amp;&amp; b == 37;</span>
  swap(&amp;a, &amp;b);
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert After_Swap</span><span style="color: #96A7A9; font-style: italic;">: a == 37 &amp;&amp; b == 42;</span>

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
We can see that Frama-C proves these assertions by obtaining “green bullets”
beside them if we execute
</p>
<pre class="example">
frama-c-gui -wp -rte myfile.c
</pre>
<p>
Or check-boxes beside them if we instead execute
</p>
<pre class="example">
frama-c-gui -gui-theme colorblind -wp -rte myfile.c
</pre>
<p>
The best way to know which options are available is to use
</p>
<pre class="example">
frama-c -wp-help
</pre>

<p>
We will however use the special Emacs calls defined at the bottom of this file,
<code>frama-c</code> and <code>frama-c-no-gui</code>, to avoid having to switch between a terminal and a
text editor. Thank-you extensible editor Emacs ⌣̈ ♥
</p>

<ul class="org-ul">
<li>Press <code>F8</code> to invoke the Frama-C gui.</li>
<li>Press <code>F9</code> to invoke Frama-C within Emacs and obtain status about the program proof.</li>
</ul>

<p>
If you uncomment the IO matter, Frama-C may yield an error.
<b>Separate your IO into its own driver file!</b>
Invoke Frama-C only on programs you want to prove &#x2013;without any IO.
</p>

<p>
Go back to the above example, and change the first assertion in <code>main,
~a == 42</code>, to assert that <code>a</code> equals <code>432</code>. Now invoke <code>M-x framac-no-gui</code>, or press <code>F9</code>,
to obtain the message,
</p>
<pre class="example">
Frama-C: 90﹪ of proof complete!
</pre>
<p>
Moreover, another buffer will be open and in red will be highlighted,
</p>
<pre class="example">
[wp] [Alt-Ergo] Goal typed_main_assert_Before_Swap : Unknown (Qed:0.63ms) (57ms)
</pre>
<p>
This indicates that, in method <code>main</code>, the named assertion <code>Before_Swap</code> could not be proven.
</p>

<p>
Now revert all alterations and in the specification of <code>swap</code>, alter
<code>ensures *a ==== \old(*b);</code> to become <code>ensures *a == \old(*a);</code>, thereby expressing
that the value of <code>a</code> is unaltered by the program. Checking this yields a false
assertion! Neato.
</p>

<p>
As such, I suggest the following literate process:
</p>
<ol class="org-ol">
<li>Write your code in Org-mode code blocks,</li>
<li>Check it works with <code>frama-c-no-gui</code> (F9) or <code>frama-c</code> (F8).
<ul class="org-ul">
<li>If we open the gui, we may right-click on a function name and select
<code>Prove function annotations by WP</code> to have our assertions checked.</li>
<li>Remember that the frama-c gui does not allow source code edition.</li>
</ul></li>
<li>Investigate output, then make changes in source file and re-check.</li>
</ol>

<p>
Observe
</p>
<ul class="org-ul">
<li>Program proof is a way to ensure that our programs only have correct behaviours,
described by our specification;</li>
<li>It is still our work to ensure that this specification is correct.</li>
</ul>
</div>
</div>
</div>

<div id="outline-container-orgf1e46b3" class="outline-3">
<h3 id="orgf1e46b3"><span class="section-number-3">1.2</span> A Prelude</h3>
<div class="outline-text-3" id="text-1-2">
<p>
Since C's <code>#include</code> is essentially a copy-paste, we can re-export other libraries
from a make-shift ‘Prelude’.
</p>

<div class="org-src-container">
<pre class="src src-c" id="org3688d34">
<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Artefacts so that exercises let the system progress as much as possible.</span>
<span style="color: #96A7A9; font-style: italic;">//</span>
<span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ predicate Exercise = \false;</span>
<span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ predicate FixMe = \false;</span>
<span style="color: #268bd2; font-weight: bold;">#define</span> <span style="color: #268bd2;">FixMeCode</span>

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Tendency to require this header file to specfiy avoidance of overflow.</span>
<span style="color: #96A7A9; font-style: italic;">//</span>
<span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>
</pre>
</div>

<p>
We will continue to be <code>(currently-working-with "Prelude")</code> in the future to add more
content. For now, we put the artefacts needed to let the exercises pass.
</p>

<blockquote>
<p>
The use of <code>\false</code> is not the most appropriate, since its occurrence in a precondition
  vacuously makes everything true! This is something that should change.
</p>

<p>
The current setup produces only <code>.c</code> files, whence we use the prelude by declaring
<code>#include "Prelude.c".</code> Forgive my use of a <code>.c</code> file in-place of a header file.
The alternative is to force all code block names to end in a <code>.c</code>.
</p>
</blockquote>
</div>
</div>
</div>

<div id="outline-container-org93bf90a" class="outline-2">
<h2 id="org93bf90a"><span class="section-number-2">2</span> Basic Constructs</h2>
<div class="outline-text-2" id="text-2">
<p>
Recall that a Hoare Triple <i>{G} S {R}</i> expresses that if execution of program <code>S</code> is begun
in a state satisfying proposition <i>G</i> then it terminates in a state satisfying proposition <i>R</i>.
We usually know <i>R</i> &#x2013;it is the required behaviour on <code>S</code> after all&#x2013; but otherwise we usually
only have a vague idea of what <i>G</i> could possible be.
Dijkstra's <i>weakest precondition</i> operator ‘wp’ tells us how to <b>compute</b> <i>G</i> from <i>R</i>
&#x2013;in the process we usually <b>discover</b> <code>S</code>.
</p>

<p>
Hence, all in all, programming begins with the required goal from which code is then derived.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <p>
Post-conditions <i>R</i> are expressed using the <code>ensures</code> clause, and dually pre-conditions <i>G</i>
are expressed using <code>requires</code> clauses. These <i>G</i> are properties assumed for the input
and it is the callers responsibility to ensure that they are true
&#x2013;recall that when a contract is breached, the implementation may behave arbitrarily.
</p>
 </details>

<p>
Since ‘wp’ is intended to <b>compute</b> the weakest precondition establishing a given
postcondition <i>R</i> for a statement <code>S</code>, it necessarily satisfies
</p>
<pre class="example">
    { G } S { R }   ≡   G ⇒ wp S R
</pre>

<p>
The left side may be rendered using ACSL,
</p>
<pre class="example">
// @ assert G;
S;
// @ assert R;
</pre>

<p>
The WP plugin for Frama-C essentially works by computing <code>wp S R</code> then attempts to obtain
a proof for <code>G ⇒ wp S R</code>.
In particular, by the reflexivity of implication, ‘wp’ guarantees to produce a
precondition so that the following Hoare triple is valid.
</p>
<pre class="example">
   { wp S R } S { R }
</pre>

<p>
Most programming languages have, among others, five constructs:
Assignment, variable declaration, sequence, test, and loop.
These constructs from the <i>imperative core</i> of the language.
Since programs are built using such primitive control structures, it suffices to define
wp “by induction” on the shape of <code>S</code>.
</p>

<p>
One reasonable property we impose on wp from the outset is: <br>
If <i>S</i> establishes <i>R</i> which implies <i>R′</i>, then <i>S</i> also establishes <i>R′</i>.
</p>
<pre class="example">
Monotonicity: R ⇒ R′   implies  wp S R ⇒ wp S R′
                       That is, {wp S R} S {R′}
</pre>
<p>
Whence for each definitional clause of wp, we must ensure this desirable property is held.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <p>

</p>

<p>
Imperative programs alter state, as such a statement <code>S</code> is essentially a function
that transforms the memory state of the computer.
Expressing in English what happens when a statement is executed is possible
for simple examples, but such explanations quickly become complicated and imprecise.
Therefore, one introduces a theoretical framework reifying statements as state transformers.
</p>

<p>
The two popular notions are the “forwards” <code>⟦S⟧</code> moves current state to a new state,
whereas we are working with “backwards” <code>wp S</code> which takes a desired state and yields
a necessary previous state. The forward notion ‘executes’ a program by starting in
the empty state and stepping through each command to result in some final state.
Whereas the backwards notion takes an end goal and suggests which programming constructs
are needed to obtain it.
Hence, ‘forwards’ is verification whereas ‘backwards’ is correct-by-construction
programming.
</p>

<p>
Suppose there is an infinite set <code>Var</code> of variables and an infinite set <code>Val</code> of values,
which are integers, booleans, etc. In the ‘forwards’ notion, a <i>state</i> is a partial
function from variables to values &#x2013;`partial' since it may be undefined at some
variables, since we usually use only finitely many
in our programs anyways. E.g., state <code>{x ↦ 5, y ↦ 6}</code> associates the value 5 to variable <code>x</code>
but is undefined for variable <code>z</code>. Dually, in the ‘backwards’ notion, a <i>state</i> is a predicate of the
variables and their values that satisfy the predicate; e.g., the previous example state
corresponds to the predicate <code>x = 5 ∧ y = 6</code>, where <code>z</code> can have <i>any</i> value.
Hence the predicate formulation is more relaxed and we shall refer to it instead.
</p>
 </details>
</div>

<div id="outline-container-org72726a6" class="outline-3">
<h3 id="org72726a6"><span class="section-number-3">2.1</span> Assignment</h3>
<div class="outline-text-3" id="text-2-1">
<p>
The <i>assignment</i> construct allows the creation of a statement with a variable
<code>x</code> and an expression <code>E</code>. The assignment statement is written <code>x = E;</code>.
</p>
<ul class="org-ul">
<li>Variables are identifiers which are written with one or more letters.</li>
<li>Expressions are composed of variables, constants, and operator calls.</li>
<li>Sometimes one <i>notates</i> assignment by <code>x ≔ E</code> even though it is invalid C code.</li>
</ul>

<p>
To understand the execution of an assignment, suppose that within the
recesses of your computer's memory, there is a compartment labelled <code>x</code>.
Obtain the <i>value</i> of <code>E</code> &#x2013;possibly having to look up values of variables
that <code>E</code> mentions&#x2013; then erase the contents of <code>x</code> and fill the compartment
with the newly obtained value.
</p>

<p>
The whole of the contents of the computer's memory is called a <i>state</i>.
We also say “predicate <i>R</i> is the current state” as shorthand for:
The current state is (non-deterministically) <i>any</i> variable-value assignment
such that predicate <i>R</i> is true.
</p>

<p>
All in all, executing <code>x ≔ E</code> loads memory location <code>x</code> with the value of expression <code>E</code>;
hence state <i>R</i> is satisfied after an assignment precisely when it is satisfied
with variable <code>x</code> replaced by expression <code>E</code>. For example, <code>wp (x ≔ x+1) (x = 5)  ≡  (x+1 = 5)</code>.
</p>
<pre class="example">
wp (x ≔ E) R  ≡  R[x ≔ E]
</pre>

<p>
Before being able to assign values to a variable <code>x</code>, it must be <i>declared</i>,
which associates the name <code>x</code> to a location in the computer's memory.
<i>Variable declaration</i> is a construct that allows the creation of a statement
composed of a variable, an expression, and a statement. This is written
<code>{T x = e; p}</code>, then variable <code>x</code> can be used in statement <code>p</code>, which is called
the <i>scope</i> of variable <code>x</code>.
( When <code>p</code> has no assignments, functional programmers would call this statement
  a <i>let statement</i> since it lets <code>x</code> take on the value <code>e</code> in <code>p</code>. )
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <ul class="org-ul">
<li>Division:
If both arguments are integers, then the operation rounds down; write, e.g., <code>5 / 2.0</code>
to mark the result as floating point.</li>

<li>Modulo: The number <code>a % b</code> is <code>a - b * (a / b).</code></li>

<li>Ternary Conditional: For all types, the expression <code>(c) ? t : f</code>
yields <code>t</code> if boolean <code>c</code> holds and is <code>f</code> otherwise.</li>

<li>A useful inclusion: <i>2*10<sup>9</sup> &lt;= 2<sup>32</sup> &lt;= 2*10<sup>10</sup></i></li>
</ul>
 </details>
</div>

<div id="outline-container-org73c162d" class="outline-4">
<h4 id="org73c162d"><span class="section-number-4">2.1.1</span> Overshadowing &amp; Explicit Scope Delimitation</h4>
<div class="outline-text-4" id="text-2-1-1">
<p>
Imperative languages generally do not allow the declaration of the same variable multiple
times, e.g., the following program crashes with <code>error: redefinition of ‘x’</code>.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 3;
  printf(<span style="color: #2aa198;">"x has value: %d"</span>, x);

  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 4;
  printf(<span style="color: #2aa198;">"x has value: %d"</span>, x);

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
<p>
However, if we <i>explicitly delimit the scope</i> of a variable by using braces, then we can
obtain multiple declarations:
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 3;
  printf(<span style="color: #2aa198;">" x has value: %d"</span>, x);  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">3</span>

  { <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 4;
    printf(<span style="color: #2aa198;">"\n x has value: %d"</span>, x); <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">4</span>
  }
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
When explicitly delimiting scope, it is the most recent declarations
that are used. We say that earlier declarations are <i>hidden</i>, or <i>overshadowed</i>,
by the later declarations.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 3;

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert x == 3;</span>

  { <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Begun new scope</span>

    <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Old facts are still true.</span>
    <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert x == 3;</span>

    <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Now overshadowing &#8216;x&#8217;</span>
    <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 4;

    <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">This new &#8216;x&#8217; is equal to 4.</span>
    <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert x == 4;</span>
  }

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Back to the parent scope.</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">In this scope, &#8216;x&#8217; still has its orignal value.</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert x == 3;</span>

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
 </details>
</div>
</div>

<div id="outline-container-orga8d5445" class="outline-4">
<h4 id="orga8d5445"><span class="section-number-4">2.1.2</span> Constant Variables</h4>
<div class="outline-text-4" id="text-2-1-2">
<p>
<i>Constant variables</i> are variables which may have only one initial value that
can never be changed. A non-constant variable is called <i>mutable</i>, which is
the default in imperative languages. For example, the following
crashes with <code>error: assignment of read-only variable ‘x’</code>.
</p>

<div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #859900; font-weight: bold;">const</span> <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 3;
  x = 4;
  printf(<span style="color: #2aa198;">"x has value: %d"</span>, x);

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
</div>
</div>
</div>

<div id="outline-container-org8424d18" class="outline-3">
<h3 id="org8424d18"><span class="section-number-3">2.2</span> Sequence</h3>
<div class="outline-text-3" id="text-2-2">
<p>
A <i>sequence</i> is a construct that allows a single statement to be created out of
two statements <code>p</code> and <code>q</code>; it is written <code>{p q}</code>.
The sequence is executed in state <code>s</code> by first executing <code>p</code> in state <code>s</code> thereby
producing a new state <code>s'</code> in which statement <code>q</code> is then executed.
</p>
<ul class="org-ul">
<li>The statement <code>{p₁ {p₂ { ... pₙ } ...}}</code> can also be written <code>{p₁ p₂ ... pₙ}</code>.</li>
</ul>

<p>
Usually a ‘;’ symbol is used in favour of a space, with braces, to yield,
</p>
<pre class="example">
wp (S₁;S₂) R  ≡  wp S₁ (wp S₂ R)
</pre>
<p>
The pre-condition of the second statement becomes the post-condition of the first
statement. Hence, we “push” along the post-condition into a sequence:
In the upcoming swapping example, we read the proof steps from bottom to top!
</p>

<p>
Rendered pointfree, i.e., ignoring <i>R</i>, this rule becomes: <i>wp (S₁;S₂)  = wp S₁ ∘ wp S₂</i>.
</p>

<p>
Recall that we need to ensure monotonicity is satisfied, and indeed: If <i>R ⇒ R′</i>, then
</p>
<pre class="example">
  wp (S₁;S₂) R
≡ wp S₁ (wp S₂ R)    -- Definition of wp on sequence
⇒ wp S₁ (wp S₂ R′)   -- Monotoncity for Sᵢ, twice; with assumption R ⇒ R′
≡ wp (S₁;S₂) R      -- Definition of wp on sequence
</pre>
<p>
Neato!
</p>

<p>
Moreover, notice we have the useful ‘transitivity’ property for Hoare triples:
</p>
<pre class="example">
   {G} S₁ {R′}  ∧  {R′} S₂ {R}
≡  (G ⇒ wp S₁ R′)  ∧  (R′ ⇒ wp S₂ R)              -- Characterisation of wp
⇒ (G ⇒ wp S₁ R′)  ∧  (wp S₁ R′ ⇒ wp S₁ (wp S₂ R)) -- Monotonicity of wp
⇒ (G ⇒ wp S₁ (wp S₂ R))                           -- Transitivity of implication
≡  G ⇒ wp (S₁;S₂) R                               -- Definition of wp on sequence
≡  {G} S₁;S₂ {R}                                  -- Characterisation of wp
</pre>

<p>
Exercise: Show that <code>wp (x ≔ E; S) R  ≡  (wp S R)[x ≔ E]</code>.
</p>
</div>
</div>

<div id="outline-container-org637ffa8" class="outline-3">
<h3 id="org637ffa8"><span class="section-number-3">2.3</span> Skip</h3>
<div class="outline-text-3" id="text-2-3">
<p>
The “empty sequence” is denoted <code>{}</code> or just <code>;</code> in the C language.
It is also commonly known as the <code>skip</code> construct and its importance is akin to that
of zero to addition.
</p>

<pre class="example">
wp skip R  ≡  R
</pre>
<p>
The “do nothing” program <code>skip</code> is rendered as simple <code>;</code> or as whitespace in the C language.
This program does not alter the state at all, thus it truthifies <i>R</i> precisely when <i>R</i>
was true to begin with.
</p>

<p>
Most often this appears in a weakening/strengthening form,
</p>
<pre class="example">
...code here...
//@ assert P;
//@ assert Q;
...code here...
</pre>
<p>
Where <i>P ⇒ Q</i> is provable.
</p>

<p>
More concretely,
</p>
<div class="org-src-container">
<pre class="src src-c" id="orgee3e6e1"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires 3 &lt; a &lt; 9;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures  -20 &lt;= \result &lt;= 99;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">using_skip</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>)
{
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert our_strong_pre</span><span style="color: #96A7A9; font-style: italic;">:         3 &lt; a &lt; 9;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert weakened_intermediary</span><span style="color: #96A7A9; font-style: italic;">: -7 &lt;= a &lt;= 14;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert weakening_futher</span><span style="color: #96A7A9; font-style: italic;">:     -20 &lt;= a &lt;= 99;</span>
  <span style="color: #859900; font-weight: bold;">return</span> a;
}
</pre>
</div>
<p>
Woah! It looks like the identity function somehow transforms input satisfying
<i>3 &lt; x &lt; 9</i> to input satisfying <i>-20 ≤ x ≤ 99</i>.
Wait, the former implies the latter and that's just the definition of <i>wp</i> on <code>skip</code>.
</p>

<p>
The above example suggests the following calculation,
</p>
<pre class="example">
   (G′ ⇒ G)  ∧  {G} S {R}  ∧  (R ⇒ R′)
≡  (G′ ⇒ G)  ∧  (G ⇒ wp S R)  ∧  (R ⇒ R′) -- Characterisation of wp
⇒ (G′ ⇒ wp S R)  ∧  (R ⇒ R′)              -- Transitivity of implication
⇒ (G′ ⇒ wp S R)  ∧  (wp S R ⇒ wp S R′)    -- Monotonicity of wp
⇒ (G′ ⇒ wp S R′)                          -- Transitivity of implication
≡  {G′} S {R′}                            -- Characterisation of wp
</pre>
<p>
That is, strengthening the precondition or weakening the post-condition leaves
a Hoare triple valid. In some industry circles &#x2013;e.g., C#&#x2013;, this is referred to as
contravariance (antitone) in the input and covariance (monotone) in the output.
</p>

<p>
For example, if <code>G′, G, R, R′</code> were classes such that <code>G′</code> is a subclass of <code>G</code>
and <code>R</code> is a subclass of <code>R′</code>, then the program <code>S</code> takes an input of type <code>G</code> yielding an
output of type <code>R′</code>. However, any input of type <code>G′</code> can be cast into the parent-class
type <code>G</code> and, likewise, <code>R</code> objects can be cast into the parent-type <code>R′</code>.
Thus, program <code>S</code> can also take the type of <code>G′</code> to <code>R′</code>.
</p>

<p>
Writing <code>&lt;:</code> for ‘sub-type’, or ‘sub-class’, we have argued,
</p>
<pre class="example">
Provided    G′ &lt;: G  and R &lt;: R′
Then
       G → R  &lt;:  G′ → R′
</pre>
<p>
It is now easier to see that the second argument of function-type former ‘→’ stays
on the same side of the <code>&lt;:</code> symbol, whereas it is flipped for the first argument.
</p>

<p>
Completely unrelated &#x2013;or not&#x2013; a nearly identical property holds for implication:
If <i>G′ ⇒ G</i> and <i>R ⇒ R′</i> then (G ⇒ R) ⇒ (G′ ⇒ R′). How coincidental &#x2026; or not!
\\ ( Foreshadowing: Curry-Howard Correspondence! )
</p>

<p>
Anyhow, this strengthening-weakening law will be useful when computing the <i>wp</i> of a
statement directly is difficult &#x2013;and possibly unhelpful&#x2013; but we have a stronger
precondition and so it suffices to use that.
( Foreshadowing: Loops! )
</p>

<p>
Before we close, here is an <b>exercise</b> to the reader: An alternate proof of the above law.
</p>
<pre class="example">
   (G′ ⇒ G)  ∧  {G} S {R}  ∧  (R ⇒ R′)
≡  {G′} skip {G} ∧ {G} S {R} ∧ {R} skip {R′}    -- ???
⇒ {G′} skip; S {R} ∧ {R} skip {R′}             -- ???
⇒ {G′} skip; S; skip {R′}                      -- ???
≡  {G′} S {R′}                                  -- skip is no-op &amp; can be removed.
</pre>

<p>
The last hint in the above calculation deserves some attention.
</p>
<ol class="org-ol">
<li>Rendered pointfree, i.e., ignoring <i>R</i>, the skip rule becomes: <i>wp skip  = id</i>.
<ul class="org-ul">
<li>Where <i>id</i> is the identity function: <i>id(x) = x</i>.</li>
</ul></li>
<li>“Program Equality”: Say <code>S ≈ T</code> precisely when <code>wp S = wp T</code>.
<ul class="org-ul">
<li>Two programs are considered equal precisely when they have the same
<i>observational</i> behaviour &#x2013;i.e., can satisfy the same set of post-conditions <i>R</i>.</li>
</ul></li>
<li>Identity of Sequence Theorem: <code>S ; skip  ≈  S  ≈  skip ; S</code>.</li>
<li>Likewise, define <i>wp abort R ≡ false</i>  &#x2013; i.e., <code>abort</code> is a program that crashes on
any input.</li>
<li>Zero of Sequence Theorem: <code>S ; abort  ≈  abort  ≈  abort ; S</code>.</li>
</ol>
</div>
</div>

<div id="outline-container-org51b507c" class="outline-3">
<h3 id="org51b507c"><span class="section-number-3">2.4</span> Examples Using the Assignment, Sequence, Skip Rules</h3>
<div class="outline-text-3" id="text-2-4">
<p>
To avoid having to write the verbose <code>\at(x, Pre)</code> to refer to the value of a variable <code>x</code>
before method execution, we may use a <i>ghost variable</i>: A variable whose purpose is only
to make the assertions provable, and otherwise is not an execution-relevant variable.
</p>

<div class="org-src-container">
<pre class="src src-c" id="orgc250123"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires \valid(x) &amp;&amp; \valid(y);</span>
<span style="color: #96A7A9; font-style: italic;">  @ requires INT_MIN &lt; *x + *y &lt; INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ requires \separated(x, y); // Exercise</span><span style="color: #96A7A9; font-style: italic;">: It's a swap, why is this needed?</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns *x, *y;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">swap</span>(<span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">x</span>, <span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">y</span>)
{
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ ghost int X = *x;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ ghost int Y = *y;</span>

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert  *y == Y  &amp;&amp; *x == X;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert  *y == Y  &amp;&amp; (*x + *y) - *y == X;</span>
  *x = *x + *y;
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert  *y == Y  &amp;&amp; *x - *y == X;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert  *x - (*x - *y) == Y  &amp;&amp; *x - *y == X;</span>
  *y = *x - *y;
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert  *x - *y == Y  &amp;&amp; *y == X;</span>
  *x = *x - *y;
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert  *x == Y  &amp;&amp; *y == X;</span>

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">&#120034;&#120061;&#120042;&#120059;&#120061; upwards reading from here;</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">each assertion is obtained by the assigment, skip, and sequence rules.</span>
}
</pre>
</div>

<p>
Here is a more complicated exercise that also makes use of external functions&#x2026;
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <p>

</p>

<div class="org-src-container">
<pre class="src src-c" id="org7e4f5ca"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>

<span style="color: #268bd2; font-weight: bold;">#define</span> <span style="color: #268bd2;">RAND_MAX</span> 32767

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures 0 &lt;= \result &lt;= RAND_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">*/</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">rand</span>();

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires min &lt;= max;</span>
<span style="color: #96A7A9; font-style: italic;">  @ requires min + RAND_MAX &lt; INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  @ requires max - min &lt; INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures min &lt;= \result &lt;= max;</span>
<span style="color: #96A7A9; font-style: italic;">*/</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">random_between</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">min</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">max</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">it</span> = rand();
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert weakening</span><span style="color: #96A7A9; font-style: italic;">: 0 &lt;= it &lt;= RAND_MAX;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert assignment_rule_again</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
  it = it % (max - min + 1);
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #b58900;">@ assert simplify</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #b58900;">@ assert assignment_rule</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
  it = it + min;
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">@ assert min &lt;= it;</span>

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Start at the bottom, and push assertion upwards!</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">The assertion names are also intended to be read upwards;</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Each justifies how it was obtained.</span>

  <span style="color: #859900; font-weight: bold;">return</span> it;

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">That is,</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">return (rand() % (max - min + 1)) + min;</span>
}
</pre>
</div>
 </details>
</div>
</div>

<div id="outline-container-org5739048" class="outline-3">
<h3 id="org5739048"><span class="section-number-3">2.5</span> Test; Conditional</h3>
<div class="outline-text-3" id="text-2-5">
<p>
A <i>test</i> is a statement formed from a Boolean expression and two statements; it is
written \\ <code>if (b) p else q</code> &#x2013;sometimes a ‘then’ keyword is used for readability, but
such is not valid C code.
This is executed in a state by evaluating the Boolean
then deciding which branch to execute in the same state.
</p>

<pre class="example">
wp (if B then S₁ else S₂) R  ≣  if B then wp S₁ R else wp S₂ R
                             ≡ (B ⇒ wp S₁ R) ∧ (¬ B ⇒ wp S₂ R)
</pre>
<p>
A conditional ensures <i>R</i> precisely when its branches each ensure <i>R</i>.
</p>

<p>
Observe the following calculation,
</p>
<pre class="example">
   { G } if B then S₁ else S₂ { R }
≡  G ⇒ wp (if B then S₁ else S₂) R         -- Characterisation of wp
≡  G ⇒ (B ⇒ wp S₁ R) ∧ (¬ B ⇒ wp S₂ R)     -- Definition of wp on conditional
≡  (G ⇒ B ⇒ wp S₁ R) ∧ (G ⇒ ¬ B ⇒ wp S₂ R) -- Characterisation of meets
≡  (G ∧ B ⇒ wp S₁ R) ∧ (G ∧ ¬ B ⇒ wp S₂ R) -- Shunting
≡  {G ∧ B} S₁ {R}  ∧  {G ∧ ¬ B} S₂ {R}     -- Characterisation of wp
</pre>
<p>
That is, Hoare triples on a conditional ‘distribute’ into the branches
with each branch precondition obtaining the branch guard.
</p>
</div>
</div>

<div id="outline-container-org6d00c87" class="outline-3">
<h3 id="org6d00c87"><span class="section-number-3">2.6</span> Loop</h3>
<div class="outline-text-3" id="text-2-6">
<p>
A <i>loop</i> is a construct formed from a Boolean expression and a statement; it is
written <code>while (b) p</code>.
A loop is one of the ways in which we can express an infinite object &#x2013;which may
fail to terminate&#x2013; using a finite expression. Indeed, its executional behaviour
can be understood by realising it as a shorthand for the expression
</p>
<pre class="example">
if (b) {p if (b) {p if (b) ...
                    else skip}
          else skip}
else skip
</pre>
<p>
Where <code>skip</code> is the fictional statement that performs no action when executed.
</p>

<p>
To understand the semantics of the loop:
</p>
<ol class="org-ol">
<li>Let <code>giveup, terminate</code> be aliases for <code>abort</code> and <code>skip</code>.</li>
<li><p>
Recalling that a loop is a shorthand for an infinite nesting of conditionals,
we try to approach it as a limit of finite approximations.
</p>
<pre class="example">
     while (b) q  ≈  limₙ pₙ

  Where:
  p₀ = if (b) giveup else terminate
  p₁ = if (b) {q ; if b giveup else terminate} else terminate
  ⋯
  pₙ₊₁ = if (b) {q; pₙ} else terminate;
</pre></li>

<li>The statement <code>pₙ</code> tries to execute the statement <code>while (b) q</code> by completing
a maximum of <code>n</code> trips through the loop. If, after <code>n</code> loops, it has not
terminated on its own, it gives up.</li>

<li><p>
If the loop terminates in <i>m</i> trips, it also terminates in <i>n ≥ m</i> trips.
</p>
<ul class="org-ul">
<li><i>∀ m. pₘ defined ⇒ ∀ n ≥ m. pₙ defined</i></li>
<li><i>∀ m. pₘ defined ⇒ ∀ n ≥ m. pₙ ≈ pₘ</i></li>
</ul>

<p>
Where ‘defined’ means it terminates without aborting.
</p></li>
<li>Hence, by these two claims, we know that the sequence <i>pₙ</i> either never defined
or it is defined beyond a certain point, and in this case, it is constant over its domain.</li>
<li>Hence: <code>while(b) q ≈ limₙ pₙ</code>.</li>
</ol>

<p>
The definition of ‘wp’ for loops is complicated and generally unhelpful, however
from it we can prove the so called “Invariance Theorem”:
If a property is maintained by the body of the loop
and there is an integral value expressed using the body's variables
that starts out non-negative and is decreased by each loop pass,
then the loop will terminate and the property it maintained will be true.
</p>

<pre class="example">
   {Inv ∧ B ∧ bf = c} S {Inv ∧ bf &lt; c}
⇒
   {Inv ∧ bf ≥ 0}  while(B) S  {¬B ∧ Inv}
</pre>
<p>
A property that is maintained to be true throughout the loop is referred to as an
<i>invariant</i>. In contrast, a value that changes through every pass
&#x2013;such as the number of passes remaining, the <code>bf</code>&#x2013; is known as a <i>variant</i>.
</p>

<p>
In Frama-C rendition,
</p>
<pre class="example">
/*@ loop invariant ⋯  // property that is maintained by the loop body
  @ loop assigns ⋯    // variables that are altered by the loop body
  @ loop variant ⋯    // a bound on the total number of loops
  */
while(B) S;
</pre>

<p>
Incidentally the primary function of the <code>assigns</code> clause is that variables its does
not mention essentially have the invariant property of being equal to their value
before the loop. That is, the <code>assigns</code> clause reduces clutter regarding constants
from the invariant!
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c" id="org91ddc47"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires N &gt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">  @ requires N * (N + 1) /2 &lt; INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures \result == N * (N + 1) / 2;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">euclid</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">N</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">sum</span> = 0; <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span> = 0;
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert invariant_intially_estabished</span><span style="color: #96A7A9; font-style: italic;">: 2 * sum == 0 * (0 + 1);</span>

  <span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #b58900;">@ loop invariant main_item</span><span style="color: #96A7A9; font-style: italic;">: sum == n * (n + 1) / 2;</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant always_in_range</span><span style="color: #96A7A9; font-style: italic;">: 0 &lt;= n &lt;= N;</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant range_for_sum</span><span style="color: #96A7A9; font-style: italic;">: 0 &lt;= sum;</span>
<span style="color: #96A7A9; font-style: italic;">    // Exercise: Why can we comment out the following two lines?</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant no_overflow1</span><span style="color: #96A7A9; font-style: italic;">: n &lt; INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant no_overflow2</span><span style="color: #96A7A9; font-style: italic;">: sum &lt;= INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop assigns n, sum;</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop variant N - n;</span>
<span style="color: #96A7A9; font-style: italic;">   */</span>
  <span style="color: #859900; font-weight: bold;">while</span>(n != N)
  {
    <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert sum + n &lt; INT_MAX;</span>
    n   = n + 1;
    <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert sum + n &lt;= INT_MAX;</span>
    sum = sum + n;
    <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert sum &lt;= INT_MAX;</span>
  }
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert invariant_and_not_guard</span><span style="color: #96A7A9; font-style: italic;">: n == N  &amp;&amp;  sum == n * (n + 1) / 2;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert post_condition</span><span style="color: #96A7A9; font-style: italic;">:    sum == N * (N + 1) / 2;</span>
  <span style="color: #96A7A9; font-style: italic;">//         </span><span style="color: #96A7A9; font-style: italic;">rewrite_post:  2 * sum == N * (N + 1);  // Not true, due to &#8216;rounding&#8217;!</span>

  <span style="color: #859900; font-weight: bold;">return</span> sum;
}
</pre>
</div>
<p>
<b>Exercise:</b> Why is <code>sum ≤ INT_MAX</code> true at the end of the loop body? Fill in the proof:
</p>
<pre class="example">
   sum ≤ INT_MAX
≡  n * (n + 1) / 2 ≤ INT_MAX                     --  ???
⇐ n * (n + 1) / 2 ≤ N * (N + 1) / 2 ≤ INT_MAX   --  Transtivitity of ≤
≡  N * (N + 1) / 2 ≤ INT_MAX                     --  ???
≡  true                                          --  ???
</pre>
 </details>
<p>
Here's a simple exercise,
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c" id="orga4d0095"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires a + 10 &lt; INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures \result == \old(a) + 10;</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">look_ma_no_new_locals</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>)
{
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ ghost const int A = a;</span>

  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">i</span>;                   <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">So we can refer to this &#8216;i&#8217; *after* the loop.</span>
  <span style="color: #96A7A9; font-style: italic;">/* </span><span style="color: #96A7A9; font-style: italic;">// @ loop assigns ???;         // Fix me.</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop invariant a == 666;      // Fix me.</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop invariant 0 &lt;= i &lt;= 10;</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop variant   666;           // Fix me.</span>
<span style="color: #96A7A9; font-style: italic;">    @</span><span style="color: #96A7A9; font-style: italic;">*/</span>
  <span style="color: #859900; font-weight: bold;">for</span>(i = 0; i != 10; i++) a++;
<span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert after_loop_guarantees</span><span style="color: #96A7A9; font-style: italic;">: i == 10  &amp;&amp; a == A + i;</span>
<span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert weakening_previous_gives</span><span style="color: #96A7A9; font-style: italic;">: a == A + 10;</span>
  <span style="color: #859900; font-weight: bold;">return</span> a;
}
</pre>
</div>
 </details>

<p>
<b>Warning</b> Without the <code>loop assigns</code>, you are more likely to have trouble proving a loop
is correct!
</p>
<p>
Now a bit harder&#x2026;
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <p>

</p>

<div class="org-src-container">
<pre class="src src-c" id="orgb999c65"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures 0 &lt;= \result &lt;= 1;</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">random_bool</span>(); <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">{ return random_between(0, 1); }</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires \valid(it);</span>
<span style="color: #96A7A9; font-style: italic;">  @ requires *it + max &lt;= INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  // @ requires FixMe -- a property on `max`;</span>
<span style="color: #96A7A9; font-style: italic;">  // @ assigns FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures  \old(it) &lt;= it &lt;= it + max;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">increment_randomly</span>(<span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">it</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">max</span>)
{
   <span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ loop assigns i, *it;</span>
<span style="color: #96A7A9; font-style: italic;">     @ loop invariant *it == \at(*it, Pre) + i;</span>
<span style="color: #96A7A9; font-style: italic;">     // </span><span style="color: #b58900;">@ loop invariant range_of_i</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">     // @ loop variant FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">    */</span>
   <span style="color: #859900; font-weight: bold;">for</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">i</span> = 0; i != max &amp;&amp; random_bool(); i++) (*it)++;
}
</pre>
</div>
 </details>
</div>
</div>
</div>

<div id="outline-container-orga9bfe17" class="outline-2">
<h2 id="orga9bfe17"><span class="section-number-2">3</span> ACSL Properties</h2>
<div class="outline-text-2" id="text-3">
</div>

<div id="outline-container-org44e8a5d" class="outline-3">
<h3 id="org44e8a5d"><span class="section-number-3">3.1</span> Predicates</h3>
<div class="outline-text-3" id="text-3-1">
<p>
Our invaraints were getting out of hand, the trouble can be mitigated by defining our own
predicates rather than just using the built in ones. However, such definitions must be
<i>functional</i> in nature: They do not produce side-effects, such as altering state.
Moreover, they are generally parameterised by a <i>label</i> that refers to the C memory state
in which they would be invoked &#x2013;within the definition we cannot however reference the
special labels <code>Here, Pre, Post</code>. Otherwise parameter passing is by value as in C.
</p>

<p>
The <code>predicate</code> keyword declares Boolean values functions:
</p>
<pre class="example">
/*@ predicate name_here {Label₀, …, Labelₖ} (type₀ arg₀, …, typeₘ argₘ) =
  @ // A Boolean valued relationship between all these things.
  */
</pre>

<div class="org-src-container">
<pre class="src src-c" id="org2e89502"><span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">An integer memory location remains unaltered between given program points.</span>
<span style="color: #96A7A9; font-style: italic;">//</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ predicate unchanged{L0, L1} (int *i)   =  \at(*i, L0) == \at(*i, L1);</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">i</span> = 13, <span style="color: #268bd2;">j</span> = 12;

  <span style="color: #6c71c4; font-weight: bold;">DoSomeWork</span>:
  j = 32;

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert unchanged{DoSomeWork, Here}(&amp;i);</span>

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
More usefully, there is the need to ensure a given integer is indeed a non-negative
length of an array.
</p>
<div class="org-src-container">
<pre class="src src-c" id="orgb006a50"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ predicate valid_array(int* arr, integer len) =</span>
<span style="color: #96A7A9; font-style: italic;">  @ 0 &lt;= len &amp;&amp; \valid(arr + (0 .. len - 1));</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
</pre>
</div>
<p>
Notice that we did not specify a memory label.
That's okay, one is provided for us and the entire definition is considered to transpire
at that memory location. In particular, unlike the previous example, we cannot refer to
distinct memory locations &#x2013;after all we haven't named any!
At the call site, the implicit memory location would be <code>Here</code> thereby referring to the
current memory state &#x2013;however we may still explicitly provide a different label at the
call site.
</p>
</div>
</div>

<div id="outline-container-orgecebd71" class="outline-3">
<h3 id="orgecebd71"><span class="section-number-3">3.2</span> Logic Functions</h3>
<div class="outline-text-3" id="text-3-2">
<p>
Predicates must be either true or false, but <code>logic</code> functions are methods that
can be invoked in our specifications &#x2013;you may have noticed that C methods <b>cannot</b>
be called in a specification, which is reasonably since they may produce side-effects!
Since assignment, sequence, and loops rely on side effects they now suddenly become
useless and our definitions must rely on recursion.
Such logical functions generally need not worry about runtime issues such as overflow
&#x2013;which however must be handled at the call site, if need be.
</p>

<pre class="example">
/*@ logic return_type function_name {Label₀, …, Labelₖ} (type₀ arg₀, …, typeₘ argₘ) =
  @  // A formula using the arguments argᵢ, possibly at labels Labelᵢ
 */

//@ logic integer factorial(integer n)  =  (n &lt;= 0) ? 1 : n * factorial(n-1);
</pre>

<p>
If we wrote a program that contained many occurrences to <code>factorial</code> then the definition
would need to be invoked each time. If the occurrences all happened, for example, on the
same input, say 12 &#x2013;any larger would be an <code>unsigned int</code> overflow&#x2013; then it might
make matters faster if we simply had that as a <i>lemma</i> that is proven once and used many
times by the underlying provers. Indeed this can be accomplished by using the <code>lemma</code>
phrase, and this can be done for any property.
</p>

<pre class="example">
//@ lemma name_of_property {Label₀, …, Label₀}:  property_here ;
</pre>

<p>
For example,
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ lemma lt_plus_lt</span><span style="color: #96A7A9; font-style: italic;">: \forall integer i,j; i &lt; j ==&gt; i + 1 &lt; j + 1;</span>
</pre>
</div>
</div>
</div>

<div id="outline-container-orge283388" class="outline-3">
<h3 id="orge283388"><span class="section-number-3">3.3</span> Axiomatic Definitions</h3>
<div class="outline-text-3" id="text-3-3">
<p>
Sometimes a proof may take too long to be proven, or it cannot be proven with the
back-end provers, and, moreover, we do not wish to bother with its proof directly.
In such cases, we may tell Frama-C to trust our judgement and take our word for it
&#x2013;if we're not careful, our ‘word’ may lead us to conclude <i>false = true</i>!
</p>

<div class="org-src-container">
<pre class="src src-c" id="orga371607"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ axiomatic my_axioms {</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ axiom antisymmetry</span><span style="color: #96A7A9; font-style: italic;">: \forall integer i, j; i &lt;= j &lt;= i  ==&gt;  i == j;</span>
<span style="color: #96A7A9; font-style: italic;">  @ }</span>
<span style="color: #96A7A9; font-style: italic;">*/</span>
</pre>
</div>

<p>
Unlike lemmas, which require a proof, axioms are simply assumed to be true.
It is the responsibility of the user to ensure no inconsistencies arise, as in:
</p>
<div class="org-src-container">
<pre class="src src-c" id="org50694de"><span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ axiomatic UhOh{ axiom false_is_true</span><span style="color: #96A7A9; font-style: italic;">: \false; }</span>

<span style="color: #b58900; font-style: italic;">int</span> main()
{
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Examples of proven properties</span>

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert \false;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert \forall integer x; x == 31;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert \false == \true;</span>
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
However, <code>axiomatic</code> definitions of recursive functions are useful since the underlying
provers do not unroll the recursion when possible
&#x2013;after all, we are simply declaring the type of a function and some properties about
it, which incidentally, happen to be its defining equations.
</p>

<div class="org-src-container">
<pre class="src src-c" id="org7366a98"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ axiomatic Factorial</span>
<span style="color: #96A7A9; font-style: italic;">  {</span>
<span style="color: #96A7A9; font-style: italic;">     logic integer factorial(integer n);</span>

<span style="color: #96A7A9; font-style: italic;">     axiom factorial_base: \forall integer i;  i &lt;= 0  ==&gt;  factorial(i) == 0;</span>
<span style="color: #96A7A9; font-style: italic;">     axiom factorial_inductive:</span>
<span style="color: #96A7A9; font-style: italic;">          \forall integer i; i &gt;  0  ==&gt;  factorial(i) == i * factorial(i - 1);</span>
<span style="color: #96A7A9; font-style: italic;">  }</span>
<span style="color: #96A7A9; font-style: italic;">*/</span>
</pre>
</div>

<p>
A small subtlety is that access to memory locations must be specified in the function
headers, for example:
</p>
<div class="org-src-container">
<pre class="src src-c" id="org2ab1dd3"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ axiomatic is_constant</span>
<span style="color: #96A7A9; font-style: italic;">  {</span>
<span style="color: #96A7A9; font-style: italic;">     predicate constant{L}(int * a, integer b, integer e, integer val) reads a[b .. e-1];</span>

<span style="color: #96A7A9; font-style: italic;">     axiom constant_empty{L}:</span>
<span style="color: #96A7A9; font-style: italic;">       \forall int * a, integer b, e, val; b &gt;= e  ==&gt; constant{L}(a, b, e, val);</span>

<span style="color: #96A7A9; font-style: italic;">     axiom constant_non_empty{L}:</span>
<span style="color: #96A7A9; font-style: italic;">       \forall int * a, integer b, e, val; b &lt; e ==&gt;</span>
<span style="color: #96A7A9; font-style: italic;">          ( constant{L}(a,b,e, val)  &lt;==&gt;  constant{L}(a,b,e-1, val) &amp;&amp; a[e-1] == val );</span>
<span style="color: #96A7A9; font-style: italic;">  }</span>
<span style="color: #96A7A9; font-style: italic;">*/</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org6551851" class="outline-2">
<h2 id="org6551851"><span class="section-number-2">4</span> Functions</h2>
<div class="outline-text-2" id="text-4">
<p>
We will be proving code blocks satisfy Hoare triples, but code blocks are essentially
methods with the given predicate acting as a pre-condition and the required predicate
acting as post-condition. As such, we investigate Hoare triples by using methods.
</p>

<p>
A contract stipulates under what conditions a method will behave
&#x2013;if those conditions are not met, then it's behaviour may be arbitrary.
For example, a method may behave in a manner ensuring \\ <code>x &gt; 1/y</code> under the condition <code>y &gt; 0</code>,
and it may do anything it wants &#x2013;such as aborting the system or setting <code>x = -1</code>&#x2013;
when that condition is not met.
</p>

<p>
All in all, a function call establishes property <i>R</i> precisely when evaluating its
arguments then executing the function body together establish the property.
</p>
<pre class="example">
wp ℱ(t₁, …, tₙ) R  ≡  wp (x₁ ≔ t₁; ⋯ ; xₙ ≔ tₙ; ℬ) R

where ℱ(x₁, …, xₙ) = ℬ  -- Definition of ℱ
</pre>
</div>

<div id="outline-container-orgd5a0501" class="outline-3">
<h3 id="orgd5a0501"><span class="section-number-3">4.1</span> What are Functions?</h3>
<div class="outline-text-3" id="text-4-1">
<p>
Functions permit abstraction over program design since parts may
be constructed independently and also promotes avoidance of repetition.
</p>

<p>
Unlike functional languages where the result of a function is the final
term in its body, imperative languages signal result values by the <code>return</code> keyword;
which immediately stops execution of the function regardless of the keyword's position.
</p>

<p>
Functions which return no value but instead perform some effectful action, i.e., are <i>procedures</i>,
are marked with the <code>void</code> keyword in-place of a return type &#x2013;surprisingly, this ‘return type’
is not a type at all! One cannot declare a variable of type <code>void</code>.
</p>

<p>
Function calls that yield a value are terms whereas those that do not constitute statements.
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span>  <span style="color: #b58900;">f</span>(){ <span style="color: #859900; font-weight: bold;">return</span> 3;}
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">g</span>(){ printf(<span style="color: #2aa198;">"g(): Hello There!"</span>); }

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Valid invocations</span>
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = f();
  f();           <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Bad form! Result is discarded.</span>
  g();

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">T y = g(); // Type error! No possible type T!</span>
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
 </details>

<p>
A <i>program</i> is an sequence of global variables, function declarations, then a special
function called <code>main</code>. Since sequences are ordered, all names are declared before use!
In C, <code>main</code> usually exits with <code>return 0</code>. Global variables tend to pollute
the namespace and are more trouble than they're worth, so we shall ignore them
&#x2013;however they are already included by default since assignment is a top-level construct.
Incidentally, function declarations with no arguments may be used to simulate global
constants.
</p>

<p>
Note that C does not permit function overloading.
Moreover, C uses the same namespace for methods as well as simple variables
&#x2013;which in is not the case in, say, Java which permits naming a method <code>f</code> and a
variable <code>f</code> to obtain the valid invocation <code>f(f)</code>!
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">it</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span>){ <span style="color: #859900; font-weight: bold;">return</span> x; }
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">it</span> = 5;
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">maybe</span> = it(it);
</pre>
</div>
 </details>
</div>

<div id="outline-container-org5ede74a" class="outline-4">
<h4 id="org5ede74a"><span class="section-number-4">4.1.1</span> Effectful Expressions</h4>
<div class="outline-text-4" id="text-4-1-1">
<p>
In C, any expression followed by a semicolon is a statement:
The value of the expression is simply ignored when it is used as a statement.
</p>

<p>
Conversely, statements may be regarded as an effectful expression.
For example, assignment <code>x ≔ E</code> assigns the value of <code>E</code> to <code>x</code> and <i>returns</i> the value of <code>E</code>.
Whence, the notion of
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">continued_assgns</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 1, <span style="color: #268bd2;">y</span> = 2, <span style="color: #268bd2;">z</span> = 4;

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert x == 1  &amp;&amp;  y == 2  &amp;&amp; z == 4;</span>

  x -= y += z;         <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Assignments are right-associative!</span>

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert z == 4  &amp;&amp;  y == 2+4  &amp;&amp; x == 1-(2+4);</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert z == 4  &amp;&amp;  y == 6    &amp;&amp; x == -5;</span>
}
</pre>
</div>
 </details>
</div>
</div>
</div>

<div id="outline-container-orgf5ac87b" class="outline-3">
<h3 id="orgf5ac87b"><span class="section-number-3">4.2</span> An Example Functional Contract</h3>
<div class="outline-text-3" id="text-4-2">
<p>
Look at the definition of <code>abs</code> below and notice:
</p>

<ul class="org-ul">
<li>Frama-C contracts are comments beginning with an <code>@</code> symbol and concluded with a <code>;</code> symbol.</li>
<li>The post-condition is introduced with the <code>ensures</code> clause; which may contain the
<code>\result</code> keyword to refer to the returned value of the method.</li>
<li>We may combine multiple <code>ensures</code> clauses by using conjunction <code>&amp;&amp;</code>, or have them on
separate lines. We may also use implication <code>==&gt;</code>, disjunction <code>||</code>, negation <code>!</code>,
value equality <code>==</code>, and Boolean equivalence <code>&lt;==&gt;</code>.
<ul class="org-ul">
<li>Notice that implication:  <code>A ==&gt; B</code> informs that when <code>A</code> is true then so is <code>B</code>,
and if <code>A</code> is false then we don't care and consider the whole thing to be true.</li>
</ul></li>
<li>Notice that we may <i>name</i> our conditions, which is helpful to remind us of their purpose
as well as being helpful in the frama-c output.</li>
</ul>

<div class="org-src-container">
<pre class="src src-c" id="org05d2490"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #b58900;">@ ensures always_nonnegative</span><span style="color: #96A7A9; font-style: italic;">: \result &gt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures val &gt; 0  ==&gt;  \result == val;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures val &lt; 0  ==&gt;  \result == -val;</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">int</span> abs(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">val</span>)
{
  <span style="color: #859900; font-weight: bold;">return</span> (val &lt; 0) ? -val : val;
}
</pre>
</div>

<p>
Pressing <code>F9</code>, you will notice that we are also checking for runtime errors by using the RTE plugin
whose goal is to ensure the program cannot create runtime errors such as integer overflow,
invalid pointer dereferencing, division by 0, etc.
</p>

<p>
In our case, we have runtime problems that do not crash but instead produce logical errors:
The return value of <code>abs</code> is not positive! Indeed, in addition to the above block, add focus
to the following block by removing the prefix <code>-not</code>, then run the code with <code>F6</code> to see the output.
&#x2013;Remember to undo this alteration when you're done, otherwise the next invocation to frama-c
will take a while dealing with <code>printf</code>!
</p>
<div class="org-src-container">
<pre class="src src-c" id="orgadaec99"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span>   <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>
<span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">bounds on integers</span>
<span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;math.h&gt;</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Our implementation is faulty..</span>
  printf(<span style="color: #2aa198;">"    INT_MIN      = %d\n"</span>, INT_MIN);
  printf(<span style="color: #2aa198;">"abs(INT_MIN)     = %d\n\n"</span>, abs(INT_MIN));

  printf(<span style="color: #2aa198;">"    INT_MIN + 1  = %d\n"</span>, INT_MIN+1);
  printf(<span style="color: #2aa198;">"abs(INT_MIN + 1) = %d\n\n"</span>, abs(INT_MIN+1));

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Using standard library works..</span>
  printf(<span style="color: #2aa198;">"fabs(INT_MIN)    = %.0f"</span>, fabs(INT_MIN));

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
The WP plugin forms the necessary proof obligations to ensure the program meets its
specification, simplifies them using a module called <code>Qed</code>, then asks a prover such as
<code>Alt-Ergo</code> whether the obligation is provable or not. Sometimes a property is not
verified for two possible reasons:
</p>
<ol class="org-ol">
<li>There is not enough information &#x2013;e.g., given assumptions&#x2013; for the proof to go through.</li>
<li>The proof search timed-out &#x2013;which can be configured.</li>
</ol>
</div>

<div id="outline-container-org23bad81" class="outline-4">
<h4 id="org23bad81"><span class="section-number-4">4.2.1</span> Exercise: Fixing <code>abs</code></h4>
<div class="outline-text-4" id="text-4-2-1">
<ol class="org-ol">
<li>Remove focus from the <code>main()</code> code block.</li>
<li>Go back to the Incomplete Absolute Value code block.</li>
<li>Include the limits header file.</li>
<li>Add <code>@ requires INT_MIN &lt; val;</code> to the top of the function contract.</li>
<li>Check that the contract passes by pressing <code>F9</code>.</li>
<li>What bound conditions can you place on the result?</li>
<li>Experiment by altering the conditions or method body.</li>
</ol>
</div>
</div>

<div id="outline-container-org426754f" class="outline-4">
<h4 id="org426754f"><span class="section-number-4">4.2.2</span> Behaviours</h4>
<div class="outline-text-4" id="text-4-2-2">
<p>
Notice that our absolute value function has two disjoint behaviours
&#x2013;depending on whether the input is positive or negative.
Each behaviour has some assumptions and some conclusions.
Moreover, our behaviours are
</p>
<ul class="org-ul">
<li><code>disjoint</code>: Every input can only satisfy the assumptions of <i>at most</i> one of the behaviours.
As such, the program is ‘deterministic’: At most one of the behaviours is possible.
&#x2013;The program is really a relation and this ensures it is <i>univalent</i>; i.e., a <i>partial function</i>&#x2013;</li>
<li><code>complete</code>: Every input satisfies the assumptions of <i>at least</i> one of the behaviours.
As such, the program is ‘total’: At least one behaviour is possible.
&#x2013;The program is really a relation and this ensures it is <i>total</i>; i.e., defined on all
inputs&#x2013;</li>
</ul>

<p>
We expressed our behaviours in the forms <code>ensures ⟨assumptions⟩  ==&gt;  ⟨consequences⟩</code>.
However this can get unruly when there are many assumptions and many consequences.
Moreover, expressing disjointness is tedious and error-prone even in our little
example, below, where it becomes the assertion: (<code>val &lt; 0 &amp;&amp; val &gt;= 0) &lt;==&gt; \false</code>.
As such there is the alternative <code>behavior</code> syntax &#x2013;note the American spelling!
Using this syntax, we can ask WP to verify that the behaviours are complete or disjoint,
or both.
</p>

<div class="org-src-container">
<pre class="src src-c" id="org2f1fce2"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires INT_MIN &lt; val;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures always_nonnegative</span><span style="color: #96A7A9; font-style: italic;">: \result &gt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior positive_input</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  @    assumes val &gt; 0;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures \result == val;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior negative_input</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  @    assumes val &lt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures \result == -val;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  @ complete behaviors;</span>
<span style="color: #96A7A9; font-style: italic;">  @ disjoint behaviors;</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">abs</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">val</span>)
{
  <span style="color: #859900; font-weight: bold;">return</span> (val &lt; 0) ? -val : val;
}
</pre>
</div>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <p>

</p>

<p>
<b>Exercise</b>
Replace each FixMe so that the program is proven correct.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires INT_MIN &lt; val;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures always_nonnegative</span><span style="color: #96A7A9; font-style: italic;">: \result &gt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures Exercise</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  @      FixMe                             // Positive case</span>
<span style="color: #96A7A9; font-style: italic;">  @   &amp;&amp; (val &lt;= 0  ==&gt; \result == -val)   // Negative or 0 case</span>
<span style="color: #96A7A9; font-style: italic;">  @   &amp;&amp; !(val &gt; 0 &amp;&amp; val &lt;= 0)            // Disjointness condition</span>
<span style="color: #96A7A9; font-style: italic;">  @   &amp;&amp; FixMe                             // Completeness condition</span>
<span style="color: #96A7A9; font-style: italic;">  @   ;</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">abs</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">val</span>)
{
  <span style="color: #859900; font-weight: bold;">return</span> (val &lt; 0) ? -val : val;
}
</pre>
</div>

<p>
<b>Exercise</b>
Replace each FixMe with the weakest proposition so that the program is proven correct.
</p>

<div class="org-src-container">
<pre class="src src-c" id="org78402bc"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires INT_MIN &lt; val;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures always_nonnegative</span><span style="color: #96A7A9; font-style: italic;">: \result &gt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior positive_input</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    assumes Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures \result == val;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior negative_input</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    assumes Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures \result == -val;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  @ complete behaviors;    // Frama-C complain here, but please fix the &#8220;Exercises&#8221;!</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">abs</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">val</span>)
{
  <span style="color: #859900; font-weight: bold;">return</span> (val &lt; 0) ? -val : val;
}
</pre>
</div>

<p>
<b>Exercise</b>
Replace each FixMe with the strongest proposition so that the program is proven correct.
</p>

<div class="org-src-container">
<pre class="src src-c" id="org3b3457e"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires INT_MIN &lt; val;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures always_nonnegative</span><span style="color: #96A7A9; font-style: italic;">: \result &gt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior positive_input</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    assumes Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures \result == val;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior negative_input</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    assumes Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures \result == -val;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  @ disjoint behaviors;</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">abs</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">val</span>)
{
  <span style="color: #859900; font-weight: bold;">return</span> (val &lt; 0) ? -val : val;
}

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">This program passes 100%, but that is because it assumes false, the &#8220;FixMe&#8221;.</span>
</pre>
</div>
 </details>
</div>
</div>
</div>
<div id="outline-container-org8f973ed" class="outline-3">
<h3 id="org8f973ed"><span class="section-number-3">4.3</span> Proving is Programming</h3>
<div class="outline-text-3" id="text-4-3">
<p>
In this section we step back a little to get more comfortable with <code>requires</code>
preconditions and <code>ensures</code> postconditions. Moreover, we use this time to remind
ourselves of some elementary logic. After all, we use logic to express properties
and hope Frama-C can verify them.
</p>

<p>
In some sense <i>false, true</i> behave for the 𝔹ooleans as <i>-∞, +∞</i> behave for the ℕumbers.
</p>
<table>


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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><span class="underline">𝔹ooleans</span></td>
<td class="org-left"><span class="underline">ℕumbers</span></td>
</tr>

<tr>
<td class="org-left">p ⇒ true</td>
<td class="org-left">n ≤ +∞</td>
</tr>

<tr>
<td class="org-left">false ⇒ p</td>
<td class="org-left">-∞ ≤ n</td>
</tr>

<tr>
<td class="org-left">Implication ⇒</td>
<td class="org-left">Inclusion ≤</td>
</tr>

<tr>
<td class="org-left">Conjunction ∧</td>
<td class="org-left">Minimum ↓</td>
</tr>

<tr>
<td class="org-left">Disjunction ∨</td>
<td class="org-left">Maximum ↑</td>
</tr>
</tbody>
</table>

<p>
Using this correspondence we can rephrase the “Golden Rule” <i>p ∧ q ≡ p ≡ q ≡ p ∨ q</i>
as the following trivial property <i>x ↓ y = x  ≡  y = x ↑ y</i>
&#x2013;“The minimum of two numbers is the first precisely when the second is their maximum.”
Neat Stuff!
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <p>

</p>

<p>
<b>Ex falso quodlibet</b> From <i>false</i>, anything follows: <i>false ⇒ p,</i> for any <i>p</i>.
Edit <code>FixMe</code> in the following snippet so that it ensures the result is equal to 42.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #b58900;">@ requires uhOh</span><span style="color: #96A7A9; font-style: italic;">: a &lt; 0 &amp;&amp; a &gt; 0;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures  what: Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">id</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>){ <span style="color: #859900; font-weight: bold;">return</span> a;}
</pre>
</div>

<p>
<b>Right Identity of Implication</b> Everything implies <i>true</i>; that is <i>p ⇒ true,</i> for any <i>p</i>.
Edit <code>FixMe</code> in the following snippet so that it there are no errors.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #b58900;">@ requires Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;   // &lt;-- Change this false positive.</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures \true;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">id</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>){ <span style="color: #859900; font-weight: bold;">return</span> a; }
</pre>
</div>

<p>
<b>Exercise</b>
Replace each <code>FixMe</code> with the weakest possible predicate so that it passes.
</p>
<div class="org-src-container">
<pre class="src src-c" id="org6b1dc74"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires \true;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures UpperBound</span><span style="color: #96A7A9; font-style: italic;">: a &lt;= \result  &amp;&amp;  b &lt;= \result;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures Exercise: Selection1</span><span style="color: #96A7A9; font-style: italic;">: FixMe  ==&gt; \result == b;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures Exercise: Selection2</span><span style="color: #96A7A9; font-style: italic;">: FixMe  ==&gt; \result == a;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span><span style="color: #96A7A9; font-style: italic;">*/</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">max</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">b</span>)
{
  <span style="color: #859900; font-weight: bold;">return</span> a &lt; b ? b : a;
}
</pre>
</div>

<p>
Conversely, that maximum is the least upper bound,
<i>x ≤ z  ∧  y ≤ z  ≡  x ↑ y ≤ z</i>, corresponds to the
characterisation of disjunction <i>(p ⇒ r) ∧ (q ⇒ r)  ≡  (p ∨ q) ⇒ r</i>
&#x2013;incidentally this is also known as “case analysis” since one proves
<i>p ∨ q ⇒ r</i> by providing a proofs that if <i>p ∨ q</i> is true due to <i>p</i> then
with <i>p</i> in hand we need to show <i>r</i>, and likewise if <i>p ∨ q</i> is true due to <i>q</i>.
</p>

<p>
<b>Exercise</b>
Replace <code>FixMeCode</code> with the least amount of code so that the following passes.
</p>
<div class="org-src-container">
<pre class="src src-c" id="orgb50aa7f"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>
<span style="color: #268bd2; font-weight: bold;">#define</span> <span style="color: #b58900;">max</span>(<span style="color: #268bd2;">a</span>,<span style="color: #268bd2;">b</span>) (a &lt; b ? b : a)   <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Ignore me.</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires max(a, b) &lt;= c;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures a &lt;= c  &amp;&amp;  b &lt;= c;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span><span style="color: #96A7A9; font-style: italic;">*/</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">case_analysis</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">b</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">c</span>)
{
  FixMeCode;
}
</pre>
</div>
 </details>
</div>
</div>

<div id="outline-container-org666258d" class="outline-3">
<h3 id="org666258d"><span class="section-number-3">4.4</span> Maintaining The Sequence Rule</h3>
<div class="outline-text-3" id="text-4-4">
<p>
Since function calls may alter memory state, the computation of a term may now
not only produce a value, as before, but also alter the state altogether.
( Foreshadowing: The <code>assigns</code> ACSL keyword! )
</p>

<p>
Since a <code>return</code> interrupts executation, the sequence computation rule
<code>wp (S₁;S₂) = wp S₁ ∘ wp S₂</code> is no longer valid when the execution of <code>S₁</code> causes the
execution of a <code>return</code> thereby necessitating that <code>S₂</code> is not executed.
As we have already seen, we keep the rule valid by simply defining <code>wp U R ≡ true</code>
for any unreachable code <code>U</code> &#x2013;as is <code>S₂</code> when <code>S₁</code> has a return.
</p>

<p>
That is, unreachable assertions are always ‘true’:
They are never in a memory state, and so cannot even be evaluated, let alone be false!
</p>
<div class="org-src-container">
<pre class="src src-c" id="orgf3d80c3"><span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #859900; font-weight: bold;">goto</span> <span style="color: #6c71c4; font-weight: bold;">End</span>;
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@assert my_cool_nonsense</span><span style="color: #96A7A9; font-style: italic;">: 0 == 1;    // This is unreachale but &#8216;true&#8217;.</span>

  <span style="color: #6c71c4; font-weight: bold;">End</span>:
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
<p>
Likewise with infinite loops,
</p>
<div class="org-src-container">
<pre class="src src-c" id="org8b51a0f"><span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #859900; font-weight: bold;">while</span>(1 &gt; 0);
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@assert my_cool_nonsense</span><span style="color: #96A7A9; font-style: italic;">: 0 == 1;    // This is unreachale but &#8216;true&#8217;.</span>
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
<p>
That is, Frama-C considers ‘partial correctness’: A specification is satisfied,
<i>provided</i> it terminates.
</p>

<p>
In addition, since imperative expressions can modify memory, considerations must be
given to the fact arguments of a function are evaluated from left to right.
For example, suppose <code>x,y</code> are imperative constructions yield integers, then
 <code>x + y</code> and <code>y + x</code> are not guaranteed to produce the same behaviour!
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">f</span>(){ printf(<span style="color: #2aa198;">"\nf(): Hello with Four!"</span>);  <span style="color: #859900; font-weight: bold;">return</span> 4;}
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">g</span>(){ printf(<span style="color: #2aa198;">"\ng(): Hello with Three!"</span>); <span style="color: #859900; font-weight: bold;">return</span> 3;}

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">result</span>;

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">The output to the screen changes,</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">even though the *value* of result does not.</span>
  result = f() + g();
  printf(<span style="color: #2aa198;">"\n---"</span>);
  result = g() + f();

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
 </details>

<p>
The C language does not specify the order of evaluation of function arguments
&#x2013;albeit it is usually left-to-right&#x2013;, and it is up to the programmer to write
programs whose result does not depend on the order of evaluation.
</p>

<p>
<b>Exercise:</b> Produce a Frama-C checked variant of this example.
Remember to remove all <code>printf</code>'s!
</p>
</div>
</div>

<div id="outline-container-orgbc9010b" class="outline-3">
<h3 id="orgbc9010b"><span class="section-number-3">4.5</span> Passing Arguments by Value and by Reference</h3>
<div class="outline-text-3" id="text-4-5">
<p>
Applying the definition of <code>wp</code> to the body of the following <code>swap</code> gives us
<code>wp swap = id</code>, thereby demonstrating that <i>this</i> <code>swap</code> does not change the
values of two variables!
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">swap</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">b</span>){ <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">c</span>; c = a; a = b; b = c; }
</pre>
</div>
<p>
The default mechanism of argument passing is that of <i>pass by value</i>:
Only values are sent to function bodies, which cannot alter the original variables.
Indeed, what should <code>swap(x+y, 2)</code> perform if it were to “alter the given variables”?
</p>

<p>
To say that two <i>distinct</i> variables share the <i>same location</i> in memory
requires us to formally introduce a notion of location that variables may reference.
Rather than introduce a new such type, C makes the convention that certain numeric
values act &#x2013;possibly dual roles&#x2013; as reference locations.
</p>

<p>
Hence we can associate variables to references which are then associated to values.
That is, a state now consists of two pieces: An <i>environment</i> mapping variables to
references and a <i>memory state</i> mapping references to values.
The key insight is that the environment may be non-injective thereby associating
distinct variables to the same reference thereby permitting them to alter the shared value.
Incidentally, the shared value can be thought of as a buffer for message passing between
the two variable agents. Neato!
</p>

<p>
In C, passing by reference is not a primitive construct, but it can be simulated.
The type of references that can be associated with a value of type <code>T</code> in memory
is written <code>T*</code> in C. Incidentally, the dereference operator is written <code>*</code> in C.
For example, in environment <code>u ↦ r₁</code> and memory state <code>r₁ ↦ r₂, r₂ ↦ 4</code> we have
that <code>u</code> has <i>value</i> <code>r₂</code> whereas <code>*u</code> has <i>value</i> <code>4</code>. That is, <code>u</code> is a reference value at location <code>r₁</code>
having contents <code>r₂</code>, which when dereferenced refer to the value contained in location <code>r₂</code>
which is 4.
</p>

<p>
The reference associated with variable <code>x</code> in a C environment is written <code>&amp;x</code>.
E.g., in environment <code>x ↦ r</code> and memory state <code>r ↦ 4</code>, the value of <code>x</code> is the integer 4
whereas the value of <code>&amp;x</code> is the reference <code>r</code>. Moreover, the value of <code>*&amp;x</code> is the integer 4.
</p>

<pre class="example">
&amp;_ : ∀{T} →  Variable T → Reference T   -- “address of”
*_ : ∀{T} → Reference T → T             -- “value of”

-- Using ‘value equality’:
Inverses:  ∀ a : Var T.  *&amp;a   ≈ a
Inverses:  ∀ r : Ref T.  &amp;(*r) ≈ r
</pre>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">understanding_references</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span>  <span style="color: #268bd2;">a</span> = 4;    <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">integer a refers to 4</span>
  <span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">x</span> = &amp;a;   <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">integer reference x refers to the location of a</span>

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Facts thus far</span>
  <span style="color: #96A7A9; font-style: italic;">//</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert a_is_a_number</span><span style="color: #96A7A9; font-style: italic;">: a == 4;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert x_points_to_a</span><span style="color: #96A7A9; font-style: italic;">: *x == a == 4;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert x_is_a_location</span><span style="color: #96A7A9; font-style: italic;">: x == &amp;a;</span>

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">The inverse law: a == *(&amp;a).</span>
  <span style="color: #96A7A9; font-style: italic;">//</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert x_points_to_a</span><span style="color: #96A7A9; font-style: italic;">: *x == a == 4;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert x_is_a_location</span><span style="color: #96A7A9; font-style: italic;">: x == &amp;a;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert equals_for_equals</span><span style="color: #96A7A9; font-style: italic;">: *(&amp;a) == *x == a;</span>

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">The inverse law: &amp;(*x) == x</span>
  <span style="color: #96A7A9; font-style: italic;">//</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert x_points_to_a</span><span style="color: #96A7A9; font-style: italic;">: *x == a == 4;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert x_is_a_location</span><span style="color: #96A7A9; font-style: italic;">: x == &amp;a;</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ assert equals_for_equals</span><span style="color: #96A7A9; font-style: italic;">: &amp;(*x) == &amp;a == x;</span>

}
</pre>
</div>
 </details>

<p>
If <code>t</code> is an expression of type <code>T*</code> then the C language has the assignment
construct <code>*t = u</code>: The reference of <code>t</code> is now associated with the value
of <code>u</code>. The notation alludes to this executional behaviour:
The contents of <code>t</code>, i.e., <code>*t</code>, now refer to the value of <code>u</code>.
</p>

<p>
For example, <code>*&amp;x = u</code> has the same behaviour as the assignment <code>x = u</code>.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">x and y themselves cannot be assigned to: They're constant.</span>
<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">I.e., assignments &#8220;x = t&#8221; are forbidden, but &#8220;*x = t&#8221; are permitted.</span>
<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">This makes the compiler complain if we accidently made that assignment instead.</span>
<span style="color: #96A7A9; font-style: italic;">//</span>
<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">However, &#8220;const int* x&#8221; works in the opposite: x=t okay, but not *x=t.</span>
<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Declaration &#8220;const int* const x&#8221; prevents both types of assignments.</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">swap</span>(<span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #859900; font-weight: bold;">const</span> <span style="color: #268bd2;">x</span>, <span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #859900; font-weight: bold;">const</span> <span style="color: #268bd2;">y</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">z</span>;
  z  = *x; <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">z gets the value referenced to by x</span>
  *x = *y; <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">the location x references now gets the value referenced by y</span>
  *y =  z; <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">the location y references now gets the value z</span>
}

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span> = 5, <span style="color: #268bd2;">y</span> = 10;
  swap(&amp;x, &amp;y);         <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Note that the function is applied to the references.</span>
  printf(<span style="color: #2aa198;">"x = %d, y = %d"</span>, x , y);
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
 </details>
</div>

<div id="outline-container-orgfb8453f" class="outline-4">
<h4 id="orgfb8453f"><span class="section-number-4">4.5.1</span> Dangling References: Segmentation Faults</h4>
<div class="outline-text-4" id="text-4-5-1">
<p>
In C, we may look for references that do not exist:
C removes from memory the reference associated with a variable
when that variable is removed from the environment.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #b58900;">f</span>(<span style="color: #859900; font-weight: bold;">const</span> <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">p</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span> = p;
  <span style="color: #859900; font-weight: bold;">return</span> &amp;n;

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">n only exists locally,</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">whence its reference is removed when it no longer exists.</span>
}

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">u</span> = f(5);
  <span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">v</span> = f(10);
  printf(<span style="color: #2aa198;">"u = %d, v = %d"</span>, *u , *v); <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Segmentation fault!</span>
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
 </details>

<p>
The compiler gives us
<code>warning: function returns address of local variable [-Wreturn-local-addr].</code>
We may thus turn on that warning &#x2013;and all warnings really!&#x2013; so that it becomes
an error at compile time.
</p>

<p>
Since we used a reference that is not declared in memory, C does not produce
a compile error but the runtime result is unpredictable. Execute the above
snippet to see different kinds of segmentation fault codes.
</p>
</div>
</div>
<div id="outline-container-orgec7fbbb" class="outline-4">
<h4 id="orgec7fbbb"><span class="section-number-4">4.5.2</span> Pointers in ACSL</h4>
<div class="outline-text-4" id="text-4-5-2">
<p>
The <code>\old</code> function is a built-in logical operation of ACSL.
It can only be used in the post-condition and it denotes the value <i>before</i> execution
of the method body. If we want to access the value at a particular memory state,
we simply refer to a label at that time frame using the <code>\at</code> construct &#x2013;see below.
</p>

<div class="org-src-container">
<pre class="src src-c" id="org4bcb7ec"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires \valid(a);</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures *a == \old(*a);</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures \at(*a, Post) == \at(*a, Pre); // Alternative way to say the same thing.</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">at_example</span>(<span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">a</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">tmp</span> = *a;
  <span style="color: #6c71c4; font-weight: bold;">AfterLine1</span>:
  *a = 23;
  <span style="color: #6c71c4; font-weight: bold;">AfterLine2</span>:
  *a = 42;
  <span style="color: #6c71c4; font-weight: bold;">AfterLine3</span>:
  *a = tmp;

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">We are now in the memory state after the fourth line.</span>
  <span style="color: #96A7A9; font-style: italic;">//</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Here are some true facts about the memory states:</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert *a == \at(*a, Pre);     // Current value of *a is same as before method.</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert \at(*a, Here) == \at(*a, Pre);     // More explicitly.</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert 42 == \at(*a, AfterLine3);</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert 23 == \at(*a, AfterLine2);</span>
}
</pre>
</div>

<p>
Besides user-defined labels, <code>\at</code> can also be used with the built-in labels:
</p>
<ul class="org-ul">
<li><code>Pre</code> : Value <i>before</i> function call.</li>
<li><code>Post</code>: Value <i>after</i> function call. &#x2013;Can only be used in the post-condition.</li>
<li><code>Here</code>: Value at the current program point. &#x2013;This' the default for stand-alone variables.</li>
</ul>

<p>
Whereas <code>\old</code> can only be used in the post-condition, <code>\at</code> can be used anywhere.
</p>

<p>
Notice that we used the built-in <code>\valid</code> to ensure that access to pointers is safe
&#x2013;i.e., pointing to a real memory location&#x2013; thereby avoiding runtime errors.
We may also write <code>\valid(p + (l .. u))</code> to express the validity of pointers
<code>p + i</code> for <i>l ≤ i ≤ u</i> &#x2013;this will be helpful when working with arrays.
Moreover, when working with constant, non-mutable, pointers, or if we wish to
be more accurate, we may use <code>\valid_read(p)</code> to express that the pointer <code>p</code> is valid for
read-access only &#x2013;no writing permitted.
</p>
</div>
</div>
</div>


<div id="outline-container-orgb3a9763" class="outline-3">
<h3 id="orgb3a9763"><span class="section-number-3">4.6</span> Side-effects: <code>assigns</code></h3>
<div class="outline-text-3" id="text-4-6">
<p>
Since methods may alter state, thereby producing side-effects, it becomes important
to indicate which global and local variables a method assigns to
&#x2013;that way its effects are explicit. We use the <code>assigns</code> clause to declare this.
Unless stated otherwise, WP assumes a method can modify anything in memory;
as such, the use of <code>assigns</code> becomes almost always necessary.
When a method has no side-effects, thereby not assigning to anything, we may
declare <code>assigns \nothing</code>.
</p>

<div class="org-src-container">
<pre class="src src-c" id="orgde49c2a"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires \valid(a) &amp;&amp; \valid(b);</span>
<span style="color: #96A7A9; font-style: italic;">  // @ assigns *a, *b;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures *a == \old(*b);</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures *b == \old(*a);</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">swap</span>(<span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">a</span>, <span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">b</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">tmp</span> = *a;
  *a = *b;
  *b = tmp;
}
</pre>
</div>

<p>
Notice that the following block fails to prove all goals
&#x2013;comment out the <code>assigns</code> clause <i>below</i> and re-check &#x2026; still no luck!
This can be fixed by un-commenting the <code>assigns</code> clause <i>above</i>.
</p>

<div class="org-src-container">
<pre class="src src-c" id="org74c3d62"><span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">h</span> = 12; <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">new global variable!</span>

<span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assigns \nothing;  // In particular, this method does not alter &#8220;h&#8221;.</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span> = 1; <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">b</span> = 2;

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert h == 12;</span>
  swap(&amp;a, &amp;b);
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert h == 12;</span>
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
Finally it is to be noted that <code>assigns</code> do not occur within a <code>behavior</code>
&#x2013;it occurs before by declaring <i>all</i> variables that may be altered, then
each <code>behavior</code> would include a clause for the unmodified variables by indicating
their new value is equal to their <code>\old</code> one.
</p>
</div>
</div>

<div id="outline-container-orgb4dc9d3" class="outline-3">
<h3 id="orgb4dc9d3"><span class="section-number-3">4.7</span> Pointer Aliasing: <code>separated</code></h3>
<div class="outline-text-3" id="text-4-7">
<p>
The raison d'être of pointers is to be able to have aliases for memory locations.
When the pointers refer to simple data, we may act <i>functionally</i> in that we copy
data to newly allocated memory locations. However, sometimes &#x2013;such as when we program
with linked lists&#x2013; copying large amounts of data is unreasonable and we may simply
want to alter given pointers directly. When the given pointers are <i>identical</i> then an
alteration to one of them is actually an alteration to the rest!
</p>

<p>
When we program with lists, we shall see that if we catenate two lists by altering
the first to eventually point to the second then it all works.
However, if we catenate a list with itself then the resulting alteration
is not the catenation of the list with itself but instead is an
infinite cycle of the first list! &#x2013;We'll see this later on.
</p>

<p>
Here is a simpler example,
</p>
<div class="org-src-container">
<pre class="src src-c" id="org3de682a"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires \valid(fst) &amp;&amp; \valid_read(snd);</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ requires no_flow</span><span style="color: #96A7A9; font-style: italic;">: INT_MIN &lt;= *fst + *snd &lt;= INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  // @ requires \separated(fst, snd);</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns *fst;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures uhOh</span><span style="color: #96A7A9; font-style: italic;">: *fst == \old(*fst) + *snd;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">increment_first_by_second</span>(<span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">fst</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #859900; font-weight: bold;">const</span> *<span style="color: #268bd2;">snd</span>)
{
  *fst += *snd;
}
</pre>
</div>

<p>
Notice since we're only assigning to <code>*fst</code>, we need not explicitly state
<code>ensures *snd == \old(*snd)</code>. However, in the event that <code>fst</code> and <code>snd</code> both point
to the same memory location, then we actually are assigning to both!
As such the final <code>ensures</code> is not necessarily true either!
</p>

<p>
We need to uncomment the <code>separated</code> declaration: The memory locations are distinct.
</p>

<p>
Notice that in the final call below, since the pre-condition to
<code>increment_first_by_second</code> fails, we have breached its contract and therefore
no longer guarenteed the behaviour it <code>ensures</code>.
Since the contract does not tells what happens when we breach it, anything is possible
and so anything is “true”!
</p>
<div class="org-src-container">
<pre class="src src-c" id="org3f8244e"><span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">life</span> = 42, <span style="color: #268bd2;">universe</span> = 12;

  <span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">this</span> = &amp;life;
  <span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">new</span>  = &amp;universe;

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert *this == 42  &amp;&amp; *new == 12;</span>
  increment_first_by_second(this, new);
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert *this == 42 + 12  &amp;&amp; *new == 12;   // Yay!</span>

  <span style="color: #b58900; font-style: italic;">int</span> *<span style="color: #268bd2;">that</span> = &amp;life;  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">uh-oh!</span>

  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert *this == 54  &amp;&amp; *that == 54;</span>
  increment_first_by_second(this, that);
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert *this == 54 + 54  &amp;&amp; *that == 54;   // Nope...?</span>
  <span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assert 1 == 0;                             // Notice everything is now &#8220;true&#8221;!</span>

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
We may invoke <code>\separated(p₁, …, pₙ)</code> to express the pointers <code>pᵢ</code> should refer to distinct
memory locations and therefore are non-overlapping.
</p>
</div>
</div>

<div id="outline-container-org97e45ef" class="outline-3">
<h3 id="org97e45ef"><span class="section-number-3">4.8</span> Functional Composition</h3>
<div class="outline-text-3" id="text-4-8">
<p>
As a matter of abstraction, or separation of concerns, a program may be split up
into an interface of declarations &#x2013;a ‘header’ file&#x2013; and an implementation file.
Frama-C permits this approach by allowing us to use a specification of a declared
method, which it assumes to be correct, and so we need to verify its precondition
is established whenever we call it. In some sense, for proof purposes, this allows
us to ‘postulate’ a correct method and use it elsewhere &#x2013;this idea is very helpful
when we want to use an external libary's methods but do not &#x2013;or cannot&#x2013; want to prove them.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <p>

</p>

<div class="org-src-container">
<pre class="src src-c" id="org5d1ada9"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;limits.h&gt;</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires val &gt; INT_MIN;</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures always_non_negative</span><span style="color: #96A7A9; font-style: italic;">: \result &gt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures non_negative</span><span style="color: #96A7A9; font-style: italic;">: 0 &lt;= val ==&gt; \result == val;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures negative</span><span style="color: #96A7A9; font-style: italic;">: val &lt; 0  ==&gt; \result == -val;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span><span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">abs</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">val</span>);

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures UpperBound</span><span style="color: #96A7A9; font-style: italic;">: a &lt;= \result  &amp;&amp;  b &lt;= \result;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures Selection1</span><span style="color: #96A7A9; font-style: italic;">: a &lt;= b  ==&gt;  \result == b;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures Selection2</span><span style="color: #96A7A9; font-style: italic;">: b &lt;= a  ==&gt;  \result == a;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span><span style="color: #96A7A9; font-style: italic;">*/</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">max</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">b</span>);

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Uncomment this to observe proof obligations failing.</span>
<span style="color: #96A7A9; font-style: italic;">//</span>
<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">int max(int a, int b){ return 5; }</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #b58900;">@ requires inherited_from_abs</span><span style="color: #96A7A9; font-style: italic;">: a &gt; INT_MIN  &amp;&amp; b &gt; INT_MIN;</span>

<span style="color: #96A7A9; font-style: italic;">  @ assigns \nothing;</span>

<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures always_non_negative: inherited_from_abs</span><span style="color: #96A7A9; font-style: italic;">: \result &gt;= 0;</span>

<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures upper_bound: inherited_from_max</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">         \result &gt;= a &amp;&amp; \result &gt;= -a    // &#8776; result &#8805; |a|</span>
<span style="color: #96A7A9; font-style: italic;">      &amp;&amp; \result &gt;= b &amp;&amp; \result &gt;= -b;   // &#8776; result &#8805; |b|</span>

<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures selection: inherited_from_max</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">          \result == a || \result == -a</span>
<span style="color: #96A7A9; font-style: italic;">       || \result == b || \result == -b;</span>
<span style="color: #96A7A9; font-style: italic;">*/</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">abs_max</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">a</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">b</span>)
{
  <span style="color: #859900; font-weight: bold;">return</span> max(abs(a), abs(b));
}
</pre>
</div>

<p>
If we press <code>F8</code>, the frama-c gui shows green bullets for the declarations'
specifications. They have no implementation and so are assumed to be true.
Then the <code>abs_max</code> operation ‘inherits’ the preconditions and postconditions of
the methods it calls along the variables it uses.
</p>
 </details>
</div>
</div>
</div>
<div id="outline-container-orge8a3822" class="outline-2">
<h2 id="orge8a3822"><span class="section-number-2">5</span> Records</h2>
<div class="outline-text-2" id="text-5">
<p>
Thus far we have only considered built-in types, we now turn to considering
user-defined types that are more complex and are composites of simpler types
by using the <i>record</i> construct.
</p>

<ul class="org-ul">
<li>Record ≈ Tuple with named fields</li>
</ul>

<p>
A tuple <code>x = (x₀, x₁, …, xₙ)</code> is a function over the domain <code>0..n</code>, but in programming
the domain is usually of <i>named fields, labels,</i> and then it is called a <i>record</i>.
E.g., <code>{name = "Jasim", age = 27, language = "ar"}</code> is a record.
In the case that the labels <i>are</i> numbers, we obtain the notion of an <i>array</i>.
</p>

<p>
In other languages, this may be known as a <code>class</code>.
In Haskell this is the <code>data</code> keyword with ‘record syntax’,
and in Agda we may go on to use the <code>record</code> keyword.
</p>

<p>
C records are known as <i>structures</i> and they are just a list of
labels with their types. In using <code>struct</code> types, the <code>struct</code>
keyword must precede the type name in all declarations.
</p>
<div class="org-src-container">
<pre class="src src-c" id="org09eaf3e"><span style="color: #859900; font-weight: bold;">struct</span> <span style="color: #b58900; font-style: italic;">Pair</span>
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">fst</span>;
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">snd</span>;
};

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Note that &#8220;struct&#8221; always precedes the type name &#8220;Pair&#8221;.</span>

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Structs are passed in by value: They are copied locally.</span>
<span style="color: #96A7A9; font-style: italic;">//</span>
<span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #96A7A9; font-style: italic;">@ assigns \nothing;</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">doesNothing</span>(<span style="color: #859900; font-weight: bold;">struct</span> <span style="color: #b58900; font-style: italic;">Pair</span> <span style="color: #268bd2;">p</span>)
{
  p.fst = 666;
}

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">As usual, we use pointers to pass values by reference.</span>
<span style="color: #96A7A9; font-style: italic;">//</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires \valid(p);</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns (*p).fst;</span>
<span style="color: #96A7A9; font-style: italic;">*/</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">woah</span>(<span style="color: #859900; font-weight: bold;">struct</span> <span style="color: #b58900; font-style: italic;">Pair</span>* <span style="color: #268bd2;">p</span>)
{
  (*p).fst = 313;
}
</pre>
</div>

<div class="org-src-container">
<pre class="src src-c" id="orgb0167e9"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{

  <span style="color: #859900; font-weight: bold;">struct</span> <span style="color: #b58900; font-style: italic;">Pair</span> <span style="color: #268bd2;">p</span>; <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">no initalisation</span>

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Composite type without a name.</span>
  <span style="color: #859900; font-weight: bold;">struct</span> {<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">one</span>; <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">two</span>;} <span style="color: #268bd2;">q</span>;

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Initialisation with declaration.</span>
  <span style="color: #859900; font-weight: bold;">struct</span> <span style="color: #b58900; font-style: italic;">Pair</span> <span style="color: #268bd2;">r</span> = {11, 13};

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Note that in C, non-initialised variables have &#8220;arbitrary&#8221; values</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">which may change according to each new compilation!</span>

  printf(<span style="color: #2aa198;">"\n p = &#10216;fst: %d, snd: %d&#10217;"</span>, p.fst, p.snd);

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Set only first field.</span>
  p.fst = 3;
  printf(<span style="color: #2aa198;">"\n p = &#10216;fst: %d, snd: %d&#10217;"</span>, p.fst, p.snd);

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Zero-out all fields.</span>
  <span style="color: #859900; font-weight: bold;">struct</span> <span style="color: #b58900; font-style: italic;">Pair</span> <span style="color: #268bd2;">s</span> = {};
  printf(<span style="color: #2aa198;">"\n s = &#10216;fst: %d, snd: %d&#10217;"</span>, s.fst, s.snd);

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Invoke functions</span>

  doesNothing(s);
  printf(<span style="color: #2aa198;">"\n s = &#10216;fst: %d, snd: %d&#10217;"</span>, s.fst, s.snd);

  woah(&amp;s);
  printf(<span style="color: #2aa198;">"\n s = &#10216;fst: %d, snd: %d&#10217;"</span>, s.fst, s.snd);

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
</div>

<div id="outline-container-org5481f16" class="outline-3">
<h3 id="org5481f16"><span class="section-number-3">5.1</span> Allocation of a Record</h3>
<div class="outline-text-3" id="text-5-1">
<ul class="org-ul">
<li>Recall that a variable declaration associates the variable with a reference to
the variable in memory &#x2013;i.e., it's address&#x2013;,
moreover this reference is associated a value for the variable.</li>

<li>Record variables declared without a value are given the special default value <code>null</code>.</li>

<li>In Java, a record variable's reference is not directly associated with a record in memory!
It is usually associated with <code>null</code> or another reference.</li>
</ul>

<p>
To associate a record with a variable, we need to create memory large enough to contain the record
contents. Some languages use the keyword <code>new</code> to accomplish this task: Create a new reference
and associated it with the record variable being defined.
</p>

<p>
For example, in Java,
</p>
<pre class="example">
class Pair
{
  int fst;
  int snd;
}

Pair p = new Pair();
</pre>
<p>
The resulting environment is <code>p ↦ r</code> and the resulting memory state is
<code>r ↦ r', r' ↦ {fst = 0, snd = 0}</code>. Note that default values are used.
</p>

<ul class="org-ul">
<li>A reference that was added to memory by the construct <code>new</code> is called a <i>cell</i>.</li>
<li>The set of memory cells is called a <i>heap</i>.</li>
<li>The operation that adds a new cell to the memory state is called <i>allocation</i>.</li>
</ul>

<p>
Interestingly in C the creation of records does <i>not</i> allocate cells and so there
is no need for the <code>new</code> keyword. Indeed record variables cannot ever have the value <code>null</code>.
C directly associates a variable with a reference which has the record contents as its value.
That is, C has one less level of indirection than is found in Java.
E.g., <code>struct Pair p = {2, 3};</code> gives us environment <code>p ↦ r</code> and memory state <code>r ↦ {fst = 2, snd = 3}</code>,
whereas Java would have <code>p ↦ r′</code> in the environment and <code>r′ ↦ r</code> additionally in the memory state
That is to say, <i>records in Java correspond to references to records in C</i>
and so Java access notation <code>r.f</code> is rendered in C as <code>(*r).f</code>.
</p>

<p>
Remember that references are themselves first-class values and it is possible for a
reference to be associated to another reference.
</p>
</div>
</div>

<div id="outline-container-orgec54702" class="outline-3">
<h3 id="orgec54702"><span class="section-number-3">5.2</span> The Four Constructs of Records</h3>
<div class="outline-text-3" id="text-5-2">
<p>
Records are usually handled using four constructs:
</p>
<ul class="org-ul">
<li>Defining a record type; e.g., using <code>class</code> or <code>struct</code>.</li>
<li>Allocating a cell; e.g., using <code>new</code> or <code>malloc</code>.</li>
<li>Accessing a field; e.g., <code>myRecord.myField</code>.</li>
<li>Assigning to a field; e.g., <code>myRecord.myField = myValue</code>.</li>
</ul>

<p>
Understanding records in a language is thus tantamount to understanding
these fundamental basics. E.g., in functional languages, assignment to a field
is essentially record copying or, more efficiently, reference redirection.
Incidentally, neither Haskell nor Agda make use of the <code>new</code> keyword:
Declarations <i>must be</i> accompanied by initialisation which indicate the need
for cell allocation &#x2013;consequently there is no need for a <code>null</code> value.
The use of <code>new</code> may be used for careful efficiency optimisations,
or memory management &#x2013;which is rarely brought to the forefront in functional languages.
</p>

<p>
The act of assigning to each field of a record
is so common that they are usually placed into a so-called <i>constructor</i> method.
</p>
</div>
</div>

<div id="outline-container-orgf2627f4" class="outline-3">
<h3 id="orgf2627f4"><span class="section-number-3">5.3</span> Sharing, Equality, &amp; Garbage</h3>
<div class="outline-text-3" id="text-5-3">
</div>
<div id="outline-container-org95899bb" class="outline-4">
<h4 id="org95899bb"><span class="section-number-4">5.3.1</span> Assignment</h4>
<div class="outline-text-4" id="text-5-3-1">
<p>
Suppose <code>x</code> and <code>y</code> are variables of type <code>Pair</code>, with environment and memory state:
</p>
<pre class="example">
locations  =  x ↦ r₁, y ↦ r₂
values     =  r₁ ↦ r₃, r₂ ↦ r₄, r₃ ↦ {fst = 3, snd = 5}, r₄ ↦ {fst = 7, snd = 9}
</pre>
<p>
Then assignment <code>y ≔ x</code> results in:
</p>
<pre class="example">
locations  =  x ↦ r₁, y ↦ r₂
values     =  r₁ ↦ r₃, r₂ ↦ r₃, r₃ ↦ {fst = 3, snd = 5}, r₄ ↦ {fst = 7, snd = 9}
                      -Change Here-
</pre>
<p>
That is, the value of <code>x</code> is computed which is the reference <code>r₃</code>
&#x2013;since <code>value (location x) ≈ value r₁ ≈ r₃</code>&#x2013;
and we associate this value with the location of <code>y</code>, that is, reference <code>r₂</code>.
</p>

<p>
Notice that now no variable has the value of <code>r₄</code> and so it is considered <i>garbage</i>
in our state. Moreover, nothing can get to it since values are only associated with
locations, none of which have address <code>r₄</code>. Hence there is no <i>observable</i> affect to
their absence or presence. We want to recycle the physical memory
or else we would quickly run out of memory. A <i>garbage collector</i> is an automated
system that collects and recycles such cells. Older languages like C do not
have such a system and so memory must be managed by hand.
</p>

<p>
Anyhow, henceforth <code>x</code> and <code>y</code> <b>share</b> the values of the record thereby all alterations,
through either variable, are observable by the other.
</p>

<p>
Since <code>x</code> and <code>y</code> are reference values, the assignment makes <code>x == y</code> a true statement
since they <i>share</i> the same cell. This is known as <i>physical equality</i>.
</p>

<p>
Sometimes we wish for two variables to <i>share</i> a single integer, which may
not be possible with built-in types, but can be accomplished by using <i>wrapper record types</i>:
Records that have a lone single field. This idea of `boxing up' primitive types
allows us, for example, to define functions with arguments that are passed by reference
thereby modifying their arguments; such as the <code>swap</code> function that swaps the contents of its
arguments.
</p>
</div>
</div>

<div id="outline-container-org3225ae0" class="outline-4">
<h4 id="org3225ae0"><span class="section-number-4">5.3.2</span> Copy</h4>
<div class="outline-text-4" id="text-5-3-2">
<p>
If we instead execute <code>y.fst ≔ x.fst; y.snd ≔ x.snd</code>
Then the resulting state is:
</p>
<pre class="example">
locations  =  x ↦ r₁, y ↦ r₂
values     =  r₁ ↦ r₃, r₂ ↦ r₄, r₃ ↦ {fst = 3, snd = 5}, r₄ ↦ {fst = 3, snd = 5}
                                                                    -Change Here-
</pre>

<p>
In this case, any alteration to <code>x</code>'s values are <i>not</i> observable by <code>y</code>.
</p>

<p>
Moreover, in this case, all fields are equal and so we have <code>x</code> and <code>y</code>
are <i>structurally equal</i>. This notion is sometimes called <code>equals method</code>
and the previous is <code>equals equals (==)</code>.
</p>
</div>
</div>
</div>

<div id="outline-container-orgfc2132a" class="outline-3">
<h3 id="orgfc2132a"><span class="section-number-3">5.4</span> Arrays</h3>
<div class="outline-text-3" id="text-5-4">
<p>
<i>Arrays</i> are essentially records whose labels are numeric <i>and</i> all labels have
the same type. The number of fields of an array is determined during allocation
of the array, and not during the declaration of its type, as is the case with records.
This trade-off makes arrays more desirable in certain contexts.
</p>

<p>
The fields are usually numbered <code>0</code> to <code>n-1</code>, where <code>n</code> is the number of fields.
</p>

<p>
Once an array is allocated, its size cannot be changed.
&#x2013;Stop &amp; think: Why not?
</p>

<ul class="org-ul">
<li>Arrays of type <code>T</code> are denoted by <code>T[]</code> in C/Java.
<ul class="org-ul">
<li>Matrices, or arrays of arrays, are denoted by <code>T[][]</code> with access
by <code>T[i][j]</code>.</li>
</ul></li>
</ul>

<p>
C arrays, like records, are not allocated.
Consequently, their size cannot be determined by allocation and so must be
  a part of their type.
</p>

<p>
C arrays of type <code>T</code> of length <code>n</code> are declared using the mixfix syntax: <code>T x[n];</code>
Each element of the array is arbitrary &#x2013;with no designated defaults&#x2013;
so it is best to initialise them. E.g., <code>int x[10] = {};</code> sets all elements of
the array to 0.
</p>

<div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">makeFive</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span>[], <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">length</span>)
{
  <span style="color: #859900; font-weight: bold;">for</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">i</span>=0; i != length; i++)
    x[i] = 5;
}

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">x</span>[3] = {};
  printf(<span style="color: #2aa198;">"\n x = [%d, %d, %d]"</span>, x[0], x[1], x[2]);

  makeFive(x, 3);
  printf(<span style="color: #2aa198;">"\n x = [%d, %d, %d]"</span>, x[0], x[1], x[2]);

  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
However, C arrays differ from C records in that array variables are actually
references that are associated in memory with an array. Consequently, array
arguments to methods are automatically pass by reference!
</p>

<p>
Moreover this means the assignment <code>t[k] = u</code> works in a rather general sense:
<code>t</code> suffices to be any expression whose <i>value</i> is a reference associated in memory
with an array.
</p>
</div>
</div>
</div>
<div id="outline-container-orgdb5f42b" class="outline-2">
<h2 id="orgdb5f42b"><span class="section-number-2">6</span> Arrays &#x2013; <code>\forall, \exists</code></h2>
<div class="outline-text-2" id="text-6">
<p>
Arrays are commonly handled using loops; let's look at some examples.
</p>

<div class="org-src-container">
<pre class="src src-c" id="org3a97b8d"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires 0 &lt; N;</span>
<span style="color: #96A7A9; font-style: italic;">  @ requires \valid_read(array + (0 .. N - 1));  // N is the length of the array</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior found_element</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  @    assumes \exists integer i; 0 &lt;= i &lt; N  &amp;&amp;  array[i] == element;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures 0 &lt;= \result &lt; N;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior did_not_find_element</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  @    assumes \forall integer i; 0 &lt;= i &lt; N  ==&gt; array[i] != element;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures \result == N;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  @ disjoint behaviors;</span>
<span style="color: #96A7A9; font-style: italic;">  @ complete behaviors;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">int</span> linear_search(<span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">array</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">N</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">element</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span> = 0;

  <span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ loop assigns n;</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop invariant 0 &lt;= n &lt;= N;</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant not_found_yet</span><span style="color: #96A7A9; font-style: italic;">: \forall integer i; 0 &lt;= i &lt; n  ==&gt;  array[i] != element;</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop variant N - n;</span>
<span style="color: #96A7A9; font-style: italic;">    */</span>
  <span style="color: #859900; font-weight: bold;">while</span>( n != N  &amp;&amp; array[n] != element ) n++;
  <span style="color: #859900; font-weight: bold;">return</span> n &lt; N ? n : N;
}
</pre>
</div>

<p>
Some remarks are in order.
</p>

<ul class="org-ul">
<li><code>\valid_read(array + (0 .. N - 1))</code> ensures that the memory addresses
<code>array + 0, ..., array + N-1</code> can be read &#x2013;as discussed when introducing <code>\valid</code>.</li>

<li>The loop continues as long as we have not yet found the element.
<ul class="org-ul">
<li>As such, every index thus far differs from the element sought after.</li>
<li>That is, forall index <i>i</i> in the array bounds, we have <i>array[i] ≠ element</i>.</li>
<li>This invariant is stated using the <code>forall</code> syntax.</li>
</ul></li>

<li>The variable naming <code>n, N</code> is intended to be suggestive:
When <i>n = N</i> then we have traversed the whole array.
<ul class="org-ul">
<li>We began at 0 and are working upward to <i>N</i>.</li>
<li>At each step, we traverse the array by one more item
thereby decreasing the amount of items remaining &#x2013;which is <i>N - n</i>.</li>
</ul></li>

<li>If it is <i>provable</i> that some index contains the desired element,
then in that case our program ensures the output result is a valid index.</li>

<li>The ACSL type <code>integer</code> is preferable to the C type <code>int</code> since it is not constrained by
any representation limitations and instead acts more like its pure mathematical counterpart ℤ.</li>
</ul>

<p>
It is important to note that the universal ‘∀’ uses <code>==&gt;</code> to delimit the
range from the body predicate, whereas the existential ‘∃’ uses <code>&amp;&amp;</code>
&#x2013;this observation is related to the “trading laws” for quantifiers.
</p>

<table>


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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><code>\forall type x; r(x) ==&gt; p(x)</code></td>
<td class="org-left">Every element <code>x</code> in range <code>r</code> satisfies <code>p</code></td>
</tr>

<tr>
<td class="org-left"><code>\exists type x; r(x) &amp;&amp; p(x)</code></td>
<td class="org-left">Some  element <code>x</code> in range <code>r</code> satisfies <code>p</code></td>
</tr>
</tbody>
</table>

<p>
Notice the striking difference between
the <code>\exists integer x; \false &amp;&amp; even x</code>
&#x2013;which is unprovable since false is never true!&#x2013;
and <code>\exists integer x; \false ==&gt; even x</code>
&#x2013;which can be satisfied by infinitely many <code>x</code>, since false implies anything.
</p>
<p>
Of course we could start at the end of the array and “work down” until
we find the element, or otherwise, say, return -1. Let's do so without using
a new local variable <code>n</code>.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c" id="org787e112"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires 0 &lt; N;</span>
<span style="color: #96A7A9; font-style: italic;">  // </span><span style="color: #b58900;">@ requires Exercise</span><span style="color: #96A7A9; font-style: italic;">: read access to a[0], ..., a[N-1];</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  // @ assigns FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior found_element</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    assumes Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;  // &#8220;element &#8712; array&#8221;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    ensures Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;  // Output is valid index in array</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ behavior did_not_find_element</span><span style="color: #96A7A9; font-style: italic;">:</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    assumes Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;     // &#8220;element &#8713; array&#8221;</span>
<span style="color: #96A7A9; font-style: italic;">  @    ensures \result == -1;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  @ disjoint behaviors;</span>
<span style="color: #96A7A9; font-style: italic;">  @ complete behaviors;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">linear_search_no_local</span>(<span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">array</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">N</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">element</span>)
{
  <span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ loop assigns N;</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop invariant 0 &lt;= N &lt;= \at(N, Pre);</span>
<span style="color: #96A7A9; font-style: italic;">    // </span><span style="color: #b58900;">@ loop invariant not_found_yet: Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">    // </span><span style="color: #b58900;">@ loop variant Exercise: FixMe</span><span style="color: #96A7A9; font-style: italic;">: 666;</span>
<span style="color: #96A7A9; font-style: italic;">    */</span>
  <span style="color: #859900; font-weight: bold;">while</span>( 0 != N  &amp;&amp; array[N-1] != element ) N--;
  <span style="color: #859900; font-weight: bold;">return</span> N - 1;
}
</pre>
</div>
 </details>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c" id="org9dd607b"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #b58900;">@ requires Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe; // array[0], ..., array[N-1] are accessible</span>
<span style="color: #96A7A9; font-style: italic;">  @ requires element &lt; INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns array[0 .. N-1];</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures Exercise: all_array_equals_element</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">make_constant</span>(<span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">array</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">N</span>, <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">element</span>)
{
  <span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ loop assigns N, array[0 .. \at(N,Pre)-1];</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant range_on_N</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant constant_so_far</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">    @ loop variant N;</span>
<span style="color: #96A7A9; font-style: italic;">    */</span>
  <span style="color: #859900; font-weight: bold;">for</span> (; 0 != N; N--) array[N-1] = element;
}
</pre>
</div>
 </details>
<br>

<p>
Notice that the invariants are getting way too long &#x2013;and worse: Repetitive!
We can abstract common formulae into more general and reusable shapes by
declaring them as ACSL logical functions &#x2013;keep reading!
</p>

<p>
On an unrelated note, sometimes we try to prove a program that we just
coded incorrectly, so if things are going nowhere then maybe try a few tests
to ensure you're on the right track.
</p>
</div>
</div>
<div id="outline-container-org6f07fb1" class="outline-2">
<h2 id="org6f07fb1"><span class="section-number-2">7</span> Recursion</h2>
<div class="outline-text-2" id="text-7">
<p>
The definition of <code>wp</code> for function calls is correct provided the function body
itself only contains invocations to <i>previously</i> defined functions?
</p>

<p>
What about <i>recursive function definitions</i>:
Definitions invoking the function being defined or invoking functions that invoke
functions that eventually invoke the function currently being defined?
</p>

<p>
Since invocations are delegated to the state, which handles all defined names,
we may invoke whatever function provided its name is found.
Since C requires names to be declared before use, we may have mutually recursive
functions by ‘prototyping’: Declaring the function signature, then at some point
providing the actual implementation.
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>
<span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdbool.h&gt;</span>

<span style="color: #b58900; font-style: italic;">bool</span> <span style="color: #b58900;">odd</span>(<span style="color: #b58900; font-style: italic;">int</span>); <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">protoyping the odd function</span>

<span style="color: #b58900; font-style: italic;">bool</span> <span style="color: #b58900;">even</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span>) <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">using odd here, even though it's not yet defined</span>
{
  <span style="color: #859900; font-weight: bold;">if</span> (n == 0) <span style="color: #859900; font-weight: bold;">return</span> <span style="color: #6c71c4; font-weight: bold;">true</span>;
  <span style="color: #859900; font-weight: bold;">else</span>        <span style="color: #859900; font-weight: bold;">return</span> odd(n - 1);
}

<span style="color: #b58900; font-style: italic;">bool</span> <span style="color: #b58900;">odd</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span>)
{
  <span style="color: #859900; font-weight: bold;">if</span> (n == 0) <span style="color: #859900; font-weight: bold;">return</span> <span style="color: #6c71c4; font-weight: bold;">false</span>;
  <span style="color: #859900; font-weight: bold;">else</span>        <span style="color: #859900; font-weight: bold;">return</span> even(n - 1);
}

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  printf(<span style="color: #2aa198;">"\n 7 is even? ... %d"</span>, even(7));
  printf(<span style="color: #2aa198;">"\n 7 is odd? ... %d"</span>, odd(7));
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>
 </details>

<p>
Anyhow, e.g., <code>void f(int x){ f(x); }</code> has the definition of <code>wp f(x)</code> using the value of <code>wp f(x)</code>
and so is circular. How can this be avoided?
</p>

<p>
Note that a <i>recursive definition</i> is <b>not</b> just a definition that uses the object which it
is defining. Otherwise, the previous <code>f</code> might as well have been the definition of
the factorial function that on input <code>x</code> simply invokes itself; then again it could
have been any function!
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <p>
We generally think of recursive definitions as definitions by usual ℕ-induction, however
this is not absolutely true.
E.g., the following function at <code>n</code> not only relies on
values on smaller inputs but also on values at larger inputs to compute the value at <code>n</code>!
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #268bd2; font-weight: bold;">#include</span><span style="color: #2aa198;">&lt;stdio.h&gt;</span> <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">for IO</span>

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">iterations</span> = 0;
<span style="color: #268bd2; font-weight: bold;">#define</span> <span style="color: #b58900;">RETURN</span>(<span style="color: #268bd2;">i</span>, <span style="color: #268bd2;">n</span>) { printf(<span style="color: #2aa198;">"\n **Computed value for input %d is %d**"</span>, i, n); \
                       iterations++; <span style="color: #859900; font-weight: bold;">return</span> n; }

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Uses smaller as well as larger values just to compute current value!</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">f</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span>)
{
  printf(<span style="color: #2aa198;">"\n Computing value for: %d"</span>, n);

  <span style="color: #859900; font-weight: bold;">if</span> (n &lt;= 1)     RETURN(n, 1)
  <span style="color: #859900; font-weight: bold;">if</span> (n % 2 == 0) RETURN(n, 1 + f(n / 2))
  <span style="color: #859900; font-weight: bold;">else</span>            RETURN(n, 2 * f(n + 1))
}

<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">main</span>()
{
  f(12);
  printf(<span style="color: #2aa198;">"\n\n The function was invoked for a total of %d times!"</span>, iterations);
  <span style="color: #859900; font-weight: bold;">return</span> 0;
}
</pre>
</div>

<p>
Similarly the Ackermann function is a recursive function that has been proven
to be undefinable using only nested definitions by ℕ-induction.
( However, since ℕ×ℕ is well-ordered, it is a valid definition. )
</p>
 </details>

<p>
We may try to remove recursive calls by replacing every recursive call with
the function body itself, which then has a new recursive call. We may continue
to expand forever by replacing recursive calls with the original function body.
The “result” will be an non-recursive program that is infinitely long.
</p>

<p>
Thus, recursive programs, like <code>while</code> loops, are a means of expressing infinite
programs and, like <code>while</code> loops, they introduce the possibility of non-termination.
</p>

<p>
As for <code>while</code> loops, we can approximate the infinitely long non-recursive program
by simply giving-up on the <code>n</code>-th expansion, not making any more recursive calls.
Essentially, we do <code>n</code> recursive calls then give-up if the program has not completed.
Hence, we again consider the limit.
</p>
</div>

<div id="outline-container-org7a76a2e" class="outline-3">
<h3 id="org7a76a2e"><span class="section-number-3">7.1</span> Recursive Definitions and Fixed Point Equations</h3>
<div class="outline-text-3" id="text-7-1">
<p>
A recursive function such as the factorial function can be seen as
an equation where the unknown variable is the function currently being defined.
For example, the factorial function is the <i>unique partial function</i> <code>f</code> satisfying
the equation
</p>
<div class="org-src-container">
<pre class="src src-haskell">f  <span style="color: #b58900;">&#8776;</span> (x <span style="color: #268bd2;">&#8614;</span> <span style="color: #859900; font-weight: bold;">if</span> x <span style="color: #268bd2;">&#8776;</span> 0 <span style="color: #859900; font-weight: bold;">then</span> 1 <span style="color: #859900; font-weight: bold;">else</span> x <span style="color: #268bd2;">*</span> f(x<span style="color: #268bd2;">-</span>1))
</pre>
</div>
<p>
This equation has the form <code>f ≈ F(f)</code>, so it is a “fixed point equation”.
</p>

<p>
Since recursive functions may not terminate, the functions they describe
are partial. It can be proven that any fixed point equation <i>always</i> has
at least one solution; i.e., it defines at least one partial function on the integers.
For example, the equation <code>f ≈ (x ↦ 1 + f(x))</code> corresponding to
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">loop</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span>){ <span style="color: #859900; font-weight: bold;">return</span> 1 + loop(n); }
</pre>
</div>
<p>
Has one solution: The empty function, which is not defined on any input.
</p>

<p>
The set of possible solutions can be ordered by inclusion
of their graphs and so one of them is the smallest.
Incidentally, this least solution is also obtained by the aforementioned limit!
</p>

<p>
E.g., every function is a solution to the equation <code>f ≈ (x ↦ f x)</code>.
</p>
</div>
</div>

<div id="outline-container-org4736a67" class="outline-3">
<h3 id="org4736a67"><span class="section-number-3">7.2</span> Programming without Assignment &#x2013;the Functional Core</h3>
<div class="outline-text-3" id="text-7-2">
<p>
Notice that the factorial function can be written without using assignments:
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ axiomatic Factorial</span>
<span style="color: #96A7A9; font-style: italic;">  @ {</span>
<span style="color: #96A7A9; font-style: italic;">  @    logic integer factorial(integer n);</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    axiom fact_zero</span><span style="color: #96A7A9; font-style: italic;">:  \forall integer n;    factorial(0) == 1;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    axiom fact_succ</span><span style="color: #96A7A9; font-style: italic;">:  \forall integer n; 0 &lt;= n</span>
<span style="color: #96A7A9; font-style: italic;">  @                 ==&gt;  factorial (n + 1) == n * factorial (n);</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    axiom fact_monotone </span><span style="color: #96A7A9; font-style: italic;">: \forall integer m,n; 0 &lt;= m &lt;= n</span>
<span style="color: #96A7A9; font-style: italic;">  @          ==&gt; factorial(m) &lt;= factorial(n);</span>
<span style="color: #96A7A9; font-style: italic;">  @ }</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
</pre>
</div>
 </details>
<div class="org-src-container">
<pre class="src src-c" id="org44565eb"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #b58900;">@ requires Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@ ensures Exercise</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">fact_imp</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span>)
{
  <span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">r</span> = 1;

  <span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ loop assigns i, r;</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant range_on_i</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop invariant relationship_between_r_i_n</span><span style="color: #96A7A9; font-style: italic;">: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">    </span><span style="color: #b58900;">@ loop variant FixMe</span><span style="color: #96A7A9; font-style: italic;">: 666;</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
  <span style="color: #859900; font-weight: bold;">for</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">i</span> = 1; i &lt;= n; i++)
    r = r * i;

  <span style="color: #859900; font-weight: bold;">return</span> r;
}

<span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">&#8776;</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ requires 0 &lt;= n;</span>
<span style="color: #96A7A9; font-style: italic;">  @ requires factorial(n) &lt;= INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">  @ assigns \nothing;</span>
<span style="color: #96A7A9; font-style: italic;">  @ ensures \result == factorial(n);</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #b58900;">fact_functional</span>(<span style="color: #b58900; font-style: italic;">int</span> <span style="color: #268bd2;">n</span>)
{
  <span style="color: #859900; font-weight: bold;">if</span> (n == 0) <span style="color: #859900; font-weight: bold;">return</span> 1;
  <span style="color: #859900; font-weight: bold;">return</span> n * fact_functional(n - 1);
}
</pre>
</div>
<p>
( Notice that <i>¬(i ≤ n)[i ≔ 1]  ≡  n = 0</i> when considering <i>n : ℕ</i>. )
</p>

<p>
Hence if we remove assignments, then the memory state for computation is
always empty and so sequences and loops become useless. We are left with
only variable declarations, function calls, and the built-in operations.
This is the <i>functional core</i> of the language and it is surprisingly as powerful
as the imperative core. Why? Recall that a term, or statement, can be thought
of as a (partial) function of its free variables; now the functions obtained
this way using only the functional core are the same as those obtained by also
using the whole of the imperative core!
Note that omitting recursion falsifies this result.
</p>
</div>
</div>
</div>

<div id="outline-container-orgc9ce1d2" class="outline-2">
<h2 id="orgc9ce1d2"><span class="section-number-2">8</span> Hehner's Problem</h2>
<div class="outline-text-2" id="text-8">
<p>
We have now covered enough material to tackle the problem posed
at the very beginning &#x2014;that of <code>whatDo</code>.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-c" id="orgce28f81"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@  requires \valid(x) &amp;&amp; \valid(y);</span>
<span style="color: #96A7A9; font-style: italic;">     requires 0 &lt;= *x &lt; 31;</span>
<span style="color: #96A7A9; font-style: italic;">     requires Exercise: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;">     assigns *x, *y;</span>
<span style="color: #96A7A9; font-style: italic;">     ensures Exercise: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">whatDo</span>(<span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">x</span>, <span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">y</span>)
{
  <span style="color: #859900; font-weight: bold;">if</span> (*x == 0)
    {
      *y = 1; *x = 3;
    }
  <span style="color: #859900; font-weight: bold;">else</span>
    {
      *x -= 1; *y = 7;
      whatDo(x, y);
      *y *= 2; *x = 5;
    }
}
</pre>
</div>
 </details>

<p>
Running a few test inputs, it can be seen that this program
sets <code>y</code> to be the power of <code>x</code>. Further testing may reveal interesting
issues when <code>x</code> and <code>y</code> refer to the same memory location!
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <pre class="example">
⟨x = 0, y = 0⟩ ↦ ⟨x = 3, y = 1⟩
⟨x = 1, y = 2⟩ ↦ ⟨x = 5, y = 2⟩
⟨x = 3, y = 4⟩ ↦ ⟨x = 5, y = 8⟩
⟨x = 5, y = 6⟩ ↦ ⟨x = 5, y = 32⟩
⟨x = 7, y = 8⟩ ↦ ⟨x = 5, y = 128⟩
⟨x = 0, y = 99⟩ ↦ ⟨x = 3, y = 1⟩

x == y  ⇒  Segmentation Fault
</pre>
 </details>

<p>
With this guidance in hand, we aim to axiomatise the power function:
</p>
<div class="org-src-container">
<pre class="src src-c" id="org6c5e159"><span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@ axiomatic Pow</span>
<span style="color: #96A7A9; font-style: italic;">  @ {</span>
<span style="color: #96A7A9; font-style: italic;">  @    logic integer pow(integer b, integer n);</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    axiom pow_zero</span><span style="color: #96A7A9; font-style: italic;">:  \forall integer b;        pow(b, 0) == 1;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    axiom pow_one</span><span style="color: #96A7A9; font-style: italic;">:  \forall integer b;        pow(b, 1) == b;</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    axiom pow_homomorphism</span><span style="color: #96A7A9; font-style: italic;">:  \forall integer b, m, n;</span>
<span style="color: #96A7A9; font-style: italic;">  @                 pow(b, m + n) == pow(b, m) * pow(b, n);</span>
<span style="color: #96A7A9; font-style: italic;">  @</span>
<span style="color: #96A7A9; font-style: italic;">  </span><span style="color: #b58900;">@    axiom pow_monotone </span><span style="color: #96A7A9; font-style: italic;">: \forall integer b,m,n; b &gt;= 0  &amp;&amp;  0 &lt;= m &lt;= n</span>
<span style="color: #96A7A9; font-style: italic;">  @          ==&gt; pow(b,m) &lt;= pow(b,n);</span>
<span style="color: #96A7A9; font-style: italic;">  @ }</span>
<span style="color: #96A7A9; font-style: italic;">  */</span>
</pre>
</div>

<p>
Notice that there are infinitely many solutions <code>f</code> to the equations
</p>
<ul class="org-ul">
<li><code>pow_zero</code>: <i>f(0) = 1</i></li>
<li><code>pow_homomorphism</code>: <i>f(m + n) = f(m) * f(n)</i></li>
</ul>

<p>
Which ones? Since every natural number is of the form <i>1 + 1 + ⋯ + 0</i>
the second requirement yields \\ <i>f(n) = f (1 + 1 + ⋯ + 0) = f 1 * f 1 * ⋯ f 1 * f 0;</i>
which by the first requirement simplifies to <i>f(n) = (f 1)ⁿ</i>.
Hence for any choice of number <i>f(1) : ℕ</i>, we obtain a function <i>f</i>
that satisfies these definitions. If we want <i>f</i> to be the <i>n</i> product of a number
<i>b</i> then we need to insist <i>f 1 = b</i> &#x2013;which is just <code>pow_one</code>!
</p>

<p>
From the axioms, we obtain some useful lemmas.
</p>
<div class="org-src-container">
<pre class="src src-c" id="org5b29f26"><span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ lemma pow_succ</span><span style="color: #96A7A9; font-style: italic;">:  \forall integer b, n; n &gt;= 0  ==&gt;     pow(b, n + 1) == pow(b, n) * b;</span>
<span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ lemma powNonNeg </span><span style="color: #96A7A9; font-style: italic;">: \forall integer b,n; b &gt;= 0 ==&gt; n &gt;= 0 ==&gt; pow(b,n) &gt;= 0;</span>
<span style="color: #96A7A9; font-style: italic;">//</span><span style="color: #b58900;">@ lemma pow2bound </span><span style="color: #96A7A9; font-style: italic;">: \forall integer n; 0 &lt;= n &lt; 31 ==&gt; pow(2, n) &lt; INT_MAX;</span>
<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #b58900;">@ lemma powPredT </span><span style="color: #96A7A9; font-style: italic;">: \forall integer b,n,m;  b &gt;= 0  &amp;&amp;  n &gt; 0</span>
<span style="color: #96A7A9; font-style: italic;">                                &amp;&amp;  pow(b, n) &lt;= m  ==&gt;  b * pow(b, n-1) &lt;= m;</span><span style="color: #96A7A9; font-style: italic;"> */</span>
</pre>
</div>

<p>
With this setup, the reader should now be able to solve the <code>FixMe</code>'s in <code>whatDo</code>
&#x2013;which I've renamed to “hehner”, after the fellow who showed it as an example
in his excellent specifications and correctness text, <a href="http://www.cs.toronto.edu/~hehner/aPToP/">A Practical Theory of Programming</a>.
</p>
<div class="org-src-container">
<pre class="src src-c" id="org24fa190"><span style="color: #268bd2; font-weight: bold;">#include</span> <span style="color: #2aa198;">"Prelude.c"</span>

<span style="color: #96A7A9; font-style: italic;">/*</span><span style="color: #96A7A9; font-style: italic;">@  requires \valid(x) &amp;&amp; \valid(y);</span>
<span style="color: #96A7A9; font-style: italic;">     requires 0 &lt;= *x &lt; 31;</span>
<span style="color: #96A7A9; font-style: italic;">     requires Exercise: FixMe;      // Assuming \false, gives us anything!</span>
<span style="color: #96A7A9; font-style: italic;">     assigns *x, *y;</span>
<span style="color: #96A7A9; font-style: italic;">     ensures Exercise: FixMe;</span>
<span style="color: #96A7A9; font-style: italic;"> */</span>
<span style="color: #b58900; font-style: italic;">void</span> <span style="color: #b58900;">hehner</span>(<span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">x</span>, <span style="color: #b58900; font-style: italic;">int</span>* <span style="color: #268bd2;">y</span>)
{
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">Introduce a local for reasoning, to avoid having to write \at(*x, Pre).</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">ghost const int X = *x;</span>

  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert given: 0 &lt;= X &lt; 31;</span>
  <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert taking_powers_with_monotonicity: 1 &lt;= pow(2,X) &lt;= INT_MAX;</span>
  <span style="color: #859900; font-weight: bold;">if</span> (*x == 0)
    {
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert condition: X == 0;</span>
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert taking_powers: pow(2,X) == 1;</span>
      *y = 1; *x = 3;
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert *y == pow(2,X);</span>
    }
  <span style="color: #859900; font-weight: bold;">else</span>
    {
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert condition: 0 &lt; X &lt; 31;</span>
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert pow_with_monotonicity: 1 &lt; pow(2, X) &lt;= INT_MAX;</span>
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert factoring: 2 * pow(2, X-1) &lt;= INT_MAX;</span>

      *x -= 1; *y = 7;
      hehner(x, y);

      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert function_ensures: *y == pow(2, X - 1);</span>
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert pow(2, X) == 2 * pow(2, X - 1);</span>
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert     pow(2, X    ) &#8804; INT_MAX;</span>
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert 2 * pow(2, X - 1) &#8804; INT_MAX;</span>
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert y2_no_overflow: *y * 2 &#8804; INT_MAX;</span>

      *y *= 2; *x = 5;

      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert our_goal: *y == pow(2, X);</span>
      <span style="color: #96A7A9; font-style: italic;">// </span><span style="color: #96A7A9; font-style: italic;">assert incidentally: *y &lt;= INT_MAX;</span>
    }
}
</pre>
</div>

<p>
I've left the <code>assert</code>'s since they may make the program proof more comprehensible
to the reader &#x2013;yet notice that I did not use <code>@</code> and so they are not visible,
nor necessary, to Frama-C.
</p>
</div>
</div>

<div id="outline-container-org8c13011" class="outline-2">
<h2 id="org8c13011"><span class="section-number-2">9</span> Advanced Data Structures</h2>
<div class="outline-text-2" id="text-9">
<p>
This is what I have so far, yet I look forward to including material
utilising linked lists as well as making more of the Curry-Howard Correspondence.
</p>

<p>
Anyhow, I hope you've enjoyed yourself and hopefully learned something neat! Byebye!
</p>
</div>
</div>

<div id="outline-container-orgcb55316" class="outline-2">
<h2 id="orgcb55316"><span class="section-number-2">10</span> The Underlying Elisp</h2>
<div class="outline-text-2" id="text-10">
<p>
The following utilities are loaded when this file is opened.
After the first time the file <code>InteractiveWayToC.el</code> is created and this section
may be deleted, or <code>COMMENT</code>-ed, as it is loaded in the <code>footer</code> section at the end of this file.
</p>

<ol class="org-ol">
<li>Make some changes, look at them with <code>f7</code>.
<ul class="org-ul">
<li>Or execute with <code>f6</code> if there is a <code>main</code> method.</li>
</ul></li>
<li>Check your progress with <code>f9</code>, within Emacs.</li>
<li>If confused, open the Frama-C gui with <code>f8</code>.</li>
</ol>

<p>
Note: There is a 10 second time limit on the proof search.
</p>

<p>
Every source block is in ‘focus’ when the variables <code>NAME</code> and <code>NAMECode</code> refer to it.
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">setq</span> Language <span style="color: #2aa198;">"c"</span> LanguageExtension <span style="color: #2aa198;">"c"</span>)
(<span style="color: #859900; font-weight: bold;">setq</span> NAMEEXT (<span style="color: #b58900;">buffer-name</span>) NAME (<span style="color: #b58900;">file-name-sans-extension</span> NAMEEXT))
(<span style="color: #859900; font-weight: bold;">setq</span> NAMECode (<span style="color: #b58900;">concat</span> NAME <span style="color: #2aa198;">"."</span> LanguageExtension))
</pre>
</div>
 </details>

<p>
We explicitly change focus using <code>[not-]currently-working-with</code> methods.
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">buffer-name-sans-extension</span> () <span style="color: #35a69c; font-style: italic;">""</span>
  (<span style="color: #b58900;">file-name-sans-extension</span> (<span style="color: #b58900;">buffer-name</span>))
)

(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">currently-working-with</span> (<span style="color: #b58900; font-style: italic;">&amp;optional</span> name)
  <span style="color: #35a69c; font-style: italic;">"Provide the name (without extension) of the source block's resulting file.</span>
<span style="color: #35a69c; font-style: italic;">   The name is then tied to the &#8220;NAMECode&#8221; global variable utilised</span>
<span style="color: #35a69c; font-style: italic;">   by the &#8220;show-code&#8221; method, &lt;f7&gt;, and the &#8220;interpret&#8221; command's global variable &#8220;NAME&#8221;.</span>

<span style="color: #35a69c; font-style: italic;">   If no argument is provided, we use &#8220;&#10218;BufferName&#10219;.c&#8221; as default file name.</span>
<span style="color: #35a69c; font-style: italic;">  "</span>
  (<span style="color: #859900; font-weight: bold;">unless</span> name (<span style="color: #859900; font-weight: bold;">setq</span> name (buffer-name-sans-extension)))
  (<span style="color: #859900; font-weight: bold;">setq</span> NAME name)
  (<span style="color: #859900; font-weight: bold;">setq</span> NAMECode (<span style="color: #b58900;">concat</span> name <span style="color: #2aa198;">".c"</span>))
)

(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">not-currently-working-with</span> (<span style="color: #b58900; font-style: italic;">&amp;optional</span> name)
  <span style="color: #35a69c; font-style: italic;">"When &#8220;:tangle fn&#8221; has &#8220;fn&#8221; being the empty string, the tangle does not transpire.</span>
<span style="color: #35a69c; font-style: italic;">   As such, it is as if we are not actually generating the source block.</span>

<span style="color: #35a69c; font-style: italic;">   This operation only returns the empty string and does not alter any existing state.</span>
<span style="color: #35a69c; font-style: italic;">   If we alter state, then earlier invocations to (currently-working-with) are rendered</span>
<span style="color: #35a69c; font-style: italic;">   useless!</span>
<span style="color: #35a69c; font-style: italic;">  "</span>

  <span style="color: #2aa198;">""</span>   <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Our return value.</span>
)
</pre>
</div>
 </details>

<p>
Notice that the <code>InteractiveWayToC.el</code> methods &#x2013;&lt;F6&gt; to &lt;F9&gt;&#x2013; target the
source block(s) with (<code>currently-working-with name)</code> where <code>name</code> is the most
recent name. Note that the <code>name</code> component need not be unique: Blocks having
the same one write to the same file.
</p>

<p>
In a new line enter <code>&lt;s</code> then at the end press <code>TAB</code> to obtain a nice hello-world
template. Some code will be generated for you &#x2013;free of charge&#x2013;, edit it as you like.
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">make-variable-buffer-local</span> '<span style="color: #268bd2;">org-structure-template-alist</span>)
(<span style="color: #859900; font-weight: bold;">setq</span> TEMPLATE
  (<span style="color: #b58900;">concat</span>
   <span style="color: #2aa198;">"#+NAME: ???? -- goal of this code block"</span>
   <span style="color: #2aa198;">"\n#+BEGIN_SRC "</span> Language <span style="color: #2aa198;">" :tangle (currently-working-with \"HelloWorld\") \n"</span>
   <span style="color: #2aa198;">"#include&lt;stdio.h&gt; // for IO"</span>
   <span style="color: #2aa198;">"\nint main() \n{\n  printf(\"--Hello, World!--\");\n  return 0; \n}"</span>
   <span style="color: #2aa198;">"\n#+END_SRC"</span>))
(<span style="color: #b58900;">add-to-list</span> '<span style="color: #268bd2;">org-structure-template-alist</span> `(<span style="color: #2aa198;">"s"</span> ,TEMPLATE))
</pre>
</div>
 </details>

<p>
Then to see the code generated by this file press <code>M-x</code> then enter <code>show-code</code> then enter;
alternatively, press <code>C-x C-e</code> after the final parenthesis: (show-code).
</p>

<p>
My definition of <code>(show-code)</code> begins with closing the code buffer if it exists,
then continue by extracting the most recent code and displaying it below the current buffer.
The definition of <code>(interpret)</code> is nearly the same except we switch to an output buffer,
and create it if it doesn't exist.
Note that our interpretation command is essentially the following command line invocation:
<code>NAME=myfilename ; gcc $NAME.c -o $NAME.exe ; ./$NAME.exe</code>
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">show-code</span> () <span style="color: #35a69c; font-style: italic;">"Show focused source blocks in a new buffer."</span>
  (<span style="color: #859900; font-weight: bold;">interactive</span>)
  (<span style="color: #859900; font-weight: bold;">ignore-errors</span> (<span style="color: #b58900;">kill-buffer</span> NAMECode))
  (<span style="color: #b58900;">save-buffer</span>)
  (<span style="color: #b58900;">org-babel-tangle</span>)
  (<span style="color: #b58900;">split-window-below</span>)
  (<span style="color: #b58900;">find-file</span> NAMECode)
  (<span style="color: #b58900;">other-window</span> 1))

<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Since there are many generated files, let's mention the name</span>
<span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">of the program file currently being executed, or proven.</span>
<span style="color: #96A7A9; font-style: italic;">;;</span>
(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">insert-focused-name</span> ()
   <span style="color: #35a69c; font-style: italic;">"Insert the name of the focused source blocks at the beginning of the buffer."</span>
  (<span style="color: #b58900;">beginning-of-buffer</span>)
  (<span style="color: #b58900;">insert</span> <span style="color: #2aa198;">"================\n&#10218; "</span> NAME <span style="color: #2aa198;">".c &#10219;\n================\n\n"</span>))

(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">interpret</span> () <span style="color: #35a69c; font-style: italic;">"Execute focused source blocks in a new buffer."</span>
  (<span style="color: #859900; font-weight: bold;">interactive</span>)
  (<span style="color: #b58900;">save-buffer</span>)
  (<span style="color: #b58900;">org-babel-tangle</span>)
  (<span style="color: #b58900;">switch-to-buffer-other-window</span> <span style="color: #2aa198;">"*Shell Command Output*"</span>)
  (<span style="color: #b58900;">shell-command</span>
    (<span style="color: #b58900;">concat</span> <span style="color: #2aa198;">"cd "</span> <span style="color: #268bd2;">default-directory</span> <span style="color: #2aa198;">"; NAME="</span> NAME <span style="color: #2aa198;">" ; gcc $NAME.c -o $NAME.exe ; ./$NAME.exe"</span>))
  (insert-focused-name)
  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Go back to the literate buffer.</span>
  (<span style="color: #b58900;">other-window</span> 1))

(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">frama-c</span> () <span style="color: #35a69c; font-style: italic;">""</span>
  (<span style="color: #859900; font-weight: bold;">interactive</span>)
  (<span style="color: #b58900;">save-buffer</span>)
  (<span style="color: #b58900;">org-babel-tangle</span>)
  (<span style="color: #b58900;">shell-command</span> (<span style="color: #b58900;">concat</span> <span style="color: #2aa198;">"frama-c-gui "</span> NAMECode <span style="color: #2aa198;">" -wp -rte &amp;"</span>)))

(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">try</span> (this that) <span style="color: #35a69c; font-style: italic;">""</span>
  (<span style="color: #859900; font-weight: bold;">condition-case</span> nil (<span style="color: #b58900;">eval</span> this) (<span style="color: #b58900;">error</span> (<span style="color: #b58900;">eval</span> that))))
</pre>
</div>
 </details>

<p>
The <code>frama-c-no-gui</code> command tries to find where an error happens by placing the cursor
near an unproven assertion. It does so by looking for the phrase <code>false</code> in the shell output
buffer after the <code>frama-c</code> program is invoked. If it cannot find it, you are placed at
the end of the buffer and, ideally but not necessarily, should see all goals have passed.
</p>

<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">frama-c-no-gui</span> () <span style="color: #35a69c; font-style: italic;">""</span>
  (<span style="color: #859900; font-weight: bold;">interactive</span>)
  (<span style="color: #b58900;">org-babel-tangle</span>)
  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Avoid generating proof goal statements --for now.</span>
  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">(shell-command (concat "frama-c " NAMECode " -wp -wp-msg-key=print-generated -rte"))</span>
  (<span style="color: #b58900;">shell-command</span> (<span style="color: #b58900;">concat</span> <span style="color: #2aa198;">"frama-c "</span> NAMECode <span style="color: #2aa198;">" -wp -wp-timeout 10 -rte"</span>))

  (<span style="color: #b58900;">switch-to-buffer-other-window</span> <span style="color: #2aa198;">"*Shell Command Output*"</span>)
  (insert-focused-name)
  (<span style="color: #b58900;">hl-line-mode</span>)

  (<span style="color: #859900; font-weight: bold;">setq</span> frama-c-state (<span style="color: #859900; font-weight: bold;">catch</span> '<span style="color: #6c71c4; font-weight: bold;">state</span>   <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Global variable indicating current state</span>
  (<span style="color: #859900; font-weight: bold;">dolist</span> (elem '(<span style="color: #2aa198;">"Exercise"</span> <span style="color: #2aa198;">"unknown"</span> <span style="color: #2aa198;">"user error"</span> <span style="color: #2aa198;">"false"</span> <span style="color: #2aa198;">"Timeout"</span> <span style="color: #2aa198;">"Proved goals"</span>))
    (<span style="color: #b58900;">beginning-of-buffer</span>)
    (try '(<span style="color: #859900; font-weight: bold;">and</span> (<span style="color: #b58900;">search-forward</span> elem) (<span style="color: #859900; font-weight: bold;">throw</span> '<span style="color: #6c71c4; font-weight: bold;">state</span> elem)) 'nil)
  )))

  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">global variable about status</span>
  (<span style="color: #859900; font-weight: bold;">setq</span> frama-c-status (<span style="color: #b58900;">format</span> <span style="color: #2aa198;">"Frama-C: %d&#65130; of proof complete!"</span> (frama-c-progress)))

  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Use the red colour of failure.</span>
  (<span style="color: #b58900;">set-face-background</span> '<span style="color: #556b72; background-color: #FDF6E3;">hl-line</span> <span style="color: #2aa198;">"pink"</span>)

  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Or did we succeed?</span>
  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">We might have halted a &#8220;false&#8221; *constant* even though all goals pass.</span>
  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">&#10216; This is not an issue when proofs are not being generated. &#10217;</span>
  (<span style="color: #859900; font-weight: bold;">if</span> (<span style="color: #b58900;">equal</span> frama-c-state <span style="color: #2aa198;">"Exercise"</span>)
        (<span style="color: #859900; font-weight: bold;">setq</span> frama-c-status (<span style="color: #b58900;">concat</span> frama-c-status <span style="color: #2aa198;">" &#10218;There are holes!&#10219;"</span>))
        (<span style="color: #859900; font-weight: bold;">when</span> (<span style="color: #859900; font-weight: bold;">or</span> (<span style="color: #b58900;">equal</span> frama-c-state <span style="color: #2aa198;">"Proved goals"</span>)
                  (<span style="color: #b58900;">eq</span> (frama-c-progress) 100))
              (<span style="color: #b58900;">set-face-background</span> '<span style="color: #556b72; background-color: #FDF6E3;">hl-line</span> <span style="color: #2aa198;">"pale green"</span>)
              (try '(<span style="color: #b58900;">search-forward</span> <span style="color: #2aa198;">"Proved goals"</span>) <span style="color: #268bd2;">t</span>)))
  (<span style="color: #b58900;">message</span> frama-c-status)
  (<span style="color: #b58900;">minimize-window</span>) <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">Make current buffer roughly 3 lines in height.</span>
)
</pre>
</div>
 </details>

<p>
Where the <code>frama-c-progress</code> command yields the percentage denoting the number of goals proven.
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #859900; font-weight: bold;">defun</span> <span style="color: #b58900;">frama-c-progress</span> () <span style="color: #35a69c; font-style: italic;">""</span>
  (<span style="color: #859900; font-weight: bold;">let</span> ( (here (<span style="color: #b58900;">point</span>)) )
  (<span style="color: #b58900;">beginning-of-buffer</span>)
  (try '(<span style="color: #b58900;">search-forward</span> <span style="color: #2aa198;">"Proved goals:"</span>) 0)
  <span style="color: #96A7A9; font-style: italic;">;; </span><span style="color: #96A7A9; font-style: italic;">(kill-line)</span>
    (<span style="color: #b58900;">copy-region-as-kill</span> (<span style="color: #b58900;">point</span>) (<span style="color: #b58900;">point-at-eol</span>))
    (<span style="color: #b58900;">goto-char</span> here)
    (<span style="color: #b58900;">*</span> 100 (<span style="color: #b58900;">string-to-number</span> (<span style="color: #b58900;">calc-eval</span> (<span style="color: #b58900;">current-kill</span> 0))))
  ))
</pre>
</div>
 </details>

<p>
Finally,
</p>
<details class="code-details"> <summary> <strong> <font face="Courier" size="3" color="green"> Details </font> </strong> </summary> <div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #b58900;">local-set-key</span> (<span style="color: #b58900;">kbd</span> <span style="color: #2aa198;">"&lt;f6&gt;"</span>) 'interpret)
(<span style="color: #b58900;">local-set-key</span> (<span style="color: #b58900;">kbd</span> <span style="color: #2aa198;">"&lt;f7&gt;"</span>) 'show-code)
(<span style="color: #b58900;">local-set-key</span> (<span style="color: #b58900;">kbd</span> <span style="color: #2aa198;">"&lt;f8&gt;"</span>) 'frama-c)
(<span style="color: #b58900;">local-set-key</span> (<span style="color: #b58900;">kbd</span> <span style="color: #2aa198;">"&lt;f9&gt;"</span>) 'frama-c-no-gui)
</pre>
</div>
 </details>

<p>
It is to be noted: I only know enough Elisp to get by.
</p>

<p>
Again: Hope you had fun!
</p>
</div>
</div>
<div class="taglist"><a href="https://alhassy.github.io/tags.html">Tags</a>: <a href="https://alhassy.github.io/tag-program-proving.html">program-proving</a> <a href="https://alhassy.github.io/tag-c.html">c</a> <a href="https://alhassy.github.io/tag-emacs.html">emacs</a> <a href="https://alhassy.github.io/tag-frama-c.html">frama-c</a> </div><center><strong> Generated by Emacs and Org-mode (•̀ᴗ•́)و </strong></center><center><a href="InteractiveWayToC.org.html"><img
   src="https://img.shields.io/badge/-Source-informational?logo=read-the-docs"></a>&emsp;<a href="https://github.com/alhassy/alhassy.github.io/commits/master/posts/InteractiveWayToC.org"><img
   src="https://img.shields.io/badge/-History-informational?logo=github"></a><br><a href="https://www.buymeacoffee.com/alhassy"><img src="https://img.shields.io/badge/-buy_me_a%C2%A0coffee-gray?logo=buy-me-a-coffee"></a></center>]]></description>
  <category><![CDATA[program-proving]]></category>
  <category><![CDATA[c]]></category>
  <category><![CDATA[emacs]]></category>
  <category><![CDATA[frama-c]]></category>
  <link>https://alhassy.github.io/InteractiveWayToC.html</link>
  <guid>https://alhassy.github.io/InteractiveWayToC.html</guid>
  <pubDate>Sat, 12 Jan 2019 19:29:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[Graphs are to categories as lists are to monoids]]></title>
  <description><![CDATA[</div><div style="text-align: center;">24 Dec 2018</div><center> <img src="images/PathCat.png" alt="Article image"
            style="border: 2px solid black;" width="300" height="300" align="top" /> </center><br><center><strong>Abstract</strong></center><nav id="table-of-contents">
<h2><a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a></h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#Introduction">1. Introduction</a>
<ul>
<li><a href="#Imports">Imports</a></li>
</ul>
</li>
<li><a href="#Graph-me-to-the-moon">2. <i>Graph me to the moon!</i></a>
<ul>
<li><a href="#Types-Require-Casting">2.1. Types Require Casting</a></li>
<li><a href="#Signatures">2.2. Signatures</a></li>
</ul>
</li>
<li><a href="#Cats-but-no-alligators">3. <i>Cats but no alligators</i></a>
<ul>
<li><a href="#Strict-Categories">3.1. Strict Categories</a></li>
<li><a href="#Familiar-𝒮ℯ𝓉-tings">3.2. Familiar <code>𝒮ℯ𝓉</code>-tings</a>
<ul>
<li><a href="#𝒮ℯ𝓉's">3.2.1. 𝒮ℯ𝓉's</a></li>
<li><a href="#Sets-are-trivial-categories">3.2.2. Sets are trivial categories</a></li>
<li><a href="#Categories-are-typed-monoids">3.2.3. Categories are typed monoids</a></li>
<li><a href="#Categories-are-coherently-preordered-sets">3.2.4. Categories are coherently preordered sets</a></li>
<li><a href="#Groupoids">3.2.5. Groupoids</a></li>
<li><a href="#Rule-of-Thumb">3.2.6. Rule of Thumb</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#Endowing-Structure-with-Functors">4. <i>Endowing Structure with Functors</i></a>
<ul>
<li><a href="#Functor-Examples">4.1. Examples</a></li>
</ul>
</li>
<li><a href="#The-four-postulates-of-the-apocalypse">5. <i>The four postulates of the apocalypse</i></a>
<ul>
<li><a href="#A-very-big-𝒞𝒶𝓉">5.1. A very big <code>𝒞𝒶𝓉</code></a></li>
<li><a href="#𝒢𝓇𝒶𝓅𝒽">5.2. <code>𝒢𝓇𝒶𝓅𝒽</code></a></li>
<li><a href="#𝒞𝒶𝓉-'s-are-𝒢𝓇𝒶𝓅𝒽-'s">5.3. <code>𝒞𝒶𝓉</code>'s are <code>𝒢𝓇𝒶𝓅𝒽</code>'s</a></li>
</ul>
</li>
<li><a href="#How-natural-is-naturality">6. <i>How natural is naturality?</i></a>
<ul>
<li><a href="#Identification-of-possible-paths-contraction-of-choices">6.1. Identification of possible paths &#x2014;contraction of choices</a></li>
<li><a href="#No-Choice-free-will-is-only-an-illusion">6.2. No Choice &#x2013;free will is only an illusion</a></li>
<li><a href="#Natural-means-polymorphic-without-type-inspection">6.3. Natural means polymorphic without type inspection</a></li>
<li><a href="#Natural-means-no-reference-to-types">6.4. Natural means no reference to types</a></li>
<li><a href="#Natural-means-uniformly-and-simultaneously-defined">6.5. Natural means uniformly and simultaneously defined</a></li>
<li><a href="#Naturality-is-restructure-modify-commutativity">6.6. Naturality is restructure-modify commutativity</a></li>
<li><a href="#Natural-means-obvious">6.7. Natural means obvious</a></li>
<li><a href="#Naturality-is-promotion">6.8. Naturality is promotion</a></li>
<li><a href="#Naturality-as-a-rewrite-rule">6.9. Naturality as a rewrite rule</a></li>
<li><a href="#Naturality-is-just-model-morphism">6.10. Naturality is just model morphism</a></li>
<li><a href="#Naturality-yields-pattern-matching">6.11. Naturality yields pattern matching</a></li>
<li><a href="#Naturality-Examples">6.12. Examples</a></li>
</ul>
</li>
<li><a href="#Functor-Categories">7. <i>Functor Categories</i></a>
<ul>
<li><a href="#FunctorCats-Examples">7.1. Examples</a>
<ul>
<li><a href="#All-Categories-are-Functor-Categories">7.1.1. All Categories are Functor Categories</a></li>
<li><a href="#Powers-of-Categories-are-Functor-Categories">7.1.2. Powers of Categories are Functor Categories</a></li>
<li><a href="#Arrow-Categories">7.1.3. Arrow Categories</a></li>
<li><a href="#Understand-𝒞-by-looking-at-Functor-Categories">7.1.4. Understand 𝒞 by looking at Functor Categories</a></li>
<li><a href="#Presheaves-delegating-work-to-𝒮ℯ𝓉">7.1.5. Presheaves &#x2013; delegating work to 𝒮ℯ𝓉</a></li>
<li><a href="#Slice-Categories">7.1.6. Slice Categories</a></li>
<li><a href="#Slices-of-𝒮e𝓉-are-Functor-Categories">7.1.7. Slices of <code>𝒮e𝓉</code> are Functor Categories</a></li>
<li><a href="#Natural-transformations-as-functor-categories">7.1.8. Natural transformations as functor categories</a></li>
</ul>
</li>
<li><a href="#Graphs-as-functors">7.2. Graphs as functors</a></li>
</ul>
</li>
<li><a href="#A-few-categorical-constructions">8. A few categorical constructions</a>
<ul>
<li><a href="#Opposite">8.1. Opposite</a>
<ul>
<li><a href="#ah-yeah-and-dagger-categories">8.1.1. ah-yeah: ∂ and dagger categories</a></li>
</ul>
</li>
<li><a href="#Products">8.2. Products</a>
<ul>
<li><a href="#Currying">8.2.1. Currying</a></li>
</ul>
</li>
<li><a href="#Pointwise-extensions-and-the-hom-functor">8.3. Pointwise extensions and the hom functor</a></li>
</ul>
</li>
<li><a href="#𝒮implicity-𝒰nderlies-𝒞omplexity">9. 𝒮implicity 𝒰nderlies 𝒞omplexity</a>
<ul>
<li><a href="#Being-forgetful-from-injections-to-faithful-functors">9.1. Being forgetful: from injections to faithful functors</a></li>
<li><a href="#Of-basis-vectors">9.2. Of basis vectors</a></li>
<li><a href="#Of-adjunctions">9.3. Of adjunctions</a></li>
<li><a href="#Adjunctions-and-Representable-Functors">9.4. Adjunctions and Representable Functors</a></li>
<li><a href="#Concluding-remarks">9.5. Concluding remarks</a></li>
</ul>
</li>
<li><a href="#Designing-Paths">10. Designing Paths</a>
<ul>
<li><a href="#Aside-An-Adjunction-between-𝒮ℯ𝓉-and-𝒞𝒶𝓉">10.1. Aside: An Adjunction between 𝒮ℯ𝓉 and 𝒞𝒶𝓉</a></li>
<li><a href="#Equality-Combinators-for-Paths">10.2. Equality Combinators for Paths</a></li>
<li><a href="#Category-of-paths-over-a-graph">10.3. Category of paths over a graph</a></li>
<li><a href="#The-𝒫ath-to-freedom">10.4. The 𝒫ath to freedom</a></li>
</ul>
</li>
<li><a href="#Free-at-last">11. Free at last</a>
<ul>
<li><a href="#Defining-the-needed-operations">11.1. Defining the needed operations</a></li>
<li><a href="#Realising-the-proof-obligations">11.2. Realising the proof-obligations</a></li>
<li><a href="#Another-freedom-proof">11.3. Another freedom proof</a></li>
<li><a href="#𝒫-𝒰">11.4. <code>𝒫 ⊣ 𝒰</code></a></li>
</ul>
</li>
<li><a href="#Folds-Over-Paths">12. Folds Over Paths</a>
<ul>
<li><a href="#Lists-are-special-kinds-of-paths">12.1. Lists are special kinds of paths</a></li>
</ul>
</li>
<li><a href="#That-was-fun-Bye">13. That was fun; Bye!</a></li>
</ul>
</div>
</nav>

<div id="myMathJaxStuff" style="display: none;">
<p>
We do not want the MathJax declarations to use vertical whitespace;
so we do not display their residual whitespace.
</p>

<p>
Moreover the form of MathJax declarations differs from usual LaTeX
declarations, so we iffalse&#x2026;fi these ones.
</p>

<p>
Commenting the MathJax using &lt;!&#x2013; HTML comments &#x2013;&gt;
makes the commands inaccessible elsewhere.
</p>

<p>
The alternative is to declare a <a href="https://stackoverflow.com/questions/1992114/how-do-you-create-a-hidden-div-that-doesnt-create-a-line-break-or-horizontal-sp">non-displayed ~div</a>~.
</p>

<p>
\[\newcommand{\step}[1]{ \\ = \;\; & \qquad \color{maroon}{⟨ \text{ #1 } ⟩} \\ & }\]
\[\newcommand{\stepWith}[2]{ \\ #1 \;\; & \qquad \color{maroon}{⟨ \text{ #2 } ⟩} \\ & }\]
\[\newenvironment{calc}{\begin{align*} & }{\end{align*}}\]
</p>

<p>
Having identical label references for different equations will break rendering!
</p>

<p>
eqn            := display name, then display formula.
eqnColour      := display name, then display formula, then colour
eqnLabel       := display name, then display formula, then label
eqnLabelColour := display name, then display formula, then label, then colour
eqnLabelColour := display name, then display formula, then colour, then label &#x2013; safe redundancy!
</p>

<p>
\[\newcommand{\eqnLabelColour}[4]{ \begin{equation} \color{#4}{#2} \label{#3}\tag{$\color{#4}{\text{#1}}$} \end{equation} }\]
</p>

<p>
\[\newcommand{\eqnColourLabel}[4]{ \eqnLabelColour{#1}{#2}{#4}{#3} }\]
</p>

<p>
Default equation colour is: navy
\[\newcommand{\eqnLabel}[3]{ \eqnLabelColour{#1}{#2}{#3}{navy} }\]
</p>

<p>
Default label is the display name
\[\newcommand{\eqnColour}[3]{ \eqnLabelColour{#1}{#2}{#1}{#3} }\]
</p>

<p>
\[\newcommand{\eqn}[2]{ \eqnLabel{#1}{#2}{#1} }\]
</p>

<p>
Notice that \ref{Label} and \ref{Label2} have the same displayed name,
but <b>cannot</b> have the same label!
</p>

<p>
\[\newcommand{\givens}[1]{ \color{teal}{#1} }\]
\[\newcommand{\requireds}[1]{ \color{navy}{#1} }\]
</p>

<p>
\[\def\lands{\;\land\;}\]
\[\def\landS{\quad\land\quad}\]
</p>

<p>
\[\def\impliess{\;\Rightarrow\;}\]
\[\def\impliesS{\quad\Rightarrow\quad}\]
</p>

<p>
\[\def\equivs{\;\equiv\;}\]
\[\def\equivS{\quad\equiv\quad}\]
</p>

<p>
\[\def\eqs{\;=\;}\]
\[\def\eqS{\quad=\quad}\]
</p>

<p>
\[\def\sqleqs{\;\sqsubseteq\;}\]
\[\def\sqleqS{\quad\sqsubseteq\quad}\]
</p>

<p>
\[\def\foldr{\mathsf{foldr}}\]
\[\def\edge{\mathsf{edge}}\]
\[\def\Func{\mathsf{Func}}\]
\[\def\Id{\mathsf{Id}}\]
\[\def\src{\mathsf{src}}\]
\[\def\tgt{\mathsf{tgt}}\]
\[\def\obj{\mathsf{obj}}\]
\[\def\mor{\mathsf{mor}}\]
\[\def\natTo{\overset{.}{→}}\]
\[\def\Obj{\mathsf{Obj}\,}\]
\[\def\List{\mathsf{List}\,}\]
</p>
<p>
After this, only pure latex commands should exist.
</p>

<p>
However, the whitespace they produce via MathJax is ignored
since the HTML div is not yet closed.
</p>


</div>

<style> pre.src-haskell:before { content: 'Agda' !important; }</style>

<nav id="table-of-contents">
<h2><a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a></h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#Introduction">1. Introduction</a>
<ul>
<li><a href="#Imports">Imports</a></li>
</ul>
</li>
<li><a href="#Graph-me-to-the-moon">2. <i>Graph me to the moon!</i></a>
<ul>
<li><a href="#Types-Require-Casting">2.1. Types Require Casting</a></li>
<li><a href="#Signatures">2.2. Signatures</a></li>
</ul>
</li>
<li><a href="#Cats-but-no-alligators">3. <i>Cats but no alligators</i></a>
<ul>
<li><a href="#Strict-Categories">3.1. Strict Categories</a></li>
<li><a href="#Familiar-𝒮ℯ𝓉-tings">3.2. Familiar <code>𝒮ℯ𝓉</code>-tings</a></li>
</ul>
</li>
<li><a href="#Endowing-Structure-with-Functors">4. <i>Endowing Structure with Functors</i></a>
<ul>
<li><a href="#Functor-Examples">4.1. Examples</a></li>
</ul>
</li>
<li><a href="#The-four-postulates-of-the-apocalypse">5. <i>The four postulates of the apocalypse</i></a>
<ul>
<li><a href="#A-very-big-𝒞𝒶𝓉">5.1. A very big <code>𝒞𝒶𝓉</code></a></li>
<li><a href="#𝒢𝓇𝒶𝓅𝒽">5.2. <code>𝒢𝓇𝒶𝓅𝒽</code></a></li>
<li><a href="#𝒞𝒶𝓉-'s-are-𝒢𝓇𝒶𝓅𝒽-'s">5.3. <code>𝒞𝒶𝓉</code>'s are <code>𝒢𝓇𝒶𝓅𝒽</code>'s</a></li>
</ul>
</li>
<li><a href="#How-natural-is-naturality">6. <i>How natural is naturality?</i></a>
<ul>
<li><a href="#Identification-of-possible-paths-contraction-of-choices">6.1. Identification of possible paths &#x2014;contraction of choices</a></li>
<li><a href="#No-Choice-free-will-is-only-an-illusion">6.2. No Choice &#x2013;free will is only an illusion</a></li>
<li><a href="#Natural-means-polymorphic-without-type-inspection">6.3. Natural means polymorphic without type inspection</a></li>
<li><a href="#Natural-means-no-reference-to-types">6.4. Natural means no reference to types</a></li>
<li><a href="#Natural-means-uniformly-and-simultaneously-defined">6.5. Natural means uniformly and simultaneously defined</a></li>
<li><a href="#Naturality-is-restructure-modify-commutativity">6.6. Naturality is restructure-modify commutativity</a></li>
<li><a href="#Natural-means-obvious">6.7. Natural means obvious</a></li>
<li><a href="#Naturality-is-promotion">6.8. Naturality is promotion</a></li>
<li><a href="#Naturality-as-a-rewrite-rule">6.9. Naturality as a rewrite rule</a></li>
<li><a href="#Naturality-is-just-model-morphism">6.10. Naturality is just model morphism</a></li>
<li><a href="#Naturality-yields-pattern-matching">6.11. Naturality yields pattern matching</a></li>
<li><a href="#Naturality-Examples">6.12. Examples</a></li>
</ul>
</li>
<li><a href="#Functor-Categories">7. <i>Functor Categories</i></a>
<ul>
<li><a href="#FunctorCats-Examples">7.1. Examples</a></li>
<li><a href="#Graphs-as-functors">7.2. Graphs as functors</a></li>
</ul>
</li>
<li><a href="#A-few-categorical-constructions">8. A few categorical constructions</a>
<ul>
<li><a href="#Opposite">8.1. Opposite</a></li>
<li><a href="#Products">8.2. Products</a></li>
<li><a href="#Pointwise-extensions-and-the-hom-functor">8.3. Pointwise extensions and the hom functor</a></li>
</ul>
</li>
<li><a href="#𝒮implicity-𝒰nderlies-𝒞omplexity">9. 𝒮implicity 𝒰nderlies 𝒞omplexity</a>
<ul>
<li><a href="#Being-forgetful-from-injections-to-faithful-functors">9.1. Being forgetful: from injections to faithful functors</a></li>
<li><a href="#Of-basis-vectors">9.2. Of basis vectors</a></li>
<li><a href="#Of-adjunctions">9.3. Of adjunctions</a></li>
<li><a href="#Adjunctions-and-Representable-Functors">9.4. Adjunctions and Representable Functors</a></li>
<li><a href="#Concluding-remarks">9.5. Concluding remarks</a></li>
</ul>
</li>
<li><a href="#Designing-Paths">10. Designing Paths</a>
<ul>
<li><a href="#Aside-An-Adjunction-between-𝒮ℯ𝓉-and-𝒞𝒶𝓉">10.1. Aside: An Adjunction between 𝒮ℯ𝓉 and 𝒞𝒶𝓉</a></li>
<li><a href="#Equality-Combinators-for-Paths">10.2. Equality Combinators for Paths</a></li>
<li><a href="#Category-of-paths-over-a-graph">10.3. Category of paths over a graph</a></li>
<li><a href="#The-𝒫ath-to-freedom">10.4. The 𝒫ath to freedom</a></li>
</ul>
</li>
<li><a href="#Free-at-last">11. Free at last</a>
<ul>
<li><a href="#Defining-the-needed-operations">11.1. Defining the needed operations</a></li>
<li><a href="#Realising-the-proof-obligations">11.2. Realising the proof-obligations</a></li>
<li><a href="#Another-freedom-proof">11.3. Another freedom proof</a></li>
<li><a href="#𝒫-𝒰">11.4. <code>𝒫 ⊣ 𝒰</code></a></li>
</ul>
</li>
<li><a href="#Folds-Over-Paths">12. Folds Over Paths</a>
<ul>
<li><a href="#Lists-are-special-kinds-of-paths">12.1. Lists are special kinds of paths</a></li>
</ul>
</li>
<li><a href="#That-was-fun-Bye">13. That was fun; Bye!</a></li>
</ul>
</div>
</nav>

<p>
Numbers are the lengths of lists which are the flattenings of trees which are
the spannings of graphs.
Unlike the first three, graphs have <i>two</i> underlying types of interest
&#x2013;the vertices and the edges&#x2013; and it is getting to grips with this complexity
that we attempt to tackle by considering their ‘algebraic’ counterpart: Categories.
</p>

<p>
In our exploration of what graphs could possibly be and their relationships to lists are,
we shall <i>mechanise,</i> or <i>implement,</i> our claims since there will be many details and it is easy
to make mistakes &#x2013;moreover as a self-learning project, I'd feel more confident to make
<b>bold</b> claims when I have a proof assistant checking my work ;-)
</p>

<p>
Assuming slight familiarity with the Agda programming language, we motivate the need for
basic concepts of category theory with the aim of discussing adjunctions with
a running example of a detailed construction and proof of a free functor.
Moreover, the article contains a host of <code>exercises</code> whose solutions can be found in the
literate source file. Raw Agda code can be found <a href="https://github.com/alhassy/AgdaCheatSheet/blob/master/PathCat.agda">here</a>.
</p>

<p>
Since the read time for this article is more than two hours, excluding the interspersed
exercises, it may help to occasionally consult a some reference sheets:
</p>
<div class="org-center">
<p>
<a href="https://alhassy.github.io/CatsCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Category_Theory-CheatSheet-success?logo=gimp"></a>
<a href="https://alhassy.github.io/AgdaCheatSheet/CheatSheet.pdf"><img src="https://img.shields.io/badge/Agda-CheatSheet-success?logo=haskell"></a>
</p>
</div>

<p>
Coming from a background in order theory, I love Galois Connections and so
our categorical hammer will not be terminal objects nor limits, but rather adjunctions.
As such, <i>everything is an adjunction</i> is an apt slogan for us :-)
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">This file has been extracted from https://alhassy.github.io/PathCat/</span>
<span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Type checks with Agda version 2.6.0.</span>
</pre>
</div>
<small> <center>
<p>
( Photo by
<a href="https://unsplash.com/@miklevasilyev">Mikhail Vasilyev</a>
on <a href="https://unsplash.com/">Unsplash</a> )
</p>
</center> </small>

<div id="outline-container-org5acd3cc" class="outline-2">
<h2 id="Introduction"><span class="section-number-2">1</span> Introduction</h2>
<div class="outline-text-2" id="text-Introduction">
<p>
Lists give free monoids \(ℒ\, A = (\List\, A, +\!+, [])\)
&#x2014;a monoid \(𝒮 = (S, ⊕, 0_⊕)\) is a triple consisting of a set \(S\) with a binary operation
\(⊕\) on it that is associative and has a unit, \(0_⊕\).
That it is ‘free’ means that to define a structure-preserving map between monoids
\((\List\, A, +\!+, []) \,⟶\, (S, ⊕, 0_⊕)\) it suffices to only provide a map between their
carriers \(\List\, A → S\) &#x2014;freedom means that plain old maps between types freely,
at no cost or effort, give rise to maps that preserve monoid structure.
Moreover, the converse also holds and in-fact we have a bijection:
\[
  (ℒ\, A ⟶ 𝒮) \qquad≅\qquad (A ⟶ 𝒰\, 𝒮)
\]
Where we write \(𝒰\, (S, ⊕, 0_⊕) = S\) for the operation that gives us the 𝒰nderlying carrier
of a monoid.
</p>

<p>
Loosely put, one says we have an ‘adjunction’, written \(ℒ ⊣ 𝒰\).
</p>

<p>
Observe that natural numbers <code>ℕ ≅ List Unit</code> are a monoid whose operation is commutative.
By using different kinds of elements <code>A</code> &#x2013;and, importantly, still not imposing any equations&#x2013;
we lose commutativity with <code>List A</code>.
Then by generalising further to binary trees <code>BinTree A</code>, we lose associtivity and identity
are are only left with a set and an operation on it &#x2014;a structure called a ‘magma’.
</p>

<p>
This is the order that one usually learns about these inductively built structures.
One might be curious as to what the next step up is in this hierarchy of generalisations.
It is a non-inductive type called a ‘graph’ and in this note we investigate them by
comparison to lists.
Just as we shifted structures in the hierarchy, we will
move to a setting called a ‘category’ &#x2014;such are more structured than magmas
but less restrictive than monoids.
</p>

<p>
For those who know category theory, this article essentially formalises the
often seen phrase “consider the category generated by this diagram, or graph”.
Indeed every category is essentially a free category over a graph but with
additional equations that ‘confuse’ two paths thereby declaring, e.g., that
one edge is the composition of two other edges.
</p>
</div>

<div id="outline-container-org99fbe6a" class="outline-3">
<h3 id="Imports">Imports</h3>
<div class="outline-text-3" id="text-Imports">
<p>
In our exploration of what graphs could possibly be and their relationships to lists are,
we shall <i>mechanise</i> or <i>implement</i> our claims since there will be many details and it is easy
to make mistakes &#x2013;moreover as a self-learning project, I'd feel more confident to make
<b>bold</b> claims when I have a proof assistant checking my work ;-)
</p>

<p>
Before reading any further please ingrain into your mind that the Agda keyword
<code>Set</code> is read “type”! This disparity is a historical accident.
</p>

<p>
Since the Agda prelude is so simple, the core language doesn’t even come with Booleans or numbers by default
&#x2014;they must be imported from the standard library. This is a pleasant feature.
As a result, Agda code tends to begin with a host of imports.
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">PathCat</span> <span style="color: #859900; font-weight: bold;">where</span>

<span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Level</span> using (<span style="color: #b58900; font-style: italic;">Level</span>) renaming (zero to &#8467;&#8320; ; suc to &#8467;suc ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8852;</span><span style="color: #859900; font-weight: bold;">_</span> to <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8845;</span><span style="color: #859900; font-weight: bold;">_</span>)

<span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Numbers</span>
<span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Data.Fin</span>
  using (<span style="color: #b58900; font-style: italic;">Fin</span> ; to&#8469; ; from&#8469; ; from&#8469;<span style="color: #268bd2;">&#8804;</span> ; reduce<span style="color: #268bd2;">&#8805;</span> ; inject<span style="color: #268bd2;">&#8804;</span>)
  renaming (<span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&lt;</span><span style="color: #859900; font-weight: bold;">_</span> to _f<span style="color: #268bd2;">&lt;</span><span style="color: #859900; font-weight: bold;">_</span> ; zero to fzero ; suc to fsuc)
<span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Data.Nat</span>
<span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Relation.Binary</span> using (<span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">DecTotalOrder</span>)
<span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Data.Nat.Properties</span> using(<span style="color: #268bd2;">&#8804;-</span>decTotalOrder ; <span style="color: #268bd2;">&#8804;-</span>refl)
<span style="color: #b58900;">open</span> <span style="color: #b58900; font-style: italic;">DecTotalOrder</span> <span style="color: #268bd2;">Data.Nat.Properties.&#8804;-</span>decTotalOrder

<span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Z-notation for sums</span>
<span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Data.Product</span> using (<span style="color: #b58900; font-style: italic;">&#931;</span> ; proj&#8321; ; proj&#8322; ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#215;</span><span style="color: #859900; font-weight: bold;">_</span> ; <span style="color: #859900; font-weight: bold;">_</span>,<span style="color: #859900; font-weight: bold;">_</span>)
<span style="color: #b58900; font-style: italic;">&#931;</span><span style="color: #b58900;">&#8758;&#8226;</span> <span style="color: #b58900; font-style: italic;">:</span> {a b <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Level</span>} (<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> a) (<span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> b) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> (a <span style="color: #268bd2;">&#8845;</span> b)
<span style="color: #b58900; font-style: italic;">&#931;</span><span style="color: #b58900;">&#8758;&#8226;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#931;</span>
<span style="color: #859900; font-weight: bold;">infix</span> <span style="color: #b58900;">-</span>666 <span style="color: #b58900; font-style: italic;">&#931;</span><span style="color: #268bd2;">&#8758;&#8226;</span>
<span style="color: #b58900;">syntax</span> <span style="color: #b58900; font-style: italic;">&#931;</span><span style="color: #268bd2;">&#8758;&#8226;</span> <span style="color: #b58900; font-style: italic;">A</span> (&#955; x <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span>) <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#931;</span> x <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8226;</span> <span style="color: #b58900; font-style: italic;">B</span>

<span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Equalities</span>
<span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Relation.Binary.PropositionalEquality</span> using (<span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8791;</span><span style="color: #859900; font-weight: bold;">_</span> ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8801;</span><span style="color: #859900; font-weight: bold;">_</span>)
  renaming (sym to <span style="color: #268bd2;">&#8801;-</span>sym ; refl to <span style="color: #268bd2;">&#8801;-</span>refl ; trans to <span style="color: #859900; font-weight: bold;">_</span>&#10216;<span style="color: #268bd2;">&#8801;&#8801;</span>&#10217;<span style="color: #859900; font-weight: bold;">_</span>
           ; cong to <span style="color: #268bd2;">&#8801;-</span>cong ; cong&#8322; to <span style="color: #268bd2;">&#8801;-</span>cong&#8322;
           ; subst to <span style="color: #268bd2;">&#8801;-</span>subst ; subst&#8322; to <span style="color: #268bd2;">&#8801;-</span>subst&#8322; ; setoid to <span style="color: #268bd2;">&#8801;-</span>setoid)
</pre>
</div>

<p>
Notice that we renamed transitivity to be an infix combinator.
</p>

<p>
Let us make equational-style proofs available for any type.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> <span style="color: #859900; font-weight: bold;">_</span> {i} {<span style="color: #b58900; font-style: italic;">S</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> i} <span style="color: #859900; font-weight: bold;">where</span>
    open <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Relation.Binary.EqReasoning</span> (<span style="color: #268bd2;">&#8801;-</span>setoid <span style="color: #b58900; font-style: italic;">S</span>) public
</pre>
</div>

<p>
We intend our proofs to be sequences of formulae interleaved with
justifications for how the formulae are related. At times, the justifications
are by definition and so may be omitted, but we may want to mention them
for presentational &#x2013;pedagogical?&#x2013; purposes. Hence, we introduce the
combinator notation <code>lhs ≡⟨" by definition of something "⟩′ rhs</code>.
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Agda.Builtin.String</span>

defn<span style="color: #b58900;">-</span>chasing <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i} {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> i} (x <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">String</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">A</span>
defn<span style="color: #b58900;">-</span>chasing x reason supposedly<span style="color: #268bd2;">-</span>x<span style="color: #268bd2;">-</span>again <span style="color: #268bd2;">=</span> supposedly<span style="color: #268bd2;">-</span>x<span style="color: #268bd2;">-</span>again

<span style="color: #b58900;">syntax</span> defn<span style="color: #268bd2;">-</span>chasing x reason xish <span style="color: #268bd2;">=</span> x <span style="color: #268bd2;">&#8801;</span>&#10216; reason &#10217;<span style="color: #268bd2;">&#8242;</span> xish

<span style="color: #859900; font-weight: bold;">infixl</span> 3 defn<span style="color: #268bd2;">-</span>chasing
</pre>
</div>

<p>
While we’re making synonyms for readability, let’s make another:
</p>
<div class="org-src-container">
<pre class="src src-haskell">_even<span style="color: #b58900;">-</span>under_ <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {a b} {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> a} {<span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> b} {x y} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#8801;</span> y <span style="color: #268bd2;">&#8594;</span> (f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span>) <span style="color: #268bd2;">&#8594;</span> f x <span style="color: #268bd2;">&#8801;</span> f y
_even<span style="color: #b58900;">-</span>under_ <span style="color: #268bd2;">=</span> &#955; eq f <span style="color: #268bd2;">&#8594;</span> <span style="color: #268bd2;">&#8801;-</span>cong f eq
</pre>
</div>

<p>
Example usage would be to prove
<code>mor G (mor F Id) ≡ mor G Id</code> directly by <code>≡-cong (mor G) (id F)</code>
which can be written more clearly as
<code>functor F preserves-identities even-under (mor G)</code>,
while longer it is also much clearer and easier to read and understand
&#x2013;self-documenting in some sense.
</p>

<p>
Interestingly, our first calculational proof is in section 5 when
we introduced an large 𝒞𝒶𝓉egory.
</p>
</div>
</div>
</div>

<div id="outline-container-orga187a83" class="outline-2">
<h2 id="Graph-me-to-the-moon"><span class="section-number-2">2</span> <i>Graph me to the moon!</i></h2>
<div class="outline-text-2" id="text-Graph-me-to-the-moon">
<div class="org-center">
<p>
<i>What's a graph? Let's motivate categories!</i>
</p>
</div>
<p>
A ‘graph’ is just a parallel-pair of maps,
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900;">record</span> <span style="color: #b58900; font-style: italic;">Graph</span>&#8320; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>&#8321; <span style="color: #859900; font-weight: bold;">where</span>
  field
    <span style="color: #b58900; font-style: italic;">V</span>   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>
    <span style="color: #b58900; font-style: italic;">E</span>   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>
    src <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">E</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span>
    tgt <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">E</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span>
</pre>
</div>
<p>
This of-course captures the usual notion of a set of nodes <code>V</code> and a set of directed and labelled
edges <code>E</code> where an edge <code>e</code> begins at <code>src e</code> and concludes at <code>tgt e</code>.
</p>

<p>
What is good about this definition is that it can be phrased in any category: <code>V</code> and <code>E</code> are
any two objects and <code>src, tgt</code> are a parallel pair of morphisms between them.
How wonderful! We can study the notion of graphs in arbitrary categories!
&#x2014;This idea will be made clearer when categories and functors are formally introduced.
</p>

<p>
However, the notion of structure-preserving map between graphs, or ‘graph-maps’ for short,
then becomes:
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900;">record</span> _&#119970;<span style="color: #268bd2;">&#10230;</span>&#8320;<span style="color: #859900; font-weight: bold;">_</span> (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>&#8320;) <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>&#8321; <span style="color: #859900; font-weight: bold;">where</span>
  open <span style="color: #b58900; font-style: italic;">Graph</span>&#8320;
  field
    vertex <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>(<span style="color: #b58900; font-style: italic;">G</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span>(<span style="color: #b58900; font-style: italic;">H</span>)
    edge   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">E</span>(<span style="color: #b58900; font-style: italic;">G</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">E</span>(<span style="color: #b58900; font-style: italic;">H</span>)
    src<span style="color: #268bd2;">-</span>preservation <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> e <span style="color: #268bd2;">&#8594;</span> src(<span style="color: #b58900; font-style: italic;">H</span>) (edge e) <span style="color: #268bd2;">&#8801;</span>  vertex (src(<span style="color: #b58900; font-style: italic;">G</span>) e)
    tgt<span style="color: #268bd2;">-</span>preservation <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> e <span style="color: #268bd2;">&#8594;</span> tgt(<span style="color: #b58900; font-style: italic;">H</span>) (edge e) <span style="color: #268bd2;">&#8801;</span>  vertex (tgt(<span style="color: #b58900; font-style: italic;">G</span>) e)
</pre>
</div>
<p>
( The fancy 𝒢 and ⟶ are obtained in Agda input mode by <code>\McG</code> and <code>\--&gt;</code>, respectively. )
</p>

<p>
This is a bit problematic in that we have two proof obligations and at a first glance it is not
at all clear their motivation besides ‘‘structure-preserving’’.
</p>

<p>
However, our aim is in graphs in usual type theory, and as such we can use a definition that is
equivalent in this domain: A graph is a
type <code>V</code> of vertices and a ‘type’ <code>v ⟶ v’</code> of edges for each pair of vertices <code>v</code> and <code>v’</code>.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8216;small graphs&#8217; , since we are not using levels</span>
<span style="color: #b58900;">record</span> <span style="color: #b58900; font-style: italic;">Graph</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>&#8321; <span style="color: #859900; font-weight: bold;">where</span>
  field
    <span style="color: #b58900; font-style: italic;">V</span>    <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>
    <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span>

<span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">i.e., Graph &#8776; &#931; V &#8758; Set &#8226; (V &#8594; V)</span>
<span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Graphs are a dependent type!</span>
</pre>
</div>

<p>
Now the notion of graph-map, and the meaning of structure-preserving, come to the forefront:
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900;">record</span> <span style="color: #b58900; font-style: italic;">GraphMap</span> (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>) <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>&#8321; <span style="color: #859900; font-weight: bold;">where</span>
    private
      open <span style="color: #b58900; font-style: italic;">Graph</span> using (<span style="color: #b58900; font-style: italic;">V</span>)
      <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span>g_ <span style="color: #268bd2;">=</span> Graph._<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">G</span>
      <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span>h_ <span style="color: #268bd2;">=</span> Graph._<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">H</span>
    field
      ver  <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>(<span style="color: #b58900; font-style: italic;">G</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span>(<span style="color: #b58900; font-style: italic;">H</span>)                                <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">vertex morphism</span>
      edge <span style="color: #b58900; font-style: italic;">:</span> {x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>(<span style="color: #b58900; font-style: italic;">G</span>)} <span style="color: #268bd2;">&#8594;</span> (x <span style="color: #268bd2;">&#10230;</span>g y) <span style="color: #268bd2;">&#8594;</span> (ver x <span style="color: #268bd2;">&#10230;</span>h ver y) <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">arrow preservation</span>

<span style="color: #b58900;">open</span> <span style="color: #b58900; font-style: italic;">GraphMap</span>
</pre>
</div>

<p>
Note <code>edge</code> essentially says that <code>mor</code> ‘shifts’, or ‘translates’, types
<code>x ⟶g y</code> into types <code>ver x ⟶h ver y</code>.
</p>

<p>
While equivalent, this two-piece definition is preferable over the four-piece one given
earlier since it means less proof-obligations and less constructions in general, but the same
expressiblity. Yay!
</p>

<p>
Before we move on, let us give an example of a simple chain-graph.
For clarity, we present it in both variations.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">embedding: j &lt; n &#8658; j &lt; suc n</span>
`<span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{n} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Fin</span> n <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Fin</span> (suc n)
` j <span style="color: #268bd2;">=</span> inject<span style="color: #268bd2;">&#8804;</span> j (<span style="color: #268bd2;">&#8804;-</span>step <span style="color: #268bd2;">&#8804;-</span>refl) <span style="color: #859900; font-weight: bold;">where</span> open <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Data.Nat.Properties</span> using (<span style="color: #268bd2;">&#8804;-</span>step)
</pre>
</div>
<p>
This' an example of a ‘forgetful functor’, keep reading!
</p>

<div class="org-src-container">
<pre class="src src-haskell">[<span style="color: #859900; font-weight: bold;">_</span>]&#8320; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#8469;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Graph</span>&#8320;
[ n ]&#8320; <span style="color: #268bd2;">=</span> record
    { <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Fin</span> (suc n)                  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8776; {0, 1, ..., n - 1, n}</span>
    ; <span style="color: #b58900; font-style: italic;">E</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Fin</span> n                        <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8776; {0, 1, ..., n - 1}</span>
    ; src <span style="color: #268bd2;">=</span> &#955; j <span style="color: #268bd2;">&#8594;</span> ` j
    ; tgt <span style="color: #268bd2;">=</span> &#955; j <span style="color: #268bd2;">&#8594;</span> fsuc j
    }
</pre>
</div>
<p>
That is, we have <code>n+1</code> vertices named <code>0, 1, …, n</code> and <code>n</code> edges named <code>0, 1, …, n-1</code>
with one typing axiom being <code>j : j ⟶ (j+1)</code>. Alternatively,
</p>

<div class="org-src-container">
<pre class="src src-haskell">[<span style="color: #859900; font-weight: bold;">_</span>] <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#8469;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Graph</span>
[ n ] <span style="color: #268bd2;">=</span> record {<span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Fin</span> (suc n) ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">=</span> &#955; x y <span style="color: #268bd2;">&#8594;</span> fsuc x <span style="color: #268bd2;">&#8801;</span> ` y }
</pre>
</div>
</div>

<div id="outline-container-orgad6d15b" class="outline-3">
<h3 id="Types-Require-Casting"><span class="section-number-3">2.1</span> Types Require Casting</h3>
<div class="outline-text-3" id="text-Types-Require-Casting">
<p>
However, we must admit that a slight downside of the typed approach
&#x2013;the two-piece definition&#x2013; is now
we may need to use the following ‘shifting’ combinators:
They shift, or slide, the edge types.
</p>

<pre class="example">
-- casting
_⟫_ : ∀{x y y’} →  (x ⟶ y) → (y ≡ y’) → (x ⟶ y’)
e ⟫ ≡-refl = e

-- Casting leaves the edge the same, only type information changes
≅-⟫ : ∀{x y y’} {e : x ⟶ y} (y≈y’ : y ≡ y’) → e ≅ (e ⟫ y≈y’)
≅-⟫ ≡-refl = ≅-refl
</pre>

<p>
Such is the cost of using a typed-approach.
</p>

<p>
Even worse, if we use homogeneous equality then we’d have the ghastly operator
</p>
<pre class="example">
≡-⟫ : ∀{x y y’} {e : x ⟶ y} (y≈y’ : y ≡ y’) → e ⟫ y≈y’ ≡ (≡-subst (λ ω → x ⟶ ω) y≈y’ e)
</pre>

<p>
However, it seems that our development does not even make use of these.
Lucky us! However, it is something to be aware of.
</p>
</div>
</div>

<div id="outline-container-org43d0b63" class="outline-3">
<h3 id="Signatures"><span class="section-number-3">2.2</span> Signatures</h3>
<div class="outline-text-3" id="text-Signatures">
<p>
A <i>signature</i> consists of ‘sort symbols’ and ‘function symbols’ each of which is associated source-sorts
and a target-sort &#x2013;essentially it is an interface in the programming sense of the word thereby providing
a lexicon for a language.
A <i>model</i> or <i>algebra</i> of a language is an interpretation of the sort symbols as sets and an interpretation of the
function symbols as functions between those sets; i.e., it is an <i>implementation</i> in programming terms.
Later you may note that instead of sets and functions we may use the objects and morphisms of
a fixed category instead, and so get a model in that category.
</p>

<div class="org-center">
<table>


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

<col  class="org-left">

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left"><span class="underline">Math</span></td>
<td class="org-left">≅</td>
<td class="org-left"><span class="underline">Coding</span></td>
</tr>

<tr>
<td class="org-left">Signature</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">Interface, record type, class</td>
</tr>

<tr>
<td class="org-left">Algebra</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">Implementation, instance, object</td>
</tr>

<tr>
<td class="org-left">Language Term</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">Syntax</td>
</tr>

<tr>
<td class="org-left">Interpretation</td>
<td class="org-left">&#xa0;</td>
<td class="org-left">Semantics, i.e., an implementation</td>
</tr>
</tbody>
</table>
</div>

<p>
Formally, one-sorted signatures are defined:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Data.Vec</span>
  using (<span style="color: #b58900; font-style: italic;">Vec</span>)
  renaming (<span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8759;</span><span style="color: #859900; font-weight: bold;">_</span> to <span style="color: #859900; font-weight: bold;">_</span>,,<span style="color: #859900; font-weight: bold;">_</span> ; <span style="color: #b58900; font-style: italic;">[]</span> to nil) <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">, already in use for products :/</span>

<span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">one sorted</span>
<span style="color: #b58900;">record</span> <span style="color: #b58900; font-style: italic;">Signature</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> <span style="color: #859900; font-weight: bold;">where</span>
    field
     <span style="color: #b58900; font-style: italic;">&#119977;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#8469;</span>        <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">How many function symbols there are</span>
     ar <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Vec</span> <span style="color: #b58900; font-style: italic;">&#8469;</span> <span style="color: #b58900; font-style: italic;">&#119977;</span> <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Their arities: lookup i ar == arity of i-th function symbol</span>

<span style="color: #b58900;">open</span> <span style="color: #b58900; font-style: italic;">Signature</span> &#10627;<span style="color: #268bd2;">...</span>&#10628; <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#119977; now refers to the number of function symbols in a signature</span>
</pre>
</div>

<p>
For example, the signature of monoids consists of a single sort symbol <code>C</code> &#x2013;which can be
interpreted as the carrier of the monoid&#x2013; and two function symbols <code>m , u</code>
&#x2013;which can be interpreted as the monoid multiplication and unit&#x2013; with source-target
sort lists <code>((),C) , ((C,C), C)</code> &#x2014;some would notate this by <code>u :→ C , m : C × C → C</code>.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900; font-style: italic;">MonSig</span> <span style="color: #b58900;">:</span> <span style="color: #b58900; font-style: italic;">Signature</span>
<span style="color: #b58900; font-style: italic;">MonSig</span> <span style="color: #268bd2;">=</span> record { <span style="color: #b58900; font-style: italic;">&#119977;</span> <span style="color: #268bd2;">=</span> 2 ; ar <span style="color: #268bd2;">=</span> 0 ,, 2 ,, nil }
<span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">unit u : X&#8304; &#8594; X and multiplication m : X&#178; &#8594; X</span>
</pre>
</div>

<p>
Generalising on monoids by typing the multiplication we obtain
the signature of categories which consists of three sort symbols <code>O, A, C</code> &#x2013;which can be
interepreted as objects, arrows, and composable pairs of arrows&#x2013; and four function symbols
<code>⨾ , src, tgt, id</code> with source-target sort lists <code>(C,A) , (A,O) , (A,O) , (O,A)</code>
&#x2014;notice that only a language of symbols
has been declared without any properties besides those of typing. If we discard <code>C, ⨾, id</code> we
then obtain the signature of graphs. Without knowing what categories are, we have seen that their
signatures are similar to both the graph and monoid signatures and so expect their logics to
also be similar. Moreover we now have a few slogans,
\[\color{green}{\text{Categories are precisely typed monoids!}}\]
\[\color{green}{\text{Categories are precisely graphs with a monoidal structure!}}\]
\[\color{green}{\text{Categories are precisely coherently constructive lattices!}}\]
</p>

<p>
( The last one is completely unmotivated from our discussion, but it's a good place for the slogan and
  will be touched on when we look at examples of categories. )
</p>

<p>
A signature can be visualised in the plane by associating a dot for each sort symbol and an arrow
for each function symbol such that the arrow has a tail from each sort in the associated function
symbols source sorts list and the end of the arrow is the target sort of the sort symbol.
That is, a signature can be visualised as a hyper-graph.
</p>

<ul class="org-ul">
<li>A signature whose function symbols each have only one sort symbol for source-sorts is called a
‘graph signature’ since it corresponds to &#x2014;or can be visualised as&#x2014; a graph.</li>

<li>Then a model of a graph (signature) <code>𝒢</code> is an interpreation/realisation of the graph’s vertices
as sets and the graph’s edges as functions between said sets.</li>

<li>A model of <code>𝒢</code> is nothing more than a graph morphism
<code>𝒢 ⟶ 𝒮e𝓉</code>, where <code>𝒮e𝓉</code> is the graph with vertices being sets and edges being functions.</li>
</ul>

<p>
Notice that a <code>Graph₀</code> is precisely a model of the graph <code>• ⇉ •</code>, which consists of
two vertices and two edges from the first vertex to the second vertex.
We will return to this point ;-)
</p>

<p>
Before we move on, it is important to note that a signature does not capture any
constraints required on the symbols &#x2013;e.g., a monoid is the monoid signature as well
as the constraint that the 2-arity operation is associative and the 0-arity operation is its unit.
More generally, a <i>specification</i> consists of a signature declaring symbols of interest,
along with a set of sentences over it that denote axioms or constraints.
In programming parlance, this is an interface consisting of types and functions
that need to be provided and implemented, along with constraints that are usually documented in comments.
Unsurprisingly, models of specifications also form categories.
</p>
</div>
</div>
</div>

<div id="outline-container-org6b29d54" class="outline-2">
<h2 id="Cats-but-no-alligators"><span class="section-number-2">3</span> <i>Cats but no alligators</i></h2>
<div class="outline-text-2" id="text-Cats-but-no-alligators">
<p>
In this section we introduce the notion of a ‘‘poor-man’s category’’ along with the notion of
structure preserving transformations and structure preserving transformations between such
transformations. The former are called <i>functors</i> and the latter are known as <i>natural transformations</i>
and are considered one of the most important pieces of the fundamentals of category theory;
as such, we discuss them at length.
Afterwards, we relate this section back to our motivating discussion of graphs.
</p>
</div>

<div id="outline-container-org9c4b0e6" class="outline-3">
<h3 id="Strict-Categories"><span class="section-number-3">3.1</span> Strict Categories</h3>
<div class="outline-text-3" id="text-Strict-Categories">
<p>
A category, like a monoid, is a a few types and operations for which some equations hold.
However, to discuss equations a notion of equality is needed and rather than enforce one
outright it is best to let it be given. This is a ‘set’ in constructive mathematics:
A type with an <code>E</code>-quivalence relation on it &#x2014;also called a <i>setoid</i> or an <code>E</code>-set.
However, then the structure must have a few added axioms: The operations must be congruences,
i.e., preserve the equivalence relation, and structure-preserving maps must also be congruences.
</p>

<p>
For our purposes we will use propositional equality and point-wise propositional equality,
and as such most of the proofs fall out of the fact that propositional equality is an equivalence.
However, this setoid structure becomes a bit of a noise, without providing any real insight for our uses,
and the issues of equivalences will be a distraction from the prime focus.
Instead, for our two cases where we use point-wise propositional,
we will postulate two forms of extensionality. Without question this is not a general approach
&#x2014;then again, our aim is not to develop a library for category theory, which has already been
done so elegantly by Kahl who calls it the <a href="http://relmics.mcmaster.ca/RATH-Agda/RATH-Agda-2.0.0.pdf">RATH-Agda</a> project.
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> <span style="color: #859900; font-weight: bold;">_</span> <span style="color: #859900; font-weight: bold;">where</span> <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">An anyonomous module for categorial definitions</span>

 record <span style="color: #b58900; font-style: italic;">Category</span> {i j <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Level</span>} <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> (&#8467;suc (i <span style="color: #268bd2;">&#8845;</span> j)) <span style="color: #859900; font-weight: bold;">where</span>
  <span style="color: #859900; font-weight: bold;">infixr</span> 10 <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span>
  field
    <span style="color: #b58900; font-style: italic;">Obj</span>      <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> i
    <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span>     <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> j
    <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span>      <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span>} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">C</span>
    assoc    <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #b58900; font-style: italic;">D</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span>}{g <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">C</span>} {h <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">D</span>} <span style="color: #268bd2;">&#8594;</span> (f <span style="color: #268bd2;">&#10814;</span> g) <span style="color: #268bd2;">&#10814;</span> h <span style="color: #268bd2;">&#8801;</span> f <span style="color: #268bd2;">&#10814;</span> (g <span style="color: #268bd2;">&#10814;</span> h)
    <span style="color: #b58900; font-style: italic;">Id</span>       <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span>} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">A</span>
    leftId   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span>} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Id</span> <span style="color: #268bd2;">&#10814;</span> f <span style="color: #268bd2;">&#8801;</span> f
    rightId  <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span>} <span style="color: #268bd2;">&#8594;</span> f <span style="color: #268bd2;">&#10814;</span> <span style="color: #b58900; font-style: italic;">Id</span> <span style="color: #268bd2;">&#8801;</span> f

 open <span style="color: #b58900; font-style: italic;">Category</span> using (<span style="color: #b58900; font-style: italic;">Obj</span>)
 open <span style="color: #b58900; font-style: italic;">Category</span> &#10627;<span style="color: #268bd2;">...</span>&#10628; hiding (<span style="color: #b58900; font-style: italic;">Obj</span>)

 <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Some sugar for times when we must specify the category</span>
 <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8220;colons associate to the right&#8221; ;-)</span>

 arr <span style="color: #268bd2;">=</span> Category._<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span>
 syntax arr <span style="color: #b58900; font-style: italic;">&#119966;</span> x y  <span style="color: #268bd2;">=</span>  x <span style="color: #268bd2;">&#10230;</span> y <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8220;ghost colon&#8221;</span>

 cmp <span style="color: #268bd2;">=</span> Category._<span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span>
 syntax cmp <span style="color: #b58900; font-style: italic;">&#119966;</span> f g  <span style="color: #268bd2;">=</span>  f <span style="color: #268bd2;">&#10814;</span> g <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8220;ghost colon&#8221;</span>
</pre>
</div>

<p>
However, similar to nearly everything else in this document, we can leave the setoid approach as an exercise
for the reader, which of course has solutions being in the literate source.
</p>

<p>
Moreover, lest you’re not convinced that my usage of extensionality is at all acceptable,
then note that others have used it to simplify their presentations; e.g.,
<a href="http://cs.ioc.ee/~tarmo/papers/jfr14.pdf">Relative monads formalised</a>.
Such ‘appeal to authority’ is for the lazy reader who dares not think for him- or her-self,
otherwise one ought to read up on the <a href="https://ncatlab.org/nlab/show/principle+of+equivalence">evils</a>
of using equality instead of equivalence relations so as to understand
<a href="http://www.math.harvard.edu/~mazur/preprints/when_is_one.pdf">when one thing is really another</a>.
</p>

<p>
The diligent reader may be interested to know that Maarten Fokkinga has written a very
<a href="http://maartenfokkinga.github.io/utwente/mmf92b.pdf">gentle introduction to category theory using the calculational approach</a>; I highly recommend it!
</p>

<p>
Anyhow, in place of strict equality, one uses categorical isomorphism instead.
</p>
<div class="org-src-container">
<pre class="src src-haskell"> open <span style="color: #b58900; font-style: italic;">Category</span> using (<span style="color: #b58900; font-style: italic;">Obj</span>) public

 record <span style="color: #b58900; font-style: italic;">Iso</span> {i} {j} (<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}) (<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>) <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> j <span style="color: #859900; font-weight: bold;">where</span>
   private <span style="color: #859900; font-weight: bold;">instance</span> <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> ; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
   field
     to   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span>
     from <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">A</span>
     lid  <span style="color: #b58900; font-style: italic;">:</span> to <span style="color: #268bd2;">&#10814;</span> from <span style="color: #268bd2;">&#8801;</span> <span style="color: #b58900; font-style: italic;">Id</span>
     rid  <span style="color: #b58900; font-style: italic;">:</span> from <span style="color: #268bd2;">&#10814;</span> to <span style="color: #268bd2;">&#8801;</span> <span style="color: #b58900; font-style: italic;">Id</span>

 syntax <span style="color: #b58900; font-style: italic;">Iso</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8773;</span> <span style="color: #b58900; font-style: italic;">B</span> within <span style="color: #b58900; font-style: italic;">&#119966;</span>
</pre>
</div>

<p>
Interestingly, we shall later encounter a rather large category named
𝒞𝒶𝓉 possessing the special property of being a <a href="https://ncatlab.org/nlab/show/2-category">“2-Category”</a>:
It has morphisms between objects, as expected, which are now called “1-morphisms”,
and it has morphisms between 1-morphisms, also called “2-morphisms”.
</p>

<p>
That is, it has morphisms between morphisms.
</p>

<p>
Above we argued that equality should be deferred in favour of isomorphism
at the morphism level. Hence, in a 2-Category, it is only reasonable to defer
an equation involving objects to be up to isomorphism of 2-morphisms
&#x2014;this is then called an “equivalence”.
</p>
<pre class="example">
ℒHS ≃ ℛHS  ⇔  Σ F ∶ ℒHS ⟶ ℛHS • Σ G ∶ ℛHS ⟶ ℒHS • F ⨾ G ≅ G ⨾ F ≅ Id
</pre>

<p>
Hence when it comes to categories themselves, one usually speaks in terms of
equivalences rather than isomorphisms.
</p>

<p>
For example, let 𝒫𝒶𝓇 be the supercategory of 𝒮e𝓉 with morphisms being ‘partial
functions’ <code>(A ⟶ B) = (A → B + 𝟙)</code> where the extra element of <code>𝟙 = { * }</code> represents
‘undefined’ &#x2014;also known as the <code>Partial</code>, <code>Option</code>, or <code>Maybe</code> monads.  Moreover,
let 𝒫𝒮ℯ𝓉 be the category of sets with an elected point.  Then, <code>𝒫𝒶𝓇 ≃ 𝒫𝒮e𝓉</code> is
witnessed by <code>(A ⟶ B) ↦ ( (A + 𝟙, *) ⟶ (B + 𝟙, *) )</code> and conversely
<code>( (A , a) ⟶ (B , b) ) ↦ ( A - a ⟶ B - b)</code> where
 <br> <code>X - x ≡ Σ y ∶ X • ¬(x ≡
y)</code>. Exercise: Work out the remaining details for the equivalence.
</p>
</div>
</div>

<div id="outline-container-org2b12f86" class="outline-3">
<h3 id="Familiar-𝒮ℯ𝓉-tings"><span class="section-number-3">3.2</span> Familiar <code>𝒮ℯ𝓉</code>-tings</h3>
<div class="outline-text-3" id="text-Familiar-𝒮ℯ𝓉-tings">
<p>
Let us give some elementary examples of the notion of a category to exhibit its ubiquity.
</p>
</div>

<div id="outline-container-org96c688e" class="outline-4">
<h4 id="𝒮ℯ𝓉's"><span class="section-number-4">3.2.1</span> 𝒮ℯ𝓉's</h4>
<div class="outline-text-4" id="text-𝒮ℯ𝓉's">
<p>
The collection of small, say level <code>i</code>, types and functions between them and usual function composition
with usual identity form a category and this is not at all difficult to see:
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #859900; font-weight: bold;">instance</span>
  <span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Category</span> {&#8467;suc i} {i} <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">this is a &#8216;big&#8217; category</span>
  <span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span> {i} <span style="color: #268bd2;">=</span> record {
      <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Set</span> i
    ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">=</span> &#955; <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#8594;</span> (<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span>)
    ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">=</span> &#955; f g <span style="color: #268bd2;">&#8594;</span> (&#955; x <span style="color: #268bd2;">&#8594;</span> g (f x))
    ; assoc <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
    ; <span style="color: #b58900; font-style: italic;">Id</span> <span style="color: #268bd2;">=</span> &#955; x <span style="color: #268bd2;">&#8594;</span> x
    ; leftId <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
    ; rightId <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
    }
</pre>
</div>
<p>
Sadly, this category is traditionally used to motivate constructions in arbitrary categories
and as such people usually think of objects in an arbitrary category as nothing more than
sets with extra datum &#x2014;which is completely false.
</p>

<p>
For example, the category <code>Div</code> consists of objects <i>and</i> arrows both being numbers ℕ
and there is an arrow \(k : m → n\) precisely when <code>k × m = n</code>, so that an arrow is a
constructive witness that \(m\) divides \(n\). Notice that besides ℕ, no sets nor functions
were involved in the making of this useful number-theoretic category.
</p>
</div>
</div>

<div id="outline-container-orgbae0fbf" class="outline-4">
<h4 id="Sets-are-trivial-categories"><span class="section-number-4">3.2.2</span> Sets are trivial categories</h4>
<div class="outline-text-4" id="text-Sets-are-trivial-categories">
<p>
Recall that a type, or set, is nothing more than a specified collection of values.
</p>

<p>
Every set is also a category: There is a formal syntactic object associated with each element, the only morphisms are (formal)
identities, and composition is constantly a syntactic identity.
Some define a set to be a category with only identity morphisms; also called a
‘discrete category’ when one wants to distance themself from set theory ;)
&#x2014;less loosely, a discrete category over a type <code>S</code> has <code>Obj = S</code> and <code>(x ⟶ y) = (x ≡ y)</code>,
where the equivalence is classical, having two possible members <i>true</i> or <i>false</i>.
</p>

<p>
Discrete categories are quite an important space for <a href="http://homotopytypetheory.org/">hott</a> people &#x2026;
that’s right, attractive people are interested in these things.
</p>

<p>
Observe that all arrows are invertible! &#x2014;due to the symmetry of equality.
Categories with this property are known as <i>groupoids</i>.
</p>
</div>
</div>

<div id="outline-container-orgaa64ad4" class="outline-4">
<h4 id="Categories-are-typed-monoids"><span class="section-number-4">3.2.3</span> Categories are typed monoids</h4>
<div class="outline-text-4" id="text-Categories-are-typed-monoids">
<p>
Recall that a monoid <code>(M, ⊕, e)</code> is a type <code>M</code> with an associative operation <code>⊕ : M × M → M</code>
that has a unit <code>e</code>.
</p>

<p>
Every monoid is also a category: There is one object, call it <code>★</code>, the morphisms are the monoid
elements, and composition is the monoid operation.
&#x2014;less loosely, for a monoid <code>(M, ⊕, e)</code> we take <code>Obj = {★} , _⟶_ = M</code>.
</p>

<p>
In fact, some would define a monoid to be a one-object category!
&#x2013;I'm looking at you <a href="https://books.google.ca/books/about/Categories_Allegories.html?id=fCSJRegkKdoC&amp;printsec=frontcover&amp;source=kp_read_button&amp;redir_esc=y#v=onepage&amp;q&amp;f=false">Freyd &amp; Scedrov</a> =)
</p>
</div>
</div>

<div id="outline-container-orga99af1e" class="outline-4">
<h4 id="Categories-are-coherently-preordered-sets"><span class="section-number-4">3.2.4</span> Categories are coherently preordered sets</h4>
<div class="outline-text-4" id="text-Categories-are-coherently-preordered-sets">
<p>
<a href="http://www.cs.utexas.edu/~EWD/ewd11xx/EWD1102.PDF">Recall</a> that a preordered set, or preset, is a type <code>P</code> with a relation <code>≤</code> on
it that satisfies <i>indirect inequality from above</i>:
\[
  ∀ x , y •\qquad x ≤ y \quad⇔\quad (∀ z •\; y ≤ z ⇒ x ≤ z)
\]
Equivalently, if it satisfies <i>indirect equality from below</i>:
\[ ∀ x , y •\qquad x ≤ y \quad⇔\quad (∀ z •\; z ≤ x ⇒ z ≤ y) \]
If we also have \(∀ x , y •\; x ≤ y \,∧\, y ≤ x \;⇒\; x = y\),
then we say <code>(P, ≤)</code> is a ‘poset’ or an ‘ordered set’.
</p>

<p>
Every (pre)ordered set is also a category:
The objects are the elements,
the morphisms are the order-relations,
identities are the relfexitivity of <code>≤</code>,
and composition is transitivity of <code>≤</code>.
To see this more clearly, suppose you have a group
\((M, ⊕, \_{}⁻¹, e)\) and you define \(x ≤ y \;⇔\; (∃ m : M •\; m ⊕ x = y)\)
then the this is a preorder whose induced category has a morphism
\(m : x → y\) precicely when \(m ⊕ x = y\)
&#x2013;now sit down and define the remaining categorical structure of this
‘constructive’ preorder associated to the group.
</p>

<p>
Traditionally, classically, the relation <code>≤</code> is precicely a function <code>P × P ⟶ 𝔹 = {true, flase}</code>
and thus there is at-most one morphism between any two objects
&#x2013;consequently, categories with this property are called <i>poset categories</i>.
</p>

<p>
In the constructive setting, the relation <code>≤</code> is typed <code>P × P → Set</code> and then
for a preset <code>(P, ≤)</code> we take <code>Obj = P, _⟶_ = a ≤ b</code> and insist
on ‘proof-irrelevance’ <code>∀ {a b} (p q : a ≤ b) → p ≡ q</code> so that there is at most one morphism
between any two objects.
The restriction is not needed if we were using actual categories-with-setoids since then we would
<i>define</i> morphism equality to be  <br>
<code>((a, b, p) ≈ (a’, b’, q) )  =  (a ≡ a’  ×  b ≡ b’)</code>.
</p>

<p>
Observe that in the case we have a poset, every isomorphism is an equality:
\[
  ∀ x, y •\qquad x ≅ y ⇔ x ≡ y
\]
Categories with this property are called <i>skeletal</i>.
Again, hott people like this; so much so, that they want it, more-or-less, to be a
<a href="http://arxiv.org/abs/1302.4731">foundational axiom</a>!
</p>

<p>
Poset categories are a wonderful and natural motivator for many constructions and definitions in
category theory. This idea is so broad-reaching that it would not be an exaggeration to think of
<a href="http://www.cs.nott.ac.uk/~psarb2/papers/abstract.html#CatTheory">categories as coherently constructive lattices</a>!
</p>
</div>
</div>

<div id="outline-container-orgd12b66d" class="outline-4">
<h4 id="Groupoids"><span class="section-number-4">3.2.5</span> Groupoids</h4>
<div class="outline-text-4" id="text-Groupoids">
<p>
Equivalence relations are relations that are symmetric, reflexive, and transitive.
Alternatively, they are preorder categories where every morphism is invertible &#x2014;this is the
symmetry property. But categories whose morphisms are invertible are groupoids!
</p>

<p>
Hence, groupoids can be thought of as generalized equivalence relations.
Better yet, as “constructive” equivalence relations: there might be more than one morphism/construction
witnessing the equivalence of two items.
</p>

<p>
Some insist that a <i>true</i> ‘set’ is a type endowed with an equivalence relation, that is a setoid.
However, since groupoids generalise equivalence relations, others might insist on a true set to be
a "groupoid". However, in the constructive setting of dependent-type theory, these notions
coincide!
</p>
</div>
</div>

<div id="outline-container-org423a07a" class="outline-4">
<h4 id="Rule-of-Thumb"><span class="section-number-4">3.2.6</span> Rule of Thumb</h4>
<div class="outline-text-4" id="text-Rule-of-Thumb">
<p>
It’s been said that the aforementioned categories should be consulted whenever one learns a new
concept of category theory.
Indeed, these examples show that a category is a generalisation of a system of processes,
a system of compositionality, and an ordered system.
</p>
</div>
</div>
</div>
</div>

<div id="outline-container-org3bc199c" class="outline-2">
<h2 id="Endowing-Structure-with-Functors"><span class="section-number-2">4</span> <i>Endowing Structure with Functors</i></h2>
<div class="outline-text-2" id="text-Endowing-Structure-with-Functors">
<p>
Now the notion of structure-preserving maps, for categories, is just that of graphs
but with attention to the algebraic portions as well.
</p>

<div class="org-src-container">
<pre class="src src-haskell"> record <span style="color: #b58900; font-style: italic;">Functor</span> {i j k l} (<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}) (<span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {k} {l})
  <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> (&#8467;suc (i <span style="color: #268bd2;">&#8845;</span> j <span style="color: #268bd2;">&#8845;</span> k <span style="color: #268bd2;">&#8845;</span> l)) <span style="color: #859900; font-weight: bold;">where</span>
  private
    <span style="color: #859900; font-weight: bold;">instance</span>
      <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> ;  <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
      <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> ;  <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">&#8242;</span>  <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>
  field
    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Usual graph homomorphism structure: An object map, with morphism preservation</span>
    obj   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>
    mor   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#10230;</span> y <span style="color: #268bd2;">&#8594;</span> obj x <span style="color: #268bd2;">&#10230;</span> obj y
    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Interaction with new algebraic structure: Preservation of identities &amp; composition</span>
    id    <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} <span style="color: #268bd2;">&#8594;</span> mor (<span style="color: #b58900; font-style: italic;">Id</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">=</span> x}) <span style="color: #268bd2;">&#8801;</span> <span style="color: #b58900; font-style: italic;">Id</span>       <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">identities preservation</span>
    comp  <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y z} {f <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y} {g <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#10230;</span> z} <span style="color: #268bd2;">&#8594;</span> mor (f <span style="color: #268bd2;">&#10814;</span> g) <span style="color: #268bd2;">&#8801;</span> mor f <span style="color: #268bd2;">&#10814;</span> mor g

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Aliases for readability</span>
  functor_preserves<span style="color: #268bd2;">-</span>composition <span style="color: #268bd2;">=</span> comp
  functor_preserves<span style="color: #268bd2;">-</span>identities  <span style="color: #268bd2;">=</span> id

 open <span style="color: #b58900; font-style: italic;">Functor</span> public hiding (id ; comp)
</pre>
</div>

<p>
For a functor <code>F</code>, it is common practice to denote both <code>obj F</code> and <code>mor F</code> by <code>F</code> and this is usually
not an issue since we can use type inference to deduce which is meant. However, in the Agda formalization
we will continue to use the names <code>mor , obj</code>. Incidentally in the Haskell community, <code>mor</code>
is known as <code>fmap</code> but we shall avoid that name or risk asymmetry in the definition of
a functor, as is the case in Haskell which turns out to be pragmatically useful.
</p>

<p>
A functor can be thought of as endowing an object with some form of structure
&#x2014;since categories are intrinsically structureless in category theory&#x2014;
and so the morphism component of a functor can be thought of as preserving relations:
<code>f : a ⟶ b ⇒ F f : F a ⟶ F b</code> can be read as, ‘‘if <code>a</code> is related to <code>b</code> (as witnessed by <code>f</code>)
then their structured images are also related (as witness by <code>F f</code>)’’.
Recall the category <code>Div</code> for constructive divisibility relationships ;-)
</p>
</div>

<div id="outline-container-orgf5e91f8" class="outline-3">
<h3 id="Functor-Examples"><span class="section-number-3">4.1</span> Examples</h3>
<div class="outline-text-3" id="text-Functor-Examples">
<p>
A functor among monoids &#x2013;construed as categories&#x2013; is just a monoid homomorphism;
i.e., an identity and multiplication preserving function of the carriers.
</p>
<table>


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

<col  class="org-left">
</colgroup>
<tbody>
<tr>
<td class="org-left">&#xa0;</td>
<td class="org-left"><i>(M, ⊕, e) ⟶ (N, ⊗, d)</i></td>
</tr>

<tr>
<td class="org-left">=</td>
<td class="org-left"><i>Σ h ∶ M → N • ∀ x,y • h(x ⊕ y) = h x ⊗ h y ∧ h e = d</i></td>
</tr>
</tbody>
</table>

<p>
By induction, <code>h</code> preserves all finite ⊕-multiplication and, more generally,
functors preserve finite compositions:
\[ F (f₀ ⨾ f₁ ⨾ ⋯ ⨾ fₙ) \;\;=\;\; F\,f₀ ⨾ F\,f₁ ⨾ ⋯ ⨾ F\,fₙ \]
Cool beans :-)
</p>

<p>
In the same way, sit down and check your understanding!
</p>
<ul class="org-ul">
<li>A functor among discrete categories is just a function of the associated sets.</li>
<li>A functor among poset categories is an order-preserving function.</li>
</ul>

<p>
Two examples of functors from a poset (category) to a monoid (category):
</p>

<ul class="org-ul">
<li><code>monus : (ℕ, ≤) ⟶ (ℕ,+, 0)</code> is a functor defined on morphisms by
\[ i ≤ j \quad⇒\quad \mathsf{monus}(i,j) ≔ j - i\]
Then the functor laws become  <code>i - i = 0</code> and <code>(k - j) + (j - i) = k - i</code>.</li>

<li><code>div : (ℕ⁺, ≤) → (ℚ, ×, 1)</code> is defined on morphisms by
\[i ≤ j \quad⇒\quad \mathsf{div}(i,j) ≔ j / i\]
The functor laws become <code>i / i = 1</code> and <code>(k / j) × (j / i) = k / i</code>.</li>
</ul>

<p>
Hey, these two seem alarmingly similar! What gives!
Well, they’re both functors from posets to monoids ;)
Also, they are instances of ‘residuated po-monoids’.
Non-commutative monoids may have not have a general inverse operation,
but instead might have left- and right- inverse operations known as residuals
&#x2014;we’ll mention this word again when discussing adjunctions and are
categorically seen as kan extensions.
Alternatively, they’re are instances of <a href="http://link.springer.com.libaccess.lib.mcmaster.ca/article/10.1007/s10773-004-7710-7">‘(Kopka) Difference-posets’</a>.
</p>


<p>
For more examples of categories, we will need to reason
by extensionality &#x2013;two things are ‘equal’ when they have
equivalent properties &#x2026; recall Leibniz and his
<a href="https://en.wikipedia.org/wiki/Identity_of_indiscernibles">law of indiscernibles</a> ;p
</p>
</div>
</div>
</div>
<div id="outline-container-org0410d11" class="outline-2">
<h2 id="The-four-postulates-of-the-apocalypse"><span class="section-number-2">5</span> <i>The four postulates of the apocalypse</i></h2>
<div class="outline-text-2" id="text-The-four-postulates-of-the-apocalypse">
<p>
Categories have objects and morphisms between them, functors are morphisms between categories,
and then we can go up another level and consider morphisms between functors.
These ‘level 2 morphisms’ are pretty cool, so let’s touch on them briefly.
</p>

<p>
Recall that a poset ordering is extended to (possibly non-monotone) functions \(f , g\) pointwise
\[f \overset{.}{≤} g \quad\iff\quad (∀ x •\; f\, x \,≤\, g\, x)\]
As such, with posets as our guide, we extend the notion of ‘morphism between functors’
to be a ‘witness’ of these orderings \(η : ∀ {X} → F\, X ⟶ G\, X\)
&#x2013;this is a dependent type, note that the second arrow notates category morphisms, whereas
the first acts as a separator from the implicit parameter \(X\); sometimes one sees \(η_X\)
for each component/instance of such an operation.
</p>

<div class="org-center">
<p>
\(\require{AMScd}\)
</p>
\begin{CD}
\color{navy}{F\, A} @>\color{fuchsia}{η_A}>>      \color{teal}{G\, A}    \\
@V\color{navy}{F\, f}VV    \!=                    @VV\color{teal}{G\, f}V \\
\color{navy}{F\, B} @>>\color{fuchsia}{η_B}>      \color{teal}{G\, B}
\end{CD}
</div>

<p>
However, then for any morphism \(f : A ⟶ B\) we have two ways to get from \(F\, A\) to \(G\, B\) via
<code>F f ⨾ η {B}</code> and <code>η {A} ⨾ G f</code> and rather than choose one or the other, we request that they
are identical &#x2014;similar to the case of associativity.
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #b58900; font-style: italic;">NatTrans</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j i&#8217; j&#8217;}  &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j} &#10628; &#10627; <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i&#8217;} {j&#8217;} &#10628;
            (<span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> (j&#8217; <span style="color: #268bd2;">&#8845;</span> i <span style="color: #268bd2;">&#8845;</span> j)
 <span style="color: #b58900; font-style: italic;">NatTrans</span> &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> &#10628; &#10627; <span style="color: #b58900; font-style: italic;">&#119967;</span> &#10628; <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">=</span>
   <span style="color: #b58900; font-style: italic;">&#931;</span> &#951; <span style="color: #268bd2;">&#8758;</span> (<span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} <span style="color: #268bd2;">&#8594;</span> (obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">X</span>) <span style="color: #268bd2;">&#10230;</span> (obj <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">X</span>))
       <span style="color: #268bd2;">&#8226;</span> (<span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span>} <span style="color: #268bd2;">&#8594;</span> mor <span style="color: #b58900; font-style: italic;">F</span> f <span style="color: #268bd2;">&#10814;</span> &#951; {<span style="color: #b58900; font-style: italic;">B</span>} <span style="color: #268bd2;">&#8801;</span> &#951; {<span style="color: #b58900; font-style: italic;">A</span>} <span style="color: #268bd2;">&#10814;</span> mor <span style="color: #b58900; font-style: italic;">G</span> f)
</pre>
</div>
<p>
The naturality condition is remembered by placing the target component <code>η {B}</code> <i>after</i>
lifting <code>f</code> using the <i>source</i> functor <code>F</code>;
likewise placing the source component <i>before</i> applying the target functor.
</p>

<p>
Another way to remember it:
<code>η : F ⟶̇ G</code> starts at <code>F</code> and ends at <code>G</code>, so the naturality also starts with <code>F</code> and ends
with <code>G</code>; i.e., <code>F f ⨾ η {B} = η {A} ⨾ G f</code> :-)
</p>

<p>
It is at this junction that aforementioned problem with our definition
of category comes to light: Function equality is extensional by definition
and as such we cannot prove it.
Right now we have two function-like structures for which we will postulate a
form of extensionality,
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">function extensionality</span>
 postulate extensionality <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j} {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> i} {<span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> j}
                              {f g <span style="color: #b58900; font-style: italic;">:</span> (a <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span> a}
                          <span style="color: #268bd2;">&#8594;</span> (<span style="color: #268bd2;">&#8704;</span> {a} <span style="color: #268bd2;">&#8594;</span> f a <span style="color: #268bd2;">&#8801;</span> g a) <span style="color: #268bd2;">&#8594;</span> f <span style="color: #268bd2;">&#8801;</span> g

 <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">functor extensionality</span>
 postulate funcext <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j k l} &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j} &#10628; &#10627; <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {k} {l} &#10628;
                       {<span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>}
                     <span style="color: #268bd2;">&#8594;</span> (oeq <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {o} <span style="color: #268bd2;">&#8594;</span> obj <span style="color: #b58900; font-style: italic;">F</span> o <span style="color: #268bd2;">&#8801;</span> obj <span style="color: #b58900; font-style: italic;">G</span> o)
                     <span style="color: #268bd2;">&#8594;</span> (<span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">Y</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">Y</span>}
                        <span style="color: #268bd2;">&#8594;</span> mor <span style="color: #b58900; font-style: italic;">G</span> f  <span style="color: #268bd2;">&#8801;</span>  <span style="color: #268bd2;">&#8801;-</span>subst&#8322; (Category._<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) oeq oeq (mor <span style="color: #b58900; font-style: italic;">F</span> f))
                     <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">&#8801;</span> <span style="color: #b58900; font-style: italic;">G</span>

 <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">graph map extensionality</span>
 postulate graphmapext <span style="color: #b58900; font-style: italic;">:</span> {<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span> } {f g <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">GraphMap</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span>}
                       <span style="color: #268bd2;">&#8594;</span> (veq <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {v} <span style="color: #268bd2;">&#8594;</span> ver f v <span style="color: #268bd2;">&#8801;</span> ver g v)
                       <span style="color: #268bd2;">&#8594;</span> (<span style="color: #268bd2;">&#8704;</span> {x y} {e <span style="color: #b58900; font-style: italic;">:</span> Graph._<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">G</span> x y}
                          <span style="color: #268bd2;">&#8594;</span> edge g e <span style="color: #268bd2;">&#8801;</span> <span style="color: #268bd2;">&#8801;-</span>subst&#8322; (Graph._<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">H</span>) veq veq (edge f e))
                       <span style="color: #268bd2;">&#8594;</span> f <span style="color: #268bd2;">&#8801;</span> g

 <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">natural transformation extensionality</span>
 postulate nattransext <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j i&#8217; j&#8217;} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j} } {<span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i&#8217;} {j&#8217;} }
                         {<span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>} (&#951; &#947; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">NatTrans</span> <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">G</span>)
                       <span style="color: #268bd2;">&#8594;</span> (<span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">X</span>} <span style="color: #268bd2;">&#8594;</span> proj&#8321; &#951; {<span style="color: #b58900; font-style: italic;">X</span>} <span style="color: #268bd2;">&#8801;</span> proj&#8321; &#947; {<span style="color: #b58900; font-style: italic;">X</span>})
                       <span style="color: #268bd2;">&#8594;</span> &#951; <span style="color: #268bd2;">&#8801;</span> &#947;
</pre>
</div>

<p>
Natural transformations are too cool to end discussing so briefly
and so we go on to discuss their usage is mathematics later on.
</p>
</div>

<div id="outline-container-orgedc8658" class="outline-3">
<h3 id="A-very-big-𝒞𝒶𝓉"><span class="section-number-3">5.1</span> A very big <code>𝒞𝒶𝓉</code></h3>
<div class="outline-text-3" id="text-A-very-big-𝒞𝒶𝓉">
<p>
With the notions of categories, functors, and extensionality in-hand we can now discus the
notion of the category of small categories and the category of small graphs.
Afterwards we give another example of a functor that says how every category can be
construed as a graph.
</p>

<p>
First the category of <i>smaller</i> categories,
</p>
<blockquote>
<p>
<code>𝒞𝒶𝓉</code> is a category of kind <code>(ℓsuc m, ℓsuc m)</code>, where <code>m = i ⊍ j</code>, and its objects
are categories of kind <code>(i , j)</code> and so it is not an object of itself.
</p>

<p>
Thank-you Russel and friends!
</p>

<p>
( You may proceed to snicker at the paradoxical and size issues encountered
  by those who use set theory.
  &#x2014;Then again, I’ve never actually learned, nor even attempted to learn,
  any ‘‘formal set theory’’;
  what I do know of set theory is usually couched in the language of type theory;
  I heart <a href="https://www.springer.com/gp/book/9780387941158">LADM</a>!
)
</p>
</blockquote>

<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #859900; font-weight: bold;">instance</span>
  <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Category</span> {&#8467;suc (i <span style="color: #268bd2;">&#8845;</span> j)} {&#8467;suc (i <span style="color: #268bd2;">&#8845;</span> j)}
  <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span> {i} {j} <span style="color: #268bd2;">=</span> record {
      <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}
    ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Functor</span>
    ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">=</span> &#955; {<span style="color: #b58900; font-style: italic;">&#119966;</span>} {<span style="color: #b58900; font-style: italic;">&#119967;</span>} {<span style="color: #b58900; font-style: italic;">&#8496;</span>} <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#8594;</span>
        <span style="color: #859900; font-weight: bold;">let</span> <span style="color: #859900; font-weight: bold;">instance</span>
                   <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> ; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
                   <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> ; <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>
                   <span style="color: #b58900; font-style: italic;">&#8496;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> ; <span style="color: #b58900; font-style: italic;">&#8496;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#8496;</span>
        <span style="color: #859900; font-weight: bold;">in</span> record
        { obj  <span style="color: #268bd2;">=</span>  obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">&#10814;</span> obj <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">this compositon lives in &#119982;e&#120009;</span>
        ; mor  <span style="color: #268bd2;">=</span>  mor <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">&#10814;</span> mor <span style="color: #b58900; font-style: italic;">G</span>
        ; id   <span style="color: #268bd2;">=</span>  &#955; {x} <span style="color: #268bd2;">&#8594;</span> begin
              (mor <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">&#10814;</span> mor <span style="color: #b58900; font-style: italic;">G</span>) (<span style="color: #b58900; font-style: italic;">Id</span> &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> &#10628; {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">=</span> x})
            <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #2aa198;">"definition of function composition"</span> &#10217;<span style="color: #268bd2;">&#8242;</span>
              mor <span style="color: #b58900; font-style: italic;">G</span> (mor <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">Id</span>)
            <span style="color: #268bd2;">&#8801;</span>&#10216; functor <span style="color: #b58900; font-style: italic;">F</span> preserves<span style="color: #268bd2;">-</span>identities even<span style="color: #268bd2;">-</span>under (mor <span style="color: #b58900; font-style: italic;">G</span>) &#10217;
              mor <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">Id</span>
            <span style="color: #268bd2;">&#8801;</span>&#10216; functor <span style="color: #b58900; font-style: italic;">G</span> preserves<span style="color: #268bd2;">-</span>identities &#10217;
              <span style="color: #b58900; font-style: italic;">Id</span>
            <span style="color: #268bd2;">&#8718;</span>
        ; comp <span style="color: #268bd2;">=</span> &#955; {x y z f g} <span style="color: #268bd2;">&#8594;</span>
             begin
               (mor <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">&#10814;</span> mor <span style="color: #b58900; font-style: italic;">G</span>) (f <span style="color: #268bd2;">&#10814;</span> g)
            <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #2aa198;">"definition of function composition"</span> &#10217;<span style="color: #268bd2;">&#8242;</span>
               mor <span style="color: #b58900; font-style: italic;">G</span> (mor <span style="color: #b58900; font-style: italic;">F</span> (f <span style="color: #268bd2;">&#10814;</span> g))
             <span style="color: #268bd2;">&#8801;</span>&#10216; functor <span style="color: #b58900; font-style: italic;">F</span> preserves<span style="color: #268bd2;">-</span>composition even<span style="color: #268bd2;">-</span>under mor <span style="color: #b58900; font-style: italic;">G</span> &#10217;
               mor <span style="color: #b58900; font-style: italic;">G</span> (mor <span style="color: #b58900; font-style: italic;">F</span> f <span style="color: #268bd2;">&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span> g)
             <span style="color: #268bd2;">&#8801;</span>&#10216; functor <span style="color: #b58900; font-style: italic;">G</span> preserves<span style="color: #268bd2;">-</span>composition &#10217;
               (mor <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">&#10814;</span> mor <span style="color: #b58900; font-style: italic;">G</span>) f <span style="color: #268bd2;">&#10814;</span> (mor <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">&#10814;</span> mor <span style="color: #b58900; font-style: italic;">G</span>) g
             <span style="color: #268bd2;">&#8718;</span>
        }
    ; assoc    <span style="color: #268bd2;">=</span>  &#955; {a b c d f g h} <span style="color: #268bd2;">&#8594;</span> funcext <span style="color: #268bd2;">&#8801;-</span>refl <span style="color: #268bd2;">&#8801;-</span>refl
    ; <span style="color: #b58900; font-style: italic;">Id</span>       <span style="color: #268bd2;">=</span>  record { obj <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Id</span> ; mor <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Id</span> ; id <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl ; comp <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl }
    ; leftId   <span style="color: #268bd2;">=</span>  funcext <span style="color: #268bd2;">&#8801;-</span>refl <span style="color: #268bd2;">&#8801;-</span>refl
    ; rightId  <span style="color: #268bd2;">=</span>  funcext <span style="color: #268bd2;">&#8801;-</span>refl <span style="color: #268bd2;">&#8801;-</span>refl
    }
</pre>
</div>

<p>
Some things to note,
</p>

<ul class="org-ul">
<li>First off: <code>functor F preserves-composition even-under mor G</code> is a real line of code!
It consists of actual function calls and is merely an alias for
<code>≡-cong (mor G) (mor F)</code> but it sure is far more readable than this form!</li>

<li><p>
We could have written <code>id = ≡-cong (mor G) (id F) ⟨≡≡⟩ id G</code>,
but this is not terribly clear what is going on.
Especially since we introduced categories not too long ago,
we choose to elaborate the detail.
</p>

<p>
Likewise, <code>comp = (≡-cong (mor G) (comp F)) ⟨≡≡⟩ (comp G)</code>.
</p></li>

<li><code>assoc</code> is trivial since function composition is, by definition, associative.
Likewise <code>leftId, rightId</code> hold since functional identity is, by definition, unit of function composition.</li>
</ul>
</div>
</div>
<div id="outline-container-org766b38c" class="outline-3">
<h3 id="𝒢𝓇𝒶𝓅𝒽"><span class="section-number-3">5.2</span> <code>𝒢𝓇𝒶𝓅𝒽</code></h3>
<div class="outline-text-3" id="text-𝒢𝓇𝒶𝓅𝒽">
<p>
In a nearly identical way, just ignoring the algebraic datum, we can show that
<code>Graph</code>'s with <code>GraphMap</code>'s form a graph
</p>
<pre class="example">
  𝒢𝓇𝒶𝓅𝒽 : Category
  𝒢𝓇𝒶𝓅𝒽 = {! exercise !}
</pre>
</div>
</div>

<div id="outline-container-org7a829b0" class="outline-3">
<h3 id="𝒞𝒶𝓉-'s-are-𝒢𝓇𝒶𝓅𝒽-'s"><span class="section-number-3">5.3</span> <code>𝒞𝒶𝓉</code>'s are <code>𝒢𝓇𝒶𝓅𝒽</code>'s</h3>
<div class="outline-text-3" id="text-𝒞𝒶𝓉-'s-are-𝒢𝓇𝒶𝓅𝒽-'s">
<div class="org-center">
<p>
<i>Forgive and forget: The 𝒰nderlying functor.</i>
</p>
</div>

<p>
Let’s formalise what we meant earlier when we said graphs are categories
but ignoring the algebraic data.
</p>

<p>
Given a category, we ignore the algebraic structure to obtain a graph,
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Graph</span>
 <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">=</span> record { <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">=</span> Category._<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> }
</pre>
</div>

<p>
Likewise, given a functor we ‘forget’ the property that the map of morphisms needs to preserve all
finite compositions to obtain a graph map:
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">:</span> {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span>} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; <span style="color: #b58900; font-style: italic;">&#119967;</span>
 <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">=</span> record { ver <span style="color: #268bd2;">=</span> obj <span style="color: #b58900; font-style: italic;">F</span> ; edge <span style="color: #268bd2;">=</span> mor <span style="color: #b58900; font-style: italic;">F</span> }
</pre>
</div>
<p>
This says that <code>𝒰₁</code> turns <code>ver, edge</code> into <code>obj , mor</code>
--<code>𝒰₁ ⨾ ver  ≡ obj</code> and <code>𝒰₁ ⨾ edge ≡ mor</code>&#x2013; reassuring us that <code>𝒰₁</code> acts
as a bridge between the graph structures: <code>ver , edge</code> of graphs and
<code>obj , mor</code> of categories.
</p>

<p>
Putting this together, we obtain a functor.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Underlying/forgetful functor: Every category is a graph</span>
 <span style="color: #b58900; font-style: italic;">&#119984;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span>
 <span style="color: #b58900; font-style: italic;">&#119984;</span> <span style="color: #268bd2;">=</span> record { obj <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; ; mor <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; ; id <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl ; comp <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl }
</pre>
</div>
<p>
We forget about the extra algebraic structure of a category and of a functor to
arrive at a graph and graph-map, clearly --<code>≡-refl</code>&#x2013; such ‘forgetfullness’ preserves identities
and composition since it does not affect them at all!
</p>

<p>
Those familiar with category theory may exclaim that just as I have mentioned
the names ‘underlying functor’ and ‘forgetful functor’ I ought to mention
‘stripping functor’ as it is just as valid since it brings about connotations of
‘stripping away’ extra structure.
I’m assuming the latter is less popular due to its usage for
poor mathematical jokes and puns.
</p>

<p>
Before we move on, the curious might wonder if ‘‘categories are graphs’’
then what is the analgoue to ‘‘\(X\) are hypergraphs’’,
it is <a href="http://arxiv.org/PS_cache/math/pdf/0305/0305049v1.pdf#page=178">multicategories</a>.
</p>

<p>
Now the remainder of these notes is to build-up the material
needed to realise the notion of ‘free’ which is, in some sense,
<i>the best-possible approximate inverse</i> to ‘forgetful’
&#x2013;however, forgetting is clearly not invertible since it can easily
confuse two categories as the same graph!
</p>
</div>
</div>
</div>

<div id="outline-container-org29dd6b5" class="outline-2">
<h2 id="How-natural-is-naturality"><span class="section-number-2">6</span> <i>How natural is naturality?</i></h2>
<div class="outline-text-2" id="text-How-natural-is-naturality">
<p>
Recall, that a natural transformation \(η : F \natTo G\) is a family
\(∀ \{X \,:\, \Obj 𝒞\} \,→\, F\, X ⟶ G\, X\) that satisfies the naturality condition:
\(∀ \{A \; B\} \{f \,:\, A ⟶ B\} \,→\, F f ⨾ η {B} \;≡\; η {A} ⨾ G f\).
</p>

<ul class="org-ul">
<li>In the type of η, note that the first <i>show</i> arrow ‘→’ acts as a separator from the
the ∀-quantified variable \(X\), whereas the second <i>longer</i> arrow ‘⟶’ denotes the
morphism type in the category 𝒞.</li>

<li>We will freely interchange the informal mathematical rendition \((η_x : F\, X → G\, X)_{x ∈ \Obj 𝒞}\)
with the aforementioned formal Agda forms <code>∀{X : Obj 𝒞} → F X → G → X</code> and invocation <code>η {X}</code>.</li>
</ul>

<div class="org-center">
<p>
\(\require{AMScd}\)
</p>
\begin{CD}
\color{navy}{F\, A} @>\color{fuchsia}{η_A}>>      \color{teal}{G\, A}    \\
@V\color{navy}{F\, f}VV    \!=                    @VV\color{teal}{G\, f}V \\
\color{navy}{F\, B} @>>\color{fuchsia}{η_B}>      \color{teal}{G\, B}
\end{CD}
</div>
<p>
Let us look at this from a few different
angles; in particular, <a href="http://mathoverflow.net/questions/56938/what-does-the-adjective-natural-actually-mean/56956">what does the adjective ‘natural’ actually mean?</a>
It’s been discussed on many forums and we collect a few of the key points here.
</p>
</div>

<div id="outline-container-orgb539a49" class="outline-3">
<h3 id="Identification-of-possible-paths-contraction-of-choices"><span class="section-number-3">6.1</span> Identification of possible paths &#x2014;contraction of choices</h3>
<div class="outline-text-3" id="text-Identification-of-possible-paths-contraction-of-choices">
<p>
Given two functors \(F , G\), for any object \(~x\) we obtain two objects \(F\, x\, , \, G\, x\) and so a morphism
from \(F\) to \(G\) ought to map such \(F\,x\) to \(G\, x\). That is, a morphsim of functors is a family  <br>
\(η \,:\, ∀ \{x : \Obj 𝒞\} \,→\, F \,x ⟶ G \,x\). Now for any \(f : a → b\) there are two ways to form a morphism
\(F\, a → G\, b\): \(F f ⨾ η \{b\}\) and \(η \{a\} ⨾ G\, f\). Rather than make a choice each time we want such
a morphism, we eliminate the choice all together by insisting that they are identical.
This is the naturality condition.
</p>

<p>
This is similar to when we are given three morphisms \(f : a → b , g : b → c , h : c → d\),
then there are two ways to form a morphism \(a → d\); namely \((f ⨾ g) ⨾ h\) and \(f ⨾ (g ⨾ h)\).
Rather than make a choice each time we want such a morphism, we eliminate the choice all together
by insisting that they are identical. This is the associativity condition for categories.
</p>

<p>
Notice that if there’s no morphism \(F\, x ⟶ G\, x\) for some \(x\), they by definition there’s no
possible natural transformation \(F \natTo G\).
</p>
</div>
</div>

<div id="outline-container-org84d29b7" class="outline-3">
<h3 id="No-Choice-free-will-is-only-an-illusion"><span class="section-number-3">6.2</span> No Choice &#x2013;free will is only an illusion</h3>
<div class="outline-text-3" id="text-No-Choice-free-will-is-only-an-illusion">
\begin{align*}
     & \quad\text{the natural $X$}
\\ = & \quad\text{the $X$ which requires no arbitrary choices}
\\ = & \quad\text{the canonical/standard $X$}
\end{align*}

<p>
That is,
</p>
\begin{align*}
     & \quad \text{it is a natural construction/choice}
\\ = & \quad \text{distinct people would arrive at the same construction;}
\\   & \quad \text{ (no arbitrary choice or cleverness needed) }
\\ = & \quad \text{ there is actually no choice, i.e., only one possiility, }
\\   & \quad \text{ and so two people are expected to arrive at the same ‘choice’}
\end{align*}

<p>
Thus, if a construction every involves having to decide between distinct routes, then chances are
the result is not formally natural.
Sometimes this ‘inution’ is developed from working in a field for some time;
sometimes it just “feel”" natural.
</p>

<p>
<a href="http://math.stackexchange.com/questions/939404/do-natural-transformations-make-god-given-precise?rq=1">Some would even say</a>: <i>Natural = God-given</i>.
</p>
</div>
</div>

<div id="outline-container-org126aef8" class="outline-3">
<h3 id="Natural-means-polymorphic-without-type-inspection"><span class="section-number-3">6.3</span> Natural means polymorphic without type inspection</h3>
<div class="outline-text-3" id="text-Natural-means-polymorphic-without-type-inspection">
<p>
A natural transformation can be thought of as a polymorphic function
<code>∀ {X} → F X ⟶ G X</code> <i>where</i> we restrict ourselves to avoid inspecting any <code>X</code>.
</p>

<ul class="org-ul">
<li>Recall that a <code>mono</code>-morphic operation makes no use of type variables in its signature,
whereas a <code>poly</code>-morphic operation uses type variables in its signature.</li>

<li>For example, in C# one can ask if one type <code>is</code> a subclass of another thereby
obtaining specific information, whereas there is no such mechanism in Haskell.</li>
</ul>

<p>
Inspecting type parameters or not leads to the distinction of ad hoc plymorphism vs. parametric
polymorphism &#x2014;the later is the kind of polymorphism employed in functional language like Haskell
and friends and so such functions are natural transformations by default!
<a href="http://ecee.colorado.edu/ecen5533/fall11/reading/free.pdf">Theorems for free!</a>
</p>

<p>
For example,
</p>
<pre class="example">
-- Let 𝒦 x y ≔ Id {x} for morphisms, and 𝒦 x y ≔ x for objects.

size : ∀ {X} → List X → 𝒦 ℕ X
size [x₁, …, xₙ] = n
</pre>
<p>
is a polymorphic function and so naturality follows and is easily shown &#x2013;show it dear reader!
So we have always have
\[List\; f \;⨾\; size \quad=\quad size\]
Since <code>𝒦 ℕ f = Id</code>, then by extensionality: <code>size : List ⟶̇ 𝒦</code>.
</p>
<p>
On the other hand, the polymorphic function
</p>
<pre class="example">
whyme : ∀ {X} → List X → 𝒦 Int X
whyme {X} [x₁,…,xₙ] = If X = ℕ then 1729 else n
</pre>
<p>
is not natural: The needed equation <code>F f ⨾ η {B} = η {A} ⨾ G f</code>
for any <code>f : A → B</code> breaks as witnessed by
<code>f = (λ x → 0) : ℝ → ℕ</code> and any list with length <code>n ≠ 1729</code>,
and this is easily shown &#x2013;do so!
</p>
<p>
One might exclaim, <i>hey! this only works ’cuz you’re using Ramanujan’s taxi-cab number!</i>
<i>1729 is the smallest number expressible as a sum of 2 cubes in 2 ways:</i>
<i>\(1729 = 12³ + 1³ = 10³ + 9 ³\).</i> I assure you that this is not the reason that naturality breaks,
and I commend you on your keen observation.
</p>

<p>
Notice that it is natural if we exclude the type inspected, ℕ.
That is, if we only consider \(f : A → B\) with \(A ≠ ℕ ≠ B\).
In general, is it the case that a transformation can be made natural by excluding
the types that were inspected?
</p>

<p>
Before we move on, observe that a solution in \(h\) to the absorptive-equation \(F f ⨾ h = h\)
is precisely a natural transformation from \(F\) to the aforementioned ‘diagonal functor’:
\[F f ⨾ h \;=\; h \qquad⇔\qquad ∃ X : Obj \;•\; h ∈ F \overset{.}{⟶} 𝒦 X ~\]
</p>

<p>
In particular, due to the constant-fusion property \(g \,⨾\, 𝒦\, e \;=\; 𝒦\, e\), we have that
\[∀ \{F\} \{X\} \{e \,:\, X\} \;→\; (𝒦\, e) \,∈\, F \overset{.}{⟶} 𝒦\, X \]
Is the converse also true? If \(h ∈ F ⟶̇ 𝒦 X\) then \(h \,=\, 𝒦\, e\) for some \(e\)?
</p>
</div>
</div>

<div id="outline-container-org6bdd5ea" class="outline-3">
<h3 id="Natural-means-no-reference-to-types"><span class="section-number-3">6.4</span> Natural means no reference to types</h3>
<div class="outline-text-3" id="text-Natural-means-no-reference-to-types">
<p>
The idea that a natural transformation cannot make reference to the type variable at all can be
seen by yet another example.
</p>

<pre class="example">
  data 𝟙 : Set where ★ : 𝟙

  -- Choice function: For any type X, it yields an argument of that type.
  postulate ε : (X : Set) → X

  nay : ∀ {X} → X → X
  nay {X} _ = ε X
</pre>

<p>
Now naturality \(\Id \, f ⨾ nay_B \;=\; nay_A ⨾ \Id \, f\) breaks as witnessed by
\(f \;=\; (λ _ → εℕ + 1) \;:\; 𝟙 → ℕ\) &#x2013;and provided \(εℕ ≠ 0\), otherwise
we could use an \(f\) with no fix points.
</p>

<p>
From this we may hazard the following:
If we have natural transformations \(ηᵢ \,:\, ∀ {X : Objᵢ} →\, F X \overset{.}{⟶} G X\)
where the \(Objᵢ\) partition the objects available &#x2014; i.e., \(Obj \;=\; Σ i \,•\, Objᵢ\) &#x2014;
then the transformation \(η_{(i, X)} \;=\; ηᵢ\) is generally unnatural since it clearly makes choices,
for each partition.
</p>
</div>
</div>

<div id="outline-container-org9a860aa" class="outline-3">
<h3 id="Natural-means-uniformly-and-simultaneously-defined"><span class="section-number-3">6.5</span> Natural means uniformly and simultaneously defined</h3>
<div class="outline-text-3" id="text-Natural-means-uniformly-and-simultaneously-defined">
<p>
A family of morphisms is <i>natural in x</i> precisely when it is defined
<i>simultaneously</i> for all <i>x</i> &#x2014;there is no inspection of some particular <i>x</i> here and there,
no, it is uniform! With this view, the naturality condition is thought of as a ‘simultaneity’
condition. <a href="https://www.google.ca/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8&amp;client=ubuntu#q=general%20theory%20of%20natural%20equivalences">Rephrasing GToNE</a>.
</p>

<p>
The idea of naturality as uniformly-definable is pursued by <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.107.2336&amp;rep=rep1&amp;type=pdf">Hodges and Shelah</a>.
</p>
</div>
</div>

<div id="outline-container-org9e841a7" class="outline-3">
<h3 id="Naturality-is-restructure-modify-commutativity"><span class="section-number-3">6.6</span> Naturality is restructure-modify commutativity</h3>
<div class="outline-text-3" id="text-Naturality-is-restructure-modify-commutativity">
<p>
Recall that a functor can be thought of as endowing an object with structure.
Then a transformation can be thought of as a restructuring operation and naturality means
that it doesn’t matter whether we restructure or modify first, as long as we do both.
</p>
</div>
</div>

<div id="outline-container-org0886c90" class="outline-3">
<h3 id="Natural-means-obvious"><span class="section-number-3">6.7</span> Natural means obvious</h3>
<div class="outline-text-3" id="text-Natural-means-obvious">
<p>
It may help to think of <i>there’s a natural transformation from F to G</i> to mean
<i>there’s an obvious/standard/canconical way to transform F structure into G structure</i>.
</p>

<p>
Likewise, <i>F is naturally isomorphic to G</i> may be read <i>F is obviously isomorphic to G</i>.
For example, <b>TODO</b> seq-pair or pair-seq ;-)
</p>

<p>
Sometimes we can show ‘‘F X is isomorphic to G X, if we make a choice dependent on X’’
and so the isomorphism is not obvious, since a choice must be made.
</p>
</div>
</div>

<div id="outline-container-org244fe3e" class="outline-3">
<h3 id="Naturality-is-promotion"><span class="section-number-3">6.8</span> Naturality is promotion</h3>
<div class="outline-text-3" id="text-Naturality-is-promotion">
<ul class="org-ul">
<li>I think Richard Bird refers to the naturality condition as a promotion law where the functors
involved are thought of as (list) constructions.</li>

<li>The nomenclature is used <a href="https://www.cs.ox.ac.uk/files/3378/PRG56.pdf">to express the idea than operation on a compound structure
can be ‘promoted’ into its components.</a></li>

<li>Reading <code>F f ⨾ η {B} = η {A} ⨾ G f</code> from left to right:
Mapping \(f\) over a complicated structure then handling the result
<i>is the same as</i>
handling the complex datum then mapping \(f\) over the result.

<ul class="org-ul">
<li>`Handling' can be thought of as `processing' or as `reshaping'.</li>
</ul></li>
</ul>

<p>
Lists give many examples of natural transformations by considering
<a href="https://link.springer.com/chapter/10.1007/3-540-51305-1_24">a categorical approach to the theory of lists.</a>
</p>
</div>
</div>

<div id="outline-container-orgc3df58f" class="outline-3">
<h3 id="Naturality-as-a-rewrite-rule"><span class="section-number-3">6.9</span> Naturality as a rewrite rule</h3>
<div class="outline-text-3" id="text-Naturality-as-a-rewrite-rule">
<p>
The naturality condition can be seen as a rewrite rule that let’s us replace a complicated or
inefficient side with a simplier or more efficient yet equivalent expression.
I think I first learned this view of equations at the insistence of
<a href="https://www.amazon.com/Algebra-Programming-Prentice-hall-International-Computer/dp/013507245X">Richard Bird and Oege de Moor</a>
&#x2013;whose text can be found <a href="http://themattchan.com/docs/algprog.pdf">here</a>, albeit the legitimacy of the link may be suspect.
</p>

<p>
For example, recall the 𝒦onstant functor now construed only as a polymorphic binary operation:
</p>
<pre class="example">
_⟪_    :  ∀{A B : Set} → A → B → A
a ⟪ b  =  a
</pre>

<p>
The above is a constant time operation, whereas the next two are linear time operations; i.e.,
they take <code>n</code> steps to compute, where <code>n</code> is the length of the given list.
</p>

<pre class="example">
-- This' map for List's; i.e., the mor of the List functor
map    : ∀ {A B : Set} (f : A → B) → List A → List B
map f []         =  []
map f (x ∷ xs)  =  f x ∷ map f xs

-- Interpret syntax `x₀∷⋯∷xₙ₋₁` semantically as `x₀ ⊕ ⋯ ⊕ xₙ₋₁`, where ⊕ = cons.
fold  : ∀ {A B : Set} (cons : A → B → B) (nil : B) → List A → B
fold cons nil [] = nil
fold cons nil (x ∷ xs) = cons x (fold cons nil xs)
</pre>

<p>
By <i>Theorems for Free</i>, or a simple proof, we have that <code>fold</code> is a natural
transformation \(List \overset{.}{→} \Id\):
\[ List\; f \;⨾\; fold \; cons_B \; nil_B \qquad=\qquad fold \; cons_A \; nil_A \;⨾\; f \]
Note that here we are ranging over objects \(X\) equipped with \(nil_X : X, \; cons_X : X → X → X\);
as such the equation is not valid when this is not the case.
</p>

<p>
Now we map compute,
</p>
<pre class="example">
postulate A B : Set
postulate nil-B : B
postulate f : A → B -- possibly expensive operation

head  :  List B → B
head  =  fold _⟪_ nil-B

compute  :  List A → B
compute  =  map f  ⨾  head
</pre>

<p>
That is,
</p>
<pre class="example">
  compute [x₀, …, xₙ₋₁]
= head (map f [x₀, …, xₙ₋₁])
= head [f x₀, …, f xₙ₋₁]
= f x₀  ⟪  f x₁ ⟪ ⋯ ⟪ ⋯ f xₙ₋₁ ⟪ nil-B
= f x₀
</pre>

<p>
However this approach performs the potentially expensive operation \(f\) a total of
\(n = \text{“length of input”}\) times! In spite of that, it only needs the first element of
the list and performs the operation only once! Indeed, by the naturality of <code>fold</code> we have
an equivalent, and more efficient, formulation:
</p>
<pre class="example">
compute  =  head  ⨾  f
</pre>

<p>
This operation only performs the potentially costly <code>f</code> once!
</p>

<p>
A more concrete and realistic example is to produce an efficient version of the function
that produces the <code>average xs = div (sum xs, length xs)</code> of a list of numbers: This operation
traverses the input list twice, yet we can keep track of the length as we sum-along the list
to obtain an implementation that traverses the list only once!
</p>

<p>
<a href="https://scss.tcd.ie/publications/tech-reports/reports.99/TCD-CS-1999-74.pdf">Indeed</a>,
</p>
<pre class="example">
div : ℕ × ℕ → ℕ
div (0, 0) = 0
div (m, n) = m ÷ n

average     :  List ℕ → ℕ
average xs  =  div (fold _⊕_ 𝟘 xs)
  where  𝟘 = (0 , 0)
         _⊕_  : ℕ → (ℕ × ℕ) → ℕ
         a ⊕ (b , n) = (a + b , n + 1)
</pre>
</div>
</div>

<div id="outline-container-orgf6be294" class="outline-3">
<h3 id="Naturality-is-just-model-morphism"><span class="section-number-3">6.10</span> Naturality is just model morphism</h3>
<div class="outline-text-3" id="text-Naturality-is-just-model-morphism">
<p>
Given two functors \(F , G : 𝒞 ⟶ 𝒟\) let us construe them as only graph homomorphisms.
Then each is a model of the graph \(𝒰₀ \; 𝒞\) &#x2014;each intereprets the nodes and edges of <code>𝒰₀ 𝒞</code> as
actual objects and morphisms of 𝒟&#x2014; and a natrual transformation is then nothing
more than a morphism of models.
</p>
</div>
</div>

<div id="outline-container-org0355542" class="outline-3">
<h3 id="Naturality-yields-pattern-matching"><span class="section-number-3">6.11</span> Naturality yields pattern matching</h3>
<div class="outline-text-3" id="text-Naturality-yields-pattern-matching">
<p>
In the setting of types and functions, <code>η : F ⟶̇ G</code> means we have <code>η (F f x) = G f (η x)</code>
which when read left-to-right says that <code>η</code> is defined by pattern-matching on its argument
to obtain something of the form <code>F f x</code> then it is defined recursively by examining <code>x</code> and then
applying <code>G f</code> to the result &#x2014;of course there’s some base case <code>F</code> definitions as well.
</p>

<p>
Alternatively, the input to <code>η</code> is of the form <code>F …</code> and its
output is of the form <code>G …</code> &#x2013;mind blown!
</p>

<p>
For example, I want to define a transformation \(\mathsf{List} ⟶̇ \mathsf{List}\).
</p>
<ol class="org-ol">
<li>So let me suppose the input is of the shape \(\List \, f\, x\), more concretely
it is of the shape
 <br> <code>[f x₀, f x₁, …, f xₙ₋₁]</code> &#x2013;for arbitrary \(f : A → B\).</li>
<li>Then the output shape must be \(\List \, f\, (η \, x)\), more concretely
it is of the shape  <br> <code>[f y₀, f y₁, …, f yₘ₋₁]</code> where \(y \,=\, η\,x\).</li>
<li><p>
So my <i>only</i> choices are \(y : \List A\) and \(m : ℕ\)
</p>

<p>
Here are some possibilities and the resulting η:
</p>
<dl class="org-dl">
<dt>\(y, m = x, n\)</dt><dd>Identity function</dd>
<dt>\(y, m = x, 0\)</dt><dd>Constantly empty list <code>[]</code> function</dd>
<dt>\(y, m = x, 1\)</dt><dd>The first element, ‘head’, function</dd>
<dt>\(y, m = x, k\)</dt><dd>The first \(k < n\) elements function</dd>
<dt>\(m = n\) with \(yᵢ = xₙ₋ᵢ\)</dt><dd>List reversal function</dd>
<dt>\(y, m = \mathsf{reverse}(x), k\)</dt><dd>The last \(k < n\) elements, in reverse, function
<ul class="org-ul">
<li>Here we applied an already known natural transformation
and indeed the composition of naturally transformation is itself natural.</li>
</ul></dd>
</dl></li>
</ol>
</div>
</div>

<div id="outline-container-org0d72c72" class="outline-3">
<h3 id="Naturality-Examples"><span class="section-number-3">6.12</span> Examples</h3>
<div class="outline-text-3" id="text-Naturality-Examples">
<dl class="org-dl">
<dt>Pointwise Monotonicity</dt><dd>A functor among poset categories is an order-preserving function and a natural transformation
\(f \natTo g\) is a proof that \(f \overset{.}{≤} g\) pointwise: \(∀ x \,•\, f\, x \;≤\; g\, x\)
&#x2014;all the other pieces for a natural
transformation are automatic from the definition of begin a poset category.</dd>

<dt>conjugation</dt><dd><p>
A functor among monoids &#x2013;construed as categories&#x2013; is just a monoid homomorphism:
</p>
\begin{align*}
           & (M, ⊕, e) ⟶ (N, ⊗, d)
{{{newline}}} ≅ \quad & Σ h ∶ M → N • ∀ \{x \, y \} •\; h(x ⊕ y) = h x ⊗ h y \lands h e = d
\end{align*}
<p>
A natural transformation <code>(f, prf) ⟶ (g, prf’)</code> is a point \(n : N\) with
\(∀ x ∶ M \;•\; f x ⊗ n \,=\, n ⊗ g x\), a so-called ‘conjugation’ by \(n\) that takes \(f\) to \(g\).
</p></dd>

<dt>fold</dt><dd><p>
Recall from the introduction \(𝒰(S, ⊕, e) \;=\; S\) was the underlying functor from monoids to sets.
</p>

<p>
Let \(𝒰 × 𝒰\) be the functor that for objects \(M \;↦\; 𝒰\, M \,×\, 𝒰\, M\) and for morphisms
\(h \;↦\; λ (x,y) → (h\, x, h\, y)\). Then the monoid multiplication (of each monoid) is a natural
transformation \(𝒰 × 𝒰 \natTo 𝒰\), where naturality says that for any monoid homomorphism \(h\), the
application of \(𝒰\, h\) to the (monoid) multiplication of two elements is the same as the
(monoid) multiplication of the \(𝒰\, h\) images of the two elements,
and this is evident from the homomorphism condition.
</p>

<p>
Extending to finite products, \(ℒ \;≔\; (Σ n ∶ ℕ • ∏ i ∶ 1..n • 𝒰)\), the natural transformation
\(ℒ \natTo 𝒰\) is usually called <i>fold, reduce, or cata</i> and <code>ℒ</code> is known as the
<i>free monoid functor</i> with notations \(A* \;=\; \List A \;=\; ℒ\, A\).
</p>

<p>
Loosely put,
</p>
<pre class="example">
    ℒ₀    :  Monoid → Set
    ℒ₀ M  =  Σ n ∶ ℕ • ∏ i : 1..n • 𝒰 M   -- finite sequences of elements from M

    ℒ₁ : ∀ {M N : Monoid} → (M ⟶ N) → ℒ₀ M → ℒ₀ N
    ℒ₁ (h , prf) = λ (n , x₁, …, xₙ) → (n , h x₁ , … , h xₙ)

    fold : ∀ {M : Monoid} → ℒ₀ M → 𝒰₀ M
    fold {(M, ⊕, e)} = λ (n , x₁, …, xₙ) → x₁ ⊕ ⋯ ⊕ xₙ
</pre>

<p>
&#x2013;The reader would pause to consider implementing this formally using Agda's <code>Data.Fin</code> and <code>Data.Vec</code> ;-)&#x2013;
</p>

<p>
Now for any monoid homomorphism <code>h</code>, applying induction, yields
</p>
<pre class="example">
    h₀(x₁ ⊕ ⋯ ⊕ xₙ)  =  h₀ x₁ ⊕ ⋯ ⊕ h₀ xₙ  where  h₀ = 𝒰 (h₀, prf) = 𝒰 h
</pre>
<p>
Which is easily seen to be just naturality &#x2013; if we use backwards composition \(f ⨾ g \;=\; g ∘ f\) &#x2013;
</p>
<pre class="example">
    𝒰 h ∘ fold {M}  =  fold {N} ∘ ℒ h
</pre>
<p>
Woah!
</p></dd>

<dt>Every operation in any multisorted algebraic structure gives a natural transformation</dt><dd><p>
This is mentioned in the <a href="http://www.math.mcgill.ca/triples/Barr-Wells-ctcs.pdf">Barr and Wells' <i>Category Theory for Computing Science</i> text</a>, citing
Linton, 1969a-b.
</p>

<p>
For example, <code>src, tgt</code> &#x2014;from the graph signature&#x2014; give natural transformations
\(V \natTo E\) from the vertex functor to the edge functor &#x2026; keep reading ;)
</p></dd>

<dt>Representability</dt><dd><p>
Recall that \(V(G)\) is essentially \(ℙ₀ ⟶ G\) where
\(ℙₙ\) is the graph of \(n\) edges on \(n+1\) vertices named \(0..n\) with typing \(i \,:\, i-1 ⟶ i\),
which I like to call <i>the path graph of length n</i>; and in particular \(ℙ₀\) is the graph of
just one dot, called 0, and no edges. &#x2014;Earlier I used the notation <code>[n]</code>, but I’m using \(ℙ\) since
I like the view point of ℙaths.
</p>

<p>
What does it mean to say that <i>V(G) is essentially ℙ₀ ⟶ G</i>?
</p>

<p>
It means that the vertices functor
&#x2013; \(𝒱 \;:\; 𝒢𝓇𝒶𝓅𝒽 ⟶ 𝒮ℯ𝓉\) that takes objects \(G ↦ V(G)\) and morphisms \(h ↦ \mathsf{ver}\, h\) &#x2013;
can be ‘represented’ as the Hom functor \((ℙ₀ ⟶ \_{})\), that is to say
\[𝒱 \quad≅\quad (ℙ₀ ⟶ \_{}) \;\mathsf{within \; Func} \; 𝒢𝓇𝒶𝓅𝒽 \; 𝒮ℯ𝓉\]
--<code>Func</code>-tor categories will be defined in the next section!&#x2013;
</p>

<p>
Notice that we arrived at this expression by
‘eta-reducing’ the phrase <i>V(G) is essentially ℙ₀ ⟶ G</i>! ;)
</p>

<p>
More generally, we have the functor \(ℙₙ ⟶ \_{}\) which yields all paths of length \(n\)
for a given graph.
</p>

<p>
Observe &#x2013;i.e., show&#x2013; that we also have an edges functor.
</p></dd>
</dl>
</div>
</div>
</div>
<div id="outline-container-org8ec0e2f" class="outline-2">
<h2 id="Functor-Categories"><span class="section-number-2">7</span> <i>Functor Categories</i></h2>
<div class="outline-text-2" id="text-Functor-Categories">
<p>
With a notion of morphisms between functors, one is led inexorably to ask
whether functors as objects and natural transformations as morphisms constitute
a category?
They do!
However, we leave their definition to the reader &#x2014;as usual, if the reader is ever so desperate
for solutions, they can be found as comments in the unruliness that is the source file.
</p>
<pre class="example">
 instance
  Func       :  ∀ {i j i’ j’} (𝒞 : Category {i} {j}) (𝒟 : Category {i’} {j’}) → Category _
  Func 𝒞 𝒟  =  {! exercise !}
</pre>

<ul class="org-ul">
<li>A hint: The identity natural transformation is the obvious way to get from \(F\, X\) to \(F\, X\),
for any \(X\) given \(F\) &#x2014;well the only way to do so, without assuming anything else about the
functor \(F\), is simply \(\Id_{F X}\). This is the ‘natural’ choice, any other choice would be
‘unnatural’ as it would require some ‘cleverness’.</li>

<li>Another hint: The obvious way to define \(η ⨾ γ\) to get \(F\, X ⟶ H\, X\) from
\(F\, X ⟶ G\, X ⟶ H\, X\) is composition of morphisms in the category!
That is, pointwise composition. Nothing ‘clever’, just using the obvious candidates!</li>
</ul>

<p>
This is a good exercise as it will show you that there is an identity functor and that composition of functors
is again a functor. Consequently, functors are in abundance: Given any two, we can form [possibly] new ones by composition.
</p>

<p>
It is a common construction that when a type \(Y\) is endowed with some structure, then we can endow
the function space \(X → Y\), where \(X\) is any type, with the same structure and we do so
‘pointwise’. This idea is formalised by functor categories.
Alternatively, one can say we have ‘categorified’ the idea; where
<i>categorification</i> is the process of replacing types and functions with categories and
functors and possibly adding some coherence laws.
</p>

<p>
There are people who like to make a show about how ‘big’ 𝒞𝒶𝓉 or <code>Func 𝒞 𝓓</code> are;
these people adhere to something called ‘set theory’ which is essentialy type theory but
ignoring types, loosely put they work only with the datatype
</p>
<pre class="example">
data SET : Set where
  Elem : ∀ {A : Set} → A → SET
</pre>
<p>
Such heathens delegate types-of-types into ‘classes’ of ‘small’ and ‘big’ sets and it’s not
uniform enough for me.
Anyhow, such people would say that functor categories ‘‘cannot be constructed (as sets)’’ unless
one of the categories involved is ‘‘small’’. Such shenanigans is ignored due to the hierarchy of
types we are using :-)
</p>

<p>
We must admit that at times the usage of a single type, a ‘uni-typed theory’ if you will can be
used when one wants to relise types in an extrinsic fashion rather than think of data as
intrinsically typed &#x2013;E.g., graphs with <code>src, tgt</code> <i>then</i> deriving a notion of ‘type’ with <code>_⟶_</code>.
Everything has its place &#x2026; nonetheless, I prefer (multi)typed settings!
</p>
</div>

<div id="outline-container-org9eff4c9" class="outline-3">
<h3 id="FunctorCats-Examples"><span class="section-number-3">7.1</span> Examples</h3>
<div class="outline-text-3" id="text-FunctorCats-Examples">
</div>

<div id="outline-container-orge6c82d5" class="outline-4">
<h4 id="All-Categories-are-Functor-Categories"><span class="section-number-4">7.1.1</span> All Categories are Functor Categories</h4>
<div class="outline-text-4" id="text-All-Categories-are-Functor-Categories">
<p>
Let <code>𝟙 ≔ [ • ]</code> be the discrete category of one object (and only the identity arrow on it).
</p>

<p>
Then <code>𝒞 ≅ Func 𝟙 𝒞</code>.
</p>
</div>
</div>

<div id="outline-container-orgecdfb7a" class="outline-4">
<h4 id="Powers-of-Categories-are-Functor-Categories"><span class="section-number-4">7.1.2</span> Powers of Categories are Functor Categories</h4>
<div class="outline-text-4" id="text-Powers-of-Categories-are-Functor-Categories">
<p>
Let <code>𝟚₀ ≔ [• •]</code> be the discrete category of two objects.
  Then the <i>𝒞-squared</i> category can be defined <code>𝒞 ⊗ 𝒞 ∶≅ Func 𝟚₀ 𝒞</code>:
  This category essentially consists of pairs of 𝒞-objects with pairs of 𝒞-arrows
  between them.
</p>

<p>
The subscript 0 is commonly used for matters associated with objects and
the name <code>𝟚₀</code> is suggestive of the category of 2 objects only.
</p>

<p>
More generally, if 𝒩 is the discrete category of \(n\) objects, then
the <i>n</i>-fold product category is defined by
<code>(∏ i ∶ 1..n • 𝒞) ∶≅ Func 𝒩 𝒞</code>.
</p>

<p>
These are also commonly denoted \(𝒞^2\) and \(𝒞^𝒩\) since they are essentially
products, and more generally <code>Func 𝒳 𝒴</code> is also denoted 𝒴<sup>𝒳</sup> and referred.
</p>
</div>
</div>

<div id="outline-container-org3696c87" class="outline-4">
<h4 id="Arrow-Categories"><span class="section-number-4">7.1.3</span> Arrow Categories</h4>
<div class="outline-text-4" id="text-Arrow-Categories">
<p>
We can add an arrow to <code>𝟚₀</code> to obtain another category&#x2026;
</p>

<p>
Let <code>𝟚 ≔ • ⟶ •</code> be the category of two objects, call them 0 and 1, with one arrow between them.
  Then a functor <code>𝟚 ⟶ 𝒞</code> is precisely a morphism of 𝒞 and a natural transformation
<code>f ⟶ g</code> boils down to just a pair of morphisms <code>(h,k)</code> with <code>h ⨾ g = f ⨾ k</code>.
</p>

<p>
Hence, the <i>arrow category of 𝒞</i> is \(𝒞^𝟚 \;≅\; 𝒞^→ \;≅\; \mathsf{Func}\, 𝟚 𝒞\);
which is essentially the category with objects being 𝒞-morphisms and morphisms being <i>commutative squares</i>.
</p>

<p>
Notice that a functor can be used to
</p>
<ul class="org-ul">
<li><i>select</i> two arbitrary 𝒞 objects &#x2013;if it's source is 𝟚₀</li>
<li><i>select</i> two arbitrary 𝒞 objects with a 𝒞 arrow between them &#x2013;if it's source is 𝟚</li>
<li><i>select</i> an arbitrary 𝒞 arrow &#x2013;if it's source is 𝟚</li>
</ul>

<p>
Likewise, a natural transformation can be used to <i>select</i> a commutative diagram.
</p>
</div>
</div>

<div id="outline-container-org0a44123" class="outline-4">
<h4 id="Understand-𝒞-by-looking-at-Functor-Categories"><span class="section-number-4">7.1.4</span> Understand 𝒞 by looking at Functor Categories</h4>
<div class="outline-text-4" id="text-Understand-𝒞-by-looking-at-Functor-Categories">
<p>
It is a common heuristic that when one suspects the <i>possibility</i> of a category <code>𝒞</code>, then one
can make <i>probes</i> to discover its structure. The objects are just functors <code>𝟙 ⟶ 𝒞</code> and the
morphisms are just functors <code>𝟚 ⟶ 𝒞</code>.
</p>
</div>
</div>

<div id="outline-container-orgd0639dc" class="outline-4">
<h4 id="Presheaves-delegating-work-to-𝒮ℯ𝓉"><span class="section-number-4">7.1.5</span> Presheaves &#x2013; delegating work to 𝒮ℯ𝓉</h4>
<div class="outline-text-4" id="text-Presheaves-delegating-work-to-𝒮ℯ𝓉">
<p>
The <i>category of presheaves of 𝒞</i> is the category <code>PSh 𝒞 ≔ Func (𝒞 ᵒᵖ) 𝒮e𝓉</code>.
</p>

<p>
This is a pretty awesome category since it allows nearly all constructions in 𝒮ℯ𝓉 to be
realised! Such as subsets, truth values, and even powersets! All these extra goodies make it
a ‘topos’ aka ‘power allegory’ &#x2014;the first is a category that has all finite limits and
a notion of powerset while the second, besides the power part, looks like a totally different beast;
the exhilaration!
</p>
</div>
</div>

<div id="outline-container-org0f0c084" class="outline-4">
<h4 id="Slice-Categories"><span class="section-number-4">7.1.6</span> Slice Categories</h4>
<div class="outline-text-4" id="text-Slice-Categories">
<p>
The <i>slice category of 𝒞 over B : Obj 𝒞</i> is the category <code>𝒞 / B ≔ Σ F ∶ Func 𝟚 𝒞 • (F 1 = B)</code>.
</p>

<p>
Essentially it is the category of objects being 𝒞-morphisms with target \(B\)
and morphisms \(f ⟶ g\) being \((h,k)\) with \(h ⨾ g = f ⨾ k\) but a natural choice for
\(k : B ⟶ B\) is \(\Id_B\) and so we can use morphism type
\((f ⟶’ g) \;≔\; Σ h : \src f ⟶ \src g \;•\; h ⨾ g = f\).
</p>

<p>
This is seen by the observation \[(h, k) \;∈\; f ⟶ g \qquad⇔\qquad h \;∈\; (f ⨾ k) ⟶’ g\]
Of course a formal justification is obtained by showing
\[\_{}⟶\_{} \quad≅\quad \_{}⟶’\_{} \quad \mathsf{within \; Func }\; (𝒞 ᵒᵖ ⊗ 𝒞) 𝒮e𝓉 \]
&#x2026;which I have not done and so may be spouting gibberish!
</p>

<p>
Just as the type <code>Σ x ∶ X • P x</code> can be included in the type <code>X</code>, by forgetting the second
component, so too the category <code>Σ F ∶ 𝟚 ⟶ 𝒞 • F 1 ≈ B</code> can be included into the category
𝒞 and we say it is a <i>subcategory</i> of 𝒞.
</p>

<p>
The notation <code>Σ o ∶ Obj 𝒞 • P o</code> defines the subcategory of 𝒞 obtained by deleting
all objects not satisfying predicate <code>P</code> and deleting all morphisms incident to such objects; i.e.,
it is the category 𝒟 with
\[ \Obj 𝒟 \quad≡\quad Σ o ∶ \Obj 𝒞 \,•\, P o
   \qquad\text{ and }\qquad
   (o , prf) ⟶_𝒟 (o' , prf') \quad≡\quad o ⟶_𝒞 o'
\]
This is the largest/best/universal subcategory of 𝒞 whose objects satisfy \(P\).
 <br> Formalise this via a universal property ;)
</p>
</div>
</div>

<div id="outline-container-org2500424" class="outline-4">
<h4 id="Slices-of-𝒮e𝓉-are-Functor-Categories"><span class="section-number-4">7.1.7</span> Slices of <code>𝒮e𝓉</code> are Functor Categories</h4>
<div class="outline-text-4" id="text-Slices-of-𝒮e𝓉-are-Functor-Categories">
<p>
\[ \Func \; S \; 𝒮e𝓉  \qquad≅\qquad  𝒮e𝓉 / S \]
Where S in the left is construed as a discrete category and in the right
is construed as an object of 𝒮e𝓉.
</p>

<p>
This is because a functor from a discrete category need only be a function of objects since
there are no non-identity morphisms. That is, a functor \(f : S ⟶ 𝒮ℯ𝓉\)
is determined by giving a set \(f\,s\) for each element \(s ∈ S\) &#x2014;since there are no non-identity morphisms.
Indeed a functor \(f : S ⟶ Set\) yields an <i>S</i>-targeted
function
\[ (Σ s ∶ S \,•\, f\, s) → S  \quad:\quad λ (s , fs) → s \]
Conversely a function \(g : X → S\) yields a functor by sending elements to their pre-image sets:
\[ S ⟶ Set \quad:\quad λ s → (Σ x ∶ X \,•\, g\, x ≡ s)\]
</p>

<p>
Because of this example, <code>𝒞 / B</code> can be thought of as ‘𝒞-objects indexed by B’
&#x2013;extending this idea further leads to <i>fibred categories</i>.
</p>
</div>
</div>

<div id="outline-container-org5220ee5" class="outline-4">
<h4 id="Natural-transformations-as-functor-categories"><span class="section-number-4">7.1.8</span> Natural transformations as functor categories</h4>
<div class="outline-text-4" id="text-Natural-transformations-as-functor-categories">
<p>
In a similar spirit, we can identify natural transformations as functors!
\[\Func \, 𝒞 \, (𝒟 ^ 𝟚) \quad≅\quad (Σ F , G ∶ 𝒞 ⟶ 𝒟 \;•\; \mathsf{NatTrans}\, F\, G)\]
</p>

<p>
A functor \(N : 𝒞 ⟶ 𝒟 ^ 𝟚\) gives, for each object \(C : \Obj 𝒞\) an object in \(𝒟 ^ 𝟚\) which
is precisely an arrow in \(𝒟\), rewrite it as \(N_C : F\,C ⟶ G\,C\) where \(F\,C \,≔\, N\, C\, 0\)
and \(G\, C \,≔\, N\, C\, 1\).
</p>

<p>
Likewise, for each arrow \(f : A ⟶ B\) in 𝒞 we obtain an arrow \(N\, f \,:\, N\, A ⟶ N\, B\)
in \(𝒟 ^ 𝟚\) which is precisely a commutative square in 𝒟;
that is, a pair of 𝒟-arrows \((F\,f , G\,f) ≔ N\,f\)
with \(N_A ⨾ G\,f \;=\; F\,f ⨾ N_B\).
</p>

<p>
Notice that we have implicitly defined two functors \(F, G : 𝒞 ⟶ 𝒟\).
Their object and morphism mappings are clear, but what about functoriality?
We prove it for both \(F, G\) together.
</p>

<p>
<span class="underline">Identity:</span>
</p>
\begin{calc}
     (F \,\Id \, , \, G\, \Id)
\step{ definition of $F$ and $G$ }
     N \, \Id
\step{ $N$ is a functor }
     \Id \,∶\, 𝒟 ^ 𝟚
\step{ identity in arrow categories }
     (\Id , \Id)
\end{calc}

<p>
<span class="underline">Composition:</span>
</p>
\begin{calc}
     ( F (f ⨾ g) , G (f ⨾ g) )
   \step{ definition of $F$ and $G$ }
     N\, (f ⨾ g)
   \step{ $N$ is a functor }
     N\, f  ⨾  N\, g
   \step{ definition of $F$ and $G$ }
     (F\, f, G\, f) ⨾ (F\,g , G\,g)
   \step{ composition in arrow categories }
     (F\,f ⨾ F\,g , G\,f ⨾ G\,g)
\end{calc}

<p>
Sweet!
</p>

<p>
Conversely, given a natural transformation \(η : F \overset{.}{⟶} G\)
we define a functor \(N : 𝒞 ⟶ 𝒟 ^ 𝟚\) by sending objects \(C\) to \(η_C : F\, C ⟶ G\, C\),
which is an object is \(𝒟 ^ 𝟚\), and sending morphisms \(f : A ⟶ B\) to pairs \((G f , F f)\),
which is a morphism in \(𝒟 ^ 𝟚\) due to naturality of η; namely
\(η_A ⨾ G\, f \;=\; F\, f ⨾ η_B\).
It remains to show that \(N\) preserves identities and composition &#x2013;Exercise!
</p>

<p>
Now it remains to show that these two processes are inverses
and the isomorphism claim is complete. Woah!
</p>

<p>
Similarly, to show
\[ \Func\, (𝟚 ⊗ 𝒞) \, 𝒟 \qquad≅\qquad (Σ F₀ , F₁ ∶ 𝒞 ⟶ 𝒟 • \mathsf{NatTrans}\, F₁ \, F₂)\]
</p>

<p>
By setting \(H\, i \;=\; Fᵢ\) on objects and likewise for morphisms
but with \(H(\Id, 1) \;=\; η\) where \(1 : 0 ⟶ 1\) is the non-identity arrow of <code>𝟚</code>.
</p>

<p>
(Spoilers! Alternatively: <code>Arr (Func 𝒞 𝒟) ≅ 𝟚 ⟶ 𝒞 ^ 𝒟 ≅ 𝒞 × 𝟚 ⟶ 𝒟</code> since <code>𝒞𝒶𝓉</code> has exponentials,
   and so the objects are isomorphic; i.e., natural transformations correspond to functors <code>𝒞×𝟚⟶𝒟</code>)
</p>

<p>
Why are we mentioning this alternative statement? Trivia knowledge of-course!
</p>

<p>
On a less relevant note, if you’re familiar with the theory of stretching-without-tearing,
formally known as topology which is pretty awesome, then you might’ve heard of paths and
deformations of paths are known as homotopies which are just continuous functions
\(H : X × I ⟶ Y\) for topological spaces $X, Y,$ and \(I \,=\, [0,1]\) being the unit interval in ℝ.
Letting \(𝒥 = 𝟚\) be the ‘categorical interval’ we have that functors \(𝒞 × 𝒥 ⟶ 𝒟\)
are, by the trivia-relevant result, the same as natural transformations.
That is, <i>natural transformations extend the notion of homotopies, or path-deformations.</i>
</p>

<p>
On <a href="http://mathoverflow.net/a/75686/42716">mathoverflow</a>, the above is recast succinctly as:
   A natural transformation from \(F\) to \(G\) is a functor,
   targeting an arrow category, whose ‘source’
   is \(F\) and whose ‘target’ is \(G\).
   \[
       \hspace{-2em} F \overset{.}{⟶} G : 𝒞 ⟶ 𝒟 \quad≅\quad
       Σ η ∶ 𝒞 ⟶ \mathsf{Arr}\, 𝒟 •\; \mathsf{Src} ∘ η = F \;\;∧\;\; \mathsf{Tgt} ∘ η = G
   \]
   Where, the projection functors
</p>
\begin{align*}
   \mathsf{Src}&                              &:& \mathsf{Arr}\, 𝒟 ⟶ 𝒟
\\ \mathsf{Src}& (A₁ , A₂ , f)                &=& A₁
\\ \mathsf{Src}& (f  , g  , h₁ , h₂ , proofs) &=& h₁
\end{align*}
<p>
with \(\mathsf{Tgt}\) returning the other indexed items.
</p>
</div>
</div>
</div>

<div id="outline-container-orgfa7b797" class="outline-3">
<h3 id="Graphs-as-functors"><span class="section-number-3">7.2</span> Graphs as functors</h3>
<div class="outline-text-3" id="text-Graphs-as-functors">
<p>
We give an example of a functor by building on our existing graphs setup.
After showing that graphs correspond to certain functors, we then
mention that the notion of graph-map is nothing more than the associated
natural transformations!
</p>

<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #859900; font-weight: bold;">module</span> graphs<span style="color: #268bd2;">-</span>as<span style="color: #268bd2;">-</span>functors <span style="color: #859900; font-weight: bold;">where</span>
</pre>
</div>

<p>
Let us construct our formal graph category, which contains the ingredients for
a graph and a category and nothing more than the equations needed of a category.
The main ingredients of a two-sorted graph are two sort-symbols <code>E, V</code>, along with
two function-symbols <code>s, t</code> from <code>E</code> to <code>V</code> &#x2014;this is also called ‘the signature
of graphs’. To make this into a category, we need function-symbols <code>id</code> and a composition
for which it is a unit.
</p>
<div class="org-src-container">
<pre class="src src-haskell">  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">formal objects</span>
  <span style="color: #859900; font-weight: bold;">data</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> <span style="color: #859900; font-weight: bold;">where</span> <span style="color: #b58900; font-style: italic;">E</span> <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320;

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">formal arrows</span>
  <span style="color: #859900; font-weight: bold;">data</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320; <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320; <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> <span style="color: #859900; font-weight: bold;">where</span>
     s t <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321; <span style="color: #b58900; font-style: italic;">E</span> <span style="color: #b58900; font-style: italic;">V</span>
     id  <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {o} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321; o o

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">(forward) composition</span>
  fcmp <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {a b c} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321; a b <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321; b c <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321; a c
  fcmp f id <span style="color: #268bd2;">=</span> f
  fcmp id f <span style="color: #268bd2;">=</span> f
</pre>
</div>

<p>
Putting it all together,
</p>
<div class="org-src-container">
<pre class="src src-haskell">  <span style="color: #859900; font-weight: bold;">instance</span>
   <span style="color: #b58900; font-style: italic;">&#119970;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span>
   <span style="color: #b58900; font-style: italic;">&#119970;</span> <span style="color: #268bd2;">=</span> record
        { <span style="color: #b58900; font-style: italic;">Obj</span>     <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320;
        ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span>     <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321;
        ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span>     <span style="color: #268bd2;">=</span> fcmp
        ; assoc   <span style="color: #268bd2;">=</span> &#955; {a b c d f g h} <span style="color: #268bd2;">&#8594;</span> fcmp<span style="color: #268bd2;">-</span>assoc f g h
        ; <span style="color: #b58900; font-style: italic;">Id</span>      <span style="color: #268bd2;">=</span> id
        ; leftId  <span style="color: #268bd2;">=</span> left<span style="color: #268bd2;">-</span>id
        ; rightId <span style="color: #268bd2;">=</span> right<span style="color: #268bd2;">-</span>id
        }
    <span style="color: #859900; font-weight: bold;">where</span>
       <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">exercises: prove associativity, left and right unit laws</span>
</pre>
</div>

<p>
Now we can show that every graph <code>G</code> gives rise to a functor: A semantics of <code>𝒢</code> in <code>𝒮e𝓉</code>.
</p>
<div class="org-src-container">
<pre class="src src-haskell">  toFunc <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119970;</span> <span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span>
  toFunc <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">=</span> record
    { obj  <span style="color: #268bd2;">=</span> &#10214;<span style="color: #859900; font-weight: bold;">_</span>&#10215;&#8320;
    ; mor  <span style="color: #268bd2;">=</span> &#10214;<span style="color: #859900; font-weight: bold;">_</span>&#10215;&#8321;
    ; id   <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
    ; comp <span style="color: #268bd2;">=</span> &#955; {x y z f g} <span style="color: #268bd2;">&#8594;</span> fcmp<span style="color: #268bd2;">-&#10814;</span> {x} {y} {z} {f} {g}
    }
    <span style="color: #859900; font-weight: bold;">where</span>
      &#10214;<span style="color: #859900; font-weight: bold;">_</span>&#10215;&#8320; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119970;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span>
      &#10214; <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320;<span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">V</span> &#10215;&#8320; <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Graph.V</span> <span style="color: #b58900; font-style: italic;">G</span>
      &#10214; <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320;<span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">E</span> &#10215;&#8320; <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#931;</span> x <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">Graph.V</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#8226;</span> <span style="color: #b58900; font-style: italic;">&#931;</span> y <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">Graph.V</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#8226;</span> Graph._<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">G</span> x y

      &#10214;<span style="color: #859900; font-weight: bold;">_</span>&#10215;&#8321; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#10230;</span> y <span style="color: #268bd2;">&#8594;</span> (&#10214; x &#10215;&#8320; <span style="color: #268bd2;">&#8594;</span> &#10214; y &#10215;&#8320;)
      &#10214; s &#10215;&#8321; (src , tgt , edg) <span style="color: #268bd2;">=</span> src
      &#10214; t &#10215;&#8321; (src , tgt , edg) <span style="color: #268bd2;">=</span> tgt
      &#10214; id &#10215;&#8321; x <span style="color: #268bd2;">=</span> x

      <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Exercise: fcmp is realised as functional composition</span>
      fcmp<span style="color: #268bd2;">-&#10814;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y z} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321; x y} {g <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321; y z} <span style="color: #268bd2;">&#8594;</span> &#10214; fcmp f g &#10215;&#8321; <span style="color: #268bd2;">&#8801;</span> &#10214; f &#10215;&#8321; <span style="color: #268bd2;">&#10814;</span> &#10214; g &#10215;&#8321;
</pre>
</div>
<p>
Conversely, every such functor gives a graph whose vertices and edges are the sets
associated with the sort-symbols <code>V</code> and <code>E</code>, respectively.
</p>
<div class="org-src-container">
<pre class="src src-haskell">  fromFunc <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119970;</span> <span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Graph</span>
  fromFunc <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">=</span> record {
      <span style="color: #b58900; font-style: italic;">V</span>      <span style="color: #268bd2;">=</span> obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320;<span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">V</span>
    ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span>    <span style="color: #268bd2;">=</span> &#955; x y <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#931;</span> e <span style="color: #268bd2;">&#8758;</span> obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320;<span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">E</span> <span style="color: #268bd2;">&#8226;</span> src e <span style="color: #268bd2;">&#8801;</span> x <span style="color: #268bd2;">&#215;</span> tgt e <span style="color: #268bd2;">&#8801;</span> y
             <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">the type of edges whose source is x and target is y</span>
    }
    <span style="color: #859900; font-weight: bold;">where</span> tgt src <span style="color: #b58900; font-style: italic;">:</span> obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320;<span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">E</span> <span style="color: #268bd2;">&#8594;</span> obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8320;<span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">V</span>
          src <span style="color: #268bd2;">=</span> mor <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321;<span style="color: #268bd2;">.</span>s
          tgt <span style="color: #268bd2;">=</span> mor <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">&#119970;</span>&#8321;<span style="color: #268bd2;">.</span>t
</pre>
</div>

<p>
It is to be noted that we can define ‘‘graphs over 𝒞’’ to be the category <code>Func 𝒢 𝒞</code>.
Some consequences are as follows: Notion of graph in any category, the notion of graph-map
is the specialisation of natural transformation (!), and most importantly, all the power of functor categories
is avaiable for the study of graphs.
</p>

<p>
In some circles, you may hear people saying an ‘algebra over the signature of graphs’ is an interpretation
domain (<code>𝒞</code>) and an operation (<code>Functor 𝒢 𝒞</code>) interpreting the symbols. <i>Nice!</i>
</p>
</div>
</div>
</div>
<div id="outline-container-org1903b06" class="outline-2">
<h2 id="A-few-categorical-constructions"><span class="section-number-2">8</span> A few categorical constructions</h2>
<div class="outline-text-2" id="text-A-few-categorical-constructions">
<p>
We briefly take a pause to look at the theory of category theory.
In particular, we show a pair of constructions to get new categories from old ones,
interpret these constructions from the view of previously mentioned categories, and
discuss how to define the morphism type <code>_⟶_</code> on morphisms themselves, thereby
yielding a functor.
</p>
</div>

<div id="outline-container-orgbcc22e1" class="outline-3">
<h3 id="Opposite"><span class="section-number-3">8.1</span> Opposite</h3>
<div class="outline-text-3" id="text-Opposite">
<p>
The ‘dual’ or ‘opposite’ category 𝒞ᵒᵖ is the category constructed from 𝒞 by
reversing arrows: \((A ⟶_{𝒞ᵒᵖ} B) \;≔\; (B ⟶_𝒞 A)\), then necessarily
\((f ⨾_{𝒞ᵒᵖ} g) \;≔\; g ⨾_𝒞 f\).
A ‘contravariant functor’, or ‘cofunctor’, is a functor F from an opposite category and so
there is a reversal of compositions: \(F(f \,⨾\, g) \;=\; F g \,⨾\, F f\).
</p>
<pre class="example">
 _ᵒᵖ : ∀ {i j} → Category {i} {j} → Category
 𝒞 ᵒᵖ = {! exercise !}
</pre>
<p>
Notice that \((𝒞 ᵒᵖ) ᵒᵖ \;=\; 𝒞\) and \(𝒞 ᵒᵖ \;≅\; 𝒞\)
&#x2013;one may have an intuitive idea of what this isomorphsim means,
but formally it is only meaningful in the context of an ambient category; keep reading ;)
</p>

<p>
We must admit that for categories, the notion of isomorphism is considered less useful
than that of equivalence which weakens the condition of the to-from functors being
inverses to just being naturally isomorphic to identities; C.f., ‘evil’ above.
</p>

<p>
Some interpretations:
</p>

<ul class="org-ul">
<li><p>
𝒮e𝓉ᵒᵖ is usual sets and functions but with ‘backwards composition’:
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #859900; font-weight: bold;">infix</span> 10 <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8728;</span><span style="color: #859900; font-weight: bold;">_</span>
 <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8728;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j } &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}&#10628; {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #268bd2;">&#8594;</span>  <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">C</span>
 f <span style="color: #268bd2;">&#8728;</span> g <span style="color: #268bd2;">=</span> g <span style="color: #268bd2;">&#10814;</span> f
</pre>
</div>
<p>
Indeed, we have <code>g ⨾ f within 𝒞  =  f ∘ g within 𝒞 ᵒᵖ</code>; which is how these composition operators
 are usually related in informal mathematics (without mention of the ambient categories of course).
</p>

<p>
On a more serious note, the opposite of 𝒮e𝓉 is clearly 𝓉ℯ𝒮 haha
&#x2014;technically for the purposes of this pun we identify the words ‘opposite’ and ‘reverse’.
</p></li>

<li>For a discrete category, its opposite is itself.</li>

<li>For a monoid (viewed as a category), its opposite is itself if the monoid operation is commutative, otherwise
it is the ‘dual monoid’.</li>

<li><p>
For a poset (viewed as a category), its opposite is the ‘dual poset’: \((P, ⊑)ᵒᵖ \;=\; (P, ⊒)\).
</p>

<p>
In particular, the ‘least upper bound’, or ‘supremum’ in \((P, ⊑)\) of two elements
\(x,y\) is an element \(s\) with the ‘universal property’: \(∀ z •\; x ⊑ z ∧ y ⊑ z \;≡\; s ⊑ z\).
However, switching ⊑ with ⊒ gives us the notion of ‘infimum’, ‘greatest upper bound’!
So any theorems about supremums automatically hold for infimums since the infifum is nothing
more than the supremum in the dual category of the poset.
</p>

<p>
It is not difficult to see that this idea of “2 for the price of 1” for theorems holds for all
categories.
</p></li>

<li><b>Stone Duality:</b>
<code>FinBoolAlg ≃ FinSets ᵒᵖ</code> , witnessed by considering the collection of
atoms of a Boolean Algebra in one direction and the power set in the other.
Finiteness can be removed at the cost of completeness and atomicitiy,
<code>CompleteAtomicBoolAlg ≃ 𝒮ℯ𝓉 ᵒᵖ</code>.</li>

<li>What about the category of functors and natural transformations?</li>
</ul>

<p>
Speaking of functors, we can change the type of a functor by <code>ᵒᵖ</code>-ing its source and target,
while leaving it alone,
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">this only changes type</span>
 opify <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j i&#8217; j&#8217;} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}} {<span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i&#8217;} {j&#8217;}}
      <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> &#7506;&#7510;) (<span style="color: #b58900; font-style: italic;">&#119967;</span> &#7506;&#7510;)
 opify <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">=</span> record { obj   <span style="color: #268bd2;">=</span>  obj <span style="color: #b58900; font-style: italic;">F</span>
                  ; mor   <span style="color: #268bd2;">=</span>  mor <span style="color: #b58900; font-style: italic;">F</span>
                  ; id    <span style="color: #268bd2;">=</span>  Functor.id <span style="color: #b58900; font-style: italic;">F</span>
                  ; comp  <span style="color: #268bd2;">=</span>  Functor.comp <span style="color: #b58900; font-style: italic;">F</span>
                  }
</pre>
</div>

<blockquote>
<p>
Category Theory is the ‘op’ium of the people!
</p>

<p>
&#x2014; Karl Marx might say it had cats existed in his time
</p>
</blockquote>

<p>
This two definitions seem to indicate that we have some form of opposite-functor … ;)
&#x2014;keep reading!
</p>

<p>
<code>opify</code> seems to show that <code>Functor 𝒞 𝒟 ≡ Functor (𝒞 ᵒᵖ) (𝒟 ᵒᵖ)</code>, or alternatively a
functor can have ‘two different types’ &#x2014;this is akin to using the integers as reals
without writing out the inclusion formally, leaving it implicit; however, in the Agda mechanization
everything must be made explicit &#x2014;the type system doesn’t let you get away with such things.
Professor Maarten Fokkinga has informed me that
the formalization allowing multiple-types is called a
<a href="http://maartenfokkinga.github.io/utwente/mmf92b.pdf">pre-category</a>.
</p>
</div>

<div id="outline-container-org8ce1b3a" class="outline-4">
<h4 id="ah-yeah-and-dagger-categories"><span class="section-number-4">8.1.1</span> ah-yeah: ∂ and dagger categories</h4>
<div class="outline-text-4" id="text-ah-yeah-and-dagger-categories">
<p>
With <code>𝒞𝒶𝓉</code> in-hand, we can formalise the opposite, or ∂ual, functor:
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #268bd2;">&#8706;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span> {i} {j}) <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span>
 <span style="color: #268bd2;">&#8706;</span> <span style="color: #268bd2;">=</span> record { obj <span style="color: #268bd2;">=</span> _&#7506;&#7510; ; mor <span style="color: #268bd2;">=</span> opify ; id <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl ; comp <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl }
</pre>
</div>

<p>
Conjecture: Assuming categories are equipped with a contravariant involutionary functor
that is identity on objects, we can show that the identity functor is naturally isomorphic
to the opposite functor.
</p>

<div class="org-src-container">
<pre class="src src-haskell"> ah<span style="color: #268bd2;">-</span>yeah <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j} (<span style="color: #859900; font-weight: bold;">let</span> <span style="color: #b58900; font-style: italic;">Cat</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Obj</span> (<span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span> {i} {j}))
     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">identity on objects cofunctor, sometimes denoted _&#728;</span>
     <span style="color: #268bd2;">&#8594;</span> (dual <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Cat</span>) {x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>}  <span style="color: #268bd2;">&#8594;</span>  x <span style="color: #268bd2;">&#10230;</span> y <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>  <span style="color: #268bd2;">&#8594;</span>  y <span style="color: #268bd2;">&#10230;</span> x <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>)
     <span style="color: #268bd2;">&#8594;</span> (<span style="color: #b58900; font-style: italic;">Id</span><span style="color: #268bd2;">&#728;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Cat</span> &#10628; {x <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} <span style="color: #268bd2;">&#8594;</span> dual <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">Id</span>  <span style="color: #268bd2;">&#8801;</span>  <span style="color: #b58900; font-style: italic;">Id</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">=</span> x})
     <span style="color: #268bd2;">&#8594;</span> (<span style="color: #268bd2;">&#10814;-&#728;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Cat</span> &#10628; {x y z <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} {f <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y} {g <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#10230;</span> z}
            <span style="color: #268bd2;">&#8594;</span> dual <span style="color: #b58900; font-style: italic;">&#119966;</span> (f <span style="color: #268bd2;">&#10814;</span> g <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>)  <span style="color: #268bd2;">&#8801;</span>  (dual <span style="color: #b58900; font-style: italic;">&#119966;</span> g) <span style="color: #268bd2;">&#10814;</span> (dual <span style="color: #b58900; font-style: italic;">&#119966;</span> f) <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>)
     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">which is involutionary</span>
     <span style="color: #268bd2;">&#8594;</span> (<span style="color: #268bd2;">&#728;&#728;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Cat</span> &#10628; {x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} {f <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y} <span style="color: #268bd2;">&#8594;</span> dual <span style="color: #b58900; font-style: italic;">&#119966;</span> (dual <span style="color: #b58900; font-style: italic;">&#119966;</span> f) <span style="color: #268bd2;">&#8801;</span> f)
     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">which is respected by other functors</span>
     <span style="color: #268bd2;">&#8594;</span> (respect <span style="color: #b58900; font-style: italic;">:</span> &#10627; <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Cat</span> &#10628; {<span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>} {x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} {f <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y}
                <span style="color: #268bd2;">&#8594;</span> mor <span style="color: #b58900; font-style: italic;">F</span> (dual <span style="color: #b58900; font-style: italic;">&#119966;</span> f) <span style="color: #268bd2;">&#8801;</span> dual <span style="color: #b58900; font-style: italic;">&#119967;</span> (mor <span style="color: #b58900; font-style: italic;">F</span> f))
     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">then</span>
     <span style="color: #268bd2;">&#8594;</span> <span style="color: #268bd2;">&#8706;</span> <span style="color: #268bd2;">&#8773;</span> <span style="color: #b58900; font-style: italic;">Id</span> within <span style="color: #b58900; font-style: italic;">Func</span> (<span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span> {i} {j}) <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span>
</pre>
</div>
<pre class="example">
 ah-yeah = {! exercise !}
</pre>
<p>
Some things to note.
</p>

<ul class="org-ul">
<li><p>
Categories whose morphisms are all isomorphisms are called ‘groupoids’ &#x2014;groups are just one-object groupoids.
Consequently, restricted to groupoids the opposite functor is naturally isomorphic to the identity functor!
</p>

<p>
In fact, the group case was the motivator for me to conjecture the theorem, which took a while to prove since I hadn’t
a clue what I needed to assume. Here we’d use <code>a ˘ ≔ a ⁻¹</code>.
</p></li>

<li><p>
Consider the category <code>Rel</code> whose objects are sets and whose morphisms are ‘typed-relations’ \((S, R, T)\),
where \(R\) is a relation from set \(S\) to set \(T\), and
composition is just relational composition
&#x2014;the notion of ‘untyped’, or multi-typed, morphisms is formalized as pre-categories;
see <a href="http://maartenfokkinga.github.io/utwente/mmf92b.pdf">Fokkinga</a>.
Then we can define an endofunctor by taking <code>-˘</code> to be relational converse: \(x \,(R ˘)\, y \;≡\; y \,R\, x\).
Consequently, restricted to the category <code>Rel</code> we have that the opposite functor is naturally isomorphic to the identity functor.
</p></li>
</ul>

<p>
The above items are instance of a more general concept, of course.
</p>

<p>
A category with an involutionary contravariant endofunctor that is the identity on objects
is known as <i>a dagger category, an involutive/star category, or a category with converse</i>
&#x2014;and the functor is denoted as a superscript suffix by <code>†, *, ˘</code>, respectively.
The dagger notation probably comes from
the Hilbert space setting while the converse notation comes from the relation algebra setting.
As far as I know, the first two names are more widely known.
A dagger category bridges the gap between arbitrary categories and groupoids.
</p>

<p>
Just as matrices with matrix multiplication do not form a monoid but rather a category, we have
that not all matrices are invertible but they all admit transposition and so we have a dagger
category. In the same vein, relations admit converse and so give rise to a category with converse.
</p>

<p>
Besides relations and groupoids, other examples include:
</p>
<ul class="org-ul">
<li>discrete categories with the dagger being the identity functor</li>
<li>every monoid with an anti-involution is trivially a dagger category; e.g.,
lists with involution being reverse.</li>
<li>commutative monoids are anti-involutive monoids with anti-involution being identity</li>
</ul>

<p>
Spoilers!! Just as the category of categories is carestian closed, so too is the category of dagger
categories and dagger preserving functors &#x2013;c.f.,the <code>respect</code> premise above.
</p>
</div>
</div>
</div>

<div id="outline-container-orgf393832" class="outline-3">
<h3 id="Products"><span class="section-number-3">8.2</span> Products</h3>
<div class="outline-text-3" id="text-Products">
<p>
For any two categories 𝒞 and 𝒟 we can construct their ‘product’ category
\(𝒞 ⊗ 𝒟\) whose objects and morphisms are pairs with components from 𝒞 and 𝒟:
\(\Obj\, (𝒞 ⊗ 𝒟) \;\;=\;\; \Obj\, 𝒞 \,×\, \Obj\, 𝒟\) and
\((A , X) ⟶_{𝒞 ⊗ 𝒟} (B , Y) \;\;=\;\; (A ⟶_𝒞 B) \,×\, (X ⟶_𝒟 Y)\).
</p>
<pre class="example">
 -- we cannot overload symbols in Agda and so using ‘⊗’ in-place of more common ‘×’.
 _⊗_ : ∀ {i j i’ j’} → Category {i} {j} → Category {i’} {j’} → Category
 𝒞 ⊗ 𝒟 = {! exercise !}
</pre>
<p>
Observe that in weaker languages, a category is specified by its objects, morphisms, and composition
&#x2014;the proof obligations are delegated to comments, if they are realized at all.
In such settings, one would need to prove that this construction actually produces a full-fledged
category. Even worse, this proof may be a distance away in some documentation.
With dependent types, our proof obligation is nothing more than another component of the program,
a piece of the category type.
</p>

<p>
In a similar fashion we can show that the sum of two categories is again a category and in general
we have the same for quantified variants: <code>Π 𝒞 ∶ Family • 𝒞</code>, likewise for ‘Σ’.
For the empty family, the empty sum yields the category <code>𝟘</code> with no objects and
the empty product yields the category <code>𝟙</code> of one object.
One can then show the usual ‘laws of arithmetic’ &#x2014;i.e., ×,+ form a commutative monoid, up to isomorphism&#x2014;
hold in this setting: Letting <code>★ ∈ {+,×}</code>, we have
associtivity <code>A ★ (B ★ C) ≅ (A ★ B) ★ C</code>, symmetry <code>A ★ B ≅ B ★ A</code>,
unit <code>𝟙 × A ≅ 𝟘 + A ≅ A</code>, and zero <code>𝟘 × A ≅ 𝟘</code>.
These notions can be defined for any category though the objects may or may not exist
&#x2014; in <code>𝒮e𝓉</code> and <code>𝒢𝓇𝒶𝓅𝒽</code>, for example, they do exist ;) &#x2014;and these associated arithmetical
laws also hold.
</p>

<p>
<i>Question!</i> What of the distributivity law,
<code>A × (B + C) ≅ (A × B) + (A × C)</code>, does it hold in the mentioned cases?
Let <code>𝒫𝒮e𝓉</code> be the category of sets with a distinguished point, i.e.,  <code>Σ S : Obj 𝒮e𝓉 • S</code>, and
functions that preserve the ‘point’, one can then show &#x2014;if he or she so desires, and is not
lazy&#x2014; that this category has notions of product and sum but distributivity fails.
</p>

<p>
Some interpretations:
</p>
<ul class="org-ul">
<li>For discrete categories, this is the usual Cartesian product.</li>
<li>For monoid (or poset) categories, this says that the product of two monoids (or posets) is again
a monoid (respectively poset. This follows since the product does not affect the number of
objects and so the product is again a one-object category, i.e., a monoid (poset respectively).</li>
<li>Interestingly, the <i>sum</i> of two monoids is <b>not</b> formed by their disjoint union: Instead
it is the set of all alternating lists of elements from the two given monoids.
Exercise: Find the associated operation ;-)</li>
</ul>

<p>
As expected, we have projections,
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #b58900; font-style: italic;">Fst</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j i&#8217; j&#8217;} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}} {<span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i&#8217;} {j&#8217;}}
     <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">&#8855;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) <span style="color: #b58900; font-style: italic;">&#119966;</span>
 <span style="color: #b58900; font-style: italic;">Fst</span> <span style="color: #268bd2;">=</span> record { obj <span style="color: #268bd2;">=</span> proj&#8321; ; mor <span style="color: #268bd2;">=</span> proj&#8321; ; id <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl ; comp <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl }

 <span style="color: #b58900; font-style: italic;">Snd</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j i&#8217; j&#8217;} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}} {<span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i&#8217;} {j&#8217;}}
     <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">&#8855;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) <span style="color: #b58900; font-style: italic;">&#119967;</span>
 <span style="color: #b58900; font-style: italic;">Snd</span> <span style="color: #268bd2;">=</span> record { obj <span style="color: #268bd2;">=</span> proj&#8322; ; mor <span style="color: #268bd2;">=</span> proj&#8322; ; id <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl ; comp <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl }
</pre>
</div>
</div>

<div id="outline-container-org642209f" class="outline-4">
<h4 id="Currying"><span class="section-number-4">8.2.1</span> Currying</h4>
<div class="outline-text-4" id="text-Currying">
<p>
For types we have \[ (𝒳 × 𝒴 ⟶ 𝒵) \quad≅\quad (𝒳 ⟶ 𝒵 ^ 𝒴) \quad≅\quad (𝒴 ⟶ 𝒵 ^ 𝒳)\]
Since categories are essentially types endowed with nifty structure,
we expect it to hold in that context as well.
</p>
<pre class="example">
  -- Everyone usually proves currying in the first argument,
  -- let’s rebel and do so for the second argument
 curry₂ : ∀ {ix jx iy jy iz jz}
          {𝒳 : Category {ix} {jx}} {𝒴 : Category {iy} {jy}} {𝒵 : Category {iz} {jz}}
        → Functor (𝒳 ⊗ 𝒴) 𝒵 → Functor 𝒴 (Func 𝒳 𝒵)
 curry₂ = {! exercise !}
</pre>
</div>
</div>
</div>

<div id="outline-container-org9dfe5a5" class="outline-3">
<h3 id="Pointwise-extensions-and-the-hom-functor"><span class="section-number-3">8.3</span> Pointwise extensions and the hom functor</h3>
<div class="outline-text-3" id="text-Pointwise-extensions-and-the-hom-functor">
<p>
Just as addition can be extended to number-valued functions pointwise, \(f + g \;≔\; λ x → f x + g x\),
we can do the same thing with functors.
</p>
<pre class="example">
 -- For bifunctor ‘⊕’ and functors ‘F, G’, we have a functor ‘λ x → F x ⊕ G x’
 pointwise : ∀ {ic jc id jd ix jx iy jy}
   {𝒞 : Category {ic} {jc}} {𝒟 : Category {id} {jd}} {𝒳 : Category {ix} {jx}} {𝒴 : Category {iy} {jy}}
   → Functor (𝒳 ⊗ 𝒴) 𝒟 → Functor 𝒞 𝒳 → Functor 𝒞 𝒴
   → Functor 𝒞 𝒟
 pointwise = {! exercise !}
</pre>
<p>
By ‘extensionality’ <code>p ≡ (proj₁ p , proj₂ p)</code>, we have that the pointwise extension along the projections
is the orginal operation.
</p>
<div class="org-src-container">
<pre class="src src-haskell"> exempli<span style="color: #268bd2;">-</span>gratia <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">&#119987;</span> <span style="color: #b58900; font-style: italic;">&#119988;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {&#8467;&#8320;} {&#8467;&#8320;}} (<span style="color: #268bd2;">&#8853;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119987;</span> <span style="color: #268bd2;">&#8855;</span> <span style="color: #b58900; font-style: italic;">&#119988;</span>) <span style="color: #b58900; font-style: italic;">&#119967;</span>)
                <span style="color: #268bd2;">&#8594;</span> <span style="color: #859900; font-weight: bold;">let</span> <span style="color: #859900; font-weight: bold;">_</span>&#10216;<span style="color: #268bd2;">&#8853;</span>&#10217;<span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">=</span> pointwise <span style="color: #268bd2;">&#8853;</span>
                   <span style="color: #859900; font-weight: bold;">in</span>
                      <span style="color: #b58900; font-style: italic;">Fst</span> &#10216;<span style="color: #268bd2;">&#8853;</span>&#10217; <span style="color: #b58900; font-style: italic;">Snd</span> <span style="color: #268bd2;">&#8801;</span> <span style="color: #268bd2;">&#8853;</span>
 exempli<span style="color: #268bd2;">-</span>gratia <span style="color: #b58900; font-style: italic;">Bi</span> <span style="color: #268bd2;">=</span> funcext (<span style="color: #268bd2;">&#8801;-</span>cong (obj <span style="color: #b58900; font-style: italic;">Bi</span>) <span style="color: #268bd2;">&#8801;-</span>refl) (<span style="color: #268bd2;">&#8801;-</span>cong (mor <span style="color: #b58900; font-style: italic;">Bi</span>) <span style="color: #268bd2;">&#8801;-</span>refl)
</pre>
</div>

<p>
An example bifunctor is obtained by extending the ‘⟶’ to morphisms:
Given <code>f : A ⟶ B , g : C ⟶ D</code> we define <code>(f ⟶ g) : (B ⟶ C) → (A ⟶ C)</code> by
<code>λ h → f ⨾ h ⨾ g</code> as this is the only way to define it so as to meet the type requirements.
</p>
<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #b58900; font-style: italic;">Hom</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j} } <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> &#7506;&#7510; <span style="color: #268bd2;">&#8855;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>) (<span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span> {j})
   <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">hence contravariant in &#8216;first arg&#8217; and covaraint in &#8216;second arg&#8217;</span>
 <span style="color: #b58900; font-style: italic;">Hom</span> {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} <span style="color: #268bd2;">=</span>
   <span style="color: #859900; font-weight: bold;">let</span>
     <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
     <span style="color: #859900; font-weight: bold;">instance</span> <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> ; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
     <span style="color: #268bd2;">&#10814;-</span>cong&#8322; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span>} {g g&#8217; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10230;</span> <span style="color: #b58900; font-style: italic;">C</span>}
             <span style="color: #268bd2;">&#8594;</span> g <span style="color: #268bd2;">&#8801;</span> g&#8217; <span style="color: #268bd2;">&#8594;</span> f <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g <span style="color: #268bd2;">&#8801;</span> f <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g&#8217;
     <span style="color: #268bd2;">&#10814;-</span>cong&#8322;  q  <span style="color: #268bd2;">=</span>  <span style="color: #268bd2;">&#8801;-</span>cong&#8322; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.</span><span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">&#8801;-</span>refl q
   <span style="color: #859900; font-weight: bold;">in</span> record {
     obj <span style="color: #268bd2;">=</span> &#955;{ (<span style="color: #b58900; font-style: italic;">A</span> , <span style="color: #b58900; font-style: italic;">B</span>) <span style="color: #268bd2;">&#8594;</span>  <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span> }
   ; mor <span style="color: #268bd2;">=</span> &#955;{ (f , g) <span style="color: #268bd2;">&#8594;</span> &#955; h <span style="color: #268bd2;">&#8594;</span> f <span style="color: #268bd2;">&#10814;</span> h <span style="color: #268bd2;">&#10814;</span> g }
   ; id <span style="color: #268bd2;">=</span> extensionality (&#955; {h} <span style="color: #268bd2;">&#8594;</span> begin
        <span style="color: #b58900; font-style: italic;">Id</span> <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> h <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">Id</span>
      <span style="color: #268bd2;">&#8801;</span>&#10216; leftId &#10217;
        h <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">Id</span>
      <span style="color: #268bd2;">&#8801;</span>&#10216; rightId &#10217;
        h
      <span style="color: #268bd2;">&#8718;</span>)
   ; comp <span style="color: #268bd2;">=</span>  &#955; {x y z fg fg&#8217;} <span style="color: #268bd2;">&#8594;</span>
       <span style="color: #859900; font-weight: bold;">let</span> (f , g) <span style="color: #268bd2;">=</span> fg ; (f&#8217; , g&#8217;) <span style="color: #268bd2;">=</span> fg&#8217; <span style="color: #859900; font-weight: bold;">in</span> extensionality (&#955; {h} <span style="color: #268bd2;">&#8594;</span> begin
            (f&#8217; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> f) <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> h <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> (g <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g&#8217;)
          <span style="color: #268bd2;">&#8801;</span>&#10216; assoc &#10217;
            f&#8217; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> (f <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> (h <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> (g <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g&#8217;)))
          <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #268bd2;">&#10814;-</span>cong&#8322; (<span style="color: #268bd2;">&#8801;-</span>sym assoc) &#10217;
            f&#8217; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> ((f <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> h) <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> (g <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g&#8217;))
          <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #268bd2;">&#10814;-</span>cong&#8322; (<span style="color: #268bd2;">&#8801;-</span>sym assoc) &#10217;
            f&#8217; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> ((f <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> h) <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g) <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g&#8217;
          <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #268bd2;">&#10814;-</span>cong&#8322; (<span style="color: #268bd2;">&#8801;-</span>cong&#8322; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.</span><span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span> assoc <span style="color: #268bd2;">&#8801;-</span>refl) &#10217;
            f&#8217; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> (f <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> h <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g) <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> g&#8217;
          <span style="color: #268bd2;">&#8718;</span>)
     }
</pre>
</div>
<p>
The naming probably comes from the algebra/monoid case where the functors are
monoid <code>hom</code>-omorphisms. Some prefer to use the name <code>Mor</code>, short for <code>mor</code>-phisms,
and that’s cool too. While Haskell programmers might call this the <code>Reader</code> functor.
</p>

<p>
Usual notation for this functor is <code>Hom</code>, but I like Fokkinga’s much better.
He uses <code>(_⟶_)</code> and writes <code>(f ⟶ g) = λ h • f ⨾ h ⨾ g</code>
&#x2014;the first argument of Hom is the first argument of the composition and the last
argument to Hom is the last argument of the resulting composition :-)
</p>
</div>
</div>
</div>
<div id="outline-container-org56c4685" class="outline-2">
<h2 id="𝒮implicity-𝒰nderlies-𝒞omplexity"><span class="section-number-2">9</span> 𝒮implicity 𝒰nderlies 𝒞omplexity</h2>
<div class="outline-text-2" id="text-𝒮implicity-𝒰nderlies-𝒞omplexity">
<blockquote>
<p>
One way is to make it so 𝒮imple that there are obviously no deficiencies, and the other way is to
make it so 𝒞omplicated that there are no obvious deficiencies. The first method is far more
difficult. It demands the same skill, devotion, insight, and even inspiration as the discovery of
the simple physical laws which 𝒰nderlie the complex phenomena of nature.
</p>

<p>
─<a href="https://en.wikiquote.org/wiki/C._A._R._Hoare">C.A.R. Hoare</a>
</p>
</blockquote>

<small>
<div class="org-center">
<p>
( The 𝒞omplex philosophy behinds games such as Chess and
<a href="http://playgo.to/iwtg/en/">Go</a> arise from some 𝒮imple board game rules.
)
</p>
</div>
</small>

<p>
In this section we discuss what it means to be a ‘forgetful functor’?
&#x2013;Also called an `𝒰nderlying functor'.
</p>

<p>
The modifier ‘forgetful’ is meaningful when there’s a notion of extra structure.
Indeed any functor <i>F : 𝒞 ⟶ 𝒮</i> can be thought of as forgetful by construing the objects of
𝒞 as objects of 𝒮 with extra structure.
Mostly: <i>You know it (to be forgetful) when you see it!</i>
</p>
</div>

<div id="outline-container-org5902d61" class="outline-3">
<h3 id="Being-forgetful-from-injections-to-faithful-functors"><span class="section-number-3">9.1</span> Being forgetful: from injections to faithful functors</h3>
<div class="outline-text-3" id="text-Being-forgetful-from-injections-to-faithful-functors">
<p>
A common example from set theory is the ‘inclusion’ of a subset \(A\) of \(B\), the injection
\(ι : A ↪ B : a ↦ a\) &#x2014;it is essentially a form of ‘type casting’: \(a ∈ A\) and \(ι a \;=\; a ∈ B\).
Such injections ‘forget’ the property that the argument is actually a member of a specified
subset. Indeed, construing sets as categories then functions becomes functors and inclusions
are then forgetful functors!
</p>

<p>
Since a functor <i>F</i> consists of two maps <i>(F₀, F₁) ≔ (obj F, mor F)</i> and some properties, we can speak about properties of the
functor and about properties of either of its maps.
The common definitions are a functor \(F\) is:
</p>
<dl class="org-dl">
<dt>faithful</dt><dd>If its operation on morphisms is <i>injective</i>, and it is</dd>
<dt>full    </dt><dd>If morphisms starting and ending at <i>F</i> are a result of applying \(F\);  <br>
i.e., <i>F₁</i> is surjective <i>on</i> the image of <i>F₀</i>:  <br>
\(∀ x,y ∶ Obj \;•\; ∀ g ∶ F₀ x ⟶ F₀ y \;•\; ∃ f ∶ x ⟶ y \;•\; F₁ f = g\).</dd>
</dl>

<p>
Now we can generalize the previous example.
Every faithful functor <i>F : 𝒞 ⟶ 𝒟</i> can be construed as forgetful:
The 𝒞-maps can be embedded into the 𝒟-maps, since F is faithful, and so can be thought of
as a special sub-collection of the 𝒟-maps; then \(F\) ‘forgets’ the property of being in this
special sub-collection.
</p>

<p>
Are faithful functors in abundance? Well any functor forgetting only axioms
(and/or structure) is faithful:
</p>

<ol class="org-ol">
<li>Suppose 𝒞 consists of 𝒟 objects satisfying some axioms and 𝒟 maps preserving this structure.</li>
<li>That is, 𝒞 has pairs of 𝒟 objects/morphisms with a proof that it satisfies the axioms/preserves-structure.</li>
<li>Then “\(F : 𝒞 ⟶ 𝒟\) forgets only axioms” means \(F\, (f, \mathsf{proof}) \;=\; f\).</li>
<li><p>
Now given, \(F (f , prf) = F (g , prf) \;⇔\; f = g \;⇔\; (f , prf) = (g , prf)\)
&#x2013; equality does not (extensionally) depend on proof components.
</p>

<p>
Hence, faithful :-)
</p>

<p>
(Likewise for forgetting extra structure).
</p></li>
</ol>

<p>
Of course we’re not saying all forgetful functors are necessarily faithful.
A simple counterexample is the absolute value function:
Given a real number \(x\) it’s absolute value \(∣x∣\) is obtained by totally ignoring its sign
&#x2014;of course \(x\) and \(∣x∣\) are equidistant from 0, the relation equidistant-from-0 is an equivalence
relation &#x2013;Exercise!&#x2013;, and so the the two are isomorphic in some sense.
</p>

<p>
Motivated by this, given a set \(S\) it’s size is denoted \(∣ S ∣\) which totally forgets about the
elements of the set &#x2014;of course it can be shown that two sets are isomorphic precisely if they are
equinumerous.
</p>

<p>
I assume it is with these as motivators, some people write \(∣·∣\) for a forgetful functor.
</p>

<p>
( Exercise: A functor <code>F : 𝒞 ≃ 𝒟</code> is (part of) an equivalence iff it is full,
faithful, and ‘‘essentially surjective on objects’’:
 <code>∀ D : Obj 𝒟 • Σ C : Obj 𝒞 • F C ≅ D</code> &#x2014;note the iso. )
</p>
</div>
</div>

<div id="outline-container-org3128627" class="outline-3">
<h3 id="Of-basis-vectors"><span class="section-number-3">9.2</span> Of basis vectors</h3>
<div class="outline-text-3" id="text-Of-basis-vectors">
<p>
If you’ve ever studied abstract algebra &#x2014;the math with vector spaces&#x2014; then you may recall that
a collection of vectors ℬ is called a ‘basis’ if every vector can be written as a linear
combination of these vectors: For any vector \(v\), there are scalars \(c₁, …, cₙ\) and vectors
\(b₁, …, bₙ\) in ℬ with \(v \;=\; c₁·b₁ + ⋯ + cₙ·bₙ\). That is, a basis is a collection of ‘building
blocks’ for the vector space. Then any function \(f\) between basis sets immediately lifts to a
linear transformation (think vector space morphism) \(F\) as follows: Given a vector \(v\), since we
have a basis, we can express it as \(c₁·b₁ + ⋯ + cₙ·bₙ\), now define
\(F v \;≔\; c₁·(f\, b₁) + ⋯ + cₙ·(f\, bₙ)\).
</p>

<p>
Sweet!
</p>

<p>
Thus, to define a complicated linear transformation of vector
spaces, it more than suffices to define a plain old simple function of basis sets.
Moreover, by definition, such \(F\) maps basis vectors to basis vectors: \(f \;=\; ι ⨾ F\) where
\(ι : ℬ ↪ 𝒱\) is the inclusion that realises basis vectors as just usual vectors in the vector
space 𝒱.  <b>Slogan:</b>
<i>Vector space maps are determined by where they send their basis, and basis-vectors
are preserved.</i>
</p>

<p>
In the case of <code>(List A, ++, [])</code> we may consider <code>A</code> to be a ‘basis’ of the monoid &#x2014;indeed,
every list can be written as a linear combination of elements of <code>A</code>, given list
<code>[x₁, …, xₙ] : List A</code> we have <code>[x₁, …, xₙ] = x₁ + ⋯ + xₙ</code> where <code>x + y ≔ [x] ++ [y]</code>.
Reasoning similarly as above, or if you have familiarity with <code>foldr , reduce</code>, we have a <b>slogan:</b>
<i>Monoid homomorphisms from lists are determined by where they send their basis and basis-vectors are preserved.</i>
</p>

<p>
Now the general case: <i>\(F ⊣ U\) is a (free-forgetful) ‘adjunction’</i> means
for functors ‘forget’ \(U : 𝒞 ⟶ 𝒮\) and ‘free’ \(F : 𝒮 → 𝒞\), we have that
for a given 𝒮imple-object \(S\) there’s 𝒮imple-map \(ι : S ⟶_𝒮 U\,(F\, S)\) &#x2014;a way to realise ‘basis
vectors’&#x2014; such that for any 𝒞omplicated-object \(C\) and 𝒮imple-maps \(φ : S ⟶_𝒮 U\, C\), there is a
unique 𝒞omplicated-map \(Φ : F\, S ⟶_𝒞 C\) that preserves the basis vectors: \(φ = ι ⨾ U Φ\).
</p>

<p>
By analogy to the previous two cases, we may
consider \(U\, X\) to be a ‘basis’, and make the <b>slogan</b>:
𝒞omplicated-maps from free objects are
determined by where they send their basis and ‘basis vectors’ are preserved.
</p>

<p>
[ In more categorical lingo, one says \(ι\) is the ‘insertion of generators’.
</p>

<p>
  Question: Does the way we took \(ι\) in the previous graph show that it is a natural
  transformation \(ι : \Id ⟶ F ⨾ U\)?
  &#x2014;The naturality just says that a ‘homomorphism’ \(F f\) on the free object is
  completely determined by what \(f\) does to the generators ;-)
]
</p>
</div>
</div>

<div id="outline-container-orga46ecd9" class="outline-3">
<h3 id="Of-adjunctions"><span class="section-number-3">9.3</span> Of adjunctions</h3>
<div class="outline-text-3" id="text-Of-adjunctions">
<p>
An adjunction \(L ⊣ U\), where the <code>L</code>-ower adjoint is from 𝒮 to 𝒞 and the <code>U</code>-pper adjoint is in
the opposite direction, lends itself to an elemntary interpretation if we consider 𝒞
to be some universe of 𝒞omplicated items of study, while 𝒮 to be a universe of 𝒮imple
items of study. Then adjointness implies that given a simple-object \(S\) and a complicated-object
\(C\), a simple-map \(X ⟶ U\, C\) corresponds to a complicated-map \(L\, S ⟶ C\). To work with
complicated-maps it is more than enough to work with simple-maps!
</p>

<p>
Formally this correspondence, saying \(F : 𝒞 ⟶ 𝒟\) is adjoint to \(G : 𝒟 ⟶ 𝒞\), written \(F ⊣ G\),
holds precisely when \((F ∘ X ⟶ Y) \;≅\; (X ⟶ G ∘ Y)\) in a functor category:
</p>

<div class="org-src-container">
<pre class="src src-haskell"> <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8867;</span>&#8320;<span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> (i <span style="color: #268bd2;">&#8845;</span> j)
 <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8867;</span>&#8320;<span style="color: #859900; font-weight: bold;">_</span> {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} {<span style="color: #b58900; font-style: italic;">&#119967;</span>} <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">G</span>
    <span style="color: #268bd2;">=</span>
      (<span style="color: #b58900; font-style: italic;">F</span> <span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">&#8728;</span> <span style="color: #b58900; font-style: italic;">X</span>  <span style="color: #268bd2;">&#10230;</span>&#8345;&#8336;&#8348; <span style="color: #b58900; font-style: italic;">Y</span>)  <span style="color: #268bd2;">&#8773;</span>  (<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#10230;</span>&#8345;&#8336;&#8348; <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#8728;</span> <span style="color: #b58900; font-style: italic;">Y</span>)  within  <span style="color: #b58900; font-style: italic;">Func</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> &#7506;&#7510; <span style="color: #268bd2;">&#8855;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) <span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span>
   <span style="color: #859900; font-weight: bold;">where</span>
     <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Fst</span> ; <span style="color: #b58900; font-style: italic;">Y</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Snd</span> ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> opify <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">only changes types</span>

     <span style="color: #859900; font-weight: bold;">infix</span> 5 <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span>&#8345;&#8336;&#8348;_
     <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span>&#8345;&#8336;&#8348;_ <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {i j} {<span style="color: #b58900; font-style: italic;">&#119964;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}} <span style="color: #268bd2;">&#8594;</span>
            <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> &#7506;&#7510; <span style="color: #268bd2;">&#8855;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) (<span style="color: #b58900; font-style: italic;">&#119964;</span> &#7506;&#7510;) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> &#7506;&#7510; <span style="color: #268bd2;">&#8855;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) <span style="color: #b58900; font-style: italic;">&#119964;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119966;</span> &#7506;&#7510; <span style="color: #268bd2;">&#8855;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) <span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span>
     <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span>&#8345;&#8336;&#8348;_ {i} {j} {<span style="color: #b58900; font-style: italic;">&#119964;</span>} <span style="color: #268bd2;">=</span> pointwise (<span style="color: #b58900; font-style: italic;">Hom</span> {i} {j} {<span style="color: #b58900; font-style: italic;">&#119964;</span>})
</pre>
</div>
<p>
Note that if we use Agda's built-in rewrite mechanism to add the rule,
</p>
<pre class="example">
{𝒞 𝒟 : Category {ℓ₀} {ℓ₀}} → Functor (𝒞 ᵒᵖ) (𝒟 ᵒᵖ) ≡ Functor 𝒞 𝒟
</pre>
<p>
then we might be able to get away without using <code>opify</code>.
</p>

<p>
Anyhow, this says for any objects \(X\) and \(Y\), the collection of morphisms \((F\, A ⟶ B)\)
is isomorphic to the collection \((A ⟶ G\, B)\) and naturally so in \(A\) and \(B\).
</p>

<p>
Unfolding it, we have
</p>
<div class="org-src-container">
<pre class="src src-haskell"> record <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8867;</span><span style="color: #859900; font-weight: bold;">_</span> {i j i&#8217; j&#8217;} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i} {j}} {<span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {i&#8217;} {j&#8217;}}
        (<span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>)
        <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> (j&#8217; <span style="color: #268bd2;">&#8845;</span> i&#8217; <span style="color: #268bd2;">&#8845;</span> j <span style="color: #268bd2;">&#8845;</span> i) <span style="color: #859900; font-weight: bold;">where</span>

   open <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> renaming (<span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span> to <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span>&#8322;<span style="color: #859900; font-weight: bold;">_</span>)
   open <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> renaming (<span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span> to <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span>&#8321;<span style="color: #859900; font-weight: bold;">_</span>)
   field
     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8216;left-adjunct&#8217;  L &#8776; &#8970;  and  &#8216;right-adjunct&#8217;  r &#8776; &#8968;</span>
     &#8970;<span style="color: #859900; font-weight: bold;">_</span>&#8971; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">Y</span>} <span style="color: #268bd2;">&#8594;</span>   obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">Y</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>   <span style="color: #268bd2;">&#8594;</span>   <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#10230;</span> obj <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">Y</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
     &#8968;<span style="color: #859900; font-weight: bold;">_</span>&#8969; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">Y</span>} <span style="color: #268bd2;">&#8594;</span>   <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#10230;</span> obj <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">Y</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>   <span style="color: #268bd2;">&#8594;</span>   obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">Y</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>

     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Adjuncts are inverse operations</span>
     lid <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">Y</span>} {d <span style="color: #b58900; font-style: italic;">:</span> obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">Y</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>} <span style="color: #268bd2;">&#8594;</span> &#8968; &#8970; d &#8971; &#8969; <span style="color: #268bd2;">&#8801;</span> d
     rid <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">Y</span>} {c <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#10230;</span> obj <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">Y</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} <span style="color: #268bd2;">&#8594;</span> &#8970; &#8968; c &#8969; &#8971; <span style="color: #268bd2;">&#8801;</span> c

     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">That for a fixed argument, are natural transformations between Hom functors</span>
     lfusion <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #b58900; font-style: italic;">D</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} {&#968; <span style="color: #b58900; font-style: italic;">:</span> obj <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>} {g <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">D</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>}
             <span style="color: #268bd2;">&#8594;</span>  &#8970; mor <span style="color: #b58900; font-style: italic;">F</span> f <span style="color: #268bd2;">&#10814;</span>&#8322; &#968; <span style="color: #268bd2;">&#10814;</span>&#8322; g &#8971;  <span style="color: #268bd2;">&#8801;</span>  f <span style="color: #268bd2;">&#10814;</span>&#8321; &#8970; &#968; &#8971; <span style="color: #268bd2;">&#10814;</span>&#8321; mor <span style="color: #b58900; font-style: italic;">G</span> g
     rfusion <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #b58900; font-style: italic;">D</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} {&#968; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#10230;</span> obj <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} {g <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">C</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">D</span> <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>}
             <span style="color: #268bd2;">&#8594;</span>  &#8968; f <span style="color: #268bd2;">&#10814;</span>&#8321; &#968; <span style="color: #268bd2;">&#10814;</span>&#8321; mor <span style="color: #b58900; font-style: italic;">G</span> g &#8969;  <span style="color: #268bd2;">&#8801;</span>  mor <span style="color: #b58900; font-style: italic;">F</span> f <span style="color: #268bd2;">&#10814;</span>&#8322; &#8968; &#968; &#8969; <span style="color: #268bd2;">&#10814;</span>&#8322; g
</pre>
</div>

<p>
This is easier for verifying an adjunction, while the former is easier for remembering and understanding what an adjunction actually is.
</p>

<p>
As the slogan goes ‘adjunctions are everywhere’.
They can be said to capture the notions of optimization and efficiency, but also that of simplicity.
</p>

<p>
For example, the supremum of a function is defined to be an upper bound of its image set and the least such bound.
Formally, this definition carries a few quantifiers and so a bit lengthy.
More elegantly, we can say the supremum operation is left-adjoint to the constant function: \[ \mathsf{sup} ⊣ 𝒦 \]
which means \[ ∀ z •\qquad \mathsf{sup}\, f \,≤\, z \quad⇔\quad f \overset{.}{≤} 𝒦\, z\]
Where \(𝒦\, x\, y \,=\, x\) and the \(\overset{.}{≤}\) on the right is the point-wise ordering on functions.
This formulation of supremum is not only shorter to write but easier to use in calculational proofs.
</p>

<p>
For the efficiency bit, recall that it is efficient to specify a 𝒮imple-map, then use the adjuction, to obtain
a 𝒞omplicated-map. Recall in the last paragraph how we define the super complicated notion of supremum of a function
in terms of the most elementary constant function!
</p>

<p>
Adjunctions over poset categories are called ‘Galois connections’ and a good wealth of
material on them can be found in nearly any writing by <a href="http://www.cs.nott.ac.uk/~psarb2/papers/papers.html">Backhouse et. al.</a>,
while a very accessible introduction is by <a href="http://www.cs.nott.ac.uk/~psarb2/MPC/galois.ps.gz">Aarts</a>,
and there is also an Agda mechanisation by <a href="http://relmics.mcmaster.ca/RATH-Agda/AContext-2.1.pdf">Kahl &amp; Al-hassy</a>.
</p>

<p>
Regarding forgetful functors:
Generally, but not always, forgetful functors are faithful and have left adjoints
&#x2014;because the notion of ‘forget’ ought to have a corresponding notion of ‘free’.
An exception to this is the category of fields, which has a forgetful functor to the
category of sets with no left adjoint.
</p>
</div>
</div>

<div id="outline-container-org156fe5c" class="outline-3">
<h3 id="Adjunctions-and-Representable-Functors"><span class="section-number-3">9.4</span> Adjunctions and Representable Functors</h3>
<div class="outline-text-3" id="text-Adjunctions-and-Representable-Functors">
<p>
Another awesome thing about adjunctions <code>L ⊣ U</code> is that they give us ‘representable functors’,
  a.k.a. ‘the best kind of functors’, when terminal objects exist.
</p>

<ul class="org-ul">
<li>An object <code>𝟙</code> is ‘terminal’ if for any object <code>A</code> there is a unique morphism <code>! {A} : A ⟶ 𝟙</code>.
In 𝒮ℯ𝓉 we have <code>(A ⟶ 𝟙) ≅ 𝟙</code> and <code>(𝟙 ⟶ A) ≅ A</code>.</li>

<li>Specialising the adjunction, where <code>U : 𝒞 ⟶ 𝒮e𝓉</code>, to
a given set <code>A</code> and <code>𝟙</code> we obtain <code>(L 𝟙 ⟶ A) ≅ (𝟙 ⟶ U A) ≅ U A</code> and so one says
‘ <code>U</code> is represented by <code>L 𝟙</code> ’.</li>

<li>In particular, if 𝒞 is built on 𝒮ℯ𝓉 by adding some structure
and we are interested in utilising the elements of an object <code>A</code>
then it suffices to utilise the maps <code>L 𝟙 ⟶ A</code>.</li>
</ul>

<p>
In the case of a free-forgetful adjunction, this says that
  <i>a forgetful functor is represented by the free object with generator <code>𝟙</code>.</i>
</p>

<p>
For example, for monoids the one-point monoid is the terminal object: <code>𝟙 ≔ ({*}, ⊕, *)</code> with <code>x ⊕ y ≔ ⋆</code>.
Then every monoid-homomorphism from <code>𝟙</code> just picks out an element of the carrier of a monoid and so
<code>(𝟙 ⟶ M) ≅ 𝒰 M</code> where <code>𝒰</code> is the forgetful functor for monoids mentioned in the introduction.
</p>
</div>
</div>

<div id="outline-container-org87207a5" class="outline-3">
<h3 id="Concluding-remarks"><span class="section-number-3">9.5</span> Concluding remarks</h3>
<div class="outline-text-3" id="text-Concluding-remarks">
<p>
A final note about ‘free objects’ &#x2014;arising from an adjoint to a forgetful functor.
</p>

<p>
<b>‘‘The free object is generic’’</b>: The only truths provable for the free
object are precisely those that hold for every complicated-object.
</p>

<p>
(Begin squinting eyes)
 <br>
This follows from the
definition of adjunction which says we can construct a unique morphism between complicated-objects
from a simple-map and by naturality we may transport any proof for the free object to any
complicated object.
 <br>
(Feel ‘free’ to stop squinting your eyes)
</p>


<p>
For futher reading consider reading the adjoint article at <a href="http://www.comicbooklibrary.org/articles/Left_adjoint">the comic book library</a>
and for more on the adjective ‘forgetful’ see <a href="https://ncatlab.org/nlab/show/forgetful+functor">ncatlab</a> or <a href="http://mathworld.wolfram.com/ForgetfulFunctor.html">mathworld</a>
A nice list of common free objects can be found on <a href="https://en.wikipedia.org/wiki/Free_object#List_of_free_objects">wikipedia</a>.
</p>

<p>
You might be asking,
 <i>musa, when am I ever going to encounter this in daily life? In a popular setting?</i>
This concept is everywhere, even inclusions as mentioned earlier are an
instance. For the second question, enjoy listening to
<a href="https://www.youtube.com/watch?v=BipvGD-LCjU">this lovely musical group</a> &#x2013;they use the words ‘forgetful functors’ ;)
</p>

<p>
The remainder of this document can be seen as one fully-worked out example of constructing a
free functor for the forgetful 𝒰 defined above from 𝒞𝒶𝓉 to 𝒢𝓇𝒶𝓅𝒽.
</p>
</div>
</div>
</div>

<div id="outline-container-orgbb63b76" class="outline-2">
<h2 id="Designing-Paths"><span class="section-number-2">10</span> Designing Paths</h2>
<div class="outline-text-2" id="text-Designing-Paths">
<div class="org-center">
<p>
<i>The “right” definition is hard to obtain!</i>
</p>
</div>
<p>
We can now define a ‘path’ of length <code>n</code> in a graph <code>G</code> to be a graph-map
<code>[ n ] ⟶ G</code>.
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900; font-style: italic;">Path</span>&#8320; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#8469;</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Graph</span>&#8320; <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> (&#8467;suc &#8467;&#8320;)
<span style="color: #b58900; font-style: italic;">Path</span>&#8320; n <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">=</span> [ n ]&#8320; <span style="color: #b58900; font-style: italic;">&#119970;</span><span style="color: #268bd2;">&#10230;</span>&#8320; <span style="color: #b58900; font-style: italic;">G</span>
</pre>
</div>

<p>
Unfolding the definition of graph-morphisms, this just says that a path of length <code>n</code>
consists of a sequence <code>[v₀, v₁, v₂,  …, vₙ]</code> of vertices of <code>G</code> and a sequence <code>[e₀, e₁, …, eₙ₋₁]</code>
of edges of <code>G</code> with typing <code>eᵢ : vᵢ ⟶ vᵢ₊₁</code>.
</p>

<p>
The definition is pretty slick! However, as the name suggests, perhaps we can concatenate paths
and it’s not at all clear how to do this for the vertex- and edge- morphisms of the graph-maps
involved, whereas it’s immediately clear how to do this with sequences: We just concatenate the
sequences and ensure the result is coherent.
</p>

<p>
Since the vertices can be obtained from the edges via <code>src</code> and <code>tgt</code>, we can dispense with them
and use the definition as outlined above.
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900;">open</span> <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Data.Vec</span> using (<span style="color: #b58900; font-style: italic;">Vec</span> ; lookup)

<span style="color: #b58900;">record</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8321; (n <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#8469;</span>) (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>&#8320;) <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> (&#8467;suc &#8467;&#8320;) <span style="color: #859900; font-weight: bold;">where</span>
  open <span style="color: #b58900; font-style: italic;">Graph</span>&#8320;
  field
    edges     <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Vec</span> (<span style="color: #b58900; font-style: italic;">E</span> <span style="color: #b58900; font-style: italic;">G</span>) (suc n)
    coherency <span style="color: #b58900; font-style: italic;">:</span> {i <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Fin</span> n} <span style="color: #268bd2;">&#8594;</span> tgt <span style="color: #b58900; font-style: italic;">G</span> (lookup (` i) edges) <span style="color: #268bd2;">&#8801;</span> src <span style="color: #b58900; font-style: italic;">G</span> (lookup (fsuc i) edges)
</pre>
</div>
<p>
That is, edges <code>[e₀, …, eₙ]</code> with coherency <code>tgt eᵢ ≡ src eᵢ₊₁</code>.
</p>

<p>
Great, we’ve cut the definition of <code>Path₀</code> in half but that fact that we get a raw list of edges
and then need coherency to ensure that it is a well-formed path is still not terribly lovely.
After all, we’re in Agda, we’re among kings, we should be able to form the list in such a way that
the end result is a path. Let’s do that!
</p>

<p>
Enough of this repetition, let us fix a graph <code>G</code>,
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">Path</span><span style="color: #268bd2;">-</span>definition<span style="color: #268bd2;">-</span>2 (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>&#8320;) <span style="color: #859900; font-weight: bold;">where</span>
  open <span style="color: #b58900; font-style: italic;">Graph</span>&#8320; <span style="color: #b58900; font-style: italic;">G</span>

  mutual
    <span style="color: #859900; font-weight: bold;">data</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8322; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> <span style="color: #859900; font-weight: bold;">where</span>
      <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">!</span>   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8322;
      cons <span style="color: #b58900; font-style: italic;">:</span> (v <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>) (e <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">E</span>) (ps <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8322;) (s <span style="color: #b58900; font-style: italic;">:</span> v <span style="color: #268bd2;">&#8801;</span> src e) (t <span style="color: #b58900; font-style: italic;">:</span> tgt e <span style="color: #268bd2;">&#8801;</span> head&#8322; ps) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8322;

    head&#8322; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8322; <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span>
    head&#8322; (v <span style="color: #268bd2;">!</span>) <span style="color: #268bd2;">=</span> v
    head&#8322; (cons v e p s t) <span style="color: #268bd2;">=</span> v
</pre>
</div>

<p>
Defining paths for the parallel-pair approach to graphs leaves us with the need to carry
proofs around, and this is a tad too clunky in this case. Let's try yet again.
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">Path</span><span style="color: #268bd2;">-</span>definition<span style="color: #268bd2;">-</span>3 (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>) <span style="color: #859900; font-weight: bold;">where</span>

  open <span style="color: #b58900; font-style: italic;">Graph</span> <span style="color: #b58900; font-style: italic;">G</span>

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">handy dandy syntax</span>
  <span style="color: #859900; font-weight: bold;">infixr</span> 5 cons
  syntax cons v ps e <span style="color: #268bd2;">=</span> v <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">v goes, by e, onto path ps</span>

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">we want well-formed paths</span>
  mutual
    <span style="color: #859900; font-weight: bold;">data</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8323; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> <span style="color: #859900; font-weight: bold;">where</span>
      <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">!</span>   <span style="color: #b58900; font-style: italic;">:</span> (v <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8323;
      cons <span style="color: #b58900; font-style: italic;">:</span> (v <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>) (ps <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8323;) (e <span style="color: #b58900; font-style: italic;">:</span> v <span style="color: #268bd2;">&#10230;</span> head&#8323; ps) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8323;

    head&#8323; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8323; <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span>
    head&#8323; (v <span style="color: #268bd2;">!</span>) <span style="color: #268bd2;">=</span> v
    head&#8323; (v <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps) <span style="color: #268bd2;">=</span> v

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">motivation for the syntax declaration above</span>
  example <span style="color: #b58900; font-style: italic;">:</span> (v&#8321; v&#8322; v&#8323; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>) (e&#8321; <span style="color: #b58900; font-style: italic;">:</span> v&#8321; <span style="color: #268bd2;">&#10230;</span> v&#8322;) (e&#8322; <span style="color: #b58900; font-style: italic;">:</span> v&#8322; <span style="color: #268bd2;">&#10230;</span> v&#8323;) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8323;
  example v&#8321; v&#8322; v&#8323; e&#8321; e&#8322; <span style="color: #268bd2;">=</span> v&#8321; <span style="color: #268bd2;">&#10230;</span>[ e&#8321; ]<span style="color: #268bd2;">&#10230;</span> v&#8322; <span style="color: #268bd2;">&#10230;</span>[ e&#8322; ]<span style="color: #268bd2;">&#10230;</span> v&#8323; <span style="color: #268bd2;">!</span>

  end&#8323; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8323; <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span>
  end&#8323; (v <span style="color: #268bd2;">!</span>) <span style="color: #268bd2;">=</span> v
  end&#8323; (v <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps) <span style="color: #268bd2;">=</span> end&#8323; ps

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">typed paths; squigarrowright</span>
  record <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8669;</span><span style="color: #859900; font-weight: bold;">_</span> (x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>) <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span> <span style="color: #859900; font-weight: bold;">where</span>
    field
      path   <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Path</span>&#8323;
      start  <span style="color: #b58900; font-style: italic;">:</span> head&#8323; path <span style="color: #268bd2;">&#8801;</span> x
      finish <span style="color: #b58900; font-style: italic;">:</span> end&#8323; path  <span style="color: #268bd2;">&#8801;</span> y
</pre>
</div>
<p>
This seems great, but there’s always room for improvement:
</p>


<ul class="org-ul">
<li><p>
Since the <code>cons</code> constructor's third argument depends on its first, we must
use a syntax declaration to get the desired look. Such aesthetic is not only
pleasing but reminiscent of diagrammatic paths;
moreover, it’s guaranteed to be an actual path and not just an
alternating lists of vertices and edges.
Using the clunky <code>Path₂</code>, we’d write
</p>
<pre class="example">
  v₁ ⟶[ v₁≈se₁ , e₁ , te₁≈v₂ ]⟶ v₂ ⟶[ v₂≈se₂ , e₂ , te₂≈v₃ ]⟶ v₃ !
  where
  syntax cons v e ps s t = v ⟶[ s , e , t ]⟶ ps
</pre>
<p>
yuck!
</p>

<p>
Finally, the syntax-declaration does not make the emacs agda-mode auto-case using
the syntax, and so I have to write it out by hand, each time I want to use the syntax.
</p></li>

<li>Again since <code>cons</code>'s third argument depends on the second argument, we need a mutual
definition to extract the item of the dependence. Perhaps if we embed this item at
the type level we may avoid the need of an auxiliary mutually-defined function.</li>

<li>By defining what the start and finish of a path are, we can assign types to it.
However, this approach is reminiscent of the parallel-pair approach to graphs,
as in <code>Graph₀</code>, which we argued is less preferable to the typed-approach to graphs.
Perhaps defining paths with types by default, we can reap the benefits and simplicity
of the typed-approach to graphs.</li>
</ul>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">TypedPaths</span> (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>) <span style="color: #859900; font-weight: bold;">where</span>

  open <span style="color: #b58900; font-style: italic;">Graph</span> <span style="color: #b58900; font-style: italic;">G</span> hiding(<span style="color: #b58900; font-style: italic;">V</span>)
  open <span style="color: #b58900; font-style: italic;">Graph</span>   using (<span style="color: #b58900; font-style: italic;">V</span>)

  <span style="color: #859900; font-weight: bold;">data</span> <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8669;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Set</span> <span style="color: #859900; font-weight: bold;">where</span>
    <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">!</span>      <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> x <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#8669;</span> x
    <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span>[<span style="color: #859900; font-weight: bold;">_</span>]<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> x {y &#969;} (e <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y) (ps <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#8669;</span> &#969;) <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#8669;</span> &#969;
</pre>
</div>

<p>
One might think that since we can write
</p>
<pre class="example">
  src : {x y : V G} (e : x ⟶ y) → V G
  src {x} {y} e = x
</pre>
<p>
we can again ignore vertices and it suffices to just keep a coherent list of edges.
Then what is an empty path at a vertex? This’ enough to keep vertices around
&#x2014;moreover, the ensuing terms look like diagrammatic paths! Cool!
</p>

<div class="org-center">
<p>
Finding this definitional <i>form</i> was a major hurdle in this endeavour.
</p>
</div>
</div>

<div id="outline-container-org12ecb67" class="outline-3">
<h3 id="Aside-An-Adjunction-between-𝒮ℯ𝓉-and-𝒞𝒶𝓉"><span class="section-number-3">10.1</span> Aside: An Adjunction between 𝒮ℯ𝓉 and 𝒞𝒶𝓉</h3>
<div class="outline-text-3" id="text-Aside-An-Adjunction-between-𝒮ℯ𝓉-and-𝒞𝒶𝓉">
<p>
With paths in hand, we can now consider a neat sequence of <a href="https://math.stackexchange.com/questions/1640298/coforgetful-functors">exercises</a> :-)
</p>

<ol class="org-ol">
<li><p>
Show that graphmaps preserve paths: <code>(f : G ⟶ H)  → x ⇝ y → fᵥ x ⇝ fᵥ y</code>;
this is nothing more than type-preservation for <code>f</code> to be a functor <code>𝒫G ⟶ 𝒫H</code> ;)
</p>

<p>
Hint: This is <code>lift</code> from the next section.
</p></li>

<li><p>
Define
</p>
<pre class="example">
a connected b  ≡  (a ⇝ b) ⊎ (b ⇝ a)  --  path “between” a and b; not ‘from a to b’.
</pre></li>

<li>This is an equivalence relation whose equivalence classes are called <i>the connected components of G</i>;
denote them by <code>𝒦G</code>.</li>

<li>For any category <code>𝒞</code>, define <code>𝒦 𝒞 ≔ 𝒦 (𝒰₀ 𝒞)</code> which is a subcategory of <code>𝒞</code>.</li>

<li>Phrase the connected components subcategory using a universal property,
thereby avoiding the need for quotient types.</li>

<li>Since graphmaps preserve paths, every graph map can be extended to connected components,
<code>𝒦f : 𝒦G ⟶ 𝒦H : (connected component of x) ↦ (connected component of fᵥ x)</code>.</li>

<li>Hence, we have a functor <code>𝒦 : Graph ⟶ Set</code>.</li>

<li><p>
Then there is a natural transformation <code>𝒱 ⟶ 𝒦</code>, where 𝒱 is the vertices functor.
</p>

<p>
Hint: Such a transformation means we can realise vertices as connected components and this suggests
taking assigning a vertex to the connected-component block that contains it.
</p>

<p>
yeah!
</p></li>
</ol>

<p>
Finally, if we let <code>𝒟 : 𝒮ℯ𝓉 → 𝒞𝒶𝓉</code> be the free category functor that associates each set with
the discrete category over it, then we have <code>𝒦</code> is the associated forgetful functor.
</p>
</div>
</div>

<div id="outline-container-org8dbf1c3" class="outline-3">
<h3 id="Equality-Combinators-for-Paths"><span class="section-number-3">10.2</span> Equality Combinators for Paths</h3>
<div class="outline-text-3" id="text-Equality-Combinators-for-Paths">
<p>
Here's a handy-dandy combinator for forming certain equality proofs of paths.
</p>
<div class="org-src-container">
<pre class="src src-haskell">  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Preprend preserves path equality</span>
  <span style="color: #268bd2;">&#10230;-&#8801;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y &#969;} {e <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y} {ps qs <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#8669;</span> &#969;}
      <span style="color: #268bd2;">&#8594;</span> ps <span style="color: #268bd2;">&#8801;</span> qs <span style="color: #268bd2;">&#8594;</span> (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps) <span style="color: #268bd2;">&#8801;</span> (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> qs)
  <span style="color: #268bd2;">&#10230;-&#8801;</span> {x} {y} {&#969;} {e} {ps} {qs} eq <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>cong (&#955; r <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> r) eq
</pre>
</div>
<p>
Less usefully, we leave as exercises:
</p>
<pre class="example">
  edges : ∀ {x ω} (p : x ⇝ ω) → List (Σ s ∶ V G • Σ t ∶ V G • s ⟶ t)
  edges = {! exercise !}

  path-eq : ∀ {x y} {p q : x ⇝ y} → edges p ≡ edges q → p ≡ q
  path-eq = {! exercise !}
</pre>
<p>
Given time, <code>path-eq</code> could be rewritten so as to be more easily applicable.
For now, two path equality proofs occur in the document and both are realised by
quick-and-easy induction.
</p>
</div>
</div>

<div id="outline-container-orga2c5b0c" class="outline-3">
<h3 id="Category-of-paths-over-a-graph"><span class="section-number-3">10.3</span> Category of paths over a graph</h3>
<div class="outline-text-3" id="text-Category-of-paths-over-a-graph">
<p>
Now we turn back to the problem of <a href="https://english.stackexchange.com/a/125659/327685">catenating</a> two paths.
</p>
<div class="org-src-container">
<pre class="src src-haskell">  <span style="color: #859900; font-weight: bold;">infixr</span> 5 <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">++</span><span style="color: #859900; font-weight: bold;">_</span>

  <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">++</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y z} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#8669;</span> y <span style="color: #268bd2;">&#8594;</span> y <span style="color: #268bd2;">&#8669;</span> z <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#8669;</span> z
  x <span style="color: #268bd2;">!</span> <span style="color: #268bd2;">++</span> q           <span style="color: #268bd2;">=</span> q                         <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">left unit</span>
  (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> p) <span style="color: #268bd2;">++</span> q <span style="color: #268bd2;">=</span> x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> (p <span style="color: #268bd2;">++</span> q)     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">mutual-associativity</span>
</pre>
</div>
<p>
Notice that the the base case indicate that <code>!</code> forms a left-unit for <code>++</code>,
while the inductive case says that path-formation associates with path catenation.
Both observations also hold for the definition of list catenation ;-)
</p>

<p>
If we had not typed our paths, as in <code>Path₂</code>, we would need to carry around a
proof that paths are compatible for concatenation:
</p>
<pre class="example">
  catenate : (p q : Path) (coh : end p ≡ head q) → Path
  syntax catenate p q compatibility = p ++[ compatibility ] q
</pre>
<p>
Even worse, to show <code>p ++[ coh ] q ≡ p ++[ coh’ ] q</code> we need to invoke proof-irrelevance of
identity proofs to obtain <code>coh ≡ coh’</code>, each time we want such an equality! Moving the proof
obligation to the type level removes this need.
</p>

<p>
As can be seen, being type-less is a terrible ordeal.
</p>

<p>
Just as the first clause of <code>_++_</code> indicates <code>_!</code> is a left unit,
</p>
<div class="org-src-container">
<pre class="src src-haskell">  leftId <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {x y} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">!</span> <span style="color: #268bd2;">++</span> p <span style="color: #268bd2;">&#8801;</span> p
  leftId <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
</pre>
</div>
<p>
Is it also a right identity?
</p>
<div class="org-src-container">
<pre class="src src-haskell">  rightId <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {x y} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y} <span style="color: #268bd2;">&#8594;</span> p <span style="color: #268bd2;">++</span> y <span style="color: #268bd2;">!</span> <span style="color: #268bd2;">&#8801;</span> p
  rightId {x} {<span style="color: #268bd2;">.</span>x} {<span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">!</span>} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
  rightId {x} {y } {<span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>cong (&#955; q <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> q) rightId
</pre>
</div>

<p>
Is this operation associative?
</p>
<div class="org-src-container">
<pre class="src src-haskell">  assoc <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y z &#969;} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y} {q <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#8669;</span> z} {r <span style="color: #b58900; font-style: italic;">:</span> z <span style="color: #268bd2;">&#8669;</span> &#969;}
        <span style="color: #268bd2;">&#8594;</span> (p <span style="color: #268bd2;">++</span> q) <span style="color: #268bd2;">++</span> r <span style="color: #268bd2;">&#8801;</span> p <span style="color: #268bd2;">++</span> (q <span style="color: #268bd2;">++</span> r)
  assoc {x} {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">!</span>} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
  assoc {x} {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} {q} {r} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>cong (&#955; s <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> s) (assoc {p <span style="color: #268bd2;">=</span> ps})
</pre>
</div>

<p>
Hence, we’ve shown that the paths over a graph <code>G</code> constitute a category! Let’s call it <code>𝒫 G</code>.
</p>
</div>
</div>

<div id="outline-container-org384d644" class="outline-3">
<h3 id="The-𝒫ath-to-freedom"><span class="section-number-3">10.4</span> The 𝒫ath to freedom</h3>
<div class="outline-text-3" id="text-The-𝒫ath-to-freedom">
<p>
In the last section, we showed that the paths over a graph make a category,
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Category</span>
<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">=</span> <span style="color: #859900; font-weight: bold;">let</span> open <span style="color: #b58900; font-style: italic;">TypedPaths</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #859900; font-weight: bold;">in</span>
    record
      { <span style="color: #b58900; font-style: italic;">Obj</span>     <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Graph.V</span> <span style="color: #b58900; font-style: italic;">G</span>
      ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span>     <span style="color: #268bd2;">=</span> <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8669;</span><span style="color: #859900; font-weight: bold;">_</span>
      ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span>     <span style="color: #268bd2;">=</span> <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">++</span><span style="color: #859900; font-weight: bold;">_</span>
      ; assoc   <span style="color: #268bd2;">=</span> &#955; {x y z &#969; p q r} <span style="color: #268bd2;">&#8594;</span> assoc {p <span style="color: #268bd2;">=</span> p}
      ; <span style="color: #b58900; font-style: italic;">Id</span>      <span style="color: #268bd2;">=</span> &#955; {x} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">!</span>
      ; leftId  <span style="color: #268bd2;">=</span> leftId
      ; rightId <span style="color: #268bd2;">=</span> rightId
      }
</pre>
</div>

<p>
Can we make <code>𝒫</code> into a functor by defining it on morphisms?
That is, to lift graph-maps to category-maps, i.e., functors.
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span>} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">GraphMap</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">G</span>) (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">H</span>)
<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; {<span style="color: #b58900; font-style: italic;">G</span>} {<span style="color: #b58900; font-style: italic;">H</span>} f <span style="color: #268bd2;">=</span> record
    { obj  <span style="color: #268bd2;">=</span> ver f
    ; mor  <span style="color: #268bd2;">=</span> amore
    ; id   <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
    ; comp <span style="color: #268bd2;">=</span> &#955; {x} {y} {z} {p} <span style="color: #268bd2;">&#8594;</span> comp {p <span style="color: #268bd2;">=</span> p}
    }
    <span style="color: #859900; font-weight: bold;">where</span>
      open <span style="color: #b58900; font-style: italic;">TypedPaths</span> &#10627;<span style="color: #268bd2;">...</span>&#10628; public
      <span style="color: #859900; font-weight: bold;">instance</span> <span style="color: #b58900; font-style: italic;">G'</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span> ; <span style="color: #b58900; font-style: italic;">G'</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">G</span>
               <span style="color: #b58900; font-style: italic;">H'</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span> ; <span style="color: #b58900; font-style: italic;">H'</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">H</span>

      amore <span style="color: #b58900; font-style: italic;">:</span> {x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph.V</span> <span style="color: #b58900; font-style: italic;">G</span>} <span style="color: #268bd2;">&#8594;</span>  x <span style="color: #268bd2;">&#8669;</span> y <span style="color: #268bd2;">&#8594;</span> (ver f x) <span style="color: #268bd2;">&#8669;</span> (ver f y)
      amore (x <span style="color: #268bd2;">!</span>) <span style="color: #268bd2;">=</span> ver f x <span style="color: #268bd2;">!</span>
      amore (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> p) <span style="color: #268bd2;">=</span> ver f x <span style="color: #268bd2;">&#10230;</span>[ edge f e ]<span style="color: #268bd2;">&#10230;</span> amore p

      comp <span style="color: #b58900; font-style: italic;">:</span> {x y z <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph.V</span> <span style="color: #b58900; font-style: italic;">G</span>} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y} {q <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#8669;</span> z}
          <span style="color: #268bd2;">&#8594;</span>  amore (p <span style="color: #268bd2;">++</span> q)  <span style="color: #268bd2;">&#8801;</span>  amore p <span style="color: #268bd2;">++</span> amore q
      comp {x} {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">!</span>} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">since ! is left unit of ++</span>
      comp {x} {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#10230;-&#8801;</span> (comp {p <span style="color: #268bd2;">=</span> ps})
</pre>
</div>
<p>
Sweet!
</p>

<p>
With these two together, we have that <code>𝒫</code> is a functor.
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900; font-style: italic;">&#119979;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span> <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span>
<span style="color: #b58900; font-style: italic;">&#119979;</span> <span style="color: #268bd2;">=</span> record { obj   <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320;
            ; mor  <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321;
            ; id   <span style="color: #268bd2;">=</span> &#955; {<span style="color: #b58900; font-style: italic;">G</span>} <span style="color: #268bd2;">&#8594;</span> funcext <span style="color: #268bd2;">&#8801;-</span>refl (id &#10627; <span style="color: #b58900; font-style: italic;">G</span> &#10628;)
            ; comp <span style="color: #268bd2;">=</span> funcext <span style="color: #268bd2;">&#8801;-</span>refl comp
            }
    <span style="color: #859900; font-weight: bold;">where</span>
      open <span style="color: #b58900; font-style: italic;">TypedPaths</span> &#10627;<span style="color: #268bd2;">...</span>&#10628;
      open <span style="color: #b58900; font-style: italic;">Category</span>   &#10627;<span style="color: #268bd2;">...</span>&#10628;

      <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span>   <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span>
      <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span>

      id <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> &#10627; <span style="color: #b58900; font-style: italic;">G</span> &#10628; {x y} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y}
        <span style="color: #268bd2;">&#8594;</span>   mor (<span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">Id</span> {<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">G</span>}) p  <span style="color: #268bd2;">&#8801;</span>  mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; (<span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">Id</span>)) p
      id {p <span style="color: #268bd2;">=</span> x <span style="color: #268bd2;">!</span>} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
      id {p <span style="color: #268bd2;">=</span> x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#10230;-&#8801;</span> (id {p <span style="color: #268bd2;">=</span> ps})

      comp <span style="color: #b58900; font-style: italic;">:</span> {<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #b58900; font-style: italic;">K</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>} {f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">GraphMap</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span>} {g <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">GraphMap</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #b58900; font-style: italic;">K</span>}
           <span style="color: #268bd2;">&#8594;</span> {x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph.V</span> <span style="color: #b58900; font-style: italic;">G</span>} {p <span style="color: #b58900; font-style: italic;">:</span> TypedPaths._<span style="color: #268bd2;">&#8669;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">G</span> x y}
           <span style="color: #268bd2;">&#8594;</span>  mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; f <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g) p  <span style="color: #268bd2;">&#8801;</span>  mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; (f <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> g)) p
      comp {p <span style="color: #268bd2;">=</span> x <span style="color: #268bd2;">!</span>} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
      comp {p <span style="color: #268bd2;">=</span> x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#10230;-&#8801;</span> (comp {p <span style="color: #268bd2;">=</span> ps})
</pre>
</div>

<p>
It seemed prudent in this case to explicitly delimit where the compositions lives
&#x2014;this is for clarity, since Agda can quickly resolve the appropriate category instances.
</p>

<p>
Exercise: Show that we have a natural transformation <code>Id ⟶ 𝒰 ∘ 𝒫</code>.
</p>
</div>
</div>
</div>
<div id="outline-container-orgb422b19" class="outline-2">
<h2 id="Free-at-last"><span class="section-number-2">11</span> Free at last</h2>
<div class="outline-text-2" id="text-Free-at-last">
<blockquote>
<p>
Free at last, free at last, thank God almighty we are free at last.
</p>

<p>
&#x2013; Martin Luther King Jr.
</p>
</blockquote>

<p>
Recall why lists give the ‘free monoid’: We can embed a type \(A\) into \(\List A\) by the map \([\_{}]\),
and we can lift any map \(f : A ⟶ B\) to a monoid map
\[\foldr \; (λ a b → f\, a ⊕ b)\; e \;:\; (\List A ,\_{}++\_{} , []) \,⟶\, (B,\_{}⊕\_{} , e)\]
I.e., \([a₁, …, aₖ] \;↦\; f\, a₁ ⊕ ⋯ ⊕ f\, aₖ\). Moreover
this ‘preserves the basis’ \(A\)
&#x2013; i.e., \(∀ a •\; f\, a \,=\, \foldr \,f \,e \, [ a ]\) &#x2013;
and this lifted map is unique.
</p>

<p>
Likewise, let us show that \(𝒫G\) is the ‘free category’ over the graph \(G\).
This amounts to saying that there is a way, a graph-map, say \(ι\), that embeds \(G\) into \(𝒫G\),
and a way to lift any graph-map \(f \,:\, G \,𝒢⟶\, 𝒰₀ 𝒞\) to a functor \(\mathsf{lift}\, f : 𝒫G ⟶ 𝒞\)
that ‘preserves the basis’ \(f \;=\; ι ⨾ 𝒰₁ (\mathsf{lift}\, f)\) and uniquely so.
</p>

<p>
Let’s begin!
</p>

<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> freedom (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Obj</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span>) {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {&#8467;&#8320;} {&#8467;&#8320;} } <span style="color: #859900; font-weight: bold;">where</span>

  open <span style="color: #b58900; font-style: italic;">TypedPaths</span> <span style="color: #b58900; font-style: italic;">G</span> using (<span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">!</span> ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span>[<span style="color: #859900; font-weight: bold;">_</span>]<span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> ;  <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8669;</span><span style="color: #859900; font-weight: bold;">_</span> ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">++</span><span style="color: #859900; font-weight: bold;">_</span>)
  open <span style="color: #b58900; font-style: italic;">Category</span> &#10627;<span style="color: #268bd2;">...</span>&#10628;

  <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span>
  <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> (<span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span> {&#8467;&#8320;})
  <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
  <span style="color: #859900; font-weight: bold;">instance</span> <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> ; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
</pre>
</div>
</div>

<div id="outline-container-orgd112fe8" class="outline-3">
<h3 id="Defining-the-needed-operations"><span class="section-number-3">11.1</span> Defining the needed operations</h3>
<div class="outline-text-3" id="text-Defining-the-needed-operations">
<p>
The only obvious, and most natural, way to embed a graph into its ‘graph of paths’ is to send
vertices to vertices and edges to paths of length 1.
</p>

<div class="org-src-container">
<pre class="src src-haskell">  &#953; <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">G</span>)
  &#953; <span style="color: #268bd2;">=</span> record { ver <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Id</span> ; edge <span style="color: #268bd2;">=</span> &#955; {x} {y} e  <span style="color: #268bd2;">&#8594;</span>  x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> (y <span style="color: #268bd2;">!</span>) }
</pre>
</div>

<p>
Given a graph map \(f\), following the list-analagoue of \([a₁, …, aₖ] \;↦\; f\, a₁ ⊕ ⋯ ⊕ f\, aₖ\)
we attempt to lift the map onto paths by taking the edges \(e₁, …, eₖ\) of a path
to a morphism \(\edge\, f\, e₁ ⨾ ⋯ ⨾ \edge\, f\, eₖ\).
That is, a path of the form
\[x_0 \xrightarrow{e_1} x_1 \xrightarrow{e_2} x_2 \xrightarrow{e_3} ⋯ \xrightarrow{e_k} x_k \]
Is lifted to the composition of morphisms
\[\mathsf{ver}\, f\, x_0 \xrightarrow{\edge\, f\, e_1}
   \mathsf{ver}\, f\, x_1 \xrightarrow{\edge\, f\, e_2}
   \mathsf{ver}\, f\, x_2 \xrightarrow{\edge\, f\, e_3} ⋯ \xrightarrow{\edge\, f\, e_k}
   \mathsf{ver}\, f\, x_k \]
</p>

<p>
Of course, we then need to verify that this construction is indeed
functorial.
</p>

<div class="org-src-container">
<pre class="src src-haskell">  lift <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; <span style="color: #b58900; font-style: italic;">&#119966;</span>  <span style="color: #268bd2;">&#8594;</span>  <span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
  lift f <span style="color: #268bd2;">=</span> record
     { obj  <span style="color: #268bd2;">=</span> &#955; v <span style="color: #268bd2;">&#8594;</span> ver f v <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Only way to obtain an object of &#119966;; hope it works!</span>
     ; mor  <span style="color: #268bd2;">=</span> fmap
     ; id   <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
     ; comp <span style="color: #268bd2;">=</span> &#955; {x y z p q} <span style="color: #268bd2;">&#8594;</span> proof {x} {y} {z} {p} {q}
     }
     <span style="color: #859900; font-weight: bold;">where</span>
          fmap <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {x y} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#8669;</span> y <span style="color: #268bd2;">&#8594;</span> ver f x <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10230;</span> ver f y
          fmap (y <span style="color: #268bd2;">!</span>) <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.</span><span style="color: #b58900; font-style: italic;">Id</span>
          fmap (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> p) <span style="color: #268bd2;">=</span> edge f e <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> fmap p

          <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">homomorphism property</span>
          proof <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y z} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y} {q <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#8669;</span> z} <span style="color: #268bd2;">&#8594;</span> fmap (p <span style="color: #268bd2;">++</span> q) <span style="color: #268bd2;">&#8801;</span> fmap p <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> fmap q
          proof {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">!</span>} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>sym <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.</span>leftId
          proof {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} <span style="color: #268bd2;">=</span>  <span style="color: #268bd2;">&#8801;-</span>cong (&#955; m <span style="color: #268bd2;">&#8594;</span> edge f e <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> m) (proof {p <span style="color: #268bd2;">=</span> ps})
                                     &#10216;<span style="color: #268bd2;">&#8801;&#8801;</span>&#10217; <span style="color: #268bd2;">&#8801;-</span>sym assoc
                                     <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Exercise: Rewrite this calculationally!</span>
</pre>
</div>

<p>
Now we have the embedding and the lifting, it remains to show that the aforementioned
‘preserves basis’ property holds as does uniqueness.
</p>
</div>
</div>

<div id="outline-container-org6954a65" class="outline-3">
<h3 id="Realising-the-proof-obligations"><span class="section-number-3">11.2</span> Realising the proof-obligations</h3>
<div class="outline-text-3" id="text-Realising-the-proof-obligations">
<p>
Let's begin with the ‘basis preservation’ property:
</p>

<div class="org-src-container">
<pre class="src src-haskell">  property <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; <span style="color: #b58900; font-style: italic;">&#119966;</span>}  <span style="color: #268bd2;">&#8594;</span>  f  <span style="color: #268bd2;">&#8801;</span>  (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; (lift f))
  property {f} <span style="color: #268bd2;">=</span> graphmapext
    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Proving: &#8704; {v} &#8594; ver f v &#8801; ver (&#953; &#119966;.&#10814; &#119984;&#8321; (lift f)) v</span>
    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">by starting at the complicated side and simplifying</span>
    (&#955; {v} <span style="color: #268bd2;">&#8594;</span> <span style="color: #268bd2;">&#8801;-</span>sym (begin
              ver (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; (lift f)) v
            <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of ver on composition "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
              (ver &#953; <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> ver (<span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; (lift f))) v
            <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of &#119984;&#8321; says: ver (&#119984;&#8321; F) = obj F "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
              (ver &#953; <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> obj (lift f)) v
            <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of lift says: obj (lift f) = ver f "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
              (ver &#953; <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> ver f) v
            <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of &#953; on vertices is identity "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
              ver f v
            <span style="color: #268bd2;">&#8718;</span>))

    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Proving: edge (&#953; &#10814;g &#119984;&#8321; (lift f)) e &#8801; edge f e</span>
    (&#955; {x} {y} {e} <span style="color: #268bd2;">&#8594;</span> begin
               edge (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; (lift f)) e
             <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of edge on composition "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
               (edge &#953; <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> edge (<span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; (lift f))) e
             <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of &#119984; says: edge (&#119984;&#8321; F) = mor F "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
               (edge &#953; <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> mor (lift f)) e
             <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition chasing gives: mor (lift f) (edge &#953; e) = edge f e &#10814; Id "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
               edge f e <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">Id</span>
             <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.</span>rightId &#10217;
               edge f e
             <span style="color: #268bd2;">&#8718;</span>)
</pre>
</div>

<p>
Observe that we simply chased definitions and as such <code>graphmapext ≡-refl rightId</code> suffices as a proof,
but it’s not terribly clear why, for human consumption, and so we choose to elaborate with the
detail.
</p>

<p>
Finally, it remains to show that there is a unique way to preserve ‘basis’:
</p>

<div class="org-src-container">
<pre class="src src-haskell">  uniqueness <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; <span style="color: #b58900; font-style: italic;">&#119966;</span>} {<span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #268bd2;">&#10230;</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>} <span style="color: #268bd2;">&#8594;</span> f <span style="color: #268bd2;">&#8801;</span> (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>) <span style="color: #268bd2;">&#8594;</span> lift f <span style="color: #268bd2;">&#8801;</span> <span style="color: #b58900; font-style: italic;">F</span>
  uniqueness {<span style="color: #268bd2;">.</span>(&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)} {<span style="color: #b58900; font-style: italic;">F</span>} <span style="color: #268bd2;">&#8801;-</span>refl <span style="color: #268bd2;">=</span> funcext <span style="color: #268bd2;">&#8801;-</span>refl (<span style="color: #268bd2;">&#8801;-</span>sym pf)
    <span style="color: #859900; font-weight: bold;">where</span>
      pf <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y} <span style="color: #268bd2;">&#8594;</span>  mor (lift (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)) p  <span style="color: #268bd2;">&#8801;</span>  mor <span style="color: #b58900; font-style: italic;">F</span> p
      pf {x} {<span style="color: #268bd2;">.</span>x} {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">!</span>} <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>sym (Functor.id <span style="color: #b58900; font-style: italic;">F</span>)
      pf {x} {y} {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} <span style="color: #268bd2;">=</span> begin
         mor (lift (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)) (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps)
       <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of mor on lift, the inductive clause "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
         edge (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>) e <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> mor (lift (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)) ps
       <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #268bd2;">&#8801;-</span>cong&#8322; <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.</span><span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">&#8801;-</span>refl (pf {p <span style="color: #268bd2;">=</span> ps}) &#10217; <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">inductive step</span>
         edge (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>) e <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span> ps
       <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of edge says it preserves composition "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
         (edge &#953; <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> edge (<span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)) e <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span> ps
       <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of &#119984; gives: edge (&#119984;&#8321; F) = mor F "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
         (edge &#953; <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span>) e <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span> ps
       <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of functional composition &#119982;&#8495;&#120009; "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
          mor <span style="color: #b58900; font-style: italic;">F</span> (edge &#953; e) <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span> ps
       <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #268bd2;">&#8801;-</span>sym (Functor.comp <span style="color: #b58900; font-style: italic;">F</span>) <span style="color: #96A7A9; font-style: italic;">{- </span><span style="color: #96A7A9; font-style: italic;">i.e., functors preserve composition -}</span> &#10217;
          mor <span style="color: #b58900; font-style: italic;">F</span> (edge &#953; e <span style="color: #268bd2;">++</span> ps)
       <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of embedding and concatenation "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
         mor <span style="color: #b58900; font-style: italic;">F</span> (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps)
       <span style="color: #268bd2;">&#8718;</span>
</pre>
</div>

<p>
Challenge:
Define graph-map equality ‘≈g’ by <i>extensionality</i> &#x2013;two graph maps are equal iff
their vertex <i>and</i> edge maps are extensionally equal. This is far more relaxed
than using propositional equality ‘≡’. Now show the stronger uniqueness claim:
</p>
<pre class="example">
∀{f : G ⟶ 𝒰₀ 𝒞} {F : 𝒫₀ G ⟶ 𝒞}   →   f  ≈g  (ι ⨾ 𝒰₁ F)   →   lift f  ≡  F
</pre>
</div>
</div>

<div id="outline-container-orga18e779" class="outline-3">
<h3 id="Another-freedom-proof"><span class="section-number-3">11.3</span> Another freedom proof</h3>
<div class="outline-text-3" id="text-Another-freedom-proof">
<p>
However, saying each graph-map gives rise to exactly one unique functor is tantamount to
saying the type <code>GraphMap G (𝒰₀ 𝒞)</code> is isomorphic to <code>Functor (𝒫₀ G) 𝒞</code>, that is
<code>(𝒫₀ G ⟶ 𝒞) ≅ (G ⟶ 𝒰₀ 𝒞)</code> &#x2014;observe that this says we can ‘move’ <code>𝒫₀</code> from the left to
the right of an arrow at the cost of it (and the arrow) changing.
</p>

<p>
A few healthy exercises,
</p>

<pre class="example">
  lift˘ : Functor 𝒫G 𝒞 → GraphMap G (𝒰₀ 𝒞)
  lift˘ F = ι ⨾g 𝒰₁ F  --  i.e., record {ver = obj F , edge = mor F ∘ edge ι}

  rid : ∀{f : GraphMap G (𝒰₀ 𝒞)} → ∀{x y} {e : x ⟶g y} → lift˘ (lift f) ≡ f
  rid = {! exercise !}

  lid : ∀{F : Functor 𝒫G 𝒞} → lift (lift˘ F) ≡ F
  lid = {! exercise !}
</pre>

<p>
One can of course obtain these proofs from the other ones without recourse to definitions,
however for comprehension one would do well to prove them from first principles.
The worked out solutions are available in the literate source file of this document.
</p>

<p>
We can then provide an alternative, and more succinct, proof of uniqueness for ‘basis preservation’:
</p>

<div class="org-src-container">
<pre class="src src-haskell">  uniqueness&#8217;  <span style="color: #b58900; font-style: italic;">:</span>  <span style="color: #268bd2;">&#8704;</span>{f h}   <span style="color: #268bd2;">&#8594;</span>    f  <span style="color: #268bd2;">&#8801;</span>  (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; h)   <span style="color: #268bd2;">&#8594;</span>   lift f  <span style="color: #268bd2;">&#8801;</span>  h
  uniqueness&#8217; {f} {h} f<span style="color: #268bd2;">&#8776;</span>&#953;<span style="color: #268bd2;">&#10814;</span><span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321;h <span style="color: #268bd2;">=</span> begin
      lift f
    <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #268bd2;">&#8801;-</span>cong lift f<span style="color: #268bd2;">&#8776;</span>&#953;<span style="color: #268bd2;">&#10814;</span><span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321;h &#10217;
      lift (&#953; <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; h)
    <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" definition of lift&#728; "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
      lift (lift<span style="color: #268bd2;">&#728;</span> h)
    <span style="color: #268bd2;">&#8801;</span>&#10216; lid &#10217;
      h
    <span style="color: #268bd2;">&#8718;</span>
</pre>
</div>

<p>
The difference between this proof and the original one is akin
to the difference between heaven and earth! That or it's much more elegant ;-)
</p>
</div>
</div>

<div id="outline-container-org040c202" class="outline-3">
<h3 id="𝒫-𝒰"><span class="section-number-3">11.4</span> <code>𝒫 ⊣ 𝒰</code></h3>
<div class="outline-text-3" id="text-𝒫-𝒰">
<p>
Thus far, we have essentially shown
\[(𝒫₀\, G \,⟶\, 𝒞) \quad≅\quad (G \,⟶\, 𝒰₀\, 𝒞)\]
We did so by finding a pair of inverse maps:
</p>

<pre class="example">
lift   :  (    G ⟶ 𝒰₀ 𝒞)  →  (𝒫₀ G ⟶     𝒞)
lift˘  :  (𝒫₀ G  ⟶    𝒞)  →  (   G ⟶  𝒰₀ 𝒞)
</pre>

<p>
This is nearly <code>𝒫 ⊣ 𝒰</code> which implies <code>𝒫</code> is a ‘free-functor’ since it is left-adjoint to a forgetful-functor.
</p>

<p>
‘Nearly’ since we need to exhibit naturality:
For every graph map <code>g</code> and functors <code>F, k</code> we have
<code>lift˘ (𝒫 g ⨾ k ⨾ F) ≈ g ⨾ lift˘ k ⨾ 𝒰 F</code> in the category of graphs.
</p>

<p>
<a href="http://maartenfokkinga.github.io/utwente/mmf92b.pdf">Fokkinga (Theorem A.4)</a>, among others, would call these laws ‘fusion’
instead since they inform us how to compose, or ‘fuse’, a morphism with a
<code>lift˘</code>-ed morphism: Taking <code>F</code> to be the identity and remembering that functors preserve
identities, we have that <code>g ⨾ lift˘ K ≡ lift˘( 𝒫₁ g ⨾ K)</code> &#x2013;we can push a morphism into a <code>lift˘</code>
at the cost of introducing a <code>𝒫₁</code>; dually for <code>lift</code>-ed morphisms.
</p>

<p>
First the setup,
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> <span style="color: #859900; font-weight: bold;">_</span> {<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span> {&#8467;&#8320;} {&#8467;&#8320;}}
          (g <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">GraphMap</span> <span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span>) (<span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>) <span style="color: #859900; font-weight: bold;">where</span>

  private
    lift<span style="color: #268bd2;">&#728;</span> <span style="color: #268bd2;">=</span> &#955; {<span style="color: #b58900; font-style: italic;">A</span>} {<span style="color: #b58900; font-style: italic;">C</span>} <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#8594;</span> freedom<span style="color: #268bd2;">.</span>lift<span style="color: #268bd2;">&#728;</span> <span style="color: #b58900; font-style: italic;">A</span> {<span style="color: #b58900; font-style: italic;">C</span>} <span style="color: #b58900; font-style: italic;">B</span>
    lift <span style="color: #268bd2;">=</span> &#955; {<span style="color: #b58900; font-style: italic;">A</span>} {<span style="color: #b58900; font-style: italic;">C</span>} <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#8594;</span> freedom<span style="color: #268bd2;">.</span>lift <span style="color: #b58900; font-style: italic;">A</span> {<span style="color: #b58900; font-style: italic;">C</span>} <span style="color: #b58900; font-style: italic;">B</span>
  open <span style="color: #b58900; font-style: italic;">Category</span> &#10627;<span style="color: #268bd2;">...</span>&#10628;

  <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>     <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119966;</span>
  <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>     <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119967;</span>
  <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span>
  <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span>   <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> (<span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span> {&#8467;&#8320;} {&#8467;&#8320;})
  <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span>   <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">Category</span> (<span style="color: #b58900; font-style: italic;">&#119982;e&#120009;</span> {&#8467;&#8320;})
</pre>
</div>

<p>
Just as we needed to prove two inverse laws for <code>lift</code> and <code>lift˘</code>,
we need two naturality proofs.
</p>

<div class="org-src-container">
<pre class="src src-haskell">  naturality<span style="color: #268bd2;">&#728;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">K</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Functor</span> (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8320; <span style="color: #b58900; font-style: italic;">H</span>) <span style="color: #b58900; font-style: italic;">&#119966;</span>}
              <span style="color: #268bd2;">&#8594;</span>  lift<span style="color: #268bd2;">&#728;</span> (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">K</span> <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">F</span>)  <span style="color: #268bd2;">&#8801;</span>  (g <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> lift<span style="color: #268bd2;">&#728;</span> <span style="color: #b58900; font-style: italic;">K</span> <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)
  naturality<span style="color: #268bd2;">&#728;</span> <span style="color: #268bd2;">=</span> graphmapext <span style="color: #268bd2;">&#8801;-</span>refl <span style="color: #268bd2;">&#8801;-</span>refl
</pre>
</div>

<p>
That was easier than assumed!
Hahaha: Hard to formalise but so easy to prove lolz!
It says we can ‘shunt’ <code>lift˘</code> into certain compositions at the cost
of replacing functor instances.
</p>

<p>
Now for the other proof:
</p>
<div class="org-src-container">
<pre class="src src-haskell">  naturality <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {k <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">GraphMap</span> <span style="color: #b58900; font-style: italic;">H</span> (<span style="color: #b58900; font-style: italic;">&#119984;</span>&#8320; <span style="color: #b58900; font-style: italic;">&#119966;</span>)} <span style="color: #268bd2;">&#8594;</span>     lift (g <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> k <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)
                                              <span style="color: #268bd2;">&#8801;</span>  (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> lift k <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">F</span>)
  naturality {k} <span style="color: #268bd2;">=</span> funcext <span style="color: #268bd2;">&#8801;-</span>refl (&#955; {x y p} <span style="color: #268bd2;">&#8594;</span> proof {x} {y} {p})
    <span style="color: #859900; font-weight: bold;">where</span>
      open <span style="color: #b58900; font-style: italic;">TypedPaths</span> &#10627;<span style="color: #268bd2;">...</span>&#10628;
      <span style="color: #859900; font-weight: bold;">instance</span> <span style="color: #b58900; font-style: italic;">G</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span> ; <span style="color: #b58900; font-style: italic;">G</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">G</span>
               <span style="color: #b58900; font-style: italic;">H</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span> ; <span style="color: #b58900; font-style: italic;">H</span><span style="color: #268bd2;">&#8242;</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">H</span>
      proof <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {x y <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph.V</span> <span style="color: #b58900; font-style: italic;">G</span>} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y}
            <span style="color: #268bd2;">&#8594;</span>    mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">F</span>) p
               <span style="color: #268bd2;">&#8801;</span>  mor (lift {<span style="color: #b58900; font-style: italic;">G</span>} {<span style="color: #b58900; font-style: italic;">&#119967;</span>} (g <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> k <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)) p
      proof {p <span style="color: #268bd2;">=</span> <span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">!</span>} <span style="color: #268bd2;">=</span> functor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">F</span>) preserves<span style="color: #268bd2;">-</span>identities
      proof {p <span style="color: #268bd2;">=</span> x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} <span style="color: #268bd2;">=</span> begin
            mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">F</span>) (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps)
         <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" By definition, &#8220;mor&#8221; distributes over composition "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
            (mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g) <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> mor (lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k) <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span>) (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps)
         <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" Definitions of function composition and &#8220;&#119979;&#8321; &#10814; mor&#8221; "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
            mor <span style="color: #b58900; font-style: italic;">F</span> (mor (lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k) (mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g) (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps)))
                                                  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">This explicit path is in G</span>
         <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" Lifting graph-map &#8220;g&#8221; onto a path "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
            mor <span style="color: #b58900; font-style: italic;">F</span> (mor (lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k) (ver g x <span style="color: #268bd2;">&#10230;</span>[ edge g e ]<span style="color: #268bd2;">&#10230;</span> mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g) ps))
                                                  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">This explicit path is in H</span>
         <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" Definition of &#8220;lift &#10814; mor&#8221; on inductive case for paths "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
            mor <span style="color: #b58900; font-style: italic;">F</span> (edge k (edge g e) <span style="color: #b58900; font-style: italic;">&#119966;</span><span style="color: #268bd2;">.&#10814;</span> mor (lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k) (mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g) ps))
         <span style="color: #268bd2;">&#8801;</span>&#10216; functor <span style="color: #b58900; font-style: italic;">F</span> preserves<span style="color: #268bd2;">-</span>composition &#10217;
                mor <span style="color: #b58900; font-style: italic;">F</span> (edge k (edge g e))
           <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">.&#10814;</span>  mor <span style="color: #b58900; font-style: italic;">F</span> (mor (lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k) (mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g) ps))
         <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" Definition of function composition, for top part "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
               (edge g <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> edge k <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span>) e  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8776; mor F &#8728; edge k &#8728; edge g</span>
           <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">.&#10814;</span> (mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g) <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> mor (lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k) <span style="color: #b58900; font-style: italic;">&#119982;&#8495;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> mor <span style="color: #b58900; font-style: italic;">F</span>) ps
         <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" &#8220;&#119984;&#8321; &#10814; edge = mor&#8221; and &#8220;edge&#8221; and &#8220;mor&#8221; are functorial by definition "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
                edge (g <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> k <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>) e
           <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">.&#10814;</span>  mor (<span style="color: #b58900; font-style: italic;">&#119979;</span>&#8321; g <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> lift {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} k <span style="color: #b58900; font-style: italic;">&#119966;&#119990;&#120009;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">F</span>) ps
         <span style="color: #268bd2;">&#8801;</span>&#10216; <span style="color: #96A7A9; font-style: italic;">{- </span><span style="color: #96A7A9; font-style: italic;">Inductive Hypothesis: -}</span> <span style="color: #268bd2;">&#8801;-</span>cong&#8322; <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">.</span><span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10814;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">&#8801;-</span>refl (proof {p <span style="color: #268bd2;">=</span> ps}) &#10217;
                edge (g <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> k <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>) e
           <span style="color: #b58900; font-style: italic;">&#119967;</span><span style="color: #268bd2;">.&#10814;</span>  mor (lift {<span style="color: #b58900; font-style: italic;">G</span>} {<span style="color: #b58900; font-style: italic;">&#119967;</span>} (g <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> k <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)) ps
         <span style="color: #268bd2;">&#8801;</span>&#10216;<span style="color: #2aa198;">" Definition of &#8220;lift &#10814; mor&#8221; on inductive case for paths "</span>&#10217;<span style="color: #268bd2;">&#8242;</span>
            mor (lift {<span style="color: #b58900; font-style: italic;">G</span>} {<span style="color: #b58900; font-style: italic;">&#119967;</span>} (g <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> k <span style="color: #b58900; font-style: italic;">&#119970;&#120007;&#119990;&#120005;&#119997;</span><span style="color: #268bd2;">.&#10814;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>&#8321; <span style="color: #b58900; font-style: italic;">F</span>)) (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps)
         <span style="color: #268bd2;">&#8718;</span>
</pre>
</div>

<p>
Formally, we now have an adjunction:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #b58900; font-style: italic;">&#119979;</span><span style="color: #268bd2;">&#8867;</span><span style="color: #b58900; font-style: italic;">&#119984;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">&#119979;</span> <span style="color: #268bd2;">&#8867;</span> <span style="color: #b58900; font-style: italic;">&#119984;</span>
<span style="color: #b58900; font-style: italic;">&#119979;</span><span style="color: #268bd2;">&#8867;</span><span style="color: #b58900; font-style: italic;">&#119984;</span> <span style="color: #268bd2;">=</span> record{
    &#8970;<span style="color: #859900; font-weight: bold;">_</span>&#8971; <span style="color: #268bd2;">=</span> lift<span style="color: #268bd2;">&#728;</span>
  ; &#8968;<span style="color: #859900; font-weight: bold;">_</span>&#8969; <span style="color: #268bd2;">=</span> lift
  ; lid <span style="color: #268bd2;">=</span> lid
  ; rid <span style="color: #268bd2;">=</span> &#955; {<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> c} <span style="color: #268bd2;">&#8594;</span> rid {<span style="color: #b58900; font-style: italic;">G</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} {c}
  ; lfusion <span style="color: #268bd2;">=</span> &#955; {<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> f <span style="color: #b58900; font-style: italic;">F</span> <span style="color: #b58900; font-style: italic;">K</span>} <span style="color: #268bd2;">&#8594;</span> naturality<span style="color: #268bd2;">&#728;</span> {<span style="color: #b58900; font-style: italic;">G</span>} {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} {<span style="color: #b58900; font-style: italic;">&#119967;</span>} f <span style="color: #b58900; font-style: italic;">K</span> {<span style="color: #b58900; font-style: italic;">F</span>}
  ; rfusion <span style="color: #268bd2;">=</span> &#955; {<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">H</span> <span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">&#119967;</span> f k <span style="color: #b58900; font-style: italic;">F</span>} <span style="color: #268bd2;">&#8594;</span> naturality {<span style="color: #b58900; font-style: italic;">G</span>} {<span style="color: #b58900; font-style: italic;">H</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span>} {<span style="color: #b58900; font-style: italic;">&#119967;</span>} f <span style="color: #b58900; font-style: italic;">F</span> {k} }
  <span style="color: #859900; font-weight: bold;">where</span>
    <span style="color: #859900; font-weight: bold;">module</span> <span style="color: #859900; font-weight: bold;">_</span> {<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>} {<span style="color: #b58900; font-style: italic;">&#119966;</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Category</span>} <span style="color: #859900; font-weight: bold;">where</span> open freedom <span style="color: #b58900; font-style: italic;">G</span> {<span style="color: #b58900; font-style: italic;">&#119966;</span>} public
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org10f4171" class="outline-2">
<h2 id="Folds-Over-Paths"><span class="section-number-2">12</span> Folds Over Paths</h2>
<div class="outline-text-2" id="text-Folds-Over-Paths">
<p>
Observe that for the freedom proof we recalled
that ists determine a form of quantification, ‘folding’:
given an operation ⊕, we may form the operation <code>[x₁, …, xₖ] ↦ x₁ ⊕ ⋯ ⊕ xₖ</code>.
Then used that to define our operation <code>lift</code>, whose core was essentially,
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> folding (<span style="color: #b58900; font-style: italic;">G</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>) <span style="color: #859900; font-weight: bold;">where</span>
  open <span style="color: #b58900; font-style: italic;">TypedPaths</span> <span style="color: #b58900; font-style: italic;">G</span>
  open <span style="color: #b58900; font-style: italic;">Graph</span> <span style="color: #b58900; font-style: italic;">G</span>
                                              <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Given:</span>
  fold <span style="color: #b58900; font-style: italic;">:</span> {<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>} (v <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>)               <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">realise G's vertices as X elements</span>
         (f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> x {y} (e <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>) <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">realise paths as X elements</span>
       <span style="color: #268bd2;">&#8594;</span> (<span style="color: #268bd2;">&#8704;</span> {a b} <span style="color: #268bd2;">&#8594;</span> a <span style="color: #268bd2;">&#8669;</span> b <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>)            <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Then: Any path is an X value</span>
  fold v f (b <span style="color: #268bd2;">!</span>) <span style="color: #268bd2;">=</span> v b
  fold v f (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps) <span style="color: #268bd2;">=</span> f x e (fold v f ps)
</pre>
</div>

<p>
For example, what is the length of a path?
</p>
<div class="org-src-container">
<pre class="src src-haskell">  length <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#8669;</span> y <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#8469;</span>
  length <span style="color: #268bd2;">=</span> fold (&#955; <span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">&#8594;</span> 0)          <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">single walks are length 0.</span>
                (&#955; <span style="color: #859900; font-weight: bold;">_</span> <span style="color: #859900; font-weight: bold;">_</span> n <span style="color: #268bd2;">&#8594;</span> 1 <span style="color: #268bd2;">+</span> n)  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">edges are one more than the</span>
                                    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">length of the remaining walk</span>
</pre>
</div>
<p>
Let’s verify that this is actually what we intend by the length of a path.
</p>
<div class="org-src-container">
<pre class="src src-haskell">  length<span style="color: #268bd2;">-!</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x} <span style="color: #268bd2;">&#8594;</span> length (x <span style="color: #268bd2;">!</span>) <span style="color: #268bd2;">&#8801;</span> 0
  length<span style="color: #268bd2;">-!</span> <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">True by definition of &#8220;length&#8221;: The first argument to the &#8220;fold&#8221;</span>

  length<span style="color: #268bd2;">-</span>ind <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {x y &#969;} {e <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y} {ps <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#8669;</span> &#969;}
            <span style="color: #268bd2;">&#8594;</span>  length (x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps)  <span style="color: #268bd2;">&#8801;</span>  1 <span style="color: #268bd2;">+</span> length ps
  length<span style="color: #268bd2;">-</span>ind <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">&#8801;-</span>refl
  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">True by definition of &#8220;length&#8221;: The second-argument to the &#8220;fold&#8221;</span>
</pre>
</div>

<p>
Generalising on <code>length</code>, suppose we have a ‘cost function’ <code>c</code> that assigns a cost of traversing
an edge. Then we can ask what is the total cost of a path:
</p>
<div class="org-src-container">
<pre class="src src-haskell">  path<span style="color: #268bd2;">-</span>cost <span style="color: #b58900; font-style: italic;">:</span> (c <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y}(e <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#8469;</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #268bd2;">&#8704;</span>{x y}(ps <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#8469;</span>
  path<span style="color: #268bd2;">-</span>cost c <span style="color: #268bd2;">=</span> fold (&#955; <span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">&#8594;</span> 0)           <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">No cost on an empty path.</span>
                     (&#955; x e n <span style="color: #268bd2;">&#8594;</span> c e <span style="color: #268bd2;">+</span> n) <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Cost of current edge plus</span>
                                          <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">cost of remainder of path.</span>
</pre>
</div>
<p>
Now, we have <code>length = path-cost (λ _ → 1)</code>: Length is just assigning a cost of 1 to each edge.
</p>

<p>
Under suitable conditions, list fold distributes over list catenation, can we find an analogue
for paths? Yes. Yes, we can:
</p>
<div class="org-src-container">
<pre class="src src-haskell">  fold<span style="color: #268bd2;">-++</span> <span style="color: #b58900; font-style: italic;">:</span>  <span style="color: #268bd2;">&#8704;</span>{<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>} {v <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>} {g <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> x {y} (e <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>}
          <span style="color: #268bd2;">&#8594;</span> (<span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8853;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>)
          <span style="color: #268bd2;">&#8594;</span> <span style="color: #268bd2;">&#8704;</span>{x y z <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">V</span>} {p <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#8669;</span> y} {q <span style="color: #b58900; font-style: italic;">:</span> y <span style="color: #268bd2;">&#8669;</span> z}
          <span style="color: #268bd2;">&#8594;</span> (unitl <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{x y} <span style="color: #268bd2;">&#8594;</span> y <span style="color: #268bd2;">&#8801;</span> v x <span style="color: #268bd2;">&#8853;</span> y)        <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Image of &#8216;v&#8217; is left unit of &#8853;</span>
          <span style="color: #268bd2;">&#8594;</span> (assoc <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {x y z} <span style="color: #268bd2;">&#8594;</span> x <span style="color: #268bd2;">&#8853;</span> (y <span style="color: #268bd2;">&#8853;</span> z) <span style="color: #268bd2;">&#8801;</span> (x <span style="color: #268bd2;">&#8853;</span> y) <span style="color: #268bd2;">&#8853;</span> z )  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8853; is associative</span>
          <span style="color: #268bd2;">&#8594;</span> <span style="color: #859900; font-weight: bold;">let</span> f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> x {y} (e <span style="color: #b58900; font-style: italic;">:</span> x <span style="color: #268bd2;">&#10230;</span> y) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>
                f <span style="color: #268bd2;">=</span> &#955; x e ps <span style="color: #268bd2;">&#8594;</span> g x e <span style="color: #268bd2;">&#8853;</span> ps
             <span style="color: #859900; font-weight: bold;">in</span>
               fold v f (p <span style="color: #268bd2;">++</span> q) <span style="color: #268bd2;">&#8801;</span> fold v f p <span style="color: #268bd2;">&#8853;</span> fold v f q
  fold<span style="color: #268bd2;">-++</span> {g <span style="color: #268bd2;">=</span> g} <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8853;</span><span style="color: #859900; font-weight: bold;">_</span> {x <span style="color: #268bd2;">=</span> x} {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">!</span>} unitl assoc <span style="color: #268bd2;">=</span>  unitl
  fold<span style="color: #268bd2;">-++</span> {g <span style="color: #268bd2;">=</span> g} <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8853;</span><span style="color: #859900; font-weight: bold;">_</span> {x <span style="color: #268bd2;">=</span> x} {p <span style="color: #268bd2;">=</span> <span style="color: #268bd2;">.</span>x <span style="color: #268bd2;">&#10230;</span>[ e ]<span style="color: #268bd2;">&#10230;</span> ps} unitl assoc <span style="color: #268bd2;">=</span>
    <span style="color: #268bd2;">&#8801;-</span>cong (&#955; exp <span style="color: #268bd2;">&#8594;</span> g x e <span style="color: #268bd2;">&#8853;</span> exp) (fold<span style="color: #268bd2;">-++</span> <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8853;</span><span style="color: #859900; font-weight: bold;">_</span> {p <span style="color: #268bd2;">=</span> ps} unitl assoc) &#10216;<span style="color: #268bd2;">&#8801;&#8801;</span>&#10217; assoc
</pre>
</div>

<p>
Compare this with the proof-obligation of <code>lift</code>.
</p>
</div>

<div id="outline-container-org9da341e" class="outline-3">
<h3 id="Lists-are-special-kinds-of-paths"><span class="section-number-3">12.1</span> Lists are special kinds of paths</h3>
<div class="outline-text-3" id="text-Lists-are-special-kinds-of-paths">
<p>
We called our path catenation <code>_++_</code>, why the same symbol as that for
list catenation?
</p>

<p>
How do we interpret a list over \(A\) as a graph?
Well the vertices can be any element of \(A\)
and an edge \(x ⟶ y\) merely indicates that
‘‘the item after \(x\) in the list is the element \(y\)’’,
so we want it to be always true; or always inhabited
without distinction of the inhabitant:
So we might as well use a unit type.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #859900; font-weight: bold;">module</span> lists (<span style="color: #b58900; font-style: italic;">A</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>) <span style="color: #859900; font-weight: bold;">where</span>

  open <span style="color: #859900; font-weight: bold;">import</span> <span style="color: #b58900; font-style: italic;">Data.Unit</span>

  listGraph <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Graph</span>
  listGraph <span style="color: #268bd2;">=</span> record { <span style="color: #b58900; font-style: italic;">V</span> <span style="color: #268bd2;">=</span> <span style="color: #b58900; font-style: italic;">A</span> ; <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#10230;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #268bd2;">=</span> &#955; a a&#8217; <span style="color: #268bd2;">&#8594;</span> <span style="color: #268bd2;">&#8868;</span> }
</pre>
</div>
<p>
I haven’t a clue if this works, you read my reasoning above.
</p>

<p>
The only thing we can do is test our hypothesis by looking at the
typed paths over this graph. In particular, we attempt to show every
non-empty list of \(A\)’s corresponds to a path. Since a typed path needs
a priori the start and end vertes, let us construe
<code>List A  ≅  Σ n ∶ ℕ • Fin n → A</code>
&#x2013;later note that <code>Path G  ≅  Σ n ∶ ℕ • [n] 𝒢⟶ G</code>.
</p>
<div class="org-src-container">
<pre class="src src-haskell">  open <span style="color: #b58900; font-style: italic;">TypedPaths</span> listGraph
  open folding listGraph

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Every non-empty list [x&#8320;, &#8230;, x&#8342;] of A&#8217;s corresonds to a path x&#8320; &#8669; x&#8342;.</span>
  toPath <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{n} (list <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Fin</span> (suc n) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">A</span>) <span style="color: #268bd2;">&#8594;</span>  list fzero <span style="color: #268bd2;">&#8669;</span> list (from&#8469; n)
  toPath {zero} list <span style="color: #268bd2;">=</span> list fzero <span style="color: #268bd2;">!</span>
  toPath {suc n} list <span style="color: #268bd2;">=</span> list fzero <span style="color: #268bd2;">&#10230;</span>[ tt ]<span style="color: #268bd2;">&#10230;</span> toPath {n} (&#955; i <span style="color: #268bd2;">&#8594;</span> list(fsuc i))
    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Note that in the inductive case, &#8220;list : Fin (suc (suc n)) &#8594; A&#8221;</span>
    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">whereas &#8220;suc &#10814; list : Fin (suc n) &#8594; A&#8221;.</span>
    <span style="color: #96A7A9; font-style: italic;">--</span>
    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">For example, if &#8220;list &#8776; [x , y , z]&#8221; yields</span>
    <span style="color: #96A7A9; font-style: italic;">--          </span><span style="color: #96A7A9; font-style: italic;">&#8220;fsuc &#10814; list &#8776; [y , z ]&#8221; and</span>
    <span style="color: #96A7A9; font-style: italic;">--   </span><span style="color: #96A7A9; font-style: italic;">&#8220;fsuc &#10814; fsuc &#10814; list &#8776; [z]&#8221;.</span>
</pre>
</div>
<p>
Hm! Look at that, first guess and it worked! Sweet.
</p>

<p>
Now let’s realize the list fold as an instance of path fold,
</p>
<div class="org-src-container">
<pre class="src src-haskell">  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">List type former</span>
  <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #268bd2;">=</span> &#955; (<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#931;</span> n <span style="color: #268bd2;">&#8758;</span> <span style="color: #b58900; font-style: italic;">&#8469;</span> <span style="color: #268bd2;">&#8226;</span> (<span style="color: #b58900; font-style: italic;">Fin</span> n <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>)

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Usual list folding, but it's in terms of path folding.</span>
  foldr <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{<span style="color: #b58900; font-style: italic;">B</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>} (f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span>) (e <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">B</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span>
  foldr f e (zero , l) <span style="color: #268bd2;">=</span> e
  foldr f e (suc n , l) <span style="color: #268bd2;">=</span> fold (&#955; a <span style="color: #268bd2;">&#8594;</span> f a e) (&#955; a <span style="color: #859900; font-weight: bold;">_</span> rem <span style="color: #268bd2;">&#8594;</span> f a rem) (toPath l)

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">example</span>
  listLength <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">&#8469;</span> <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">result should clearly be &#8220;proj&#8321;&#8221; of the list, anyhow:</span>
  listLength <span style="color: #268bd2;">=</span> foldr
    (&#955; a rem <span style="color: #268bd2;">&#8594;</span> 1 <span style="color: #268bd2;">+</span> rem) <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Non-empty list has length 1 more than the remainder.</span>
    0                    <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Empty list has length 0.</span>
</pre>
</div>

<p>
Let’s prepare for a more useful example
</p>
<div class="org-src-container">
<pre class="src src-haskell">  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Empty list</span>
  <span style="color: #b58900; font-style: italic;">[]</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">X</span>
  <span style="color: #b58900; font-style: italic;">[]</span> <span style="color: #268bd2;">=</span> 0 , &#955; <span style="color: #b58900; font-style: italic;">()</span>

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Cons for lists</span>
  <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8759;</span><span style="color: #859900; font-weight: bold;">_</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{<span style="color: #b58900; font-style: italic;">X</span> <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">Set</span>} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">X</span>
  <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8759;</span><span style="color: #859900; font-weight: bold;">_</span> {<span style="color: #b58900; font-style: italic;">X</span>} x (n , l) <span style="color: #268bd2;">=</span> 1 <span style="color: #268bd2;">+</span> n , cons x l
    <span style="color: #859900; font-weight: bold;">where</span>
      <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">&#8220;cons a l  &#8776;  &#955; i : Fin (1 + n) &#8594; if i &#8776; 0 then a else l i&#8221;</span>
      cons <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span>{n} <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span> <span style="color: #268bd2;">&#8594;</span> (<span style="color: #b58900; font-style: italic;">Fin</span> n <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>) <span style="color: #268bd2;">&#8594;</span> (<span style="color: #b58900; font-style: italic;">Fin</span> (suc n) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">X</span>)
      cons x l fzero <span style="color: #268bd2;">=</span> x
      cons x l (fsuc i) <span style="color: #268bd2;">=</span> l i

  map <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #268bd2;">&#8704;</span> {<span style="color: #b58900; font-style: italic;">B</span>} (f <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">B</span>) <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">B</span>
  map f <span style="color: #268bd2;">=</span>  foldr (&#955; a rem <span style="color: #268bd2;">&#8594;</span> f a <span style="color: #268bd2;">&#8759;</span> rem) <span style="color: #b58900; font-style: italic;">[]</span>  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">looks like the usual map don&#8217;t it ;)</span>

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">list concatenation</span>
  <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">++</span>&#8467;_ <span style="color: #b58900; font-style: italic;">:</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">A</span> <span style="color: #268bd2;">&#8594;</span> <span style="color: #b58900; font-style: italic;">List</span> <span style="color: #b58900; font-style: italic;">A</span>
  l <span style="color: #268bd2;">++</span>&#8467; r <span style="color: #268bd2;">=</span> foldr <span style="color: #859900; font-weight: bold;">_</span><span style="color: #268bd2;">&#8759;</span><span style="color: #859900; font-weight: bold;">_</span> r l <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">fold over &#8216;l&#8217; by consing its elements infront of &#8216;r&#8217;</span>

  <span style="color: #96A7A9; font-style: italic;">-- </span><span style="color: #96A7A9; font-style: italic;">Exercise: Write path catenation as a path-fold.</span>
</pre>
</div>

<p>
These few adventures would suggest that much of list theory can be
brought over to the world of paths. It looks promising, let me know
dear reader if you make progress on related explorations!
</p>
</div>
</div>
</div>
<div id="outline-container-org07f3eb4" class="outline-2">
<h2 id="That-was-fun-Bye"><span class="section-number-2">13</span> That was fun; Bye!</h2>
<div class="outline-text-2" id="text-That-was-fun-Bye">
<p>
This note took longer to write than I had initally assumed; perhaps I should have taken into
account
</p>
<dl class="org-dl">
<dt>Hofstadter’s Law</dt><dd><p>
It always takes longer than you expect, even when you take into account Hofstadter’s Law.
</p>

<p>
─<a href="https://en.wikipedia.org/wiki/G%C3%B6del,_Escher,_Bach">Gödel, Escher, Bach: An Eternal Golden Braid</a>
</p></dd>
</dl>

<p>
Lessons learned:
</p>

<ul class="org-ul">
<li>In Agda, Use implicits when possible in-favour of instance variables
since the former can be inferred from the local context,
whereas the latter must be resolved using the entire global context
thereby incurring possibly more unification problems to solve
thereby costing more time.</li>

<li>If you really want to learn something, teach it to someone:
A proof assistant wont let you get away with skipping over anything!</li>

<li>Coming up with the right data representation for the tasks being tackled
is a matter of discovery!</li>
</ul>

<p>
The astute reader may have noticed that the tone of writing sometimes
changes drastically. This is because some of this article was written
by me in March 2016 and I wished to preserve interesting writing style
I then had &#x2013;if anything to contrast with my now somewhat semi-formal style.
</p>

<p>
This article was motivated while I was reading <a href="https://www.amazon.ca/Conceptual-Mathematics-First-Introduction-Categories/dp/052171916X">Conceptual Mathematics</a>
for fun. One of the problems was to show that paths over a graph form
a category and do so freely. It took me about 20 minutes on paper and pencil,
but this resulting mechanisation took much more time &#x2013;but it was also
much more fun!
</p>

<p>
I had fun writing this up &amp; I hope you enjoy it too :-)
</p>

<dl class="org-dl">
<dt>Highly Recommended Read</dt><dd>The diligent reader may be interested to know that Maarten Fokkinga has written a very
accessible and <a href="http://maartenfokkinga.github.io/utwente/mmf92b.pdf">gentle introduction to category theory using the calculational approach</a>.</dd>
</dl>

<small>
<div class="org-center">
<p>
( This article is not yet ‘done’, but good enough for now. )
</p>
</div>
</small>
</div>
</div>
<div class="taglist"><a href="https://alhassy.github.io/tags.html">Tags</a>: <a href="https://alhassy.github.io/tag-category-theory.html">category-theory</a> <a href="https://alhassy.github.io/tag-agda.html">agda</a> <a href="https://alhassy.github.io/tag-types.html">types</a> </div><center><strong> Generated by Emacs and Org-mode (•̀ᴗ•́)و </strong></center><center><a href="PathCat.org.html"><img
   src="https://img.shields.io/badge/-Source-informational?logo=read-the-docs"></a>&emsp;<a href="https://github.com/alhassy/alhassy.github.io/commits/master/posts/PathCat.org"><img
   src="https://img.shields.io/badge/-History-informational?logo=github"></a><br><a href="https://www.buymeacoffee.com/alhassy"><img src="https://img.shields.io/badge/-buy_me_a%C2%A0coffee-gray?logo=buy-me-a-coffee"></a></center>]]></description>
  <category><![CDATA[category-theory]]></category>
  <category><![CDATA[agda]]></category>
  <category><![CDATA[types]]></category>
  <link>https://alhassy.github.io/PathCat.html</link>
  <guid>https://alhassy.github.io/PathCat.html</guid>
  <pubDate>Mon, 24 Dec 2018 19:29:00 -0500</pubDate>
</item>
<item>
  <title><![CDATA[Discovering Heyting Algebra]]></title>
  <description><![CDATA[
<nav id="table-of-contents">
<h2> <a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a> </h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#Meet-Semi-Lattices">1. <a href="#Meet-Semi-Lattices">Meet Semi-Lattices</a></a>
<ul>
<li><a href="#Examples">1.1. <a href="#Examples">Examples</a></a></li>
<li><a href="#The-Principle-of-Indirect-Equality">1.2. <a href="#The-Principle-of-Indirect-Equality">The Principle of Indirect Equality</a></a></li>
</ul>
</li>
<li><a href="#Relative-Pseudo-complements">2. <a href="#Relative-Pseudo-complements">Relative Pseudo-complements</a></a></li>
<li><a href="#Theorems-Generalising-Observations-from-the-Examples">3. <a href="#Theorems-Generalising-Observations-from-the-Examples">Theorems &#x2014; Generalising Observations from the Examples</a></a></li>
<li><a href="#Furnishes">4. <a href="#Furnishes">→ Furnishes ⊓</a></a></li>
<li><a href="#Internalising-and">5. <a href="#Internalising-and">Internalising ⊑ and ⊓</a></a></li>
<li><a href="#Flipping-the-Inclusions-Around">6. <a href="#Flipping-the-Inclusions-Around">Flipping the Inclusions Around</a></a></li>
<li><a href="#Almost-True-Complements">7. <a href="#Almost-True-Complements">Almost <i>True</i> Complements</a></a></li>
<li><a href="#So-long-and-thanks-for-the-fish">8. <a href="#So-long-and-thanks-for-the-fish">So long and thanks for the fish</a></a></li>
</ul>
</div>
</nav>

<div id="myMathJaxStuff" style="display: none;">
<p>
We do not want the MathJax declarations to use vertical whitespace;
so we do not display their residual whitespace.
</p>

<p>
Moreover the form of MathJax declarations differs from usual LaTeX
declarations, so we iffalse&#x2026;fi these ones.
</p>

<p>
Commenting the MathJax using &lt;!&#x2013; HTML comments &#x2013;&gt;
makes the commands inaccessible elsewhere.
</p>

<p>
The alternative is to declare a <a href="https://stackoverflow.com/questions/1992114/how-do-you-create-a-hidden-div-that-doesnt-create-a-line-break-or-horizontal-sp">non-displayed ~div</a>~.
</p>

<p>
\[\newcommand{\step}[1]{ \\ = \;\; & \qquad \color{maroon}{⟨ \text{ #1 } ⟩} \\ & }\]
\[\newcommand{\stepWith}[2]{ \\ #1 \;\; & \qquad \color{maroon}{⟨ \text{ #2 } ⟩} \\ & }\]
\[\newenvironment{calc}{\begin{align*} & }{\end{align*}}\]
</p>

<p>
Having identical label references for different equations will break rendering!
</p>

<p>
eqn            := display name, then display formula.
eqnColour      := display name, then display formula, then colour
eqnLabel       := display name, then display formula, then label
eqnLabelColour := display name, then display formula, then label, then colour
eqnLabelColour := display name, then display formula, then colour, then label &#x2013; safe redundancy!
</p>

<p>
\[\newcommand{\eqnLabelColour}[4]{ \begin{equation} \color{#4}{#2} \label{#3}\tag{$\color{#4}{\text{#1}}$} \end{equation} }\]
</p>

<p>
\[\newcommand{\eqnColourLabel}[4]{ \eqnLabelColour{#1}{#2}{#4}{#3} }\]
</p>

<p>
Default equation colour is: navy
\[\newcommand{\eqnLabel}[3]{ \eqnLabelColour{#1}{#2}{#3}{navy} }\]
</p>

<p>
Default label is the display name
\[\newcommand{\eqnColour}[3]{ \eqnLabelColour{#1}{#2}{#1}{#3} }\]
</p>

<p>
\[\newcommand{\eqn}[2]{ \eqnLabel{#1}{#2}{#1} }\]
</p>

<p>
Notice that \ref{Label} and \ref{Label2} have the same displayed name,
but <b>cannot</b> have the same label!
</p>

<p>
\[\newcommand{\givens}[1]{ \color{teal}{#1} }\]
\[\newcommand{\requireds}[1]{ \color{navy}{#1} }\]
</p>

<p>
\[\def\lands{\;\land\;}\]
\[\def\landS{\quad\land\quad}\]
</p>

<p>
\[\def\impliess{\;\Rightarrow\;}\]
\[\def\impliesS{\quad\Rightarrow\quad}\]
</p>

<p>
\[\def\equivs{\;\equiv\;}\]
\[\def\equivS{\quad\equiv\quad}\]
</p>

<p>
\[\def\eqs{\;=\;}\]
\[\def\eqS{\quad=\quad}\]
</p>

<p>
\[\def\sqleqs{\;\sqsubseteq\;}\]
\[\def\sqleqS{\quad\sqsubseteq\quad}\]
</p>

<p>
\[\def\foldr{\mathsf{foldr}}\]
\[\def\edge{\mathsf{edge}}\]
\[\def\Func{\mathsf{Func}}\]
\[\def\Id{\mathsf{Id}}\]
\[\def\src{\mathsf{src}}\]
\[\def\tgt{\mathsf{tgt}}\]
\[\def\obj{\mathsf{obj}}\]
\[\def\mor{\mathsf{mor}}\]
\[\def\natTo{\overset{.}{→}}\]
\[\def\Obj{\mathsf{Obj}\,}\]
\[\def\List{\mathsf{List}\,}\]
</p>
<p>
After this, only pure latex commands should exist.
</p>

<p>
However, the whitespace they produce via MathJax is ignored
since the HTML div is not yet closed.
</p>


</div>

<nav id="table-of-contents">
<h2> <a href="javascript:window.scrollTo(0,0)" style="color: black !important; border-bottom: none !important;" class="tooltip" title="Go to the top of the page"> Ξ </a> </h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#Meet-Semi-Lattices">1. <a href="#Meet-Semi-Lattices">Meet Semi-Lattices</a></a>
<ul>
<li><a href="#Examples">1.1. <a href="#Examples">Examples</a></a></li>
<li><a href="#The-Principle-of-Indirect-Equality">1.2. <a href="#The-Principle-of-Indirect-Equality">The Principle of Indirect Equality</a></a></li>
</ul>
</li>
<li><a href="#Relative-Pseudo-complements">2. <a href="#Relative-Pseudo-complements">Relative Pseudo-complements</a></a></li>
<li><a href="#Theorems-Generalising-Observations-from-the-Examples">3. <a href="#Theorems-Generalising-Observations-from-the-Examples">Theorems &#x2014; Generalising Observations from the Examples</a></a></li>
<li><a href="#Furnishes">4. <a href="#Furnishes">→ Furnishes ⊓</a></a></li>
<li><a href="#Internalising-and">5. <a href="#Internalising-and">Internalising ⊑ and ⊓</a></a></li>
<li><a href="#Flipping-the-Inclusions-Around">6. <a href="#Flipping-the-Inclusions-Around">Flipping the Inclusions Around</a></a></li>
<li><a href="#Almost-True-Complements">7. <a href="#Almost-True-Complements">Almost <i>True</i> Complements</a></a></li>
<li><a href="#So-long-and-thanks-for-the-fish">8. <a href="#So-long-and-thanks-for-the-fish">So long and thanks for the fish</a></a></li>
</ul>
</div>
</nav>

<p>
We attempt to motivate the structure of a Heyting Algebra
by considering ‘inverse problems’.
</p>

<p>
For example,
</p>
<ul class="org-ul">
<li>You have a secret number \(x\) and your friend has a secret number \(y\), which you've
communicated to each other in person.</li>
<li>You communicate a ‘message’ to each other
by adding onto your secret number.</li>
<li>Hence, if I receive a number \(z\), then I can <i>undo</i> the addition operation to find the ‘message’ \(m = z - y\).</li>
</ul>

<p>
What if we decided, for security, to change our protocol from using addition to using
minimum. That is, we encode our message \(m\) as \(z = x ↓ m\). Since minimum is not
invertible, we decide to send our encoded messages with a ‘context’ \(c\) as a pair \((z, c)\).
From this pair, a unique number \(m′\) can be extracted, which is not necessarily the original \(m\).
Read on, and perhaps you'll figure out which messages can be communicated 😉
</p>

<p>
This exploration demonstrates that relative pseudo-complements
</p>
<ul class="org-ul">
<li>Are admitted by the usual naturals precisely when infinity is considered a number;</li>
<li>Are <i>exactly</i> implication for the Booleans;</li>
<li><i>Internalises</i> implication for sets;</li>
<li>Yield <i>the largest complementary subgraph</i> when considering subgraphs.</li>
</ul>

<p>
In some sense, the pseudo-complement is the “best approximate inverse” to forming meets, minima, intersections.
</p>

<p>
Along the way we develop a number of the theorems describing the relationships
between different structural components of Heyting Algebras;
most notably the internalisation of much of its own structure.
</p>

<p>
The article aims to be self-contained, however it may be helpful to
look at <a href="https://alhassy.github.io/CatsCheatSheet/LatticesCheatSheet.pdf">this lattice cheat sheet</a> (•̀ᴗ•́)و
</p>

<small> <center>
<p>
( Photo by
<a href="https://unsplash.com/@ludovicphoto?utm_medium=referral&amp;utm_campaign=photographer-credit&amp;utm_content=creditBadge">Ludovic Fremondiere</a>
on <a href="https://unsplash.com/">Unsplash</a> )
</p>
</center> </small>

<div id="outline-container-orgb1ad4e6" class="outline-2">
<h2 id="Meet-Semi-Lattices"><span class="section-number-2">1</span> <a href="#Meet-Semi-Lattices">Meet Semi-Lattices</a></h2>
<div class="outline-text-2" id="text-Meet-Semi-Lattices">
<p>
Recall that an <i>order</i> is nothing more than a type \(Carrier\) with a relation
\(\_⊑\_\) such that the following list of axioms holds: For any \(x,y,z : Carrier\),
</p>

<p>
\[\eqn{⊑-reflexive}{x ⊑ x}\]
</p>

<p>
\[\eqn{⊑-transitive}{x ⊑ y \landS y ⊑ z \impliesS x ⊑ z}\]
</p>

<p>
\[\eqn{⊑-antisymmetric}{x ⊑ y \landS y ⊑ x \impliesS x = y}\]
</p>

<p>
This models notions of containment, inclusion, hierarchy, and approximation.
</p>

<p>
Recall that a <i>meet</i> is an additional operation \(\_⊓\_\) specified by
\[\eqnColour{Meet Characterisation}{ z \;⊑\; x ⊓ y \equivS z ⊑ x \landS z ⊑ y}{green}\]
</p>

<p>
Meets model notions of <i>greatest lower bounds</i>, or <i>infima</i>.
</p>

<p>
Observe that <i>meets allow us to encode a pair of inclusions as a single inclusion!</i>
That is, an <i>external</i> conjunction \(∧\) is equivalent to an <i>internal</i> meet \(⊓\).
</p>

<p>
In computing parlance, think of a top-level ‘method’ that takes and yields
data. In many languages, the method itself can be thought of as data!
It is this kind of external-internal duality we are alluding to
&#x2013;and is captured nicely by exponential objects in category theory,
of which we are considering a special case.
</p>
</div>

<div id="outline-container-orgcc5aa32" class="outline-3">
<h3 id="Examples"><span class="section-number-3">1.1</span> <a href="#Examples">Examples</a></h3>
<div class="outline-text-3" id="text-Examples">
<ul class="org-ul">
<li>(ℕ, ≤, ↓)   &#x2014;where ↓ denotes minimum.</li>
<li>(ℕ, ∣, gcd)  &#x2013;the naturals ordered by divisibility.</li>
<li>(Sets, ⊆, ∩)</li>
<li>(𝔹, ⇒, ∧)</li>
<li>Formulae of a logic ordered by the <i>proves</i>, or <i>syntactic consequence</i>, relationship \(\_⊢\_\) with
syntactic conjunction as meet.</li>
<li>(Sets, →, ×): Sets with “A atmost B” meaning there is a function \(f : A → B\),
then the Cartesian product can be used as meet.
<ul class="org-ul">
<li>This is not strictly a semi-lattice, as defined here, but it is when we consider ‘setoids’.</li>
<li>Moreover it provides excellent intuition!</li>
</ul></li>
<li>Subgraphs of a given simple graph \(G = (V,E)\) &#x2013;which is just a relation \(E\) on a set of vertices \(V\).
<ul class="org-ul">
<li>The ordering is obtained by subset inclusion, \[(V₁, E₁) ⊑ (V₂, E₂) \equivS V₁ ⊆ V₂ \lands E₁ ⊆ E₂\]
and so the meet is obtained by intersection, \[(V₁, E₁) ⊓ (V₂, E₂) = (V₁ ∩ V₂, E₁ ∩ E₂)\]</li>
<li><p>
However, we cannot define complement in the same fashion, since the resulting relation
is not over the resulting vertex set!
</p>

<p>
Indeed suppose we define \(∼ (V′, E′) \;:=\; (V - V′, E - E′)\), then
the discrete graph \((V, ø)\) has complement \((ø, E)\) which is a ‘graph’
with many edges \(E\) but no vertices! &#x2014;Provided \((V,E)\) has edges.
</p>

<p>
However, the edges can be <i>relativised</i> to the vertices to produce the pseudo-complement.
</p></li>
</ul></li>
</ul>
</div>
</div>

<div id="outline-container-orga0a56d9" class="outline-3">
<h3 id="The-Principle-of-Indirect-Equality"><span class="section-number-3">1.2</span> <a href="#The-Principle-of-Indirect-Equality">The Principle of Indirect Equality</a></h3>
<div class="outline-text-3" id="text-The-Principle-of-Indirect-Equality">
<p>
It is not clear how \(\ref{Meet Characterisation}\) can be used to <i>prove</i>
properties about meet.
</p>

<p>
The laws \ref{⊑-antisymmetric} and \ref{⊑-reflexive} can be fused together
into one law:
\[\eqnColour{Indirect Equality}{ x = y \equivS \left(∀ z \;•\quad ⊑\; z ⊑ x \equivs z ⊑ y\right)}{green}\]
</p>

<p>
That is, <i>for all practical purposes, \(x = y\) precisely when they have the same sub-parts!</i>
</p>

<p>
Using this, we can prove that meet is idempotent \(x ⊓ x = x\),
symmetric \(x ⊓ y = y ⊓ x\), associative \(x ⊓ (y ⊓ z) = (x ⊓ y) ⊓ z\),
and monotonic: \(a ⊑ b \landS c ⊑ d \impliesS a ⊓ c \sqleqS b ⊓ c\).
</p>

<p>
Below we use this principle in a number of places, for example \ref{Distributivity of → over ⊓}.
</p>
</div>
</div>
</div>

<div id="outline-container-org5ebda95" class="outline-2">
<h2 id="Relative-Pseudo-complements"><span class="section-number-2">2</span> <a href="#Relative-Pseudo-complements">Relative Pseudo-complements</a></h2>
<div class="outline-text-2" id="text-Relative-Pseudo-complements">
<p>
The <i>relative pseudo-complement of \(a\) wrt \(b\)</i>, \((a ⟶ b)\),
is <i>“the largest element \(x\) that ensures modus ponens”</i>, i.e.,
<i>“the largest element \(x\) whose meet with \(a\) is contained in \(b\)”</i>:
</p>

<p>
\[\eqnColour{Relative Pseudo-Complement Characterisation}{\hspace{-5em} a ⊓ x \;⊑\; b  \equivS  x \;⊑\; (a → b)}{green}\]
</p>

<p>
This is also sometimes denoted \(a ➩ b\) or \(a \backslash b\).
</p>

<p>
In five of the above settings this becomes,
</p>
<ul class="org-ul">
<li><p>
\(m ↓ x ≤ n  \equivS  x ≤ (m → n)\)
</p>

<p>
Not at all clear what to do so looking for a counterexample
shows that pseduocomplements cannot exist for the naturals
otherwise selecting \(m ≔ n\) yields:
</p>

\begin{calc}
   ∀ x \;•\quad  n ↓ x \;≤\; n \equivS x \;≤\; (n → n)
\step{ Weakening: The minimum is at least both its arguments }
   ∀ x \;•\quad  \mathsf{true} \equivS x \;≤\; (n → n)
\step{ Identity of ≡ }
   ∀ x \;•\quad  x \;≤\; (n → n)
\step{ Definition of Infinity }
   (n → n) \;=\; +∞
\end{calc}

<p>
Thus the existence of psquedo-complements implies the existence of a top element “\((n ⟶ n) = +∞\)”!
</p>

<p>
Okay, no problem: Let's consider 𝒩, the set of natural numbers along with a new maximum element “+∞”;
then we can define a relative pseudo-complement.
\[\eqn{Definition of → for 𝒩}{m → n \quad=\quad \mathsf{if}\; m > n \;\mathsf{then}\; n \;\mathsf{else}\; +∞ \;\mathsf{fi}}\]
We now have a way to approximate an inverse to minima, which is in general not invertible.
</p>

<p>
&#x2013;This definition works for any linear order with a top element&#x2014;
</p></li>

<li><p>
\(p ∧ x ⇒ q  \equivS  x ⇒ (p → q)\)
</p>

<p>
Starting with the right side,
</p>
\begin{calc}
   x \impliesS (p → q)
\step{ Characterisation of pseudo-complement }
   p ∧ x \impliesS q
\step{ Symmetry of ∧ }
   x ∧ p \impliesS q
\step{ Shunting }
   x \impliesS (p ⇒ q)
\end{calc}

<p>
Hence, by indirect equality, \(p → q \;=\; p ⇒ q \;=\; ¬ p ∨ q\).
</p></li>

<li><p>
\((A ∩ X)  \;⊆\;  B  \equivS  X  \;⊆\;  (A → B)\)
</p>

<p>
Where we can similarly verify \((A → B) \;\;=\;\; ∼ A ∪ B\).
</p>

<p>
It is interesting to note that \(x ∈ (A → B) \equivS x ∈ A \impliess x ∈ B\).
</p></li>

<li><p>
\(G ⊓ X \;⊑\; G′  \equivS  X \;⊑\; (G → G′)\)
</p>

<p>
We disclosed earlier that subgraph difference did not yield valid subgraphs,
but if we relativised the resulting edge relationship to only consider the
resulting vertex set, then we do have a subgraph.
</p>

<p>
That is, subgraphs admit relative pseduo-complements, by
\((V₁, E₁) → (V₂, E₂) \equivS (V₁ - V₂, (E₁ - E₂) ∩ (V₃ × V₃))\) where \(V₃ = V₁ - V₂\).
</p>

<p>
<i>The result of \(G₁ → G₂\) is the largest subgraph of \(G₁\) that does not overlap with \(G₂\).</i>
</p></li>

<li><p>
\((A × B → C) \equivS A → (B → C)\)
</p>

<p>
In the setting of functions, this says that a function taking a pair of inputs
can be considered as a function that takes one input, of type \(A\), then yields
another function that takes the second input. That is, the two inputs no longer
need to be provided at the same time. This is known as currying.
</p></li>

<li><p>
\(P, Q ⊢ R \equivS P ⊢ (Q → R)\)
</p>

<p>
In the logical formulae setting,
the characterisation is known as <i>the deduction theorem</i> and allows us
to ‘suppose’ an antecedent in the process of a proof.
</p></li>
</ul>

<p>
In the above we have witnessed that the usual naturals admit pseudo-complements precisely
when infinity is considered a number, that it is <i>exactly</i> implication for the Booleans,
that it <i>internalises</i> implication for sets, and for subgraphs it is encodes
<i>the largest complementary subgraph</i>.
</p>

<p>
In some sense, the pseudo-complement is the “best approximate inverse” to forming minima.
</p>

<p>
We leave the remaining example as an exercise 😉
</p>
</div>
</div>

<div id="outline-container-org3298048" class="outline-2">
<h2 id="Theorems-Generalising-Observations-from-the-Examples"><span class="section-number-2">3</span> <a href="#Theorems-Generalising-Observations-from-the-Examples">Theorems &#x2014; Generalising Observations from the Examples</a></h2>
<div class="outline-text-2" id="text-Theorems-Generalising-Observations-from-the-Examples">
<p>
Recall the \ref{Relative Pseudo-Complement Characterisation} says
\[a ⊓ x \;⊑\; b  \equivS  x \;⊑\; (a → b)\]
</p>

<p>
We readily obtain some results by making parts of the characterisation true.
E.g., making the left/right part true by instantiating the variables.
</p>

<p>
For example, taking \(x ≔ (a → b)\) yields
\[\eqn{Modus Ponens}{a ⊓ (a → b) \quad⊑\quad b}\]
Exercise: Prove this directly!
</p>

<ul class="org-ul">
<li>In the Boolean setting, this reads “if \(a\) is true and \(a\) implies \(b\), then \(b\) is true”.</li>
<li>In the functional setting, this reads “if \(f : A → B\) and we have an element \(a : A\), then
we have an element \(f(a) \, : \, B\)”. In this setting, this operation is known as ‘evaluation’,
or <i>function application</i>.</li>
</ul>

<p>
Using the principle of indirect equality, we can strengthen this into an equality
and also obtain a close variant.
\[\eqn{Strong Modus Ponens}{a ⊓ (a → b) \quad=\quad a ⊓ b}\]
</p>
<p>
\[\eqn{Absorption}{b ⊓ (a → b) \quad=\quad b}\]
</p>
<p>
\ref{Modus Ponens} suggest an order preservation property:
</p>
\begin{calc}
   a → x \quad⊑\quad a → y
\step{ \ref{Relative Pseudo-Complement Characterisation} }
   a ⊓ (a → x) \quad⊑\quad y
\stepWith{\Leftarrow}{ \ref{⊑-transitive} }
   a ⊓ (a → x) \quad⊑\quad x \landS x \quad⊑\quad y
\step{ \ref{Modus Ponens} and Identity of ∧ }
   x \quad⊑\quad y
\end{calc}
<p>
Hence we have derived the following <i>consequent weakening rule</i>,
\[\eqn{Monotonicity₂}{a → x \quad⊑\quad a → y \qquad\Leftarrow\qquad x \;⊑\; y}\]
</p>

<p>
An immediate sequel is,
\[\eqn{Weakening₂}{a → (x ⊓ y) \quad⊑\quad a → y}\]
</p>

<p>
Here are a few more properties for you to get familiar with this,
the first three are immediate instantiation of the characterisation,
while the fourth one may require using monotonicity properties of meet,
and the final one uses indirect equality.
</p>

<p>
\[\eqn{Top Element}{x \quad⊑\quad a → a}\]
</p>

<p>
\[\eqn{Strengthening}{b \quad⊑\quad a → b}\]
</p>

<p>
\[\eqn{UnCurrying}{x \quad⊑\quad a → a ⊓ x}\]
</p>

<p>
\[\eqn{Weakening}{x \quad⊑\quad (a ⊓ b) → b}\]
</p>

<p>
\[\eqn{Antitonicity₁}{a → x \quad⊑\quad b → x \qquad\Leftarrow\qquad b \;⊑\; a}\]
</p>
<p>
\[\eqn{Self-Sub-Distributive}{a → (b → c)  \sqleqS  (a → b) → (a → c)}\]
</p>
<p>
Observe that, in the functional case, \ref{UnCurrying} says we have a function
\[X → (A → (A × X)) \;\;:\;\; x \mapsto (a \mapsto (a, x))\]
</p>
</div>
</div>

<div id="outline-container-orgac6bd3e" class="outline-2">
<h2 id="Furnishes"><span class="section-number-2">4</span> <a href="#Furnishes">→ Furnishes ⊓</a></h2>
<div class="outline-text-2" id="text-Furnishes">
<p>
Recall \ref{Top Element} said that we have a greatest element in the order.
Suppose our universe, \(Carrier\), is non-empty and has some element \(c₀\).
Then we may <i>define</i> \(⊤ = (c₀ → c₀)\). This element is in-fact invariant
over \(c₀\):
</p>

<p>
\[\eqn{Top Element Invariance}{ ∀ x \;•\quad ⊤ \quad=\quad (x → x) }\]
The proof is simply an appeal to \ref{⊑-antisymmetric} then \ref{Top Element}
twice.
</p>

<p>
From this, we obtain two immediate properties about meets.
</p>

<p>
\[\eqn{Identity of ⊓}{ x ⊓ ⊤ \quad=\quad x }\]
</p>
<p>
\[\eqn{Shunting}{x \;⊑\; a → (b → c) \equivS x \;⊑\; (a ⊓ b) → c}\]
</p>
<p>
Along with two properties about top element.
</p>

<p>
\[\eqn{Right Zero of →}{ x → ⊤ \quad=\quad ⊤ }\]
</p>
<p>
\[\eqn{Left Identity of →}{ ⊤ → x \quad=\quad x }\]
</p>
<p>
Notice that \ref{Right Zero of →} <i>internalises</i> what it means to
be a \ref{Top Element}; namely \(∀ x \;•\; x ≤ ⊤\).
</p>

<p>
A bit more interestingly, we can <i>fuse</i> a meet with a pseudo-complement:
</p>

<p>
\[\eqn{Sub-mutual Associtivity of ⊓ and →}{\hspace{-3em}x ⊓ (a → b)  \sqleqS (x ⊓ a) → b}\]
</p>
<p>
The converse of this statement is not true in general.
In particular, in the presence of bottoms, the converse,
\((x ⊓ a) → b   \;\;⊑\;\;   x ⊓ (a → b)\), implies that there is only
one possible value: ⊥!
</p>

<p>
Exercise: Show that the converse statement implies \(⊥ = ⊤\)
then from this conclude that \(x = ⊥\), for all \(x\).
</p>
</div>
</div>

<div id="outline-container-org30934d0" class="outline-2">
<h2 id="Internalising-and"><span class="section-number-2">5</span> <a href="#Internalising-and">Internalising ⊑ and ⊓</a></h2>
<div class="outline-text-2" id="text-Internalising-and">
<p>
We have seen how relative pseudo-complements allowed us to reflect
external characterisations such as \ref{Top Element} that use
logical connectives into internal forms such as \ref{Right Zero of →}
which only <i>uses the symbols of the language</i>, namely →, ⊓, ⊤.
</p>

<p>
Even the order, which takes two elements and yields a Boolean, can be internalised:
\[\eqn{Internalising}{a ⊑ b  \equivS  (a → b) = ⊤}\]
</p>
<p>
Notice the striking resemblance to the \ref{Definition of → for 𝒩}!
</p>

<p>
\ref{Meet Characterisation} can also be internalised:
</p>
\begin{calc}
x \sqleqS (a → c) \;⊓\; (a → b)
  \step{ \ref{Meet Characterisation} }
x ⊑ (a → c) \landS  x ⊑ (a → b)
  \step{ \ref{Relative Pseudo-Complement Characterisation} }
a ⊓ x ⊑ c   \landS  a ⊓ x ⊑ b
  \step{ \ref{Meet Characterisation} }
a ⊓ x \sqleqS c ⊓ b
  \step{ \ref{Relative Pseudo-Complement Characterisation} }
x \sqleqS  a → (c ⊓ b)
\end{calc}
<p>
Hence, by the principle of \ref{Indirect Equality}, we have
\[\eqn{Distributivity of → over ⊓}{ a → (c ⊓ b) \quad=\quad (a → c) \;⊓\; (a → b) }\]
</p>
</div>
</div>

<div id="outline-container-orgc5793a0" class="outline-2">
<h2 id="Flipping-the-Inclusions-Around"><span class="section-number-2">6</span> <a href="#Flipping-the-Inclusions-Around">Flipping the Inclusions Around</a></h2>
<div class="outline-text-2" id="text-Flipping-the-Inclusions-Around">
<p>
Recall \ref{Meet Characterisation} says
\[ z \;⊑\; x ⊓ y \equivS z ⊑ x \landS z ⊑ y \]
If we now mirror ‘⊑’ with ‘⊒’ we obtain &#x2013;after renaming ⊓ with ⊔&#x2013;
\[ z \;⊒\; x ⊔ y \equivS z ⊒ x \landS z ⊒ y \]
That is, we obtain upper bounds!
\[\eqnColour{Join Characterisation}{ x ⊔ y \;⊑\; z \equivS x ⊑ z \landS y ⊑ z}{green}\]
</p>

<p>
Moreover, using →, it can be shown that joins and meets distribute over each other.
</p>

<p>
\[\eqn{Distributivity₁}{a \;⊔\; (b ⊓ c) \quad=\quad (a ⊔ b) \;⊓\; (a ⊔ c)}\]
\[\eqn{Distributivity₂}{a \;⊓\; (b ⊔ c) \quad=\quad (a ⊓ b) \;⊔\; (a ⊓ c)}\]
</p>
</div>
</div>

<div id="outline-container-orgc636ff8" class="outline-2">
<h2 id="Almost-True-Complements"><span class="section-number-2">7</span> <a href="#Almost-True-Complements">Almost <i>True</i> Complements</a></h2>
<div class="outline-text-2" id="text-Almost-True-Complements">
<p>
Can we obtain the usual De Morgan's law?
</p>

<p>
Suppose we have a <i>least element</i> \(⊥ : Carrier\), i.e., for any \(x\),
\[\eqn{Bottom Element}{ ⊥ \sqleqs x}\]
</p>

<p>
For usual ℕ, 𝔹, sets, and graphs this amounts to 0, \(\mathsf{false}\), \(\emptyset\), and the
empty graph with no nodes nor any edges, respectively.
</p>

<p>
Unsurprising this can also be internalised!
\[\eqn{ex falso quod libet}{⊥ → a \quad=\quad ⊤}\]
</p>
<p>
Moreover we can now define an operation \(¬\_\) as follows,
\[\eqn{Pseudo-Complement Definition}{ ¬ x \quad=\quad (x → ⊥) }\]
</p>

<p>
<i>A complement</i> of an element \(x\) is an element \(y\) that is
disjoint from \(x\) and together their join is the top element.
</p>

<p>
<i>A <a href="https://en.wikipedia.org/wiki/Pseudocomplement">pseudo-complement</a></i> generalises this idea by discarding the second half
of that conjunction; i.e., instead requiring a greatest element \(y\)
disjoint from \(x\).
</p>

<p>
Indeed this is the case here,
</p>
\begin{calc}
   x \;⊓\; ¬ x \quad=\quad ⊥
\step{ \ref{⊑-antisymmetric} }
   (x \;⊓\; ¬ x) \sqleqs ⊥
   \landS ⊥ \sqleqs (x \;⊓\; ¬ x)
\step{ \ref{Bottom Element} }
   x \;⊓\; ¬ x \sqleqS ⊥
\step{ \ref{Modus Ponens} }
   \mathsf{true}
\end{calc}

<p>
We now have the linguistic prerequisites to actually express De Morgan's laws.
\[\eqn{Constructive De Morgan}{¬(x \;⊔\; y) \quad=\quad ¬ x \;⊓\; ¬ y}\]
</p>

<p>
The other form \(¬(x ⊓ y) \;=\; ¬ x ⊔ ¬ y\) however is not true in general.
Likewise neither are double negation, \(¬¬x \;=\; x\), nor the law of the
the excluded middle \(x \;⊔\; ¬ x \;=\; ⊤\).
</p>

<p>
Indeed for 𝒩, the natural numbers extended with +∞, the law of the exluded middle
is falsified as follows:
</p>

\begin{calc}
   3 ↑ ¬ 3
\step{ \ref{Pseudo-Complement Definition} }
   3 ↑ (3 → 0)
\step{ \ref{Definition of → for 𝒩} }
   3 ↑ 0
\step{ Maximum }
   3
\end{calc}

<p>
Likewise double negation is falsified by \(¬ ¬ 3 \;=\; 0\).
</p>

<p>
Exercise: Find an example to falsify the other De Morgan's law.
</p>

<p>
Having ⊓, ⊔, ⊥, ⊤, → together constitutes a <a href="https://en.wikipedia.org/wiki/Heyting_algebra">Heyting Algebra</a>.
</p>
</div>
</div>

<div id="outline-container-org4040ff9" class="outline-2">
<h2 id="So-long-and-thanks-for-the-fish"><span class="section-number-2">8</span> <a href="#So-long-and-thanks-for-the-fish">So long and thanks for the fish</a></h2>
<div class="outline-text-2" id="text-So-long-and-thanks-for-the-fish">
<p>
It took a while, but we've more or less shimmied our
way to the structure needed for Heyting Algebras.
</p>

<p>
Hope you had fun!
</p>
</div>
</div>
<div class="taglist"><a href="https://alhassy.github.io/tags.html">Tags</a>: <a href="https://alhassy.github.io/tag-order-theory.html">order-theory</a> <a href="https://alhassy.github.io/tag-category-theory.html">category-theory</a> </div><center><strong> Generated by Emacs and Org-mode (•̀ᴗ•́)و </strong></center><center><a href="HeytingAlgebra.org.html"><img
   src="https://img.shields.io/badge/-Source-informational?logo=read-the-docs"></a>&emsp;<a href="https://github.com/alhassy/alhassy.github.io/commits/master/posts/HeytingAlgebra.org"><img
   src="https://img.shields.io/badge/-History-informational?logo=github"></a><br><a href="https://www.buymeacoffee.com/alhassy"><img src="https://img.shields.io/badge/-buy_me_a%C2%A0coffee-gray?logo=buy-me-a-coffee"></a></center><link rel="stylesheet" href="readremaining.js-readremainingjs/css/rr_light.css"
     type='text/css'/>
  <script
     src="readremaining.js-readremainingjs/src/readremaining.jquery.js"></script>
  <script src='readremaining.js/src/readremaining.jquery.js'
     type='text/javascript'></script>
  <script type="text/javascript"> $('body').readRemaining({showGaugeDelay : 10,
     showGaugeOnStart : true}); </script>]]></description>
  <category><![CDATA[order-theory]]></category>
  <category><![CDATA[category-theory]]></category>
  <link>https://alhassy.github.io/HeytingAlgebra.html</link>
  <guid>https://alhassy.github.io/HeytingAlgebra.html</guid>
  <pubDate>Wed, 14 Nov 2018 19:29:00 -0500</pubDate>
</item>
</channel>
</rss>
