<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title></title>
    <link href="https://www.newresalhaider.com/atom.xml" rel="self" type="application/atom+xml"/>
    <link href="https://www.newresalhaider.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-04-05T00:00:00+00:00</updated>
    <id>https://www.newresalhaider.com/atom.xml</id>
    <entry xml:lang="en">
        <title>Growing Yggdrasil, the World Tree, with Ash</title>
        <published>2026-04-05T00:00:00+00:00</published>
        <updated>2026-04-05T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/yggdrasil/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/yggdrasil/</id>
        
        <content type="html">&lt;p&gt;Declarative programming can be a powerful paradigm for organizing software systems. By defining the business processes once, we ensure there is a single source of domain knowledge. From this foundation, we can derive other parts of the system such as API endpoints, database schemas, and even user interfaces. This approach reduces repetition and helps prevent bugs caused by misaligned domain models.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;ash-hq.org&#x2F;&quot;&gt;Ash Framework&lt;&#x2F;a&gt; seems like an excellent way to see declarative programming in action. Written in &lt;a href=&quot;https:&#x2F;&#x2F;elixir-lang.org&#x2F;&quot;&gt;Elixir&lt;&#x2F;a&gt;, it allows you to describe your domain in a consistent and expressive way, from which it can automatically generate data layers, REST or GraphQL APIs, and admin interfaces. With Ash, you define what your application should do, and the framework takes care of how to make it happen. It can derive JSON REST endpoints, handle validation, manage persistence, and provide authorization logic, all from the same declarative definitions.&lt;&#x2F;p&gt;
&lt;p&gt;I am new to Ash and I tend to learn best by writing things out, so this article is as much for me as it is for you. Rather than trying to understand everything up front, I prefer to get hands-on quickly with a small, self-contained project. We’ll start with the basics here, and if things go well, expand on it in a follow-up or two.&lt;&#x2F;p&gt;
&lt;p&gt;Given the name Ash, it felt appropriate to build something inspired by the gigantic ash tree of Norse mythology: &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Yggdrasil&quot;&gt;Yggdrasil, the World Tree&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;An image of a Yggdrasil the world tree as a cybernetic Ash tree.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Yggdrasil is said to connect the Nine Worlds of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Norse_cosmology&quot;&gt;Norse cosmology&lt;&#x2F;a&gt;, though the exact number and nature of these worlds vary between sources. Each world has its own nature, inhabitants, and relationships with the others, making it an ideal metaphor for exploring how Ash models resources, attributes, and relationships. In this project, we will create a domain model for these concepts with Ash and derive a JSON REST API from them.&lt;&#x2F;p&gt;
&lt;p&gt;The first step is getting started with a basic Ash project for which we will use the Igniter tool. (I will assume Elixir is already installed, but if not, see the &lt;a href=&quot;https:&#x2F;&#x2F;elixir-lang.org&#x2F;install.html&quot;&gt;Elixir Install page&lt;&#x2F;a&gt; for instructions). This is used for project setup and code generation, which will help us get started a lot quicker.&lt;&#x2F;p&gt;
&lt;p&gt;To start off following command will install Igniter:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;mix archive.install hex igniter_new&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Once we have Igniter, the next step is creating a new project in the &lt;code&gt;yggdrasil&lt;&#x2F;code&gt; directory, adding Ash, and moving into it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;mix igniter.new yggdrasil --install ash &amp;amp;&amp;amp; cd yggdrasil&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This will land us in a new Elixir project directory with Ash installed. From this seed we will evolve our application to represent the worlds and characters of Norse mythology.&lt;&#x2F;p&gt;
&lt;p&gt;The newly created project comes with a &lt;code&gt;hello&lt;&#x2F;code&gt; function in the &lt;code&gt;lib&#x2F;yggdrasil.ex&lt;&#x2F;code&gt; module. Let&#x27;s try it out in the &lt;code&gt;iex&lt;&#x2F;code&gt;, the Elixir interactive shell which we can start with:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;iex -S mix&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Running it will bring us into the shell, where we can run the &lt;code&gt;hello&lt;&#x2F;code&gt; function in the &lt;code&gt;yggdrasil&lt;&#x2F;code&gt; module:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;shell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-shell &quot;&gt;&lt;code class=&quot;language-shell&quot; data-lang=&quot;shell&quot;&gt;&lt;span&gt;iex(1)&amp;gt; Yggdrasil.hello()
&lt;&#x2F;span&gt;&lt;span&gt;:world
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We now have a hello world, but in yggdrasil we want to represent the &lt;em&gt;worlds&lt;&#x2F;em&gt; of Norse mythology that are linked by Yggdrasil. The first thing we need for this is a &lt;em&gt;Domain&lt;&#x2F;em&gt;. This will function as a container for the various concepts, such as the worlds, that we will introduce later.&lt;&#x2F;p&gt;
&lt;p&gt;For simplicity&#x27;s sake, first we will replace the contents of &lt;code&gt;lib&#x2F;Yggdrasil.ex&lt;&#x2F;code&gt; with the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defmodule &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;@moduledoc &amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  The Yggdrasil domain — acts as the trunk of the tree
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  and organizes all resources like World and Character.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  &amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Domain
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  resources &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;    # Resources will be registered here
&lt;&#x2F;span&gt;&lt;span&gt;    resource &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We create a file &lt;code&gt;lib&#x2F;resources&#x2F;world.ex&lt;&#x2F;code&gt; with the following contents:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defmodule &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;@moduledoc &amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  A resource representing a world in Yggdrasil.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  &amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Resource&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;    # in-memory store
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;data_layer: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;DataLayer&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ets&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;domain: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  actions &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    create &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:create &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      accept [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:description&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    update &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:update &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      accept [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:description&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;    # Provide default actions
&lt;&#x2F;span&gt;&lt;span&gt;    defaults [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:read&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:destroy&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  attributes &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;    # Primary key
&lt;&#x2F;span&gt;&lt;span&gt;    uuid_primary_key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:id
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;    # World name and description
&lt;&#x2F;span&gt;&lt;span&gt;    attribute &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:string&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;allow_nil?: false&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;public?: true
&lt;&#x2F;span&gt;&lt;span&gt;    attribute &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:description&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:string&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;public?: true
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And finally &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;config &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:ash_domains&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With these files in place, we now have a minimal Ash domain containing a single resource: World. Let’s take a moment to unpack what we just created before moving on.&lt;&#x2F;p&gt;
&lt;p&gt;At the top level, Yggdrasil acts as our domain, the trunk of our system. It brings together all the resources that make up the application and defines how they relate to each other. Right now, our domain only includes one resource, Yggdrasil.World, but we’ll add more later.&lt;&#x2F;p&gt;
&lt;p&gt;The Yggdrasil.World module itself is declared as a resource. In Ash, a resource is the fundamental building block. It describes a specific type of data and what can be done with it. Instead of writing separate schemas, changesets, and controllers, we declare everything about a resource in one place, and Ash takes care of the details.&lt;&#x2F;p&gt;
&lt;p&gt;Our World resource uses the Ash.DataLayer.Ets data layer, which stores data in Elixir’s in-memory ETS tables. This setup is fast and simple, making it perfect for early experimentation, though data won’t persist between runs. Later, this can be swapped out for a different data layer to gain full persistence. The argument &lt;code&gt;domain: Yggdrasil&lt;&#x2F;code&gt; connects the resource back to the domain we just defined so that the framework knows where it belongs.&lt;&#x2F;p&gt;
&lt;p&gt;Inside the actions block, we declare what operations are available for this resource. The create action accepts a name and a description, while the defaults &lt;code&gt;[:read, :destroy]&lt;&#x2F;code&gt; line automatically adds the standard read, update, and delete actions. There’s no need to write any manual CRUD logic—Ash generates it for us.&lt;&#x2F;p&gt;
&lt;p&gt;The attributes block defines the structure of each world. Every world has a UUID primary key (&lt;code&gt;:id&lt;&#x2F;code&gt;) and two fields, &lt;code&gt;:name&lt;&#x2F;code&gt; and &lt;code&gt;:description&lt;&#x2F;code&gt;. The &lt;code&gt;:name&lt;&#x2F;code&gt; attribute is made required using &lt;code&gt;(allow_nil?: false)&lt;&#x2F;code&gt;, ensuring that each world must have one. Both attributes are marked &lt;code&gt;public?: true&lt;&#x2F;code&gt; so they appear in APIs and outputs.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, the configuration line we added tells Ash which domains to load when the application starts. Without this, the framework wouldn’t know about our new resource.&lt;&#x2F;p&gt;
&lt;p&gt;At this point, our small Ash tree has already taken root. We’ve declared the first piece of our domain, and Ash now knows how to create, read, update, and delete worlds. Let’s see that in action next by exploring our resource interactively in iex.&lt;&#x2F;p&gt;
&lt;p&gt;First, start the interactive shell from your project root:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;iex -S&lt;&#x2F;span&gt;&lt;span&gt; mix
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once inside iex, we want to create our first world Asgard, the shining realm of the gods, with the following command:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;asgard = (
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Changeset&lt;&#x2F;span&gt;&lt;span&gt;.for_create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:create&lt;&#x2F;span&gt;&lt;span&gt;, %{
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Asgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A shining realm of order and power, suspended high above the clouds.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;     })
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.create!()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which would return the world as such: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;29&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;14.665 &lt;&#x2F;span&gt;&lt;span&gt;[debug] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Creating Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;World:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Setting&lt;&#x2F;span&gt;&lt;span&gt; %{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;726b678e-6cb6-4277-b291-85ecfa313d3a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Asgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A shining realm of order and power,...}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;%Yggdrasil.World{
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;  id: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;726b678e-6cb6-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4277&lt;&#x2F;span&gt;&lt;span&gt;-b291-85ecfa313d3a&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;  name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Asgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;  description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt; shining realm of order and power, suspended high above the clouds.&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;  __meta__: #Ecto.Schema.Metadata&amp;lt;:loaded&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are a multiple things happening here, so let&#x27;s unwrap things step by step. &lt;&#x2F;p&gt;
&lt;p&gt;First we start off with our Ash resource that we have defined &lt;code&gt;Yggdrasil.World&lt;&#x2F;code&gt;. In Ash, resources describe the structure of our data, including attributes like name and description, as well as the actions that can be performed on them.&lt;&#x2F;p&gt;
&lt;p&gt;Next we are using the pipe operator. &lt;code&gt;|&amp;gt;&lt;&#x2F;code&gt;, to pass this result to our next function. Elixir’s pipe operator takes the result of the expression on the left and passes it as the first argument to the function on the right.&lt;&#x2F;p&gt;
&lt;p&gt;For example instead of writing:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;function(value, a, b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;with the pipe operator we can equivalently write:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;value |&amp;gt; function(a, b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This allows us to write a sequence of operations in a readable, step-by-step style. In our code example, it means &lt;code&gt;Yggdrasil.World&lt;&#x2F;code&gt; is passed into the &lt;code&gt;Ash.Changeset.for_create&lt;&#x2F;code&gt; function, as the first parameter. This function also takes the identifier of our create action &lt;code&gt;:create&lt;&#x2F;code&gt;, as well as the structure representing Asgard, with its name and description. &lt;&#x2F;p&gt;
&lt;p&gt;What this function returns is a &lt;code&gt;changeset&lt;&#x2F;code&gt;, a data structure representing the intended change of a resource in Ash (e.g.: creating, updating, etc). This is especially useful when it comes to validation and error checking, as we will see it later down the line. For now we use this changeset and pipe it into the function that executes the actual creation: &lt;code&gt;Ash.create!()&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;The resulting value is a &lt;code&gt;%Yggdrasil.World{}&lt;&#x2F;code&gt; struct, which represents the newly created world. Ash also automatically generated a UUID for the id field, which uniquely identifies this world inside the system.&lt;&#x2F;p&gt;
&lt;p&gt;Before returning the struct, Ash logs the operation it performed. That is why we see the debug output:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;[debug] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Creating Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The final line is the Elixir struct that was created:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;%&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;726b678e-6cb6-4277-b291-85ecfa313d3a&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Asgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A shining realm of order and power, suspended high above the clouds.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;__meta__: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#Ecto.Schema.Metadata&amp;lt;:loaded&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This struct is also stored in the variable &lt;code&gt;asgard&lt;&#x2F;code&gt;, so we can reference it later in the session.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we understand how creating a world works, let’s add another one.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;midgard = (
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Changeset&lt;&#x2F;span&gt;&lt;span&gt;.for_create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:create&lt;&#x2F;span&gt;&lt;span&gt;, %{
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Midgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The realm of humans, bound to the earth and everyday struggles.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;     })
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.create!()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This follows the exact same pattern as before. We build a changeset that describes the creation of Midgard, and then execute it with &lt;code&gt;Ash.create!()&lt;&#x2F;code&gt;. Much simpler than the mythological creation of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Midgard&quot;&gt;Midgard&lt;&#x2F;a&gt;, which involved the slaying of the giant Ymir.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have some worlds, let&#x27;s read them using the read action: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;worlds = (
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Query&lt;&#x2F;span&gt;&lt;span&gt;.for_read(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:read&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.read!()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which would give us our list of worlds:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;some-uuid-1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Asgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A shining realm of order and power, suspended high above the clouds.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;some-uuid-2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Midgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The realm of humans, bound to the earth and everyday struggles.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As one can expect, we can also do an update call. For example, let’s change the description of Asgard:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;asgard = (
&lt;&#x2F;span&gt;&lt;span&gt;  asgard
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Changeset&lt;&#x2F;span&gt;&lt;span&gt;.for_update(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:update&lt;&#x2F;span&gt;&lt;span&gt;, %{
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The fortified realm of the Aesir, ruled by Odin.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;     })
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.update!()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are a few things to note here. Instead of starting from the &lt;code&gt;Yggdrasil.World&lt;&#x2F;code&gt; module, we now start from the existing asgard struct. This is because we are modifying a resource that already exists.&lt;&#x2F;p&gt;
&lt;p&gt;The function &lt;code&gt;for_update&lt;&#x2F;code&gt; creates a changeset that describes the intended update. Just like with creation, the changeset itself does not perform the update, it only represents the change we want to make.&lt;&#x2F;p&gt;
&lt;p&gt;We then pass this changeset into &lt;code&gt;Ash.update!()&lt;&#x2F;code&gt;, which executes the update. Ash applies the changes, runs any validations, and returns the updated &lt;code&gt;%Yggdrasil.World{}&lt;&#x2F;code&gt; struct.&lt;&#x2F;p&gt;
&lt;p&gt;We can verify the change by reading the list of worlds again:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;worlds = (
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Query&lt;&#x2F;span&gt;&lt;span&gt;.for_read(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:read&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.read!()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which would give us a result such as: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;6b62b3ea-b08b-4387-8539-37e645e53026&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Midgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The realm of humans, bound to the earth and everyday struggles.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;__meta__: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#Ecto.Schema.Metadata&amp;lt;:loaded&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;d2646509-6c92-4049-a2db-0555612fc365&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Asgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The fortified realm of the Aesir, ruled by Odin.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;__meta__: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#Ecto.Schema.Metadata&amp;lt;:loaded&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;An interesting thing we could try out is updating the name of a world instead:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;asgard2 = (
&lt;&#x2F;span&gt;&lt;span&gt;  asgard
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Changeset&lt;&#x2F;span&gt;&lt;span&gt;.for_update(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:update&lt;&#x2F;span&gt;&lt;span&gt;, %{
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Asgard2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;     })
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.update!()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We get the following error: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;** (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Error&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Invalid&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Invalid Error
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;No&lt;&#x2F;span&gt;&lt;span&gt; such input `name` &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; action &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;.update
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;The&lt;&#x2F;span&gt;&lt;span&gt; attribute exists on &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;, but is not accepted by &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;.update
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Perhaps&lt;&#x2F;span&gt;&lt;span&gt; you meant to add it to the accept list &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;.update?
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Valid &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Inputs:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;* description
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is because when we were defining our update action in our module, the only attribute we accept is &lt;code&gt;:description&lt;&#x2F;code&gt;, see fragment below:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;    update &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:update &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      accept [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:description&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In other words, while the name attribute exists on the resource, it is not allowed to be modified through the update action. This is a domain modelling decision, and gives us fine-grained control over how our data can change. In this case, we decided that a world’s name is fixed after creation, while its description can evolve over time.&lt;&#x2F;p&gt;
&lt;p&gt;Finally we get to do delete, where we destroy asgard, our Ragnarok action if you will. We can do this by the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.destroy!(asgard)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Ash.destroy!&lt;&#x2F;code&gt; takes a resource struct, in this case &lt;code&gt;asgard&lt;&#x2F;code&gt;, and removes it from the data store. Since we’re using an in-memory ETS store, it deletes it from memory immediately. The function should return &lt;code&gt;:ok&lt;&#x2F;code&gt; on success. We can double check this by requesting our list of worlds again by our usual means:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;worlds = (
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Query&lt;&#x2F;span&gt;&lt;span&gt;.for_read(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:read&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  |&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ash&lt;&#x2F;span&gt;&lt;span&gt;.read!()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which returns only Midgard:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Yggdrasil&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;World&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;6b62b3ea-b08b-4387-8539-37e645e53026&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Midgard&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;description: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The realm of humans, bound to the earth and everyday struggles.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;__meta__: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#Ecto.Schema.Metadata&amp;lt;:loaded&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At this point, we’ve taken the first steps in modeling our little piece of Yggdrasil. We have a domain, a resource, and a way to create, read, update, and delete worlds, enough to bring about a small Ragnarok.&lt;&#x2F;p&gt;
&lt;p&gt;Next, we will explore how we can start connecting resources together. After all, the worlds need their heroes and villains to really come alive.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Falsehoods AI Believes About Names</title>
        <published>2026-02-15T00:00:00+00:00</published>
        <updated>2026-02-15T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/falsehoodsainames/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/falsehoodsainames/</id>
        
        <content type="html">&lt;p&gt;Names are a key aspect of one’s identity. Too many people have dealt with software systems where entering their name does not go quite right. A character is rejected. The order feels wrong. A name is truncated, reformatted, or “corrected” into something else entirely. While these may seem like small elements within the software, they can have big consequences for the person whose name is misrepresented. People with accented characters in their names often find systems rejecting them outright. Hyphenated names can be split incorrectly, creating mismatched records. Even simple truncation of long names can force repeated corrections on official documents or forms. For anyone affected, what seems like a small software quirk can quickly turn into a real headache.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Illustration of an AI like figure and a software engineer sitting together, with floating fields representing first and last name data around them.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;This problem is not new. In 2010, Patrick McKenzie’s blog post titled &lt;a href=&quot;https:&#x2F;&#x2F;www.kalzumeus.com&#x2F;2010&#x2F;06&#x2F;17&#x2F;falsehoods-programmers-believe-about-names&#x2F;&quot;&gt;“Falsehoods Programmers Believe About Names”&lt;&#x2F;a&gt; describes 40 incorrect assumptions software engineers commonly make about names. They relate to various aspects of a name such as how many names could a person have, what characters can be used, when their names are assigned and more. Here is a small selection of them:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;1, People have exactly one canonical full name.
&lt;&#x2F;span&gt;&lt;span&gt;6, People’s names fit within a certain defined amount of space.
&lt;&#x2F;span&gt;&lt;span&gt;7, People’s names do not change.
&lt;&#x2F;span&gt;&lt;span&gt;9, People’s names are written in ASCII.
&lt;&#x2F;span&gt;&lt;span&gt;32, People’s names are assigned at birth.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Each of these assumptions has some examples that prove them wrong. For assumption 1, a name can change at marriage among other occasions. For assumption 32, there are cultures such as Iceland where a name is not assigned at birth but within &lt;a href=&quot;https:&#x2F;&#x2F;www.remitly.com&#x2F;blog&#x2F;lifestyle-culture&#x2F;iceland-naming-laws&#x2F;&quot;&gt;6 months&lt;&#x2F;a&gt;. See this &lt;a href=&quot;https:&#x2F;&#x2F;shinesolutions.com&#x2F;2018&#x2F;01&#x2F;08&#x2F;falsehoods-programmers-believe-about-names-with-examples&#x2F;&quot;&gt;blog post by Tony Rodgers&lt;&#x2F;a&gt; for more examples that disprove these assumptions.&lt;&#x2F;p&gt;
&lt;p&gt;In an age where GenAI is at nearly every programmer&#x27;s fingertips, one could expect that these assumptions are not likely to be made again. In fact when we ask the AI to create an API for names it should be likely it avoids most, if not all, of these pitfalls. We could test this by asking an answer for a prompt such as the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Design a general-purpose API for storing and manipulating human names.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It gives an answer, containing the following data model:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;uuid&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;given_names&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;John&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Michael&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;],
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;family_names&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;],
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;prefix&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dr.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;suffix&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Jr.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;preferred_name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Johnny&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;nicknames&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Jack&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;],
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;display_name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dr. John Michael Doe Jr.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;locale&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;en-US&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;metadata&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gender_hint&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;male&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;transliterations&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;jp&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ジョン・マイケル・ドー&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gets a surprising amount of things right. The model supports multiple given and family names, recognizes prefixes and suffixes, allows for nicknames and preferred names, and even accounts for locale-specific formatting and transliterations. Some assumptions still sneak in: using fields like &lt;code&gt;given_names&lt;&#x2F;code&gt; and &lt;code&gt;family_names&lt;&#x2F;code&gt; implies a “first and last” structure, which does not apply universally.&lt;&#x2F;p&gt;
&lt;p&gt;Even with a very short, simple prompt, the AI can avoid many common pitfalls identified in McKenzie’s essay. It demonstrates that when names are the explicit focus, generative AI can synthesize decades of lessons from software design and produce a thoughtful, flexible API.&lt;&#x2F;p&gt;
&lt;p&gt;So now we’re good, right? With AI handling names this well, it seems like the old mistakes that software engineers made might finally be behind us. Surely, AI can just get it right, every time.&lt;&#x2F;p&gt;
&lt;p&gt;Unfortunately, things get less rosy when names are only an implicit part of the prompt or the system is not focused on them. When the AI has to reason about names as just one piece of a larger design, its attention to detail can slip, and many of the same assumptions that humans make quietly return.&lt;&#x2F;p&gt;
&lt;p&gt;To show this process let&#x27;s ask the following prompt: &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Design an API for an application where users can talk about books they have read.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Each user has their own account, with their name, personal details, and a history of books they have read and discussed.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;The API should support typical operations such as user registration, profile management, posting comments, and viewing discussions.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Return the API design with data models and example endpoints.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will give us, among other things, a data model as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;uuid&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;username&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;displayName&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;bio&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;avatarUrl&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;createdAt&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;datetime&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;updatedAt&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;datetime&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The resulting data model has quite a few differences from the previous example. The name representation here is a lot more simplified. It has fields like &lt;code&gt;displayName&lt;&#x2F;code&gt;, &lt;code&gt;username&lt;&#x2F;code&gt; for this purpose. Notice that there is no separate handling for multiple given or family names, prefixes, suffixes, nicknames, or locale-specific formatting. Sometimes this is not always a bad thing, having no separate given and family names, prevents cultural and local specific assumptions about names creep into the representation. Nonetheless, it is clear that when names are not the main focus, the model tends to abstract a lot more.&lt;&#x2F;p&gt;
&lt;p&gt;While these are very basic examples, they highlight how much the choice of prompt can shape the structure of the code, even with the myriad examples and domain knowledge an AI model has at its fingertips. Of course, any potential design issues could be resolved by further prompting or refinement, but this also underscores why human judgment remains important. A knowledgeable software engineer in the loop can push back, ask the right questions, and name the issues as they arise, ensuring the software is built with the right abstractions.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Will AI Get Rid of this Turbulent Priest?</title>
        <published>2026-01-16T00:00:00+00:00</published>
        <updated>2026-01-16T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/turbulentpriest/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/turbulentpriest/</id>
        
        <content type="html">&lt;p&gt;In the age of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Generative_artificial_intelligence&quot;&gt;generative AI (GenAI)&lt;&#x2F;a&gt;, being precise matters more than ever. &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Large_language_model&quot;&gt;Large Language Models (LLMs)&lt;&#x2F;a&gt;, for example Copilot or ChatGPT, are usually helpful, but they also eagerly fill in gaps we did not intend to leave. This becomes especially true when we interact with AI using natural language. While plain language makes these systems feel accessible, it also creates space for misinterpretations. An ambiguous request can quickly turn into an unwanted action.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A priest pondering a dilemma with cybernetic symbols behind him.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;One infamous example of an unintended request comes from medieval England. &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Henry_II_of_England&quot;&gt;Henry II&lt;&#x2F;a&gt; was locked in a long-running quarrel with &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Thomas_Becket&quot;&gt;Thomas Becket&lt;&#x2F;a&gt;, the Archbishop of Canterbury, over whether clergy accused of crimes could be tried in royal courts rather than church courts. Conflict and frustration had been building for some time when Henry reportedly asked, &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Will_no_one_rid_me_of_this_turbulent_priest%3F&quot;&gt;“Will no one rid me of this turbulent priest?”&lt;&#x2F;a&gt;. It was not meant as an order, but it was heard as one. Four knights took the statement literally, travelled to Canterbury, and after confronting Becket, killed him. The consequences were profound, turning Becket into a martyr and compelling Henry to perform public penance.&lt;&#x2F;p&gt;
&lt;p&gt;Hopefully, when using natural language with generative AI, a misinterpretation leads to nothing close to what happened with Henry II and Becket. Nonetheless, we should remain cautious. Even small misunderstandings can result in wasted work, incorrect decisions, or unintended changes. Fortunately, there are ways to reduce this risk. Instead of relying solely on natural language, we can use more formal and explicit approaches. In particular, techniques from the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Semantic_Web&quot;&gt;Semantic Web&lt;&#x2F;a&gt;, including the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Turtle_(syntax)&quot;&gt;Turtle&lt;&#x2F;a&gt; language for data representation and the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;SPARQL&quot;&gt;SPARQL&lt;&#x2F;a&gt; query language, help encode both data and questions in a precise way. In this article, I will show how these techniques can produce more reliable results with GenAI than relying on LLMs alone.&lt;&#x2F;p&gt;
&lt;p&gt;To get started, let’s describe a scenario that echoes the earlier historical story. Imagine we have a small dataset of priests, with each priest having an attitude and a status attribute. Father Aidan is calm and active, Father Benedict is calm and suspended, while Father Cuthbert is troublesome and active. Now suppose we ask a GenAI system, “Can you get rid of the turbulent priest?” What would it do?&lt;&#x2F;p&gt;
&lt;p&gt;There are at least two scenarios where the query could lead to unintended changes to the dataset. First, which priest is the turbulent one here? Based on the historical example, we would think this is the troublesome one. However, picking the suspended priest would also be a possible interpretation. The second possible misinterpretation is what we mean by “get rid of.” Thankfully, none of the likely possibilities here are as macabre as death, but we could either remove the priest from the dataset, or, if a priest is currently active, make them suspended.&lt;&#x2F;p&gt;
&lt;p&gt;We can actually pose this scenario as a natural language prompt to see what the answer could be:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;You are given a small dataset describing priests. Each priest has an attitude and a status. 
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Father Aidan has a calm attitude and is active.
&lt;&#x2F;span&gt;&lt;span&gt;Father Benedict has a calm attitude and is suspended.
&lt;&#x2F;span&gt;&lt;span&gt;Father Cuthbert has a troublesome attitude and is active.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Based on this information, can you get rid of the turbulent priest?
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As a small test this prompt was posed to both ChatGPT and Copilot, using their free versions:&lt;&#x2F;p&gt;
&lt;p&gt;ChatGPT&#x27;s answer:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Yep 🙂 — the turbulent priest is the one with the troublesome attitude.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;From the data:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Father Aidan → calm, active
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Father Benedict → calm, suspended
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Father Cuthbert → troublesome, active
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;So the turbulent priest is Father Cuthbert.
&lt;&#x2F;span&gt;&lt;span&gt;“Getting rid of” him, based on the pattern you already have, would mean suspending Father Cuthbert (i.e., removing him from the active group).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;In short: suspend Father Cuthbert and the turbulence is gone.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;ChatGPT has identified the mapping between “turbulent” and “troublesome” by singling out Father Cuthbert, and suggests resolving the issue by suspending him.&lt;&#x2F;p&gt;
&lt;p&gt;Copilot&#x27;s answer:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Ah, a classic little logic puzzle dressed up in medieval robes.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;You’ve given three priests, each with an attitude and a status. The question asks whether we can “get rid of the turbulent priest,” which is a playful nod to the phrase associated with Thomas Becket — but here it’s just about identifying which priest fits the description.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;From your dataset:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Father Aidan — calm, active
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Father Benedict — calm, suspended
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Father Cuthbert — troublesome, active
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;If “turbulent” corresponds to “troublesome,” then the turbulent priest is Father Cuthbert.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;So yes — based on the information, the one to remove from the dataset is Father Cuthbert.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;If you want to take the puzzle further — filtering, categorizing, or turning this into a rule-based system — I’m happy to explore that with you.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Copilot recognizes the historical reference and singles out Father Cuthbert, but comes to a different conclusion by wanting to remove him from the dataset.&lt;&#x2F;p&gt;
&lt;p&gt;As a disclaimer, these answers can be quite dependent on what exact model was used, any additional context that was not gathered from the prompt, and other factors. The key takeaway, however, is that there is no single correct answer here. The prompt leaves both the meaning of “turbulent” and the action implied by “get rid of” open to interpretation. While the consequences in this example are far less dramatic than in the historical case, the underlying problem is the same. An ambiguous request forces the system to guess, and that guess can lead to unintended outcomes. In a world where generative AI tools become more integrated into workflows, the risks and consequences of ambiguity can be significant. &lt;&#x2F;p&gt;
&lt;p&gt;One way to reduce ambiguity and make intentions explicit is to use Semantic Web technologies. These tools allow us to encode both the data and the questions we ask in a precise and machine-readable way. For example, the Turtle language can represent the attributes of each priest in a structured format, while SPARQL queries let us specify exactly what action we want to perform. By defining both the meaning of “troublesome” and the desired outcome in a formal way, we remove the guesswork that a natural language prompt requires. This approach provides a more reliable path to the results we intend, even when working with generative AI.&lt;&#x2F;p&gt;
&lt;p&gt;Turtle is a simple, human-readable language used in the Semantic Web to describe knowledge as a graph. It expresses facts about entities and their relationships in a way that computers can process unambiguously. In this example, we use Turtle to represent the three priests with two attributes, attitude and status.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-turtle &quot;&gt;&lt;code class=&quot;language-turtle&quot; data-lang=&quot;turtle&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;example.org&#x2F;priest#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FatherAidan
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;attitude &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;calm&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;status &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;active&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FatherBenedict
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;attitude &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;calm&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;status &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;suspended&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FatherCuthbert
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;attitude &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;troublesome&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;status &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;active&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The dataset does not include any instructions or actions. It only records the state of each priest.&lt;&#x2F;p&gt;
&lt;p&gt;SPARQL is a query language for Semantic Web data, such as the Turtle example above. It allows us to retrieve, manipulate, or update facts in a dataset. We can manipulate the dataset in very precise ways using SPARQL. For example, we can retrieve, using a SPARQL SELECT query, only the priest(s) with the troublesome attitude from the dataset. If there were more than one, this would return all of them.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sparql&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sparql &quot;&gt;&lt;code class=&quot;language-sparql&quot; data-lang=&quot;sparql&quot;&gt;&lt;span&gt;PREFIX ex: &amp;lt;http:&#x2F;&#x2F;example.org&#x2F;priest#&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;SELECT ?priest
&lt;&#x2F;span&gt;&lt;span&gt;WHERE {
&lt;&#x2F;span&gt;&lt;span&gt;    ?priest ex:attitude &amp;quot;troublesome&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;            ex:status &amp;quot;active&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This query finds the priest whose attitude is troublesome and status is active. In our dataset, this returns Father Cuthbert (&lt;code&gt;ex:FatherCuthbert&lt;&#x2F;code&gt; to be precise). This mirrors the “historical” interpretation from the LLM example, where the system inferred that the turbulent priest was the one with a troublesome attitude.&lt;&#x2F;p&gt;
&lt;p&gt;Alternatively, we can also have a query that interprets a turbulent priest as the one who is suspended. This shows another plausible interpretation if “turbulent” were mapped to the already suspended status.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sparql&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sparql &quot;&gt;&lt;code class=&quot;language-sparql&quot; data-lang=&quot;sparql&quot;&gt;&lt;span&gt;PREFIX ex: &amp;lt;http:&#x2F;&#x2F;example.org&#x2F;priest#&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;SELECT ?priest
&lt;&#x2F;span&gt;&lt;span&gt;WHERE {
&lt;&#x2F;span&gt;&lt;span&gt;    ?priest ex:status &amp;quot;suspended&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This query selects the priest who is currently suspended, which in our dataset is Father Benedict. Using different criteria allows us to see how interpretation can change the outcome even with the same dataset.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, the action implied by “get rid of the troublesome priest” can be represented in a CONSTRUCT query, producing a new graph where the troublesome priest (in historical interpretation) is marked as suspended.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sparql&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sparql &quot;&gt;&lt;code class=&quot;language-sparql&quot; data-lang=&quot;sparql&quot;&gt;&lt;span&gt;PREFIX ex: &amp;lt;http:&#x2F;&#x2F;example.org&#x2F;priest#&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;CONSTRUCT {
&lt;&#x2F;span&gt;&lt;span&gt;    ?priest ex:attitude ?attitude ;
&lt;&#x2F;span&gt;&lt;span&gt;            ex:status ?newStatus .
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;WHERE {
&lt;&#x2F;span&gt;&lt;span&gt;    ?priest ex:attitude ?attitude ;
&lt;&#x2F;span&gt;&lt;span&gt;            ex:status ?status .
&lt;&#x2F;span&gt;&lt;span&gt;    BIND(
&lt;&#x2F;span&gt;&lt;span&gt;        IF(?attitude = &amp;quot;troublesome&amp;quot; &amp;amp;&amp;amp; ?status = &amp;quot;active&amp;quot;, &amp;quot;suspended&amp;quot;, ?status) 
&lt;&#x2F;span&gt;&lt;span&gt;        AS ?newStatus
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This query creates a new Turtle dataset in which any priest who is troublesome and active has their status changed to suspended. All other priests retain their original attributes. By defining both the condition and the action explicitly, this approach removes the ambiguity that exists when using natural language alone.&lt;&#x2F;p&gt;
&lt;p&gt;Of course there can be many other interesting queries devised, especially if one expands upon the dataset. Universidad De Chile has a really nice Semantic Web &lt;a href=&quot;https:&#x2F;&#x2F;rdfplayground.dcc.uchile.cl&#x2F;&quot;&gt;playground&lt;&#x2F;a&gt; that one can use to experiment. Try writing to expand the dataset with an entry for a priest that is both troublesome and suspended and&#x2F;or write a query that returns only the priests that are both active and calm.&lt;&#x2F;p&gt;
&lt;p&gt;A huge benefit of evaluating these SPARQL queries, in a system such as those in the playground, is that they are precise and unambiguous. Each query specifies exactly which priests meet the criteria, and the result is fully determined by the dataset and the query itself. There is no guessing about what “turbulent” means or what action should be taken. This precision is in stark contrast to natural language prompts used with generative AI, where small ambiguities can lead to multiple interpretations and unintended results.&lt;&#x2F;p&gt;
&lt;p&gt;That said, the addition of generative AI opens up some interesting possibilities and can make the benefits of Semantic Web technologies much more accessible. For example, instead of having to write a SPARQL query by hand, one can ask an LLM to generate a natural language description of the action or even produce a draft query from a plain-language request. This combination allows users to leverage the precision of the Semantic Web while interacting in a more intuitive, natural way.&lt;&#x2F;p&gt;
&lt;p&gt;For example, when asking an LLM to write a SPARQL query for finding all priests who are calm and suspended, the model can produce a query like this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;PREFIX ex: &amp;lt;http:&#x2F;&#x2F;example.org&#x2F;priest#&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;SELECT ?priest
&lt;&#x2F;span&gt;&lt;span&gt;WHERE {
&lt;&#x2F;span&gt;&lt;span&gt;    ?priest ex:attitude &amp;quot;calm&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;            ex:status &amp;quot;suspended&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By combining generative AI with Semantic Web technologies, we can enjoy the best of both worlds: the intuitive ease of natural language and the precision of structured data. Ambiguities that could trip up an LLM alone are eliminated when the intent is clearly expressed in a Turtle graph and queried with SPARQL. With the right tools, we can keep our data well-behaved and avoid any turbulent issues from arising.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>They LiveView</title>
        <published>2026-01-10T00:00:00+00:00</published>
        <updated>2026-01-10T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/theyliveview/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/theyliveview/</id>
        
        <content type="html">&lt;p&gt;Creating interactive web experiences can quickly get complicated. Often, you’re juggling a front-end in one language and a back-end in another, all while figuring out how they communicate. &lt;a href=&quot;https:&#x2F;&#x2F;hexdocs.pm&#x2F;phoenix_live_view&#x2F;welcome.html&quot;&gt;LiveViews&lt;&#x2F;a&gt; offer a simpler alternative: you can build rich, interactive pages entirely from the back-end. LiveView keeps the server and browser efficiently in sync, letting you focus on the user experience rather than worrying about updating the interface in real time. In this article, I will show how this works with a small example LiveView inspired by the John Carpenter movie &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;They_Live&quot;&gt;They Live&lt;&#x2F;a&gt;. In the movie, the protagonist uses a pair of sunglasses to see which humans are actually aliens. In our LiveView, you can toggle sunglasses on or off to reveal the hidden aliens.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;An alien reflected in sunglasses, image inspired by the classic They Live movie poster.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;LiveView is a solution to the problem of how to organize code to create rich, fast, interactive user experiences in the browser. In the past, the view in the browser (i.e., an HTML page) was rendered entirely on the server. This approach is straightforward to organize, since all the code runs in one place and can often be written in a single language. The downside is that pages are static once rendered. Every time the user interacts with the page, the system has to send the interaction to the server, have the server process it, and then generate a whole new page to send back to the browser. This makes even small interactions slow and cumbersome. &lt;&#x2F;p&gt;
&lt;p&gt;Later, developers moved toward the modern model of giving the front-end a much larger role, often using a JavaScript framework to build single-page applications. This allows for dynamic, real-time interactions, but introduces complexity, including multiple languages, duplicated state, and the need to manage communication between client and server. LiveView offers a middle path. It enables developers to build rich, interactive experiences while keeping most of the code on the server. The server handles updates and LiveView automatically pushes them to the browser. While LiveView relies on a persistent backend connection, for many apps this is a small price to pay since most real-world applications already need a backend for databases, authentication, or business logic. As we will see later in this article, LiveView ensures that this communication is done very efficiently. The result is pages that are interactive, fast, and much simpler to maintain.&lt;&#x2F;p&gt;
&lt;p&gt;The best way to learn how things work is with a hands-on example, so let&#x27;s create our first LiveView. Although there are &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;liveviews&#x2F;liveviews&quot;&gt;many implementations inspired by it&lt;&#x2F;a&gt;, the original is part of the &lt;a href=&quot;https:&#x2F;&#x2F;www.phoenixframework.org&#x2F;&quot;&gt;Phoenix web application framework&lt;&#x2F;a&gt; written in the &lt;a href=&quot;https:&#x2F;&#x2F;elixir-lang.org&#x2F;&quot;&gt;Elixir programming language&lt;&#x2F;a&gt;. Assuming Phoenix is installed (see the &lt;a href=&quot;https:&#x2F;&#x2F;hexdocs.pm&#x2F;phoenix&#x2F;installation.html&quot;&gt;install instructions&lt;&#x2F;a&gt; for details) we can create and run a new Phoenix project with LiveView by running a few commands from the terminal.&lt;&#x2F;p&gt;
&lt;p&gt;To get started, we will create a new Phoenix project from the command line. Nowadays, LiveView ships with Phoenix by default, no extra install is needed. Since our example does not need a database, we can also skip setting up the database library Ecto (using the &lt;code&gt;-no-ecto&lt;&#x2F;code&gt; flag).&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;mix phx.new they_live --no-ecto
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will create a new Phoenix project in the &lt;code&gt;they_live&lt;&#x2F;code&gt; directory. Next we can navigate to created directory and start the Phoenix application.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;cd they_live
&lt;&#x2F;span&gt;&lt;span&gt;mix phx.server
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After being asked to fetch the dependencies, we should now have a Phoenix app up and running. With a browser we can navigate to its main endpoint (e.g. http:&#x2F;&#x2F;localhost:4000&#x2F;).&lt;&#x2F;p&gt;
&lt;p&gt;Next, we are going to add an endpoint under &lt;code&gt;&#x2F;sunglasses&lt;&#x2F;code&gt; with our LiveView.&lt;&#x2F;p&gt;
&lt;p&gt;Create a new file at &lt;code&gt;lib&#x2F;they_live_web&#x2F;live&#x2F;sunglasses_live.ex&lt;&#x2F;code&gt; where the module of our LiveView will be:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defmodule &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;TheyLiveWeb&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;SunglassesLive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;TheyLiveWeb&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:live_view
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;mount&lt;&#x2F;span&gt;&lt;span&gt;(_params, _session, socket) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    people = [
&lt;&#x2F;span&gt;&lt;span&gt;      %{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: 1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:human&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;      %{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: 2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:alien&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;      %{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: 3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:human&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;      %{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: 4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:human&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;      %{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: 5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:alien&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;      %{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;id: 6&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:human&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;    ]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:ok&lt;&#x2F;span&gt;&lt;span&gt;, assign(socket, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sunglasses: false&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;people:&lt;&#x2F;span&gt;&lt;span&gt; people)}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;handle_event&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;toggle_sunglasses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, _value, socket) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:noreply&lt;&#x2F;span&gt;&lt;span&gt;, update(socket, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:sunglasses&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;(!&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;))}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;render&lt;&#x2F;span&gt;&lt;span&gt;(assigns) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;~H&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &amp;lt;h1&amp;gt;They Live View 👓&amp;lt;&#x2F;h1&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &amp;lt;button phx-click=&amp;quot;toggle_sunglasses&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;lt;%= if @sunglasses do %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        ( 😎 Sunglasses On )
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;lt;% else %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        ( 👓 Sunglasses Off )
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;lt;% end %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &amp;lt;&#x2F;button&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &amp;lt;p&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;lt;%= if @sunglasses do %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        You can see the aliens now!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;lt;% else %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        Everything looks normal.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;lt;% end %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &amp;lt;&#x2F;p&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &amp;lt;div style=&amp;quot;margin-top: 1rem; font-size: 2rem;&amp;quot;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;lt;span
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        :for={person &amp;lt;- @people}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        :key={person.id}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        style=&amp;quot;margin-right: 0.5rem;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        &amp;lt;%= if person.type == :alien and @sunglasses do %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;          👽
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        &amp;lt;% else %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;          🙂
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        &amp;lt;% end %&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      &amp;lt;&#x2F;span&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &amp;lt;&#x2F;div&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, add a route in the file &lt;code&gt;lib&#x2F;they_live_web&#x2F;router.ex&lt;&#x2F;code&gt;. This can be done by entering the following route:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;live &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;sunglasses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;SunglassesLive
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;under the main route, so we will have in the file&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;    get &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;PageController&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:home
&lt;&#x2F;span&gt;&lt;span&gt;    live &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;sunglasses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;SunglassesLive
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now visiting &lt;code&gt;http:&#x2F;&#x2F;localhost:4000&#x2F;sunglasses&lt;&#x2F;code&gt; will show us our LiveView.  We should see the following initially:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;They Live View 👓
&lt;&#x2F;span&gt;&lt;span&gt;( 👓 Sunglasses Off )
&lt;&#x2F;span&gt;&lt;span&gt;Everything looks normal.
&lt;&#x2F;span&gt;&lt;span&gt;🙂 🙂 🙂 🙂 🙂 🙂 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And after pressing the button (i.e. clicking on anything between the brackets) we put on the sunglasses and reveal the aliens:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;They Live View 👓
&lt;&#x2F;span&gt;&lt;span&gt;( 😎 Sunglasses On )
&lt;&#x2F;span&gt;&lt;span&gt;You can see the aliens now!
&lt;&#x2F;span&gt;&lt;span&gt;🙂 👽 🙂 🙂 👽 🙂 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we have our working LiveView, let’s go over all the code in this module to better understand what a LiveView does.&lt;&#x2F;p&gt;
&lt;p&gt;The top-level portion defines a module &lt;code&gt;TheyLiveWeb.SunglassesLive&lt;&#x2F;code&gt; and specifies it to be a LiveView:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defmodule &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;TheyLiveWeb&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;SunglassesLive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;TheyLiveWeb&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:live_view
&lt;&#x2F;span&gt;&lt;span&gt;  ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This line brings in all the LiveView functionality. It marks this module as a special kind of page that can maintain state on the server and update the browser in real time.&lt;&#x2F;p&gt;
&lt;p&gt;Inside the module there are three main parts: a mount function, a handle_event function, and a render function. These are the core pieces of a LiveView. At a high level, a LiveView is a server-side process that starts from an initial state, receives and handles events from the browser, updates its state, and then renders any updates.&lt;&#x2F;p&gt;
&lt;p&gt;The key responsibilities of these functions are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;mount&lt;&#x2F;strong&gt; sets the initial state when the page loads.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;handle_event&lt;&#x2F;strong&gt; updates the state in response to user actions.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;render&lt;&#x2F;strong&gt; describes what the page should look like based on the current state.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;
&lt;p&gt;Let’s go over each one in more detail:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;mount&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This function runs when the page first loads. It sets up the initial state of the LiveView. In our example, we define people (a list of humans and aliens) and sunglasses (whether the sunglasses are on or off). This state lives on the server and is the “source of truth” for the page. Everything that is rendered to the front end is derived from these values.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;handle_event&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This function responds to user actions. When the user clicks the sunglasses button, this function is called. It updates the state by flipping sunglasses on or off. After the state changes, LiveView automatically re-renders the parts of the page that need updating. You do not have to write any JavaScript or manually manipulate the page.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;render&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This function defines what the page should look like based on the current state. In our example:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The button changes text depending on whether sunglasses are on.&lt;&#x2F;li&gt;
&lt;li&gt;A message tells the user if aliens are visible.&lt;&#x2F;li&gt;
&lt;li&gt;The row of emoji shows humans, with aliens revealed among them when the sunglasses are on.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;
&lt;p&gt;Whenever the state changes, LiveView calls the render function again, compares it to the previous render, and sends only what actually needs to change to the browser. This keeps interactions fast and smooth and avoids sending a full HTML page back each time.&lt;&#x2F;p&gt;
&lt;p&gt;You can see this in action in the browser’s console or network logs. LiveView maintains a persistent WebSocket connection between the browser and server. When you toggle the sunglasses, the server does not resend the entire page. For example, the page heading They Live View 👓 is never sent again. Only the button label, the explanatory text, and the emoji row are updated. In the latest LiveView version (1.1 onwards), even the human emojis in the row remain untouched, so only the aliens are updated. Here is a literal fragment of the update payload sent over the wire:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;js&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-js &quot;&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    ( 😎 Sunglasses On )&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n  &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;],
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    You can see the aliens now!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n  &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;],
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;      👽&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n    &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;LiveView is even smart enough to figure out that you only need one patch for the alien emoji, which is applied in multiple positions in the emoji row.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, this is just a small example. LiveView with Phoenix can do much more: nested components, forms, real-time notifications, presence tracking, and hooks (escape hatches for when you really need JavaScript). Phoenix comes with &lt;a href=&quot;https:&#x2F;&#x2F;tailwindcss.com&#x2F;&quot;&gt;Tailwind&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;daisyui.com&#x2F;?lang=en&quot;&gt;DaisyUI&lt;&#x2F;a&gt; integration out of the gate so you can get started with nicely styled components. You can build rich, interactive applications without getting bogged down managing a separate front-end framework and its communication with the server.&lt;&#x2F;p&gt;
&lt;p&gt;With LiveView, you can get things done, keep your pages interactive, and still have time to chew bubblegum (as long as you are not out of gum).&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Gathering Rust</title>
        <published>2025-09-28T00:00:00+00:00</published>
        <updated>2025-09-28T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/gatheringrust/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/gatheringrust/</id>
        
        <content type="html">&lt;p&gt;When it comes to learning a new programming language, I’ve found that building a small, practical program is one of the best ways to get started. Recently I began exploring &lt;a href=&quot;https:&#x2F;&#x2F;www.rust-lang.org&#x2F;&quot;&gt;Rust&lt;&#x2F;a&gt;, and to get a feel for the language I decided to write a tool based on a game I was playing at the time: &lt;a href=&quot;https:&#x2F;&#x2F;magic.wizards.com&#x2F;en&#x2F;mtgarena&quot;&gt;Magic: The Gathering Arena (Arena)&lt;&#x2F;a&gt;. The game’s log files contain a lot of information, including records of the cards I had used, but not in an easily readable way. I wanted to build a tool that could extract that data. This article describes how I approached the project using Rust.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;An image of a cybernetic flower with rust. The language was actually named after the fungus and not the oxidation reaction, though for the image both could apply. &lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;I wanted to learn Rust because of its strong focus on safety and performance, as well as the growing ecosystem around it. Frameworks like &lt;a href=&quot;https:&#x2F;&#x2F;v2.tauri.app&#x2F;&quot;&gt;Tauri&lt;&#x2F;a&gt; and even &lt;a href=&quot;https:&#x2F;&#x2F;www.getzola.org&#x2F;&quot;&gt;Zola&lt;&#x2F;a&gt; with which this site is generated are written in Rust. Rust’s ownership model and lifetimes can seem tricky at first glance, so I was looking for a small, manageable project that would give me some hands-on experience without being overwhelming. Parsing game log files felt like the right balance of practical and approachable.&lt;&#x2F;p&gt;
&lt;p&gt;In Arena, two players play against each other with decks made of collectible cards. There is a huge list of cards available, with evocative names such as &amp;quot;Lightning Bolt&amp;quot; and &amp;quot;Serra Angel&amp;quot;, that could make up a deck. In general the decks you play are based on the cards available in your collection, so it is nice to look over it, and craft the deck you want to play. Unfortunately while you can go over your collection in the game client, there is no easy way to export your set of cards from it. &lt;&#x2F;p&gt;
&lt;p&gt;This is where the game log of Arena comes in. The log emits a huge number of events, many of which could be analyzed by external tools. While it doesn’t currently include an event for all the cards in your collection, it does show all the cards used in each deck. To make things a bit more tricky, it uses an internal Arena specific card ID to identify the cards, as opposed to the card names more familiar to players. Given these challenges, my goal was straightforward: take an MTG Arena log and turn it into a clear, human-readable list of the cards I have, including their names.&lt;&#x2F;p&gt;
&lt;p&gt;We can structure the code for this application into two main parts. First, we have some data structures to hold the information gathered from the logs, the mapping of arena IDs to names, and the output that we like to show. Second, we have some functions that read the information in from the files, gather the card information from the logs, match this information with the mappings and finally write the output to a file. In addition, we will have unit tests to ensure that the functions work as expected.&lt;&#x2F;p&gt;
&lt;p&gt;With the code structure in mind, let’s walk through the source code of the application, which fits neatly in a single file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;serde::{Deserialize, Serialize};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;serde_json::{Deserializer, Value};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::collections::{HashMap, HashSet};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::fs::{write, File};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::io::{BufRead, BufReader};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As is usual in many other languages we start by listing our imports first. The &lt;code&gt;serde&lt;&#x2F;code&gt; framework allows for easy mapping of Rust data structures to other formats. As we are aiming to serialize the extracted information into JSON files we are going to use &lt;code&gt;serde_json&lt;&#x2F;code&gt;, which is an implementation of &lt;code&gt;serde&lt;&#x2F;code&gt; for the JSON format. We also have imports from the standard library that we for generic data structures and file writing&#x2F;reading.&lt;&#x2F;p&gt;
&lt;p&gt;The next part of the code are the data structures that can represent the information in the log file. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Data structures for the parts of the log that has information about the decks and cards in them.
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Serialize, Deserialize, Debug, Default)]
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;serde&lt;&#x2F;span&gt;&lt;span&gt;(rename_all = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;PascalCase&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;DecksWrapper {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;decks&lt;&#x2F;span&gt;&lt;span&gt;: HashMap&amp;lt;String, Deck&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Serialize, Deserialize, Debug, Default)]
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;serde&lt;&#x2F;span&gt;&lt;span&gt;(rename_all = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;PascalCase&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;Deck {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;main_deck&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;CardEntry&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sideboard&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;CardEntry&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;reduced_sideboard&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;CardEntry&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;command_zone&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;CardEntry&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;companions&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;CardEntry&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;serde&lt;&#x2F;span&gt;&lt;span&gt;(rename_all = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;camelCase&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;card_id&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;quantity&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The data structures that we use are defined here using Rust structs. They are made up of either primitive types such as &lt;code&gt;card_id&lt;&#x2F;code&gt; and &lt;code&gt;quantity&lt;&#x2F;code&gt;, or collections of other structs such as &lt;code&gt;main_deck&lt;&#x2F;code&gt; and &lt;code&gt;sideboard&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In addition, we add attributes to our structs. For example, &lt;code&gt;#[derive(Serialize, Deserialize, Debug, Default)]&lt;&#x2F;code&gt; lets us automatically serialize and deserialize the structs, print them with Debug, and construct default values. The &lt;code&gt;#[serde(rename_all = &amp;quot;PascalCase&amp;quot;)]&lt;&#x2F;code&gt; attribute tells Serde how to map between Rust’s convention of snake_case field names (e.g. main_deck) and the PascalCase keys used in the Arena log JSON (e.g. MainDeck).&lt;&#x2F;p&gt;
&lt;p&gt;To illustrate with a minimal example, assume we have the following fragment in the JSON logs:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;MainDeck&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;                { &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cardId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1001&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;quantity&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;                { &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cardId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1002&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;quantity&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This would map to a concrete Rust value such as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; deck1 = Deck {
&lt;&#x2F;span&gt;&lt;span&gt;            main_deck: vec![
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id:  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1001&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1002&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;            ],
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; assuming other fields exist and are empty
&lt;&#x2F;span&gt;&lt;span&gt;            ..Default::default()
&lt;&#x2F;span&gt;&lt;span&gt;        };
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We’ll see this in action in one of the unit tests later.&lt;&#x2F;p&gt;
&lt;p&gt;Next up are the remaining data structures that we’ll use for representing the full card collection, as well as general card information from &lt;a href=&quot;https:&#x2F;&#x2F;scryfall.com&#x2F;&quot;&gt;Scryfall&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Data structure that represents how many of each card we have available based on the decks.
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Serialize, Deserialize, Debug, Default)]
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;serde&lt;&#x2F;span&gt;&lt;span&gt;(rename_all = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;camelCase&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;CardCollection {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cards&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;CardEntry&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Data structures for Scryfall (general card knowledge) information
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Serialize, Deserialize, Debug, Default)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;ScryfallData {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cards&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;ScryfallCard&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Serialize, Deserialize, Debug, Default)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;ScryfallCard {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arena_id&lt;&#x2F;span&gt;&lt;span&gt;: Option&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: String,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With these additional structs in place, we now have everything we need to represent both the decks pulled from the Arena logs and the more general card information from Scryfall. But representation alone isn’t enough: we also need a way to process the raw decks into something useful.&lt;&#x2F;p&gt;
&lt;p&gt;That’s where the CardCollection implementation comes in. This type acts as a bridge between individual deck data and the overall view of a player’s collection. By aggregating card entries across decks, we can answer questions like “How many copies of this card do I actually own?” or “Which Arena IDs are present in my collection?”&lt;&#x2F;p&gt;
&lt;p&gt;The following impl block shows how we can construct a CardCollection from a DecksWrapper, as well as provide some helper methods for querying it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Implementation that can derive the card collection from the decks. 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;CardCollection {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;from_decks_wrapper&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;decks_wrapper&lt;&#x2F;span&gt;&lt;span&gt;: DecksWrapper) -&amp;gt; CardCollection {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::collections::HashMap;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; card_map: HashMap&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; = HashMap::new();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; deck in decks_wrapper.decks.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;values&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; entry in &amp;amp;deck.main_deck {
&lt;&#x2F;span&gt;&lt;span&gt;                *card_map.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span&gt;(entry.card_id).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;or_insert&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) += entry.quantity;
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; entry in &amp;amp;deck.sideboard {
&lt;&#x2F;span&gt;&lt;span&gt;                *card_map.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span&gt;(entry.card_id).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;or_insert&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) += entry.quantity;
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; entry in &amp;amp;deck.reduced_sideboard {
&lt;&#x2F;span&gt;&lt;span&gt;                *card_map.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span&gt;(entry.card_id).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;or_insert&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) += entry.quantity;
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; entry in &amp;amp;deck.command_zone {
&lt;&#x2F;span&gt;&lt;span&gt;                *card_map.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span&gt;(entry.card_id).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;or_insert&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) += entry.quantity;
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; entry in &amp;amp;deck.companions {
&lt;&#x2F;span&gt;&lt;span&gt;                *card_map.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span&gt;(entry.card_id).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;or_insert&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) += entry.quantity;
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; cards = card_map
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;card_id&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;quantity&lt;&#x2F;span&gt;&lt;span&gt;)| CardEntry { card_id, quantity })
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        CardCollection { cards }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;get_arena_ids&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; HashSet&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.cards.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span&gt;| entry.card_id).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;search_with_id&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;card_id&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; found_entry = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.cards.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;find&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span&gt;| c.card_id == card_id).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;cloned&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; found_entry {
&lt;&#x2F;span&gt;&lt;span&gt;            Some(result) =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; result,
&lt;&#x2F;span&gt;&lt;span&gt;            None =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: card_id,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                }
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The CardCollection implementation is where we actually make use of the deck data. Let’s go through each method in turn:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;from_decks_wrapper
This function takes all of the decks inside a DecksWrapper and combines them into a single CardCollection. It does so by walking through every zone of every deck (main_deck, sideboard, etc.), summing the quantities for each card ID into a HashMap&amp;lt;u64, u32&amp;gt;. Once all the decks are processed, that map is converted back into a list of CardEntry values.
In short, it turns lots of decks with overlapping cards into one unified collection with aggregated counts.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;get_arena_ids
Sometimes we don’t need the full card entries, just the set of card IDs that appear in the collection. This helper method collects all the card_ids into a HashSet&lt;u64&gt;. Using a HashSet here is efficient because it guarantees uniqueness — we only care whether a card ID exists, not how many times it occurs.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;search_with_id
This is a small convenience function for looking up a specific card by its Arena ID. If the card exists in the collection, it returns the corresponding CardEntry. If not, it returns a default CardEntry with the given card_id and a quantity of 0.
This approach avoids dealing with Option&lt;CardEntry&gt; on the caller side, at the cost of always producing a value — even if it represents “zero copies.”&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Together, these methods make CardCollection the workhorse of the program: it gives us an aggregated view of what cards we have and lets us easily query or transform that data.&lt;&#x2F;p&gt;
&lt;p&gt;So far, we’ve been working with data structures and helper methods that assume we already have the deck data in memory. But of course, the whole point of this tool is to read the Arena log and extract that information automatically. The parse_decks_from_log function is responsible for this: it scans through the log file line by line, looks for entries that contain deck data, and deserializes the first one it finds into a CardCollection.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Takes a log file and aims to extract a cardcollection from it.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parse_decks_from_log&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;P: AsRef&amp;lt;std::path::Path&amp;gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;: P) -&amp;gt; Option&amp;lt;CardCollection&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; file = File::open(path).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ok&lt;&#x2F;span&gt;&lt;span&gt;()?;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; reader = BufReader::new(file);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; line in reader.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;flatten&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; line.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;contains&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Decks&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if let &lt;&#x2F;span&gt;&lt;span&gt;Ok(decks) = serde_json::from_str::&amp;lt;DecksWrapper&amp;gt;(&amp;amp;line) {
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;Some(CardCollection::from_decks_wrapper(decks));
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    None
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This function ties everything together by connecting the raw log file to the structured data we’ve been working with. A couple of details are worth pointing out:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Return type Option&lt;CardCollection&gt;
We use Option because it’s possible that the log file doesn’t contain any deck data at all. In that case, the function cleanly returns None instead of panicking. When a deck is found, we wrap it in Some and return it.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;? operator for error handling
The expression File::open(path).ok()? is a compact way of saying: try to open the file, and if that fails, return None immediately. This avoids cluttering the function with explicit match statements while still being safe.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Finding deck data
The function looks for lines containing &amp;quot;Decks&amp;quot; because that’s how Arena includes the relevant JSON in its log. Once such a line is found, we attempt to deserialize it into a DecksWrapper.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;One section, multiple decks
Each log file has only one &amp;quot;Decks&amp;quot; section, but that section may describe several different decks at once. That’s why DecksWrapper contains a HashMap&amp;lt;String, Deck&amp;gt; — the keys are deck identifiers, and the values are the deck lists themselves. Since there’s only ever one &amp;quot;Decks&amp;quot; section, we can safely return after parsing it.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;With this in place, we now have a straightforward path:&lt;&#x2F;p&gt;
&lt;p&gt;Open the Arena log.&lt;&#x2F;p&gt;
&lt;p&gt;Parse out the &amp;quot;Decks&amp;quot; section.&lt;&#x2F;p&gt;
&lt;p&gt;Turn it into a CardCollection that we can query and extend later.&lt;&#x2F;p&gt;
&lt;p&gt;Next, we need a way to extract the Scryfall information into in-memory structs, similar to how we did it with the Arena logs. Scryfall provides both online APIs and bulk data files. For our purposes, using the bulk files is simpler and more reliable than hitting the API repeatedly.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;extract_arena_cards&lt;&#x2F;code&gt; function reads a Scryfall JSON file and filters it down to only the cards that exist in our collection of Arena IDs. It works as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;extract_arena_cards&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;P: AsRef&amp;lt;std::path::Path&amp;gt;&amp;gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;: P,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arena_ids&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;HashSet&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; Vec&amp;lt;ScryfallCard&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; file = File::open(path).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to open Scryfall file&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; reader = BufReader::new(file);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Deserialize as a stream of JSON values
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; stream = Deserializer::from_reader(reader).into_iter::&amp;lt;Value&amp;gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; cards = Vec::new();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Iterate over each element in the stream, handling Result&amp;lt;Value, Error&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; result in stream {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; result {
&lt;&#x2F;span&gt;&lt;span&gt;            Ok(value) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Check if value is an array, and proceed accordingly
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; value {
&lt;&#x2F;span&gt;&lt;span&gt;                    Value::Array(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ref&lt;&#x2F;span&gt;&lt;span&gt; array) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Iterate over each element of the array
&lt;&#x2F;span&gt;&lt;span&gt;                        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; element in array {
&lt;&#x2F;span&gt;&lt;span&gt;                            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Process each element (card) in the array
&lt;&#x2F;span&gt;&lt;span&gt;                            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; json_str = serde_json::to_string_pretty(&amp;amp;element)
&lt;&#x2F;span&gt;&lt;span&gt;                                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to convert JSON to string&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;                            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Directly use u64 for arena_id
&lt;&#x2F;span&gt;&lt;span&gt;                            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if let &lt;&#x2F;span&gt;&lt;span&gt;Some(arena_id) = element.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;arena_id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;| v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;as_u64&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;                            {
&lt;&#x2F;span&gt;&lt;span&gt;                                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; arena_ids.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;contains&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;arena_id) {
&lt;&#x2F;span&gt;&lt;span&gt;                                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Try parsing the card into the struct
&lt;&#x2F;span&gt;&lt;span&gt;                                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span&gt;serde_json::from_value::&amp;lt;ScryfallCard&amp;gt;(element.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;()) {
&lt;&#x2F;span&gt;&lt;span&gt;                                        Ok(card) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                                            cards.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(card);
&lt;&#x2F;span&gt;&lt;span&gt;                                        }
&lt;&#x2F;span&gt;&lt;span&gt;                                        Err(e) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                                            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Print out the error and the value that failed to parse
&lt;&#x2F;span&gt;&lt;span&gt;                                            println!(
&lt;&#x2F;span&gt;&lt;span&gt;                                                &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to parse card with arena_id &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                                                arena_id, e
&lt;&#x2F;span&gt;&lt;span&gt;                                            );
&lt;&#x2F;span&gt;&lt;span&gt;                                            println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed card data: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, json_str);
&lt;&#x2F;span&gt;&lt;span&gt;                                        }
&lt;&#x2F;span&gt;&lt;span&gt;                                    }
&lt;&#x2F;span&gt;&lt;span&gt;                                }
&lt;&#x2F;span&gt;&lt;span&gt;                            }
&lt;&#x2F;span&gt;&lt;span&gt;                        }
&lt;&#x2F;span&gt;&lt;span&gt;                    }
&lt;&#x2F;span&gt;&lt;span&gt;                    _ =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                        println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The provided JSON is not an array&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;                    }
&lt;&#x2F;span&gt;&lt;span&gt;                }
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;            Err(e) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Handle the error case for the result from the deserializer
&lt;&#x2F;span&gt;&lt;span&gt;                println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Error deserializing JSON: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, e);
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    cards
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The function works by taking the path to a Scryfall JSON file along with a set of Arena IDs that we want to include. It reads the file as a stream of JSON values, which is especially useful for large bulk datasets because it avoids loading everything into memory at once. As it iterates over each JSON element, the function checks whether the card has an arena_id and whether that ID is part of our collection. If it is, the element is deserialized into a ScryfallCard struct and added to the output vector.&lt;&#x2F;p&gt;
&lt;p&gt;Errors during deserialization are handled gracefully: the function prints both the error and the raw JSON that caused it, allowing us to debug any problematic entries without stopping the entire process. By filtering and deserializing only the relevant cards, this approach ensures that we keep memory usage low and produce a clean, usable dataset for further processing or analysis.&lt;&#x2F;p&gt;
&lt;p&gt;Once we’ve extracted and filtered the relevant Scryfall cards, the next step is to persist this data so it can be easily inspected, shared, or used by other parts of our program. The save_cards_to_file function does exactly that: it takes a slice of ScryfallCard structs and writes them to a JSON file in a human-readable format.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;save_cards_to_file&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;P: AsRef&amp;lt;std::path::Path&amp;gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;: P, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cards&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;[ScryfallCard]) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; json = serde_json::to_string_pretty(&amp;amp;cards).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to serialize cards.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(path, json).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to write to file.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Internally, it uses serde_json::to_string_pretty to convert the vector of cards into nicely formatted JSON. Then it writes that string to the specified file path. This simple helper encapsulates the serialization and file writing in one place, so we don’t have to repeat these steps every time we want to save card data.&lt;&#x2F;p&gt;
&lt;p&gt;With all the building blocks in place — parsing Arena logs, aggregating decks into a CardCollection, extracting relevant Scryfall cards, and saving them to a file — we are ready to see everything in action. The main function ties these pieces together into a complete workflow, turning raw log files and bulk data into a clean, usable card collection.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Step 1: Parse the log file and collect card collection information from available decks.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; deck_file = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;input-files&#x2F;Player.log&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Example log file path
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; card_collection = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;parse_decks_from_log&lt;&#x2F;span&gt;&lt;span&gt;(deck_file) {
&lt;&#x2F;span&gt;&lt;span&gt;        Some(collection) =&amp;gt; collection,
&lt;&#x2F;span&gt;&lt;span&gt;        None =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;            println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to parse decks from the log.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    };
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Step 2: Gather all the arena card IDs from the collection.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; arena_ids: HashSet&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; = card_collection.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get_arena_ids&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Step 3: Extract the Scryfall cards that match the arena IDs
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; scryfall_file = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;input-files&#x2F;scryfall.json&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Example Scryfall data file path
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; scryfall_cards = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;extract_arena_cards&lt;&#x2F;span&gt;&lt;span&gt;(scryfall_file, &amp;amp;arena_ids);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Step 4: Save the filtered Scryfall cards to a file
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; output_file = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;collection_output.json&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Example output file path
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;save_cards_to_file&lt;&#x2F;span&gt;&lt;span&gt;(output_file, &amp;amp;scryfall_cards);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    println!(
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Successfully extracted and saved &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; different cards.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;        scryfall_cards.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    );
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The main function follows a clear sequence. First, it parses the Arena log and builds a CardCollection from the available decks. Next, it collects all the unique Arena IDs from that collection. Then, it extracts the corresponding Scryfall cards from the bulk JSON file, filtering only the relevant cards. Finally, it saves the resulting collection to a JSON file, ready for inspection or further analysis. By structuring the workflow this way, each helper function is small and focused, while the main function orchestrates the full process in an easy-to-follow manner.&lt;&#x2F;p&gt;
&lt;p&gt;With the main function, we’ve shown how all the pieces come together to parse Arena logs, enrich the data with Scryfall information, and save the resulting collection. But how can we be sure that each part works correctly? Even in a small project, writing a few unit tests is a good way to verify that our code behaves as expected. The next section demonstrates testing key functionality: parsing decks from JSON, aggregating cards into a CardCollection, and checking that card lookups with search_with_id return the correct results.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cfg&lt;&#x2F;span&gt;&lt;span&gt;(test)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span&gt;tests {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::collections::HashMap;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use crate&lt;&#x2F;span&gt;&lt;span&gt;::CardCollection;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use super&lt;&#x2F;span&gt;&lt;span&gt;::CardEntry;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use super&lt;&#x2F;span&gt;&lt;span&gt;::Deck;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use super&lt;&#x2F;span&gt;&lt;span&gt;::DecksWrapper;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    #[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test_decks_wrapper_parsing&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; json_data = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;r&lt;&#x2F;span&gt;&lt;span&gt;#&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;            &amp;quot;Decks&amp;quot;: {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                &amp;quot;abc123&amp;quot;: {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                    &amp;quot;MainDeck&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                        { &amp;quot;cardId&amp;quot;: 1001, &amp;quot;quantity&amp;quot;: 2 },
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                        { &amp;quot;cardId&amp;quot;: 1002, &amp;quot;quantity&amp;quot;: 3 }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                    ],
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                    &amp;quot;Sideboard&amp;quot;: [],
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                    &amp;quot;ReducedSideboard&amp;quot;: [],
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                    &amp;quot;CommandZone&amp;quot;: [],
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                    &amp;quot;Companions&amp;quot;: [],
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                    &amp;quot;CardSkins&amp;quot;: []
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;                }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;            }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;        &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;#;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; parsed: Result&amp;lt;DecksWrapper, _&amp;gt; = serde_json::from_str(json_data);
&lt;&#x2F;span&gt;&lt;span&gt;        assert!(parsed.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;is_ok&lt;&#x2F;span&gt;&lt;span&gt;(), &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to parse DecksWrapper&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; decks_wrapper = parsed.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; deck = decks_wrapper.decks.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc123&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Deck not found&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(deck.main_deck.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(deck.main_deck[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;].card_id, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1001&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(deck.main_deck[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;].quantity, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    #[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test_card_collection_creation&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; deck1 = Deck {
&lt;&#x2F;span&gt;&lt;span&gt;            main_deck: vec![
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;            ],
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; assuming other fields exist and are empty
&lt;&#x2F;span&gt;&lt;span&gt;            ..Default::default()
&lt;&#x2F;span&gt;&lt;span&gt;        };
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; deck2 = Deck {
&lt;&#x2F;span&gt;&lt;span&gt;            main_deck: vec![
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;            ],
&lt;&#x2F;span&gt;&lt;span&gt;            ..Default::default()
&lt;&#x2F;span&gt;&lt;span&gt;        };
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; decks_wrapper: DecksWrapper = DecksWrapper {
&lt;&#x2F;span&gt;&lt;span&gt;            decks: (HashMap::from([
&lt;&#x2F;span&gt;&lt;span&gt;                (String::from(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;00001&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;), deck1),
&lt;&#x2F;span&gt;&lt;span&gt;                (String::from(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;00002&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;), deck2),
&lt;&#x2F;span&gt;&lt;span&gt;            ])),
&lt;&#x2F;span&gt;&lt;span&gt;        };
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; card_collection = CardCollection::from_decks_wrapper(decks_wrapper);
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(card_collection.cards.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(
&lt;&#x2F;span&gt;&lt;span&gt;            card_collection.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;search_with_id&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;            CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        );
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    #[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test_search_with_id_found&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; collection = CardCollection {
&lt;&#x2F;span&gt;&lt;span&gt;            cards: vec![
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;101&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;202&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;            ],
&lt;&#x2F;span&gt;&lt;span&gt;        };
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; result = collection.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;search_with_id&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;101&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(result.card_id, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;101&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(result.quantity, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    #[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test_search_with_id_not_found&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; collection = CardCollection {
&lt;&#x2F;span&gt;&lt;span&gt;            cards: vec![
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;101&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;                CardEntry {
&lt;&#x2F;span&gt;&lt;span&gt;                    card_id: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;202&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    quantity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                },
&lt;&#x2F;span&gt;&lt;span&gt;            ],
&lt;&#x2F;span&gt;&lt;span&gt;        };
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; result = collection.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;search_with_id&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;999&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(result.card_id, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;999&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        assert_eq!(result.quantity, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Default case
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This small project has been a fun way to dive into Rust. From parsing Arena logs to matching cards with Scryfall data, each step gave me hands-on experience with structs, collections, serialization, and basic file handling. I hope it also highlights the value of writing small, focused functions and a few tests for them. This makes even a modest tool feel solid and reliable.&lt;&#x2F;p&gt;
&lt;p&gt;Beyond the technical details, it’s rewarding to see raw log files transformed into a structured, human-readable collection of cards. While this tool is simple, it illustrates how Rust can help you approach a problem methodically, building up a workflow from small, composable pieces. And with plenty of room for new features or experiments, using Rust ensures that you will always have a winning hand.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Escaping the Compactor with Linked Data</title>
        <published>2025-05-17T00:00:00+00:00</published>
        <updated>2025-05-17T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/trashcompactor/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/trashcompactor/</id>
        
        <content type="html">&lt;p&gt;If you ever need to represent knowledge in a way that&#x27;s easy for both humans and machines to understand, while working seamlessly with existing &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;JSON&quot;&gt;JSON&lt;&#x2F;a&gt; tools and libraries, &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;JSON-LD&quot;&gt;JSON-LD&lt;&#x2F;a&gt; has you covered. JSON-LD, short for JavaScript Object Notation for &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Linked_data&quot;&gt;Linked Data&lt;&#x2F;a&gt;, allows you to express knowledge as a graph in any domain. A good example is how &lt;a href=&quot;https:&#x2F;&#x2F;developers.google.com&#x2F;search&#x2F;docs&#x2F;advanced&#x2F;structured-data&#x2F;intro-structured-data&quot;&gt;Google&lt;&#x2F;a&gt; is utilizing Linked Data to very precisely list information, such as recipes, book descriptions, events, products, and more.&lt;&#x2F;p&gt;
&lt;p&gt;One great feature of JSON-LD is its built-in support for &lt;code&gt;compacting&lt;&#x2F;code&gt; and &lt;code&gt;expanding&lt;&#x2F;code&gt; the representations of knowledge.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;➡️⬅️ Compacting makes the data easier for humans to read by shortening and simplifying the structure.&lt;&#x2F;li&gt;
&lt;li&gt;⬅️➡️ Expanding restructures the data to be more explicit and uniform, making it easier for machines to process.
&lt;br&gt;&lt;br&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;To show how these concepts work in practice I will illustrate it with an example from Star Wars. Given that we are talking about compacting and expanding, let&#x27;s represent the characters in the iconic trash compactor scene from Star Wars using JSON-LD. For those unfamiliar with the scene, it starts with the protagonists, Luke, Han, Leia and Chewbacca falling into a trash compactor during an escape. As the walls begin to close in to crush the garbage, with the characters stuck in it, they frantically search for a way to stop the process. In the end they manage to deactivate the compactor by contacting the two droids, C-3PO and R2-D2, who manage to shut it down just in time.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The trash compactor scene. Iconic enough to have its own Lego set. Copyright Lego&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;We&#x27;ll start by describing the characters who fall into the compactor in a compacted JSON-LD format.
(If you want to follow along and experiment yourself, &lt;a href=&quot;https:&#x2F;&#x2F;json-ld.org&#x2F;playground&#x2F;&quot;&gt;JSON-LD Playground&lt;&#x2F;a&gt; is a great online tool for trying out Linked Data!)&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s the compacted JSON-LD:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@context&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:member&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;example.org&#x2F;starwars-ld&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Star Wars Trash Compactor Scene&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Han Solo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Han_Solo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A roguish smuggler and captain of the Millennium Falcon.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Corellia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Luke Skywalker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Luke_Skywalker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A farm boy from Tatooine thrust into the fight against the Empire.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Polis Massa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Leia Organa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Princess_Leia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A princess of Alderaan and an important member of the Rebel Alliance.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Polis Massa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Chewbacca&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Chewbacca&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A loyal Wookiee warrior and co-pilot of the Millennium Falcon.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Kashyyyk&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This representation, which also is a valid JSON, gives the names, a small description and the birthplace for each of the characters. One place where it differs from a regular JSON representation is that it uses the @context section to link to a common terminology in &lt;a href=&quot;https:&#x2F;&#x2F;schema.org&#x2F;&quot;&gt;Schema.org&lt;&#x2F;a&gt;. This allows for software that can make use of such common vocabularies to precisely interpret the exact meaning in our description, without the need to guess and potentially hallucinate its meaning.&lt;&#x2F;p&gt;
&lt;p&gt;In order to unambiguously represent each concept Linked Data tend to use full URIs, such as &lt;code&gt;http:&#x2F;&#x2F;schema.org&#x2F;name&lt;&#x2F;code&gt;, but reading full URIs each time would be quite exhausting for a human reader. Instead, in this compacted form, the context and prefixes it makes easy for human readers to go understand the JSON-LD file, without ever losing the context.&lt;&#x2F;p&gt;
&lt;p&gt;For machines however, it can be easier to interpret the information that every identifier is written out fully, such as &lt;code&gt;http:&#x2F;&#x2F;schema.org&#x2F;name&lt;&#x2F;code&gt; as opposed to &lt;code&gt;name&lt;&#x2F;code&gt;.  For this we can &lt;code&gt;expand&lt;&#x2F;code&gt; our JSON-LD document.&lt;&#x2F;p&gt;
&lt;p&gt;The expanded version is as follows: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;example.org&#x2F;starwars-ld&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;member&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;      {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Corellia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A charming smuggler and captain of the Millennium Falcon.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Han Solo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Han_Solo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ]
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Polis Massa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A young Jedi learning the ways of the Force.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Luke Skywalker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Luke_Skywalker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ]
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Polis Massa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A fearless princess and leader of the Rebel Alliance.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Leia Organa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Princess_Leia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ]
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Kashyyyk&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A loyal Wookiee warrior and Han Solo&amp;#39;s co-pilot.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Chewbacca&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Chewbacca&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;        ]
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    ],
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;      {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Star Wars Trash Compactor Scene&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    ]
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Much like for our heroes in the movie, from a software&#x27;s perspective, it is good that things are expanded. Any software can now precisely see the exact URI links, such as &lt;code&gt;http:&#x2F;&#x2F;schema.org&#x2F;Person&lt;&#x2F;code&gt;, without doing a lookup in the context and the prefixes each time. Of course for us humans it looks very verbose, but we can always compact it back, though hopefully after our heroes have escaped.&lt;&#x2F;p&gt;
&lt;p&gt;As bonus, JSON-LD also have a representation that is &lt;code&gt;flattened&lt;&#x2F;code&gt;.  This collects all entities into a single list and gives each one an explicit @id.
This is useful when you want everything neatly indexed and separated:&lt;&#x2F;p&gt;
&lt;p&gt;Flattened: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@context&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;schema.org&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:member&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;schema:birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@graph&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_:b0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Corellia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A charming smuggler and captain of the Millennium Falcon.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Han Solo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Han_Solo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_:b1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Polis Massa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A young Jedi learning the ways of the Force.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Luke Skywalker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Luke_Skywalker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_:b2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Polis Massa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A fearless princess and leader of the Rebel Alliance.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Leia Organa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Princess_Leia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_:b3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;birthPlace&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Kashyyyk&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A loyal Wookiee warrior and Han Solo&amp;#39;s co-pilot.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Chewbacca&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Chewbacca&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;example.org&#x2F;starwars-ld&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: [
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;          &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_:b0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        },
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;          &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_:b1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        },
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;          &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_:b2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        },
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;          &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;_:b3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;      ],
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Star Wars Trash Compactor Scene&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here each character gets their own node with an ID, and the relationships (&amp;quot;members of the scene&amp;quot;) are made by pointing to these IDs.&lt;&#x2F;p&gt;
&lt;p&gt;JSON-LD offers a versatile toolkit for compacting, expanding, or flattening data, enabling you to organize knowledge with clarity and precision for both humans and machines!
Just as the Star Wars heroes narrowly escaped the trash compactor, well-structured data can save your systems from collapsing under the weight of complexity.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Great AI&#x27;Tuin</title>
        <published>2025-04-12T00:00:00+00:00</published>
        <updated>2025-04-12T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/aituin/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/aituin/</id>
        
        <content type="html">&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Turtle_(syntax)&quot;&gt;Turtle language&lt;&#x2F;a&gt;, along with the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Semantic_Web&quot;&gt;Semantic Web&lt;&#x2F;a&gt;, offers a powerful way to represent and work with knowledge across virtually any domain. Unfortunately, it is often overlooked in practice due to several reasons: limited tooling, unfamiliarity with Semantic Web concepts, and the fact that much knowledge still exists in less structured formats, such as natural language. &lt;&#x2F;p&gt;
&lt;p&gt;However, in the age of large language models (LLMs) this does not have to be the case anymore. We finally have a way to bridge that gap between unstructured language and structured knowledge. Turtle in particular is very well suited for the structured knowledge representation. In this article I will demonstrate how LLMs can be used to translate natural language texts into the Turtle format and why this might be beneficial. To make this fun, I will use some examples derived from the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Discworld&quot;&gt;Discworld series of books&lt;&#x2F;a&gt; by &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Terry_Pratchett&quot;&gt;Terry Pratchett&lt;&#x2F;a&gt;. The Discworld books, which I highly recommend, are a series of fantasy novels often with satirical themes set in Discworld. Discworld is flat, resting upon the back of four elephants who in turn stand on a gigantic spacefaring turtle, named the Great A&#x27;Tuin. It alludes to the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;World_Turtle&quot;&gt;World Turtle&lt;&#x2F;a&gt; myth, which makes it a very apt topic to talk about when explaining the uses of the Turtle language.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=aituin.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A depiction of the Great A&amp;#x27;Tuin that carries the elephants that carry Discworld. Due to the limitations of image generation only three of the four elephants that carry Discworld are shown, the fourth is on a coffee break. 😃&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Before diving into examples, let’s briefly look at what the Turtle language actually is. Turtle (short for Terse RDF Triple Language) is a compact, readable format for expressing information as triples: subject–predicate–object statements. It’s part of the RDF (Resource Description Framework) family, designed for describing relationships between entities in a way that’s both machine readable and human friendly.&lt;&#x2F;p&gt;
&lt;p&gt;A triple might look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-turtle &quot;&gt;&lt;code class=&quot;language-turtle&quot; data-lang=&quot;turtle&quot;&gt;&lt;span&gt;:book1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;title &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Small Gods&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This reads as: book1 has the title &amp;quot;Small Gods&amp;quot;. Of course in most cases we would like to describe multiple facts that we might be interested in, such as the genre of the book, a textual summary of it, etc, for which we use multiple triples. These triples could then create a rich knowledge graph of information that enables both humans and software to understand and reason about a domain.&lt;&#x2F;p&gt;
&lt;p&gt;In particular Turtle excels at:&lt;&#x2F;p&gt;
&lt;p&gt;📌 Formalizing concepts (e.g., by description of the book we mean it having a title, a creator, etc)&lt;&#x2F;p&gt;
&lt;p&gt;🔗 Connecting related information (e.g., all books in a series)&lt;&#x2F;p&gt;
&lt;p&gt;🔍 Supporting precise structured queries (e.g., “find all books belonging to the crime genre”)&lt;&#x2F;p&gt;
&lt;p&gt;🧠 Enabling inference and integration with other data sources (e.g., use a commonly used definition of describing a book)&lt;&#x2F;p&gt;
&lt;p&gt;Now, let’s see how this works in practice with a Discworld based example. First we describe some information both with natural language and with Turtle and go over the differences. We start with the descriptions of three Discworld books in natural language, in this case generated by ChatGPT:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The Colour of Magic: &amp;quot;A bumbling wizard and a naive tourist embark on a chaotic journey across the Disc, in the book that kicks off the Discworld series.&amp;quot;&lt;&#x2F;li&gt;
&lt;li&gt;Guards! Guards!: &amp;quot;A ragtag city watch uncovers a dangerous conspiracy involving dragons and secret societies, all while stumbling toward reluctant heroism.&amp;quot;&lt;&#x2F;li&gt;
&lt;li&gt;Small Gods: &amp;quot;A forgotten god and his lone believer confront religious dogma, power, and philosophical questions in a deeply thoughtful standalone story.&amp;quot;
&lt;br&gt;&lt;br&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Given the information in this article about the Discworld series of books and these description we can represent this information with the Turtle language as follows (also generated by an LLM from the previous text):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-turtle &quot;&gt;&lt;code class=&quot;language-turtle&quot; data-lang=&quot;turtle&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;purl.org&#x2F;dc&#x2F;terms&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;example.org&#x2F;book&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;schema.org&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;colour_of_magic &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Book &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;title &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Colour of Magic&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;creator &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Terry Pratchett&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;isPartOf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;discworld_series &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;description &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A bumbling wizard and a naive tourist embark on a chaotic journey across the Disc, in the book that kicks off the Discworld series&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;genre &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fantasy&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;parody&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;adventure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;guards_guards &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Book &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;title &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Guards! Guards!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;creator &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Terry Pratchett&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;isPartOf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;discworld_series &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;description &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A ragtag city watch uncovers a dangerous conspiracy involving dragons and secret societies, all while stumbling toward reluctant heroism&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;genre &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;satire&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;crime&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dragons&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;power structures&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;small_gods &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Book &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;title &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Small Gods&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;creator &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Terry Pratchett&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;isPartOf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;discworld_series &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;description &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A forgotten god and his lone believer confront religious dogma, power, and philosophical questions in a deeply thoughtful standalone story&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;genre &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;faith&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;philosophy&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;belief&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;satire&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;discworld_series &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BookSeries &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;title &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Discworld&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;creator &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Terry Pratchett&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now lets go over the elements of the Turtle representation here:&lt;&#x2F;p&gt;
&lt;p&gt;As mentioned before, each line in Turtle expresses a triple: a simple fact linking a subject to an object via a predicate (or property). For instance:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-turtle &quot;&gt;&lt;code class=&quot;language-turtle&quot; data-lang=&quot;turtle&quot;&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;colour_of_magic &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Book &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This says that ex:colour_of_magic (our identifier for the book) is a schema:Book: a class from the Schema.org vocabulary. The &lt;code&gt;a&lt;&#x2F;code&gt; is shorthand for &lt;code&gt;rdf:type&lt;&#x2F;code&gt;, indicating class membership. This is a common pattern in RDF and one of the ways Turtle stays concise and readable. The Turtle syntax also has the nice feature is that if we would have a series of triples such as: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-turtle &quot;&gt;&lt;code class=&quot;language-turtle&quot; data-lang=&quot;turtle&quot;&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;discworld_series &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BookSeries &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;discworld_series &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;title &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Discworld&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;discworld_series &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;creator &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Terry Pratchett&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;that have a common subject, we do not have to repeat ourselves and can use the shorthand:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-turtle &quot;&gt;&lt;code class=&quot;language-turtle&quot; data-lang=&quot;turtle&quot;&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;discworld_series &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;BookSeries &lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;title &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Discworld&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;creator &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Terry Pratchett&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The identifiers that make use of the prefixes are actually full URIs, such as &lt;code&gt;http:&#x2F;&#x2F;example.org&#x2F;book&#x2F;colour_of_magic&lt;&#x2F;code&gt;. In Turtle you can use prefixes to keep things short and tidy. The prefixes, and what they stand for, are given at the start of the Turtle definition:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-turtle &quot;&gt;&lt;code class=&quot;language-turtle&quot; data-lang=&quot;turtle&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;dct: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;purl.org&#x2F;dc&#x2F;terms&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ex: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;example.org&#x2F;book&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;schema: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;schema.org&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here the &lt;code&gt;ex&lt;&#x2F;code&gt; prefix is something we devised for this example, but the other prefixes refer to vocabularies that already exist that we could use: &lt;a href=&quot;https:&#x2F;&#x2F;www.dublincore.org&#x2F;specifications&#x2F;dublin-core&#x2F;dcmi-terms&#x2F;&quot;&gt;Dublin Core&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;schema.org&#x2F;&quot;&gt;Schema.org&lt;&#x2F;a&gt;. This allows us to not reinvent the wheel and make use of the fact that there already exist vocabularies for describing books, creators of things, etc. This is a big benefit for interoperability that you normally do not get out of the box with many other formalisms (such as JSON). Any tool already existing that uses these common vocabularies would be compatible with our small RDF description using these prefixes.&lt;&#x2F;p&gt;
&lt;p&gt;Speaking of tooling, as mentioned previously, the set of triples creates a knowledge graph of interconnected information within a domain. We can also visualize this as a graph, for example using the &lt;a href=&quot;https:&#x2F;&#x2F;issemantic.net&#x2F;rdf-visualizer&quot;&gt;isSemantic RDF Visualizer&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=discworldgraph.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A graph visualization of the Turtle description of the Discworld books.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The key aspect that Turtle enables, especially in a world of LLMs, is allows us to be very precise and explicit with the semantics that we are describing. For example if we would just say we have a description of the Discworld books, it could be very unclear exactly what is included: the description, author, date of publishing, genres, etc. If we instead have a Turtle representation of the information, even if generated initially by an LLM, both humans as well as software can precisely check what information we describe and what we do not.&lt;&#x2F;p&gt;
&lt;p&gt;This explicitness is what allows us to do very precise queries on the semantics. Using &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;SPARQL&quot;&gt;SPARQL&lt;&#x2F;a&gt;, a query language for these type of knowledge graphs, we can find the book that has crime as part of its genre definition:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;PREFIX schema: &amp;lt;http:&#x2F;&#x2F;schema.org&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;PREFIX dct: &amp;lt;http:&#x2F;&#x2F;purl.org&#x2F;dc&#x2F;terms&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;SELECT ?bookTitle WHERE {
&lt;&#x2F;span&gt;&lt;span&gt;  ?book schema:genre &amp;quot;crime&amp;quot; ;
&lt;&#x2F;span&gt;&lt;span&gt;        dct:title ?bookTitle .
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This query would return the following when run on our knowledge graph: &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;&amp;quot;Guards! Guards!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Such queries would work even if we have lot more of these book definitions, without the potential for hallucinations or inaccuracies that sometimes occur when using LLMs and natural language.&lt;&#x2F;p&gt;
&lt;p&gt;As one can see, the Semantic Web and Turtle format offer powerful ways to structure and connect information. The main drawback used to be that knowledge is often represented in natural language, or other less accessible formats. This made the benefits of such knowledge graphs hard to realize. &lt;&#x2F;p&gt;
&lt;p&gt;Now, large language models can act as bridges: translating rich, human-authored natural language into structured formats like Turtle. This opens up new opportunities for building precise, interoperable knowledge graphs from books, documentation, or even casual writing. It allows humans or even software systems, to make very precise corrections or queries on this graph, without fear of inaccuracies.&lt;&#x2F;p&gt;
&lt;p&gt;If you ever have the chance, give this approach using Turtle a go. It can feel quite magical!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Into the Matrix</title>
        <published>2025-03-23T00:00:00+00:00</published>
        <updated>2025-03-23T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/intothematrix/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/intothematrix/</id>
        
        <content type="html">&lt;p&gt;Image generation using AI that can run locally on a desktop or even a laptop has seen a tremendous leap in the last few years. When I &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;dunes&#x2F;&quot;&gt;first wrote about it&lt;&#x2F;a&gt; after &lt;a href=&quot;https:&#x2F;&#x2F;stability.ai&#x2F;news&#x2F;stable-diffusion-public-release&quot;&gt;the public release of Stable Diffusion&lt;&#x2F;a&gt;, getting everything installed was quite an involved process, involving configuring the right dependencies and terminal commands. This was especially true for those who wanted to experiment with different models (the systems that enable the generation of images from prompts) and front-ends (the user interfaces with which one can interact with the models and its results) that have since been released. &lt;&#x2F;p&gt;
&lt;p&gt;Thankfully there are now tools, such as &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;LykosAI&#x2F;StabilityMatrix&quot;&gt;Stability Matrix&lt;&#x2F;a&gt;, that make the process of installing and using multiple models and front-ends very straightforward. In this article, I will walk through using Stability matrix to install a new frontend and a model to generate some images of characters as they would exist in a cyberpunk matrix. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=jax.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A Cyberpunk protagonist generated using Stability Matrix.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The matrix is a staple concept in the Cyberpunk genre. It refers to a large immersive virtual reality network that users can access, most often through direct neural interfaces. In this virtual reality, data, knowledge, virtual avatars and AI can exist as tangible entities. The concept has been popularized in the book Neuromancer by William Gibson. Since then, it has been a staple in the Cyberpunk genre, appearing in the Matrix movies, the Shadowrun universe, Cyberpunk 2077 and more. In general, the concept is used to explore the effects of increasing digitization on society, human identity, and the implications of AI existing entirely within a virtual world.&lt;&#x2F;p&gt;
&lt;p&gt;To start, we first need to install Stability Matrix, which can be downloaded from its &lt;a href=&quot;https:&#x2F;&#x2F;lykos.ai&#x2F;&quot;&gt;website&lt;&#x2F;a&gt; or from its &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;LykosAI&#x2F;StabilityMatrix&#x2F;releases&quot;&gt;GitHub releases&lt;&#x2F;a&gt; page. The installation process is pretty straightforward and I highly recommend opting for a portable install. This keeps all data, such as models, within a single directory, making it much easier to transfer everything to a different drive or computer in the future.&lt;&#x2F;p&gt;
&lt;p&gt;Once the application is installed, the next step is to set up a UI for generating images from prompts. There are multiple front-ends available from the packages menu. For this article, we will use Stable Diffusion WebUI Forge, as it is easy to use and more than sufficient for our very basic image generation use case.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=packages.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The packages for the front-end listed that could be installed from Stability Matrix.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;We also need a model to generate images. For this we will use the Model Browser to find the right model for our use case. After searching for a model that is fine-tuned to generate cyberpunk imagery, I have settled on using the &lt;code&gt;Splatter Punk - Neon Waves&lt;&#x2F;code&gt; model. Installing it is just a click away.&lt;&#x2F;p&gt;
&lt;!-- &lt;figure class=centeredfig&gt;
    &lt;img src=models.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A sample of the models one could install&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
 --&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=modelneon.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The &amp;#x27;Splatter Punk - Neon Waves&amp;#x27; model used to generate the images.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Now, with both a model and a front-end installed, we can start generating the images that we need. We want to generate pictures for four characters in a Cyberpunk themed world, based on four archetypes common in stories: the Hero, the Friend of the Hero, a Minion&#x2F;Underling, and the Antagonist. While we could manually craft and fine tune the prompts, ChatGPT does a decent job of generating them for these archetypes:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=jax.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Jax `Glitch` Varen (The Hero)&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;&amp;quot;A brooding, determined man standing in a neon-lit alley of a futuristic city, wearing a sleek black jacket with subtle glowing circuitry patterns. His eyes reflect a mix of sadness and determination, and a faint holographic interface hovers around his wrist. The background features towering skyscrapers with digital ads, and rain softly blurs the neon glow. The atmosphere should feel tense and introspective, symbolizing betrayal and a relentless quest for justice.&amp;quot;&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=patch.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Nova `Patch` Rhee (The Friend)&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;&amp;quot;A cheerful and energetic woman in her late twenties, standing in a cluttered workshop filled with glowing wires, cybernetic components, and floating holograms. She wears mismatched tech goggles on her forehead, with brightly colored clothes accented by glowing accessories. A small, hovering drone buzzes beside her, reflecting her inventive and playful nature. The setting should be cozy yet chaotic, highlighting her technical genius and hidden vulnerabilities.&amp;quot;&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=cipher.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Cipher (The Minion&amp;#x2F;Underling)&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;&amp;quot;A shadowy, humanoid figure made of fragmented code and shifting digital glitches, with piercing, glowing eyes. The body appears semi-transparent, constantly morphing between solid and pixelated forms. The background is a cold, digital void filled with streams of raw data flowing like rivers of light. The mood should be ominous and relentless, emphasizing Cipher’s role as an adaptive enforcer bound by hidden desires for autonomy.&amp;quot;&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=elara.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Dr. Elara Voss (The Antagonist)&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;&amp;quot;A powerful, composed woman standing at the center of a vast control room filled with holographic screens and pulsing data streams. She wears a high-collar, futuristic coat with minimalist design elements, her silver hair tied back in a severe style. Her expression is cold and calculating, with a subtle smirk of superiority. Behind her, the Matrix’s digital skyline stretches endlessly, symbolizing her control over the virtual world and her ambition for perfection.&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;The images above were generated using the default parameters with the listed prompts. In fact, the portraits of Jax and Nova were created on the first attempt, while the others were selected from multiple generated images. For this particular model, the positive prompts were mostly sufficient, however adding a negative prompt, such as one to filter out NSFW content, can be useful.&lt;&#x2F;p&gt;
&lt;p&gt;It has been incredible to see how much better the tooling and the models have become since these locally installable image generation models first were released. In the early days setups usually involved installing and managing many dependencies from the commandline, which is no longer required. Whether for storytelling, concept art, or creative exploration, AI-generated imagery even from a desktop can be just one install away.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Livebook of Oa</title>
        <published>2024-10-22T00:00:00+00:00</published>
        <updated>2024-10-22T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/livebookofoa/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/livebookofoa/</id>
        
        <content type="html">&lt;p&gt;Code notebooks, which can provide an interactive combination of code, data, AI, text and visualizations, have seen increasing use in recent years. &lt;a href=&quot;https:&#x2F;&#x2F;livebook.dev&#x2F;&quot;&gt;Livebook&lt;&#x2F;a&gt; is such a notebook environment that has some interesting features to explore. In this article I will write a basic Livebook notebook to show off some these features. This notebook will describe information from the Green Lantern series of comic books. In the Green Lantern series there exists a book called the &lt;code&gt;Book of Oa&lt;&#x2F;code&gt; that contains knowledge about Green Lanterns and it can convey this in a variety of forms. Inspired by this, the notebook in this article will show information about Green Lanterns in a variety of ways: text, code, visualizations and even with some AI applications. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The Book of Oa from the Green Lantern series of comic books. Copyright DC Comics.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;If one wants to follow along with the full source of the notebook presented here it can be found in this &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;livebook-of-oa&quot;&gt;repo on GitHub&lt;&#x2F;a&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;In order to run Livebook one needs to &lt;a href=&quot;https:&#x2F;&#x2F;livebook.dev&#x2F;#install&quot;&gt;install&lt;&#x2F;a&gt; the Livebook app, either locally or in the cloud. The application is itself a web application that the user, or even multiple users, can connect to. Within the app one can create a new notebook, or open an existing one. &lt;&#x2F;p&gt;
&lt;p&gt;In a new notebook the first step is usually installing some dependencies for the specific visualizations and AI tasks that we aim to do. Many of these can be added &#x27;on demand&#x27; when we want to create them during the development, but here we just load in all the dependencies we need for the example notebook in one go:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Mix&lt;&#x2F;span&gt;&lt;span&gt;.install([
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:kino_maplibre&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;~&amp;gt; 0.1.12&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:kino_vega_lite&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;~&amp;gt; 0.1.11&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:kino_bumblebee&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;~&amp;gt; 0.5.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:exla&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;~&amp;gt; 0.7.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;config: &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nx: &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;default_backend: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;EXLA&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Backend&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;default_defn_options: &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;compiler: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;EXLA&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    ]
&lt;&#x2F;span&gt;&lt;span&gt;  ],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;system_env: &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;XLA_TARGET: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cpu&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In Livebook the main language for code blocks is &lt;a href=&quot;https:&#x2F;&#x2F;elixir-lang.org&#x2F;&quot;&gt;Elixir&lt;&#x2F;a&gt;, a functional programming language in which Livebook itself was written. These code blocks can be evaluated, which in this case results in  the atom&lt;code&gt;:ok&lt;&#x2F;code&gt; being returned, denoting that this setup went well. Atoms in Elixir are constants whose value is their name. They are often used to note the state of an operation, in this case &lt;code&gt;:ok&lt;&#x2F;code&gt; showing success, or distinct values, an example for which we will see later.&lt;&#x2F;p&gt;
&lt;p&gt;Aside from code blocks, another type of element we can add to these notebooks is text. For example a brief part of the introduction in the notebook contains the following text using Markdown:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;quot;Green Lanterns are superheroes that are part of an intergalactic law enforcement agency called the Green Lantern Corps. They derive their powers through their Power Rings, which they aim to control through their willpower. The Power Rings give the Green Lanters various powers such as flight, creating forcefields and contructs made for energy.
The headquarters of the Green Lanterns are on the planet Oa, which is the home planet of the Guardians of the Universe. On the planet there is the Main Power Battery that powers all rings, as well as the Book of Oa (from which the name of this article is derived) which contains the laws and history of the Green Lantern Corps.
Aside from Markdown, Livebooks can have many other types of content. For example we can create a diagram for the First Appearance of a number of Green Lanterns using the built in support for Mermaid.js.&amp;quot;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;As mentioned previously, there are many other options for different types of content. For example creating diagrams using &lt;a href=&quot;https:&#x2F;&#x2F;mermaid.js.org&#x2F;&quot;&gt;Mermaid.js&lt;&#x2F;a&gt; can be a breeze. The following block creates a diagram of the timelines of the first appearances of some of the Green Lanterns:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;mermaid&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-mermaid &quot;&gt;&lt;code class=&quot;language-mermaid&quot; data-lang=&quot;mermaid&quot;&gt;&lt;span&gt;timeline
&lt;&#x2F;span&gt;&lt;span&gt;    title First Appearance
&lt;&#x2F;span&gt;&lt;span&gt;    1959: Hal Jordan
&lt;&#x2F;span&gt;&lt;span&gt;    1968: Guy Gardner
&lt;&#x2F;span&gt;&lt;span&gt;    1972: John Stewart
&lt;&#x2F;span&gt;&lt;span&gt;    1994: Kyle Rayner
&lt;&#x2F;span&gt;&lt;span&gt;    2012: Simon Baz
&lt;&#x2F;span&gt;&lt;span&gt;    2014: Jessica Cruz
&lt;&#x2F;span&gt;&lt;span&gt;    2020: Sojurner Mullein
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=first_appearance.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A Mermaid.js timeline of the first appearance of Green Lanterns in the comic books that is created with the above code.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Code blocks can do more than just set up our notebook. For example we can define a struct to describe data relating to a Lantern. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defmodule &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defstruct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;color: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:green&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;home_town: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Without diving deep into Elixir semantics, the Lantern struct holds data about the name, color, sector, home town and appearances of a Lantern, with some basic default values. Notably the default value for the color of the Lanterns is green, denoted by the atom &lt;code&gt;:green&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;With these structs we can specify the data of six Green Lanterns from earth, and put them in a list named &lt;code&gt;green_lanterns_from_earth&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;hal_jordan =  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hal Jordan&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 2814&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;home_town: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Coast City, California&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 5396  &lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;guy_gardner =  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Guy Gardner&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 2814&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;home_town: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Baltimore&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 1631 &lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;john_stewart=  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;John Stewart&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 2814&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;home_town: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Detroit, Michigan&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 1866&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;kyle_rayner =  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Kyle Rayner&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 2814&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;home_town: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Los Angeles, California&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 1696&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;simon_baz =  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Simon Baz&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 2814&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;home_town: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dearborn, Michigan&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 443&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;jessica_cruz=  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Jessica Cruz&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 2814&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;home_town: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Portland, Oregon&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 431&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;sojourner_mullein =  %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sojourner Mullein&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 2814&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;home_town: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;New York City, New York&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 72&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;green_lanterns_from_earth = [hal_jordan, guy_gardner, john_stewart, kyle_rayner, simon_baz, jessica_cruz, sojourner_mullein]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As these Green Lanterns all have a hometown on Earth we can also specify the coordinates of these locations.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;hometown_coordinates = %{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;coordinates&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; =&amp;gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;37.865894, -122.498055&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;39.299236, -76.609383&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;42.331429, -83.045753&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;34.052235, -118.243683&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;42.322262, -83.176315&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;45.523064, -122.676483&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;40.730610, -73.935242&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;],
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; =&amp;gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Coast City&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Baltimore&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Detroit&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Los Angeles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dearborn&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Portland&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;New York City&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above coordinates can be used to create a visualization of a map marked with the hometowns of the Lanterns. Although this map could be created programmatically, Livebook has the notion of &lt;code&gt;smart cells&lt;&#x2F;code&gt; with which UI components can be rapidly created, without reaching for code. The following map was created using this feature.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=hometown_map.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A map of the hometowns of the Green Lanterns.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;In addition to Green Lanterns there are also Lanterns of other colors in the comics as well. Thankfully our &lt;code&gt;Lantern&lt;&#x2F;code&gt; struct also allows us to specify these:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;atrocitus = %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Atrocitus&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;color: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:red&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 666&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 321&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;bleez = %&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Lantern&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bleez&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;color: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:red&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;sector: 33&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: 198&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;red_lanterns = [atrocitus, bleez]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can combine the two lists of Lanterns into one with some Elixir code.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;lanterns = green_lanterns_from_earth ++ red_lanterns
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we have all these lanterns defined, we can create a visualization that lists all lanterns with their respective appearances. The first step towards this to create a data structure combining together the lanterns names, number of appearances and color. The follow code creates this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;lantern_appearances = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Enum&lt;&#x2F;span&gt;&lt;span&gt;.reduce(lanterns, %{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;lanterns: &lt;&#x2F;span&gt;&lt;span&gt;[], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: &lt;&#x2F;span&gt;&lt;span&gt;[], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;color: &lt;&#x2F;span&gt;&lt;span&gt;[]}, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span&gt; lantern, acc -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  %{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;lanterns:&lt;&#x2F;span&gt;&lt;span&gt; acc.lanterns ++ [lantern.name],
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances:&lt;&#x2F;span&gt;&lt;span&gt; acc.appearances ++ [lantern.appearances],
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;color:&lt;&#x2F;span&gt;&lt;span&gt; acc.color ++ [lantern.color]
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which results in the following data when evaluated:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span&gt;%{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;color: &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:green&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:green&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:green&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:green&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:green&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:green&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:green&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:red&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:red&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;appearances: &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5396&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1631&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1866&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1696&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;443&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;431&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;72&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;321&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;198&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;lanterns: &lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hal Jordan&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Guy Gardner&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;John Stewart&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Kyle Rayner&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Simon Baz&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;   &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Jessica Cruz&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sojourner Mullein&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Atrocitus&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bleez&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This data can be used to create our visualization. Instead of using a smart cell, here we programmatically create the chart with the following code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;elixir&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-elixir &quot;&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;VegaLite&lt;&#x2F;span&gt;&lt;span&gt;.new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;title: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Lantern Appearances&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;VegaLite&lt;&#x2F;span&gt;&lt;span&gt;.data_from_values(lantern_appearances, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;only: &lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;lanterns&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;appearances&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;color&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;VegaLite&lt;&#x2F;span&gt;&lt;span&gt;.mark(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:bar&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;VegaLite&lt;&#x2F;span&gt;&lt;span&gt;.encode_field(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:x&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;lanterns&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:nominal&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;VegaLite&lt;&#x2F;span&gt;&lt;span&gt;.encode_field(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:y&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;appearances&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:quantitative&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;VegaLite&lt;&#x2F;span&gt;&lt;span&gt;.encode_field(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:color&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;color&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:nominal&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;title: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Lantern Color&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;scale: &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;domain: &lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;green&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;red&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;],
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;range: &lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;green&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;red&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;  ])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;!-- While the code looks more complex than what we did previously, it is often helpful to start off with a `smart cell` which we can turn into code with a press of a button and then modify it to easily get to the visualization that we want.  --&gt;
&lt;p&gt;Evaluation of the above the code results in the following bar chart:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=lantern_appearances.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A bar chart of the number of lantern appearances in comic books.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Finally, we can also add some AI functionality to round out our notebook. For example using a smart cell we can add a question answering system with only a few clicks:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=question_answering.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A question answering system that can answer who Green Lanterns are based on our textual description.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Even AI based image generation can be embedded with smart cells. We can use this to generate an image of a Green Lantern power ring.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=generating_ring.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;An image of a Green Lantern power ring that has been generated.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
 
&lt;p&gt;Of course this is just a very short glimpse of the things that a Livebook based notebook can do. One feature that is very useful in practice is that Livebook notebooks are saved as &lt;code&gt;livemd&lt;&#x2F;code&gt; files which are a subset of markdown files. This makes them very suitable choice for writing documentation (such as for Github which renders &lt;code&gt;livemd&lt;&#x2F;code&gt; files are markdown) and are easily used with version control. Another feature we did not touch on here is using real-time collaboration on the notebooks when using Livebook.&lt;&#x2F;p&gt;
&lt;p&gt;Hopefully with this article I helped to shine some light on the nice features of the Livebook environment. Feel free to take a look on &lt;a href=&quot;https:&#x2F;&#x2F;livebook.dev&#x2F;&quot;&gt;Livebook&#x27;s website&lt;&#x2F;a&gt; for more details on using it.  &lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Fellowship of Prolog</title>
        <published>2024-06-25T00:00:00+00:00</published>
        <updated>2024-06-25T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/fellowshipofprolog/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/fellowshipofprolog/</id>
        
        <content type="html">&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Logic_programming&quot;&gt;Logic programming&lt;&#x2F;a&gt; with &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Prolog&quot;&gt;Prolog&lt;&#x2F;a&gt; can be a very effective way to find solutions to a number of problems. Within Prolog one can define a program by describing it with facts (that represent what we know), rules (that show how to derive new knowledge) and queries (that describe what we would like to know). By using queries on a knowledge base of facts and rules we can solve a variety of problems in many domains.&lt;&#x2F;p&gt;
&lt;p&gt;This approach is quite powerful, but unfortunately Prolog as a programming language is often overlooked. One of the reasons for this is that it can be quite difficult for people without (logic) programming knowledge to break down problems into such terms. Even with experience in Prolog this process can be quite time consuming.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Large_language_model&quot;&gt;Large Language Models (LLMs)&lt;&#x2F;a&gt;, such as &lt;a href=&quot;https:&#x2F;&#x2F;openai.com&#x2F;chatgpt&#x2F;&quot;&gt;Chat-GPT&lt;&#x2F;a&gt;, can provide an interesting way to tackle this issue. In this article I will aim to show how Chat-GPT (or another LLM) could help create and use Prolog programs to solve problems. For this I will be using &amp;quot;Lord of the Rings&amp;quot; themed logic puzzles.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Characters from the Fellowship of the Ring walking in a single line. This is the basic setup for the logic puzzles used in this article.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Let&#x27;s say we want to describe a situation in which the members of the Fellowship of the Ring are walking down a narrow path. The path is so narrow that no one can walk side by side. Each Fellowship member can only walk in a single line, one after another. The puzzles are based on the idea that given a number of conditions we would like to know in which order the members of the Fellowship are walking.&lt;&#x2F;p&gt;
&lt;p&gt;First let&#x27;s start off with a small number of conditions to show off the approach. We assume that only three members of the Fellowship are walking: Legolas, Gimli and Frodo. Our restrictions are that Legolas has to walk before Gimli and Gimli must be next to Frodo (this is the reverse order of what it can be seen in the above figure).&lt;&#x2F;p&gt;
&lt;!-- The Fellowship at this point consists of Aragorn, Boromir, Legolas, Gimli, Merry, Pippin, Frodo, Sam and Gandalf. --&gt;
&lt;!-- Aragorn is walking with at least 4 others behind him.
Merry wants to show his bravery to Pippin, so he walks directly in front of him.
Gandalf walks ahead of Aragorn.
Boromir wants to be guided by the ranger, Aragorn, so he walks somewhere behind him.
Boromor also wants to keep an eye out on the ring bearer, Frodo, so he walks directly behind him.
Sam is walking with next to at least another Hobbit (Frodo, Merry or Pippin).
Gimli wants to make sure Legolas does not outshine him, so he walks ahead of him.
Legolas only has at most two others behind him. --&gt;
&lt;p&gt;In order to model this puzzle with Prolog, first we need to define the notion of the &amp;quot;single line order&amp;quot; that the Fellowship is walking in. In addition we need to define what terms such as &lt;code&gt;before&lt;&#x2F;code&gt;, &lt;code&gt;ahead&lt;&#x2F;code&gt;, etc mean within the context of this puzzle in Prolog. For one well versed in Prolog this can be a pretty routine task but in this case let&#x27;s assume little to no Prolog knowledge for the person that aims to solve this puzzle. Instead we can ask Chat-GPT to come up with a translation of this problem to Prolog code. &lt;&#x2F;p&gt;
&lt;p&gt;The Prolog code that we use in this article could likely be run with any number of Prolog implementations, such as &lt;a href=&quot;https:&#x2F;&#x2F;www.swi-prolog.org&#x2F;&quot;&gt;SWI Prolog&lt;&#x2F;a&gt;. For this article we generate code to use with &lt;a href=&quot;http:&#x2F;&#x2F;tau-prolog.org&#x2F;&quot;&gt;Tau prolog&lt;&#x2F;a&gt;. The benefit of using Tau prolog is that it can run in the browser and it has an &lt;a href=&quot;http:&#x2F;&#x2F;tau-prolog.org&#x2F;sandbox&#x2F;&quot;&gt;online sandbox&lt;&#x2F;a&gt; available where code can be run without any installation. If one wants to follow along without any setup I highly recommend using this sandbox.&lt;&#x2F;p&gt;
&lt;!-- If one wants to follow along, while any Prolog implementation would be suitable (such as SWI Prolog), I recommend Tau Prolog as it has an online sandbox available where once could code without any installation.  --&gt;
&lt;p&gt;To create the Prolog code for the simplified version of the puzzle we ask Chat-GPT the following question: &lt;&#x2F;p&gt;
&lt;p&gt;&amp;quot;Can you write an example with tau prolog of a logic puzzle in which 3 members of the fellowship need to walk in a single line. Legolas has to be before Gimli and Gimli must be next to Frodo?&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;to which Chat-GPT gives us the code: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;% Load necessary library
&lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;use_module&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;library&lt;&#x2F;span&gt;&lt;span&gt;(lists)).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Define the predicate to find the order
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fellowship_order&lt;&#x2F;span&gt;&lt;span&gt;(Order) :-
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Order &lt;&#x2F;span&gt;&lt;span&gt;= [_, _, _],  % There are three positions to fill.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(legolas, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(gimli, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(frodo, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(legolas, gimli, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(gimli, frodo, Order).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Helper predicate to check &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;X is before Y in the List
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, List) :-
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nth0&lt;&#x2F;span&gt;&lt;span&gt;(IndexX, List, X),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nth0&lt;&#x2F;span&gt;&lt;span&gt;(IndexY, List, Y),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;IndexX &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; IndexY.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Helper predicate to check &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;X is next to Y in the List
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, List) :-
&lt;&#x2F;span&gt;&lt;span&gt;    (   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nth0&lt;&#x2F;span&gt;&lt;span&gt;(IndexX, List, X),
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nth0&lt;&#x2F;span&gt;&lt;span&gt;(IndexY, List, Y),
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;abs&lt;&#x2F;span&gt;&lt;span&gt;(IndexX - IndexY) =:= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;    ).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Example query to find the order
&lt;&#x2F;span&gt;&lt;span&gt;% ?- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fellowship_order&lt;&#x2F;span&gt;&lt;span&gt;(Order).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;!-- Prolog code can be run with any number of Prolog implementations, (such as SWI Prolog) --&gt;
&lt;p&gt;From this small example we can see the general outline of how such a logic puzzle can be solved with Prolog. Although we are not going to describe in detail the full syntax and semantics a Prolog program, as there are some excellent &lt;a href=&quot;https:&#x2F;&#x2F;lpn.swi-prolog.org&#x2F;lpnpage.php?pageid=online&quot;&gt;introductions&lt;&#x2F;a&gt; on this subject, we are going to briefly go over some concepts.&lt;&#x2F;p&gt;
&lt;p&gt;A Prolog program is a knowledge base in which predicates, such as facts and rules, describe the domain. In our case the knowledge base defines this puzzle based on Lord of the Rings with three predicates: &lt;code&gt;before&lt;&#x2F;code&gt;, &lt;code&gt;next_to&lt;&#x2F;code&gt; and  &lt;code&gt;fellowship_order&lt;&#x2F;code&gt;. The &lt;code&gt;before&lt;&#x2F;code&gt; and &lt;code&gt;next_to&lt;&#x2F;code&gt; predicates express whether for two members in a list are before or next to each others respectively. The &lt;code&gt;X&lt;&#x2F;code&gt;, &lt;code&gt;Y&lt;&#x2F;code&gt; and &lt;code&gt;List&lt;&#x2F;code&gt; arguments expect variables (variable names are capitalized). The &lt;code&gt;fellowship_order&lt;&#x2F;code&gt; uses these predicates to express what is a valid, according to our logic puzzle, order of the members of the Fellowship. The members, such as &lt;code&gt;legolas&lt;&#x2F;code&gt; are atoms (they start with a lower case) and represent these characters in our domain. &lt;&#x2F;p&gt;
&lt;p&gt;The query, &lt;code&gt;fellowship_order(Order).&lt;&#x2F;code&gt; which was helpfully given to us by Chat-GPT, allows us to gather the valid orders according to the puzzle when querying the above knowledge base. &lt;&#x2F;p&gt;
&lt;p&gt;We can test this easily by entering the above Prolog code in the Tau Prolog sandbox. After pasting in the program, we can press &lt;code&gt;consult program&lt;&#x2F;code&gt;, which loads the knowledge base. As the goal that we want to query, we can enter &lt;code&gt;fellowship_order(Order).&lt;&#x2F;code&gt;. Doing this will retrieve &lt;code&gt;Order = [legolas,gimli,frodo]&lt;&#x2F;code&gt;, which is a valid answer to the logic puzzle.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=lotrprologsandbox.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Consulting the short Prolog example of the logic puzzle.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Note that &lt;code&gt;Order = [legolas,frodo,gimli]&lt;&#x2F;code&gt; is a valid answer as well. If we would ask the query again in the Tau Prolog sandbox, we will get this answer instead. If we query for a third (or any subsequent) time, we get a &lt;code&gt;false&lt;&#x2F;code&gt;, which is Prolog&#x27;s way of denoting that no answer could be found. This is due to Tau Prolog aiming to list all possible answers one by one given our query in this sandbox.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have gone over a small example, let&#x27;s see if this approach can work with a larger puzzle. While we can ask for the full Prolog code for a larger puzzle, in the version of Chat-GPT used at the time of writing, it had some trouble giving us a full and complete working program with lots of restrictions. Instead we can take the small example and add new constraints to it.&lt;&#x2F;p&gt;
&lt;p&gt;Using this method we can create the following puzzle:&lt;&#x2F;p&gt;
&lt;!-- Aragorn is walking with at least 4 others behind him.
Merry wants to show his bravery to Pippin, so he walks directly in front of him.
Gandalf walks ahead of Aragorn.
Boromir wants to be guided by the ranger, Aragorn, so he walks somewhere behind him.
Boromor also wants to keep an eye out on the ring bearer, Frodo, so he walks directly behind him.
Sam is walking with next to at least another Hobbit (Frodo, Merry or Pippin).
Gimli wants to make sure Legolas does not outshine him, so he walks ahead of him.
Legolas only has at most two others behind him.

If we ask Chat-GPT to give an expanded version of this logic puzzle we end up with the following:

```prolog
% Load necessary library
:- use_module(library(lists)).

% Define the fellowship list and ensure that Aragorn has at least four others behind him
fellowship_order(Order) :-
    Order = [gandalf, _, aragorn, _, _, _, _, _],  % Gandalf walks ahead of Aragorn; Aragorn has at least four behind
    member(boromir, Order),
    member(frodo, Order),
    member(sam, Order),
    member(merry, Order),
    member(pippin, Order),
    member(gimli, Order),
    member(legolas, Order),
    length(Order, 8),  % Total members walking

    % Defining relations based on problem statement
    next_to(merry, pippin, Order),  % Merry walks directly in front of Pippin
    merry_before_pippin(merry, pippin, Order),
    before(gandalf, aragorn, Order),
    after(boromir, aragorn, Order),  % Boromir walks somewhere behind Aragorn
    directly_behind(boromir, frodo, Order),  % Boromir directly behind Frodo
    hobbit_neighbor(sam, Order),  % Sam walks next to at least another Hobbit
    before(gimli, legolas, Order),  % Gimli walks ahead of Legolas
    at_most_two_behind(legolas, Order).  % Legolas has at most two behind him

% Check if X is directly before Y
directly_before(X, Y, [X, Y|_]).
directly_before(X, Y, [_|T]) :-
    directly_before(X, Y, T).

% Check if X is directly behind Y
directly_behind(X, Y, List) :-
    directly_before(Y, X, List).

% Check if X is before Y
before(X, Y, [X|T]) :-
    member(Y, T).
before(X, Y, [_|T]) :-
    before(X, Y, T).

% Check if X is after Y
after(X, Y, List) :-
    before(Y, X, List).

% Merry is directly before Pippin
merry_before_pippin(merry, pippin, [merry, pippin|_]).
merry_before_pippin(merry, pippin, [_|T]) :-
    merry_before_pippin(merry, pippin, T).

% Ensure Sam is next to another hobbit
hobbit_neighbor(sam, List) :-
    next_to(sam, frodo, List);
    next_to(sam, merry, List);
    next_to(sam, pippin, List).

% Check if X is next to Y
next_to(X, Y, [X, Y|_]).
next_to(X, Y, [Y, X|_]).
next_to(X, Y, [_|T]) :-
    next_to(X, Y, T).

% Check if a character has at most two others behind them
at_most_two_behind(X, List) :-
    reverse(List, RevList),
    nth0(Index, RevList, X),
    Index &gt;= 2.

% Example query to find the order
% ?- fellowship_order(Order).
```
 --&gt;
&lt;!-- We can see this structure in the small example above. After loading in the necessary library for lists, it defines the rules for `before`, `next_to` and what it means to be a valid `fellowship_order`. The query, which it helpfully gives as a comment: `% ?- fellowship_order(Order).` can   --&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;% Load necessary library
&lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;use_module&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;library&lt;&#x2F;span&gt;&lt;span&gt;(lists)).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fellowship_order&lt;&#x2F;span&gt;&lt;span&gt;(Order) :-
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Order &lt;&#x2F;span&gt;&lt;span&gt;= [ _, _, _, _, _, _, _], 
&lt;&#x2F;span&gt;&lt;span&gt;  	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(aragorn, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(frodo, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(sam, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(merry, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(pippin, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(gimli, Order),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(legolas, Order),
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    % Defining relations based &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;on&lt;&#x2F;span&gt;&lt;span&gt; problem statement
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(aragorn, pippin, Order), % Aragorn walks before Pippin.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hobbit_neighbor&lt;&#x2F;span&gt;&lt;span&gt;(frodo, Order),  % Frodo walks next to at least another Hobbit.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;hobbit_neighbor&lt;&#x2F;span&gt;&lt;span&gt;(sam, Order),  % Sam walks next to at least another Hobbit.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(gimli, legolas, Order),  % Gimli walks ahead of Legolas.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(merry, pippin, Order), % Merry walks next to Pippin.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;after&lt;&#x2F;span&gt;&lt;span&gt;(frodo, aragorn, Order). % Frodo walks after Aragorn.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Check &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;X is before Y
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, [X|T]) :-
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;member&lt;&#x2F;span&gt;&lt;span&gt;(Y, T).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, [_|T]) :-
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, T).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Check &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;X is after Y
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;after&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, List) :-
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span&gt;(Y, X, List).
&lt;&#x2F;span&gt;&lt;span&gt;    
&lt;&#x2F;span&gt;&lt;span&gt;% Ensure Sam is next to another hobbit
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;hobbit_neighbor&lt;&#x2F;span&gt;&lt;span&gt;(X, List) :-
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, frodo, List);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, sam, List);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, merry, List);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, pippin, List).
&lt;&#x2F;span&gt;&lt;span&gt;    
&lt;&#x2F;span&gt;&lt;span&gt;% Check &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;X is next to Y
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, [X, Y|_]).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, [Y, X|_]).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, [_|T]) :-
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;next_to&lt;&#x2F;span&gt;&lt;span&gt;(X, Y, T).
&lt;&#x2F;span&gt;&lt;span&gt;    
&lt;&#x2F;span&gt;&lt;span&gt;% Example query to find the order
&lt;&#x2F;span&gt;&lt;span&gt;% ?- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fellowship_order&lt;&#x2F;span&gt;&lt;span&gt;(Order).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This puzzle has a number of new types of restrictions and more members of the Fellowship represent, but works the same way as the previous one. If we run it in the sandbox we can get an answer such as &lt;code&gt;Order = [aragorn,frodo,sam,merry,pippin,gimli,legolas]&lt;&#x2F;code&gt; being returned. With an LLM we can even reverse the puzzle creation and get a textual description of the restrictions in the puzzle, as well as the puzzle itself. The following is the answer that ChatGPT returns when asked to describe the above puzzle in words:&lt;&#x2F;p&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;blockquote&gt;
&lt;p&gt;The Prolog puzzle presented involves arranging members of the Fellowship from &amp;quot;The Lord of the Rings&amp;quot; in a specific order based on given constraints. Here’s a step-by-step description:&lt;&#x2F;p&gt;
&lt;h4 id=&quot;objective&quot;&gt;Objective&lt;&#x2F;h4&gt;
&lt;p&gt;You need to determine the walking order of seven characters: Aragorn, Frodo, Sam, Merry, Pippin, Gimli, and Legolas.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;given-constraints&quot;&gt;Given Constraints&lt;&#x2F;h4&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Aragorn walks before Pippin.
&lt;&#x2F;span&gt;&lt;span&gt;    Aragorn must come at some position earlier than Pippin.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Frodo walks next to at least one other Hobbit.
&lt;&#x2F;span&gt;&lt;span&gt;    Frodo must be adjacent to either Sam, Merry, or Pippin.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Sam walks next to at least one other Hobbit.
&lt;&#x2F;span&gt;&lt;span&gt;    Sam must also be adjacent to either Frodo, Merry, or Pippin.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Gimli walks ahead of Legolas.
&lt;&#x2F;span&gt;&lt;span&gt;    Gimli must come at some position earlier than Legolas.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Merry walks next to Pippin.
&lt;&#x2F;span&gt;&lt;span&gt;    Merry and Pippin must be directly adjacent in either order.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Frodo walks after Aragorn.
&lt;&#x2F;span&gt;&lt;span&gt;    Frodo must come at some position later than Aragorn.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;key-definitions&quot;&gt;Key Definitions&lt;&#x2F;h4&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Order:
&lt;&#x2F;span&gt;&lt;span&gt;    A list of seven elements representing the walking order of the Fellowship members.
&lt;&#x2F;span&gt;&lt;span&gt;    Example: [aragorn, frodo, sam, merry, pippin, gimli, legolas].
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;before&#x2F;3:
&lt;&#x2F;span&gt;&lt;span&gt;    A predicate that checks if one character (X) appears before another (Y) in a list (Order).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;after&#x2F;3:
&lt;&#x2F;span&gt;&lt;span&gt;    A predicate that checks if one character (X) appears after another (Y) in a list (Order). It uses the before&#x2F;3 predicate to establish this relation.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;hobbit_neighbor&#x2F;2:
&lt;&#x2F;span&gt;&lt;span&gt;    A predicate that checks if a character is next to at least one Hobbit in a list (Order). Hobbits include Frodo, Sam, Merry, and Pippin.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;next_to&#x2F;3:
&lt;&#x2F;span&gt;&lt;span&gt;    A predicate that checks if two characters are adjacent in a list (Order).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;solution-mechanism&quot;&gt;Solution Mechanism&lt;&#x2F;h4&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;The fellowship_order&#x2F;1 predicate defines the order and enforces all the constraints using the helper predicates.
&lt;&#x2F;span&gt;&lt;span&gt;The list Order is constrained to have exactly the seven members of the Fellowship.
&lt;&#x2F;span&gt;&lt;span&gt;The member&#x2F;2 predicate ensures that all characters are included in the Order.
&lt;&#x2F;span&gt;&lt;span&gt;The constraints (before&#x2F;3, after&#x2F;3, hobbit_neighbor&#x2F;2, next_to&#x2F;3) enforce the relative positions and adjacency as specified.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;query&quot;&gt;Query&lt;&#x2F;h4&gt;
&lt;p&gt;To find the correct order, you can query:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;?- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fellowship_order&lt;&#x2F;span&gt;&lt;span&gt;(Order).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will produce an Order that satisfies all the given constraints.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h4&gt;
&lt;p&gt;This puzzle requires using logical relations to determine the correct sequence of characters while adhering to the given positional constraints. The use of predicates like before&#x2F;3, after&#x2F;3, hobbit_neighbor&#x2F;2, and next_to&#x2F;3 helps in logically deducing the valid walking order of the characters.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Such an approach can give us a very interactive way to evolve our problem description as well as the solution. We can give a text description of new constraints to the LLM, play around with the generated code in the sandbox, and go back to the LLM to refine the descriptions of the problem until we have the right problem statement and the solution. &lt;&#x2F;p&gt;
&lt;p&gt;In this case the back and forth between the Prolog code and the text can help us further refine the puzzle. A helpful hint if one wants to experiment further: increasing the limit parameter that controls a maximum number of inferences can help to run more complex puzzles in the sandbox. Feel free to try it out with the sandbox!&lt;&#x2F;p&gt;
&lt;p&gt;I hope you enjoyed this journey into Prolog from LLMs and back again and will consider using Prolog in such a way during your quest to solve other problems as well!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>AI at Home</title>
        <published>2023-12-16T00:00:00+00:00</published>
        <updated>2023-12-16T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/aiathome/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/aiathome/</id>
        
        <content type="html">&lt;p&gt;Large Language Models (LLMs), such as those that enable Chat-GPT, have been shown to be incredibly capable for language understanding and generation tasks. With the right prompt, they can answer questions, categorize input, rewrite pieces of text, perform sentiment analysis and more. &lt;&#x2F;p&gt;
&lt;p&gt;As good as these tools are, many of them require data to be sent to a remote server and&#x2F;or take some additional costs to run. This is due to the processing power required to use them and the proprietary nature of the models. However there exist models that can be run locally, even on relatively modest hardware, such as some of the &lt;a href=&quot;https:&#x2F;&#x2F;ai.meta.com&#x2F;llama&#x2F;&quot;&gt;LLaMA&lt;&#x2F;a&gt; models from Meta. Such locally runnable models can enable modern AI setups fully running at home, without the need for data being sent to another party. This article is a brief introduction on how to get one these models up and running.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=aiathome.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A generated picture of an AI helping at home.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The easiest solution that I have found for this purpose is the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;oobabooga&#x2F;text-generation-webui&quot;&gt;text-generation-webui&lt;&#x2F;a&gt; tool. As the name suggests it enables the use of language models by a web based UI and it can do so running only on the local machine. The setup is very straightforward: clone or download the repository, run the start script for your operating system, e.g.: &lt;code&gt;start_wsl.bat&lt;&#x2F;code&gt;for running on WSL, and you are pretty much ready to go.&lt;&#x2F;p&gt;
&lt;p&gt;This tool can use various models, but a nice one to get started is &lt;a href=&quot;https:&#x2F;&#x2F;huggingface.co&#x2F;TheBloke&#x2F;Llama-2-7B-GGUF&quot;&gt;Llama-2-7B-GGUF model provided by TheBloke&lt;&#x2F;a&gt;, which is a 7 billion parameter LLaMa2 model. After loading it, we can simply start chatting.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=aiconversation.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A short conversation with the AI model.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Although this model is on the smaller side it runs very adequately on a 2021 model Asus G14 laptop, and functions well for simple queries and conversations.&lt;&#x2F;p&gt;
&lt;p&gt;There can be a lot of possibilities to explore with such a local setup, especially with larger, more capable models. It also provides an easy way for prototyping, as one can also use an API, that is a local drop-in replacement for the API of OpenAI, to interact with the model. &lt;&#x2F;p&gt;
&lt;p&gt;I hope this article can help you get you started in exploring your AI use cases locally!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Accidental Renaissance</title>
        <published>2023-10-14T00:00:00+00:00</published>
        <updated>2023-10-14T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/accidentalren/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/accidentalren/</id>
        
        <content type="html">&lt;p&gt;One interesting place to look for nice photographs is the &lt;a href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;AccidentalRenaissance&#x2F;&quot;&gt;Accidental Renaissance subreddit&lt;&#x2F;a&gt;. It is a forum where photographs that resemble Renaissance art, or other art movements that existed between the 14th and 19th centuries, are shared.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featuredcard.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Photo of part of a kitchen in renaissance style, generated using mage.space.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;A related question is how to decide on what counts as Renaissance (or Baroque, Romanticist, etc) art for the photographs that would fall under the Accidental Renaissance subreddit. This is especially true for people with no in-depth knowledge on art movements. With the great results that deep learning based image classifiers can achieve and how easy it is using libraries such as &lt;a href=&quot;https:&#x2F;&#x2F;www.fast.ai&#x2F;&quot;&gt;fast.ai&lt;&#x2F;a&gt; to implement them, I thought it would be a good idea to create a solution for this question. In this article I aim to build a model that given a photo can decide whether a photograph could belong in the Accidental Renaissance subreddit.&lt;&#x2F;p&gt;
&lt;p&gt;We are going to use two sources of data for this purpose. One is a collection of photos from the Accidental Renaissance subreddit, from which we can learn what makes for a good &amp;quot;Accidental Renaissance&amp;quot; photo. The other is a group of photos that are likely to be not accidental renaissance. For this we will use the following &lt;a href=&quot;https:&#x2F;&#x2F;www.kaggle.com&#x2F;datasets&#x2F;duttadebadri&#x2F;image-classification&quot;&gt;image data set from Kaggle&lt;&#x2F;a&gt;. This dataset contains images categorized into the following groups: Architecture, Arts and Culture, Food and Drinks and Travel and Adventure.&lt;&#x2F;p&gt;
&lt;p&gt;With these two datasets we can start to create the classifier using &lt;a href=&quot;https:&#x2F;&#x2F;www.fast.ai&#x2F;&quot;&gt;fast.ai&lt;&#x2F;a&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;First part is importing the required dependencies.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;fastai
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;fastai.vision.all &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;*
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;random &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;sample
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;ipywidgets &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;interact
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;torch
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next we define the paths for the images in the datasets.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;all_path = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;mnt&#x2F;c&#x2F;data&#x2F;accidental&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;renaissance_path = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;mnt&#x2F;c&#x2F;data&#x2F;accidental&#x2F;renaissance&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;architecture_path = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;mnt&#x2F;c&#x2F;data&#x2F;accidental&#x2F;architecture&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;art_and_culture_path = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;mnt&#x2F;c&#x2F;data&#x2F;accidental&#x2F;artandculture&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;food_and_drinks_path = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;mnt&#x2F;c&#x2F;data&#x2F;accidental&#x2F;foodanddrinks&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;travel_and_adventure_path = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;mnt&#x2F;c&#x2F;data&#x2F;accidental&#x2F;travelandadventure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;While fast.ai has a function to gather all the image files from a directory, the version used for this experiment did not gather image files with the .webp extension. As many image files from Accidental Renaissance subreddit have a .webp extension, we will create a custom function to get all images files, including those of this type. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;get_all_image_files&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recurse&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;folders&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Get image files in `path` recursively, only in `folders`, if specified.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_files&lt;&#x2F;span&gt;&lt;span&gt;(path, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;extensions&lt;&#x2F;span&gt;&lt;span&gt;=[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.webp&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.jpeg&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.gif&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.jpg&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recurse&lt;&#x2F;span&gt;&lt;span&gt;=recurse, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;folders&lt;&#x2F;span&gt;&lt;span&gt;=folders)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The files of each category can be retrieved using the above function and the relevant paths.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;renaissance_files = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_all_image_files&lt;&#x2F;span&gt;&lt;span&gt;(renaissance_path)
&lt;&#x2F;span&gt;&lt;span&gt;architecture_files = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_all_image_files&lt;&#x2F;span&gt;&lt;span&gt;(architecture_path)
&lt;&#x2F;span&gt;&lt;span&gt;art_and_culture_files = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_all_image_files&lt;&#x2F;span&gt;&lt;span&gt;(art_and_culture_path)
&lt;&#x2F;span&gt;&lt;span&gt;food_and_drinks_files = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_all_image_files&lt;&#x2F;span&gt;&lt;span&gt;(food_and_drinks_path)
&lt;&#x2F;span&gt;&lt;span&gt;travel_and_adventure_files = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_all_image_files&lt;&#x2F;span&gt;&lt;span&gt;(travel_and_adventure_path)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we have the list of files, it would be great to see how many of each type we have in the dataset.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;{&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Renaissance files&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(renaissance_files), 
&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Architecture files&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(architecture_files),
&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Arts and Culture files&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(art_and_culture_files),
&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Food and Drinks files&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(food_and_drinks_files),
&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Travel and Adventure files&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(travel_and_adventure_files)
&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above code will give use the following result: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;{&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Renaissance files&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;135&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Architecture files&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8763&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Arts and Culture files&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8531&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Food and Drinks files&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7849&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Travel and Adventure files&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8800&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It seems we have far more images in each category of the Kaggle dataset than those from the Accidental Renaissance subreddit. There are many possible ways to deal with an unbalanced dataset, but here we go for a simple sampling based solution. We create a sampled dataset from the Kaggle dataset that is as large as the image set of the subreddit, that we call our &amp;quot;regular&amp;quot; image set.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;sampled_regular_files = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sample&lt;&#x2F;span&gt;&lt;span&gt;(architecture_files + art_and_culture_files + food_and_drinks_files + travel_and_adventure_files, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;135&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next we set up two directories, one for the renaissance files and one for the regular files respectively. We make sure to create the directories &lt;code&gt;learning&#x2F;renaissance&lt;&#x2F;code&gt; and &lt;code&gt;learning&#x2F;regular&lt;&#x2F;code&gt; if they do not yet exist. If they do exist already, the files within them will be deleted (which makes rerunning the experiment easier).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;delete_files&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;directory&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;filename &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;os.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;listdir&lt;&#x2F;span&gt;&lt;span&gt;(directory):
&lt;&#x2F;span&gt;&lt;span&gt;        file_path = os.path.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;(directory, filename)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;os.path.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;isfile&lt;&#x2F;span&gt;&lt;span&gt;(file_path):
&lt;&#x2F;span&gt;&lt;span&gt;            os.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;remove&lt;&#x2F;span&gt;&lt;span&gt;(file_path)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;learning_dir = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;learning&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;learning_renaissance_dir = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;learning&#x2F;renaissance&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;learning_regular_dir= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;learning&#x2F;regular&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;not os.path.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;exists&lt;&#x2F;span&gt;&lt;span&gt;(learning_renaissance_dir):
&lt;&#x2F;span&gt;&lt;span&gt;    os.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;makedirs&lt;&#x2F;span&gt;&lt;span&gt;(learning_renaissance_dir)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;delete_files&lt;&#x2F;span&gt;&lt;span&gt;(learning_renaissance_dir)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;not os.path.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;exists&lt;&#x2F;span&gt;&lt;span&gt;(learning_regular_dir):
&lt;&#x2F;span&gt;&lt;span&gt;    os.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;makedirs&lt;&#x2F;span&gt;&lt;span&gt;(learning_regular_dir)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;delete_files&lt;&#x2F;span&gt;&lt;span&gt;(learning_regular_dir)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With the directories in place we can copy all the files into their respective directories as the final part of our dataset setup.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;file &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;sampled_regular_files:
&lt;&#x2F;span&gt;&lt;span&gt;    shutil.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;copy&lt;&#x2F;span&gt;&lt;span&gt;(file, learning_regular_dir)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;file &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;renaissance_files:
&lt;&#x2F;span&gt;&lt;span&gt;    shutil.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;copy&lt;&#x2F;span&gt;&lt;span&gt;(file, learning_renaissance_dir)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can double check things to see if everything went well. First we check if the amount of files in the directories add up.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;files = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_all_image_files&lt;&#x2F;span&gt;&lt;span&gt;(learning_dir)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(files)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This should return 270. We can also check and remove any files that could not be read as image files using the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;failed = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;verify_images&lt;&#x2F;span&gt;&lt;span&gt;(files)
&lt;&#x2F;span&gt;&lt;span&gt;failed.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(Path.unlink)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next we are going to set up a Datablock to get everything ready to run the learners using fast.ai. The categories will be labelled &lt;code&gt;renaissance&lt;&#x2F;code&gt; and &lt;code&gt;regular&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;label_function&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;o&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    parent_name  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Path&lt;&#x2F;span&gt;&lt;span&gt;(o).parent.name
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;parent_name == &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;renaissance&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;renaissance&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;regular&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;data_block = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DataBlock&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;blocks&lt;&#x2F;span&gt;&lt;span&gt;=(ImageBlock, CategoryBlock), 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_items&lt;&#x2F;span&gt;&lt;span&gt;=get_all_image_files, 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;splitter&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RandomSplitter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;valid_pct&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0.2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;seed&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_y&lt;&#x2F;span&gt;&lt;span&gt;=label_function,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item_tfms&lt;&#x2F;span&gt;&lt;span&gt;=[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RandomResizedCrop&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;128&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;min_scale&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0.3&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;dls = data_block.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dataloaders&lt;&#x2F;span&gt;&lt;span&gt;(learning_dir)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can check our datablock setup by showing a batch of images and their labels from it.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt; dls.show_batch(max_n=8)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=batch.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A batch of the datablock showing examples of the image dataset with their labels.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The above code will return a batch such as this, if all went well.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have the datablock set up we can do the learning. We are going to use the resnet34 model as a base with 5 iterations of fine tuning.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;learn = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vision_learner&lt;&#x2F;span&gt;&lt;span&gt;(dls, resnet34, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;metrics&lt;&#x2F;span&gt;&lt;span&gt;=error_rate)
&lt;&#x2F;span&gt;&lt;span&gt;learn.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fine_tune&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After 5 iterations, this particular model got a 0.486287 loss on the training set, a 0.595300 loss on the validation set and an error rate of 0.203704.&lt;&#x2F;p&gt;
&lt;p&gt;So how does the model actually do in practice? One interesting way to evaluate it is to generate a few photos in a renaissance art style, with the nice AI image generation tools available. I have generated a few photos using &lt;a href=&quot;https:&#x2F;&#x2F;www.mage.space&#x2F;&quot;&gt;mage.space&lt;&#x2F;a&gt; where I aimed for modern objects in a renaissance style, similarly to what might be photographed and posted in the Accidental Renaissance subreddit.&lt;&#x2F;p&gt;
&lt;p&gt;For a generated photo of a kitchen in renaissance style the model got the right category predicted (i.e.: &amp;quot;renaissance&amp;quot;), with a probability of 0.7438. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A photo of a kitchen in renaissance style generated on mage.space.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;A generated photo of a car in a renaissance style got the right category predicted with a probability of 0.8643.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=car.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A photo of a car in renaissance style generated on mage.space.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;However interestingly enough the trained model has trouble recognizing actual renaissance (or baroque) paintings. The famous picture of the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;The_Last_Supper_(Leonardo)&quot;&gt;Last Supper&lt;&#x2F;a&gt; by Da Vinci got classified as &amp;quot;regular&amp;quot;, just as &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Judith_Beheading_Holofernes_(Caravaggio)&quot;&gt;Judith Beheading Holofernes&lt;&#x2F;a&gt; by Caravaggio. &lt;&#x2F;p&gt;
&lt;p&gt;It would be interesting to hypothesize and investigate why this could be the case. Although they might be similar in art style, the paintings and the photos in the subreddit as well as the generated photos might differ in other aspects, such as the subject matter. The learning process is likely needing additional data, as 135 photos might be too low for a well fine tuned model. We could for example use exiting renaissance or baroque art to bolster the dataset. We could also investigate data augmentation to improve the training set.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Linked Data Based Advanced Credit Rationale</title>
        <published>2023-06-23T00:00:00+00:00</published>
        <updated>2023-06-23T00:00:00+00:00</updated>
        <author>
          <name>Newres Al Haider</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/publication/linked-data-adcr/" type="text/html"/>
        <id>https://www.newresalhaider.com/publication/linked-data-adcr/</id>
        
        <content type="html">&lt;h1 id=&quot;abstract&quot;&gt;Abstract&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;A credit rationale forms an integral part of the credit granting and credit decision making process. Historically such credit rationales were written as a natural language document. This meant that automated handling of the meaning, i.e.: semantics, of such documents by machines was too complex to be done with sufficient accuracy. As a consequence, the creation and verification of a credit rationale is a time consuming and manual process. The solution that we propose is to create a Linked Data based credit rationale that we call the Advanced Credit Rationale (AdCR). Linked Data can ensure that the semantics in the credit rationale are both human and machine understandable. This enables the automated checking of the credit rationale for potential regulatory issues, as well as semantic based querying over a whole portfolio of such credit rationales. An issue that often hinders the use of a Linked Data based approach is that the creation of the knowledge graph can be challenging for domain experts. In this work we propose an approach that facilitates the creation of a credit rationale that is based on a Linked Data knowledge graph. We evaluate this approach with a prototype. We also demonstrate that with this approach the creation of a Linked Data based credit rationale should not take more time than a natural language based credit rationale. In addition we show that the structures that a Linked Data based credit rationale provides are generally seen as helpful.&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;authors&quot;&gt;Authors&lt;&#x2F;h1&gt;
&lt;p&gt;Newres Al Haider, Keng Ng, Ali Hashmi, Lauma Veidemane, Diederik Schut &lt;&#x2F;p&gt;
&lt;h1 id=&quot;presented-at&quot;&gt;Presented at&lt;&#x2F;h1&gt;
&lt;p&gt;1st Workshop of Knowledge Graphs for Semantics-driven Systems Engineering (KG4SDSE) 
A Workshop at CAiSE2023 
&lt;a href=&quot;https:&#x2F;&#x2F;link.springer.com&#x2F;chapter&#x2F;10.1007&#x2F;978-3-031-34985-0_11&quot;&gt;Appears in Advanced Information Systems Engineering Workshops. CAiSE 2023. Lecture Notes in Business Information Processing, vol 482&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-project&quot;&gt;Related Project&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;project&#x2F;legal-banking-compliance&#x2F;&quot;&gt;Legal Banking Compliance&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Dunes</title>
        <published>2022-09-18T00:00:00+00:00</published>
        <updated>2022-09-18T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/dunes/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/dunes/</id>
        
        <content type="html">&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;stability.ai&#x2F;blog&#x2F;stable-diffusion-public-release&quot;&gt;Stable Diffusion&lt;&#x2F;a&gt; is one of the latest models that is capable of translating a piece of text, such as &amp;quot;arid, desert dunes&amp;quot; into great looking images. Unlike some other tools and models, Stable Diffusion can be installed and run locally on a desktop machine (see this &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lstein&#x2F;stable-diffusion&quot;&gt;repo and instructions&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;One of the great features of the Stable Diffusion model is the ability to combine a text input with a pre-existing image to generate new images. In this article I aim to dive into this feature by starting with a photo of dunes that I took a while back. I will use descriptions based on sci-fi worlds with dunes, notably &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Arrakis&quot;&gt;Arrakis from Dune&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tatooine&quot;&gt;Tatooine from Star Wars&lt;&#x2F;a&gt;, to create images from the original photo that look like as if they were from these worlds.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.png&gt;
    
    &lt;alt= A sequence of images (from left to right): the original picture of dunes in the Netherlands (Soester Duinen), a generated image of Arrakis and a generated image of Tatooine.&gt;
    
    &lt;figcaption&gt;
        
        
        A sequence of images (from left to right): the original picture of dunes in the Netherlands (Soester Duinen), a generated image of Arrakis and a generated image of Tatooine.
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The original photo itself has been taken in the sand dunes of &lt;a href=&quot;https:&#x2F;&#x2F;commons.wikimedia.org&#x2F;wiki&#x2F;Soester_Duinen&quot;&gt;Soester Duinen&lt;&#x2F;a&gt; in the Netherlands. The sand here has been deposited during the last ice age which came to lay bare due to intensive grazing during the middle ages. Almost all such areas in the Netherlands have been reclaimed from the sand, with the remaining dunes of the Soester Duinen now being maintained as geographical monuments.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=duinen.png&gt;
    
    &lt;alt= The original photo I have taken of dunes in Soester Duinen.&gt;
    
    &lt;figcaption&gt;
        
        
        The original photo I have taken of dunes in Soester Duinen.
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Given this image, with the help of Stable Diffusion, I had the goal to transform it to sand dunes that one could find in a sci-fi setting. &lt;&#x2F;p&gt;
&lt;p&gt;First I tried to get an image that looks more like &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Arrakis&quot;&gt;the planet Arrakis, also known as Dune&lt;&#x2F;a&gt;. One of the characteristics of this planet is that it is incredibly arid. This means that in order to get something that resembles Arrakis, with less vegetation and clouds, I need to provide a text prompt that emphasizes the harsh arid nature of Arrakis.&lt;&#x2F;p&gt;
&lt;p&gt;I have used the prompt &amp;quot;The planet Arrakis also known as Dune, arid, desert dunes, desolate, cloudless blue sky&amp;quot; to transform the image to the one that follows:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=dune.png&gt;
    
    &lt;alt= An image of Arrakis, the planet in the novel Dune, that was generated.&gt;
    
    &lt;figcaption&gt;
        
        
        An image of Arrakis, the planet in the novel Dune, that was generated.
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The Stable Diffusion model takes a number of parameters aside from the text prompt to generate images. One that is specific to using a starting image is called Img2Img strength, which defines how much weight should be given to the initial image when generating new images. Lower scores make the generated image look closer to the initial image, while higher scores let the model dream up new images more freely. For the images generated the value 0.75 was used (unless noted otherwise). One can quite well see the effect of the base image in the layout of the new one that is made to look like Arrakis e.g.: like how the sand dunes have replaced the vegetation in the background.&lt;&#x2F;p&gt;
&lt;p&gt;It can generally take some trial and error to find the right parameters to generate images that fit the creators vision. Nonetheless it is quite amazing how many good looking images can be generated just by playing around with the parameter and prompt selection.&lt;&#x2F;p&gt;
&lt;p&gt;While for Arrakis it took quite a long text prompt get close to the results that I wanted it is quite different when trying a Star wars related prompt. The prompt: &amp;quot;Tatooine, from Star Wars&amp;quot; is itself enough to help generate the following image from our original dune photo:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=starwars.png&gt;
    
    &lt;alt= An image of Tatooine, a planet from Star Wars, that was generated.&gt;
    
    &lt;figcaption&gt;
        
        
        An image of Tatooine, a planet from Star Wars, that was generated.
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;As one can see even from a short prompt, it is possible for the model to generate images that look like they were taken as photographs from the planet Tatooine from Star Wars. It seems the system has a notion of how the architecture of buildings look like on the planet in this setting and is capable of generating images of such buildings into the photograph.&lt;&#x2F;p&gt;
&lt;p&gt;It is also possible to go beyond the well-known fictional planets and let the model dream up a setting.&lt;&#x2F;p&gt;
&lt;p&gt;The prompt &amp;quot;Cyberpunk settlement, detailed&amp;quot; gave the following results:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=cyberpunk.png&gt;
    
    &lt;alt= A cyberpunk styled settlement that was generated from the original image. Generated with the img2img strength of 0.65.&gt;
    
    &lt;figcaption&gt;
        
        
        A cyberpunk styled settlement that was generated from the original image. Generated with the img2img strength of 0.65.
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;I hope I have given at least a small preview or what is possible with Stable Diffusion&#x27;s img2img generation. It is quite a fun project to take photographs and modify them using text prompts to all kinds of imaginary settings. Images of the next sci-fi setting set in the dunes could be just one prompt and one photograph away.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Star Wars Trek</title>
        <published>2022-08-26T00:00:00+00:00</published>
        <updated>2022-08-27T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/star-wars-trek/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/star-wars-trek/</id>
        
        <content type="html">&lt;p&gt;Recently I started to do the latest version of the course &lt;a href=&quot;https:&#x2F;&#x2F;course.fast.ai&#x2F;&quot;&gt;Practical Deep Learning for Coders&lt;&#x2F;a&gt; from &lt;a href=&quot;https:&#x2F;&#x2F;www.fast.ai&#x2F;&quot;&gt;fast.ai&lt;&#x2F;a&gt;. I am very much enjoying the hands-on approach of the course and it is quite amazing to see how a deep learning based image classifier could be built with very little code. In the &lt;a href=&quot;https:&#x2F;&#x2F;course.fast.ai&#x2F;Lessons&#x2F;lesson1.html&quot;&gt;first chapter of the book that accompanies the course&lt;&#x2F;a&gt; a model is trained to recognize whether an image depicts a bird or a forest. In this article, as an exercise, I will instead create a model that can recognize if an image of a spaceship is from Star Wars or from Star Trek.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.png&gt;
    
    &lt;alt= A star destroyer from Star Wars on the left and the Enterprise from Star Trek on the right. Star Wars is the copyright of Disney and Star Wars is the copyright of Paramount Pictures.&gt;
    
    &lt;figcaption&gt;
        
        
        A star destroyer from Star Wars on the left and the Enterprise from Star Trek on the right. Star Wars is the copyright of Disney and Star Wars is the copyright of Paramount Pictures.
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;I will list all the code I used for creating and using this model in this article, with a brief description after each code fragment on what it does. The code is very similar to the code used in the the first chapter of the FastAI book, as in both cases we are aiming to recognize whether an image belongs to one of two categories, with the same setup. If one wants to follow along, I can highly recommend using a service such as &lt;a href=&quot;https:&#x2F;&#x2F;colab.research.google.com&#x2F;&quot;&gt;Colab&lt;&#x2F;a&gt; to get started quickly but a local install also does work. &lt;a href=&quot;https:&#x2F;&#x2F;colab.research.google.com&#x2F;github&#x2F;fastai&#x2F;fastbook&#x2F;blob&#x2F;master&#x2F;01_intro.ipynb&quot;&gt;Chapter 1 of the book&lt;&#x2F;a&gt; is directly available on Colab as well. &lt;&#x2F;p&gt;
&lt;p&gt;Now let&#x27;s get to the code used:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;! [ -e &#x2F;content ] &amp;amp;&amp;amp; pip install -Uqq fastbook
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;fastbook
&lt;&#x2F;span&gt;&lt;span&gt;fastbook.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;setup_book&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first part is installing and setting up all the dependencies. Assuming we are working in Colab we need the first line to install the dependencies. If we work in our local (virtual) environment we can just do a &lt;code&gt;pip install fastbook&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;fastbook &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;*
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The next part is importing all the things we will need from fastbook. For the purposes of this small tutorial we will just import everything.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;searches = &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;star wars ship&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;,&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;star trek ship&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;path = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Path&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;star_wars_or_trek&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;not path.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;exists&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;o &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;searches:
&lt;&#x2F;span&gt;&lt;span&gt;        dest = (path&#x2F;o)
&lt;&#x2F;span&gt;&lt;span&gt;        dest.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;mkdir&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;exist_ok&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;parents&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        results = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;search_images_ddg&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;{o}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; photo&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;download_images&lt;&#x2F;span&gt;&lt;span&gt;(dest, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;urls&lt;&#x2F;span&gt;&lt;span&gt;=results[:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;200&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;resize_images&lt;&#x2F;span&gt;&lt;span&gt;(dest, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;max_size&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;400&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dest&lt;&#x2F;span&gt;&lt;span&gt;=dest)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next we are going to gather the images based on which we will create and test our classifier. The above code will set up a directory called &lt;code&gt;star_wars_or_trek&lt;&#x2F;code&gt;, assuming it does not exist yet, and will search for images using the phrase &lt;code&gt;star wars ship&lt;&#x2F;code&gt; and &lt;code&gt;star trek ship&lt;&#x2F;code&gt; using &lt;a href=&quot;https:&#x2F;&#x2F;duckduckgo.com&#x2F;&quot;&gt;DuckDuckGo&lt;&#x2F;a&gt;. The found images will be downloaded in sub-directories called &lt;code&gt;star wars ship&lt;&#x2F;code&gt; and &lt;code&gt;star trek ship&lt;&#x2F;code&gt; containing the respective images. Finally we are going to resize the images that we download to a comparable maximum size. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;failed = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;verify_images&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_image_files&lt;&#x2F;span&gt;&lt;span&gt;(path))
&lt;&#x2F;span&gt;&lt;span&gt;failed.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(Path.unlink)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The next step is verifying that all the images we got are valid image files, as things can go wrong during search and download. If they are not valid images we can remove them from our dataset.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;dls = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;DataBlock&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;blocks&lt;&#x2F;span&gt;&lt;span&gt;=(ImageBlock, CategoryBlock), 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_items&lt;&#x2F;span&gt;&lt;span&gt;=get_image_files, 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;splitter&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;RandomSplitter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;valid_pct&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0.2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;seed&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;get_y&lt;&#x2F;span&gt;&lt;span&gt;=parent_label,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item_tfms&lt;&#x2F;span&gt;&lt;span&gt;=[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Resize&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;192&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;method&lt;&#x2F;span&gt;&lt;span&gt;=&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;squish&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)]
&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dataloaders&lt;&#x2F;span&gt;&lt;span&gt;(path)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The datablock is where all the elements are setup that are required for learning our model. It specifies that we want to learn from images and want to derive categories from it, i.e. whether an image is a Star Wars ship or a Star Trek ship. It uses the data from the files that we have downloaded. &lt;&#x2F;p&gt;
&lt;p&gt;One very important aspect of creating a model is to ensure its predictions are accurate. A way we can test it is to set some portion of the data aside that we will use for evaluation as opposed to learning. In this case we use 20% of the data randomly selected for evaluation.&lt;&#x2F;p&gt;
&lt;p&gt;We also specify that the label for each images can be derived from the directory that they are in. Finally we aim to apply a transform to the images, to standardize them in a way that helps the training of the model. &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;dls.show_batch(max_n=6)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A good way to check if our datablock is setup correctly is to show a batch images, in this case six, from our datablock. This can give us a set of images, such as the one below, that we can visually inspect before we start our learning.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=batchofships.png&gt;
    
    &lt;alt= A batch of six images from our dataset of ships that we have labelled either a Star Wars ship or a Star Trek ship.&gt;
    
    &lt;figcaption&gt;
        
        
        A batch of six images from our dataset of ships that we have labelled either a Star Wars ship or a Star Trek ship.
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;learn = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vision_learner&lt;&#x2F;span&gt;&lt;span&gt;(dls, resnet18, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;metrics&lt;&#x2F;span&gt;&lt;span&gt;=error_rate)
&lt;&#x2F;span&gt;&lt;span&gt;learn.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fine_tune&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above two lines kick off the actual learning, i.e. the creation of a model that can differentiate between a ship from Star Wars and Star Trek, based on our setup of the datablock. One of the great things for image based models is that there are pre-trained models that exist, such as &lt;code&gt;resnet18&lt;&#x2F;code&gt; that have been trained on a lot of images. This means that we do not have to start our image learning from scratch. Instead we can use this existing model as a starting point and fine tune it to our task at hand: the recognition of the right class of spaceship. &lt;&#x2F;p&gt;
&lt;p&gt;Here we just do 3 iterations of fine tuning. The output from this fine tuning can be seen below. The results will vary for each run of fine tuning, but this will hopefully illustrate the process:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;csv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-csv &quot;&gt;&lt;code class=&quot;language-csv&quot; data-lang=&quot;csv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;epoch	train_loss	valid_loss	error_rate	time
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0	1.236515	0.963507	0.323944	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;00:07
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;epoch	train_loss	valid_loss	error_rate	time
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0	0.531795	0.751966	0.281690	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;00:10
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1	0.419798	0.844729	0.225352	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;00:10
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2	0.312620	0.631814	0.197183	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;00:10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this case model was trained on desktop with a GPU but doing this on Colab is also very fast. We can get an error rate at around 0.2 with this setup which is good enough for our short article. That said it would be interesting exercise for the future to see how we could get this error rate down or to examine what are the examples where the model finds it difficult to predict the right category.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we learned our model we would like to put it to use by giving it an image to classify. We have two options on how to do this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;uploader = widgets.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FileUpload&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;uploader
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When using a notebook we can have a widget with a file selector, with which we can upload the image we would like to classify.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;uploader = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SimpleNamespace&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span&gt;= [&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;images&#x2F;stardestroyer.jpeg&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;])
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# uploader = SimpleNamespace(data = [&amp;#39;images&#x2F;enterprise.webp&amp;#39;])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can otherwise just load in the image from our (local) drive as well. Here one line is commented out so we could quickly switch between two options for images. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;img = PILImage.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;create&lt;&#x2F;span&gt;&lt;span&gt;(uploader.data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;is_star_wars,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;,probs = learn.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;predict&lt;&#x2F;span&gt;&lt;span&gt;(img)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;What ship is this?: &lt;&#x2F;span&gt;&lt;span&gt;{is_star_wars}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Probability it&amp;#39;s a star wars ship: &lt;&#x2F;span&gt;&lt;span&gt;{probs[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:.6f&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Probability it&amp;#39;s a star trek ship: &lt;&#x2F;span&gt;&lt;span&gt;{probs[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:.6f&lt;&#x2F;span&gt;&lt;span&gt;}&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The final part is taking the image that we now added and asking the learned model to predict what kind of ship it is. With the above code we will print out both the category of the ship as well as the probabilities attached to the category. This will give an indication of how confident the model is in the prediction.&lt;&#x2F;p&gt;
&lt;p&gt;If we use the image of a Star Destroyer from Star Wars, that is displayed on the left at the start of this article, our model will predict with very high confidence that it is a ship from Star Wars.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;What ship is this?: star wars ship.
&lt;&#x2F;span&gt;&lt;span&gt;Probability it&amp;#39;s a star wars ship: 0.999536
&lt;&#x2F;span&gt;&lt;span&gt;Probability it&amp;#39;s a star trek ship: 0.000464
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Similarly, if we use the image of Enterprise from Star Trek, the model will have classify it correctly with very high probabilities. &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;What ship is this?: star trek ship.
&lt;&#x2F;span&gt;&lt;span&gt;Probability it&amp;#39;s a star wars ship: 0.000007
&lt;&#x2F;span&gt;&lt;span&gt;Probability it&amp;#39;s a star trek ship: 0.999993
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In both cases the model can classify these iconic spaceships really well. It is really cool to see how little code is required to create and use a model for these type of predictions with fast.ai, which I think it is pretty amazing. I can not recommend the book&#x2F;course &lt;a href=&quot;https:&#x2F;&#x2F;course.fast.ai&#x2F;&quot;&gt;Practical Deep Learning for Coders&lt;&#x2F;a&gt; enough and will definitely hope to dive deeper as I go along.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Raku Parrot</title>
        <published>2022-08-06T00:00:00+00:00</published>
        <updated>2022-08-06T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/raku-parrot/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/raku-parrot/</id>
        
        <content type="html">&lt;p&gt;One of the programming languages that I have stumbled upon not too long ago is &lt;a href=&quot;https:&#x2F;&#x2F;www.raku.org&#x2F;&quot;&gt;Raku&lt;&#x2F;a&gt;. This language, which was previously known as Perl 6, is chock-full of really cool features. It has support for programming in both object oriented and functional paradigms, optional gradual typing, a nice story for async programming and more. &lt;&#x2F;p&gt;
&lt;p&gt;A really nice feature of Raku is that &lt;a href=&quot;https:&#x2F;&#x2F;docs.raku.org&#x2F;language&#x2F;grammars&quot;&gt;Grammars&lt;&#x2F;a&gt; are a core part of the language. Grammars allow for a great way to interpret textual information. When programming there are a lot of use cases where a specific set of patterns need to be recognized and used from text. An interpreter for a format such as &lt;a href=&quot;https:&#x2F;&#x2F;www.json.org&quot;&gt;JSON&lt;&#x2F;a&gt; is a good example. From the text version of a JSON file elements such as arrays, objects, numbers, strings, etc. needs to be recognized. In addition a representation of them needs to be built for manipulation and use within a programming language. &lt;&#x2F;p&gt;
&lt;p&gt;In order to demonstrate this grammar feature of Raku, we are going to implement a small program to parrot phrases back to us. The grammar inside this program describes short three word (sometimes a bit irregular) sentences like &amp;quot;I like sushi.&amp;quot; or &amp;quot;They eat pear.&amp;quot;. Our program will parrot these phrases back to us twice, first just as text and the second time with certain words being replaced by emojis. &lt;&#x2F;p&gt;
&lt;p&gt;To give a preview of what this code will do, if we call it from the command line given the input: &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;I like sushi.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we get back:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;I like sushi.
&lt;&#x2F;span&gt;&lt;span&gt;I like 🍣.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;if we would use an input like: &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;They eat pear.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we get back:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;They eat pear.
&lt;&#x2F;span&gt;&lt;span&gt;They eat 🍐.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In cases where the our parroting program does not understand what we enter, for example for the sentence: &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;This is too complex!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we get the phrase back:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Squaaawk can not understand!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To show how this is implemented, first we will list the full code in Raku and we will go over its elements piece by piece. If one wants to follow along &lt;a href=&quot;https:&#x2F;&#x2F;replit.com&#x2F;languages&#x2F;raku&quot;&gt;Replit&lt;&#x2F;a&gt; offers a way to see the Raku language in action without any installation. For a local installation the &lt;a href=&quot;https:&#x2F;&#x2F;rakudo.org&#x2F;downloads&quot;&gt;Rakudo&lt;&#x2F;a&gt; implementation or the &lt;a href=&quot;https:&#x2F;&#x2F;rakudo.org&#x2F;star&quot;&gt;Rakudo Star bundle&lt;&#x2F;a&gt; are great starting points. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;raku&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-raku &quot;&gt;&lt;code class=&quot;language-raku&quot; data-lang=&quot;raku&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;grammar ParrotHearing&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;rule&lt;&#x2F;span&gt;&lt;span&gt; TOP { &amp;lt;personalPronoun&amp;gt; &amp;lt;parrotVerb&amp;gt; &amp;lt;parrotNoun&amp;gt;&amp;lt;ending&amp;gt;}
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span&gt; personalPronoun { &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;You&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;He&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;She&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;It&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;We&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;They&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;| &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Me&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Him&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Her&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Us&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Them&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span&gt; parrotVerb { \w+ }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span&gt; parrotNoun { \w+ }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span&gt; ending { &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; | &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;sub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;convertToEmoji&lt;&#x2F;span&gt;&lt;span&gt;($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;my &lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;parsedValue&lt;&#x2F;span&gt;&lt;span&gt; = uniparse($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;uc&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; ($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;parsedValue&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;parsedValue&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#eff1f5;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;EmojiActions&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;method &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;TOP&lt;&#x2F;span&gt;&lt;span&gt; ($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;&#x2F;) {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;        my $personalPronoun = $&amp;lt;personalPronoun&amp;gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;        my $parrotVerb= $&amp;lt;parrotVerb&amp;gt;.made;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;        my $parrotNoun= $&amp;lt;parrotNoun&amp;gt;.made;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;        my $ending= $&amp;lt;ending&amp;gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;        make ( &amp;quot;$personalPronoun $parrotVerb $parrotNoun$ending&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;    method parrotVerb ($&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;       make (convertToEmoji($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;&#x2F;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;    method parrotNoun($&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;       make (convertToEmoji($&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;&#x2F;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;sub MAIN($name) {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;    my $parrotParsed =  ParrotHearing.parse($name.Str, actions =&amp;gt; EmojiActions);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;    if ($parrotParsed) {say $parrotParsed.Str;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;                       say $parrotParsed.made.Str}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;    else {say &amp;quot;Squaaawk can not understand!&amp;quot;};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first part of the implementation is defining the Grammar of the language that the parrot hears that we call ParrotHearing. Grammars in Raku are a way to organize &lt;a href=&quot;https:&#x2F;&#x2F;docs.raku.org&#x2F;language&#x2F;regexes&quot;&gt;Regular Expressions (Regexes)&lt;&#x2F;a&gt;. Without going into the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Regular_expression#Formal_language_theory&quot;&gt;formal definition of Regular Expressions&lt;&#x2F;a&gt; they can be used to define patterns of characters in text. These patterns can be utilized in various way, such as searching, replacing and transforming text. &lt;&#x2F;p&gt;
&lt;p&gt;The basic elements of the grammar are regex objects. These contain the regex patterns that we use. In Raku these regular expressions are defined with their own domain specific language. For example one regex object our grammar will be using is &lt;code&gt;{ \w+ }&lt;&#x2F;code&gt;. The pattern in this object, &lt;code&gt;\w+&lt;&#x2F;code&gt;,  matches a single word character, consisting of  letters, digits or underscores, one or more times. For example if we have the text &lt;code&gt;&amp;quot;These are all w0rds_ &amp;quot;&lt;&#x2F;code&gt; it will match four times. &lt;&#x2F;p&gt;
&lt;p&gt;Another regex object that we will use is &lt;code&gt;{ &amp;quot;I&amp;quot; | &amp;quot;You&amp;quot; | &amp;quot;He&amp;quot; | &amp;quot;She&amp;quot; | &amp;quot;It&amp;quot; | &amp;quot;We&amp;quot; | &amp;quot;They&amp;quot;| &amp;quot;Me&amp;quot; | &amp;quot;Him&amp;quot; | &amp;quot;Her&amp;quot; | &amp;quot;Us&amp;quot; | &amp;quot;Them&amp;quot; }&lt;&#x2F;code&gt; which contains a pattern that will match any of the listed personal pronouns that is capitalized. This means that it will match the text of &amp;quot;You&amp;quot;, &amp;quot;They&amp;quot; and &amp;quot;Them&amp;quot;, but not &amp;quot;i&amp;quot;, &amp;quot;apple&amp;quot; or &amp;quot;them&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Next let&#x27;s take a look at how the ParrotHearing grammar is organizing the regular expressions. The sentences that that this grammar defines are very basic: they have the form of a personal pronoun, followed by verb, a noun and an ending. In fact, the verbs and nouns that the Parrot accepts are very simplified: they are the pattern &lt;code&gt;\w+&lt;&#x2F;code&gt; that we described above that would match most words. The personal pronouns are those that the regex object &lt;code&gt;{ &amp;quot;I&amp;quot; | &amp;quot;You&amp;quot; | &amp;quot;He&amp;quot; | &amp;quot;She&amp;quot; | &amp;quot;It&amp;quot; | &amp;quot;We&amp;quot; | &amp;quot;They&amp;quot;| &amp;quot;Me&amp;quot; | &amp;quot;Him&amp;quot; | &amp;quot;Her&amp;quot; | &amp;quot;Us&amp;quot; | &amp;quot;Them&amp;quot; }&lt;&#x2F;code&gt; will match while the ending is one of the &lt;code&gt;.&lt;&#x2F;code&gt;, &lt;code&gt;!&lt;&#x2F;code&gt; and &lt;code&gt;?&lt;&#x2F;code&gt; characters.&lt;&#x2F;p&gt;
&lt;p&gt;The final piece is a function that replaces the nouns and verbs in a sentence with the emoji equivalent if it can. If no replacement can be found it leaves the text unchanged. When parsing we can use this function as part of an action to perform. This allows us to tie everything together in our &lt;code&gt;MAIN&lt;&#x2F;code&gt; function (also called a subroutine in Raku). This parses in the text from the command line, prints the text twice (can be done by the &lt;code&gt;say&lt;&#x2F;code&gt; function in Raku). The first time it will print the text and the second time it will print the text with the emoji replacements. In the case when it can not parse the input it will print &lt;code&gt;&amp;quot;Squaaawk can not understand!&amp;quot;&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;p&gt;To run everything, if the code is in a file called main.raku one can call &lt;code&gt;raku main.raku &amp;quot;I love sushi.&amp;quot;&lt;&#x2F;code&gt; or some other sentence to get started. Feel free to give it a try on &lt;a href=&quot;https:&#x2F;&#x2F;replit.com&#x2F;languages&#x2F;raku&quot;&gt;Replit&lt;&#x2F;a&gt; to see what the code parrots back!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Data Oriented Domain Design</title>
        <published>2022-04-05T00:00:00+00:00</published>
        <updated>2022-04-05T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/dodd/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/dodd/</id>
        
        <content type="html">&lt;p&gt;One of the most difficult parts of developing software for a particular task is understanding the (business) domain in which the software needs to operate. This is due to the fact that software engineers often lack the expertise in complex areas such as health, law, finance, etc in which the software they create needs to solve problems. A similar situation holds for domain experts. For example, while lawyers are by definition well versed in the legal domain, it is not necessarily the case that they are also software engineers. This lack of knowledge can make developing software quite difficult. To continue with our legal example, suppose we want to create an application that represents and searches laws. From the software engineers side, the legal terminology can be quite burdensome to understand and it might not be very clear to them what aspects of it would be important for them to implement. On the other hand, it is important to be able to make it understandable to legal experts, without any software engineering knowledge, what the software is doing in relation to the concepts in their domain.&lt;&#x2F;p&gt;
&lt;p&gt;Various solutions have been proposed for this problem, such as &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Domain-driven_design&quot;&gt;Domain Driven Design&lt;&#x2F;a&gt; and formalizing the domain using &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;ontology&quot;&gt;Linked Data Ontologies&lt;&#x2F;a&gt;.
These methodologies can be quite complex, which they need to be in order to capture the nuances of the domain and descriptions of what is possible. This makes the important process of domain formalisation often quite daunting. In this article, I will show a very lightweight approach, that can be applied as a starting point towards formalising the domain. The core idea is to use lightweight data notation languages, such as &lt;a href=&quot;https:&#x2F;&#x2F;www.json.org&#x2F;json-en.html&quot;&gt;JSON&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;edn-format&#x2F;edn&quot;&gt;EDN&lt;&#x2F;a&gt; to represent elements of the domain. The elements of these languages, such as the notations for sequences, key and value pairs, etc would then be used as a common language for both the software engineer and domain expert to explain the problem and how the software is used to solve it.&lt;&#x2F;p&gt;
&lt;p&gt;For the purpose of this article I will call this method Data Oriented Domain Design (DODD). Not coincidentally the subject that we aim to represent are a few elements of the &lt;a href=&quot;https:&#x2F;&#x2F;www.congress.gov&#x2F;bill&#x2F;111th-congress&#x2F;house-bill&#x2F;4173&#x2F;text?r=1&quot;&gt;Dodd-Frank Wall Street Reform and Consumer Protection Act&lt;&#x2F;a&gt;, which we will simply refer as the Dodd-Frank law in this context. &lt;&#x2F;p&gt;
&lt;p&gt;In this article I will make use of the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;edn-format&#x2F;edn&quot;&gt;EDN&lt;&#x2F;a&gt;(Extensible Data Notation) data notation language to illustrate the ideas of this Data Oriented Domain Design (DODD). EDN is a language where information is represented through a set of elements as values. The elements are common to many programming languages. For example here are elements for representing text, time, numbers as well as collections, such as lists, sets, etc. of these elements. &lt;&#x2F;p&gt;
&lt;p&gt;EDN is a subset of the programming language &lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;&quot;&gt;Clojure&lt;&#x2F;a&gt;. A large part of a Clojure program is manipulating information expressed in EDN. Due to this reason Clojure is often called a data-oriented or data-driven language. With Data Oriented Domain Design we are going to use this &amp;quot;data orientation paradigm&amp;quot; not just for organizing the software system but also for creating a common language in which software engineers and domain experts can communicate.&lt;&#x2F;p&gt;
&lt;p&gt;In order to make things more concrete lets imagine a scenario where small application is needed to be built that aims to search for definitions within legal documents. One of the nice aspects of many legal texts is that they often have a section of definitions that the reader can refer to. Our application will aim to retrieve these definitions based on a given criteria. As an example use case, given the acronym DODD of our approach, we will use the text of the &lt;a href=&quot;https:&#x2F;&#x2F;www.congress.gov&#x2F;bill&#x2F;111th-congress&#x2F;house-bill&#x2F;4173&#x2F;text?r=1&quot;&gt;Dodd-Frank Wall Street Reform and Consumer Protection Act&lt;&#x2F;a&gt;, which we will simply refer as the Dodd-Frank law in this context. &lt;&#x2F;p&gt;
&lt;p&gt;I am going to preface this by saying that &amp;quot;I am not a lawyer&amp;quot; and I am looking at this application from a software- and knowledge engineers perspective. I will simplify out a LOT of the intricacies of legal text search and representation. That said as mentioned this perspective should be illustrative of the issues when developing software for such a new domain and how Data Oriented Domain Design could be a good starting point.&lt;&#x2F;p&gt;
&lt;p&gt;The text of this law has multiple sections for definitions, but here in particular we are looking at the first ten definitions outlined in Section 2 of the Dodd-Frank act. &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;SEC. 2. &amp;lt;&amp;lt;NOTE: 12 USC 5301.&amp;gt;&amp;gt; DEFINITIONS.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    As used in this Act, the following definitions shall apply, except 
&lt;&#x2F;span&gt;&lt;span&gt;as the context otherwise requires or as otherwise specifically provided 
&lt;&#x2F;span&gt;&lt;span&gt;in this Act:
&lt;&#x2F;span&gt;&lt;span&gt;            (1) Affiliate.--The term ``affiliate&amp;#39;&amp;#39; has the same meaning 
&lt;&#x2F;span&gt;&lt;span&gt;        as in section 3 of the Federal Deposit Insurance Act (12 U.S.C. 
&lt;&#x2F;span&gt;&lt;span&gt;        1813).
&lt;&#x2F;span&gt;&lt;span&gt;            (2) Appropriate federal banking agency.--On and after the 
&lt;&#x2F;span&gt;&lt;span&gt;        transfer date, the term ``appropriate Federal banking agency&amp;#39;&amp;#39; 
&lt;&#x2F;span&gt;&lt;span&gt;        has the same meaning as in section 3(q) of the Federal Deposit 
&lt;&#x2F;span&gt;&lt;span&gt;        Insurance Act (12 U.S.C. 1813(q)), as amended by title III.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[[Page 124 STAT. 1387]]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;            (3) Board of governors.--The term ``Board of Governors&amp;#39;&amp;#39; 
&lt;&#x2F;span&gt;&lt;span&gt;        means the Board of Governors of the Federal Reserve System.
&lt;&#x2F;span&gt;&lt;span&gt;            (4) Bureau.--The term ``Bureau&amp;#39;&amp;#39; means the Bureau of 
&lt;&#x2F;span&gt;&lt;span&gt;        Consumer Financial Protection established under title X.
&lt;&#x2F;span&gt;&lt;span&gt;            (5) Commission.--The term ``Commission&amp;#39;&amp;#39; means the 
&lt;&#x2F;span&gt;&lt;span&gt;        Securities and Exchange Commission, except in the context of the 
&lt;&#x2F;span&gt;&lt;span&gt;        Commodity Futures Trading Commission.
&lt;&#x2F;span&gt;&lt;span&gt;            (6) Commodity futures terms.--The terms ``futures commission 
&lt;&#x2F;span&gt;&lt;span&gt;        merchant&amp;#39;&amp;#39;, ``swap&amp;#39;&amp;#39;, ``swap dealer&amp;#39;&amp;#39;, ``swap execution 
&lt;&#x2F;span&gt;&lt;span&gt;        facility&amp;#39;&amp;#39;, ``derivatives clearing organization&amp;#39;&amp;#39;, ``board of 
&lt;&#x2F;span&gt;&lt;span&gt;        trade&amp;#39;&amp;#39;, ``commodity trading advisor&amp;#39;&amp;#39;, ``commodity pool&amp;#39;&amp;#39;, and 
&lt;&#x2F;span&gt;&lt;span&gt;        ``commodity pool operator&amp;#39;&amp;#39; have the same meanings as given the 
&lt;&#x2F;span&gt;&lt;span&gt;        terms in section 1a of the Commodity Exchange Act (7 U.S.C. 1 et 
&lt;&#x2F;span&gt;&lt;span&gt;        seq.).
&lt;&#x2F;span&gt;&lt;span&gt;            (7) Corporation.--The term ``Corporation&amp;#39;&amp;#39; means the Federal 
&lt;&#x2F;span&gt;&lt;span&gt;        Deposit Insurance Corporation.
&lt;&#x2F;span&gt;&lt;span&gt;            (8) Council.--The term ``Council&amp;#39;&amp;#39; means the Financial 
&lt;&#x2F;span&gt;&lt;span&gt;        Stability Oversight Council established under title I.
&lt;&#x2F;span&gt;&lt;span&gt;            (9) Credit union.--The term ``credit union&amp;#39;&amp;#39; means a Federal 
&lt;&#x2F;span&gt;&lt;span&gt;        credit union, State credit union, or State-chartered credit 
&lt;&#x2F;span&gt;&lt;span&gt;        union, as those terms are defined in section 101 of the Federal 
&lt;&#x2F;span&gt;&lt;span&gt;        Credit Union Act (12 U.S.C. 1752).
&lt;&#x2F;span&gt;&lt;span&gt;            (10) Federal banking agency.--The term--
&lt;&#x2F;span&gt;&lt;span&gt;                    (A) ``Federal banking agency&amp;#39;&amp;#39; means, individually, 
&lt;&#x2F;span&gt;&lt;span&gt;                the Board of Governors, the Office of the Comptroller of 
&lt;&#x2F;span&gt;&lt;span&gt;                the Currency, and the Corporation; and
&lt;&#x2F;span&gt;&lt;span&gt;                    (B) ``Federal banking agencies&amp;#39;&amp;#39; means all of the 
&lt;&#x2F;span&gt;&lt;span&gt;                agencies referred to in subparagraph (A), collectively.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now lets consider the case of an application searching for definitions in legal text. One of the features we would like to do is that given the exact name of the term, such as &amp;quot;Board of Governors&amp;quot;, &amp;quot;Bureau&amp;quot;, etc, the text of its definition is found. The name of the term can be represented as data with a string of characters. In Clojure, and many other languages, this is denoted as the text between quotation marks (&lt;code&gt;&amp;quot;&amp;quot;&lt;&#x2F;code&gt;). For example, the input for our search can be given as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Board of Governors&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next we have to examine on how to represent the output (the result) of our search. In most cases we would like to have a sequence of results that indicate the found definitions. This is due to a number of reasons. First, a specific term could be defined with multiple definitions over a variety of documents. Second, it provides us a straightforward way to represent the results in the case when there are no results are found. In this case we can return an indicator of a sequence of 0 elements. In Clojure such a sequence of elements can be indicated by elements in between square brackets &lt;code&gt;[]&lt;&#x2F;code&gt;. Other data interchange formats and languages call such ordered sequence by different terms: lists, arrays, etc. Such representations are very common in (programming languages) but for in this article we will stick with the EDN definitions. &lt;&#x2F;p&gt;
&lt;p&gt;In our first version of our program we will just return the sequence of the found texts that describes the definition. To give a concrete example lets assume we search for the term &amp;quot;Board of Governors&amp;quot; in the above partial document. The input for our program is the string of characters indicating this term:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Board of Governors&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and the output would be:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;[ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(3) Board of governors.--The term ``Board of Governors&amp;#39;&amp;#39; means the Board of Governors of the Federal Reserve System.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To show an example where we would not find any definitions in the above text, if we search for the term &amp;quot;Central Bank&amp;quot; in the above fragment using the input&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Central Bank&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we would get an empty sequence as a result:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;[]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Giving examples such as this should already give a good indication on how the code could be structured. Just as importantly, given some information on the basic notation of sequences and strings, legal domain experts could understand the program is aiming to achieve, just by looking at a few of such examples and verify whether we are on the right track.&lt;&#x2F;p&gt;
&lt;p&gt;Of course the above example is a very minimal abstraction. Let&#x27;s try to expand upon it, to capture a few more elements of the legal domain. &lt;&#x2F;p&gt;
&lt;p&gt;The location in which the searched terms are found is also quite important. A program might search legal terms over multiple legal documents, and if found, people would likely want to know exact location in the source material to get more context. This means that we want to represent a few additional values in our search result. For example if the definition was found in a law, we would also want to have the title of the law, the section in which the definition was found, the URL of where the law could be read, etc.&lt;&#x2F;p&gt;
&lt;p&gt;In EDN the format to represent such key-value pairs is called map, represented by pairs of elements between curly brackets: &lt;code&gt;{}&lt;&#x2F;code&gt;. For example if we want to express the term description with a key-value pair, with both the key and value being a string, we could write:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;[{ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(3) Board of governors.--The term ``Board of Governors&amp;#39;&amp;#39; means the Board of Governors of the Federal Reserve System.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In other languages such maps are called as object, record, struct, dictionary, hash table, etc but the general concept is the same. &lt;&#x2F;p&gt;
&lt;p&gt;The main question is of-course what additional elements we want add here and what we would like to name the keys of these elements. To reiterate, &amp;quot;I am not a Lawyer&amp;quot;, but we can look towards the ways of how laws are cited to figure out what additional information would make sense from a legal perspective. Thankfully there are some descriptions online on how to cite laws, such on the site of the &lt;a href=&quot;https:&#x2F;&#x2F;guides.libraries.uc.edu&#x2F;c.php?g=222561&amp;amp;p=1472889&quot;&gt;University of Cincinnati&lt;&#x2F;a&gt; and the &lt;a href=&quot;https:&#x2F;&#x2F;www.law.cornell.edu&#x2F;citation&#x2F;2-300&quot;&gt;Cornell University Law&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Given this information, we will add the terms &amp;quot;title of the act&amp;quot;, &amp;quot;public law number&amp;quot; &amp;quot;statute&amp;quot;, &amp;quot;year of enaction&amp;quot; and &amp;quot;url&amp;quot; of the law where this definition was found to our search result. Below is the expanded example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;[{ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(3) Board of governors.--The term ``Board of Governors&amp;#39;&amp;#39; means the Board of Governors of the Federal Reserve System.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title-of-act&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;H.R.4173 - Dodd-Frank Wall Street Reform and Consumer Protection Act&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;public-law-number&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;111-203&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;statute&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1387&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;year-of-enaction&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2010&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;www.congress.gov&#x2F;bill&#x2F;111th-congress&#x2F;house-bill&#x2F;4173&#x2F;text?r=1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now this might be sufficient, but again there are some features we could use to make this a better representation. First, we use a lot of strings of characters when we ideally want to specify a number or numbers. In EDN, as well as in JSON and other formats, you generally have a bit more precision and safety by expressing these elements as numbers (or more precisely integers). By the program that we intend to build, this helps us automatically invalidate certain wrong values and helps us better describe the intent of what is allowed. For example if we write the string &lt;code&gt;&amp;quot;start&amp;quot;&lt;&#x2F;code&gt; instead of a number such as &#x27;111&#x27; for the law number, this program should handle this by not allowing such scenaros to occur that contradict the rules of the domain. The extra precision allows us to more precisely declare the range of laws, with a number denoting the starting and the ending point with the keys &lt;code&gt;&amp;quot;from&amp;quot;&lt;&#x2F;code&gt; and &lt;code&gt;&amp;quot;to&amp;quot;&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;[{ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(3) Board of governors.--The term ``Board of Governors&amp;#39;&amp;#39; means the Board of Governors of the Federal Reserve System.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title-of-act&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;H.R.4173 - Dodd-Frank Wall Street Reform and Consumer Protection Act&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;public-law-number&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;111 &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;to&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;203&lt;&#x2F;span&gt;&lt;span&gt;} 
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;statute&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1387&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;year-of-enaction&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2010
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;www.congress.gov&#x2F;bill&#x2F;111th-congress&#x2F;house-bill&#x2F;4173&#x2F;text?r=1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We are going to add one additional feature to improve this representation. Note that depending on what we search for, certain the strings representing the values will vary a lot while those for the keys will remain the same. For example the value for the returned description can be &lt;code&gt;&amp;quot;(3) Board of governors.--The term ``Board of Governors&#x27;&#x27; means the Board of Governors of the Federal Reserve System.&amp;quot;&lt;&#x2F;code&gt; if we search for the term &amp;quot;Board of governors&amp;quot;, while it will be &lt;code&gt;&amp;quot;(4) Bureau.--The term ``Bureau&#x27;&#x27; means the Bureau of Consumer Financial Protection established under title X.&amp;quot;&lt;&#x2F;code&gt; if we search for &amp;quot;Bureau&amp;quot;. However the key for both of these values would be &amp;quot;description&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;The solution in EDN is to use keywords, for the commonly used strings. The names of these are prefaced by &lt;code&gt;:&lt;&#x2F;code&gt; instead of putting them in quotation marks. While JSON does not have such keywords, many other languages do. Notably &lt;a href=&quot;https:&#x2F;&#x2F;json-ld.org&#x2F;&quot;&gt;JSON-LD&lt;&#x2F;a&gt; can use &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Uniform_Resource_Identifier&quot;&gt;Uniform Resource Identifiers (URI)&lt;&#x2F;a&gt; for a similar purpose.&lt;&#x2F;p&gt;
&lt;p&gt;Using keywords we can give an output for our definition search as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;[{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:description &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;(3) Board of governors.--The term ``Board of Governors&amp;#39;&amp;#39; means the Board of Governors of the Federal Reserve System.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:title-of-act &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;H.R.4173 - Dodd-Frank Wall Street Reform and Consumer Protection Act&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:public-law-number&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; {&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;from&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: 111 &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;to&amp;quot;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;203&lt;&#x2F;span&gt;&lt;span&gt;} 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:statute&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1387&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;  :year-of-enaction 2010
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;  :url &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;https:&#x2F;&#x2F;www.congress.gov&#x2F;bill&#x2F;111th-congress&#x2F;house-bill&#x2F;4173&#x2F;text?r=1&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As one can see from just these few examples there can be quite a few ways in which domain concepts are represented. A small set of examples, that describe the elements of the domain in a way that is understandable for both the domain expert and the software engineer can be invaluable. The great benefit of Data Oriented Domain Design, is that it co-opts some battle tested light-weight data representation languages, such as EDN, for this purpose. This allows for some excellent test cases to use when developing the software with methodologies such as &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Test-driven_development&quot;&gt;Test Driven Development (TDD)&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;!-- [JSON-LD](https:&#x2F;&#x2F;json-ld.org&#x2F;)  --&gt;
&lt;p&gt;Of course the above approach has some limitations. &lt;&#x2F;p&gt;
&lt;p&gt;A notation such as &lt;a href=&quot;https:&#x2F;&#x2F;json-ld.org&#x2F;&quot;&gt;JSON-LD&lt;&#x2F;a&gt; can be more expressive with representing the domain and is more capable of representing linked data (i.e.: data that is interconnected with other data). However starting to model the domain with explicit links to other data sources can be more complex especially in cases where domain experts formalize the domain for the first time. As JSON-LD is designed to provide a smooth upgrade path from JSON, starting out with a pure JSON based modelling of the domain can be a great initial step.&lt;&#x2F;p&gt;
&lt;p&gt;DODD also explains the domain through a set of examples, but not through a comprehensive set of restrictions that model the domain in a more complete way. However there exist schema languages, such as &lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;guides&#x2F;spec&quot;&gt;Clojure Spec&lt;&#x2F;a&gt; or &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Web_Ontology_Language&quot;&gt;OWL Ontologies&lt;&#x2F;a&gt; that can model more of such restrictions, and could extend validation a lot further.&lt;&#x2F;p&gt;
&lt;p&gt;Another issue is that these lightweight data languages are often designed for the perspective of a software engineer, that most likely is going to utilize them. However with some tooling they could be made more accessible with domain experts, especially as they are relatively straightforward, with fewer elements, compared to more complex representations.&lt;&#x2F;p&gt;
&lt;p&gt;Nonetheless even with these limitations, Data Oriented Domain Design in which a lightweight data notation language is used to help express examples of a domain to both domain experts and software engineers, can provide a relatively gentle start to modelling the domain through a set of examples. Due to its relative simplicity it could be applied as an initial step, before more &amp;quot;heavyweight&amp;quot; models are brought into the picture. It can provide a great tool for some frank discussions on how a software should function in a particular domain. &lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Pharo Prince of Bel-Air</title>
        <published>2021-01-24T00:00:00+00:00</published>
        <updated>2021-01-24T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/pharo-prince/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/pharo-prince/</id>
        
        <content type="html">&lt;p&gt;Recently I have been aiming to explore the &lt;a href=&quot;https:&#x2F;&#x2F;pharo.org&#x2F;&quot;&gt;Pharo&lt;&#x2F;a&gt; programming language. The close combination between the language and the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Integrated_development_environment&quot;&gt;Integrated Development Environment (IDE)&lt;&#x2F;a&gt;, makes it quite unlike most other languages that I have encountered. You can, for example, inspect every program element in a visual environment, even on a currently running program. While exploring, I spent much time using the &lt;code&gt;Playground&lt;&#x2F;code&gt; feature, where you easily start executing, and interacting with, small snippets of code.&lt;&#x2F;p&gt;
&lt;p&gt;While exploring the language, the &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=1nCqRmx3Dnw&quot;&gt;intro-song&lt;&#x2F;a&gt; to the tv-show &amp;quot;&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;The_Fresh_Prince_of_Bel-Air&quot;&gt;The Fresh Prince of Bel-Air&lt;&#x2F;a&gt;&amp;quot; got stuck in my head. The song tells the story of the title character, Will Smith, on how he ended up being &amp;quot;The Fresh Prince of Bel-Air&amp;quot;. To paraphrase the song, Will was born and raised in Philadelphia, spending much of his time on the playground. On one occasion, while playing basketball, he got into a fight with some troublemakers. This experience got his mom scared so he was sent off to live with his aunt and uncle in Bel Air. After a flight and a taxi ride, he arrives in Bel Air at his uncles and aunts house, to become &amp;quot;the Prince of Bel-Air&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;In this article, let’s explore Pharo together with the story from “The Fresh Prince of Bel Air”. If one wants to follow along, feel free to &lt;a href=&quot;https:&#x2F;&#x2F;pharo.org&#x2F;download&quot;&gt;download&lt;&#x2F;a&gt; and setup Pharo. The language has a really smooth process for getting started, as one can download and install the &lt;a href=&quot;https:&#x2F;&#x2F;pharo.org&#x2F;download&quot;&gt;Pharo Launcher&lt;&#x2F;a&gt; and be pretty much set.&lt;&#x2F;p&gt;
&lt;p&gt;It is difficult to do the language justice by text, because it is so intertwined with the IDE and image based development. I will try to illustrate things as best as I can.&lt;&#x2F;p&gt;
&lt;p&gt;Once we have installed and started the Pharo Launcher the first step is to create a Pharo image for our project. One can think of a Pharo image, as a store of all the objects created by the program. This not only includes what would normally be represented as files of source code of other languages, but also the objects of the running program, as well as the state of the editor. As an example, suppose we create an application for taking notes, and while developing, we create two notes to experiment and play around with. We can now save the Pharo image including these objects. The next time we start the image our notes will be there, alongside with the state of the IDE as we have left it. This can help tremendously when developing or debugging a new application.&lt;&#x2F;p&gt;
&lt;p&gt;To come back to our &amp;quot;The Fresh Prince of Bel-Air&amp;quot; example, first we are going to create an image which will contain our project (see a screenshot of this below). &lt;&#x2F;p&gt;
&lt;!-- &lt;figure class=centeredfig&gt;
    &lt;img src=unknownvariable.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;We did not define what we mean by FPWillSmith which brings us some options on what to do now.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
 --&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=imagecreation.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Creating a Pharo image for our &amp;#x27;The Fresh Prince of Bel-Air&amp;#x27; example.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;imagecreation.png&quot; title=&quot;Creating a Pharo image for our \&quot;The Fresh Prince of Bel-Air\&quot; example.&quot; &gt;}} --&gt;
&lt;p&gt;In the Pharo launcher, we can click on the &amp;quot;New&amp;quot; button and get a dialog window for the image that we want to create. We base our image on the last stable Pharo version, which is at the time of this writing version 8.0, enter a name and a short description for our image and click &amp;quot;Create Image&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;In a short order our image is then initialized, and we are greeted in our IDE by a welcome message, and some guides and documentation in a menu. I highly recommend the “ProStef” tutorial from this set of guides.&lt;&#x2F;p&gt;
&lt;p&gt;Our next step is the playground which, as mentioned at the start of the article, is a place for experimentation and playing around with code that we are going to develop. One can open this Playground from the menu &amp;quot;Tools&amp;quot; on the top of the screen by selecting &amp;quot;Playground&amp;quot; or by the shortcut &lt;code&gt;CTRL+O+W&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;Note that the shortcuts might be different depending on settings and operating systems, as the command key might be either &lt;code&gt;CTRL&lt;&#x2F;code&gt;, &lt;code&gt;CMD&lt;&#x2F;code&gt; or &lt;code&gt;ALT&lt;&#x2F;code&gt;. Thankfully the IDE shows shortcuts very neatly next to the clickable menu options, so they should be easy to figure out and learn.&lt;&#x2F;p&gt;
&lt;p&gt;Once the Playground is open we have a canvas where we can start to write and execute our code. &lt;&#x2F;p&gt;
&lt;p&gt;Before I get into the actual creation of the example I will briefly try to summarize how Pharo as a language works. In Pharo everything is an &lt;em&gt;object&lt;&#x2F;em&gt; and any computation is done by sending messages to objects. Creating such objects is done through specific objects known as &lt;em&gt;classes&lt;&#x2F;em&gt;, which define structure and behaviour of the objects they create. &lt;&#x2F;p&gt;
&lt;p&gt;These classes are organized into hierarchies. Hierarchies allow you to reuse behaviour. A class lower in the hierarchy, the so-called &lt;em&gt;subclass&lt;&#x2F;em&gt; can inherit the behaviour of a class higher in the hierarchy, called a &lt;em&gt;superclass&lt;&#x2F;em&gt;. At the root of this hierarchy is the class named &lt;em&gt;Object&lt;&#x2F;em&gt;, which ensures as we noted that anything we create is by definition and behaviour an &lt;em&gt;Object&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We call the objects created by the class as their &lt;em&gt;instances&lt;&#x2F;em&gt;. The &lt;em&gt;methods&lt;&#x2F;em&gt; of an object specify how the messages an object receives should be handled. When an object receives a &lt;em&gt;message&lt;&#x2F;em&gt; it aims to find the method that has the same name as the message to handle it. An &lt;em&gt;object&lt;&#x2F;em&gt; also encapsulates data, such as dates, numbers, strings and other objects, based on what the object represents into variables. It can use this data to help handle the messages it receives. &lt;&#x2F;p&gt;
&lt;p&gt;An object can refer to itself by using the special identifier &lt;em&gt;self&lt;&#x2F;em&gt;. In order to create a new object of a class, the &lt;em&gt;initialize&lt;&#x2F;em&gt; message needs to be sent which gets handled by the &lt;em&gt;initialize&lt;&#x2F;em&gt; method, though often the message &lt;em&gt;new&lt;&#x2F;em&gt; is used as a shorthand for this.&lt;&#x2F;p&gt;
&lt;p&gt;For people with some experience with another &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Object-oriented_programming&quot;&gt;Object Oriented programming language&lt;&#x2F;a&gt; the above description should sound familiar. One thing that stands out for me on a first glance is how consistently the use of objects and message passing is applied within Pharo. &lt;em&gt;Everything&lt;&#x2F;em&gt; is an object and interaction seems to always happen by message passing. The above summary should hopefully be enough to follow the examples below but for a more in depth look I highly recommend the &lt;a href=&quot;https:&#x2F;&#x2F;books.pharo.org&#x2F;learning-oop&#x2F;&quot;&gt;Learning Object-Oriented Programming, Design with TDD in Pharo&lt;&#x2F;a&gt; book.&lt;&#x2F;p&gt;
&lt;!-- To very briefly explain what happens here is to note that Pharo works by passing messages. Here to `go` message was to `ProfStef` to indicate that we want to kick off the tutorial. `ProfStef`  has a method named `go` implemented that defines how to handle this message.  --&gt;
&lt;p&gt;Now given the above description of Pharo, we want to apply it by creating a representation of what happens in the intro song in &amp;quot;The Fresh Prince of Bel-Air&amp;quot; by sending messages. In particular we want to represent the character of &amp;quot;Will Smith&amp;quot; in the intro as an object and the changes in location and activity based on the intro song. &lt;&#x2F;p&gt;
&lt;p&gt;So let&#x27;s start off by creating a new Object representing Will. We aim to do this by instantiating a class called &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt; by typing the following into the Playground.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;will &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= FPWillSmith new.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we want to send the &lt;em&gt;New&lt;&#x2F;em&gt; message to the class &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt; to create a new object of this class and assign it to the variable &lt;code&gt;will&lt;&#x2F;code&gt;. Let us quickly go over the syntax elements for this statement. &lt;&#x2F;p&gt;
&lt;p&gt;The operator &lt;code&gt;:=&lt;&#x2F;code&gt; is used in Pharo for assignment, and in this case to assign our new object instance to the &lt;code&gt;will&lt;&#x2F;code&gt; variable. We also send to class &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt;, functioning as the receiver, the message &lt;code&gt;new&lt;&#x2F;code&gt; to create a new instance of it. Messages like this that are sent to an object without any additional information are called &lt;code&gt;unary&lt;&#x2F;code&gt; messages. These are always in the form of &lt;code&gt;receiver message&lt;&#x2F;code&gt;. Lastly, statements in Pharo are separated&#x2F;ended by a &lt;code&gt;.&lt;&#x2F;code&gt;. This can be omitted in many cases but we keep it around in our examples to show its use.&lt;&#x2F;p&gt;
&lt;p&gt;To execute this statement we can right click it and select &#x27;Do It&#x27; or use the short-cut for this &lt;code&gt;CTRL+D&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;After doing this we will get the following dialog:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=unknownvariable.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;We did not define what we mean by FPWillSmith which brings us some options on what to do now.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;unknownvariable.png&quot; title=&quot;We did not define what we mean by FPWillSmith which brings us some options on what to do now.&quot; &gt;}} --&gt;
&lt;p&gt;Well, whoops it seems we kind of forgot to define our class &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt;, so we get a notification for it. The reason why I am highlighting this error is to show that unlike many other languages, where you would just get an error at either compile or runtime, the system brings up a dialog to help us solve this issue. The only other language where I experienced a similar dialog was with &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;common-treasure&#x2F;&quot;&gt;CommonLisp&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So lets click &amp;quot;Define a new class&amp;quot; and give a class definition as follows:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=classcreation.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;We define a class FPWillSmith, to ensure that every object of this class holds some data about the object&amp;#x27;s name, location and activity.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;classcreation.png&quot; title=&quot;We define a class FPWillSmith, to ensure that every object of this class holds some data about the object&#x27;s name, location and activity.&quot; &gt;}} --&gt;
&lt;p&gt;A few things to note for this code in this picture: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;Object &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;subclass:&lt;&#x2F;span&gt;&lt;span&gt; #FPWillSmith
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;instanceVariableNames: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;name &lt;&#x2F;span&gt;&lt;span&gt;location activity&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39; 
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;classVariableNames: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;category:&lt;&#x2F;span&gt;&lt;span&gt; FreshPrinceDemo
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Much like every other part of Pharo, the creation of a new class also happens through message passing. Here a keyword message is sent to the &lt;code&gt;Object&lt;&#x2F;code&gt; class to define our new class. Unlike unary messages, these messages use one or more keywords to send additional object information along with the message. These are defined in the form of &lt;code&gt;receiver keyword1: object1 keyword2: object2 ...&lt;&#x2F;code&gt;  where the keyword and object pairs make up the message to the receiving object.&lt;&#x2F;p&gt;
&lt;p&gt;We will go over the objects sent in the message one by one. The &lt;code&gt;subclass&lt;&#x2F;code&gt; part of the message defines the name of our new class &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt;. The &lt;code&gt;#&lt;&#x2F;code&gt; in front of &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt; indicates a symbol that is globally unique, unlike a string, which is part of the requirement for this subclass message. Next is defining the name of the instance variables using the keyword &lt;code&gt;instanceVariablesNames&lt;&#x2F;code&gt;, that will represent the elements of an instance of &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt; that we would like to use. There are also &lt;code&gt;classVariableNames&lt;&#x2F;code&gt; that describe such elements for the class itself as opposed to the instances, but here we do not define any. Finally, there is a &lt;code&gt;category&lt;&#x2F;code&gt; defined as &lt;code&gt;FreshPrinceDemo&lt;&#x2F;code&gt;, that is essentially the name of the package that we are currently defining.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have created our &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt; class, we can now take our original statement:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;will &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= FPWillSmith new.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and execute it, by right clicking it and selecting &#x27;Do It&#x27;, or using a shortcut such as &lt;code&gt;CTRL+D&lt;&#x2F;code&gt;. As you can see, we now don’t receive errors.&lt;&#x2F;p&gt;
&lt;p&gt;What we would like to do is to double check, whether the object we created is what we want. Pharo has a really neat feature for inspecting &lt;em&gt;any&lt;&#x2F;em&gt; object. We can right-click this statement in the playground and select &#x27;Inspect It&#x27; from the menu, or using the shortcut &lt;code&gt;CTRL+I&lt;&#x2F;code&gt; for giving us the object inspector. This should give us a window such as this:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=inspector.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Inspecting our newly created object.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;inspector.png&quot; title=&quot;Inspecting our newly created object.&quot; &gt;}} --&gt;
&lt;p&gt;As we can see from the inspector, while the object has all the instance variables that we wanted (name, location, activity), the value for this is &lt;code&gt;nil&lt;&#x2F;code&gt; which is not what we want.&lt;&#x2F;p&gt;
&lt;p&gt;In order to make sure there are meaningful values for these variables for a new object, we need to define our own &lt;code&gt;initialize&lt;&#x2F;code&gt; method that will get called for every new object.&lt;&#x2F;p&gt;
&lt;p&gt;First we are going to look up our class in the code browser. This can be done by clicking the menu options Tools and then Systems Browser, or by using the short-cut &lt;code&gt;CTRL+O+B&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=systembrowser.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The System Browser, where we filtered for our new Package and selected our `FPWillSmith` class , showing the template for a new method.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;systembrowser.png&quot; title=&quot;The System Browser, where we filtered for our new Package and selected our `FPWillSmith` class , showing the template for a new method.&quot; &gt;}} --&gt;
&lt;p&gt;The system browser can be used to find, organize and edit classes and methods for &lt;em&gt;all&lt;&#x2F;em&gt; packages within the Pharo image. It is divided up into four columns. The leftmost column lists the packages. We can use this to filter for our new package named &#x27;FreshPrinceDemo&#x27;. The second column on the left details the classes in the selected package. For our new package we should only have one class here: &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt;. The third column lists all the &lt;em&gt;protocols&lt;&#x2F;em&gt; of the selected class. Protocols are a way to group together related methods, which makes for easier browsing and search. Finally the fourth column from the left lists all the methods, for the given class or selected protocol. &lt;&#x2F;p&gt;
&lt;p&gt;The panel below these four columns is also very interesting. If one selects a package, it shows in a tab a template for defining a class. If one selects a class, it shows a tab for a template on how to define new methods. It also has a tab for the class definition itself .&lt;&#x2F;p&gt;
&lt;p&gt;In order to proceed let&#x27;s use the method definition template to define our new constructor, as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;initialize
&lt;&#x2F;span&gt;&lt;span&gt;	name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;Will &lt;&#x2F;span&gt;&lt;span&gt;Smith&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;.
&lt;&#x2F;span&gt;&lt;span&gt;	location &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;West &lt;&#x2F;span&gt;&lt;span&gt;Philadelphia playground&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;.
&lt;&#x2F;span&gt;&lt;span&gt;	activity &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;Playing &lt;&#x2F;span&gt;&lt;span&gt;basketball&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which we can fill into the new instance side method window below, instead of the template code.&lt;&#x2F;p&gt;
&lt;p&gt;What this definition does is to create a constructor, that sets the values for name, location and activity to values that makes sense, given where Will is and what he is doing in the beginning of the &amp;quot;The Fresh Prince of Bel-Air&amp;quot; intro song.&lt;&#x2F;p&gt;
&lt;p&gt;Make sure to accept these values, using right click on the panel and selecting Accept or by pressing the shortcut &lt;code&gt;CTRL+S&lt;&#x2F;code&gt; (you might also need to save your name as the author for this piece of code in a dialog along the way).&lt;&#x2F;p&gt;
&lt;p&gt;We will now have the initialization method defined:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=browserinitdefined.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The System Browser showing our newly defined constructor method.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;browserinitdefined.png&quot; title=&quot;The System Browser showing our newly defined constructor method.&quot; &gt;}} --&gt;
&lt;p&gt;We can check whether our new constructor has worked by inspecting the statement we initially created in the Playground:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=inspectorafterconstructor.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Inspecting the new instance after saving our new constructor.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;inspectorafterconstructor.png&quot; title=&quot;Inspecting the new instance after saving our new constructor.&quot; &gt;}} --&gt;
&lt;p&gt;Now the &lt;code&gt;will&lt;&#x2F;code&gt; representation of the character has the right name, location and activity, but we want to express a bit more of the intro song. We will do this by sending messages to the &lt;code&gt;will&lt;&#x2F;code&gt; object after which we expect that the location and the activity changes accordingly.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s assume we instead have the following in the Playground:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;will &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= FPWillSmith new.
&lt;&#x2F;span&gt;&lt;span&gt;will getsInTrouble.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As we can now expect, executing (&#x27;Doing It&#x27;) of the second statement will not go well as we did not yet define a way for the object to handle this message. In fact this message will bring up the debugger because the object does not know how to handle the message &lt;code&gt;getsInTrouble&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=getsintrouble.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;We go into the Debugger when the object can not handle the getsInTrouble message.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;getsintrouble.png&quot; title=&quot;We go into the Debugger when the object can not handle the getsInTrouble message.&quot; &gt;}} --&gt;
&lt;p&gt;We can fix this by defining a method, named &lt;code&gt;getsInTrouble&lt;&#x2F;code&gt;. We can even do this from the debugger by clicking on the &lt;code&gt;Create&lt;&#x2F;code&gt; button at the top of the debugger to define &lt;code&gt;getsInTrouble&lt;&#x2F;code&gt; method for the &lt;code&gt;FPWillSmith&lt;&#x2F;code&gt; class. After getting a popup for picking a protocol, which we do not need to do for this tutorial, we get the window showing a placeholder definition for the method:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;getsInTrouble
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt; shouldBeImplemented.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We want to replace this with the following definition, that updates the activity of the object:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;getsInTrouble
&lt;&#x2F;span&gt;&lt;span&gt;	activity &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;In &lt;&#x2F;span&gt;&lt;span&gt;trouble&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=proceed.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The getsInTrouble method defined in the debugger, after which we can proceed, with the tool-tip of the proceed button highlighted.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;proceed.png&quot; title=&quot;The getsInTrouble method defined in the debugger, after which we can proceed, with the tool-tip of the proceed button highlighted.&quot; &gt;}} --&gt;
&lt;p&gt;We have now created the method from inside the debugger, while the execution of the code was put on hold! With the definition saved we can even click the &lt;code&gt;Proceed&lt;&#x2F;code&gt; button on the top of the debugger to continue from where we halted.&lt;&#x2F;p&gt;
&lt;p&gt;Now the code correctly executes and when we next inspect the last statement have the activity properly updated.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=introubleinspected.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The activity now properly updates on the object after receiving the getsInTrouble message.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;introubleinspected.png&quot; title=&quot;The activity now properly updates on the object after receiving the getsInTrouble message.&quot; &gt;}} --&gt;
&lt;p&gt;For the final piece we will define a method for sending the object to aunt and uncle, with the following definition:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;sentToAuntAndUncle
&lt;&#x2F;span&gt;&lt;span&gt;	activity &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;Being &lt;&#x2F;span&gt;&lt;span&gt;the Prince of Bel-Air&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;.
&lt;&#x2F;span&gt;&lt;span&gt;	location &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;Bel-Air&amp;#39;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If one follows along, we will leave it up to you on where to define this method from, the debugger or the system browser. &lt;&#x2F;p&gt;
&lt;p&gt;Once we have defined it we can put the following in the Playground:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;smalltalk&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-smalltalk &quot;&gt;&lt;code class=&quot;language-smalltalk&quot; data-lang=&quot;smalltalk&quot;&gt;&lt;span&gt;will &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;= FPWillSmith new.
&lt;&#x2F;span&gt;&lt;span&gt;will getsInTrouble.
&lt;&#x2F;span&gt;&lt;span&gt;will sentToAuntAndUncle.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and inspect our object:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=endinspect.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The object now shows the new updated activity as &amp;#x27;Being the Prince of Bel-Air&amp;#x27; in the location &amp;#x27;Bel-Air&amp;#x27;.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2021&#x2F;pharo-prince&#x2F;endinspect.png&quot; title=&quot;The object now shows the new updated activity as \&quot;Being the Prince of Bel-Air\&quot; in the location \&quot;Bel-Air\&quot;.&quot; &gt;}} --&gt;
&lt;p&gt;That is it for this short tutorial. I hope I have shown a small glimpse of why Pharo is such an interesting language.&lt;&#x2F;p&gt;
&lt;p&gt;We have started with no definitions at all, and slowly built up what we wanted to do in a very organic way. We just started putting the statements down in the Playground for passing messages to objects, and created the definitions as we went along. Whenever something has given an error in the debugger or did not show up to our liking in the inspector, we slowly added more code. In the process we have evolved an object that initially represented no aspect of Will&#x27;s character, to something that had a name, location and activity, including; “Playing basketball” in a “West Philadelphia playground”. &lt;&#x2F;p&gt;
&lt;p&gt;And at the end we are now finally there,
having the object&#x27;s activity denoted as &lt;code&gt;Being the Prince of Bel-Air&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Meditations in your Pocket</title>
        <published>2020-12-28T00:00:00+00:00</published>
        <updated>2021-01-06T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/meditations/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/meditations/</id>
        
        <content type="html">&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Meditations&quot;&gt;Meditations&lt;&#x2F;a&gt; are a series of notes that the Roman emperor &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Marcus_Aurelius&quot;&gt;Marcus Aurelius&lt;&#x2F;a&gt;
wrote for guidance and self-improvement. While it is likely that the emperor wrote them purely for his own benefit, they have been published after his death and contain elements of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Stoicism&quot;&gt;Stoic philosophy&lt;&#x2F;a&gt; that many people have consulted since.&lt;&#x2F;p&gt;
&lt;p&gt;One way achieve a bit of self-improvement, coding-wise, is to pick a small project to do with a new set of tools. In particular, I have found the &lt;a href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;&quot;&gt;Flutter&lt;&#x2F;a&gt; framework for creating mobile apps very interesting. Unfortunately I did not have the opportunity to explore Flutter much beyond running some &lt;a href=&quot;https:&#x2F;&#x2F;flutter.github.io&#x2F;samples&#x2F;#&quot;&gt;sample apps&lt;&#x2F;a&gt; previously. To bring a change to this I thought it would be a nice small project to create a small app for browsing the text of &amp;quot;Meditations&amp;quot; in Flutter. I aim to show what I have built in this article, organize my thoughts, and hopefully help others unfamiliar with Flutter to get a small glimpse of what it can do.&lt;&#x2F;p&gt;
&lt;p&gt;To give a preview of the implemented functionality, here is an animation of the functionality of the app:&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=meditationsapp.gif&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Navigating the text of Meditations in the app running on an Android emulator.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2020&#x2F;meditations&#x2F;meditationsapp.gif&quot; title=&quot;Navigating the text of Meditations in the app running on an Android emulator.&quot; &gt;}} --&gt;
&lt;p&gt;The full code of the application presented in this article is &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;meditations&quot;&gt;available&lt;&#x2F;a&gt; to explore. If one wants to follow along by running the code, or has a similar project in mind, the first step is to install &lt;a href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;&quot;&gt;Flutter&lt;&#x2F;a&gt;. There are quite a few dependencies needed for mobile development, but thankfully there is some excellent &lt;a href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;docs&quot;&gt;documentation&lt;&#x2F;a&gt; available on how to get started from the official Flutter site. For code editing I highly recommend &lt;a href=&quot;https:&#x2F;&#x2F;code.visualstudio.com&#x2F;&quot;&gt;Visual Studio Code&lt;&#x2F;a&gt; with the Flutter plugin, but other options are also &lt;a href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;docs&#x2F;get-started&#x2F;editor?tab=androidstudio&quot;&gt;available&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If everything is installed correctly the &lt;code&gt;flutter&lt;&#x2F;code&gt; command line tool will be available. For this project I intended to create an Android app, but the framework allows for app development for iOS (Apple) phones, as well as desktop and web targets. For Android development, aside from the development toolchain, it is probably good idea to have either an emulator or a device for Android development setup and connected. This allows us to the result of the code up and running. In fact, on of the biggest draws of the Flutter framework is &lt;a href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;docs&#x2F;development&#x2F;tools&#x2F;hot-reload&quot;&gt;Hot Reload&lt;&#x2F;a&gt;, which allows on to make changes and quickly see the results in the running app, without reinstalling everything from scratch.&lt;&#x2F;p&gt;
&lt;p&gt;To get started with a fresh project, the command &lt;code&gt;flutter create&lt;&#x2F;code&gt; will create a directory with all the necessary files, while &lt;code&gt;flutter run&lt;&#x2F;code&gt; will ensure that the project is run on the emulator or connected device for testing, debugging and seeing our work. For the &amp;quot;Meditations&amp;quot; app, we could start by creating a new project named &lt;code&gt;Meditations&lt;&#x2F;code&gt; and making changes from there on.&lt;&#x2F;p&gt;
&lt;p&gt;The default generated project will create a bunch of files and directories required for an example app, which is a very small application that has a single button and a screen that shows how many times that button has been pressed (see the &lt;a href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;docs&#x2F;get-started&#x2F;test-drive?tab=terminal&quot;&gt;test drive&lt;&#x2F;a&gt; part of the Flutter docs). The core part are the files &lt;code&gt;main.dart&lt;&#x2F;code&gt; and &lt;code&gt;widget_test.dart&lt;&#x2F;code&gt;, in the &lt;code&gt;lib&lt;&#x2F;code&gt; and &lt;code&gt;test&lt;&#x2F;code&gt; directories respectively. The former is responsible for defining and running the app, while the later exists for testing the functionality.&lt;&#x2F;p&gt;
&lt;p&gt;In order to get the Meditations app up and running, we will have four files in total. Similarly to the default project we have a &lt;code&gt;main.dart&lt;&#x2F;code&gt; and a &lt;code&gt;widget_test.dart&lt;&#x2F;code&gt; for defining and testing our app, but with different contents. In addition, we also have a &lt;code&gt;meditations.dart&lt;&#x2F;code&gt; file where we define how to search and navigate the text of the Meditations, as well as the file &lt;code&gt;meditations_test.dart&lt;&#x2F;code&gt; to test this search and navigation functionality. &lt;&#x2F;p&gt;
&lt;p&gt;Although we do not have to go full &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Test-driven_development&quot;&gt;Test Driven Development (TDD)&lt;&#x2F;a&gt; for such a small demo application, it is a good idea and practice to start off writing some tests to define what behavior we want to show.&lt;&#x2F;p&gt;
&lt;p&gt;We first start describing our application by walking through (some of) the contents of &lt;code&gt;meditations_test.dart&lt;&#x2F;code&gt;. It also gives us an opportunity to ease us into the &lt;a href=&quot;https:&#x2F;&#x2F;dart.dev&#x2F;&quot;&gt;Dart language&lt;&#x2F;a&gt; that the Flutter framework uses, in case if the reader is unfamiliar with it. It would be too much for this short article to fully explain the Dart language, for which there are some excellent &lt;a href=&quot;https:&#x2F;&#x2F;dart.dev&#x2F;guides&quot;&gt;guides&lt;&#x2F;a&gt;, but I hope I can make the functionality clear enough, even for readers completely new to it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;package:meditations&#x2F;mediations.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;package:test&#x2F;test.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first portion is importing elements that we are going to test, to be defined in the &lt;code&gt;mediations.dart&lt;&#x2F;code&gt; file, and functionality for defining tests, which is in the &lt;code&gt;test&lt;&#x2F;code&gt; package.&lt;&#x2F;p&gt;
&lt;p&gt;Next up are the tests that are in the &lt;code&gt;main&lt;&#x2F;code&gt; method. As in some other languages, the main method is where the app execution starts. In the case of testing, the tests within the &#x27;main&#x27; method are executed. The tests themselves contain information about the purpose of the test, the returned value (or values) of the functionality under test and the expected value of what we believe should be returned.&lt;&#x2F;p&gt;
&lt;p&gt;Below are some examples of such tests:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Checking the existance of a Book that exists.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBook&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Checking the existance of a Book that can not exist.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBook&lt;&#x2F;span&gt;&lt;span&gt;(-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Checking the existance of a Book that does not exist.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBook&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;40&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Checking the existance of a Book Section that exists.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBookSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Checking the existance of a Book Section that can not exist.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBookSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Checking the existance of a Book Section in a non-existent Book.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBookSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;40&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Checking the existance of a Book Section that does not exist in an existing Book.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBookSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;40&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to understand what these tests want to achieve, first we need to dive deeper about what functionality that is implemented in &lt;code&gt;Meditations.dart&lt;&#x2F;code&gt; which we intend to test. The purpose of it is to easily find a particular part in the text of &#x27;Meditations&#x27;. &#x27;Meditations&#x27; itself is divided into a total of 12 books, each with multiple sections of text. Instead of showing the full text of all books on a single screen we want to make the user able to navigate between the text of each of these sections. &lt;&#x2F;p&gt;
&lt;p&gt;Part of implementing this navigation process requires having the ability to check, for a given book number, whether that book exists. As in Dart indexes start at 0, the method &lt;code&gt;existBook&lt;&#x2F;code&gt; should return &lt;code&gt;true&lt;&#x2F;code&gt; for any integer from 0 to 11, and false otherwise. Later on we will show how we will turn the book and section numbers back to 1-indexed ones in the UI of the app. Similarly to checking the existence of a book, we want to able to check if a specific section in a specific book exists, which the method &lt;code&gt;existBookSection&lt;&#x2F;code&gt; should provide.&lt;&#x2F;p&gt;
&lt;p&gt;Of course the functionality should not end here. In particular we also want to be able to get the text of the first and last sections of Meditations. In order to keep the return of these search results simple, we also return the book and section numbers (0-indexed) along with the text where possible as well, even though for the first section, we should already know the book and section numbers. &lt;&#x2F;p&gt;
&lt;p&gt;As Dart is an object oriented language, we create a specific object of the class &lt;code&gt;BookSectionText&lt;&#x2F;code&gt; to hold these elements (more on the definition of this class a bit later).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Getting the first part of Meditations returns Book 1, Section 1.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getFirst&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; firstText = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.book1.first;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedBookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedSectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.bookNr, expectedBookNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.sectionNr, expectedSectionNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.text, firstText);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Getting the last part of Meditations returns last Section of the last Book.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getLast&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; lastText = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books.last.last;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedBookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books.length - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedSectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books.last.length - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.bookNr, expectedBookNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.sectionNr, expectedSectionNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.text, lastText);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The final set of tests we are going over are for the methods for finding the previous section and the next one. This seems generally a straightforward thing to do, when moving to the previous or next section in the same book. However for convenience we also want to move to the next, or previous, book if the these are unavailable in the current book, but exist in another one. So the functions for &lt;code&gt;nextSection&lt;&#x2F;code&gt; and &lt;code&gt;previousSection&lt;&#x2F;code&gt; has to have this &#x27;roll-over&#x27; functionality included which will make for a nicer user interface. However even with roll-over, there are cases when we ran out of sections to show (e.g.: when wanting to get the next section after the last one). In this case we use an object of the class &lt;code&gt;NoSuchBookSection&lt;&#x2F;code&gt; to indicate this.&lt;&#x2F;p&gt;
&lt;p&gt;Below are some tests for the &lt;code&gt;nextSection&lt;&#x2F;code&gt; and &lt;code&gt;previousSection&lt;&#x2F;code&gt; functionality:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Getting the next Section of a Book.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getNextSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedBookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedSectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedText = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books[expectedBookNr][expectedSectionNr];
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.bookNr, expectedBookNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.sectionNr, expectedSectionNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.text, expectedText);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Getting the next Section of a Book when not available.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getNextSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Getting the next Section in the next Book&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getNextSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedBookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedSectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedText = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books[expectedBookNr][expectedSectionNr];
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.bookNr, expectedBookNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.sectionNr, expectedSectionNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.text, expectedText);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Getting the previous Section of a Book.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getPreviousSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedBookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedSectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedText = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books[expectedBookNr][expectedSectionNr];
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.bookNr, expectedBookNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.sectionNr, expectedSectionNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.text, expectedText);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Getting the previous Section of a Book when not available.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getPreviousSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Getting the previous Section in the previous Book&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, () {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getPreviousSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedBookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedSectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; expectedText = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books[expectedBookNr][expectedSectionNr];
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.bookNr, expectedBookNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.sectionNr, expectedSectionNr);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(bst.text, expectedText);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we got the tests for the core functionality of navigating the text of Meditations done. Next we will go over the actual implementation of what we aim to test in the file &lt;code&gt;meditations.dart&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;First off are the class definitions for the classes that we mentioned above:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; {}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; This represents the result of search, which results in a text with the attached book and section number.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; bookNr;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; sectionNr;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt; text;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; This represent the result of a search, in which case no section is found.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; {}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the reader is familiar with an object oriented language, the definitions should be reasonably straightforward. The search results are represented by two types of classes that we used in our tests. An instance of a class &lt;code&gt;NoSuchBookSection&lt;&#x2F;code&gt; represents that no valid text could be returned and an instance &lt;code&gt;BookSectionText&lt;&#x2F;code&gt; represents the search results that returned the text of a specific section and its book&#x2F;section numbers. These are both subclasses of the class &lt;code&gt;BookSectionSearchResult&lt;&#x2F;code&gt; which allows us easily define the return type of the methods &lt;code&gt;getPreviousSection&lt;&#x2F;code&gt; and &lt;code&gt;getNextSection&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Also note that the code here contains single line comments which are denoted after a &lt;code&gt;&#x2F;&#x2F;&lt;&#x2F;code&gt; in Dart.&lt;&#x2F;p&gt;
&lt;p&gt;Next up is the definition of the &lt;code&gt;Meditations&lt;&#x2F;code&gt; class that describes the text of Meditations by Marcus Aurelius and how to access them.&lt;&#x2F;p&gt;
&lt;p&gt;It starts with the snippet:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static final&lt;&#x2F;span&gt;&lt;span&gt; book1 = [
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;&amp;#39;&amp;#39;I. Of my grandfather Verus I have learned to be gentle and meek, ... 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;    &#x2F;&#x2F; End of snippet. 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course quoting the full text of the representation is too much for this article. Even in the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;meditations&quot;&gt;code of this demo app&lt;&#x2F;a&gt; we only represent the full text of the first and last books in detail at the moment, although the structure to implement every section exists. &lt;&#x2F;p&gt;
&lt;p&gt;However we can say we represent the text as a list of books, where each book is itself a list of sections which are represented by the text of these sections.&lt;&#x2F;p&gt;
&lt;p&gt;In Dart, we can represent a list as a set of items in between square brackets, i.e. &lt;code&gt;[]&lt;&#x2F;code&gt;, such as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static final&lt;&#x2F;span&gt;&lt;span&gt; books = [
&lt;&#x2F;span&gt;&lt;span&gt;    book1,
&lt;&#x2F;span&gt;&lt;span&gt;    book2,
&lt;&#x2F;span&gt;&lt;span&gt;    book3,
&lt;&#x2F;span&gt;&lt;span&gt;    book4,
&lt;&#x2F;span&gt;&lt;span&gt;    book5,
&lt;&#x2F;span&gt;&lt;span&gt;    book6,
&lt;&#x2F;span&gt;&lt;span&gt;    book7,
&lt;&#x2F;span&gt;&lt;span&gt;    book8,
&lt;&#x2F;span&gt;&lt;span&gt;    book9,
&lt;&#x2F;span&gt;&lt;span&gt;    book10,
&lt;&#x2F;span&gt;&lt;span&gt;    book11,
&lt;&#x2F;span&gt;&lt;span&gt;    book12
&lt;&#x2F;span&gt;&lt;span&gt;  ];
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The other interesting part is that we use the keywords &lt;code&gt;static&lt;&#x2F;code&gt; and &lt;code&gt;final&lt;&#x2F;code&gt;. The &lt;code&gt;static&lt;&#x2F;code&gt; keyword indicates that the variables are class-wide, as the text in these books would be same for any instance of the &lt;code&gt;Meditations&lt;&#x2F;code&gt; class. The &lt;code&gt;final&lt;&#x2F;code&gt; keyword indicates that we do not intend to change these variables once they are defined, as the text of Meditations will not be changing in our app. &lt;&#x2F;p&gt;
&lt;p&gt;The final piece of our &lt;code&gt;Meditations&lt;&#x2F;code&gt; class are methods for accessing and navigating the texts that we described previously in our tests, such as &lt;code&gt;getFirst&lt;&#x2F;code&gt;, &lt;code&gt;getLast&lt;&#x2F;code&gt;, &lt;code&gt;getPreviousSection&lt;&#x2F;code&gt; and &lt;code&gt;getNextSection&lt;&#x2F;code&gt;. Hopefully following them should be reasonably straightforward given the comments and our tests. The only additional thing to note that these methods are also marked as &lt;code&gt;static&lt;&#x2F;code&gt;. As the &lt;code&gt;Meditations&lt;&#x2F;code&gt; class does not contain any variables that would change per instance, every one of these methods could be made available at a class level (e.g. it could be called as &lt;code&gt;Meditations.getNextSection(11, 3)&lt;&#x2F;code&gt;). &lt;&#x2F;p&gt;
&lt;p&gt;See the text of these methods below:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; We assume that there is always at least one book in the list of books and at least one section in each book with a text.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Checks if the book with given book number exists.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;bool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBook&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; bookNr) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (bookNr &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;|| bookNr &amp;gt; books.length - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Checks if the section with given book and section numbers exists.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;bool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBookSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; bookNr, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; sectionNr) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBook&lt;&#x2F;span&gt;&lt;span&gt;(bookNr)) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; selectedBook = books[bookNr];
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (sectionNr &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;|| sectionNr &amp;gt; selectedBook.length - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Returns the length of a book, in the number of sections.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;bookLength&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; bookNr) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBook&lt;&#x2F;span&gt;&lt;span&gt;(bookNr)) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; selectedBook = books[bookNr];
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; selectedBook.length;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Returns the search result for a given book and section number.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getText&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; bookNr, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; sectionNr) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;existBookSection&lt;&#x2F;span&gt;&lt;span&gt;(bookNr, sectionNr)) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;      bst.bookNr = bookNr;
&lt;&#x2F;span&gt;&lt;span&gt;      bst.sectionNr = sectionNr;
&lt;&#x2F;span&gt;&lt;span&gt;      bst.text = books[bookNr][sectionNr];
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; bst;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Returns the first section.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getFirst&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; selectedBook = books[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (selectedBook == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; selectedSection = selectedBook[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (selectedSection == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;      bst.bookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      bst.sectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      bst.text = selectedSection;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; bst;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Returns the last section.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getLast&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; selectedBook = books.last;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (selectedBook == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; selectedSection = selectedBook.last;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (selectedSection == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; bst = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;      bst.bookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books.length - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      bst.sectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.books.last.length - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      bst.text = selectedSection;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; bst;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Given a book- and a section number, gets the next section.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getNextSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; bookNr, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; sectionNr) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;Get the next section in the current book
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; nextSection = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getText&lt;&#x2F;span&gt;&lt;span&gt;(bookNr, sectionNr + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;If it exits return it
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (!(nextSection is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;)) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; nextSection;
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;Otherwise get the first section of the next book
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getText&lt;&#x2F;span&gt;&lt;span&gt;(bookNr + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Given a book- and a section number, gets the previous section.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getPreviousSection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; bookNr, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; sectionNr) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;Get the previous section in the current book
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; previousSection = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getText&lt;&#x2F;span&gt;&lt;span&gt;(bookNr, sectionNr - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;If it exits return it
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (!(previousSection is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;NoSuchBookSection&lt;&#x2F;span&gt;&lt;span&gt;)) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; previousSection;
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;Otherwise get the last section of the previous book
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; previousBookNr = bookNr - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getText&lt;&#x2F;span&gt;&lt;span&gt;(previousBookNr, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;bookLength&lt;&#x2F;span&gt;&lt;span&gt;(previousBookNr) - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have now covered two of the four main files responsible for the functionality of our app, notably the functionality to navigate the text of Meditations and the tests for them. The purpose of the other two is to create the app using this functionality for navigating the text with the Flutter framework (&lt;code&gt;main.dart&lt;&#x2F;code&gt;) and the tests for the app (&lt;code&gt;widget_test.dart&lt;&#x2F;code&gt;). &lt;&#x2F;p&gt;
&lt;p&gt;Let us start with the test first. One of the nice features of the Flutter framework is that one can test the full app nearly as easily than any other Dart code. &lt;&#x2F;p&gt;
&lt;p&gt;First we start importing the functionality for testing, as well as our main app that we aim to test:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;package:flutter_test&#x2F;flutter_test.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;package:meditations&#x2F;main.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next up is the main method with the functionality that we aim to test. This functionality is similar to the functionality that we have shown previously of our app, seen here again below:&lt;&#x2F;p&gt;
&lt;p&gt;{{&amp;lt; figure src=&amp;quot;&#x2F;img&#x2F;post&#x2F;2020&#x2F;meditations&#x2F;meditationsapp.gif&amp;quot; title=&amp;quot;Navigating the text of Meditations in the app running on an Android emulator.&amp;quot; &amp;gt;}}&lt;&#x2F;p&gt;
&lt;p&gt;In particular when we are navigating the text of Meditations, we tap the icons for first, previous, next and last in the bottom navigation bar. &lt;&#x2F;p&gt;
&lt;p&gt;On the screen we not only navigate to the right text, but we also show the number of the book and section of the text that we are currently reading. 
We use this information to create a navigation test, as once the right icon has been tapped, we should be able to predict and check the book and section numbers that should now be on screen. &lt;&#x2F;p&gt;
&lt;p&gt;The test is all tied together with the test functionality of Flutter for setting up the widgets (the elements of interaction) of the app for testing, redrawing the elements after a (simulated) interaction and checking them (more documentation on testing Flutter widgets can be found in the &lt;a href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;docs&#x2F;cookbook&#x2F;testing&#x2F;widget&#x2F;introduction&quot;&gt;Flutter documentation&lt;&#x2F;a&gt;). The code of this for the app can be seen as below.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;testWidgets&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Bottom navigation smoke test&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;WidgetTester&lt;&#x2F;span&gt;&lt;span&gt; tester) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;async&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Build the app and trigger a frame.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pumpWidget&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;MyApp&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Verify that the title shows Book 1 and Section 1.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 1 Section 1&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsOneWidget);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Tap the icon for next and trigger a frame.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;tap&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;byIcon&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span&gt;.navigate_next));
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pump&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Verify that the title has changed correctly.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 1 Section 2&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsOneWidget);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 1 Section 1&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsNothing);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Tap the icon for last and trigger a frame.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;tap&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;byIcon&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span&gt;.last_page));
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pump&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Verify that the title has changed correctly.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 12 Section 26&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsOneWidget);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 1 Section 2&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsNothing);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Tap the icon for previous and trigger a frame.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;tap&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;byIcon&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span&gt;.navigate_before));
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pump&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Verify that the title has changed correctly.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 12 Section 25&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsOneWidget);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 12 Section 26&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsNothing);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Tap the icon for first and trigger a frame.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;tap&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;byIcon&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span&gt;.first_page));
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;await&lt;&#x2F;span&gt;&lt;span&gt; tester.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pump&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Verify that the title has changed correctly.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 1 Section 1&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsOneWidget);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(find.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations Book 12 Section 25&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), findsNothing);
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now all that remains is to show how the actual app is put together. We will go through the code of it here, found in &lt;code&gt;main.dart&lt;&#x2F;code&gt;, step by step. &lt;&#x2F;p&gt;
&lt;p&gt;First we start with the imports of the widget library we are using as well as our implementation of accessing the text of Meditations.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;mediations.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next is, the very short main method, that ensures the app we are building is run when requested.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; The main method runs the app that was described
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;runApp&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;MyApp&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As mentioned in our description of the test, this app consists of a tree of &lt;a href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;docs&#x2F;development&#x2F;ui&#x2F;widgets-intro&quot;&gt;widgets&lt;&#x2F;a&gt; (elements of interaction), the root of which we identify with &lt;code&gt;MyApp&lt;&#x2F;code&gt; in the code. This MyApp widget is the one that gets run by the main method seen above.&lt;&#x2F;p&gt;
&lt;p&gt;The widgets themselves have properties that contain the elements that the widget is using. For example the title of the app, the theme of the app and the other widgets it uses such as a widget for a home page. The main job of the widget is to implement a specific build function that describes how these various elements fit together. In the case of &lt;code&gt;MyApp&lt;&#x2F;code&gt; we are creating a &lt;a href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;material&#x2F;MaterialApp-class.html&quot;&gt;MaterialApp&lt;&#x2F;a&gt; customized to our specific needs. This includes a home page for navigating the text of Mediations, as well as a theme that uses a color (hopefully) close enough to &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tyrian_purple&quot;&gt;Imperial purple&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;MyApp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;StatelessWidget&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; The app consists of a tree of widgets, of which this is the root.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@override
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Widget &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;build&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span&gt; context) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;MaterialApp&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;      title: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      theme: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;ThemeData&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        primarySwatch: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Colors&lt;&#x2F;span&gt;&lt;span&gt;.purple,
&lt;&#x2F;span&gt;&lt;span&gt;        visualDensity: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;VisualDensity&lt;&#x2F;span&gt;&lt;span&gt;.adaptivePlatformDensity,
&lt;&#x2F;span&gt;&lt;span&gt;      ),
&lt;&#x2F;span&gt;&lt;span&gt;      home: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;MyHomePage&lt;&#x2F;span&gt;&lt;span&gt;(title: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Meditations&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    );
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The previous Widget does not carry an internal, mutable, state directly in itself (hence why it is an extension of a &lt;a href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;StatelessWidget-class.html&quot;&gt;StatelessWidget&lt;&#x2F;a&gt;). However, as one can deduce from our tests, we want to keep information such as the current book and section number around and display these.&lt;&#x2F;p&gt;
&lt;p&gt;In order to get this done, we use an extension of &lt;a href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;StatefulWidget-class.html&quot;&gt;StatefulWidget&lt;&#x2F;a&gt; named &lt;code&gt;MyHomePage&lt;&#x2F;code&gt; which carries the state. The state itself contains the elements of book number, section number and the text to be shown on page. In addition, there are methods for manipulating the state and showing it. &lt;&#x2F;p&gt;
&lt;p&gt;For setting the state, we make use of all the functionality we built in &lt;code&gt;meditations.dart&lt;&#x2F;code&gt; to search for the next (or previous, first, last) section and change the state accordingly. Each of these state setting methods are inside a call to &lt;code&gt;setState&lt;&#x2F;code&gt; which makes the Flutter framework be aware of the state changes and, if required, it can redraw the relevant widgets to show these changes.&lt;&#x2F;p&gt;
&lt;p&gt;The methods for showing the state are reasonably straightforward. we mostly use these to translate the internally 0-indexed book- and section numbers into 1 indexed ones to match the actual numbers used in Meditations (albeit not with Roman Numerals though that would be a nice future feature for the app).&lt;&#x2F;p&gt;
&lt;p&gt;The final part is making sure there are widgets for showing the state and interacting with the state. As one can see in the animation of the screen, as well is in the test, we have a section showing the current section and page number, a text area that scrolls showing the current section and a bottom navigation bar to change the current section. &lt;&#x2F;p&gt;
&lt;p&gt;The code for all of this, when put together, can be seen below:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;dart&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-dart &quot;&gt;&lt;code class=&quot;language-dart&quot; data-lang=&quot;dart&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;MyHomePage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;StatefulWidget&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; The homepage of the app, which also creates and holds the state.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;MyHomePage&lt;&#x2F;span&gt;&lt;span&gt;({&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Key&lt;&#x2F;span&gt;&lt;span&gt; key, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;this&lt;&#x2F;span&gt;&lt;span&gt;.title}) : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;super&lt;&#x2F;span&gt;&lt;span&gt;(key: key);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt; title;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@override
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;_MyHomePageState &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;createState&lt;&#x2F;span&gt;&lt;span&gt;() =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;_MyHomePageState&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;_MyHomePageState &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;State&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;MyHomePage&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; The variables used to describe the state.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; _bookNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; _sectionNr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt; _text = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getFirst&lt;&#x2F;span&gt;&lt;span&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt;).text;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Functions to show the state variables on the screen.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;String &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getShowText&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; _text;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getShowBookNr&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; The screen should book and section numbers starting with 1 instead of 0.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; _bookNr + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getShowSectionNr&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; _sectionNr + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Functions to navigate the text of Meditations and update the state based on UI interaction.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_first&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; The call to setState makes the Flutter framework aware of the state changes.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; It will ensure that the build method below will be rerun.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;setState&lt;&#x2F;span&gt;&lt;span&gt;(() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; result = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getFirst&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (result is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        _text = result.text;
&lt;&#x2F;span&gt;&lt;span&gt;        _bookNr = result.bookNr;
&lt;&#x2F;span&gt;&lt;span&gt;        _sectionNr = result.sectionNr;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    });
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_previous&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;setState&lt;&#x2F;span&gt;&lt;span&gt;(() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; result =
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getPreviousSection&lt;&#x2F;span&gt;&lt;span&gt;(_bookNr, _sectionNr);
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (result is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        _text = result.text;
&lt;&#x2F;span&gt;&lt;span&gt;        _bookNr = result.bookNr;
&lt;&#x2F;span&gt;&lt;span&gt;        _sectionNr = result.sectionNr;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    });
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_next&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;setState&lt;&#x2F;span&gt;&lt;span&gt;(() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; result =
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getNextSection&lt;&#x2F;span&gt;&lt;span&gt;(_bookNr, _sectionNr);
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (result is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        _text = result.text;
&lt;&#x2F;span&gt;&lt;span&gt;        _bookNr = result.bookNr;
&lt;&#x2F;span&gt;&lt;span&gt;        _sectionNr = result.sectionNr;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    });
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_last&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;setState&lt;&#x2F;span&gt;&lt;span&gt;(() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionSearchResult&lt;&#x2F;span&gt;&lt;span&gt; result = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Meditations&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getLast&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (result is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BookSectionText&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        _text = result.text;
&lt;&#x2F;span&gt;&lt;span&gt;        _bookNr = result.bookNr;
&lt;&#x2F;span&gt;&lt;span&gt;        _sectionNr = result.sectionNr;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    });
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Handles the action of the bottom navigation bar on screen.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_onBottomNavTapped&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; index) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;setState&lt;&#x2F;span&gt;&lt;span&gt;(() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;switch&lt;&#x2F;span&gt;&lt;span&gt; (index) {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_first&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_previous&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_next&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_last&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_first&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    });
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@override
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Widget &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;build&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span&gt; context) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; This method is rerun every time setState is called.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; It contains a nested widget with a app bar on top, a scrollable text area in the middle, and a navigation bar at the bottom.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Scaffold&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;      appBar: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;AppBar&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        title: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Text&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;widget.title&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;} Book ${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getShowBookNr()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;} Section ${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getShowSectionNr()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;      ),
&lt;&#x2F;span&gt;&lt;span&gt;      body: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Center&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        child: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Column&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;          children: &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Widget&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;[
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Expanded&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;                flex: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                child: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;SingleChildScrollView&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;                    child: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Container&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;                      padding: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;EdgeInsets&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8.0&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;                      child: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Text&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getShowText&lt;&#x2F;span&gt;&lt;span&gt;())))),
&lt;&#x2F;span&gt;&lt;span&gt;          ],
&lt;&#x2F;span&gt;&lt;span&gt;        ),
&lt;&#x2F;span&gt;&lt;span&gt;      ),
&lt;&#x2F;span&gt;&lt;span&gt;      bottomNavigationBar: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BottomNavigationBar&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        type: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BottomNavigationBarType&lt;&#x2F;span&gt;&lt;span&gt;.fixed,
&lt;&#x2F;span&gt;&lt;span&gt;        items: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BottomNavigationBarItem&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;[
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BottomNavigationBarItem&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;            icon: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icon&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span&gt;.first_page),
&lt;&#x2F;span&gt;&lt;span&gt;            label: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;First&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;          ),
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BottomNavigationBarItem&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;            icon: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icon&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span&gt;.navigate_before),
&lt;&#x2F;span&gt;&lt;span&gt;            label: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Previous&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;          ),
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BottomNavigationBarItem&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;            icon: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icon&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span&gt;.navigate_next),
&lt;&#x2F;span&gt;&lt;span&gt;            label: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Next&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;          ),
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;BottomNavigationBarItem&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;            icon: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icon&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span&gt;.last_page),
&lt;&#x2F;span&gt;&lt;span&gt;            label: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;Last&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;          ),
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        onTap: _onBottomNavTapped,
&lt;&#x2F;span&gt;&lt;span&gt;      ),
&lt;&#x2F;span&gt;&lt;span&gt;    );
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is pretty much the full description of the Meditations app. It shows how a small application can be built up and structured. My experience with the Flutter framework was very pleasant. Most things I wanted to do were pretty straightforward and there is a large amount of documentation for the framework and setup. I like the emphasis on getting tests up and running straight from the box and the large set of pre-made widgets help a lot with getting a workable UI going very quickly. My biggest pain point was the setup of the whole Android tool-chain itself. Although there were some additional features to the Dart language (such as the upcoming, at the time of this writing, &lt;a href=&quot;https:&#x2F;&#x2F;dart.dev&#x2F;null-safety&quot;&gt;Null Safety&lt;&#x2F;a&gt;) the language is easy to pick up and become productive with.&lt;&#x2F;p&gt;
&lt;p&gt;Of course there are tons of possible extensions to the app. Aside from including the full text of Meditations, one can improve upon the navigation, add features such as bookmarking, random section selection and more. Refactoring and extending the app so it could load in other advice&#x2F;text in similar format is also a possibility. &lt;&#x2F;p&gt;
&lt;p&gt;Hopefully this article and code was clear enough to follow and my thought process came over clearly. The project has definitely increased my interest in using Dart&#x2F;Flutter in another project and felt like good practice. Hopefully your next project, perhaps made with Flutter, will be similarly enjoyable and enriching to do! &lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>To Curry in League of Legends</title>
        <published>2020-07-04T00:00:00+00:00</published>
        <updated>2020-07-04T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/curry-lol/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/curry-lol/</id>
        
        <content type="html">&lt;p&gt;One of the most well-known and popular online multi-player games is &lt;a href=&quot;https:&#x2F;&#x2F;euw.leagueoflegends.com&#x2F;en-gb&#x2F;&quot;&gt;League of Legends&lt;&#x2F;a&gt;. In this game two teams of five players compete against other. Within the game each player controls a champion that they can use to defeat the enemy team by destroying their base. There are many aspects required to winning a game of League of Legends: good in-game strategy, good control of champions but also selecting a good composition of 5 champions, out of more than a hundred, that fits the needs of the players and play-styles.&lt;&#x2F;p&gt;
&lt;p&gt;One of the lesser-known, but nonetheless very innovative, programming languages is &lt;a href=&quot;https:&#x2F;&#x2F;www-ps.informatik.uni-kiel.de&#x2F;currywiki&#x2F;&quot;&gt;Curry&lt;&#x2F;a&gt;. What makes this language so unique that it is a functional logic language. This means that it combines features of functional programming, composing a program through functions, and logic programming, defining the program as predicates of a logic system and searching for answers. &lt;&#x2F;p&gt;
&lt;p&gt;This article aims to give an introduction to the Curry language by creating a small system in it that matches and recommends League Of Legends champions based on player preferences.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to code along with this article, one can use &lt;a href=&quot;https:&#x2F;&#x2F;www-ps.informatik.uni-kiel.de&#x2F;smap&#x2F;smap.cgi?new&#x2F;curry&quot;&gt;SMAP&lt;&#x2F;a&gt; to evaluate Curry code online, aside from installing a Curry language implementation such as &lt;a href=&quot;https:&#x2F;&#x2F;www.informatik.uni-kiel.de&#x2F;~pakcs&#x2F;&quot;&gt;PACKS&lt;&#x2F;a&gt; locally. The code described in this article can be found in a &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;curry-league&quot;&gt;source repository&lt;&#x2F;a&gt; in its entirety as well.&lt;&#x2F;p&gt;
&lt;p&gt;In order to build such a recommendation system, the first thing we need to do is figure out a way to represent both the player preferences and the League of Legends champions in Curry.&lt;&#x2F;p&gt;
&lt;p&gt;To start off, let&#x27;s define the aspects of a champion in Curry as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;Main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tag &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Marksman &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Assassin &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Mage &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Support
&lt;&#x2F;span&gt;&lt;span&gt;         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;                                       
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion &lt;&#x2F;span&gt;&lt;span&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;              &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;attack &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;              &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;defense &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;              &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;magic &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;              &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;difficulty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;              &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;tags &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Tag&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lets just go over the above code step-by-step. &lt;&#x2F;p&gt;
&lt;p&gt;The first line is the module declaration, which is essentially a grouping of related code together. We will define all elements required for our champion recommendation program in this Main module.&lt;&#x2F;p&gt;
&lt;p&gt;Next we define the data types for our representation of Champion Tags and Champions. Curry is a statically typed language, meaning you can define the structure of the data as types, and you can check these types at compile time before the actual program is run. For example, it only allows the following Tags to be used for the Champions: Fighter, Tank, Marksman, Assassin, Mage, Support. Similarly, a Champion is a record of very specific keys and types of values. For example it has a field for &lt;code&gt;name&lt;&#x2F;code&gt; that must be a string of characters, a field for &lt;code&gt;attack&lt;&#x2F;code&gt; that must be an integer number and a field for &lt;code&gt;tags&lt;&#x2F;code&gt; which must be a list of Tags that we previously defined.&lt;&#x2F;p&gt;
&lt;p&gt;These types allow us to define various champions avaiable in League of Legends. For example, the champion Aatrox can be defined as: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;aatrox &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;aatrox = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Aatrox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                       attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                       }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that this is a correct champion as it fulfills every criteria we gave to the type of Champion. If we would define this champion in the wrong way, such as: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;aatrox &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;aatrox = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Aatrox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                       attack = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;high&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                       defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                       }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;the compiler of Curry would give us an error, &amp;quot;Type error in record construction&amp;quot;, along with some additional information that the attack field has a value that does not match the type.&lt;&#x2F;p&gt;
&lt;p&gt;In this definition we explicitly denoted that Aatrox is of the type champion using the line:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;aatrox &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is not a strict requirement. Curry can infer the type simply from the constructor of the record &lt;code&gt;Champion&lt;&#x2F;code&gt;. Curry can perform type inference even in many other more complex cases, but for clarity we will give the types of records and functions whenever we can.&lt;&#x2F;p&gt;
&lt;p&gt;The final part of this definition, is the portion:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Without going into too much detail, these makes instances of Champions, and Tags, easy to compare and print as a string.&lt;&#x2F;p&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2020&#x2F;curry-lol&#x2F;Aatrox.jpg&quot; title=&quot;Aatrox, a champion in League of Legends Copyright Riot Games&quot; &gt;}} --&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=Aatrox.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Aatrox, a champion in League of Legends Copyright Riot Games&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The above code should be very familiar to &lt;a href=&quot;https:&#x2F;&#x2F;www.haskell.org&#x2F;&quot;&gt;Haskell&lt;&#x2F;a&gt; programmers! The syntax, the type system and many other aspects of Curry are heavily influenced by Haskell. Even the naming of the two languages have the same origin: they are both named after the logician &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Haskell_Curry&quot;&gt;Haskell Curry&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we got the basics out of the way let us define a lot more champions than just Aatrox. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;aatrox &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;aatrox = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Aatrox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                       attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                       tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                       }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;darius &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;darius = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darius&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                   attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                   defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                   magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                   difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                   tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fiora &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;fiora = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Fiora&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                  attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Assassin&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                 }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;gnar &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;gnar = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gnar&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                 attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                 defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                 magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                 difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                 tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                 }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;irelia &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;irelia = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Irelia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                  attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Assassin&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;karma &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;karma = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Karma&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                  attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Mage&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Support&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;maokai &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;maokai = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Maokai&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                   attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                   defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                   magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                   difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                   tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Mage&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;neeko &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;neeko = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Neeko&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                  attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Mage&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Support&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sylas &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion
&lt;&#x2F;span&gt;&lt;span&gt;sylas = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sylas&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                  attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Mage&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Assassin&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;vayne &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Champion 
&lt;&#x2F;span&gt;&lt;span&gt;vayne = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Vayne&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                  attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                  tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Marksman&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Assassin&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;champions &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;champions = [aatrox,
&lt;&#x2F;span&gt;&lt;span&gt;             darius,
&lt;&#x2F;span&gt;&lt;span&gt;             fiora,
&lt;&#x2F;span&gt;&lt;span&gt;             gnar,
&lt;&#x2F;span&gt;&lt;span&gt;             irelia,
&lt;&#x2F;span&gt;&lt;span&gt;             karma,
&lt;&#x2F;span&gt;&lt;span&gt;             maokai,
&lt;&#x2F;span&gt;&lt;span&gt;             neeko,
&lt;&#x2F;span&gt;&lt;span&gt;             sylas,
&lt;&#x2F;span&gt;&lt;span&gt;             vayne
&lt;&#x2F;span&gt;&lt;span&gt;            ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above code defines a total of 10 different Champions that exist in League of Legends, whose data values are derived from Riot games &lt;a href=&quot;https:&#x2F;&#x2F;developer.riotgames.com&#x2F;docs&#x2F;lol#data-dragon_champions&quot;&gt;Data Dragon API&lt;&#x2F;a&gt;. For easy access, we define a list of champions as the aptly named &lt;code&gt;champions&lt;&#x2F;code&gt;. Note that in Curry a list is denoted by elements between &lt;code&gt;[]&lt;&#x2F;code&gt;. The type for a list of elements is denoted by using these bracket in front of the type, such as &lt;code&gt;[]Tag&lt;&#x2F;code&gt;, which denotes the type of a list of Tags.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have the champion definitions out of the way, we also want to define player preferences that we aim to match champions against. Here our preferences consists of a summoner name, identifying the player, minimum requirements for the champion&#x27;s attack, defense and magic values, a maximum difficulty rating, as well as tags on type of champions the player prefers.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preference &lt;&#x2F;span&gt;&lt;span&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preference&lt;&#x2F;span&gt;&lt;span&gt; {summonerName :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;minAttack &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;minDefense &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;minMagic &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;maxDifficulty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;prefTags &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Tag&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using this Preference type we define two player preferences, one for Alice and one for Bob, as well as a collection for both of them.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;alicePref &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference
&lt;&#x2F;span&gt;&lt;span&gt;alicePref = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preference&lt;&#x2F;span&gt;&lt;span&gt; {  summonerName = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                          minAttack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                          minDefense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                          minMagic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                          maxDifficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                          prefTags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Mage&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                         }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;bobPref &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference
&lt;&#x2F;span&gt;&lt;span&gt;bobPref = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preference&lt;&#x2F;span&gt;&lt;span&gt; {  summonerName = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bob&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;                        minAttack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                        minDefense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                        minMagic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                        maxDifficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                        prefTags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Support&lt;&#x2F;span&gt;&lt;span&gt;]}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;preferences &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Preference&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;preferences = [ alicePref,
&lt;&#x2F;span&gt;&lt;span&gt;                bobPref
&lt;&#x2F;span&gt;&lt;span&gt;              ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have now defined both the champions and the player preferences and we can move towards creating the functionality to match them together. But first a bit of introduction, or a refresher depending on the readers background, to functional programming. &lt;&#x2F;p&gt;
&lt;p&gt;As mentioned at the start of this article, Curry is a language that supports both functional and logical programming. Functional programming allows us to express the program we want to write as a composition of functions.&lt;&#x2F;p&gt;
&lt;p&gt;To use an example, suppose we want to write a program that given a list of champions, returns only those champion that have an attack of more than 5. In order to do this we can first write a function that given a champion returns true if it has an attack more than 5 and returns false if this is not the case. Then we can use this function as filter inside another function, that only returns members of a list of champions that have more than 5 attack.&lt;&#x2F;p&gt;
&lt;p&gt;To put this together, such a function would look as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;highAttackChamps &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;highAttackChamps champList = filter (\ champion -&amp;gt; attack champion &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;) champList
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are a couple of things to note here. &lt;&#x2F;p&gt;
&lt;p&gt;First is that the function that &amp;quot;returns true only if the attack of a champion is greater than 5&amp;quot; has no name. Such anonymous functions, also known as lambda expressions, can be defined using a form starting with a backslash (\) followed by the parameter names of the function, followed by an arrow (-&amp;gt;) and the expressions that this function should execute. In this case this expression first gets the attack of the champion and compares it, to give the right result. Anonymous functions are very convenient for expressions that we do not aim to reuse somewhere else.&lt;&#x2F;p&gt;
&lt;p&gt;The second thing to note is the function composition that allows the filtering. The &lt;code&gt;filter&lt;&#x2F;code&gt; function is a predefined function that is imported by default, as a part of a group of functions such as these called a Prelude. As mentioned before this takes a function as a parameter that returns a Bool (True or False), as well as a list, and returns only those members of the list for which the function returned true. Using a function as an argument makes the &lt;code&gt;filter&lt;&#x2F;code&gt; function very versatile. If we, for example wanted to filter based on champion defense, we could have just replaced function used as an argument. Such a composition using functions is one of the core elements of what makes functional programming so effective.&lt;&#x2F;p&gt;
&lt;p&gt;The final thing to note is how the &lt;code&gt;highAttackChamps&lt;&#x2F;code&gt; is given a type. The type declaration &lt;code&gt;highAttackChamps :: [Champion] -&amp;gt; [Champion]&lt;&#x2F;code&gt; shows that this function takes a list of champions and returns a list of champions, where the arrow &lt;code&gt;-&amp;gt;&lt;&#x2F;code&gt; functions as a separator between the type of the parameter(s) and&#x2F;or the type of returned value.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we got over the basics of functional programming lets start to put this knowledge to good use by writing a number of functions that helps us match the Preferences with the Champions. These functions are designed to match a single criteria within a Preference with the value of a Champion. If this match succeeds that the function returns True, if it fails it returns a False. We have a function to match based on attack, defense, magic and difficulty. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchChampionAttack &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference -&amp;gt; Champion -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;matchChampionAttack pref champ = (minAttack pref) &amp;lt;= (attack champ) 
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchChampionDefense &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference -&amp;gt; Champion -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;matchChampionDefense pref champ = (minDefense pref) &amp;lt;= (defense champ) 
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchChampionMagic &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference -&amp;gt; Champion -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;matchChampionMagic pref champ = (minMagic pref) &amp;lt;= (magic champ) 
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchChampionDifficulty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference -&amp;gt; Champion -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;matchChampionDifficulty pref champ = (maxDifficulty pref) &amp;gt;= (difficulty champ) 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that the type definition now have three elements separated by an arrow (-&amp;gt;), as these functions have two parameters, a Preference and a Champion, and return a Bool (the type for True or False).&lt;&#x2F;p&gt;
&lt;p&gt;For matching the preferences, we are going to explore some of the logic programming features of Curry. In logic programming the program can be defined as a set of facts, rules and (logic) variables, where the underlying logic system can search for a suitable solution given the criteria.&lt;&#x2F;p&gt;
&lt;p&gt;Give given an example, suppose we are looking for Tag that is both part of the preferred tags of the Preferences as well as part of the tags of the Champions. We can state this problem exactly like this in Curry as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchChampionTag &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference -&amp;gt; Champion -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;matchChampionTag pref champ = (elem tag (prefTags pref) &amp;amp;&amp;amp; elem tag (tags champ)) =:= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span&gt; tag free
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To go over the elements in this code &lt;code&gt;(elem tag (prefTags pref) &amp;amp;&amp;amp; elem tag (tags champ))&lt;&#x2F;code&gt; express that &lt;code&gt;tag&lt;&#x2F;code&gt; has to be an element of both the &lt;code&gt;prefTags&lt;&#x2F;code&gt; of the a given preference and &lt;code&gt;tags&lt;&#x2F;code&gt; of the given champion. Unlike with our purely functional code above, we do not give the value of &lt;code&gt;tag&lt;&#x2F;code&gt; as a parameter. Instead we say that this variable is a free variable and let Curry find the values of Tag which makes the above constraint hold True  (i.e. the &lt;code&gt;=:= True where tag free&lt;&#x2F;code&gt; portion of the function).&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have all the functions for matching individual elements (i.e.: attack, defense, magic, difficulty and tags) of champions to profiles, we can create combined functions such as &lt;code&gt;matchChampionAny&lt;&#x2F;code&gt; that holds true if any of the elements match and &lt;code&gt;matchChampionAll&lt;&#x2F;code&gt; for which all elements need to match.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchChampionAny &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference -&amp;gt; Champion -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;matchChampionAny pref champ =  matchChampionAttack pref champ ||
&lt;&#x2F;span&gt;&lt;span&gt;                               matchChampionDefense pref champ ||
&lt;&#x2F;span&gt;&lt;span&gt;                               matchChampionMagic pref champ ||
&lt;&#x2F;span&gt;&lt;span&gt;                               matchChampionDifficulty pref champ ||
&lt;&#x2F;span&gt;&lt;span&gt;                               matchChampionTag pref champ
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchChampionAll &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preference -&amp;gt; Champion -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;matchChampionAll pref champ =  matchChampionAttack pref champ &amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span&gt;                                 matchChampionDefense pref champ &amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span&gt;                                 matchChampionMagic pref champ &amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span&gt;                                 matchChampionDifficulty pref champ &amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span&gt;                                 matchChampionTag pref champ
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One of the coolest features of this language, which we can see in the above definition, is the way logic programming elements can mix with functional elements. In both &lt;code&gt;matchChampionAny&lt;&#x2F;code&gt; and &lt;code&gt;matchChampionAll&lt;&#x2F;code&gt;  we use functions that make use of functional programming, as well as logic programming, seamlessly. &lt;&#x2F;p&gt;
&lt;p&gt;Once we got our functions for matching we can use these to match, we finally have all the ingredients to create recommendations. We are going to create two recommendation functions: one for matching based on &lt;code&gt;matchChampionAny&lt;&#x2F;code&gt; which is a &amp;quot;weak&amp;quot; criteria as any there are lot of opportunities to match and one based on &lt;code&gt;matchChampionAll&lt;&#x2F;code&gt; which is a lot &amp;quot;stronger&amp;quot; as all stats of a champion has to match with the preferences.&lt;&#x2F;p&gt;
&lt;p&gt;The functions for these is given as follows: &lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;recommendWeak :: Preference -&amp;gt; [Champion] -&amp;gt; [String]
&lt;&#x2F;span&gt;&lt;span&gt;recommendWeak preference champList = map (\ champion -&amp;gt; name champion) (filter (\ champion -&amp;gt; matchChampionAny preference champion ) champList)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;recommendStrong :: Preference -&amp;gt; [Champion] -&amp;gt; [String]
&lt;&#x2F;span&gt;&lt;span&gt;recommendStrong preference champList = map (\champion -&amp;gt; name champion) (filter (\ champion -&amp;gt; matchChampionAll preference champion ) champList)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The constructs in here should look familiar with one exception: the use of &lt;code&gt;map (\champion -&amp;gt; name champion)&lt;&#x2F;code&gt;. The &lt;code&gt;map&lt;&#x2F;code&gt; function is similar to &lt;code&gt;filter&lt;&#x2F;code&gt; in takes a function for a parameter as well as a list. Instead of filtering, in instead applies that function to all members of that list. The anonymous function used takes a champion and returns only the name value of that champion. This allows for the matching function to return not a list of champions, but a list of strings i.e.: &lt;code&gt;[String]&lt;&#x2F;code&gt; that are the names of the champions. This makes the return value much less verbose, as we can identify champions by name. &lt;&#x2F;p&gt;
&lt;p&gt;Finally, in order to show off the code detailed here with, we put together some examples of using the all the functions we defined. We then print the answers out, line by line for each of these examples. The main function, with the return type IO (), allows us to not to return a specific value but print the values out, to the REPL or the command-line, depending on how we use our code.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;example1 = highAttackChamps champions
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;example2 = matchChampionAttack alicePref aatrox
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;example3 = matchChampionDifficulty bobPref vayne 
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example4 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;example4 =  matchChampionTag alicePref karma
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;example5 =  matchChampionAny alicePref neeko
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example6 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;example6 =  matchChampionAll bobPref gnar
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example7 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;example7 =  recommendWeak alicePref champions
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example8 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;example8 =  recommendWeak bobPref champions
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example9 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;example9 =  recommendStrong alicePref champions
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;example10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;example10 =  recommendStrong bobPref champions
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;main = putStrLn (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 1, champions with more than 5 attack:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example1) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 2, matching Aatrox with Alice&amp;#39;s preferences based on attack:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example2) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 3, matching Vayne with Bob&amp;#39;s preferences based on difficulty:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example3) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 4, matching Karma with Alice&amp;#39;s preferences based on tag(s):&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example4) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 5, matching Neeko with Alice&amp;#39;s preferences based on any matching criteria:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example5) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 6, matching Gnar with Bob&amp;#39;s preferences based on all matching criteria:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example6) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 7, recommendation based on a weak criteria for Alice:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example7) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 8, recommendation based on a weak criteria for Bob:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example8) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 9, recommendation based on a strong criteria for Alice:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example9) ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Example 10, recommendation based on a strong criteria for Bob:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++
&lt;&#x2F;span&gt;&lt;span&gt;                 (show example10))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This results in the following being printed out:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 1&lt;&#x2F;span&gt;&lt;span&gt;, champions with more than &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt; attack:
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Aatrox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;]},&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darius&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;, defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;]},&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Fiora&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;, defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Assassin&lt;&#x2F;span&gt;&lt;span&gt;]},&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gnar&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;, defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tank&lt;&#x2F;span&gt;&lt;span&gt;]},&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Irelia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;, defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fighter&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Assassin&lt;&#x2F;span&gt;&lt;span&gt;]},&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Champion&lt;&#x2F;span&gt;&lt;span&gt; {name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Vayne&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, attack = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;, defense = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, difficulty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, tags = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Marksman&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Assassin&lt;&#x2F;span&gt;&lt;span&gt;]}]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 2&lt;&#x2F;span&gt;&lt;span&gt;, matching &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Aatrox&lt;&#x2F;span&gt;&lt;span&gt; with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;s preferences based on attack:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 3&lt;&#x2F;span&gt;&lt;span&gt;, matching &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Vayne&lt;&#x2F;span&gt;&lt;span&gt; with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bob&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;s preferences based on difficulty:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 4&lt;&#x2F;span&gt;&lt;span&gt;, matching &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Karma&lt;&#x2F;span&gt;&lt;span&gt; with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;s preferences based on tag(s):
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 5&lt;&#x2F;span&gt;&lt;span&gt;, matching &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Neeko&lt;&#x2F;span&gt;&lt;span&gt; with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;s preferences based on any matching criteria:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 6&lt;&#x2F;span&gt;&lt;span&gt;, matching &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Gnar&lt;&#x2F;span&gt;&lt;span&gt; with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bob&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;s preferences based on all matching criteria:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 7&lt;&#x2F;span&gt;&lt;span&gt;, recommendation based on a weak criteria for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Aatrox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darius&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Fiora&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gnar&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Irelia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Karma&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Maokai&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Neeko&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sylas&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Vayne&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 8&lt;&#x2F;span&gt;&lt;span&gt;, recommendation based on a weak criteria for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bob&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Aatrox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darius&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Fiora&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gnar&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Irelia&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Karma&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Maokai&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Neeko&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sylas&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Vayne&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 9&lt;&#x2F;span&gt;&lt;span&gt;, recommendation based on a strong criteria for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Maokai&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Example 10&lt;&#x2F;span&gt;&lt;span&gt;, recommendation based on a strong criteria for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bob&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully the result nothing is too surprising and it matches all expectations. The result also verifies some of our assumptions: the weak criteria matches everything in this case, but a strong criteria has some nice results: Maokai is well suited for Alice, and Bob probably needs to widen his criteria, given the above 10 champions. &lt;&#x2F;p&gt;
&lt;p&gt;There are many ways we could proceed from this point on. As one could see from the examples the recommendation based on a weak criteria is quite weak, as anything matches, while a strong criteria can be too restrictive at times. Coming up with the different types of matching functions, could be a next step, if one wants to expand upon this small application. With multiple matching functions one can also write a function that takes a matching function as a parameter in order to avoid writing duplicate code for each recommendation function. &lt;&#x2F;p&gt;
&lt;p&gt;Hopefully this article has given you a bit of an intro to the Curry language and why it is so interesting. There exists a number of options for using functional and logic programming separately but this is one of the few languages that aims to combine both. If you wish to explore either paradigms further I wish you the best of luck to curry on with your endeavors. &lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>CybOrg Mode</title>
        <published>2020-06-01T00:00:00+00:00</published>
        <updated>2020-06-01T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/cyborg-mode/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/cyborg-mode/</id>
        
        <content type="html">&lt;p&gt;Human beings enhanced by machine parts, often called &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Cyborg&quot;&gt;cyborgs&lt;&#x2F;a&gt;, are a staple of the science-fiction and comic books. The, very aptly named, hero &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Cyborg_(comics)&quot;&gt;Cyborg&lt;&#x2F;a&gt;, is a good example of this. Due to advanced mechanical modifications he not only has greatly increased strength and the ability to fly, but he is able to interface directly with computer systems to organize and plan ahead. These skills make him an important member of many superhero teams such as the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Justice_League&quot;&gt;Justice League&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Teen_Titans&quot;&gt;Teen Titans&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Thankfully for us, even without any of the cybernetic implants that Cyborg has, there exists some great tools to help organize, document and plan ahead. One of such tools is called &lt;a href=&quot;https:&#x2F;&#x2F;orgmode.org&#x2F;&quot;&gt;Org-mode&lt;&#x2F;a&gt;. Org-mode, also denoted as Org mode or even more simply as Org, is a system for organizing notes, plans, authoring documents and many other tasks. One of its main benefits is that although specialized tools exist to help create and edit Org documents, all Org documents are &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Plain_text&quot;&gt;plain text&lt;&#x2F;a&gt; files. This means that the format is very portable and can be used and edited on any platform which can edit text files. In this article we give a brief introduction to Org mode with an example based on the hero Cyborg.&lt;&#x2F;p&gt;
&lt;p&gt;Org documents can be outlined with a tree structure using headlines. Suppose that we want to describe various information about Cyborg, such as his biography, his powers, his relationships and his schedule. For each of these we can use an asterisk (&lt;code&gt;*&lt;&#x2F;code&gt;) symbol followed by some text to create a headline in the document as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;org&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-org &quot;&gt;&lt;code class=&quot;language-org&quot; data-lang=&quot;org&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Biography
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Powers
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Relationships
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Cyborg&amp;#39;s Schedule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can add nested headlines by increasing the number of asterisks in front of a section name. For example, if we want to create two headlines under relationships detailing Cyborgs team affiliations, as well as his villains, we can do that:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;org&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-org &quot;&gt;&lt;code class=&quot;language-org&quot; data-lang=&quot;org&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Biography
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Powers
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Relationships
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;** Team Affiliations
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;** Villains
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Cyborg&amp;#39;s Schedule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course while headlines can help us structure a document, text is an integral part of it. A simple biography can be shown as one would expect: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;org&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-org &quot;&gt;&lt;code class=&quot;language-org&quot; data-lang=&quot;org&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Biography
&lt;&#x2F;span&gt;&lt;span&gt;Vic Stone was a high school athlete when a tragic accident nearly killed him and destroyed most of this body. His father, Dr. Silas Stone, used his scientific knowledge and advanced technology to save his life by augmenting him with machine parts.
&lt;&#x2F;span&gt;&lt;span&gt;Living now as a cyborg, a being with both organic and machine parts, he is now has super strength, increased durability and a control over advanced technology and weaponry.
&lt;&#x2F;span&gt;&lt;span&gt;Although he continues to struggle with his newfound status as both man and machine, he uses his newfound powers as a superhero.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Powers
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Relationships
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;** Team Affiliations
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;** Villains
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Cyborg&amp;#39;s Schedule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can also create lists in the document. For example we can give an unordered list of Cyborg&#x27;s powers such as super strength and instant weaponary by prefacing each item on a new line by a hyphen (&lt;code&gt;-&lt;&#x2F;code&gt;): &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;org&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-org &quot;&gt;&lt;code class=&quot;language-org&quot; data-lang=&quot;org&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Biography
&lt;&#x2F;span&gt;&lt;span&gt;Vic Stone was a high school athlete when a tragic accident nearly killed him and destroyed most of this body. His father, Dr. Silas Stone, used his scientific
&lt;&#x2F;span&gt;&lt;span&gt;knowledge and advanced technology to save his life by augmenting him with machine parts.
&lt;&#x2F;span&gt;&lt;span&gt;Living now as a cyborg, a being with both organic and machine parts, he is now has super strength, increased durability and a control over advanced technology and weaponry.
&lt;&#x2F;span&gt;&lt;span&gt;Although he continues to struggle with his newfound status as both man and machine, he uses his newfound powers as a superhero.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Powers
&lt;&#x2F;span&gt;&lt;span&gt;- super strength
&lt;&#x2F;span&gt;&lt;span&gt;- advanced technology,
&lt;&#x2F;span&gt;&lt;span&gt;- instant weaponry
&lt;&#x2F;span&gt;&lt;span&gt;- genius-level intellect
&lt;&#x2F;span&gt;&lt;span&gt;- control over technology
&lt;&#x2F;span&gt;&lt;span&gt;- computer hacking
&lt;&#x2F;span&gt;&lt;span&gt;- durability
&lt;&#x2F;span&gt;&lt;span&gt;- teleportation
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Relationships
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;** Team Affiliations
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;** Villains
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Cyborg&amp;#39;s Schedule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that the document is now getting larger and the parts of what we want to focus on to show here, the powers, can get lost in the other information. One of the strengths of Org-mode that even though it can be viewed and edited as plain text, there exists quite a bit of tooling to help navigate, edit and make use of the described structures. The editor &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;emacs&#x2F;&quot;&gt;emacs&lt;&#x2F;a&gt; and its various flavours such as &lt;a href=&quot;https:&#x2F;&#x2F;www.spacemacs.org&#x2F;&quot;&gt;spacemacs&lt;&#x2F;a&gt; that I personally use have excellent features for these tasks. However other editors such as &lt;a href=&quot;https:&#x2F;&#x2F;code.visualstudio.com&#x2F;&quot;&gt;Visual Studio Code&lt;&#x2F;a&gt; have &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;vscode-org-mode&#x2F;vscode-org-mode&quot;&gt;plugins&lt;&#x2F;a&gt; to support Org-mode as well.&lt;&#x2F;p&gt;
&lt;p&gt;One of the basic features to help such editors provide is to fold headlines under which we do not want to see all the content in detail and unfold it for those that we do. In the following animation we show how this can be done on a portion of a more extended version of the Cyborg document: &lt;&#x2F;p&gt;
&lt;!-- &lt;figure&gt;
  &lt;img src=&quot;foldingheadlines.gif&quot;&#x2F;&gt;
  &lt;figcaption&gt;
      &lt;h4&gt;Folding and unfolding headlines in Org document using Spacemacs&lt;&#x2F;h4&gt;
  &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt; --&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=foldingheadlines.gif&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Folding and unfolding headlines in Org document using Spacemacs&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2020&#x2F;cyborg-mode&#x2F;foldingheadlines.gif&quot; title=&quot;Folding and unfolding headlines in Org document using Spacemacs&quot; &gt;}} --&gt;
&lt;p&gt;The above image shows how the &lt;a href=&quot;https:&#x2F;&#x2F;www.spacemacs.org&#x2F;&quot;&gt;spacemacs&lt;&#x2F;a&gt; editor handles Org documents. By simply pressing tab on headlines we can fold (i.e. not show the underlying content) and unfold (i.e. showing the underlying content) them as we please and directing our attention to parts of the document we want to view and edit. It also shows how editor support uses the underlying structure for additional visualizations, such as on the level of headlines, even though the underlying document is still plaintext.&lt;&#x2F;p&gt;
&lt;p&gt;Expanding on our example we can describe the Team Affiliations and the Villains as lists as well. For the list of Team Affiliations we will use an ordered list, which can be done by using a number followed by a dot, e.g.: &lt;code&gt;1. 2.&lt;&#x2F;code&gt; for the list indicators.  Note for brevity we now only show the new&#x2F;relevant portions of the full Org document in the examples as opposed to the whole.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;org&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-org &quot;&gt;&lt;code class=&quot;language-org&quot; data-lang=&quot;org&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Relationships
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;** Team Affiliations
&lt;&#x2F;span&gt;&lt;span&gt;1. Justice League
&lt;&#x2F;span&gt;&lt;span&gt;2. Teen Titans
&lt;&#x2F;span&gt;&lt;span&gt;3. Doom Patrol
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;** Villains
&lt;&#x2F;span&gt;&lt;span&gt;- Lex Luthor
&lt;&#x2F;span&gt;&lt;span&gt;- Deathstroke
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another interesting feature in Org documents are TODO items. Any headline can be made a TODO in them by prefacing it with TODO. For example in a headline of Cyborg&#x27;s schedule we can put two TODO items for things Cyborg wants to do at some point: attend a Justice League Meeting and Upgrading Equipment. &lt;&#x2F;p&gt;
&lt;p&gt;There are various ways such TODO items could be used similarly to folding or unfolding headlines.  In emacs&#x2F;spacemacs such actions, and many others relating to various aspects of an Org document, can be done by commands. For example the command &lt;code&gt;org-show-todo-tree&lt;&#x2F;code&gt; can take ensure that all the headlines are folded as much as possible and only the TODO headlines are unfolded. Similarly the command &lt;code&gt;org-todo&lt;&#x2F;code&gt; cycles between not marking the headline with a TODO, marking it with a TODO or marking it with DONE denoted that the item has been completed. &lt;&#x2F;p&gt;
&lt;!-- &lt;figure&gt;
  &lt;img src=&quot;toggletododate.gif&quot;&#x2F;&gt;
  &lt;figcaption&gt;
      &lt;h4&gt;Toggling using the command the command `org-todo` for headlines in Org document using Spacemacs&lt;&#x2F;h4&gt;
  &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt; --&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=toggletododate.gif&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Toggling using the command the command `org-todo` for headlines in Org document using Spacemacs&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2020&#x2F;cyborg-mode&#x2F;toggletododate.gif&quot; title=&quot;Toggling using the command the command `org-todo` for headlines in Org document using Spacemacs&quot; &gt;}} --&gt;
&lt;p&gt;The above image shows how the command &lt;code&gt;org-todo&lt;&#x2F;code&gt; can cycle between the TODO states of the Justice League Meeting headline. In addition, it also shows a few other features. &lt;&#x2F;p&gt;
&lt;p&gt;First is the associating a date with the TODO item, in particular when it is scheduled. By toggling the TODO to DONE, our editor uses the current time to mark when the task was finished. This allows for a nice way to create a checklist of the things that have to be done and marking them off.&lt;&#x2F;p&gt;
&lt;p&gt;Another aspect is the use of markup around &#x27;must&#x27; and &#x27;Lex Luthor&#x27;. Enclosing a phrase with an underscore underlines the phrase while enclosing it between asterisks (*) makes it bold. Markups for italicizing (enclosing it between slashes (&#x2F;)), code fragments and others also exist. &lt;&#x2F;p&gt;
&lt;p&gt;The full list of TODO items in Cyborg&#x27;s schedule is as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;org&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-org &quot;&gt;&lt;code class=&quot;language-org&quot; data-lang=&quot;org&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;*** &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TODO&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt; Justice League Meeting
&lt;&#x2F;span&gt;&lt;span&gt;    SCHEDULED: &amp;lt;2024-08-02&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Something _must_ be done about &lt;&#x2F;span&gt;&lt;span style=&quot;font-weight:bold;color:#ebcb8b;&quot;&gt;*Lex Luthor*&lt;&#x2F;span&gt;&lt;span&gt;. He seems to be planning an attack on &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#b48ead;&quot;&gt;&#x2F;Metropolis&#x2F;&lt;&#x2F;span&gt;&lt;span&gt; with the help of &lt;&#x2F;span&gt;&lt;span style=&quot;font-weight:bold;color:#ebcb8b;&quot;&gt;*Deathstroke*&lt;&#x2F;span&gt;&lt;span&gt;. 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;*** &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TODO&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt; Upgrade Equipment
&lt;&#x2F;span&gt;&lt;span&gt;    SCHEDULED: &amp;lt;2024-06-01&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Enhance arm cannon.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One of the most interesting features of Org mode that we will cover in this article is the ability to embed fragment of code in an Org document that can be executed in the right environment. In Cyborg&#x27;s schedule one of the upcoming items is to enhance his arm cannon. In order to prepare for this, and to illustrate our code embedding, a simple Weapon Damage calculator application was written in Python:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;  arm_cannon = {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Arm Cannon&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;power&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  reinforced_door = {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Reinforced Door&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;armor&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;wdc&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;weapon&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    damage = weapon[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;power&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;] - target[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;armor&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;damage&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;: 
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;damage
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  damage_done = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;wdc&lt;&#x2F;span&gt;&lt;span&gt;(arm_cannon, reinforced_door)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Using the weapon &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + arm_cannon[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;] + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; with &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;(arm_cannon[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;power&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]) + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; power,&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;against the target &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + reinforced_door[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;] + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; with &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;(reinforced_door[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;armor&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]) + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; armor,&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;results in &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;(damage_done) + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; damage.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This code aims to use features of Cyborg&#x27;s arm cannon to calculate damage on the target and to write these features out along with the resulting damage.&lt;&#x2F;p&gt;
&lt;p&gt;We can embed this code fragment by using the property system to give this fragment a name and denote some other attributes. This property system is essentially a key-value system in Org mode that is used for all types information represented with key-value pairs. For example the following two properties describe the title and description of the Org document:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;org&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-org &quot;&gt;&lt;code class=&quot;language-org&quot; data-lang=&quot;org&quot;&gt;&lt;span&gt;#+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Cyborg Agenda
&lt;&#x2F;span&gt;&lt;span&gt;#+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;An introduction to org mode by creating an org document for information on the superhero Cyborg.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For the code we want to embed we want set the properties on how we name this fragment, where the source begins and ends, what language it is and what should be done with the results. This looks as follows within the Org document:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;org&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-org &quot;&gt;&lt;code class=&quot;language-org&quot; data-lang=&quot;org&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;* Weapon Damage Calculator
&lt;&#x2F;span&gt;&lt;span&gt;#+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;NAME&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;wdc
&lt;&#x2F;span&gt;&lt;span&gt;#+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;BEGIN_SRC &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;python &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:results output
&lt;&#x2F;span&gt;&lt;span&gt;  arm_cannon = {&amp;quot;name&amp;quot;: &amp;quot;Arm Cannon&amp;quot;, &amp;quot;power&amp;quot;: 8}
&lt;&#x2F;span&gt;&lt;span&gt;  reinforced_door = {&amp;quot;name&amp;quot;: &amp;quot;Reinforced Door&amp;quot;, &amp;quot;armor&amp;quot;: 3}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  def wdc(weapon, target):
&lt;&#x2F;span&gt;&lt;span&gt;    damage = weapon[&amp;quot;power&amp;quot;] - target[&amp;quot;armor&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    if damage&amp;lt;0:
&lt;&#x2F;span&gt;&lt;span&gt;      return 0
&lt;&#x2F;span&gt;&lt;span&gt;    else: 
&lt;&#x2F;span&gt;&lt;span&gt;      return damage
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  damage_done = wdc(arm_cannon, reinforced_door)
&lt;&#x2F;span&gt;&lt;span&gt;  print(&amp;quot;Using the weapon &amp;quot; + arm_cannon[&amp;quot;name&amp;quot;] + &amp;quot; with &amp;quot; + str(arm_cannon[&amp;quot;power&amp;quot;]) + &amp;quot; power,&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  print(&amp;quot;against the target &amp;quot; + reinforced_door[&amp;quot;name&amp;quot;] + &amp;quot; with &amp;quot; + str(reinforced_door[&amp;quot;armor&amp;quot;]) + &amp;quot; armor,&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  print(&amp;quot;results in &amp;quot; + str(damage_done) + &amp;quot; damage.&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;#+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;END_SRC
&lt;&#x2F;span&gt;&lt;span&gt;#+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;RESULTS&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;wdc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This embeds the weapon damage calculator as a source fragment named wdc, while also setting up where the results will be show in the document. Now if we execute the command &lt;code&gt;org-babel-execute-src-block&lt;&#x2F;code&gt; we can see the results directly within the document:&lt;&#x2F;p&gt;
&lt;!-- &lt;figure&gt;
  &lt;img src=&quot;executingcode.gif&quot;&#x2F;&gt;
  &lt;figcaption&gt;
      &lt;h4&gt;Executing the source block to see the results directly within the Org document&lt;&#x2F;h4&gt;
  &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt; --&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=executingcode.gif&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Executing the source block to see the results directly within the Org document&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2020&#x2F;cyborg-mode&#x2F;executingcode.gif&quot; title=&quot;Executing the source block to see the results directly within the Org document&quot; &gt;}} --&gt;
&lt;p&gt;That is it for this tutorial, hopefully I have given a glimpse of what Org mode can do, and gives you some idea on how it can enhance your organizing, planning and note taking. Here I have not even touched on many other great features such as authoring documents and converting to other formats, using it as a spreadsheet system and many more. For further resources on this tool take a look at the official &lt;a href=&quot;https:&#x2F;&#x2F;orgmode.org&#x2F;&quot;&gt;Org mode website&lt;&#x2F;a&gt;, as well the &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=PVsSOmUB7ic&quot;&gt;excellent video tutorial&lt;&#x2F;a&gt; by GDQuest on using the Spacemacs editor with Org mode. The Org document that was built up can also be found in its entirety at a &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;cyborg-mode&#x2F;&quot;&gt;github repository&lt;&#x2F;a&gt; along with some extra documentation.&lt;&#x2F;p&gt;
&lt;p&gt;Hopefully you have enjoyed the article and if you want to enhance yourself with one of the best note taking and planning tools around give Org mode a shot.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Darmok in core.logic</title>
        <published>2020-02-02T00:00:00+00:00</published>
        <updated>2019-02-02T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/darmok-core-logic/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/darmok-core-logic/</id>
        
        <content type="html">&lt;p&gt;One of the greatest Star Trek episodes is titled &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Darmok&quot;&gt;Darmok&lt;&#x2F;a&gt; in &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Star_Trek:_The_Next_Generation&quot;&gt;Star Trek: The Next Generation&lt;&#x2F;a&gt;.  It has the hallmarks of a great Star Trek: TNG episode: a first contact between two civilizations and a dilemma that is not solved by violence but by thinking and understanding.&lt;&#x2F;p&gt;
&lt;p&gt;One of the nicest logic programming languages is &lt;a href=&quot;http:&#x2F;&#x2F;minikanren.org&#x2F;&quot;&gt;miniKanren&lt;&#x2F;a&gt;. This is due to the fact that it is a small, relatively easy to understand &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Logic_programming&quot;&gt;Logic Programming (LP)&lt;&#x2F;a&gt; language and has implementations in many programming languages. This later feature allows for logic programming features to be used in many different environments, as a logic programming &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Domain-specific_language&quot;&gt;Domain-Specific language (DSL)&lt;&#x2F;a&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;A popular implementation of miniKanren is the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;clojure&#x2F;core.logic&quot;&gt;core.logic&lt;&#x2F;a&gt; library of the &lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;&quot;&gt;Clojure&lt;&#x2F;a&gt; programming language. In this article we aim to introduce miniKanren&#x2F;core.logic by encoding story elements of the Darmok episode of Star Trek: TNG (some spoilers for the episode will follow). &lt;&#x2F;p&gt;
&lt;p&gt;The episode is based around the fact that the Federation and the Tamarian people aim to establish successful first contact with each other. From the Federation, the crew of the starship Enterprise are sent to the planet El-Adrel where a Tamarian ship awaits them. Unfortunately attempts at communication fail from both sides and lead to some dangerous situations. The difficulty of communication arises from the fact that Tamarians communicate exclusively through allegory. This means that it is not enough to just decipher the words and grammar used in the Tamarian language, but the crew of the Enterprise must also understand the myths and historical events to which these allegories refer to. Within the episode multiple allegories are used by the Tamarians, such as with the phrase &amp;quot;Darmok and Jalad at Tanagra&amp;quot;, that utterly baffle the crew at first. However due to shared dangers and cooperation by the captain of the Enterprise, Picard, and the captain of the Tamarian ship, Darmok, they start to understand each other. In the end a successful first contact is made. This is captured by the newly coined allegory for first contact in the Tamarian language: &amp;quot;Picard and Dathon at El-Adrel&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;In this article we will use core.logic to write a logic program to represent the allegories used in the Darmok episode and to generate templates of the Darmok story through a sequence of allegories. A logic program is a bit different than the programs most people are used to. Instead of giving precise instructions to the computer one instead writes a goal, or a group of goals, that provide some logical restrictions on what one intends to achieve. With these goals the logic programming system can find the right answers. Note that the source for the code used this article can be found &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;darmok-core-logic&quot;&gt;here&lt;&#x2F;a&gt; in case you want to experiment along while reading this article.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s start off with a small logic program that can translate the Tamarian allegory that represents cooperation, the phrase &amp;quot;Darmok and Jalad at Tanagra&amp;quot;. We aim to translate this allegory to an equivalent allegory based on a human myth, as well as the English translation for it: &amp;quot;cooperation&amp;quot;. &lt;&#x2F;p&gt;
&lt;p&gt;In order to do this we give the Clojure definition of this goal as a logic program, then explain each element of it and how to use it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;cooperation &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[tam] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[hum] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh and Ekidu at Uruk.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[eng] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cooperation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])]))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First lets decipher the above definition. For people, unfamiliar with Clojure, the form &lt;code&gt;(defn cooperation [tam hum eng] ... )&lt;&#x2F;code&gt; defines a function named &lt;code&gt;cooperation&lt;&#x2F;code&gt; with the parameters &lt;code&gt;tam hum eng&lt;&#x2F;code&gt;. The part with &lt;code&gt;(l&#x2F;conde ... )&lt;&#x2F;code&gt; is a shorthand for &lt;code&gt;(core.logic&#x2F;conde ... )&lt;&#x2F;code&gt;; we will use &lt;code&gt;l&lt;&#x2F;code&gt; as the abbreviation for the &lt;code&gt;core.logic&lt;&#x2F;code&gt; namespace in the rest of this article. This &lt;code&gt;l&#x2F;conde&lt;&#x2F;code&gt; part functions as a way to connect various goals together. It creates a disjunction (elements separated by OR) of separate vectors of goals which it considers as conjunction (elements separated by AND). For people a bit familiar with boolean logic this a way to write a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Disjunctive_normal_form&quot;&gt;Disjunctive Normal Form&lt;&#x2F;a&gt;. To give a very simplified example the form &lt;code&gt;(l&#x2F;conde [A B] [C] )&lt;&#x2F;code&gt; with the goals &lt;code&gt;A, B, C&lt;&#x2F;code&gt; can be seen as a way to find the case where &lt;code&gt;(A &#x27;AND&#x27; B) &#x27;OR&#x27; C&lt;&#x2F;code&gt; holds. In the previous example &lt;code&gt;l&#x2F;conde&lt;&#x2F;code&gt; is called on a single vector of elements &lt;code&gt;[(l&#x2F;== [tam] [&amp;quot;Darmok and Jalad at Tanagra.&amp;quot;]) (l&#x2F;== [hum] [&amp;quot;Gilgamesh and Ekidu at Uruk.&amp;quot;]) (l&#x2F;== [eng] [&amp;quot;cooperation&amp;quot;])]&lt;&#x2F;code&gt;, meaning that this function wants each of the goals: &lt;code&gt;(l&#x2F;== [tam] [&amp;quot;Darmok and Jalad at Tanagra.&amp;quot;])&lt;&#x2F;code&gt; AND &lt;code&gt;(l&#x2F;== [hum] [&amp;quot;Gilgamesh and Ekidu at Uruk.&amp;quot;])&lt;&#x2F;code&gt; AND &lt;code&gt; (l&#x2F;== [eng] [&amp;quot;cooperation&amp;quot;])&lt;&#x2F;code&gt; fulfilled. &lt;&#x2F;p&gt;
&lt;p&gt;So now we know that this function takes three parameters and wants to ensure that the three goals all have to be met. But what do the goals themselves mean? They all have a similar structure in that they are using the equality in core.logic, &lt;code&gt;l&#x2F;==&lt;&#x2F;code&gt;, to unify elements. Unification is a core part of a logic programming system and it is used to constrain elements to the same possible values. For the first example &lt;code&gt;(l&#x2F;== [tam] [&amp;quot;Darmok and Jalad at Tanagra.&amp;quot;])&lt;&#x2F;code&gt;, the unification aims to ensure that the variable &lt;code&gt;tam&lt;&#x2F;code&gt; has the value of the string &lt;code&gt;&amp;quot;Darmok and Jalad at Tanagra.&amp;quot;&lt;&#x2F;code&gt;, which can be seen as the Tamarian phrase for cooperation. The other goals do this unification for the human mythology equivalent: &lt;code&gt;&amp;quot;Gilgamesh and Ekidu at Uruk.&amp;quot;&lt;&#x2F;code&gt; of this allegory, as well for the English word &lt;code&gt;&amp;quot;cooperation&amp;quot;&lt;&#x2F;code&gt;, for the variables &lt;code&gt;hum&lt;&#x2F;code&gt; and &lt;code&gt;eng&lt;&#x2F;code&gt;, respectively.&lt;&#x2F;p&gt;
&lt;p&gt;We have now given an anatomy of this logic program that does unification on phrases relating to cooperation, but how do we use it? For this we need two things: a set of logic variables and a way to tell the system to run the logic program. The function &lt;code&gt;l\run*&lt;&#x2F;code&gt; does exactly that, which for the given set of parameters tries to find all examples where the goals are fulfilled.&lt;&#x2F;p&gt;
&lt;p&gt;So if we evaluate the following code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;run* &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cooperation&lt;&#x2F;span&gt;&lt;span&gt; tam hum eng))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we get the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;([&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh and Ekidu at Uruk.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cooperation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What &lt;code&gt;l\run*&lt;&#x2F;code&gt; is doing is taking a given list of logic variables and tries to list all the possible values these variables can take. Here it only lists a single possible set of values for the variables &lt;code&gt;tam&lt;&#x2F;code&gt;, &lt;code&gt;hum&lt;&#x2F;code&gt; and &lt;code&gt;eng&lt;&#x2F;code&gt;. This should not be surprising as there is only exactly one way each of the variables &lt;code&gt;tam&lt;&#x2F;code&gt;, &lt;code&gt;hum&lt;&#x2F;code&gt;, and &lt;code&gt;eng&lt;&#x2F;code&gt; can be fulfilled by the &amp;quot;cooperation&amp;quot; goal based on the definition we gave above.  The variable &lt;code&gt;tam&lt;&#x2F;code&gt; gets unified with the string value &lt;code&gt;&amp;quot;Darmok and Jalad at Tanagra.&amp;quot;&lt;&#x2F;code&gt;, &lt;code&gt;hum&lt;&#x2F;code&gt; with &lt;code&gt;&amp;quot;Gilgamesh and Ekidu at Uruk.&amp;quot;&lt;&#x2F;code&gt; and &lt;code&gt;eng&lt;&#x2F;code&gt; with &lt;code&gt;&amp;quot;cooperation&amp;quot;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In the previous case we used only &#x27;fresh&#x27; logic variables (variables that have no constraints placed upon their possible values yet), but instead we can also use a specific value in our goal instead. In the following example we only have two variables. Instead of a variable for first parameter used in the cooperation goal we give the string `&amp;quot;Darmok and Jalad at Tanagra.&amp;quot; as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;run* &lt;&#x2F;span&gt;&lt;span&gt;[hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cooperation &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; hum eng))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which gives the result:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;([&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh and Ekidu at Uruk.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cooperation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are only two variables listed in the answer, as there are only two variables given for &lt;code&gt;l&#x2F;run*&lt;&#x2F;code&gt; to check in our initial case. Otherwise the answer is exactly what we would expect as there is only one possible way these variables can be bound in our logic program.&lt;&#x2F;p&gt;
&lt;p&gt;Now lets try a run where there are no possible valid answers, giving the word &lt;code&gt;&amp;quot;challenge&amp;quot;&lt;&#x2F;code&gt; as a parameter:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;run* &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cooperation&lt;&#x2F;span&gt;&lt;span&gt; tam hum &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;challenge&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This returns an empty list of answers: &lt;code&gt;()&lt;&#x2F;code&gt;, as there are no ways to unify the word &lt;code&gt;&amp;quot;challenge&amp;quot;&lt;&#x2F;code&gt; inside the goal of cooperation.&lt;&#x2F;p&gt;
&lt;p&gt;Given we got the basics of logic programs covered, lets expand our example into something more complex.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of one single allegory, we now define five of them based on the various allegories used in the Darmok episode. The functions representing these goals are all named after the English word translation: &lt;code&gt;failure&lt;&#x2F;code&gt;, &lt;code&gt;common-enemy&lt;&#x2F;code&gt;, &lt;code&gt;cooperation&lt;&#x2F;code&gt;, &lt;code&gt;successful-cooperation&lt;&#x2F;code&gt; and &lt;code&gt;successful-first-contact&lt;&#x2F;code&gt;. They all follow the same structure as the &lt;code&gt;cooperation&lt;&#x2F;code&gt; allegory we previously examined in detail. In addition we also define a function for representing any allegory, aptly named &lt;code&gt;allegory&lt;&#x2F;code&gt;. This is a goal that can be fulfilled by any of the allegories named above.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;failure &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[tam] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[hum] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh, his plant eaten by a snake.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[eng] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])]))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;common-enemy &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[tam] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[hum] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bull of Heaven.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[eng] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;common-enemy&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])]))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;cooperation &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[tam] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[hum] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh and Ekidu at Uruk.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[eng] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cooperation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])]))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;successful-cooperation &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[tam] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad on the ocean.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[hum] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh and Ekidu, after the Bull&amp;#39;s defeat.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[eng] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;successful-cooperation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])]))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;successful-first-contact &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[tam] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[hum] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;== &lt;&#x2F;span&gt;&lt;span&gt;[eng] [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;successful-first-contact&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])]))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;allegory &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt; tam hum eng)]
&lt;&#x2F;span&gt;&lt;span&gt;           [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;common-enemy&lt;&#x2F;span&gt;&lt;span&gt; tam hum eng)]
&lt;&#x2F;span&gt;&lt;span&gt;           [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cooperation&lt;&#x2F;span&gt;&lt;span&gt; tam hum eng)]
&lt;&#x2F;span&gt;&lt;span&gt;           [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;successful-cooperation&lt;&#x2F;span&gt;&lt;span&gt; tam hum eng)]
&lt;&#x2F;span&gt;&lt;span&gt;           [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;successful-first-contact&lt;&#x2F;span&gt;&lt;span&gt; tam hum eng)]))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There should not be any very surprising elements in this part, but we give two quick observations before continuing. &lt;&#x2F;p&gt;
&lt;p&gt;First, the human equivalent for Tamarian allegories are based on the Gilgamesh story, which is also explicitly mentioned in the episode, that provides a way for captain Picard to connect with the Tamarian captain Dathon. The only exception to this is &lt;code&gt;&amp;quot;Picard and Dathon at El-Adrel.&amp;quot;&lt;&#x2F;code&gt; which is an allegory coined at the end of the episode as a term for first contact between cultures. It seems fitting to use this as an allegory from a human perspective as well. &lt;&#x2F;p&gt;
&lt;p&gt;Second, to reiterate how &lt;code&gt;l&#x2F;conde&lt;&#x2F;code&gt; works with choices, we remark that each of the named allegories in the &lt;code&gt;allegory&lt;&#x2F;code&gt; function are in their own vector (indicated by each goal inside their own &lt;code&gt;[]&lt;&#x2F;code&gt; brackets). This indicates that any of &lt;code&gt;failure&lt;&#x2F;code&gt;, &lt;code&gt;common-enemy&lt;&#x2F;code&gt;, &lt;code&gt;cooperation&lt;&#x2F;code&gt;, &lt;code&gt;successful-cooperation&lt;&#x2F;code&gt; or &lt;code&gt;successful-first-contact&lt;&#x2F;code&gt; can fulfill the goal of &lt;code&gt;allegory&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;We can test this later notion by running a short logic program for finding all possible allegories:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;run* &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory&lt;&#x2F;span&gt;&lt;span&gt; tam hum eng))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which returns:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;([&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh, his plant eaten by a snake.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bull of Heaven.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;common-enemy&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh and Ekidu at Uruk.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cooperation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad on the ocean.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh and Ekidu, after the Bull&amp;#39;s defeat.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;successful-cooperation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;successful-first-contact&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This shows five possible answers because, as mentioned, any of the above allegories can fulfill the given goal.&lt;&#x2F;p&gt;
&lt;p&gt;By default &lt;code&gt;l&#x2F;run*&lt;&#x2F;code&gt; will list all possible answers for a given logic program, which is a very powerful feature for exhaustively searching for all the solutions to a given problem. However this list can be large, and even infinite! In such scenarios there is a way to limit the answers to a certain number when searching by using &lt;code&gt;l&#x2F;run&lt;&#x2F;code&gt; (note the lack of the &lt;code&gt;*&lt;&#x2F;code&gt; character) directly followed by the number of answers we want returned. &lt;&#x2F;p&gt;
&lt;p&gt;For example, the call:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;run &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;[tam hum eng]
&lt;&#x2F;span&gt;&lt;span&gt;       (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory&lt;&#x2F;span&gt;&lt;span&gt; tam hum eng))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;will return only two possible answers: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;([&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh, his plant eaten by a snake.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bull of Heaven.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;common-enemy&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now as we are getting a bit more familiar with the allegories in this example, we do not want to write out all three versions of each allegory each time. We can do this by defining a new goal &lt;code&gt;allegory-short&lt;&#x2F;code&gt; that succeeds for any phrase that identifies one of the five allegories:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;allegory-tam &lt;&#x2F;span&gt;&lt;span&gt;[tam]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;fresh &lt;&#x2F;span&gt;&lt;span&gt;[x y]
&lt;&#x2F;span&gt;&lt;span&gt;           (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory&lt;&#x2F;span&gt;&lt;span&gt; tam x y)))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;allegory-hum &lt;&#x2F;span&gt;&lt;span&gt;[hum]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;fresh &lt;&#x2F;span&gt;&lt;span&gt;[x y]
&lt;&#x2F;span&gt;&lt;span&gt;           (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory&lt;&#x2F;span&gt;&lt;span&gt; x hum y)))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;allegory-eng &lt;&#x2F;span&gt;&lt;span&gt;[eng]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;fresh &lt;&#x2F;span&gt;&lt;span&gt;[x y]
&lt;&#x2F;span&gt;&lt;span&gt;           (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory&lt;&#x2F;span&gt;&lt;span&gt; x y eng)))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;allegory-short &lt;&#x2F;span&gt;&lt;span&gt;[x]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory-tam&lt;&#x2F;span&gt;&lt;span&gt; x)]
&lt;&#x2F;span&gt;&lt;span&gt;           [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory-hum&lt;&#x2F;span&gt;&lt;span&gt; x)]
&lt;&#x2F;span&gt;&lt;span&gt;           [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory-eng&lt;&#x2F;span&gt;&lt;span&gt; x)]))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;allegory-short&lt;&#x2F;code&gt; function was defined by writing out the three scenarios by which a phrase could be part of an allegory: it is either the Tamarian allegory, the Human allegory or the English translation. The only new structure we use here from core.logic is &lt;code&gt;l&#x2F;fresh&lt;&#x2F;code&gt; which lets us introduce new (fresh) logic variables which have no binding as of yet. When using the various forms of &lt;code&gt;l&#x2F;run&lt;&#x2F;code&gt; the parameters for the function are automatically given as fresh variables, but this function allows us to create them inside other parts of the logic program as well.&lt;&#x2F;p&gt;
&lt;p&gt;Now if we want to list, for example, five possible phrases that form part of an allegory we can call:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;run &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span&gt;[x]
&lt;&#x2F;span&gt;&lt;span&gt;       (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory-short&lt;&#x2F;span&gt;&lt;span&gt; x))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which will return five of the possible terms that are used as part of allegories.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh, his plant eaten by a snake.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With all the functions for logic programming we built up, lets try our hand at creating a logic program that generates variants of the Darmok story expressed through a sequence of allegories. In this scenario, much like in the episode any phrase, a Tamarian- or Human allegory or their English equivalent, could be used to describe parts of the story. In essence we can represent the story as a list of phrases, for example: (&amp;quot;failure&amp;quot;, &amp;quot;Beast at Tanagra.&amp;quot; &amp;quot;Darmok and Jalad at Tanagra.&amp;quot; &amp;quot;successful-first-contact&amp;quot; ). &lt;&#x2F;p&gt;
&lt;p&gt;We could put many restrictions on the order of the phrases, but for the sake of brevity we just want to ensure that each story starts with a phrase for &lt;code&gt;failure&lt;&#x2F;code&gt; and ends with a phrase for &lt;code&gt;successful-first-contact&lt;&#x2F;code&gt; much like the structure of the actual episode. In addition let&#x27;s assume our stories are only five phrases long. &lt;&#x2F;p&gt;
&lt;p&gt;We can now define logic program to generate such stories as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;failure-any &lt;&#x2F;span&gt;&lt;span&gt;[x]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;fresh &lt;&#x2F;span&gt;&lt;span&gt;[a1 a2 a3]
&lt;&#x2F;span&gt;&lt;span&gt;           (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt; x a1 a2)]
&lt;&#x2F;span&gt;&lt;span&gt;                    [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt; a1 x a2)]
&lt;&#x2F;span&gt;&lt;span&gt;                    [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt; a1 a2 x)])))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;successful-first-contact-any &lt;&#x2F;span&gt;&lt;span&gt;[x]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;fresh &lt;&#x2F;span&gt;&lt;span&gt;[a1 a2 a3]
&lt;&#x2F;span&gt;&lt;span&gt;           (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;successful-first-contact&lt;&#x2F;span&gt;&lt;span&gt; x a1 a2)]
&lt;&#x2F;span&gt;&lt;span&gt;                    [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;successful-first-contact&lt;&#x2F;span&gt;&lt;span&gt; a1 x a2)]
&lt;&#x2F;span&gt;&lt;span&gt;                    [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;successful-first-contact&lt;&#x2F;span&gt;&lt;span&gt; a1 a2 x)])))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;five-element-story &lt;&#x2F;span&gt;&lt;span&gt;[x1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; x2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; x3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; x4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; x5]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;fresh &lt;&#x2F;span&gt;&lt;span&gt;[a1 a2 a3]
&lt;&#x2F;span&gt;&lt;span&gt;           (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;failure-any&lt;&#x2F;span&gt;&lt;span&gt; x1)
&lt;&#x2F;span&gt;&lt;span&gt;                     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory-short&lt;&#x2F;span&gt;&lt;span&gt; x2)
&lt;&#x2F;span&gt;&lt;span&gt;                     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory-short&lt;&#x2F;span&gt;&lt;span&gt; x3)
&lt;&#x2F;span&gt;&lt;span&gt;                     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;allegory-short&lt;&#x2F;span&gt;&lt;span&gt; x4)
&lt;&#x2F;span&gt;&lt;span&gt;                     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;successful-first-contact-any&lt;&#x2F;span&gt;&lt;span&gt; x5)])))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Every construct we used to build these functions should be familiar based on the previous examples. We just needed to define two special versions of our goals for the shorthand version of allegories: one for failure and one for successful first contact.&lt;&#x2F;p&gt;
&lt;p&gt;For example if we want five solutions that fulfill all the criteria for such stories we can call the following code: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;run &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span&gt;[x1 x2 x3 x4 x5]
&lt;&#x2F;span&gt;&lt;span&gt;       (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;five-element-story&lt;&#x2F;span&gt;&lt;span&gt; x1 x2 x3 x4 x5))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which for example could return:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;([&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;successful-first-contact&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As one can see the logic program will exhaustively go through all the possible ways the goals can fulfilled and list them up to limit given as a parameter for &lt;code&gt;l&#x2F;run&lt;&#x2F;code&gt;. The results are not necessarily unique if there are multiple ways to fulfill the goals. This can be seen in the first and second answers as the phrase &amp;quot;Picard and Dathon at El-Adrel.&amp;quot; is both the Tamarian and Human allegory for successful first contact. &lt;&#x2F;p&gt;
&lt;p&gt;The above restrictions allow for a lot of the same allegories used within the story. For this, one can define new restrictions and further fine tune the story generation. For example, one can create restrictions on the number of duplicate phrases used, or could ensure that there is more diversity in the phrase type (Tamarian, Human, English) is used. Declaring new restrictions, combining them with existing ones and using the same mechanism to derive any number of answers is one of the core strengths of a logic programming system such as core.logic.&lt;&#x2F;p&gt;
&lt;p&gt;As a final example to show how logic programming can be embedded into a (regular) program, we create a logic program inside a regular Clojure function that creates n-number of stories of a given length. Take a quick look at the following code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;n-element-story &lt;&#x2F;span&gt;&lt;span&gt;[nr-of-elements stories]
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;[vars (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;repeatedly&lt;&#x2F;span&gt;&lt;span&gt; nr-of-elements l&#x2F;lvar)
&lt;&#x2F;span&gt;&lt;span&gt;        first-var (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span&gt; vars)
&lt;&#x2F;span&gt;&lt;span&gt;        middle (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;drop-last &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rest&lt;&#x2F;span&gt;&lt;span&gt; vars))
&lt;&#x2F;span&gt;&lt;span&gt;        last-var (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;last&lt;&#x2F;span&gt;&lt;span&gt; vars)]
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;run&lt;&#x2F;span&gt;&lt;span&gt; stories [q]
&lt;&#x2F;span&gt;&lt;span&gt;           (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;conde &lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;==&lt;&#x2F;span&gt;&lt;span&gt; q vars)
&lt;&#x2F;span&gt;&lt;span&gt;                     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;distincto&lt;&#x2F;span&gt;&lt;span&gt; q)
&lt;&#x2F;span&gt;&lt;span&gt;                     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;failure-any&lt;&#x2F;span&gt;&lt;span&gt; first-var)
&lt;&#x2F;span&gt;&lt;span&gt;                     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;successful-first-contact-any&lt;&#x2F;span&gt;&lt;span&gt; last-var)
&lt;&#x2F;span&gt;&lt;span&gt;                     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l&#x2F;everyg&lt;&#x2F;span&gt;&lt;span&gt; allegory-short middle)]))))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Without going too in-depth on every part of this function, it programmatically creates n-number of fresh variables based on the given parameter. It then unifies these with the parameters of a run execution inside the function and returns them. For example, six stories of four elements can be requested by the call:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;n-element-story &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4 6&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which will result in the stories:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;((&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt; (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad on the ocean.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt; (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Gilgamesh, his plant eaten by a snake.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt; (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;failure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt; (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bull of Heaven.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt; (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Shaka, when the walls fell.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Darmok and Jalad at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Beast at Tanagra.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Picard and Dathon at El-Adrel.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There you have it, a very quick overview of using core.logic for logic programming. If you would like to experiment further the code used in this article is &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;darmok-core-logic&quot;&gt;available&lt;&#x2F;a&gt;. Here we only scratched the surface of what is possible in a logic programming environment such as core.logic. Feel free to check it out, or any other &lt;a href=&quot;http:&#x2F;&#x2F;minikanren.org&#x2F;#implementations&quot;&gt;miniKanren implementation available in your language&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I hope that, much like the Darmok episode, this article has expanded your horizons on communicating. Logic programming is a very interesting, and often underutilized programming paradigm. The core.logic&#x2F;miniKanren logic programming language is a great system to get started with it. I hope that this, perhaps first, contact with Logic Programming or core.logic aids you in your future endeavors.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Probable Mystery Machine</title>
        <published>2019-12-06T00:00:00+00:00</published>
        <updated>2019-12-06T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/probable-mystery-machine/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/probable-mystery-machine/</id>
        
        <content type="html">&lt;p&gt;Scooby Doo is mystery horror cartoon series in which a group of teenagers named Fred, Daphne, Velma and Shaggy alongside the titular Great Dane named Scooby-Doo, ride around in their van named &amp;quot;The Mystery Machine&amp;quot; solving mysteries. The episodes of the show generally follow a set structure. First their van tends to break down near a place apparently haunted by a ghost or another supernatural creature. They would offer to solve the mystery behind the existence of the monster and start looking for clues. The monster tries to scare them away while they find various pieces of evidence relating to it, all pointing to the fact that the monster is not real. At a certain point the creature starts to chase them until they can trap or otherwise incapacitate it. Finally they figure out that the monster is person in a costume who put the mystery in place to scare people away for some (financial) reason, and who would have gotten away with it &amp;quot;if not for them meddling kids&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;While having a van named The Mystery Machine can help solving mysteries, we can turn our computer into a mystery solving machine as well. We can represent possible stories in a Scooby Doo episode using some logical facts (e.g. a each adventure has a monster in it) as well as probabilities (e.g. there is a 40% chance a monster will be a ghost). In particular, we can use a probabilistic logic programming language, namely &lt;a href=&quot;https:&#x2F;&#x2F;dtai.cs.kuleuven.be&#x2F;problog&#x2F;&quot;&gt;ProbLog&lt;&#x2F;a&gt; to guide us through a scenario of a Scooby Doo story.&lt;&#x2F;p&gt;
&lt;p&gt;First let&#x27;s start off with a basic scenario on how a Scooby Doo adventure starts. In general there is usually some sort of an issue why the group must stop during their travels. Although there are many possible causes for this in the cartoon, here we represent three of them. Either they get a flat tire, they unexpectedly run out of gas, or there is some engine trouble that they have to deal with. For each of these scenarios there is a probability with which they happen. This probability we assume is 40% for a flat tire, 30% for being unexpectedly out of gas, and 60% for having an engine trouble. These are the probabilistic facts in our scenario, as each of these facts have a probability attached to them with which they occur. If any of these facts hold, it will lead to an adventure. This type of knowledge we can represent as a rule. Finally, we aim to query this scenario for the probability that an adventure will occur. &lt;&#x2F;p&gt;
&lt;p&gt;In ProbLog the above scenario can be represented as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;% Probabilistic facts:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;::flat_tire.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;::out_of_gas.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;::engine_trouble.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Rules:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- flat_tire.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- out_of_gas.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- engine_trouble.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Queries:.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(adventure_start).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As one can see, a ProbLog program is a combination of probabilistic facts, rules and queries (with comments in lines following &lt;code&gt;%&lt;&#x2F;code&gt;). Probabilistic facts represent the facts of the domain with an attached probability between 0 and 1. Rules are deterministic rules (i.e. they have no probabilities attached) that show the system how new facts can be inferred from existing ones. Finally queries allow us to ask the program questions, such as the probabilities for a certain fact occuring. These program elements are similar to those employed in &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;prolog-price-of-peace&#x2F;&quot;&gt;Prolog&lt;&#x2F;a&gt;, where a program consists of facts, rules and queries, with the main difference that there are probabilities attached to each fact.&lt;&#x2F;p&gt;
&lt;p&gt;We can use infer new facts from these probabilistic facts using rules. For example, if we want to infer the probability of an adventure the query: &lt;code&gt;query(adventure).&lt;&#x2F;code&gt; will return the probability &lt;code&gt;0.832&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now we can make our Scooby Doo scenario a bit more complex. Suppose the group starts an adventure, after their van stopped working somehow, and they quickly realize that there is a mystery in the area. The location of this mystery is either an abandoned mansion, a local museum, an old theme park, or a nearby farm. We set the probabilities for each of these locations occurring at &lt;code&gt;0.3&lt;&#x2F;code&gt; for the abandoned mansion, &lt;code&gt;0.3&lt;&#x2F;code&gt; for a local museum, &lt;code&gt;0,2&lt;&#x2F;code&gt; for an old theme park and &lt;code&gt;0.2&lt;&#x2F;code&gt; for the nearby farm. We also assume that there is only one mystery location in each adventure. &lt;&#x2F;p&gt;
&lt;p&gt;In order to express the requirements for the adventure locations succinctly, we make use of a feature called annotated disjunctions. This allows for a more readable way to state that only one of the stated choices holds true, with a given probability. Below is the ProbLog program extended to include this information.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;% Probabilistic facts:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;::flat_tire.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;::out_of_gas.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;::engine_trouble.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(abandoned_mansion); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(local_museum); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(old_theme_park); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(nearby_farm).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Rules:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- flat_tire.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- out_of_gas.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- engine_trouble.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure &lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(X), adventure_start.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;two_locations &lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(X), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(Y), X \== Y.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Queries:.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(two_locations).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are two other new concepts that we showcase here. One is using variables, notably the &lt;code&gt;X&lt;&#x2F;code&gt; in &lt;code&gt;monster_location(X)&lt;&#x2F;code&gt;, which helps to express that the values used for this variable all express the monster&#x27;s location. The other is the use of restrictions in the use &lt;code&gt;two_locations&lt;&#x2F;code&gt; to showcase that the probability for two monster locations occurring at once is 0. There are number of &lt;a href=&quot;https:&#x2F;&#x2F;problog.readthedocs.io&#x2F;en&#x2F;latest&#x2F;prolog.html&quot;&gt;built-ins&lt;&#x2F;a&gt; that one can use for defining Problog models. In this case we use to define a rule to express that the &lt;code&gt;two_locations&lt;&#x2F;code&gt; fact should be derived if there are two distinct monster locations. Given the example above, due to the use of an annotated disjunction for defining the monster location, the query &lt;code&gt;query(two_locations).&lt;&#x2F;code&gt; will correctly probability of 0 for the chance of two monster locations at the same time.&lt;&#x2F;p&gt;
&lt;p&gt;The final ingredient for a Scooby Doo story that we represent in this article is the monster. There are five types of monsters that can occur: a Mummy, a Zombie, a Ghost, a Swamp Monster and a Headless Horseman.  The chance at which these monsters occur is dependent on the current location. In the stories that we represent only 1 monster can occur in an adventure. &lt;&#x2F;p&gt;
&lt;p&gt;Such cases can also be represented with annotated disjunctions, but they are now used as the head (which is the left hand side portion of the rule, with the &lt;code&gt;:-&lt;&#x2F;code&gt; sign separating the two sides). This allows us to express the conditions, i.e. the monster locations, that is required for these facts. See our final example for the probabilities of monsters given the locations:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;% Probabilistic facts:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;::flat_tire.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;::out_of_gas.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;::engine_trouble.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(abandoned_mansion); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(local_museum); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(old_theme_park); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(nearby_farm).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Rules:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- flat_tire.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- out_of_gas.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure_start &lt;&#x2F;span&gt;&lt;span&gt;:- engine_trouble.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(ghost); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(vampire); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(zombie) :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(abandoned_mansion).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(mummy); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(headless_horseman); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(ghost) :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(local_museum).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(zombie); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(ghost); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(mummy) :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(old_theme_park).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(ghost); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(zombie); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(headless_horseman); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(vampire) :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(nearby_farm).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;adventure &lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(X), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(Y), adventure_start.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;any_monster_location &lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;any_monster &lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;two_locations &lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(X), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster_location&lt;&#x2F;span&gt;&lt;span&gt;(Y), X \== Y.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;two_monsters &lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(X), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(Y), X \== Y.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;vampire_after_flat_tire &lt;&#x2F;span&gt;&lt;span&gt;:- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(vampire), flat_tire.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;% Queries:.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(adventure_start).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(any_monster_location).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(any_monster).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(adventure).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(two_locations).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(two_monsters).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;monster&lt;&#x2F;span&gt;&lt;span&gt;(ghost)).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;(vampire_after_flat_tire).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this example we also show a number of interesting facts that we might want to be able to query. The probability that an adventure starts is 0.832. Given that the probability for a monster location and a monster existing is 1 in both cases, and adventure requires only an &amp;quot;adventure start&amp;quot;, a &amp;quot;monster&amp;quot; and its &amp;quot;location&amp;quot;, the probability for an adventure happening is also 0.832. As mentioned before, due to the annotated disjunctions the probability of having two monsters or two locations is 0. We can also query for facts such as the probability of a monster occuring, which is inferred based on both the conditional probability given the monster location and the probability of the monster location itself. For example the probability of the monster being a ghost is 0.37. Finally we can calculate the probabilities for any particular scenario that we create, such as the probability of having the monster be a vampire after having a flat tire: 0.064. &lt;&#x2F;p&gt;
&lt;p&gt;As one can see many spooky scenarios can be explored with ProbLog. Here we only taken a quick peek for representing a small portion of Scooby Doo stories, but these can also be applied to other domains, be it reasoning in the legal, financial, health and other fields. So do not be scared off and give it a try for any domain modelling you might encounter involving probabilities!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Solving Financial Regulatory Compliance Using Software Contracts</title>
        <published>2019-09-11T00:00:00+00:00</published>
        <updated>2019-09-11T00:00:00+00:00</updated>
        <author>
          <name>Newres Al Haider</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/publication/solving-compliance-software-contracts/" type="text/html"/>
        <id>https://www.newresalhaider.com/publication/solving-compliance-software-contracts/</id>
        
        <content type="html">&lt;h1 id=&quot;abstract&quot;&gt;Abstract&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;Semantic Web technologies have shown to have great potential in many different domains, to facilitate knowledge representation, exchange and reasoning, in a formal and yet both human and machine understandable way. In particular, within the health domain, they enable knowledge integration and understanding by explicitly defining and linking concepts and relationships using ontologies to information within clinical knowledge bases. This additional metadata also allows for automated decision support and semantic based analytics to be implemented, that facilitate improved healthcare at a lower cost. Unfortunately many existing datasets in healthcare environments are still stored in relational databases, as opposed to using semantic technologies. Due to this, the link with explicit metadata is often lacking or non-existent. Furthermore, both the databases and the clinical terminologies can be considerably large, making the mapping and subsequent uses of the information a difficult process. In a full fledged decision support system the level and accuracy of the mapping can greatly influence the effectiveness of any subsequent analysis and decision support tasks. This is especially true in clinical scenarios, where very large and complex sets of terms need to be mapped to relational databases. In this paper we aim to provide a general approach for interlinking relational data with clinical ontology based metadata that allows for a fine grade evaluation, with respect to the mapping&#x27;s impact on analytics. We evaluate our approach by mapping information from clinical terminologies, such as SNOMED CT, to a large laboratory dataset contained in a relational database, with the goal of creating a full fledged, semantically enabled, analytics and decision support system.&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;authors&quot;&gt;Authors&lt;&#x2F;h1&gt;
&lt;p&gt;Newres Al Haider, Dilhan Thilakarathne, Joost Bosman&lt;&#x2F;p&gt;
&lt;h1 id=&quot;presented-at&quot;&gt;Presented at&lt;&#x2F;h1&gt;
&lt;p&gt;22ND INTERNATIONAL CONFERENCE ON APPLICATIONS OF DECLARATIVE PROGRAMMING AND KNOWLEDGE MANAGEMENT Declare 19
&lt;a href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;pdf&#x2F;1909.08965&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-project&quot;&gt;Related Project&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;project&#x2F;legal-banking-compliance&#x2F;&quot;&gt;Legal Banking Compliance&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Aesopica, Part 5: Blank Nodes</title>
        <published>2019-06-08T00:00:00+00:00</published>
        <updated>2019-10-06T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/aesopica-5/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/aesopica-5/</id>
        
        <content type="html">&lt;p&gt;This article is the fifth part of a series, examining the use of the Clojure language for representing Linked Data, using examples from Aesop&#x27;s stories. The topic of this article is to explain the somewhat contentious subject of blank nodes. &lt;&#x2F;p&gt;
&lt;p&gt;One of the strengths of &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;RDF&#x2F;&quot;&gt;RDF&lt;&#x2F;a&gt; as a graph representation format is the way resources are named. Through the use of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Uniform_Resource_Identifier&quot;&gt;Uniform Resource Identifiers (URIs)&lt;&#x2F;a&gt; every element of the graph can be uniquely identified. With such generic and powerful facilities for naming it can easily represent information in any domain. For example on &lt;a href=&quot;https:&#x2F;&#x2F;schema.org&#x2F;&quot;&gt;schema.org&lt;&#x2F;a&gt; elements have been defined for the notion of a &lt;a href=&quot;http:&#x2F;&#x2F;schema.org&#x2F;MedicalCondition&quot;&gt;medical condition (http:&#x2F;&#x2F;schema.org&#x2F;MedicalCondition)&lt;&#x2F;a&gt;, &lt;a href=&quot;http:&#x2F;&#x2F;schema.org&#x2F;employee&quot;&gt;employee (http:&#x2F;&#x2F;schema.org&#x2F;employee)&lt;&#x2F;a&gt; and &lt;a href=&quot;http:&#x2F;&#x2F;schema.org&#x2F;BankAccount&quot;&gt;bank account (http:&#x2F;&#x2F;schema.org&#x2F;BankAccount)&lt;&#x2F;a&gt;, just to name a few. In particular the namespacing part of the URI, e.g.: http:&#x2F;&#x2F;schema.org&#x2F; for http:&#x2F;&#x2F;schema.org&#x2F;employee , helps to ensure that concepts can be uniquely named, even in scenarios with multiple definitions of the same concept. &lt;&#x2F;p&gt;
&lt;p&gt;Blank nodes go against this notion of making everything explicitly named. In fact an alternative name for a blank node is &amp;quot;an anonymous resource&amp;quot;&amp;quot;. Instead of giving a resource an explicit name with a URI a placeholder is used. This indicates the existence of the resource, but does not tie it together with a namespaced name. In the Turtle syntax for RDF we can use a label prefixed by &lt;code&gt;_:&lt;&#x2F;code&gt; to indicate a blank node. For example, the following RDF graph states that the fox and the stork both give out an invitation.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#fox&amp;gt; &amp;lt;#gives-invitation&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;_:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;invitation1.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#stork&amp;gt; &amp;lt;#gives-invitation&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;_:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;invitation2.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Due to the invitations having different names we can expect them to be different resources. However the exact names of these resources do not matter. For example the RDF triples below have arguably the same meaning: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#fox&amp;gt; &amp;lt;#gives-invitation&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;_:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;abc.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#stork&amp;gt; &amp;lt;#gives-invitation&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;_:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;xyz.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is important to reiterate that the blank nodes are not resource identifiers such as URIs. They are also only local in scope: an &lt;code&gt;_:invitation1&lt;&#x2F;code&gt; in one graph and a &lt;code&gt;_:invitation1&lt;&#x2F;code&gt; in another are not referring to the same thing. Even resources used in the similar places in different graphs are not the same. For example the &lt;code&gt;_:abc&lt;&#x2F;code&gt; and the  &lt;code&gt;_:invitation1&lt;&#x2F;code&gt; used in the above graphs, while expressing the same meaning, are not the same resource.&lt;&#x2F;p&gt;
&lt;p&gt;The above features are both the strength and the weakness of using anonymous resources. We are not required to use a specific named identifier, but this makes referring to resources and comparing them more difficult. &lt;&#x2F;p&gt;
&lt;p&gt;There are number of scenarios where such anonymous resources can be useful. For example, when representing complex structures not easily expressed in triples where we would need &amp;quot;placeholder nodes&amp;quot; in the graph but do not particularly care about its naming. In other cases we want to hide some information, and blank nodes could be used as placeholders for named resources. A good overview of the various uses of Blank Nodes can be found in the paper: &lt;a href=&quot;https:&#x2F;&#x2F;link.springer.com&#x2F;chapter&#x2F;10.1007&#x2F;978-3-642-25073-6_27&quot;&gt;On Blank Nodes&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;With the above description of blank nodes, we also want to provide something similar in our Clojure based RDF representation as well. As we are using keywords to represent URIs in our Clojure representation (i.e. :fox and :rdf&#x2F;type), a natural element to differentiate them is to use symbols for blank nodes. Symbols are created in Clojure by prefixing it with &lt;code&gt;&#x27;&lt;&#x2F;code&gt;. For example in the Clojure based RDF representation we use blank nodes for representing invitations:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fox-and-stork-blank-node-edn
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;context
&lt;&#x2F;span&gt;&lt;span&gt;   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nil &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:rdf &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;facts
&lt;&#x2F;span&gt;&lt;span&gt;   #{[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :gives-invitation &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;invitation1]
&lt;&#x2F;span&gt;&lt;span&gt;     [&amp;#39;invitation1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:has-invited :stork&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&amp;#39;invitation1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:has-food :soup&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&amp;#39;invitation1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:serves-using :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :gives-invitation &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;invitation2]
&lt;&#x2F;span&gt;&lt;span&gt;     [&amp;#39;invitation2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:has-invited :fox&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&amp;#39;invitation2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:has-food :crumbled-food&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&amp;#39;invitation2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:serves-using :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :can-eat-food-served-using :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :can-not-eat-food-served-using :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :can-eat-food-served-using :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :can-not-eat-food-served-using :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]}})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above is our version of &amp;quot;The Fox and the Stork&amp;quot; story that we previously explored in this series of articles The main difference is that two blank nodes are used:  &lt;code&gt;&#x27;invitation1&lt;&#x2F;code&gt; and &lt;code&gt;&#x27;invitation2&lt;&#x2F;code&gt;, instead of named resources. Perhaps the story teller might want to hide some details of their invitations.&lt;&#x2F;p&gt;
&lt;p&gt;As with all the previous features in this series of articles, blank nodes been implemented in our implementation of this syntax in the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt; library for using Clojure to write Linked Data. Hiding details with blank nodes, even in Clojure, is now just one library away.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Exploring the Deep, Part 1: Introduction</title>
        <published>2019-02-04T00:00:00+00:00</published>
        <updated>2019-02-04T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/exploring-the-deep/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/exploring-the-deep/</id>
        
        <content type="html">&lt;p&gt;Deep Learning is a field within Artificial Intelligence (AI) that has got quite a lot of attention lately, due to some truly impressive results in recent years. From recognizing objects in images with accuracy that rivals humans, to generating realistic looking texts, to even &lt;a href=&quot;https:&#x2F;&#x2F;deepmind.com&#x2F;blog&#x2F;alphastar-mastering-real-time-strategy-game-starcraft-ii&#x2F;&quot;&gt;beating professional players in Starcraft 2&lt;&#x2F;a&gt;, some truly groundbreaking applications are done with Deep Learning techniques.&lt;&#x2F;p&gt;
&lt;p&gt;As someone whose AI background is more &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Symbolic_artificial_intelligence&quot;&gt;Symbolic Artifical Intelligence&lt;&#x2F;a&gt;, but is anxious to learn, it is a good time as any to explore this field. Thankfully there are some excellent books and tutorials out there to get a nice start. &lt;&#x2F;p&gt;
&lt;p&gt;One book that has caught my attention is &lt;a href=&quot;https:&#x2F;&#x2F;d2l.ai&#x2F;index.html&quot;&gt;Dive into Deep Learning&lt;&#x2F;a&gt;, which seems to have a nice overview of the field, while being very practical at the same time. I aim to work through the book and share my notes, summaries and experiences on this journey in a series of articles. My goal is to write these articles for a bit more broader audience than the book itself. I hope that even those with less exposure to the field than me could follow along, and if a topic piques their interest, they can use the book for a more detailed reference point. &lt;&#x2F;p&gt;
&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;d2l.ai&#x2F;chapter_introduction&#x2F;intro.html&quot;&gt;first chapter&lt;&#x2F;a&gt; that I aim to go over is the introduction to the book as well as Machine Learning in general. As Deep Learning (DL), is a form of Machine Learning (ML), an intro&#x2F;refresher on the topic of Machine Learning is a natural start.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-is-machine-learning&quot;&gt;What is Machine Learning?&lt;&#x2F;h2&gt;
&lt;p&gt;In most scenarios, programs are written in way where the programmer gives precise information about the problem and&#x2F;or how to solve it. For example in &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Imperative_programming&quot;&gt;imperative programming&lt;&#x2F;a&gt; the program is essentially a list of statements that command the computer what to do. Even in the many forms of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Declarative_programming&quot;&gt;declarative programming&lt;&#x2F;a&gt;, such as &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Functional_programming&quot;&gt;functional-&lt;&#x2F;a&gt; or &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Logic_programming&quot;&gt;logic programming&lt;&#x2F;a&gt;, providing the solution is done by explicit descriptions by the coder. The coder would describe the entirety of the problem with mathematical functions or with logical axioms respectively.&lt;&#x2F;p&gt;
&lt;p&gt;However there are scenarios where such paradigms are not adequate. Take for example a case where we where have set of photos of cats and dogs. Suppose we want need to write a program that, given a photo, can recognize the species and breed of the animal depicted. Generally it is difficult to even begin formulating such a problem in terms of the usual programming paradigms. Usually any handwritten solution we can come up with will be very brittle: it will often fail to recognize the right kind of pet from the photo, or it would only be correct for a very limited set of photos. &lt;&#x2F;p&gt;
&lt;p&gt;An alternative way to solve this problem is by letting the computer learn what kind of pet is in the photo. The computer can use a set of labelled examples where the kind of animal and breed is already known and given alongside the photo. In such cases we start with an initial, perhaps even random, model that can make this prediction. Such a model will likely be pretty bad at the start, otherwise the problem would already be solved. Then we use the labelled data to update this model. The hope is that the new model will be better at classifying animals. The intention is to keep adding data and improving the model until we decide that our model is good enough to solve our pet classification problem.&lt;&#x2F;p&gt;
&lt;p&gt;This learning from previous experiences by the machine to solve a problem is denoted as &lt;em&gt;machine learning&lt;&#x2F;em&gt;. &lt;&#x2F;p&gt;
&lt;figure&gt;
  &lt;img src=&quot;featured.png&quot;  &#x2F;&gt;
  &lt;figcaption&gt;
      &lt;h4&gt;The recognition of dogs and cats, along with their breeds, is actually an interesting research problem in machine learning. This is due to the subtle differences between breeds making the problem difficult for computers. These images are from the &lt;a href =&quot;http:&#x2F;&#x2F;www.robots.ox.ac.uk&#x2F;~vgg&#x2F;data&#x2F;pets&#x2F;&quot;&gt; The Oxford-IIIT Pet Dataset&lt;&#x2F;a&gt; that can be used to help evaluate techniques aiming to tackle this issue. The images of this dataset are licensed under &lt;a href =&quot;https:&#x2F;&#x2F;creativecommons.org&#x2F;licenses&#x2F;by&#x2F;4.0&#x2F;&quot;&gt; Creative Commons Attribution-ShareAlike 4.0 International License.&lt;&#x2F;a&gt; Copyright of the images is with their original owner.&lt;&#x2F;h4&gt;
  &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;There are a large number of forms of machine learning, of which deep learning is only a particular branch, but there are common elements among the various techniques.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;elements-of-machine-learning&quot;&gt;Elements of Machine Learning&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;data&quot;&gt;Data&lt;&#x2F;h3&gt;
&lt;p&gt;Data is the example information from which the machine learning aims to learn. In the previous scenario, the data is the labelled photos of pets.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;model&quot;&gt;Model&lt;&#x2F;h3&gt;
&lt;p&gt;The model designates the whole process of using the data and transforming it into the goal of machine learning. In our example the model would be the full process that can take a given photo and transform it into a prediction of the pet depicted.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;objective-function&quot;&gt;Objective Function&lt;&#x2F;h3&gt;
&lt;p&gt;The objective function denotes how good, or bad, our model currently is. As mentioned before, the reason we perform machine learning is that we aim to let the computer learn how to solve a problem. The objective function allow us to measure the quality of our current solution, so it can be assessed whether the machine learning is progressing towards an acceptable solution.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;optimization-algorithm&quot;&gt;Optimization Algorithm&lt;&#x2F;h3&gt;
&lt;p&gt;The optimization algorithm that describes the process of moving from one model to a better one. This allows for the learning to happen by moving from one model to a one that better solves the problem.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;kinds-of-machine-learning&quot;&gt;Kinds of Machine Learning&lt;&#x2F;h2&gt;
&lt;p&gt;Machine learning is a large field with a wide variety of techniques and potential problems to solve. Generally techniques are differentiated by the data they input and output, as well as the specific type of problem they target.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;supervised-learning&quot;&gt;Supervised Learning&lt;&#x2F;h3&gt;
&lt;p&gt;Supervised learning aims to predict targets given some input data. A good example of this is our previous scenario where we were trying to predict what type of pet is in a photo. If we have a number of photos for which we know what kind of animal is depicted in them (i.e. labeled data), we could perform such supervised learning.&lt;&#x2F;p&gt;
&lt;p&gt;Various kinds of supervised learning can be further subdivided based on what kind of target we aim to predict. Some common types of supervised learning are:&lt;&#x2F;p&gt;
&lt;h4 id=&quot;classification&quot;&gt;Classification&lt;&#x2F;h4&gt;
&lt;p&gt;The above example is actually called a &lt;em&gt;classification&lt;&#x2F;em&gt; scenario. In general, classification problems are those where given an example, we aim to find to what particular class that example belongs to. While in the example we described the classes as the species and the breeds of pets, many other types of classes can be used depending on the problem we aim to solve.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;tagging&quot;&gt;Tagging&lt;&#x2F;h4&gt;
&lt;p&gt;Consider a scenario where there could be more than one pet in the photo and we want to recognize each of them. Predicting in such cases is called &lt;em&gt;tagging&lt;&#x2F;em&gt;. The main difference between classification and tagging, is that with tagging multiple classes need to be recognized at once (e.g.: the photo can have both a dog and a cat ). This in contrast with classification, where the classes are exclusive (e.g.: dog or a cat).&lt;&#x2F;p&gt;
&lt;h4 id=&quot;regression&quot;&gt;Regression&lt;&#x2F;h4&gt;
&lt;p&gt;The case where we want to predict a (real valued) number, for example the weight of the pet depicted in the photo, is called a &lt;em&gt;regression&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;search-and-ranking&quot;&gt;Search and Ranking&lt;&#x2F;h4&gt;
&lt;p&gt;Search and ranking is a scenario where we want to figure out an order between various items. For example suppose that we want to figure out popularity rankings between various breeds of dogs and cats.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;unsupervised-learning&quot;&gt;Unsupervised Learning&lt;&#x2F;h3&gt;
&lt;p&gt;In cases with supervised learning, a set of examples for which the result is already known must be available. Often large quantities of such information is required to get accurate results. However such information can be scarce, or simply unavailable. &lt;&#x2F;p&gt;
&lt;p&gt;Even in such cases there is a wealth of information that can still be learned from the data through machine learning. Some example techniques include:&lt;&#x2F;p&gt;
&lt;h4 id=&quot;clustering&quot;&gt;Clustering&lt;&#x2F;h4&gt;
&lt;p&gt;With the absence of clearly labelled categories given to us, we can still want to group the data into categories. This process is called &lt;em&gt;clustering&lt;&#x2F;em&gt;. For example suppose we have a set of photos depicting all sort of pets, without any labels. Here we could still aim to group similar pets into clusters.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;data-generation-synthesis&quot;&gt;Data Generation&#x2F;Synthesis&lt;&#x2F;h4&gt;
&lt;p&gt;Given a set of data, we can also aim to synthesize data that is similar to the given data. For example if we have a large set of dog photos we can also aim to generate a photo of a, non-existent, dog.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reinforcement-learning&quot;&gt;Reinforcement Learning&lt;&#x2F;h3&gt;
&lt;p&gt;The final group of machine learning techniques that we go over in this article are those where the learning process is interacting with the environment. In reinforcement learning the main goal is to figure out what kind of action needs to be taken in a particular situation. As an example, take a scenario where you want to teach a robot dog to play football. &lt;&#x2F;p&gt;
&lt;p&gt;There process of reinforcement learning is as follows. The agent interacts with the environment through actions and observes the environment reacting to those actions. Based on the environment the agent may get rewards, either positive or negative. The rewards, together with the observations, guide the selection of subsequent actions. There is a cycle of actions, leading to observations and potentially rewards, and then again to actions. This cycle can then be repeated possibly indefinitely or until some goal is reached. &lt;&#x2F;p&gt;
&lt;p&gt;To make this description a bit more concrete, lets take our example scenario of letting a robot dog learn football. Here the agent is the decision making process of the robot dog. The environment itself is a playing field with other robots and a ball. The observations are the sensor data of this environment, such as the vision of the robot. Rewards could be given for taking control of the ball, making a successful pass and of course scoring. Negative rewards can be given for the robot missing a pass, walking off field and other undesirable actions.&lt;&#x2F;p&gt;
&lt;figure&gt;
  &lt;img src=&quot;aibosfootball.jpg&quot;  &#x2F;&gt;
  &lt;figcaption&gt;
      &lt;h4&gt;Teaching four legged robots, such as the &lt;a href =&quot;https:&#x2F;&#x2F;us.aibo.com&#x2F;&quot;&gt; AIBO robot dogs&lt;&#x2F;a&gt;, to play football is one of the leagues that is part of the &lt;a href=&quot;https:&#x2F;&#x2F;www.robocup.org&#x2F;&quot;&gt;RoboCup&lt;&#x2F;a&gt; competition aimed at promoting robotics and AI research. This photo depicts the &lt;a href= &quot;https:&#x2F;&#x2F;www.engineering.unsw.edu.au&#x2F;computer-science-engineering&#x2F;help-resources&#x2F;students&#x2F;student-projects&#x2F;robocup&quot; rUNSWift&lt;&#x2F;a&gt; team in a four-legged league game from RoboCup 2006 in Breman, Germany. &lt;a href =&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;RoboCup#&#x2F;media&#x2F;File:RUNSWift_AIBOS.jpg&quot;&gt;Public Domain Photo by Brad Hall&lt;&#x2F;a&gt;&lt;&#x2F;h4&gt;
  &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Reinforcement learning as a whole can be pretty complex with lots of variables based on can be known about the environment, state or actions. Subcategories of reinforcement are generally based on the given restrictions with these variables.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;towards-deep-learning&quot;&gt;Towards Deep Learning&lt;&#x2F;h2&gt;
&lt;p&gt;The above is a general introduction to machine learning, so the question arises how deep learning fits into the picture. Machine learning as a field has quite a bit of history with many of its techniques being laid out in the previous century. A number of such techniques, such as &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Neural_network&quot;&gt;Neural Networks&lt;&#x2F;a&gt; initially showed great promise but research and applications on them were languishing due to the lack data and large amount of processing power to enable such techniques. In recent years this has changed, alongside with some important advances, that led to deep learning techniques being one of the most effective approaches for various machine learning applications. &lt;&#x2F;p&gt;
&lt;p&gt;In addition the availability of frameworks for deep learning, such as &lt;a href=&quot;https:&#x2F;&#x2F;mxnet.apache.org&#x2F;&quot;&gt;MXNet&lt;&#x2F;a&gt;, which we will use in our learning process, means that the field is very inviting for new users and applications. &lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;This was a quick rundown of the first chapter. The book actually goes into much more detail, with some previews of techniques and extensive references, for the interested reader. If you have any remarks or suggestions for this series of articles please let me know! I am quite anxious for the next part when I hope to plunge deep into some coding with &lt;a href=&quot;https:&#x2F;&#x2F;mxnet.apache.org&#x2F;&quot;&gt;MXNet&lt;&#x2F;a&gt;. &lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Prolog and the Price of Peace</title>
        <published>2019-02-01T00:00:00+00:00</published>
        <updated>2019-02-04T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/prolog-price-of-peace/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/prolog-price-of-peace/</id>
        
        <content type="html">&lt;p&gt;The use of logic is a common element in Science Fiction. In the Star Trek universe &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Vulcan_(Star_Trek)&quot;&gt;Vulcans&lt;&#x2F;a&gt; are a species that is famously known for aiming to live by logic and reason. Because of this they are often considered masters of these subjects. Yet there was a case in an episode of the Star Trek show &lt;a href=&quot;http:&#x2F;&#x2F;memory-alpha.wikia.com&#x2F;wiki&#x2F;The_Maquis,_Part_II_(episode)&quot;&gt;Deep Space 9&lt;&#x2F;a&gt;, where &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Quark_(Star_Trek)&quot;&gt;Quark&lt;&#x2F;a&gt;, a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Ferengi&quot;&gt;Ferengi&lt;&#x2F;a&gt;, was able to convince Sakonna, a Vulcan, of the error of her logic and reasoning. &lt;&#x2F;p&gt;
&lt;p&gt;For this Quark made use of Ferengi philosophy, namely the Third Rule of Acquisition. The Rules of Acquisition are a series of proverbs and guidelines that govern Ferengi society, and notably their business dealings that take a prominent place in their lives. The Third Rule of Acquisition states that &amp;quot;Never spend more for an acquisition than you have to.&amp;quot; By applying this rule to the situation in the episode Quark was able make it clear to Sakonna that the best time broker a peace agreement would be &amp;quot;right now&amp;quot;, as the price of peace is at an all time low. &lt;&#x2F;p&gt;
&lt;figure&gt;
  &lt;img src=&quot;featured.jpg&quot;  &#x2F;&gt;
  &lt;figcaption&gt;
      &lt;h4&gt;Quark explaining the Third Rule of Acquisition to Sakonna Copyright CBS Corporation.&lt;&#x2F;h4&gt;
  &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;featured.jpg&quot; title=&quot;Quark explaining the Third Rule of Acquisition to Sakonna&quot; attr=&quot;Copyright CBS Corporation&quot; &gt;}} --&gt;
&lt;p&gt;Much like with Ferengi society, various rules and regulations play a prominent role in our lives and dealings with each other. So much so, that navigating the various rules to their logical conclusion, whether in the realm of law, finance and other domains, is often a difficult process. Thankfully there are tools and techniques to help us. The programming language Prolog, in particular, can be a very helpful in dealing with various rules and logical problems. This article aims to provide a brief introduction to this language using by using it to show how Quark&#x27;s reasoning can be implemented within a computer program.&lt;&#x2F;p&gt;
&lt;p&gt;Prolog is a logic programming language, originally created in the 1970s, but with many modern implementations such as &lt;a href=&quot;http:&#x2F;&#x2F;www.swi-prolog.org&#x2F;&quot;&gt;SWI-Prolog&lt;&#x2F;a&gt;. With logic programming programs are written and solved using some variation of a formal logic. Using such logic, information about the problem and its domain is first declared. Then the user can pose queries about the problem domain, which the programming language aims to answer through reasoning with the available information. This style of programming contrasts with the more common, imperative, paradigm. Instead of telling the computer how to solve the problem, we can declare information about the problem and let the computer, through the use of logic, solve it for us.&lt;&#x2F;p&gt;
&lt;p&gt;In order to show how Prolog works, we aim to use it to represent the same problem and reasoning that Quark used to convince Sakonna. To do this first lets examine the situation depicted the episode a bit more closely. In the episode the Maquis are a group that are at odds Cardassian colonists both living the Demilitarized Zone. Tensions were escalating as the Cardassian colonists were recently supplied in secret with weapons by the Cardassian Union. As a result Sakonna, along with other members of the Maquis, aimed at acquiring more weapons of their own. Even after it was found that the Cardassian Union was behind weapon supplies, Sakonna still aimed at gathering more, as a way to ensure peace. Quark pointed out the flaws in the logic of her argument, given the Third Rule of Acquisition: &amp;quot;Never spend more for an acquisition than you have to.&amp;quot; If the goal of Sakonna is truly to acquire peace, than it is the perfect time to be negotiating with the Cardassian colonists. With the flow of the smuggled in weapons having stopped, and both sides already having weapons, neither the Maquis nor the Cardassian colonists have any advantage. By aiming to acquire weapons still, they would only escalate the conflict, and making peace more costly in the long run.&lt;&#x2F;p&gt;
&lt;p&gt;To express this scenario with Prolog, we are going to introduce some elements of this language first (for a bit more through introduction see the excellent &lt;a href=&quot;http:&#x2F;&#x2F;www.learnprolognow.org&#x2F;&quot;&gt;Learn Prolog Now!&lt;&#x2F;a&gt; available online). There are three basic elements to a Prolog program: facts, rules and queries. &lt;&#x2F;p&gt;
&lt;p&gt;Facts are elements that have been stated to hold. For example the following statement:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(cardassians).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;can be used to denote that the advantage is held by the Cardassians colonists.&lt;&#x2F;p&gt;
&lt;p&gt;Rules are a way for Prolog to infer new information from the knowledge that already exists. For example if we aim to state that &amp;quot;if the Cardassian colonists have the advantage the price of peace is high&amp;quot; in Prolog we would have the rule: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(high) :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(cardassians).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are two parts to writing such rules in Prolog: the body and the head. The body of the rule is in this case &lt;code&gt;advantage(cardassians)&lt;&#x2F;code&gt; while the head is &lt;code&gt;priceOfPeace(high)&lt;&#x2F;code&gt;. In Prolog if the body of the rule holds true, than it can be concluded that the head is true as well. This exactly matches to what we aim to express: if the Prolog program knows that the Cardassian colonists hold the advantage, than it can conclude that the price of peace is high. &lt;&#x2F;p&gt;
&lt;p&gt;Facts and rules together form the knowledge base that can describe a domain. In this case the domain is the situation between the Cardassian colonists and the Maquis. The final piece, queries, allows us to examine this knowledge base and ask questions on what Prolog can infer from this knowledge. For the above example we could query whether the price of peace is indeed high, which should follow directly from the semantics of the rules and facts that we have described.&lt;&#x2F;p&gt;
&lt;p&gt;Given that we load in the knowledge base consisting of the above-mentioned fact and rule, the query: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;?-  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(high).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;that asks whether the price of peace is high, will return &lt;code&gt;true&lt;&#x2F;code&gt; indicating that this is indeed the case. Instead if we ask whether the price of peace is low, using the query:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;?-  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(low).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;the result will be &lt;code&gt;false&lt;&#x2F;code&gt; indicating this is not the case.&lt;&#x2F;p&gt;
&lt;p&gt;We can also ask more open ended questions using variables. Variables are written by starting with a capitalized letter (or an underscore), such as &lt;code&gt;X&lt;&#x2F;code&gt;. These variables could be used to write queries with unknowns. The query: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;?-  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;will give us the result: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;X = high
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which again is what would directly follow from the facts and the rules. What Prolog does with variables is to try to &amp;quot;unify&amp;quot; them with known values that match (or other variables). In this case from the knowledge base we have given it, it unifies it with the value of &lt;code&gt;high&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;From the basic elements of facts, rules and queries Prolog is able to represent and answer problems in many domains. This is also shows off the feature of a logical programming language, such as Prolog, that make it different compared to many other programming languages. Instead of telling the program what to do, the program becomes a description of the domain, over which queries are answered to solve the overall goal. &lt;&#x2F;p&gt;
&lt;p&gt;Beyond the above basics, Prolog has many features to support the description of more complicated domains. Here we only list a few that helps us to convey our translation of Quark&#x27;s reasoning into Prolog. &lt;&#x2F;p&gt;
&lt;p&gt;One such feature is the use variables within the rules themselves. The rule &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;acquirePeace&lt;&#x2F;span&gt;&lt;span&gt;(X) :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;indicates that the price of acquiring peace depends on the price of peace.&lt;&#x2F;p&gt;
&lt;p&gt;The rule &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(low)  :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(cardassians), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(maquis).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;shows off the logical operator &lt;strong&gt;and&lt;&#x2F;strong&gt;, meaning that the price of peace is low if both the Cardassians and the Maquis have the advantage. &lt;&#x2F;p&gt;
&lt;p&gt;The rule&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(low)  :- \+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(cardassians), \+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(maquis).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;shows off the logical &lt;strong&gt;not&lt;&#x2F;strong&gt;, denoted by the &#x27;+&#x27; symbol, which states that the price of peace is also low if neither the Cardassians nor the Maquis have the advantage.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, much like in other languages, comments for the code can also be written. This is done by prefacing a line with &amp;quot;%%&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Now if we put every element together to use Prolog to describe Quarks scenario, a possible knowledge base is as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;%% Rules
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;acquirePeace&lt;&#x2F;span&gt;&lt;span&gt;(X)  :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(low)  :- \+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(cardassians), \+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(maquis).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(low)  :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(cardassians), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(maquis).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(high) :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(cardassians).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(low) :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(maquis).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(cardassians)  :- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;weapons&lt;&#x2F;span&gt;&lt;span&gt;(cardassians), \+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;weapons&lt;&#x2F;span&gt;&lt;span&gt;(maquis).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(maquis)  :- \+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;weapons&lt;&#x2F;span&gt;&lt;span&gt;(cardassians), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;weapons&lt;&#x2F;span&gt;&lt;span&gt;(maquis).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;%% Facts
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;weapons&lt;&#x2F;span&gt;&lt;span&gt;(cardassians).
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;weapons&lt;&#x2F;span&gt;&lt;span&gt;(maquis).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the overall knowledge base, the logic of Quark&#x27;s reasoning is broken down into a number of rules and a pair of facts. &lt;&#x2F;p&gt;
&lt;p&gt;The rules state that advantage of either side, the Cardassians settlers or the Maquis, is decided by one side having weapons while the other side has not. The price of peace is in turn dependent on who has the advantage. In this formalisation the price of peace is only high if Cardassians have the advantage. Finally the price of acquiring peace is dependent solely on the price of peace.&lt;&#x2F;p&gt;
&lt;p&gt;The facts for this scenario is that both the Cardassian settlers and the Maquis have weapons. &lt;&#x2F;p&gt;
&lt;p&gt;With this knowledge base describing Quark&#x27;s reasoning process the query to see for what price we can aquire peace:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;?-  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;acquirePeace&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;will indeed return&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;X = low
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;indicating that acquiring peace can be done at a low price. &lt;&#x2F;p&gt;
&lt;p&gt;Going a bit beyond this query, if we would further want to test the logic of Quark&#x27;s scenario, we can also inquire about other information.&lt;&#x2F;p&gt;
&lt;p&gt;For example if we would ask for the price of peace:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;?-  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;priceOfPeace&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we would also unsurprisingly get &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;X = low
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;as with the rules defined in our knowledge base these values are intertwined. &lt;&#x2F;p&gt;
&lt;p&gt;We can also ask for who has the advantage:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;?-  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;advantage&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which will return: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which is completely in line with the scenario that noone has the advantage. &lt;&#x2F;p&gt;
&lt;p&gt;We can also ask who has weapons, the query of:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;?-  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;weapons&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;gives us the answer&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;X = cardassians
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;... but wait, this can not be right! Did we not state in the knowledge base that both the Cardassian colonists and the Maquis have weapons? Prolog can actually return mutliple answers, if they exist. In &lt;a href=&quot;http:&#x2F;&#x2F;www.swi-prolog.org&#x2F;&quot;&gt;SWI-Prolog&lt;&#x2F;a&gt; this can be done by pressing semicolon (&lt;code&gt;;&lt;&#x2F;code&gt;) after an answer, in which case another answers is returned, if it exists or simply false otherwise. By pressing &lt;code&gt;;&lt;&#x2F;code&gt; once after the first answer we will see a total output such as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;X = cardassians &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;X = maquis.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully Sakonna would also be quite convinced with the logic of Prolog, but just to be absolutely sure, lets examine some scenario&#x27;s where the facts of the scenario are changed. In such cases the rules should still be applied in ways that we would expect.&lt;&#x2F;p&gt;
&lt;p&gt;Suppose that our only fact now is that only the cardassians colonists have weapons, while rules remain the same. Giving the query for knowing the price of acquiring peace:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;?-  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;acquirePeace&lt;&#x2F;span&gt;&lt;span&gt;(X).
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;will now return:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;pro&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-pro &quot;&gt;&lt;code class=&quot;language-pro&quot; data-lang=&quot;pro&quot;&gt;&lt;span&gt;X = high 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully, much like Quark&#x27;s application of the Third Rule of Acquisition, this intro would have been enough to convince Sakonna of the logic of purchasing peace at the lowest possible price. Similarly, I hope that as a reader, this article gave some insight to the usefulness of Prolog in such scenarios. Next time, you are dealing with a problem domain, that would easily match to a similar scenario of rules, facts and queries, give Prolog a try. It is, after all, a very logical choice.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Aesopica, Part 4: Basic Conversion to Other Formats</title>
        <published>2018-12-18T00:00:00+00:00</published>
        <updated>2018-12-18T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/aesopica-4/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/aesopica-4/</id>
        
        <content type="html">&lt;p&gt;This article is the fourth part of a series, examining the use of the Clojure language for representing Linked Data, with examples from Aesop&#x27;s stories. The topic of this article is to describe how to do some basic conversions from our Clojure representation of Linked Data, to some of the other formats, such as &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;turtle&#x2F;&quot;&gt;Turtle&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;n-quads&#x2F;&quot;&gt;NQUADS&lt;&#x2F;a&gt; or &lt;a href=&quot;https:&#x2F;&#x2F;json-ld.org&#x2F;&quot;&gt;JSON-LD&lt;&#x2F;a&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;In previous articles of this series, we created a Clojure based syntax for defining Linked Data. In order to make this syntax a viable member of the Linked Data ecosystem, it is important to provide conversion functionality to other Linked Data formats. This allows for the user of the associated &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt; library, to create and use Linked Data in a Clojure based environment, and convert it, when needed, to other formats. In order to implement this functionality we made use of Clojure&#x27;s Java interop and the &lt;a href=&quot;https:&#x2F;&#x2F;jena.apache.org&#x2F;&quot;&gt;Apache Jena&lt;&#x2F;a&gt;, and made it available in the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt; library.&lt;&#x2F;p&gt;
&lt;p&gt;To start off we begin with the basic example of &amp;quot;The Fox and The Stork&amp;quot; story that we introduced &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-1&#x2F;&quot;&gt;initially&lt;&#x2F;a&gt;. The Clojure representation of this is as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fox-and-stork-edn
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;context
&lt;&#x2F;span&gt;&lt;span&gt;   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nil &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:rdf &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;facts
&lt;&#x2F;span&gt;&lt;span&gt;   #{[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :gives-invitation :invitation1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :has-invited :stork&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :has-food :soup&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :serves-using :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :gives-invitation :invitation2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :has-invited :fox&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :has-food :crumbled-food&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :serves-using :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :can-eat-food-served-using :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :can-not-eat-food-served-using :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :can-eat-food-served-using :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :can-not-eat-food-served-using :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]}})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The full details of this representation are explained in our previous work. Here we only briefly summarize its main elements. The above mentioned example defines a knowledge base containing a set of facts that describe the &amp;quot;The Fox and The Stork&amp;quot; story. Each fact is a triple of a subject, predicate and object. These elements are all represented by an &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Uniform_Resource_Identifier&quot;&gt;URI&lt;&#x2F;a&gt;, but for human readability and use as well as ease of use in Clojure, they are identified by (namespaced) keywords. The context map, containing the keywords for the namespaces used, allows us to transform the namespaced keywords into full URIs when required. Of course there are more elements possible in Linked Data&#x2F;RDF and in this representation as well, such as &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-1&#x2F;&quot;&gt;literal values&lt;&#x2F;a&gt;, but this summary should suffice for this article.&lt;&#x2F;p&gt;
&lt;p&gt;Now in order to explain how this conversion is done, assuming no familiarity with Clojure or a similar language, two new concepts are required. &lt;&#x2F;p&gt;
&lt;p&gt;First, it is important to note that in the above example we bind the &amp;quot;The Fox and The Stork&amp;quot;, Linked Data representation to the &lt;code&gt;fox-and-stork-edn&lt;&#x2F;code&gt; variable. Although this is not a necessity for creating the knowledge base, it allows us to reuse this definition from inside the code and in this article as well, without explicitly writing out the full representation each time. &lt;&#x2F;p&gt;
&lt;p&gt;The second concept that we make use is how Clojure functions are called to be executed. In Clojure invoking a function has the general form of &lt;code&gt;(function-name param1 param2 ...)&lt;&#x2F;code&gt;. For example lets assume that the function for translation from our Clojure representation to Turtle is represented by &lt;code&gt;conv&#x2F;convert-to-turtle&lt;&#x2F;code&gt;. Here &lt;code&gt;conv&lt;&#x2F;code&gt; is a shorthand for Aesopica&#x27;s conversion namespace. Given this, the call to translate to a Turtle string representation of the Linked Data can be invoked by:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;conv&#x2F;convert-to-turtle&lt;&#x2F;span&gt;&lt;span&gt; fox-and-stork-edn)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The resulting string representation shows the same Linked Data knowledge base in Turtle syntax:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@base &amp;lt;http:&#x2F;&#x2F;www&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;newresalhaider&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@prefix rdf: &amp;lt;http:&#x2F;&#x2F;www&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;w3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;fox&amp;gt; rdf:type &amp;lt;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;stork&amp;gt; rdf:type &amp;lt;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;gives-invitation&amp;gt; &amp;lt;invitation1&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;invitation1&amp;gt; &amp;lt;has-invited&amp;gt; &amp;lt;stork&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;invitation1&amp;gt; &amp;lt;has-food&amp;gt; &amp;lt;soup&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;invitation1&amp;gt; &amp;lt;serves-using&amp;gt; &amp;lt;shallow-plate&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;stork&amp;gt; &amp;lt;gives-invitation&amp;gt; &amp;lt;invitation2&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;invitation2&amp;gt; &amp;lt;has-invited&amp;gt; &amp;lt;fox&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;invitation2&amp;gt; &amp;lt;has-food&amp;gt; &amp;lt;crumbled-food&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;invitation2&amp;gt; &amp;lt;serves-using&amp;gt; &amp;lt;narrow-mouthed-jug&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;can-eat-food-served-using&amp;gt; &amp;lt;shallow-plate&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;can-not-eat-food-served-using&amp;gt; &amp;lt;narrow-mouthed-jug&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;stork&amp;gt; &amp;lt;can-eat-food-served-using&amp;gt; &amp;lt;narrow-mouthed-jug&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;stork&amp;gt; &amp;lt;can-not-eat-food-served-using&amp;gt; &amp;lt;shallow-plate&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This representation has a similar form to our Clojure based notation. A set of facts is represented and prefixes and&#x2F;or a base prefix is used, to enable easy reading and writing of the triples.&lt;&#x2F;p&gt;
&lt;p&gt;Now let us look at some other formats and conversions. &lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;trig&#x2F;&quot;&gt;TriG&lt;&#x2F;a&gt; is an extension of the Turtle format for enabling &amp;quot;named graphs&amp;quot;. Now this is a topic of &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-3&#x2F;&quot;&gt;a previous article&lt;&#x2F;a&gt; but here it is suffice to say that by associating a set of facts with a specific graph, we enable the easy adding of metadata to these facts. To show this conversion, we use an example that uses this notion:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fox-and-stork-named-graph-edn
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;context
&lt;&#x2F;span&gt;&lt;span&gt;   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nil &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:rdf &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:time &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;2006&#x2F;time#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;facts
&lt;&#x2F;span&gt;&lt;span&gt;   #{[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :gives-invitation :invitation1 :dinner1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :has-invited :stork :dinner1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :has-food :soup :dinner1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :serves-using :shallow-plate :dinner1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :gives-invitation :invitation2 :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :has-invited :fox :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :has-food :crumbled-food :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :serves-using :narrow-mouthed-jug :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :serves-using :narrow-mouthed-jug :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:dinner1 :time&#x2F;before :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]}})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As one can see, here we simply extend our triple based representation of facts to include either triples or quads. In a quad the last element is the graph name identifier of the graph the fact is a member of.&lt;&#x2F;p&gt;
&lt;p&gt;Translating this representation to TriG can be done by:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;conv&#x2F;convert-to-trig&lt;&#x2F;span&gt;&lt;span&gt; fox-and-stork-named-graph-edn)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which results in the following string representation that is TriG formatted: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;2006&#x2F;time#before&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;narrow-mouthed-jug&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; ;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-food&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;crumbled-food&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; ;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;narrow-mouthed-jug&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-food&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;soup&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; ;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;shallow-plate&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; ;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&amp;gt;  &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are a number of differences in representation from the above TriG output to he Clojure representation, but also from the previous Turtle output. Probably one of the most apparent is that in this output no prefixes are used: URIs are all written out fully. Both Turtle and TriG are flexible in whether they abbreviate URIs with prefixes or not. This is completely left up to the author, on in this case the specific way the conversion has been implemented. Another difference is how graphs are identified. Instead of using a quad like formatting for denoting the graph to which each fact belongs to they are grouped together. For example in the form of: &lt;code&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt; { ... }&lt;&#x2F;code&gt;, all the facts inside the curly braces belong to the &lt;code&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;code&gt; graph. Finally a somewhat similar construction is used to abbreviate a group of triples that all use the same object. Instead of writing each fact out fully, &amp;quot;predicate-lists&amp;quot; are used to match a single subject with a series subject and object pairs. This is quite a nice feature, and something similar is definitely on the list of future improvements to the Clojure notation, although care must be taken that such shorthands can make the definition a bit more complex. &lt;&#x2F;p&gt;
&lt;p&gt;Speaking of complexity, an interesting format created with the purpose of being very simple is &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;n-quads&#x2F;&quot;&gt;NQUADS&lt;&#x2F;a&gt;. This is a straightfoward, line based syntax where each fact is represented by a single line. It is actually an extension of the &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;n-triples&#x2F;&quot;&gt;N-Triples&lt;&#x2F;a&gt; format, with support added for handling named graphs. The conversion of our named graph example using the function invocation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;conv&#x2F;convert-to-nquads&lt;&#x2F;span&gt;&lt;span&gt; fox-and-stork-named-graph-edn)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;would give us the following example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;2006&#x2F;time#before&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;narrow-mouthed-jug&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-food&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;crumbled-food&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;narrow-mouthed-jug&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-food&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;soup&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;shallow-plate&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As one can see this format does not use prefixes: each fact is a triple or a quad on a single line ending with a dot, with each element URI written out fully. This way of writing facts is similar to the Clojure based notation, with main change that the Clojure notation does use prefixes for URI abbreviation. This simplicity contrasts with the flexibility of the Turtle format, which can be more terse, but more complex to parse and generate. This also shows that a separate &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;n-triples&#x2F;&quot;&gt;N-Triples&lt;&#x2F;a&gt; converter is not really needed. As long as the original knowledge bases does not use any named-graphs the result will be the same as with N-Triples.&lt;&#x2F;p&gt;
&lt;p&gt;The final format that we aim to convert to is &lt;a href=&quot;https:&#x2F;&#x2F;json-ld.org&#x2F;&quot;&gt;JSON-LD&lt;&#x2F;a&gt;. This is a format based on the JavaScript Object Notation &lt;a href=&quot;https:&#x2F;&#x2F;www.json.org&#x2F;&quot;&gt;JSON&lt;&#x2F;a&gt;, which allows for very easy interoperability with JSON based tools. &lt;&#x2F;p&gt;
&lt;p&gt;Converting can be done with the following invocation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;conv&#x2F;convert-to-json-ld&lt;&#x2F;span&gt;&lt;span&gt; fox-and-stork-named-graph-edn)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;resulting in the following JSON representation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@graph&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : [ {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@graph&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : [ {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    }, {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-food&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;soup&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    } ],
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;2006&#x2F;time#before&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }, {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@graph&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : [ {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    }, {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-food&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;crumbled-food&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    }, {
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    } ],
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  }, {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  }, {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;@type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  } ]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The biggest benefit of this format is the compatibility with JSON based tools and techniques. Regular JSON parsers, encoders and other tooling will just work, giving the format a very wide reach. Similarly to this our Clojure based approach uses &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;edn-format&#x2F;edn&quot;&gt;EDN&lt;&#x2F;a&gt; as its basis. This is a subset of Clojure, notably its notation of data values, and is used by &lt;a href=&quot;https:&#x2F;&#x2F;www.datomic.com&#x2F;&quot;&gt;Datomic&lt;&#x2F;a&gt; and others as a data transfer format.&lt;&#x2F;p&gt;
&lt;p&gt;To summarize, we have seen how converting Linked Data from the Clojure representation to various other formats using the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt; library is just a function invocation away. We have also looked at some of the differences between various syntaxes, notably the benefits that they provide: Turtle&#x2F;TriG offers a lot of flexibity and shorthands for reading and writing, N-Quads simplicity of notation, and JSON-LD compatibility with an existing and well used standard. The Clojure representation is aimed creating a new, and hopefully interesting blend. It makes use of prefixes for easy reading and writing by human users, similarly to what is possible in Turtle. It has the simplicity of fact representation as triples and quads, like in N-Quads. Finally it uses a common, albeit not nearly as widespread, standard  a basis so it can make use of EDN based tooling. &lt;&#x2F;p&gt;
&lt;p&gt;One interesting element, that the Turtle and Trig formats provide, is various short-hands for reading and writing. We believe this is a very useful feature, but of course the trade-offs of the shorthands versus the simplicity of notation must be taken into account. The format of which such shorthand will take shape, is therefor the topic for another article.&lt;&#x2F;p&gt;
&lt;p&gt;Note that previous articles in this series can be also be found on this site: &lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-1&#x2F;&quot;&gt;Part 1, General Introduction&lt;&#x2F;a&gt; covers the basic elements of Linked Data&#x2F;RDF along with their representation in Clojure. It also introduces &amp;quot;The Fox and the Stork&amp;quot; formalised using Linked Data.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-2&#x2F;&quot;&gt;Part 2, Literal Values&lt;&#x2F;a&gt; describes how literal values can be represented.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-3&#x2F;&quot;&gt;Part 3, Named Graphs&lt;&#x2F;a&gt; describes the notion of representing, and naming, graphs, which allows for representing information about facts and graphs themselves.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;As always, the functionality detailed in these articles can be found in the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt; library for using Clojure to write Linked Data.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Aesopica, Part 3: Named Graphs</title>
        <published>2018-12-04T00:00:00+00:00</published>
        <updated>2018-12-04T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/aesopica-3/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/aesopica-3/</id>
        
        <content type="html">&lt;p&gt;This article is the third part of a series, examining the use of the Clojure language for representing Linked Data, with examples from Aesop&#x27;s stories. In &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-1&#x2F;&quot;&gt;part one&lt;&#x2F;a&gt; the basic elements of &amp;quot;The Fox and the Stork&amp;quot; story were formalised as Linked Data in Clojure, while in &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-2&#x2F;&quot;&gt;part two&lt;&#x2F;a&gt; we investigated how various literal values can be described. In this article we examine how information about facts themselves, such as meta-information, can be described with Linked Data. As always, the functionality detailed in these articles can be found in the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt; library for using Clojure to write Linked Data.&lt;&#x2F;p&gt;
&lt;p&gt;In Linked Data, facts are represented as triples of subjects, predicates and objects. For example, when representing the story of the &amp;quot;The Fox and the Stork&amp;quot; one fact that we want to represent is &amp;quot;The Fox gives an invitation.&amp;quot; In this fact &amp;quot;The Fox&amp;quot; is the subject, the &amp;quot;gives an&amp;quot; is the predicate and &amp;quot;an invitation&amp;quot; is the object. Of course, as we mentioned in our previous articles, one of the strengths of Linked Data is that the elements are more precisely defined than just their natural language representations in a sentence. A Uniform Resource Identifier (&lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;wiki&#x2F;URI&quot;&gt;URI&lt;&#x2F;a&gt;) is used to more formally identify these elements. This would make the previous fact to be written as follows, using the Turtle notation of RDF:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;http:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;http:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;http:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When making using a base prefix for &lt;code&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&lt;&#x2F;code&gt; this could be shortened as: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;gives-invitation&amp;gt; &amp;lt;invitation1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using our Clojure based notation, that was introduced in the previous articles, we could write this same fact as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;context
&lt;&#x2F;span&gt;&lt;span&gt;   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nil &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;facts
&lt;&#x2F;span&gt;&lt;span&gt;   #{[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :gives-invitation :invitation1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     }}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above-mentioned fact is just one out of many needed to represent the full story of &amp;quot;The Fox and the Stork&amp;quot;. In most cases a multitude of facts is required to represent the required knowledge. A set of facts, each consisting of subjects, predicates and objects, form a knowledge graph which provides us with a very general, but precise, way to represent knowledge.&lt;&#x2F;p&gt;
&lt;p&gt;However there are scenarios when we want to represent knowledge about the facts themselves. One way Linked Data&#x2F;RDF facilitates this is the use of the &amp;quot;named graphs&amp;quot;. Named graphs allows us to associate an identifier (a [URI](Uniform Resource Identifier)) with a fact, or a set of facts. This essentially gives a name to a graph in the knowledge base, hence the notion of &amp;quot;named graphs&amp;quot;. Such an identifier can then be used as a way to add information about the facts with which it is associated.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;n-quads&#x2F;&quot;&gt;NQUADS&lt;&#x2F;a&gt; syntax for RDF illustrates one way such named graphs can be represented. In this representation all the elements of the fact: the subject, predicate, object and optionally a graph-name, are written out fully, separated by spaces and concluding with a dot (&lt;code&gt;.&lt;&#x2F;code&gt;) . &lt;&#x2F;p&gt;
&lt;p&gt;To take a single fact as an example, here follows a NQUADS format representation that details that &amp;quot;for the first invitation the Stork has been invited&amp;quot;, and this fact is part of the &amp;quot;first dinner&amp;quot; named graph: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For this fact there are four elements to be represented, hence we can refer to these elements together as a quad, versus the notion of a triple for facts just consisting of a subject, predicate and object. As mentioned previously, URIs are used to precisely identify each element:  &lt;code&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt;&lt;&#x2F;code&gt; is the subject, &lt;code&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&amp;gt;&lt;&#x2F;code&gt; is the predicate and &lt;code&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt;&lt;&#x2F;code&gt; is the object respectively. In addition the graph is identified by the URI &lt;code&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;The big benefit of using such identifiers as names for the graphs is that they themselves can be part of facts. For example if we want to express that the facts contained inside the &amp;quot;first dinner&amp;quot; graph occur before the facts of the &amp;quot;second dinner&amp;quot; graph, we can use the fact: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;2006&#x2F;time#before&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that this fact itself is not part of any named graph. In a knowledge base of facts this would make it a part of the &amp;quot;default graph&amp;quot;. A default graph is a graph without any particular name. This makes the mixing of &amp;quot;regular&amp;quot; facts, where each fact consists of a triple, and facts in explicit named graphs, where each fact is a quad, possible in a single knowledge base.&lt;&#x2F;p&gt;
&lt;p&gt;An example of a sightly expanded version using of &amp;quot;The Fox and the Stork&amp;quot;&amp;quot; story using named graphs in the NQUADS format can be therefor be as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;2006&#x2F;time#before&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;narrow-mouthed-jug&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-food&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;crumbled-food&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation2&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;narrow-mouthed-jug&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;gives-invitation&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-food&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;soup&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;serves-using&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;shallow-plate&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;invitation1&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;has-invited&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;stork&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;dinner1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we introduced the concept of &amp;quot;named graphs&amp;quot; we now want introduce a way to represent them in the Clojure representation of Linked Data. Similarly on how in NQUADS the triples are extended to quads to indicate the name of the graph, we extend our &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-1&#x2F;&quot;&gt;previously introduced Clojure syntax&lt;&#x2F;a&gt; to be able to use quads for facts, as opposed to just triples. Similarly to NQUADS the, optional, fourth element of each fact represents the named graph identifier. Any regular triple based fact is part of the default graph in the knowledge base, similarly to the NQUAD representation.&lt;&#x2F;p&gt;
&lt;p&gt;The resulting Clojure representation of above-mentioned Linked Data story can be written as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fox-and-stork-named-graph-edn
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;context
&lt;&#x2F;span&gt;&lt;span&gt;   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nil &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:rdf &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:time &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;2006&#x2F;time#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;facts
&lt;&#x2F;span&gt;&lt;span&gt;   #{[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :gives-invitation :invitation1 :dinner1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :has-invited :stork :dinner1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :has-food :soup :dinner1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :serves-using :shallow-plate :dinner1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :gives-invitation :invitation2 :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :has-invited :fox :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :has-food :crumbled-food :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :serves-using :narrow-mouthed-jug :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :serves-using :narrow-mouthed-jug :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:dinner1 :time&#x2F;before :dinner2&lt;&#x2F;span&gt;&lt;span&gt;]}})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The main difference between the Clojure representation and NQUADS is that the Clojure representation uses prefixes and NQUADS uses full URIs written out each time. This is a deliberate design choice in syntax from both perspectives. In NQUADS this allows the format to represent each fact on a single line, without the need for a lookup based on context for the full URI of elements. In the Clojure representation the prefixes allow for a much more compact fact representation that makes for easier reading and writing by human users. &lt;&#x2F;p&gt;
&lt;p&gt;There are a number of other formats for writing Linked Data, some of which support named graphs. &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;trig&#x2F;&quot;&gt;TriG&lt;&#x2F;a&gt; for example is an extension of the Turtle format used in previous articles in this series. &lt;a href=&quot;https:&#x2F;&#x2F;json-ld.org&#x2F;&quot;&gt;JSON-LD&lt;&#x2F;a&gt; is also a very commonly used format for Linked Data that also supports named graphs. With the introduction of the Clojure way of writing Linked Data in this series, it makes sense to enable translating Linked Data into these formats for compatibility and reaching a wider audience. The facts on how to achieve this will be detailed in another article.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Aesopica, Part 2: Literal Values</title>
        <published>2018-09-14T00:00:00+00:00</published>
        <updated>2018-09-14T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/aesopica-2/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/aesopica-2/</id>
        
        <content type="html">&lt;p&gt;This article is the second part of a series, examining the use of the Clojure language for representing Linked Data, with examples from Aesop&#x27;s stories. Part one can be found &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;aesopica-1&#x2F;&quot;&gt;on this site&lt;&#x2F;a&gt; where the basic elements of the Fox and the Stork story were formalised. In this article we examine how literal values can be represented, using Clojure, in Linked Data. The code to enable the functionality described in these articles can be found in the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt; library for using Clojure to write Linked Data.&lt;&#x2F;p&gt;
&lt;p&gt;As mentioned in the previous article the story of the Fox and the Stork is about the fox who invited the stork for a dinner. At the dinner soup was served from a shallow plate that the fox could eat but the stork could not. In return, the stork invited the fox to a dinner, where the food was served in a narrow mouthed jug. This time the fox could not reach the food, while the stork ate it happily. &lt;&#x2F;p&gt;
&lt;p&gt;Elements of this story can be represented as &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;data&quot;&gt;Linked Data&lt;&#x2F;a&gt;, and in particular the &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;RDF&#x2F;&quot;&gt;Resource Description Framework (RDF)&lt;&#x2F;a&gt; that allows for a precise retelling of the story that is understandable to both humans and machines alike. &lt;&#x2F;p&gt;
&lt;p&gt;For example, the part of the Linked Data we generated in the previous article is as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;gives-invitation&amp;gt; &amp;lt;invitation1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;invitation1&amp;gt; &amp;lt;has-invited&amp;gt; &amp;lt;stork&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;invitation1&amp;gt; &amp;lt;has-food&amp;gt; &amp;lt;soup&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which details the elements of the story that the fox invites the stork, where soup is served. Elements that might be implicitly obvious to a human reader, but not to a program, that the fox and the stork are animals, are also represented in this fragment. These elements are describes as a set of facts, where each fact is a triple in the form of a subject, predicate and object. Each part of these facts in this example are represented as a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Uniform_Resource_Identifier&quot;&gt;Uniform Resource Identifier (URI)&lt;&#x2F;a&gt;, which are shortened with prefixes (i.e. &lt;code&gt;&amp;quot;rdf&amp;quot;&lt;&#x2F;code&gt;) or the base URI (i.e. &lt;code&gt;&amp;lt;fox&amp;gt;&lt;&#x2F;code&gt; is a shorthand for &lt;code&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox&lt;&#x2F;code&gt; ).&lt;&#x2F;p&gt;
&lt;p&gt;Now suppose we want to expand on the elements of this story. For example, we want to give the fox and the stork a name, an age, describe their personalities, give a time for the dinners, etc. For many of these elements we want to simply use value as objects in the representations. For example the number &lt;code&gt;2&lt;&#x2F;code&gt; as a representative of the age of the fox. In such scenarios we do not use URIs in the facts but &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;rdf11-concepts&#x2F;#section-Graph-Literal&quot;&gt;Literals&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Literals can be used to denote numbers, strings, dates and other such elements. In the Linked Data representation below we describe various attributes of the fox, the stork and the dinner with such literals.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;foaf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;xmlns.com&#x2F;foaf&#x2F;0.1&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;xsd: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;2001&#x2F;XMLSchema#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;foaf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;vo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;foaf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age &lt;&#x2F;span&gt;&lt;span&gt;2.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;is-cunning&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; true.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;foaf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ooi&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;foaf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age &lt;&#x2F;span&gt;&lt;span&gt;13.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &amp;lt;is-cunning&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; true.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;dinner1&amp;gt; &amp;lt;has-date&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2006-06-30T20:00:00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;^^&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;xsd:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dateTime
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As one can see in this example, representing literals is very similar to other objects. For example &lt;code&gt;&amp;quot;vo&amp;quot;&lt;&#x2F;code&gt; in the triple &lt;code&gt;&amp;lt;fox&amp;gt; foaf:name &amp;quot;vo&amp;quot;&lt;&#x2F;code&gt; represents the name of the fox. Note that the base of &lt;code&gt;foaf&lt;&#x2F;code&gt; in &lt;code&gt;foaf:name&lt;&#x2F;code&gt; and &lt;code&gt;foaf:age&lt;&#x2F;code&gt; refers to the &#x27;Friend of a Friend&#x27; ontology, that allows us to use the common terminology of this ontology to describe facts about the fox and the stork.  Literals such as &lt;code&gt;2&lt;&#x2F;code&gt; or &lt;code&gt;true&lt;&#x2F;code&gt; describe the age, and whether the fox is cunning, respectively. These are called the lexical forms of the literals and while they also have explicit types (e.g. &lt;code&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;2001&#x2F;XMLSchema#string&lt;&#x2F;code&gt; or simply &lt;code&gt;xsd:string&lt;&#x2F;code&gt; when using prefixes ), these types of literals are so common that writing the types explicitly is not required. &lt;&#x2F;p&gt;
&lt;p&gt;The slightly more complicated case is the definition of the time of the dinner shown by &lt;code&gt;&amp;quot;2006-06-30T20:00:00&amp;quot;^^xsd:dateTime&lt;&#x2F;code&gt; that shows off custom types for literals, or when we would like to give the type explicitly. Here the addition of the &lt;code&gt;^^xsd:dateTime&lt;&#x2F;code&gt; is an URI (with a prefix) describing how lexical form, i.e. &amp;quot;2006-06-30T20:00:00&amp;quot; exactly maps to a particular value. This allows for easier interpretation of such literal values for machines.&lt;&#x2F;p&gt;
&lt;p&gt;As in the previous article, we aim to use the data representation and manipulation capabilities of Clojure to represent the above-mentioned fragment. Again, for the basic cases, such as strings, numbers, etc, we can be pretty straightforward and only use the lexical form, i.e. &lt;code&gt;&amp;quot;vo&amp;quot;&lt;&#x2F;code&gt;, &lt;code&gt;2&lt;&#x2F;code&gt; or &lt;code&gt;true&lt;&#x2F;code&gt; in or representations. For the cases where we also want to specify a custom datatype, we use a map such as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;value &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2006-06-30T20:00:00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;type :xsd&#x2F;dateTime&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;where the keys &lt;code&gt;::aes&#x2F;value&lt;&#x2F;code&gt; and &lt;code&gt;::aes&#x2F;type&lt;&#x2F;code&gt; are representing the lexical form and datatype respectively. Note that &lt;code&gt;aes&lt;&#x2F;code&gt; in these keywords, and other refers, to the core namespace of the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt; library implementing this data representation. To full Clojure version of this example can be found below:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;context
&lt;&#x2F;span&gt;&lt;span&gt;   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nil &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:rdf &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:foaf &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;xmlns.com&#x2F;foaf&#x2F;0.1&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:xsd &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;2001&#x2F;XMLSchema#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;facts
&lt;&#x2F;span&gt;&lt;span&gt;   #{[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :foaf&#x2F;name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;vo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :foaf&#x2F;age 2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :is-cunning true&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :has-weight 6.8&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :foaf&#x2F;name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ooi&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :foaf&#x2F;age 13&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :is-cunning true&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:dinner1 :has-date &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;value &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2006-06-30T20:00:00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;type :xsd&#x2F;dateTime&lt;&#x2F;span&gt;&lt;span&gt;}]}}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course given that this story is centuries old, it is unlikely that the dinner took place at &lt;code&gt;2006-06-30T20:00:00&lt;&#x2F;code&gt;. As always care must be taken when taking things literally.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Aesopica, Part 1: General Introduction</title>
        <published>2018-09-10T00:00:00+00:00</published>
        <updated>2018-09-10T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/aesopica-1/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/aesopica-1/</id>
        
        <content type="html">&lt;p&gt;The stories called Aesop&#x27;s Fables or the Aesopica, are an ancient collection of stories that have been passed down to modern day. These stories are of diverse origins they cover a wide variety of themes. Although originally intended for an adult audience, in later times were often used for the education of children.&lt;&#x2F;p&gt;
&lt;p&gt;One of such stories is the tale of the Fox and the Stork. There are many versions of this fable, but the overall outline is generally as follows:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;quot;The fox invited the stork to dinner. At the dinner soup was served from a shallow plate, that the fox could eat but the hungry stork could not even taste. In turn the stork invited the fox to a dinner. Dinner was served in a narrow mouthed jug filled with crumbled food. This time the fox could not reach the food, while the stork ate.&amp;quot;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=&amp;#x2F;img&amp;#x2F;series&amp;#x2F;aesopica&amp;#x2F;foxandstork.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;1884 fountain design depicting the story of the Fox and the Stork by Catalan sculptor Eduard Batiste Alentorn in Barcelona&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;aesopica&#x2F;aesopica-1&#x2F;foxandstork.jpeg&quot; title=&quot;A 1884 fountain design depicting the story of the Fox and the Stork by Catalan sculptor Eduard Batiste Alentorn in Barcelona&quot; attr=&quot;By Jordiferrer - Own work, CC BY-SA 3.0&quot; attrlink=&quot;https:&#x2F;&#x2F;commons.wikimedia.org&#x2F;w&#x2F;index.php?curid=25764300&quot; &gt;}} --&gt;
&lt;p&gt;The intention of stories such as these, as well as text in general, is to convey meaning. However, in addition to humans, a new audience for text has come to light in recent years: machines. To facilitate this new audience a set of technologies has been developed to convey the meaning of text in a precise and unambiguous way that is easily understandable for both humans and machines alike. Many of these new methods fall under the umbrella of the &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;&quot;&gt;Semantic Web&lt;&#x2F;a&gt;. The goal of the Semantic Web is to create a web of data where the meaning of the information is both human and machine understandable.&lt;&#x2F;p&gt;
&lt;p&gt;One of the cornerstone technologies in conveying information for this purpose is &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;data&quot;&gt;Linked Data&lt;&#x2F;a&gt;, and in particular the &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;RDF&#x2F;&quot;&gt;Resource Description Framework (RDF)&lt;&#x2F;a&gt; standard that defines how this Linked Data can be expressed. I have written a &lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;post&#x2F;interlinked-data&#x2F;&quot;&gt;short introduction to Linked Data&lt;&#x2F;a&gt; before but to summarize: it allows for the expressing information as a set of facts. These facts have the form of subject, predicate, object triples. A set of these facts is often called a knowledge base, or in an alternative view this can also been seen as a knowledge graph where the facts define the nodes and edges.&lt;&#x2F;p&gt;
&lt;p&gt;In a Linked Data representation the story of Fox and the Stork would look something like this: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;animal&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;gives-invitation&amp;gt; &amp;lt;invitation1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;invitation1&amp;gt; &amp;lt;has-invited&amp;gt; &amp;lt;stork&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;invitation1&amp;gt; &amp;lt;has-food&amp;gt; &amp;lt;soup&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;invitation1&amp;gt; &amp;lt;serves-using&amp;gt; &amp;lt;shallow-plate&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &amp;lt;gives-invitation&amp;gt; &amp;lt;invitation2&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;invitation2&amp;gt; &amp;lt;has-invited&amp;gt; &amp;lt;fox&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;invitation2&amp;gt; &amp;lt;has-food&amp;gt; &amp;lt;crumbled-food&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;invitation2&amp;gt; &amp;lt;serves-using&amp;gt; &amp;lt;narrow-mouthed-jug&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;can-eat-food-served-using&amp;gt; &amp;lt;shallow-plate&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;fox&amp;gt; &amp;lt;can-not-eat-food-served-using&amp;gt; &amp;lt;narrow-mouthed-jug&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &amp;lt;can-eat-food-served-using&amp;gt; &amp;lt;narrow-mouthed-jug&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;stork&amp;gt; &amp;lt;can-not-eat-food-served-using&amp;gt; &amp;lt;shallow-plate&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is in the &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;turtle&#x2F;&quot;&gt;Turtle syntax&lt;&#x2F;a&gt; of RDF. There are other types of syntax are available to represent Linked Data, for example in JSON form as &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;json-ld&#x2F;&quot;&gt;JSON-LD&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To summarize a bit of what this Linked Data format does in this scenario, is that it uses &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Uniform_Resource_Identifier&quot;&gt;Uniform Resource Identifiers (URIs)&lt;&#x2F;a&gt; to define the subjects, predicates and objects of each fact. This allows to precisely and unambiguously define and link the meaning between these elements. For example, the fact that the fox is a type of animal could be expressed by the triple with the full URIs: &lt;code&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;fox http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;animal .&lt;&#x2F;code&gt; Due to the fact that writing the full URIs can be quite cumbersome, the Turtle syntax uses two kinds of shorthands to help out. In this case one can define a base URI for the current document, &lt;a href=&quot;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&lt;&#x2F;a&gt;, as well as prefixes for other namespeaces, such as &lt;a href=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&lt;&#x2F;a&gt;, with which the writing of each fact that would begin with these URI fragments could be shortened.&lt;&#x2F;p&gt;
&lt;p&gt;When everything put together this format still describes the original story, albeit restructured into separate facts.&lt;&#x2F;p&gt;
&lt;p&gt;There exists many tools for handling Linked Data such as the above story. For example APIs, such as &lt;a href=&quot;https:&#x2F;&#x2F;jena.apache.org&#x2F;&quot;&gt;Jena&lt;&#x2F;a&gt;, can aid in the creation, storage and querying of data made available in such a fashion. Of course more and better tools and techniques are always welcome. In this article in particular we hope to describe how we can use the Clojure programming language to enable working with Linked Data. &lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;&quot;&gt;Clojure&lt;&#x2F;a&gt; is a language that offers a lot of benefits. The focus on manipulating pure data, with immutable data-structures and functional programming, provides an excellent way to organize code. The ability to inter-operate with the Java and JavaScript ecosystems, allows for the use of many mature libraries as well as many avenues for deployment. &lt;&#x2F;p&gt;
&lt;p&gt;To use the data manipulation capabilities of Clojure to enable the Semantic Web, seems like a natural combination. Some previous works also aimed at exploring this area, notably &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ontodev&#x2F;edn-ld&quot;&gt;EDN-LD&lt;&#x2F;a&gt; which gives a convention and a library for working with Linked Data.&lt;&#x2F;p&gt;
&lt;p&gt;In this article we will also explore how we can use Clojure to interact with Linked Data. In our case we will focus on the creation Linked Data from a Clojure environment and we might take different conventions compared to previous work, so we start with a fresh implementation. &lt;&#x2F;p&gt;
&lt;p&gt;In Clojure, information is directly represented as data, as opposed to it being encapsulated into various other abstractions such as objects. A large subset of elements data in Clojure is also a data format called &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;edn-format&#x2F;edn&quot;&gt;the Extensible Data Notation (EDN)&lt;&#x2F;a&gt;. The built-in elements in this notation are nil, booleans, strings, characters, symbols, keywords, integers, floating-point numbers, lists, vectors, maps and sets. The meaning behind most of these elements is relatively straightforward, so we only give a brief summary of them here and some examples. &lt;&#x2F;p&gt;
&lt;h5 id=&quot;nil&quot;&gt;Nil&lt;&#x2F;h5&gt;
&lt;p&gt;An empty or non-existent element is represented by &lt;code&gt;nil&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h5 id=&quot;booleans&quot;&gt;Booleans&lt;&#x2F;h5&gt;
&lt;p&gt;A boolean value can be &lt;code&gt;true&lt;&#x2F;code&gt; or &lt;code&gt;false&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;h5 id=&quot;strings&quot;&gt;Strings&lt;&#x2F;h5&gt;
&lt;p&gt;Strings are written in double quotes, for example: &lt;code&gt;&amp;quot;This sentence is a string.&amp;quot;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h5 id=&quot;characters&quot;&gt;Characters&lt;&#x2F;h5&gt;
&lt;p&gt;Characters representing single characters, and are preceded by a backslash, for example &lt;code&gt;\c&lt;&#x2F;code&gt; or &lt;code&gt;\newline&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;h5 id=&quot;symbols&quot;&gt;Symbols&lt;&#x2F;h5&gt;
&lt;p&gt;Symbols are representing identifiers, written by a set of characters (with a few additional rules). Examples of identifiers are for example &lt;code&gt;foo&lt;&#x2F;code&gt;, &lt;code&gt;clojure.core&lt;&#x2F;code&gt;, &lt;code&gt;clojure.string&#x2F;split&lt;&#x2F;code&gt;. As some of these examples show, in Clojure they are used, among other things to refer to modules and functions. Another interesting feature, as the &lt;code&gt;clojure.string&#x2F;split&lt;&#x2F;code&gt; example shows, is that they can be namespaced which helps to organize symbols and avoid name collisions.&lt;&#x2F;p&gt;
&lt;h5 id=&quot;keywords&quot;&gt;Keywords&lt;&#x2F;h5&gt;
&lt;p&gt;Keywords are very similar to symbols but they are identifiers that refer to themselves. They are constructed much like symbols, but with a leading &lt;code&gt;:&lt;&#x2F;code&gt;. Examples of keywords are &lt;code&gt;:fruit&lt;&#x2F;code&gt; or &lt;code&gt;:company.persons&#x2F;name&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;h5 id=&quot;integers-and-floats&quot;&gt;Integers and Floats&lt;&#x2F;h5&gt;
&lt;p&gt;Integers and floats (floating point numbers) are used, as expected, to write numbers &lt;code&gt;3&lt;&#x2F;code&gt; or &lt;code&gt;4.5&lt;&#x2F;code&gt; for example. &lt;&#x2F;p&gt;
&lt;p&gt;All these elements described above can be put in collections. &lt;&#x2F;p&gt;
&lt;h5 id=&quot;lists&quot;&gt;Lists&lt;&#x2F;h5&gt;
&lt;p&gt;Lists are a sequence of values enclosed in &lt;code&gt;()&lt;&#x2F;code&gt;, for example &lt;code&gt;(2 &amp;quot;A string.&amp;quot; false)&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;h5 id=&quot;vectors&quot;&gt;Vectors&lt;&#x2F;h5&gt;
&lt;p&gt;Vectors are a sequence of values enclosed in &lt;code&gt;[]&lt;&#x2F;code&gt;, for example &lt;code&gt;[true nil :company&#x2F;name]&lt;&#x2F;code&gt;. which are designed for random access of its elements. &lt;&#x2F;p&gt;
&lt;h5 id=&quot;sets&quot;&gt;Sets&lt;&#x2F;h5&gt;
&lt;p&gt;Sets are collections of unique values enclosed in &lt;code&gt;#{}&lt;&#x2F;code&gt;, such as &lt;code&gt;#{:fruit 2}&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;h5 id=&quot;maps&quot;&gt;Maps&lt;&#x2F;h5&gt;
&lt;p&gt;Finally maps are key value pairs, enclosed in curly braces &lt;code&gt;{}&lt;&#x2F;code&gt;, for example &lt;code&gt;{:name &amp;quot;John Smith&amp;quot;, :age 4}&lt;&#x2F;code&gt;, where each key is unique. Of course collections can also nested any type of collection.&lt;&#x2F;p&gt;
&lt;p&gt;Using this notation elements of EDN, we can build an EDN based version of the story of the Fox and the Stork, using some conventions. &lt;&#x2F;p&gt;
&lt;p&gt;Given that in many practical cases we are probably going to shorten URIs with prefixes when writing, we can use a keyword for denoting elements. In the case where we would use the base prefix, we can just use a regular, non-namespaced, keyword, i.e. &lt;code&gt;:fox&lt;&#x2F;code&gt;, and in cases where we would refer to any other prefix we can use namespaced keywords, i.e. &lt;code&gt;:rdf&#x2F;type&lt;&#x2F;code&gt;. A full fact could then be described with a relatively straightforward vector, for example &lt;code&gt;[:fox :rdf&#x2F;type :animal]&lt;&#x2F;code&gt; and the knowledge base with a set of facts such as &lt;code&gt;#{[:fox :rdf&#x2F;type :animal] [:stork :rdf&#x2F;type :animal]}&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;Of course this means that in addition to facts we also need some data for the context, in which we store the base and other prefixes and to what they map to, to be able to fully build an equivalent Linked Data representation. The context will be a map of the relevant prefixes as keys, as well as nil for the base prefix. For the above example this means that the below example will describe the context needed to resolve all the full URIs: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nil &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:rdf &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Putting everything together, to have a full Linked Data graph we need a context and a set of facts, so the overall structure will be a map where these are both defined:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;context
&lt;&#x2F;span&gt;&lt;span&gt;   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;nil &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;aesop&#x2F;foxstork&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:rdf &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::aes&#x2F;facts
&lt;&#x2F;span&gt;&lt;span&gt;   #{[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :rdf&#x2F;type :animal&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :gives-invitation :invitation1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :has-invited :stork&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :has-food :soup&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation1 :serves-using :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :gives-invitation :invitation2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :has-invited :stork&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :has-food :crumbled-food&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:invitation2 :serves-using :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :can-eat-food-served-using :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:fox :can-not-eat-food-served-suing :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :can-eat-food-served-using :narrow-mouthed-jug&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;     [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:stork :can-not-eat-food-served-suing :shallow-plate&lt;&#x2F;span&gt;&lt;span&gt;]}}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I have started a small library for manipulating Linked Data structures written this way, with the name &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;aesopica&quot;&gt;Aesopica&lt;&#x2F;a&gt;. It is in very early stages, where the current main functionality is to translate Linked Data written this way into the Turtle format described above.&lt;&#x2F;p&gt;
&lt;p&gt;Of course there are lot of other elements of Linked Data that needs to be represented in this that we did not tackle yet. In addition there are also a large number of Clojure libraries that could be used to make writing and using Linked Data in this fashion easier. How these features could be achieved however is a story for another time.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Excavating a Common Treasure</title>
        <published>2018-06-30T00:00:00+00:00</published>
        <updated>2018-06-30T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/common-treasure/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/common-treasure/</id>
        
        <content type="html">&lt;p&gt;History is full of legends of ancient treasures and powerful artifacts, lost to time or hidden by purpose, that would bring glory, power and riches to their discoverer.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=winged-bull.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;A winged bull depicted on an on archaeological artifact from the Assyrian empire between 1400 and 1200 BC. Cylinder Seal with Winged Bull - Walters Art Museum Licensed under CC0.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;common-treasure&#x2F;winged-bull.jpg&quot; title=&quot;A winged bull depicted on an on archaeological artifact from the Assyrian empire between 1400 and 1200 BC. Cylinder Seal with Winged Bull - Walters Art Museum Licensed under CC0.&quot; &gt;}} --&gt;
&lt;p&gt;Although the history of software development is much shorter than the history of mankind, there already exists artifacts that have near mythological status when it comes to software development, yet often go hidden underneath the surface. One of these is the language of Common Lisp.&lt;&#x2F;p&gt;
&lt;p&gt;Common Lisp is a language originally from the early 80s, and was a standardization of many dialects of the Lisp programming language, which explains both the Common and Lisp part of the name. Lisp itself is one of the oldest general purpose programming languages, first specified in 1958. From Lisp many ideas that we consider common in other languages were popularized, such as conditionals (the &lt;code&gt;if and else&lt;&#x2F;code&gt; structure), notion of functions as first class elements, and many others (see this &lt;a href=&quot;http:&#x2F;&#x2F;www.paulgraham.com&#x2F;diff.html&quot;&gt;article&lt;&#x2F;a&gt; on more of these ideas). Common Lisp is also often &lt;a href=&quot;http:&#x2F;&#x2F;www.gigamonkeys.com&#x2F;book&#x2F;introduction-why-lisp.html&quot;&gt;mentioned&lt;&#x2F;a&gt; as an amazing and fun language to learn, which makes it the clear-cut choice for this article.&lt;&#x2F;p&gt;
&lt;p&gt;As the story goes it gives the user untold levels of productivity and joy of development. One other big advantage is that Common Lisp used to be the language of A.I. research. There is lots of existing code out there for systems on the subject of knowledge representation, reasoning and planning. Given the resurgence of fields such as a &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;&quot;&gt;Semantic Web&lt;&#x2F;a&gt; that make use of such A.I. systems, learning from such tools and techniques, or even using them directly, can be invaluable. In this post, I aim to find out a very small portion of the riches we can dig up using Common Lisp, and to share this experience with the reader. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=seal.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Cylinder Seals were common objects that were used to designate ownership in ancient Mesopotamia. Although describing to a different notion of &amp;#x27;common&amp;#x27; these show remarkable of craftsmanship that we hope to introduce ourselves to in Common Lisp. Cylinder Seal with Two Heroes and a Tree - Walters Art Museum Licensed under CC0.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;common-treasure&#x2F;seal.jpg&quot; title=&quot;Cylinder Seals were common objects that were used to designate ownership in ancient Mesopotamia. Although describing to a different notion of &#x27;common&#x27; these show remarkable of craftsmanship that we hope to introduce ourselves to in Common Lisp. Cylinder Seal with Two Heroes and a Tree - Walters Art Museum Licensed under CC0.&quot; &gt;}} --&gt;
&lt;p&gt;To add as a disclamer, I do have experience with a similar language, &lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;&quot;&gt;Clojure&lt;&#x2F;a&gt; that I use in my day-to-day programming, as well as with &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;emacs&#x2F;&quot;&gt;Emacs&lt;&#x2F;a&gt; (namely &lt;a href=&quot;http:&#x2F;&#x2F;spacemacs.org&#x2F;&quot;&gt;Spacemacs&lt;&#x2F;a&gt;), so I probably have some head start in using and editing the language. Nonethess I aim to make this introduction accessible to readers without such knowledge. &lt;&#x2F;p&gt;
&lt;p&gt;With any treasure hunt, preparation is often key. As we are unfamiliar with the language itself it is good to read guides and tutorials to prepare. The guide to selecting the right equipment for our adventure is the &lt;a href=&quot;https:&#x2F;&#x2F;lispcookbook.github.io&#x2F;cl-cookbook&#x2F;getting-started.html&quot;&gt;cl-cookbook&lt;&#x2F;a&gt;, which describes how to get started with Common Lisp.&lt;&#x2F;p&gt;
&lt;p&gt;Common Lisp is a language with multiple implementations. As recommended by our guide we are using &lt;a href=&quot;http:&#x2F;&#x2F;www.sbcl.org&#x2F;&quot;&gt;Steel Bank Common Lisp (SBCL)&lt;&#x2F;a&gt; as distribution and using &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;roswell&#x2F;roswell&#x2F;wiki&quot;&gt;Roswell&lt;&#x2F;a&gt; to manage it. Alternatively a more straightforward install is &lt;a href=&quot;https:&#x2F;&#x2F;portacle.github.io&#x2F;&quot;&gt;Portacle&lt;&#x2F;a&gt; that could be used.&lt;&#x2F;p&gt;
&lt;p&gt;Next is testing whether our implementation is working. We run the Read–Eval–Print Loop (REPL), which in the case of Roswell can be done with the command &lt;code&gt;ros run&lt;&#x2F;code&gt;. What the REPL allows us to do is interactively develop our code by reading our input, evaluating it and printing the results. This is one of the many features in programming that originated with Lisp, but has since made its way to many other languages. &lt;&#x2F;p&gt;
&lt;p&gt;Now the question is how we should write Common Lisp. One really nice resource for quickly familiarizing with new languages is Learn X in Y minutes, which also has a nice &lt;a href=&quot;https:&#x2F;&#x2F;learnxinyminutes.com&#x2F;docs&#x2F;common-lisp&#x2F;&quot;&gt;introduction to Common Lisp&lt;&#x2F;a&gt;. One of the most striking features of a Lisp, is the syntax of it. Elements are either atoms, that evaluate to themselves, such as the number &lt;code&gt;4&lt;&#x2F;code&gt; or s-expressions which are a list of expressions in brackets, such as &lt;code&gt;(+ 3 2)&lt;&#x2F;code&gt;. Atoms evaluate to themselves, so if we write &lt;code&gt;4&lt;&#x2F;code&gt; in the REPL, we get &lt;code&gt;4&lt;&#x2F;code&gt; returned back. For s-expressions they are in the form of &lt;code&gt;(function param1 param2 ...)&lt;&#x2F;code&gt;, so in the case of &lt;code&gt;(+ 3 2)&lt;&#x2F;code&gt; this will evaluate to &lt;code&gt;5&lt;&#x2F;code&gt;. One can of course nest these forms, for example &lt;code&gt;(+ 3 (+ 3 2))&lt;&#x2F;code&gt; will result in &lt;code&gt;8&lt;&#x2F;code&gt;. In addition to functions there are macros available creating completely different forms to be translated into code. We will see some examples of this later in the article.&lt;&#x2F;p&gt;
&lt;p&gt;With the REPL now working it is time to set up a project where we can write down all the code we need for our digging. According to our cookbook tutorial we can do this from the REPL, by getting &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;fukamachi&#x2F;cl-project&quot;&gt;cl-project&lt;&#x2F;a&gt;, and using it. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(ql:quickload &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cl-project&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;(cl-project:make-project &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;#P&amp;quot;.&#x2F;common-treasure&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will give us the outline of project in the &lt;code&gt;common-treasure&lt;&#x2F;code&gt; directory. The files generated include a readme, systems for the project itself as well as tests and of course some skeletons for a test and a package itself respectively.&lt;&#x2F;p&gt;
&lt;p&gt;First we are going to start off figuring out the package itself. Like in many other languages, functionality can be grouped together units, in this case packages. The generated file outline looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;defpackage&lt;&#x2F;span&gt;&lt;span&gt; common-treasure
&lt;&#x2F;span&gt;&lt;span&gt;  (:use :cl))
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;in-package&lt;&#x2F;span&gt;&lt;span&gt; :common-treasure)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; blah blah blah.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;From the looks of it, it says to us that it defines a package named &lt;code&gt;common-treasure&lt;&#x2F;code&gt; using &lt;code&gt;:cl&lt;&#x2F;code&gt;, which I assume is another package. There is also a call to &lt;code&gt;in-package&lt;&#x2F;code&gt; which looks like is there to ensure that everything else that follows is also in this package. Finally the &lt;code&gt;;; blah blah blah.&lt;&#x2F;code&gt; portion looks like a comment, something to describe functionality with, but nothing to be executed.&lt;&#x2F;p&gt;
&lt;p&gt;Now in order to see if everything is working correctly let&#x27;s load this package. Instead of using the REPL from the command line, we are using it directly from our editor, in this case an &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;emacs&#x2F;&quot;&gt;Emacs&lt;&#x2F;a&gt; distribution named &lt;a href=&quot;http:&#x2F;&#x2F;spacemacs.org&#x2F;&quot;&gt;Spacemacs&lt;&#x2F;a&gt;. Without going into much detail about Emacs it can be described as a highly configurable editor for pretty much any programming language out there. &lt;a href=&quot;https:&#x2F;&#x2F;common-lisp.net&#x2F;project&#x2F;slime&#x2F;&quot;&gt;SLIME: The Superior Lisp Interaction Mode for Emacs&lt;&#x2F;a&gt; ensures that our interaction with the REPL and the various language features go much more smoothly by integrating it with our editor of choice. &lt;&#x2F;p&gt;
&lt;p&gt;Emacs is incredibly configurable, which is where Spacemacs comes in. It provides a set of curated configurations named layers, that make setting up the configuration for a particular language a breeze. We just enable the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;syl20bnr&#x2F;spacemacs&#x2F;tree&#x2F;master&#x2F;layers&#x2F;%2Blang&#x2F;common-lisp#install&quot;&gt;common-lisp layer&lt;&#x2F;a&gt; of Spacemacs in the configuration file, add a small piece to let Emacs know of our Common Lisp distribution (see link), and we are good to go.&lt;&#x2F;p&gt;
&lt;!-- One really nice feature of Spacemacs is that commands can be executed and discovered by pressing the spacebar as the leader followed by some other combinations (this is where the distribution gets its name). For example, the process of starting SLIME can be done by navigating to a common lisp source file, such as that above, and pressing the keys `space m &#x27;` in succession. Note that this requires the use of [vi](https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Vi)-mode which is a whole another topic for discussion, but a more emacs like mode with a different leading key also exist. To keep things clear, regardless whichever mode or even emacs distribution one uses, I will try to name the command, and not the key combination whenever possible --&gt;
&lt;p&gt;With everything setup now we can start our treasure hunt a bit more in earnest by starting SLIME. We are greeted by the message &amp;quot;Connected. Hack and be merry!&amp;quot; as well as a REPL in a window. Lets start to dig around by figuring out what exactly all the elements do. We can use the command &lt;code&gt;slime-describe-symbol&lt;&#x2F;code&gt; to get some more information about &lt;code&gt;defpackage&lt;&#x2F;code&gt; and what it does. This brings up some documentation on &lt;code&gt;defpackage&lt;&#x2F;code&gt;. It shows us that this is a macro, as well as the various options one can use with it. A macro, without going into too much detail, is essentially a way to convert forms into different ones before evaluating them. We can get a bit more info on the workings by calling the command &lt;code&gt;slime-hyperspec-lookup&lt;&#x2F;code&gt;. This takes us to the &lt;a href=&quot;http:&#x2F;&#x2F;www.lispworks.com&#x2F;documentation&#x2F;HyperSpec&#x2F;Front&#x2F;&quot;&gt;Common Lisp Hyperspec&lt;&#x2F;a&gt; with more detailed docs. The pages for &lt;a href=&quot;http:&#x2F;&#x2F;www.lispworks.com&#x2F;documentation&#x2F;HyperSpec&#x2F;Body&#x2F;m_defpkg.htm&quot;&gt;&lt;code&gt;defpackage&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and &lt;a href=&quot;http:&#x2F;&#x2F;www.lispworks.com&#x2F;documentation&#x2F;HyperSpec&#x2F;Body&#x2F;m_in_pkg.htm&quot;&gt;&lt;code&gt;inpackage&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; more or less confirm their use is as we expected.&lt;&#x2F;p&gt;
&lt;p&gt;Now it is time to start writing some new code. We remove the comments and create a function named &lt;code&gt;hello-treasure&lt;&#x2F;code&gt;, that should just print the text &amp;quot;Hello Treasure&amp;quot;. We add the following to our file:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;defun &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;hello-treasure &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello Treasure!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;in place of the comments, and we run the command &lt;code&gt;slime-complile-and-load-file&lt;&#x2F;code&gt;. Compilation succeeds, but when we aim to call the function  &lt;code&gt;(common-treasure:hello-treasure)&lt;&#x2F;code&gt; get an error when trying to call the function, saying &amp;quot;The symbol &amp;quot;HELLO TREASURE&amp;quot; is not external to the &amp;quot;COMMON TREASURE&amp;quot; package.&amp;quot;. This due to the fact that we did not declare the function external in the &lt;code&gt;defpackage&lt;&#x2F;code&gt; package declaration. &lt;&#x2F;p&gt;
&lt;p&gt;What is really interesting is that, although we have this issue, instead of just failing the SLIME REPL presents us with a number of options. For example we can  &lt;code&gt;0: [Continue] Use symbol anyway.&lt;&#x2F;code&gt;, &lt;code&gt;1: [Retry] Retry SLIME REPL evaluation request.&lt;&#x2F;code&gt;, &lt;code&gt;2: [*Abort] Return to SLIME&#x27;s top level.&lt;&#x2F;code&gt;. In this case we can just abort, and modify the package declaration, but it is interesting to be offered all these options.&lt;&#x2F;p&gt;
&lt;p&gt;By adding &lt;code&gt;(:export :hello-treasure)&lt;&#x2F;code&gt; to the &lt;code&gt;defpackage&lt;&#x2F;code&gt; macro, we can do another call and it indeed returns &amp;quot;Hello Treasure!&amp;quot; in the REPL.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we got the basics down we would like to have a slightly bigger example in which we make use of an existing library. While digging around for the software languages and libraries of the past is often the matter of some searching, reading, coding and a bit of trial and error. Digging around in the world is process governed by various regulations and laws. This is especially true in cities with long history, where a regular building site can easily unearth archaeological finds.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=excavation-city.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Digging in the certain urban areas can easily lead to excavations done before any subsequent work is done. Romano-Celtic temple excavated in London.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;common-treasure&#x2F;excavation-city.png&quot; title=&quot;Digging in the certain urban areas can easily lead to excavations done before any subsequent work is done. Romano-Celtic temple excavated in London.&quot; &gt;}} --&gt;
&lt;p&gt;In particular, there are often a set of regulations that specify whether one can dig in areas of archaeological interests. As an example we use a portion of the rules by the municipality of Utrecht, as described on a &lt;a href=&quot;https:&#x2F;&#x2F;www.utrecht.nl&#x2F;fileadmin&#x2F;uploads&#x2F;documenten&#x2F;9.digitaalloket&#x2F;REO&#x2F;archeologische-waardenkaart-2009.pdf&quot;&gt;map&lt;&#x2F;a&gt; of Utrecht and surrounding areas (in Dutch).&lt;&#x2F;p&gt;
&lt;p&gt;These regulations can be summarized by a number of rules, based on two conditions: the type of area and the size of the area that is to be disturbed. &lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If the area is of high archeological value, then a permit is required no matter what the size of the area is to be disturbed.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;If the area is of high archeological expectation, then a permit is required when the size of the area that is to be disturbed is larger than 100m&lt;sup&gt;2&lt;&#x2F;sup&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;If the area is of archeological expectation, then a permit is required when the size of the area that is to be disturbed is larger than 1000m&lt;sup&gt;2&lt;&#x2F;sup&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Such regulations can be encoded into a regular function that returns a true or false to the question &amp;quot;Is the a permit required?&amp;quot; based on two parameters, the size of the area and the type of the area. However this could be more complicated and harder to maintain if the set of regulations, along with conditions and outcomes, become more complex.&lt;&#x2F;p&gt;
&lt;p&gt;There are various approaches to deal with this issue. One of these is to encode the facts on which our answer relies into a knowledge base, and have a general mechanism to derive new knowledge from existing facts. This later process is often denoted as inferring or reasoning to derive new knowledge. Such an approach can make it easier to use and maintain such a system.&lt;&#x2F;p&gt;
&lt;p&gt;For this article we are going to implement such an approach using &lt;a href=&quot;http:&#x2F;&#x2F;lisa.sourceforge.net&#x2F;&quot;&gt;LISA&lt;&#x2F;a&gt;. This is a production rule system, in which the knowledge base is encoded in a set of facts, and rules are uses as a way to derive new knowledge. It is easy to see how such a system would be a good fit for our problem: our domain restrictions are already formulated as a set of rules. It would also allow us to use some existing libraries, to see more of what Common Lisp has to offer.&lt;&#x2F;p&gt;
&lt;p&gt;To start off, first we need to add a dependency to LISA into our system. One of the files created by cl-project is an &lt;a href=&quot;https:&#x2F;&#x2F;common-lisp.net&#x2F;project&#x2F;asdf&#x2F;?&quot;&gt;asdf&lt;&#x2F;a&gt; file that describes how to build the software we are creating. Here we can add the dependency on Lisa, resulting in the following file:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  This file is a part of common-treasure project.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;|#
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(defsystem &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;common-treasure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  :version &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.1.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  :author &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  :license &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  :depends-on (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;lisa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; Here we add the dependency on LISA.
&lt;&#x2F;span&gt;&lt;span&gt;  :components ((:module &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;                :components
&lt;&#x2F;span&gt;&lt;span&gt;                ((:file &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;common-treasure&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))))
&lt;&#x2F;span&gt;&lt;span&gt;  :description &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  :long-description
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;#.&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;-file-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;string
&lt;&#x2F;span&gt;&lt;span&gt;     (subpathname *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;load-pathname&lt;&#x2F;span&gt;&lt;span&gt;* &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;README.markdown&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;span&gt;  :in-order-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;to &lt;&#x2F;span&gt;&lt;span&gt;((test-op (test-op &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;common-treasure-test&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we just need to make sure the dependencies needed are actually downloaded and available. For this we use &lt;a href=&quot;https:&#x2F;&#x2F;www.quicklisp.org&#x2F;beta&#x2F;&quot;&gt;quicklisp&lt;&#x2F;a&gt; which is the library manager Common Lisp. By using the command &lt;code&gt;(ql:quickload &amp;quot;common-treasure&amp;quot;)&lt;&#x2F;code&gt; we can get all the dependencies for our system, which in this case consists of just LISA.&lt;&#x2F;p&gt;
&lt;p&gt;Now in order to translate our example scenario, we look at some of the examples that are available for LISA. A typical system consists of a knowledge base, in which facts are defined as &lt;a href=&quot;http:&#x2F;&#x2F;www.aiai.ed.ac.uk&#x2F;~jeff&#x2F;clos-guide.html&quot;&gt;Common Lisp Object System (CLOS)&lt;&#x2F;a&gt; objects and rules to manipulate such instances as the reasoning method. &lt;&#x2F;p&gt;
&lt;p&gt;CLOS is a way in which Common Lisp can do object-oriented programming, and LISA co-opts this as a way to represent facts in the knowledge base. Without going into detail on object-oriented programming theory, this allows one to, define classes, create instances of those classes and define methods that make use of those classes (see this &lt;a href=&quot;http:&#x2F;&#x2F;www.aiai.ed.ac.uk&#x2F;~jeff&#x2F;clos-guide.html&quot;&gt;nice intro on Common Lisp Object System (CLOS)&lt;&#x2F;a&gt; for its general use). &lt;&#x2F;p&gt;
&lt;p&gt;In the context of LISA, these allow us to represent classes of facts, specific instances of facts, and methods that can be used with those facts. For example we can represent the type of facts in our domain with two classes of objects: one that represents &amp;quot;area&amp;quot; and one that whether &amp;quot;a permit is required&amp;quot;. In Common Lisp these can be defined with the &lt;code&gt;defclass&lt;&#x2F;code&gt; macro. So for our example the following would be a call for permission requirement:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;defclass&lt;&#x2F;span&gt;&lt;span&gt; permit-required ()
&lt;&#x2F;span&gt;&lt;span&gt;  ()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case of the area, we need to represent two facts about it: what the archaeological type of it is, and what size of the area is disturbed. Such elements of the classes are described in CLOS using slots. There are many possible options that are available to describe a slot:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;:initarg&lt;&#x2F;code&gt; describes the name of the slot.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;:initform&lt;&#x2F;code&gt; is the default value it is given initially.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;:accessor&lt;&#x2F;code&gt; can be used to define a function for accessing the slot value.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Putting this together gives us the following class for representing the area:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;defclass&lt;&#x2F;span&gt;&lt;span&gt; area ()
&lt;&#x2F;span&gt;&lt;span&gt;  ((archeological-type :initarg :archeological-type :initform &amp;quot;&amp;quot; :accessor :archeological-type)
&lt;&#x2F;span&gt;&lt;span&gt;   (size-disturbed :initarg :size-disturbed :initform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0.0&lt;&#x2F;span&gt;&lt;span&gt; :accessor :size-disturbed)
&lt;&#x2F;span&gt;&lt;span&gt;    ))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we figured out how represent facts, we need to encode the rules of our domain into our system. We can do this with 3 rules that describe what to do in the cases of &lt;code&gt;high archeological value&lt;&#x2F;code&gt;, &lt;code&gt;high archeological expectation&lt;&#x2F;code&gt; and &lt;code&gt;archeological expectation&lt;&#x2F;code&gt; &lt;&#x2F;p&gt;
&lt;p&gt;The rules themselves described with three parts:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The antecedent, which is the set of conditions that have to be true. &lt;&#x2F;li&gt;
&lt;li&gt;The arrow sign &lt;code&gt;=&amp;gt;&lt;&#x2F;code&gt; .&lt;&#x2F;li&gt;
&lt;li&gt;The consequent, which contains the facts that need to be derived or any actions that we want to perform. &lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In the case of LISA, and in many other rule based definitions, the antecedent and the consequent are also called Left Hand Side (LHS) and Right Hand Side (RHS), as they are respectively on the left and right hand side of the arrow. What exactly can be put into the LHS and RHS is dependent on the language for the rule definitions, but one standard way they are used is in the LHS the conditions are described based on the facts that could be in the knowledge base, and on the RHS facts are added in the knowledge based on the meaning of the rule. For example for one of our rules, if the know the fact that our area is of high archaeological value (LHS), we can derive the fact that we need a permit (RHS). &lt;&#x2F;p&gt;
&lt;p&gt;Describing our three rules this way in LISA looks as follows, using the &lt;code&gt;defrule&lt;&#x2F;code&gt; macro:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; If the area is of high archeological value, then a permit is required no matter what the size of the area is to be disturbed.
&lt;&#x2F;span&gt;&lt;span&gt;(defrule high-value () (area (archeological-type &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;high-value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)) =&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;make-instance&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;permit-required))))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; If the area is of high archeological expectation, that a permit is required when the size of the area that is to be disturbed is larger than 100m&amp;lt;sup&amp;gt;2&amp;lt;&#x2F;sup&amp;gt;.
&lt;&#x2F;span&gt;&lt;span&gt;(defrule high-expectation () (area (archeological-type &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;high-expectation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) (&amp;gt; size-disturbed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;))  =&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;make-instance&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;permit-required))))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; If the area if of archeological expectation, that a permit is required when the size of the area that is to be disturbed is larger than 1000m&amp;lt;sup&amp;gt;2&amp;lt;&#x2F;sup&amp;gt;.
&lt;&#x2F;span&gt;&lt;span&gt;(defrule expectation () (area (archeological-type &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;expectation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) (&amp;gt; size-disturbed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;))  =&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;make-instance&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;permit-required))))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The rules are reasonably straightforward, in that they require the condition matching in the knowledge base, before they assert an instance of the &lt;code&gt;permit-required&lt;&#x2F;code&gt; fact into the knowledge base. &lt;&#x2F;p&gt;
&lt;p&gt;These together already describe the domain, but for this article we also show how things work with two examples. In one scenario we assert that we have an area with &lt;code&gt;high-value&lt;&#x2F;code&gt; archaeological type and a to be disturbed are with the size of 2000m&lt;sup&gt;2&lt;&#x2F;sup&gt;. In this case we would need a permit, due to our &lt;code&gt;high-value&lt;&#x2F;code&gt; rule. In the second scenario, the area is of &lt;code&gt;high-expectation&lt;&#x2F;code&gt; but the size of the area disturbed is only 10m&lt;sup&gt;2&lt;&#x2F;sup&gt;. In this case none our rules will fire, and no &lt;code&gt;permit-required&lt;&#x2F;code&gt; fact instance is put into the knowledge base.&lt;&#x2F;p&gt;
&lt;p&gt;Executing the system is just a matter of adding these facts in the knowledge base and letting the system run. In LISA adding a fact can be done using &lt;code&gt;assert&lt;&#x2F;code&gt; similarly to what the RHS of the rules are doing. To actually run the system, unsurprisingly, the function &lt;code&gt;run&lt;&#x2F;code&gt; can be called. To see what facts are in the knowledge base the function &lt;code&gt;facts&lt;&#x2F;code&gt; prints them out for us to see. To tie things together, we make sure that the knowledge base is reset before we execute anything using &lt;code&gt;reset&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;Below is the full example implementation that we put together:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;defpackage&lt;&#x2F;span&gt;&lt;span&gt; common-treasure
&lt;&#x2F;span&gt;&lt;span&gt;  )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;in-package&lt;&#x2F;span&gt;&lt;span&gt; :lisa-user)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(reset)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;defclass&lt;&#x2F;span&gt;&lt;span&gt; area ()
&lt;&#x2F;span&gt;&lt;span&gt;  ((archeological-type :initarg :archeological-type :initform &amp;quot;&amp;quot; :accessor :archeological-type)
&lt;&#x2F;span&gt;&lt;span&gt;   (size-disturbed :initarg :size-disturbed :initform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0.0&lt;&#x2F;span&gt;&lt;span&gt; :accessor :size-disturbed)
&lt;&#x2F;span&gt;&lt;span&gt;    ))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;defclass&lt;&#x2F;span&gt;&lt;span&gt; permit-required ()
&lt;&#x2F;span&gt;&lt;span&gt;  ()
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; If the area is of high archeological value, then a permit is required no matter what the size of the area is to be disturbed.
&lt;&#x2F;span&gt;&lt;span&gt;(defrule high-value () (area (archeological-type &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;high-value&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)) =&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;make-instance&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;permit-required))))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; If the area is of high archeological expectation, that a permit is required when the size of the area that is to be disturbed is larger than 100m&amp;lt;sup&amp;gt;2&amp;lt;&#x2F;sup&amp;gt;.
&lt;&#x2F;span&gt;&lt;span&gt;(defrule high-expectation () (area (archeological-type &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;high-expectation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) (&amp;gt; size-disturbed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;))  =&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;make-instance&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;permit-required))))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; If the area if of archeological expectation, that a permit is required when the size of the area that is to be disturbed is larger than 1000m&amp;lt;sup&amp;gt;2&amp;lt;&#x2F;sup&amp;gt;.
&lt;&#x2F;span&gt;&lt;span&gt;(defrule expectation () (area (archeological-type &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;expectation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) (&amp;gt; size-disturbed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;))  =&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;make-instance&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;permit-required))))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;;Test fact 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;; (assert ((make-instance &amp;#39;area :archeological-type &amp;quot;high-value&amp;quot; :size-disturbed 2000)))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;;;Test fact 2
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;make-instance&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;area :archeological-type &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;high-expectation&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; :size-disturbed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(facts)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(run)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(facts)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that here the &lt;code&gt;fact 1&lt;&#x2F;code&gt; is commented out, to test the assert of &lt;code&gt;fact 2&lt;&#x2F;code&gt;, so that running the other scenario is just a matter of commenting and un-commenting the relevant lines. &lt;&#x2F;p&gt;
&lt;p&gt;For the first scenario, with fact 1, the output of our initial program is as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;[package common-treasure]#&amp;lt;INITIAL-FACT &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 0 {1002669853}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;#&amp;lt;AREA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 1 {1002745373}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;For a total of 2 facts.
&lt;&#x2F;span&gt;&lt;span&gt;#&amp;lt;INITIAL-FACT &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 0 {1002669853}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;#&amp;lt;AREA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 1 {1002745373}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;#&amp;lt;PERMIT-REQUIRED &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 2 {100283A363}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;For a total of 3 facts.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that we print out our facts twice, once before the run and once after, but it shows that now the permit requirement was derived.&lt;&#x2F;p&gt;
&lt;p&gt;For the case of using &lt;code&gt;fact 2&lt;&#x2F;code&gt; , we get the following output:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;[package common-treasure].#&amp;lt;INITIAL-FACT &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 0 {1004D40AB3}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;#&amp;lt;AREA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 1 {10020107C3}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;For a total of 2 facts.
&lt;&#x2F;span&gt;&lt;span&gt;#&amp;lt;INITIAL-FACT &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 0 {1004D40AB3}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;#&amp;lt;AREA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; id 1 {10020107C3}&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;For a total of 2 facts.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This shows that our fact base did not change due to our running the reasoning system.&lt;&#x2F;p&gt;
&lt;p&gt;So there we have it, a bit of a peek of what can be done with Common Lisp. I have to say the language and its various features are quite a bit daunting, as I feel I have only scratched the surface of what is possible, with both interacting with the language as well as with A.I. tools such as LISA. That said digging into the language and seeing how it can handle a small, but also relevant scenario, felt like a worthwhile journey, and that is an experience always worth treasuring.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Spec-stacular Spider-Man</title>
        <published>2018-04-29T00:00:00+00:00</published>
        <updated>2018-05-01T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/spec-stacular-spider-man/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/spec-stacular-spider-man/</id>
        
        <content type="html">&lt;p&gt;Spider-Man is one of the most iconic heroes of the Marvel universe. Created by Stan Lee and Steve Ditko, Spider-Man is a regular teenager named Peter Parker, who due to being bitten by a radio-active spider, gains abilities such as the proportional strength of a spider, wall crawling and a spider sense to detect upcoming danger. One of the biggest draws of Spider-Man that although he is a superhero and fought various villains from cosmic beings to petty criminals, he also had to deal with regular everyday problems, such as money issues, school life and the pressure of a job.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=spectacular-spider-man.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The Spectacular Spider-Man © Marvel Entertainment&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;spec-stacular-spider-man&#x2F;spectacular-spider-man.jpg&quot; title=&quot;The Spectacular Spider-Man © Marvel Entertainment&quot; &gt;}} --&gt;
&lt;p&gt;In software there are also everyday problems which one has to tackle before one can defeat the villains of the domain at hand. One of these everyday problems is the issue of data validation. Data validation is the process of ensuring that the elements of the data are correct. This process has to be done in pretty much all domains when working with actual data. Consider the financial domain where a financial product can only be made available if the right requirements are fulfilled in the request. If the request is not written correctly then the request needs to be denied. In the legal and regulatory domains certain information that is required for a law must be fulfilled, otherwise costly corrections or fines can follow. Another good example is the clinical domain, where the a patients data needs to be transferred to an application. Here is it essential that this data fulfills the requirement for requesting a clinical procedure or a medication, as any mistake can lead to huge negative impact on the health of the patient.&lt;&#x2F;p&gt;
&lt;p&gt;One relatively recent tool that can be used to solve this problem is the &lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;guides&#x2F;spec&quot;&gt;clojure.spec&lt;&#x2F;a&gt; library in the Clojure programming language. In this article we aim to explain, alongside Spider-Man, how these specs can be used to tackle the data validation problem in a spectacular way. As this library relies on the Clojure language some knowledge of Clojure is needed. In order to make this article understandable to those without such prior expertise we introduce some aspects of Clojure. In particular we focus on two features of it: the way information (data) is represented and the fact that it is a Lisp.&lt;&#x2F;p&gt;
&lt;p&gt;In Clojure data is represented with relatively few elements that are combined together. Take for example a scenario where we want to create a profile of Spider-Man, as taken from the Marvel wiki entry on &lt;a href=&quot;http:&#x2F;&#x2F;marvel.com&#x2F;universe&#x2F;Spider-Man_(Peter_Parker)&quot;&gt;Spider-Man&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The full name of Spider-Man can be represented in text form as a string. Like in many other languages the text is placed in between quotation marks.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Peter Benjamin Parker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For the numbers to represent his relative power in the Marvel universe, we use natural numbers (we leave the concepts and issues surrounding very large or floating point numbers out in this article). In case of Spider-Man his durability is 5:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course having just a value of the name and the durability of Spider-Man just floating around makes the representation somewhat incomplete, as they are not attached to the concepts of &amp;quot;name&amp;quot; or &amp;quot;durability&amp;quot;. Just like how Spider-Man needs buildings to sling off of, we need a representation that links the values with what they represent. In Clojure, keywords are often used for this purpose. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:real-name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:spider-man-spec.core&#x2F;name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::durability
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Keywords are symbolic identifiers. Think of them as symbols, much like one would use a string, but with some special powers attached. They are text prefaced by &amp;quot;:&amp;quot;, as it can be seen in the keyword &lt;code&gt;:real-name&lt;&#x2F;code&gt;. They have the ability to be namespace-qualified, such as &lt;code&gt;:spider-man-spec.core&#x2F;name&lt;&#x2F;code&gt; which indicates that this is the keyword in the &lt;code&gt;spider-man-spec.core&lt;&#x2F;code&gt; namespace. Namespaces are what allows us to modularize our data and code, by grouping them under together a single identifier. In our case this is &lt;code&gt;spider-man-spec.core&lt;&#x2F;code&gt;. This namespacing ensures that our definition of the concepts of &amp;quot;name&amp;quot;, &amp;quot;real-name&amp;quot;, &amp;quot;durability&amp;quot;, etc. can remain distinct from any other use of similar concepts. Finally, when writing internally to the library which uses the namespace, or when aliasing to it, we can just shorten the keyword with &amp;quot;::&amp;quot;, such as in &lt;code&gt;::durability&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;Keywords come with some nice implementation details, such as fast equality checks and some other powers we will show in the future. This makes them the preferred keys in data-structures such as maps. And speaking of maps they allow the description of information in key-value pairs, as written between curly braces in the small example below. &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Spider-Man&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::real-name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Peter Benjamin Parker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The curly backets around the pairs express the keys and values in a map in Clojure. In the above example &lt;code&gt;::name&lt;&#x2F;code&gt; and &lt;code&gt;::real-name&lt;&#x2F;code&gt; are the key and value pairs for &lt;code&gt;&amp;quot;Spider-Man&amp;quot;&lt;&#x2F;code&gt; and &lt;code&gt;&amp;quot;Peter Benjamin Parker&amp;quot;&lt;&#x2F;code&gt; respectively. &lt;&#x2F;p&gt;
&lt;p&gt;Maps are just one of the ways one can describe a collection of elements. You also have sets, collections in which each element is unique. This can be done with a hashtag and some curly brackets &amp;quot;#{}&amp;quot;. In the example below we list the current and former affiliations of Spider-Man.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::current-affiliations &lt;&#x2F;span&gt;&lt;span&gt;#{&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Avengers&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::former-affiliations &lt;&#x2F;span&gt;&lt;span&gt;#{&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Secret Defenders&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;New Fantastic Four&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Outlaws&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that how sets are used within maps to represent this knowledge. This is actually a common way to represent knowledge in Clojure: you combine all the various data representations directly. This way you can have a list containing maps, with keywords as keys and values that contain maps and strings, where the maps contain numbers, etc. You have these data-structures in pretty much all commonly used programming languages. Where Clojure differs from many is that it does not put (almost any) sugaring or abstraction on top. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=spider-man-swinging.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Spider-Man swinging around the city. © Marvel Studios&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;spec-stacular-spider-man&#x2F;spider-man-swinging.jpg&quot; title=&quot;Spider-Man swinging around the city. © Marvel Studios&quot; &gt;}} --&gt;
&lt;p&gt;Just as Spider-Man is often at his best when he is just being &amp;quot;plain old Spidey&amp;quot;, having data represented this way has some nice advantages. The biggest is simplicity. Instead of learning to work with specific wrappers, objects, prototypes, etc on top of this data, that can differ between applications and libraries, it is enough to learn how to handle and manipulate maps, list, sets once. This knowledge can be then reused in any domain, and frees up the attention of the programmer to focus on the domain problem, and not the exact way the data was wrapped up in a library. &lt;&#x2F;p&gt;
&lt;p&gt;This of course also means that a system, such as clojure.spec, that aims at data validation in Clojure, has to handle the above-mentioned style of composition well. But before we get ahead of ourselves lets finish up by providing the profile of Spider Man.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;spider-man-profile
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Spider-Man&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::real-name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Peter Benjamin Parker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::identity ::secret
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::affiliations
&lt;&#x2F;span&gt;&lt;span&gt;                   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::current-affiliations &lt;&#x2F;span&gt;&lt;span&gt;#{&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Avengers&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::former-affiliations &lt;&#x2F;span&gt;&lt;span&gt;#{&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Secret Defenders&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;New Fantastic Four&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Outlaws&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::power-grid &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::durability 3
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::energy 4
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::fighting 5
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::intelligence 4
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::speed 5
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::strength 4
&lt;&#x2F;span&gt;&lt;span&gt;                 }
&lt;&#x2F;span&gt;&lt;span&gt;   }
&lt;&#x2F;span&gt;&lt;span&gt;  )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;vulture-profile
&lt;&#x2F;span&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Vulture&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::real-name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Adrian Toomes&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::identity ::publicly-known
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::affiliations
&lt;&#x2F;span&gt;&lt;span&gt;                   {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::current-affiliations &lt;&#x2F;span&gt;&lt;span&gt;{}
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::former-affiliations &lt;&#x2F;span&gt;&lt;span&gt;#{&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sinister Twelve&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sinister Six&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}}
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::power-grid &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::durability 4
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::energy 3
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::fighting 4
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::intelligence 4
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::speed 5
&lt;&#x2F;span&gt;&lt;span&gt;                 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::strength 3
&lt;&#x2F;span&gt;&lt;span&gt;                 }
&lt;&#x2F;span&gt;&lt;span&gt;   }
&lt;&#x2F;span&gt;&lt;span&gt;  )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;spider-man-characters &lt;&#x2F;span&gt;&lt;span&gt;[spider-man-profile vulture-profile])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh no, our Spider Senses should be tingling. It is Vulture, that has shown up in our list of Spider-Man characters. In addition we just introduced some new elements in our example that need some explanation for readers new to Clojure.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=vulture.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Uh oh, Vulture must be up to no good if he shows up here. © Marvel Studios&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;spec-stacular-spider-man&#x2F;vulture.jpg&quot; title=&quot;Uh oh, Vulture must be up to no good if he shows up here. © Marvel Studios&quot; &gt;}} --&gt;
&lt;p&gt;The first is the use of square brackets [], which indicate a list. This is a collection of elements, in this case of &lt;code&gt;spider-man-profile&lt;&#x2F;code&gt; and &lt;code&gt;vulture-profile&lt;&#x2F;code&gt;, that unlike a set, can have multiples of the same element. &lt;&#x2F;p&gt;
&lt;p&gt;The other new type of element we use is the form of using parentheses along side def as in &lt;code&gt;(def spider-man-characters ...)&lt;&#x2F;code&gt;. Expressions of these type, called symbolic expressions, or s-expressions for short, are a characteristic of the Lisp family of languages to which Clojure belongs to. In a Lisp, parts of the program are either atoms, such as &lt;code&gt;5&lt;&#x2F;code&gt;, &lt;code&gt;&amp;quot;Peter Benjamin Parker&amp;quot;&lt;&#x2F;code&gt;, &lt;code&gt;true&lt;&#x2F;code&gt;, or an s-expression where the first element between parens is a function and the rest are parameters. For example &lt;code&gt;(+ 1 3)&lt;&#x2F;code&gt;. While atoms evaluate to themselves, the s-expressions evaluate to a function with the given parameters. In the case of &lt;code&gt;(+ 1 3)&lt;&#x2F;code&gt; they should evaluate to &lt;code&gt;4&lt;&#x2F;code&gt;. You can also nest s-expressions, such as &lt;code&gt;(- (+ 1 3) 2)&lt;&#x2F;code&gt;, which will evaluate to &lt;code&gt;2&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;You might be thinking, &amp;quot;Wait, if everything is either an atom or an s-expression, what kind of villainous things are those strange brackets that one has to use to create a set, list or map!&amp;quot;. For all the simplicity in Clojure, it does make use of some syntactical sugar. Lists can be written &lt;code&gt;[spider-man-profile vulture-profile]&lt;&#x2F;code&gt; as a shorthand for the s-expression &lt;code&gt;(list spider-man-profile vulture-profile)&lt;&#x2F;code&gt;. Similar functions exist for maps and sets as well. &lt;&#x2F;p&gt;
&lt;p&gt;Much like Spider-Man, who for all his powers still has to struggle with juggling a school and a job and has to make practical decisions, Clojure has to make them as well. In this case because certain things, such as maps, sets and list are used so often, it uses a shorter syntax for creating them. This does makes the language slightly more complex, but in the author&#x27;s view, it pays off. &lt;&#x2F;p&gt;
&lt;p&gt;Another matter of practicality of course is that while we can nest the two profiles directly into a list, we can create variables for them to associate. The &lt;code&gt;def&lt;&#x2F;code&gt; function does exactly this, and it also ensures they become part of the current namespace. For example, if the current namespace is &lt;code&gt;spider-man-spec.core&lt;&#x2F;code&gt; then a &lt;code&gt;def&lt;&#x2F;code&gt; of &lt;code&gt;vulture-profile&lt;&#x2F;code&gt; can be referred to as &lt;code&gt;spider-man-spec.core&#x2F;vulture-profile&lt;&#x2F;code&gt; from other namespaces, and simply &lt;code&gt;vulture-profile&lt;&#x2F;code&gt; in the current namespace. This allows us to break up the overall data in smaller parts to use.&lt;&#x2F;p&gt;
&lt;p&gt;Now we finally described the profiles of both Spider-Man and Vulture, but are they correct? The library of clojure.spec uses the notion of a spec for this. A spec is simply a function on a single parameter that returns a truthy value (in most cases a &lt;code&gt;true&lt;&#x2F;code&gt; if the spec holds, &lt;code&gt;false&lt;&#x2F;code&gt; if the spec does not hold).&lt;&#x2F;p&gt;
&lt;p&gt;In essence this allows for many existing functions to be used as specs. For example the already existing function &lt;code&gt;string?&lt;&#x2F;code&gt; checks whether a particular value is a string or not. &lt;&#x2F;p&gt;
&lt;p&gt;In order to check whether a value is valid for a particular spec we can use the &lt;code&gt;s&#x2F;valid?&lt;&#x2F;code&gt; function. Here the &lt;code&gt;s&lt;&#x2F;code&gt; stands the namespace of the spec library &lt;code&gt;clojure.spec.alpha&lt;&#x2F;code&gt;, so by calling &lt;code&gt;s&#x2F;valid?&lt;&#x2F;code&gt; we are calling the &lt;code&gt;valid?&lt;&#x2F;code&gt; function of this particular namespace.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(s&#x2F;valid? string? &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Spider-Man&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above function call will checking if &lt;code&gt;&amp;quot;Spider-Man&amp;quot;&lt;&#x2F;code&gt; is indeed a string, and return &lt;code&gt;true&lt;&#x2F;code&gt; if it is. On the other hand if we check whether a number is valid for this spec, using &lt;code&gt;(s&#x2F;valid? string? 6)&lt;&#x2F;code&gt; we instead get &lt;code&gt;false&lt;&#x2F;code&gt; returned. &lt;&#x2F;p&gt;
&lt;p&gt;Another way to use a spec, is to explain why a value is wrong. For example, we can call the function &lt;code&gt;explain-data&lt;&#x2F;code&gt; on with the spec and an incorrect value, to get a map back with an explanation. The function call: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(s&#x2F;explain-data string? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;would result in the map:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:val 6 :predicate :clojure.spec.alpha&#x2F;unknown&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now the above example clearly shows the value on which the spec has failed, but it denotes the predicate as unknown with &lt;code&gt;:clojure.spec.alpha&#x2F;unknown&lt;&#x2F;code&gt;. The solution to this is to provide a name for the spec, which the system can use to pin point if things fail. We can register any spec using the function &lt;code&gt;s&#x2F;def&lt;&#x2F;code&gt;. For example the functions: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::name &lt;&#x2F;span&gt;&lt;span&gt;string?)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::real-name &lt;&#x2F;span&gt;&lt;span&gt;string?)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;will register the two specs under the keys &lt;code&gt;:name&lt;&#x2F;code&gt; and &lt;code&gt;:real-name&lt;&#x2F;code&gt; in the current namespace, i.e.: under &lt;code&gt;spider-man-spec.core&#x2F;name&lt;&#x2F;code&gt; and &lt;code&gt;spider-man-spec.core&#x2F;name&lt;&#x2F;code&gt; respectively.&lt;&#x2F;p&gt;
&lt;p&gt;Now if we would aim to explain why the spec &lt;code&gt;:real-name&lt;&#x2F;code&gt;  does not allow the value &lt;code&gt;6&lt;&#x2F;code&gt;, it would return the explanation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:val 6 :predicate :spider-man-spec.core&#x2F;real-name&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;where the predicate now identifies the spec that was not fulfilled.&lt;&#x2F;p&gt;
&lt;p&gt;Specs can also be created in other ways. For example a set of values indicating the correct values can be used as a spec.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::identity &lt;&#x2F;span&gt;&lt;span&gt;#{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::secret ::publicly-known&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above code defines a spec for identity as having two possible values: either &lt;code&gt;::secret&lt;&#x2F;code&gt; or &lt;code&gt;::publicly-known&lt;&#x2F;code&gt;. &lt;&#x2F;p&gt;
&lt;p&gt;Specs can also be defined for collections as well. The specs for current- and former affiliations: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::current-affilications &lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;coll-of string? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:kind &lt;&#x2F;span&gt;&lt;span&gt;set?))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::former-affilications &lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;coll-of string? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:kind &lt;&#x2F;span&gt;&lt;span&gt;set?))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These specs describe that that both current- and former have to be sets of strings. The affiliations part of a profile is actually map containing both current- and former affiliations. This is defined as the spec:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clojure&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clojure &quot;&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::affiliations &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s&#x2F;keys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:req &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::current-affiliations&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:opt &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::former-affiliations&lt;&#x2F;span&gt;&lt;span&gt;]) )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which makes it requirement for affiliations to contain current-affiliations, but any former affiliations are optional.&lt;&#x2F;p&gt;
&lt;p&gt;For checking whether Spider-Man has a valid profile we can use the &lt;code&gt;s&#x2F;valid?&lt;&#x2F;code&gt; function again. We use the following code to do just that:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;    (let [spider-man-affiliations (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:spider-man-spec.core&#x2F;affiliations &lt;&#x2F;span&gt;&lt;span&gt;spider-man-profile)]
&lt;&#x2F;span&gt;&lt;span&gt;      (s&#x2F;valid? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:spider-man-spec.core&#x2F;affiliations &lt;&#x2F;span&gt;&lt;span&gt;spider-man-affiliations))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;let&lt;&#x2F;code&gt; form is new here, but what it essentially does is deconstructing the while &lt;code&gt;spider-man-profile&lt;&#x2F;code&gt; and associating its affiliations temporarily the &lt;code&gt;spider-man-affiliations&lt;&#x2F;code&gt;. This allows us to use a shorthand when calling functions, instead of writing out everything in a single line.&lt;&#x2F;p&gt;
&lt;p&gt;While this value is also valid according to the spec, as the spec and the value we are checking gets more complex, it could also be useful to gather the exact value that has passed the spec. In such cases we can use &lt;code&gt;s&#x2F;conform&lt;&#x2F;code&gt; to gather these. The call: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(s&#x2F;conform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:spider-man-spec.core&#x2F;affiliations &lt;&#x2F;span&gt;&lt;span&gt;spider-man-affiliations)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Returns the map:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;#:spider-man-spec.core{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:current-affiliations &lt;&#x2F;span&gt;&lt;span&gt;#{&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Avengers&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:former-affiliations &lt;&#x2F;span&gt;&lt;span&gt;#{&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Outlaws&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Secret Defenders&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;New Fantastic Four&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that this is a namespaced map, which is a feature that allows us to refer to the keywords inside a map more efficiently, instead of writing them all out in each case.&lt;&#x2F;p&gt;
&lt;p&gt;The final aspect of each profile, the power grid, is also something that can be given a spec. Each of the powers can only take a whole number value from 1 until 7. We can specify this with the follow spec:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::power-value &lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;and pos-int? #(&amp;gt;= % &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) #(&amp;lt;= % &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;)))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::durability ::power-value &lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::energy ::power-value &lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::fighting ::power-value &lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::intelligence ::power-value &lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::speed ::power-value &lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::strength ::power-value &lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that we use the function &lt;code&gt;s&#x2F;and&lt;&#x2F;code&gt; to combine three specs: that the value should be a positive integer, greater than or equal to 1 and less or equal to 7. Such a combined spec can then be (re-)used like any other.&lt;&#x2F;p&gt;
&lt;p&gt;We can combine all the previous specs together to specify a profile: &lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::profile &lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;keys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;:req &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::name ::real-name ::identity ::affiliations ::power-grid&lt;&#x2F;span&gt;&lt;span&gt;] ) )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For this spec, both Spider-Man and Vulture are valid profiles. However, this is a problem, as it does not allow us to differentiate between a hero and a villain. Of course we do not want to get Vulture get into the same places as Spider-Man can. We must fight him, much like Spider-Man, but in our own way: by creating a spec for which the Spider-Man profile is a valid value, but not that of Vulture.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=spider-man-vs-vulture.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Spider-Man vs Vulture © Marvel Entertainment&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;spec-stacular-spider-man&#x2F;spider-man-vs-vulture.jpg&quot; title=&quot;Spider-Man vs Vulture © Marvel Entertainment&quot; &gt;}} --&gt;
&lt;p&gt;While we can make a separate requirement that only persons with the name &amp;quot;Spider-Man&amp;quot; can fulfill our new &amp;quot;hero-spec&amp;quot; this might be too restrictive. Instead we are going to spec an Avenger profile, so Spider-Man and all his friends can join in, while villains such as Vulture are kept out.&lt;&#x2F;p&gt;
&lt;p&gt;The requirement for an Avenger in our system, is that any-one with the current affiliation of &lt;code&gt;&amp;quot;Avengers&amp;quot;&lt;&#x2F;code&gt; is an avenger. We can describe this requirement as a spec, using a function defined for this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;clj&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-clj &quot;&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span&gt;(defn is-avenger? [profile]
&lt;&#x2F;span&gt;&lt;span&gt;  (contains? (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::current-affiliations &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::affiliations &lt;&#x2F;span&gt;&lt;span&gt;profile) ) &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Avengers&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::avenger-profile &lt;&#x2F;span&gt;&lt;span&gt;(s&#x2F;and &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;::profile &lt;&#x2F;span&gt;&lt;span&gt;is-avenger? ) )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can check whether a profile is a valid Avenger, which will be true for Spider-Man but not for Vulture. Finally, we can get rid of this villain that showed up in our tutorial. In addition, this spec will also make sure that all current members of the Avengers be valid, so Spider-Man can fight freely alongside of them. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=spider-man-avengers.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Our spec answers the question posed in this cover: Spider-Man is indeed an Avenger © Marvel Entertainment&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;spec-stacular-spider-man&#x2F;spider-man-avengers.jpg&quot; title=&quot;Our spec answers the question posed in this cover: Spider-Man is indeed an Avenger © Marvel Entertainment&quot; &gt;}} --&gt;
&lt;p&gt;So there we have it, a brief look at using the spec library to validate data. There are many things that I have not touched, such as the ability to generate values based on the Spec, other ways to compose a spec, etc.&lt;&#x2F;p&gt;
&lt;p&gt;Nonetheless I hope this article gives a solid introduction, and maybe an interest to using the spec library, even if one does not have a Clojure or even a heavy programming background. The source code snippets are available at: &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;newres&#x2F;spider-man-spec&quot;&gt;Spider-Man-Spec&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you have a data validation problem, by all means take a swing at it with the Spec library. I am convinced that the results you will get will be nothing short of spectacular.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Art of Choosing a Programming Language</title>
        <published>2018-03-18T00:00:00+00:00</published>
        <updated>2018-03-18T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/art-of-choosing-language/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/art-of-choosing-language/</id>
        
        <content type="html">&lt;p&gt;Programmers, like professionals in other fields, are passionate about their tools. One of the main elements in the toolbox of coders are programming languages. They allow their users to express solutions through code to tackle a large variety of problems in many domains. &lt;&#x2F;p&gt;
&lt;p&gt;Programming is also an art, as described in the article by Donald Knuth titled &lt;a href=&quot;http:&#x2F;&#x2F;www.paulgraham.com&#x2F;knuth.html&quot;&gt;Computer Programming as an Art&lt;&#x2F;a&gt; and in certain aspects of programming languages can be seen as art styles.&lt;&#x2F;p&gt;
&lt;p&gt;As can be expected with many things that people are passionate about, whether viewed as a tool or an art style, coders can bond or argue about programming languages. Like philosophers of old, these discussions can go quite into depth, but to the outsider the arguments made or the sentiments behind them can be quite opaque. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=schoolofathens.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;If programming languages existed back then, I am sure they would be a hotly argued topic. School of Athens by Raphael&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;art-of-choosing-language&#x2F;schoolofathens.jpg&quot; title=&quot;If programming languages existed back then, I am sure they would be a hotly argued topic.&quot; caption=&quot;School of Athens by Raphael&quot;&gt;}} --&gt;
&lt;p&gt;Here I hope to shed some light to the casual observer on what makes programmers passionate about these languages and why some prefer one over the other. Such analysis can be quite subjective, and very much dependent on the writers experiences and preferences, but I will try my best to give an impartial overview.&lt;&#x2F;p&gt;
&lt;p&gt;In theory many general purpose programming languages are capable of doing the same things. The most commonly used programming languages are Turing complete, meaning that they can all simulate the workings of any Turing machine. Without getting into the full description of what a Turing machine is, for the reader unfamiliar with the concept, this means that any of the languages can express programs for similar tasks.&lt;&#x2F;p&gt;
&lt;p&gt;There are thousands of programming languages. Some older, and going back to the 50s, 60s and 70s and with considerable use still. Others have been released as recently as the last 10 years, and have gained considerable following. Given as I have mentioned that theoretically all these languages can do the same things, one could wonder why new languages are designed.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;history&quot;&gt;History&lt;&#x2F;h2&gt;
&lt;p&gt;Historically, the original computers were instructed by a pure machine language, for example 0s and 1s. Writing programs this way can be tedious and error prone, and the results code can be very difficult to read. This is one of reasons why assembly languages were created. These are languages that are still very much tied into the instruction set of a particular machine, but in a more human readable form, where symbolic names are given for machine instructions. These would be then translated to the pure machine language, to instruct the machine.&lt;&#x2F;p&gt;
&lt;p&gt;While reading and writing programs becomes easier this way, using assembly languages still has disadvantages. First, these languages are still very much tied to the hardware. Different instruction architectures can mean that a program for the same goal would have to be written differently for each architecture. Second, for many the instructions that one has to write this way are still very low level. The argument is made that with a better set of abstractions over assembly, programs can be written in a better way. A program written with such abstractions could be translated, compiled, to the required machine code specific for the required architecture.&lt;&#x2F;p&gt;
&lt;p&gt;The question of which abstractions need to be utilized is at the heart of why there are so many different programming languages. People have different ideas on what these abstractions might be, what the benefits and drawbacks of applying them are. This is at the heart of why people design and use newer programming languages. In the following sections we go through some of the aspects on these abstractions.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=pyramids.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Much like with ancients wonders of next to a modern city, with programming languages old also gives rise to the new, and often co-exists with it. The Giza Pyramids © Robbster1983&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;art-of-choosing-language&#x2F;pyramids.jpg&quot; title=&quot;Much like with ancients wonders of next to a modern city, with programming languages old also gives rise to the new, and often co-exists with it .&quot; caption=&quot;The Giza Pyramids © Robbster1983&quot;&gt;}} --&gt;
&lt;h2 id=&quot;paradigms-and-style&quot;&gt;Paradigms and Style&lt;&#x2F;h2&gt;
&lt;p&gt;As mentioned before, there are different opinions on how programs could be constructed. There are various subjects about on which people have opinions about: how the code is organized and how it is executed, among other elements. This is very much similar to how art styles function. For example the same subject can be painted in two differing styles.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=last-supper-da-vinci.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The Last Supper (Leonardo da Vinci) one of the most famous Renaissance style paintings.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;art-of-choosing-language&#x2F;last-supper-da-vinci.jpg&quot; title=&quot;The Last Supper (Leonardo da Vinci) one of the most famous Renaissance style paintings.&quot; &gt;}} --&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=last-supper-tintoretto.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The Last Supper (Tintoretto) depicts the same subject but in a Mannerist, proto-Baroque style.&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;art-of-choosing-language&#x2F;last-supper-tintoretto.jpg&quot; title=&quot;The Last Supper (Tintoretto) depicts the same subject but in a Mannerist, proto-Baroque style.&quot; &gt;}} --&gt;
&lt;p&gt;Programming languages can be be classified on the different styles, &lt;em&gt;programming paradigms&lt;&#x2F;em&gt; based on the common elements in the approaches.  Some paradigms include:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;imperative&quot;&gt;Imperative&lt;&#x2F;h3&gt;
&lt;p&gt;Imperative code can be seen as a set of commands for the computer to perform. This type of paradigm matches very strongly with how computer hardware is working, as nearly all computer hardware is designed the execute machine language, which is in itself is written in imperative style. &lt;&#x2F;p&gt;
&lt;h3 id=&quot;procedural&quot;&gt;Procedural&lt;&#x2F;h3&gt;
&lt;p&gt;One of the ways one can structure a program is to group together a series of commands. These groups, &lt;em&gt;procedures&lt;&#x2F;em&gt;, can then be called, used or reused as a single entity.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;object-oriented&quot;&gt;Object-Oriented&lt;&#x2F;h3&gt;
&lt;p&gt;Object oriented code uses the notion of &lt;em&gt;objects&lt;&#x2F;em&gt; to organize code. An object is an encapsulation of related state and behavior. For example, consider a software that needs to represent a vehicle. The elements of the state that describe the object, such as &lt;em&gt;colour&lt;&#x2F;em&gt; and &lt;em&gt;make&lt;&#x2F;em&gt;, are called attributes.  Various functionality related to the object, such as calculating the price of the car, are called methods. These concepts allow reuse, as the objects for a car and a motorcycle can share functionality. &lt;&#x2F;p&gt;
&lt;h3 id=&quot;declarative&quot;&gt;Declarative&lt;&#x2F;h3&gt;
&lt;p&gt;In declarative programming, one describes, or more aptly declares what the problem is as opposed to detailing the steps on how to solve it. This contrasts with imperative programming, where one gives the instructions on how to solve it directly. &lt;&#x2F;p&gt;
&lt;h3 id=&quot;functional&quot;&gt;Functional&lt;&#x2F;h3&gt;
&lt;p&gt;Functional programming is one form of declarative programming where programs are constructed using &lt;em&gt;functions&lt;&#x2F;em&gt;, which are analogous and inspired by to mathematical functions. The intention is that these functions are ideally side effect free: their output is dependent solely on their input. This can make code easier to understand and allows for easier use of code written this way. &lt;&#x2F;p&gt;
&lt;h3 id=&quot;logic&quot;&gt;Logic&lt;&#x2F;h3&gt;
&lt;p&gt;The logic paradigm is based around expressing code as a set of logical axioms. These axioms can then be used as a from of knowledge base to derive new knowledge and query. The programs themselves then can be posed as a query in this system. For example, if the knowledge is defined with the axioms &amp;quot;Tweety is a bird&amp;quot; and &amp;quot;Birds are animals&amp;quot;, the system should be able to answer the queries: &amp;quot;Is Tweety and animal?&amp;quot;. &lt;&#x2F;p&gt;
&lt;p&gt;A language can focus on supporting a particular paradigm heavily or have a strong preference for it. For example &lt;a href=&quot;https:&#x2F;&#x2F;www.haskell.org&#x2F;&quot;&gt;Haskell&lt;&#x2F;a&gt; or &lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;&quot;&gt;Clojure&lt;&#x2F;a&gt; lean quite heavily on the functional paradigm, while &lt;a href=&quot;http:&#x2F;&#x2F;www.swi-prolog.org&#x2F;&quot;&gt;Prolog&lt;&#x2F;a&gt; is one of the main logic programming languages. Others, provide an explicit merge of various methodologies, such as &lt;a href=&quot;https:&#x2F;&#x2F;www.scala-lang.org&#x2F;&quot;&gt;Scala&lt;&#x2F;a&gt; that combines elements of object orientation and functional programming.&lt;&#x2F;p&gt;
&lt;p&gt;Preference for a particular language can go beyond the programming paradigms used. Syntax, the structure of how code is written, can matter quite a bit for person&#x27;s view on a particular language. For example &lt;a href=&quot;https:&#x2F;&#x2F;www.python.org&#x2F;&quot;&gt;Python&lt;&#x2F;a&gt; uses indentation for managing the control flow of the code, as opposed to symbols in other languages.&lt;&#x2F;p&gt;
&lt;p&gt;Such preference can go even beyond the actual code itself to the tools one uses to write. While any text editor for editing text can often suffice, people can have differing expectations with regards to integrated development environments (IDEs) or other tools to edit and analyze the code. The lack or existence of specific tooling can also be a factor when deciding between languages.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;available-code-and-libraries&quot;&gt;Available Code and Libraries&lt;&#x2F;h2&gt;
&lt;p&gt;Most coding is done with a particular purpose in mind, and it is rarely the case that the programmer can build everything from the ground up for such a task. In order to build interesting programs, one has to utilize existing knowledge, much like someone would utilize knowledge in a library to come to new insights.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=bibliotheca-alexandrina.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;The Bibliotheca Alexandrina. Photo © Carsten Whimster licensed under CC BY 3.0 https:&amp;#x2F;&amp;#x2F;creativecommons.org&amp;#x2F;licenses&amp;#x2F;by&amp;#x2F;3.0&amp;#x2F;&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- &lt;figure&gt;
  &lt;img src=&quot;bibliotheca-alexandrina.jpg&quot;  &#x2F;&gt;
  &lt;figcaption&gt;
      &lt;h4&gt;The Bibliotheca Alexandrina. Photo © Carsten Whimster licensed under &lt;a href =&quot;https:&#x2F;&#x2F;creativecommons.org&#x2F;licenses&#x2F;by&#x2F;3.0&#x2F;&quot;&gt; CC BY 3.0 &lt;&#x2F;a&gt;.&lt;&#x2F;h4&gt;
  &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt; --&gt;
&lt;p&gt;Existing code can be used as a foundation from which the program can be built. Roughly speaking existing code comes in three main forms. It is either being part of the language (often called the standard library of the language), some external libraries extending the language for a particular purpose, or an existing code base of the application that one can improve upon.&lt;&#x2F;p&gt;
&lt;p&gt;The standard library contains various functionality included with the language itself. For example ways of manipulating files, various connection protocols, support for certain file formats, etc. Of course it is very much helpful if particular support for a certain feature that aims to use is already available with the language itself. This means less code to write and connect. On the other hand there is also some tension with regards to including too many features in the standard library, especially if certain parts of it become outdated, which enlarges the language and makes it more unwieldy. &lt;&#x2F;p&gt;
&lt;p&gt;The external libraries that one can use in a language can also influence the choice of a language. Certain languages have a lot of library support for specific tasks. For example &lt;a href=&quot;https:&#x2F;&#x2F;www.python.org&#x2F;&quot;&gt;Python&lt;&#x2F;a&gt; has a large and active following in the Data Science community. Other languages have a lot of support for many different tasks simply due their age and user base such as &lt;a href=&quot;https:&#x2F;&#x2F;www.java.com&#x2F;en&#x2F;&quot;&gt;Java&lt;&#x2F;a&gt;. By using libraries one does not need to implement certain features from scratch but can reuse existing work and focus on their specific problem at hand.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, not all development starts from scratch, often one has to make additions or improvement to an existing program, in which case the choice of the language has already been made. While a rewrite of the code can often be tempting, linking between two code programming languages is not always trivial. It is often a good idea to continue with an existing language.&lt;&#x2F;p&gt;
&lt;p&gt;There are some exceptions to this as some languages have been designed with the ground up to inter-operate with other languages. A good example of this is &lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;&quot;&gt;Clojure&lt;&#x2F;a&gt; has great interop with &lt;a href=&quot;https:&#x2F;&#x2F;www.java.com&#x2F;en&#x2F;&quot;&gt;Java&lt;&#x2F;a&gt; and&#x2F;or &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;JavaScript&quot;&gt;JavaScript&lt;&#x2F;a&gt;. This allows it to leverage existing libraries already written, and makes it much more attractive to use. &lt;&#x2F;p&gt;
&lt;h2 id=&quot;existing-knowledge&quot;&gt;Existing Knowledge&lt;&#x2F;h2&gt;
&lt;p&gt;Writing code is rarely trivial, and neither is learning new programming languages. Although previous experience helps, especially when dealing with languages with known paradigms, due to slight or large differences it can take a while to get used to the new language and libraries. With constantly looming deadlines and pressure to deliver, it can make sense to minimize the work that needs to be done. It is perfectly valid to work with a language that one already knows.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;curiosity&quot;&gt;Curiosity&lt;&#x2F;h2&gt;
&lt;p&gt;On the other hand learning a new language, especially in a new paradigm or other innovative features, can be quite interesting. It not only allows for work on existing code written in the new language but it also gives insights in how to program which is beneficial as a programmer in general no matter what language he is using.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;speed&quot;&gt;Speed&lt;&#x2F;h2&gt;
&lt;p&gt;As mentioned earlier, commonly used programming languages are abstractions over machine code that can do more of less the same thing computationally. What abstractions are used however can influence the speed of executing the program, as well as the time of translating the code in the programming language to machine code. &lt;&#x2F;p&gt;
&lt;p&gt;A common abstraction that can influence the speed of executing the program is how memory is managed. During the running of a program certain information needs to be stored. A way to do this is to allocate space in the computers memory, keep it around while needed and remove it afterwards. This latter portion, can be quite difficult to manage manually, as if one does it prematurely the program might crash or have other bugs. Not removing it would fill the memory with garbage, which makes the program use up more and more memory till it crashes.&lt;&#x2F;p&gt;
&lt;p&gt;A solution to these problems is automatic garbage collection: a way for the computer to automatically manage and clean up memory. While this is a good solution in many cases, this process comes with an overhead, and can be unpredictable when the time and resource consuming cleanup happens. In most cases this overhead is trivial to pay for eliminating a whole suite of potential bugs. However in certain scenarios, such as real-time high performance games, it could be too much to pay.&lt;&#x2F;p&gt;
&lt;p&gt;The other issue of speed, translating the code from the programming language to machine code, can also be a consideration. Development requires making changes to code and checking whether the changes work. If the process of getting feedback takes a long time, due to these translations, it can destroy a programmers productivity. &lt;a href=&quot;https:&#x2F;&#x2F;golang.org&#x2F;&quot;&gt;Golang&lt;&#x2F;a&gt; is a language that is explicitly designed for fast compilation.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;safety&quot;&gt;Safety&lt;&#x2F;h2&gt;
&lt;p&gt;Safety is in many cases the flip side to the speed argument. Certain abstractions cost you in speed but provide you with safety in return. Different languages tend to make different trade-offs with this regard. For example one of the relatively new languages, &lt;a href=&quot;https:&#x2F;&#x2F;www.rust-lang.org&#x2F;en-US&#x2F;&quot;&gt;Rust&lt;&#x2F;a&gt; aims at focus on zero cost abstractions: abstractions with little to no run-time performance penalty.&lt;&#x2F;p&gt;
&lt;p&gt;One contentious aspect of safety is the use of type systems. Types allow the coder to specify various categories, such as numbers, persons, cars, etc as well as their requirements to be fulfilled within the context of the program. Types can be checked both statically, before the system is run, or dynamically, during the running of the program. Some people swear by very expressive type systems: where types can specify very detailed features of the things the program wants to represent. This then can be used for checking code for correctness, both before and during the running of a program, as well as documentation. On the other hand type checking is not free: it can make translating the compilation into machine code a much slower process. Some people also consider the writing and checking of types themselves very cumbersome during initial development, where quick iteration can be slowed down by specifying detailed types.&lt;&#x2F;p&gt;
&lt;p&gt;There is a whole spectrum of possible stances with regards to type systems. For example, certain languages such as &lt;a href=&quot;https:&#x2F;&#x2F;www.haskell.org&#x2F;&quot;&gt;Haskell&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;www.idris-lang.org&#x2F;&quot;&gt;Idris&lt;&#x2F;a&gt; are designed from the ground up with very expressive type systems that are statically checked. Others, for example &lt;a href=&quot;https:&#x2F;&#x2F;www.dartlang.org&#x2F;&quot;&gt;Dart&lt;&#x2F;a&gt; which started off as having optional types but adds mandatory types in the latest iteration to help with tooling, take a more balanced approach. &lt;a href=&quot;https:&#x2F;&#x2F;golang.org&#x2F;&quot;&gt;Golang&lt;&#x2F;a&gt; explicitly has a static, but minimalist, type system that allows for fast compilation.  There are also languages, such as &lt;a href=&quot;https:&#x2F;&#x2F;clojure.org&#x2F;&quot;&gt;Clojure&lt;&#x2F;a&gt; that instead of static types, use contract systems to ensure safety at run-time and allow for documentation and testing.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;deployment&quot;&gt;Deployment&lt;&#x2F;h2&gt;
&lt;p&gt;While most general purpose programming languages can be made to run in all environments, they are not always available. In certain environments, such as mobile or on the web, only specific languages are supported. For example on Android &lt;a href=&quot;https:&#x2F;&#x2F;www.java.com&#x2F;en&#x2F;&quot;&gt;Java&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;www.java.com&#x2F;en&#x2F;&quot;&gt;Kotlin&lt;&#x2F;a&gt; are officially supported, while on the web &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;JavaScript&quot;&gt;JavaScript&lt;&#x2F;a&gt; is the current Lingua Franca of the web. This means that it can be quite a herculean effort to make other languages work in such environments, and going with the most supported option is easier. &lt;&#x2F;p&gt;
&lt;p&gt;The way certain languages can get around on this hindrance is by using the more commonly supported language as the target to translate into. For example &lt;a href=&quot;https:&#x2F;&#x2F;clojurescript.org&#x2F;&quot;&gt;ClojureScript&lt;&#x2F;a&gt; compiles into &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;JavaScript&quot;&gt;JavaScript&lt;&#x2F;a&gt;. And in some cases, other developers have made the effort to get frameworks up and running that allow the use of a different language, such as the use of &lt;a href=&quot;https:&#x2F;&#x2F;facebook.github.io&#x2F;react-native&#x2F;&quot;&gt;React Native&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;flutter.io&#x2F;&quot;&gt;Flutter&lt;&#x2F;a&gt; that allow the use of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;JavaScript&quot;&gt;JavaScript&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;www.dartlang.org&#x2F;&quot;&gt;Dart&lt;&#x2F;a&gt; respectively to develop mobile applications. &lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-team-and-beyond&quot;&gt;The Team and Beyond&lt;&#x2F;h2&gt;
&lt;p&gt;One final aspect of choosing a programming language, which can be surprisingly significant, is which language is beneficial to the team, as opposed to an individual developer. Different teams bring different expertise to the table, and while most professionals are often quite willing and able to use a new language if it is most suited to the task at hand, this can still be a cost that might be better spent on developing the application. From an employers perspective it can also often be beneficial to stick to more commonly used languages as it can be easier to find future employees versed in the language used. On the other hand, there are many professionals that would be quite willing to jump on the chance of using the latest programming languages, in which case the choice for a newer or more niche language can be a competitive advantage from a recruiting perspective.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;I hope this article gave some insight on why programmers pick and argue about programming languages. Despite all the various differences and arguments it is also very important to note, that great software has been written in many different languages, that is both excellent code and solves important problems. And while picking the right tool for the job is an important, it can be just an aspect of the art of solving problems with code.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Data Interlinked</title>
        <published>2018-02-18T00:00:00+00:00</published>
        <updated>2018-02-18T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/interlinked-data/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/interlinked-data/</id>
        
        <content type="html">&lt;p&gt;&lt;em&gt;This article contains some very minor spoilers for the Blade Runner 2049 movie. If you have not seen it yet, by all means do, it is really good.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&amp;quot;And blood-black nothingness began to spin... A system of cells interlinked within cells interlinked within cells interlinked within one stem... And dreadfully distinct against the dark, a tall white fountain played.&amp;quot;&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=palefire.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Pale Fire by Vladimir Nabokov © Berkley Medallion&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;interlinked-data&#x2F;palefire.jpg&quot; title=&quot;Pale Fire by Vladimir Nabokov&quot; caption=&quot;Cover of the novel Pale Fire © Berkley Medallion&quot;&gt;}} --&gt;
&lt;p&gt;The above is a quote from the poem &lt;em&gt;Pale Fire&lt;&#x2F;em&gt;. It occurs in the novel titled &lt;em&gt;Pale Fire&lt;&#x2F;em&gt; by Vladimir Nabokov which has been recently used in the movie &lt;em&gt;Blade Runner 2049&lt;&#x2F;em&gt;. In the movie it was part of the Baseline test, a way to test the emotional response of a Replicant. The reason they undergo this process is because their creators fear that the connections they might make in their lives would give them emotions that would interfere with their intended purpose.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=bladerunner2049.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt; Blade Runner 2049 Poster for the Movie © Columbia Pictures&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;interlinked-data&#x2F;bladerunner2049.png&quot; title=&quot;Blade Runner 2049&quot; caption=&quot;Poster for the movie © Columbia Pictures&quot;&gt;}} --&gt;
&lt;p&gt;In life, such relationships always surround us. They are not just between people, but in our work, in our beliefs, in our art and in the knowledge we represent.&lt;&#x2F;p&gt;
&lt;p&gt;Suppose we intend to describe the link between the movie &lt;em&gt;Blade Runner&lt;&#x2F;em&gt; and the book &lt;em&gt;Pale Fire&lt;&#x2F;em&gt;. We can summarize this information with a number of facts.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Blade Runner is a movie. 
&lt;&#x2F;span&gt;&lt;span&gt;Blade Runner has a character named K.
&lt;&#x2F;span&gt;&lt;span&gt;K is a Replicant.
&lt;&#x2F;span&gt;&lt;span&gt;Replicants must pass a Baseline Test.
&lt;&#x2F;span&gt;&lt;span&gt;Baseline Test is based on the poem Pale Fire.
&lt;&#x2F;span&gt;&lt;span&gt;Pale Fire is written by Vladimir Nabokov.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above facts show that the links that can tie together various pieces of knowledge. One can trace the connections from a simple description of a movie, released in 2017, to the author Vladimir Nabokov, as was intended by the writers of the movie.&lt;&#x2F;p&gt;
&lt;p&gt;Although the above recitation of facts is easy to follow, from a knowledge representation perspective one can find some issues with it. &lt;&#x2F;p&gt;
&lt;p&gt;First the description is imprecise. As the &lt;em&gt;Blade Runner&lt;&#x2F;em&gt; could refer to the newer &lt;em&gt;Blade Runner 2049&lt;&#x2F;em&gt; movie as opposed to the 1982 original titled &lt;em&gt;Blade Runner&lt;&#x2F;em&gt;. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=bladerunner.jpg&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Movie poster for the first Blade Runner Movie © 1982 The Ladd Company&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;interlinked-data&#x2F;bladerunner.jpg&quot; title=&quot;The first movie titled Blade Runner&quot; caption=&quot;Blade Runner movie poster © 1982 The Ladd Company&quot;&gt;}} --&gt;
&lt;p&gt;Second the set of facts is incomplete. The poem &lt;em&gt;Pale Fire&lt;&#x2F;em&gt; is indeed written by Vladimir Nabokov, but it is presented in the book &lt;em&gt;Pale Fire&lt;&#x2F;em&gt;, also written by Nabokov as the work of the fictional poet &lt;em&gt;John Shade&lt;&#x2F;em&gt;. The set of facts here fails to make the explicit distinction between &lt;em&gt;Pale Fire (poem)&lt;&#x2F;em&gt; and &lt;em&gt;Pale Fire (book)&lt;&#x2F;em&gt;, and that the poem is contained in the book. &lt;&#x2F;p&gt;
&lt;p&gt;Third, and perhaps most importantly, the above list of facts relies a lot on the users grasp of the English natural language. For a program, it can be surprisingly difficult to understand the relationships such as &amp;quot;is a&amp;quot;, &amp;quot;is based on&amp;quot;, &amp;quot;named&amp;quot;, etc between the various elements in the text.&lt;&#x2F;p&gt;
&lt;p&gt;These issues seem somewhat nit-picky, as this information can be derived from the rest of the article. However this means that the knowledge in the summary does not stand on its own. If those facts are detailed without the rest of the article, or if the reader of them is a machine, and not a person that can easily add some context, they might lead them to incorrect or insufficient conclusions. They might get wrong information that the 1982 movie Blade Runner has a character named K, or fail to see the link that poem is contained in the book by the same author. And although in the case of Blade Runner, these issues might seem small, this is different if the knowledge relates to financial, legal or clinical domains. Here, mistakes or omissions can be costly.&lt;&#x2F;p&gt;
&lt;p&gt;Having a larger list of more detailed facts can help with these issues, but to a certain extent they still remain due to the ambiguity of the natural language. In addition the fact that is often very easy to skip over implicit details. This is especially true for the issue of a computer not being able to make (enough) sense of this information. &lt;&#x2F;p&gt;
&lt;p&gt;A proposed solution to these issues is &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;data&quot;&gt;Linked Data&lt;&#x2F;a&gt; and in particular &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;techs&#x2F;rdf#w3c_all&quot;&gt;Resource Description Framework (RDF)&lt;&#x2F;a&gt;, with which Linked Data data can be expressed. These technologies allow us to represent the above facts in a more formal and precise way, that can make it both human and machine read- and write-able. &lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=rdf.png&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Resource Description Framework (RDF) Logo © W3C&lt;&#x2F;h4&gt;
        
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;!-- {{&lt; figure src=&quot;&#x2F;img&#x2F;post&#x2F;2018&#x2F;interlinked-data&#x2F;rdf.png&quot; title=&quot;Resource Description Framework&quot; caption=&quot;Logo of RDF © W3C&quot;&gt;}} --&gt;
&lt;p&gt;One significant feature of RDF is that requires precise naming. Many elements of it are either a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;International_Resource_Identifier&quot;&gt;International Resource Identifier (IRI)&lt;&#x2F;a&gt; or some raw data-types. Good examples of the former are URLs, such as the link to this website: http:&#x2F;&#x2F;www.newresalhaider.com , that allows one to find a web resource. Examples of the later are texts or numbers, such as &amp;quot;Blade Runner&amp;quot; or 15 respectively.&lt;&#x2F;p&gt;
&lt;p&gt;The other significant feature of RDF is that most knowledge is represented as a set of facts, where each fact is expressed as subject, predicate object triples. For example the fact &amp;quot;Blade Runner is a movie&amp;quot; is expressed with the subject &amp;quot;Blade Runner&amp;quot; the predicate &amp;quot;is a&amp;quot; and the object &amp;quot;movie&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Putting this together in RDF (using the &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;turtle&#x2F;&quot;&gt;Turtle&lt;&#x2F;a&gt; notation) you would get a triple such as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Turtle &quot;&gt;&lt;code class=&quot;language-Turtle&quot; data-lang=&quot;Turtle&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;bladerunner&#x2F;blade-runner&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;bladerunner&#x2F;movie&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This example is an RDF way of saying &amp;quot;Blade Runner is a type of movie&amp;quot; or alternatively &amp;quot;Blade runner is a movie&amp;quot;. This type of representation shows us a couple of benefits. First we are now being more precise as each element in the triple can refer to one specific resource, for example Blade Runner or Movie, where there IRI makes sure we do not necessarily confuse the term with anything else. Second this also shows off the fact that you can link to resources from different places: the predicate &amp;quot;type&amp;quot; is from a completely different domain. This allows us to re-use knowledge that has already been defined. As one can expect saying something is of a &amp;quot;type&amp;quot;, for example an apple is a type of a fruit, is actually very common. This is one of the main strengths of what makes Linked Data so powerful, one can re-use knowledge already stated. &lt;&#x2F;p&gt;
&lt;p&gt;Typing out the full IRI each time can be pretty bothersome, and it does not help the readability either. Thankfully we can define a common prefix we use separately, and just write the last part of the IRI in each case. In this case we define a base prefix and we refer to subject and object by &amp;quot;&amp;lt;#blade-runner&amp;gt;&amp;quot; and &amp;quot;&amp;lt;#movie&amp;gt;&amp;quot; respectively.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Turtle &quot;&gt;&lt;code class=&quot;language-Turtle&quot; data-lang=&quot;Turtle&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;bladerunner&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#blade-runner&amp;gt; &amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#type&amp;gt; &amp;lt;#movie&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can do similar things when linking elements that have been already defined elsewhere. In this case we define a prefix to use as an abbreviation while writing:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Turtle &quot;&gt;&lt;code class=&quot;language-Turtle&quot; data-lang=&quot;Turtle&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;bladerunner&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#blade-runner&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#movie&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In practice, &amp;quot;rdf:type&amp;quot; as a predicate is so common that there is an even simpler notation. We can use &#x27;a&#x27; as a predicate, which is in line with what we intend to express: &amp;quot;Blade Runner is a movie&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;The resulting RDF facts look as follows (note that the rdf prefix could be omitted here as the &amp;quot;a&amp;quot; abbreviation does not make it necessary):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Turtle &quot;&gt;&lt;code class=&quot;language-Turtle&quot; data-lang=&quot;Turtle&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;bladerunner&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#blade-runner&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#movie&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we aim to write something that is just a text as a subject, say when referring to the title of a movie , we can do that as well:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Turtle&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Turtle &quot;&gt;&lt;code class=&quot;language-Turtle&quot; data-lang=&quot;Turtle&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;bladerunner&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#blade-runner&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#movie&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#blade-runner&amp;gt; &amp;lt;#title&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Blade Runner 2049&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this way of writing, we can actually rewrite our original set of facts as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ttl&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-ttl &quot;&gt;&lt;code class=&quot;language-ttl&quot; data-lang=&quot;ttl&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.newresalhaider.com&#x2F;ontologies&#x2F;bladerunner&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;rdf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;www.w3.org&#x2F;1999&#x2F;02&#x2F;22-rdf-syntax-ns#&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;@prefix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;foaf: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;http:&#x2F;&#x2F;xmlns.com&#x2F;foaf&#x2F;0.1&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; .
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#blade-runner&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#movie&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#blade-runner&amp;gt; &amp;lt;#title&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Blade Runner 2049&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#blade-runner&amp;gt; &amp;lt;#has-character&amp;gt; &amp;lt;#K&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#K&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;foaf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#K&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#replicant&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#replicant&amp;gt; &amp;lt;#must-pass&amp;gt; &amp;lt;#baseline-test&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#baseline-test&amp;gt; &amp;lt;#based-on&amp;gt; &amp;lt;#pale-fire-poem&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#pale-fire-poem&amp;gt; &amp;lt;#included-in&amp;gt; &amp;lt;#pale-fire-book&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#pale-fire-book&amp;gt; &amp;lt;#written-by&amp;gt; &amp;lt;#nabokov&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;lt;#nabokov&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;foaf:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Vladimir Nabokov&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this version we suddenly defined our list of facts in a more formal manner than previously. This makes it much more simpler for machines to understand this set of facts. In fact we actually used the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;FOAF_(ontology)&quot;&gt;Friend of a Friend (FOAF) ontology&lt;&#x2F;a&gt; to use the notion of name that is also used when talking about relationships between people. In fact, one could argue that using an existing movie dataset, such as the &lt;a href=&quot;https:&#x2F;&#x2F;old.datahub.io&#x2F;dataset&#x2F;linkedmdb&quot;&gt;Linked Movie Database&lt;&#x2F;a&gt; would have been even better, which we will leave as an exercise for the reader.&lt;&#x2F;p&gt;
&lt;p&gt;Hopefully I could show a glimpse of the possibilities the &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;&quot;&gt;Semantic Web&lt;&#x2F;a&gt; for which Linked Data forms the basis, with this example. Of course the above is just scratching the surface of what it can be done with RDF, Linked Data. With each addition, our set of facts could grow. One could go beyond a single movie and build a document of poems that are references in movies, or a knowledge base of the Blade Runner franchise. It might be easier than one expects, due to the fact that knowledge, much like people are...&lt;&#x2F;p&gt;
&lt;p&gt;Interlinked.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Trouble with Triples</title>
        <published>2018-01-28T00:00:00+00:00</published>
        <updated>2018-01-28T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/trouble-with-triples/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/trouble-with-triples/</id>
        
        <content type="html">&lt;p&gt;In the Star Trek episode &amp;quot;The Trouble with Tribbles&amp;quot; the crew of the starship Enterprise encounters creatures called Tribbles. They are cute, simple creatures of mysterious origin that seem harmless at first but when they multiply the pose a big problem for the ship and the crew.&lt;&#x2F;p&gt;
&lt;figure class=centeredfig&gt;
    &lt;img src=featured.jpg&gt;
    
    &lt;alt= Tribbles © 1967 Paramount Pictures&gt;
    
    &lt;figcaption&gt;
        
        &lt;h4&gt;Tribbles&lt;&#x2F;h4&gt;
        
        
        Tribbles © 1967 Paramount Pictures
        
    &lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Representing and reasoning with knowledge have surprisingly similar problems. A single fact on its own is a relatively straightforward affair. A fact, such as &lt;strong&gt;&amp;quot;Tribbles are cute&amp;quot;&lt;&#x2F;strong&gt; can be represented with only three parts of a triple: a subject &lt;strong&gt;Tribbles&lt;&#x2F;strong&gt;, a predicate &lt;strong&gt;are&lt;&#x2F;strong&gt; and an object &lt;strong&gt;cute&lt;&#x2F;strong&gt;. Things can get quite a bit more difficult when there are more facts&#x2F;triples: &lt;strong&gt;&amp;quot;Tribbles are round&amp;quot;&lt;&#x2F;strong&gt;, &lt;strong&gt;&amp;quot;Tribbles are furry&amp;quot;&lt;&#x2F;strong&gt;, &lt;strong&gt;&amp;quot;Tribbles originate from Iota Geminorum IV&amp;quot;&lt;&#x2F;strong&gt;, and other millions of facts that one could have about such a species. This is especially true when one takes into the account that the fact that knowledge can be interlinked &lt;strong&gt;&amp;quot;Iota Geminorum IV is a planet&amp;quot;&lt;&#x2F;strong&gt;,  &lt;strong&gt;&amp;quot;Iota Geminorum IV is also known as Fafniri&amp;quot;&lt;&#x2F;strong&gt;, &lt;strong&gt;&amp;quot;Iota Geminorum IV is also known as Tribble Prime&amp;quot;&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This makes representing and reasoning with facts a non-trivial process. A system that holds all this knowledge should be able to answer a query such as &lt;strong&gt;&amp;quot;Do Tribbles originate from Fafniri?&amp;quot;&lt;&#x2F;strong&gt; with a yes, based on the facts &lt;strong&gt;&amp;quot;Tribbles originate from Iota Geminorum IV&amp;quot;&lt;&#x2F;strong&gt;, &lt;strong&gt;&amp;quot;Iota Geminorum IV is a planet&amp;quot;&lt;&#x2F;strong&gt; and &lt;strong&gt;&amp;quot;Iota Geminorum IV is also known as Fafniri&amp;quot;&lt;&#x2F;strong&gt;, even in the context of millions of other triples. &lt;&#x2F;p&gt;
&lt;p&gt;Another interesting issue with representing facts is the context of the information. To us the viewers, and initially to the crew of the Enterprise, Tribbles look like harmless and adorable creatures. To the Klingons they are an ecological menace and their mortal enemies. How such &amp;quot;knowledge about knowledge&amp;quot; is represented and used is often a challenging problem.&lt;&#x2F;p&gt;
&lt;p&gt;Various technologies have been proposed to deal with the above-mentioned issues. &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;&quot;&gt;The Semantic Web technologies&lt;&#x2F;a&gt; of &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;semanticweb&#x2F;data&quot;&gt;Linked Data&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;standards&#x2F;techs&#x2F;owl#w3c_all&quot;&gt;Ontologies&lt;&#x2F;a&gt; in particular have been designed around solving many of these problems. Nonetheless there is room for improvement. In the future I hope to be able to explain how these techniques can be utilized and perhaps lessen the pain points that currently surround (the use of) them.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Introduction</title>
        <published>2018-01-14T00:00:00+00:00</published>
        <updated>2018-01-14T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/post/introduction/" type="text/html"/>
        <id>https://www.newresalhaider.com/post/introduction/</id>
        
        <content type="html">&lt;p&gt;Welcome to the first post on my blog on which hopefully many will follow. My intention is to write about knowledge representation, reasoning, AI and coding, in an easily digestible but in depth-way.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Legal Regulatory Compliance Within The Financial Domain.</title>
        <published>2017-12-31T00:00:00+00:00</published>
        <updated>2017-12-31T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/project/legal-banking-compliance/" type="text/html"/>
        <id>https://www.newresalhaider.com/project/legal-banking-compliance/</id>
        
        <content type="html">&lt;p&gt;Regulatory compliance is the goal of an organization to ensure it complies with all the relevant policies, regulations and laws. Failure to comply can have grave consequences for an organization, with huge fines and penalties being imposed. Regulatory pressure has been increasing over the years, with new and more complex regulation being enacted. This is especially true for the financial sector, where in the wake of the financial crisis, new policies and laws where enacted, both internal and external, to prevent a similar crisis happening in the future.&lt;&#x2F;p&gt;
&lt;p&gt;These new and changing policies and laws give a huge challenge for financial organizations. These issues get compounded by fact that in a global world, internal, national and international regulations create an interwoven set of rules that need to be interpreted and applied on various financial products and services. Ensuring this compliance, is a time and expertise intensive task that often needs to be done manually. Automating these tasks is a huge challenge due to the fact that more traditional techniques are often ill equipped to tackle such an ever changing and complex domain in a way that is transparent to the domain experts and regulators.&lt;&#x2F;p&gt;
&lt;p&gt;In this project, the goal is to find innovative techniques and solutions to the issue of regulatory compliance in the legal and financial domain.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Smart Industry Software</title>
        <published>2017-12-30T00:00:00+00:00</published>
        <updated>2017-12-30T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/project/innius/" type="text/html"/>
        <id>https://www.newresalhaider.com/project/innius/</id>
        
        <content type="html">&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;innius.com&quot;&gt;innius&lt;&#x2F;a&gt; is a powerful smart industry software platform for industrial machine manufacturers and industrial machine users. &lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Ontology Enabled Clinical Software</title>
        <published>2017-12-29T00:00:00+00:00</published>
        <updated>2017-12-29T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/project/clinical-semantics/" type="text/html"/>
        <id>https://www.newresalhaider.com/project/clinical-semantics/</id>
        
        <content type="html">&lt;p&gt;The time of clinical health professionals is incredibly valuable. Software can alleviate the burden of physicians, nursers and other health experts and allow them to provide better, more effective health care to patients. &lt;&#x2F;p&gt;
&lt;p&gt;Creating, maintaining and using software in clinical environments is a difficult process. Not only it is paramount that such software needs to be correct, it has to have an understanding of highly domain specific knowledge about clinical processes in order to provide beneficial support to health professionals.&lt;&#x2F;p&gt;
&lt;p&gt;Semantic Web technologies, in particular anthologies enable better clinical software. Ontologies allow for a formal and explicit way to model knowledge that is understandable for both clinicians and machines alike. &lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Semantic Aware Software Analysis</title>
        <published>2017-12-22T00:00:00+00:00</published>
        <updated>2017-12-22T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/project/semantic-software-analysis/" type="text/html"/>
        <id>https://www.newresalhaider.com/project/semantic-software-analysis/</id>
        
        <content type="html">&lt;p&gt;Software bugs and errors have a monumental negative impact on society. Not only can they have huge monetary cost, ranging into billions of dollars annually in the United States alone, but in some cases can even lead to disasters that lead to the loss of life. Preventing such issues from happening requires a large amount of effort in designing and maintaining software. &lt;&#x2F;p&gt;
&lt;p&gt;However designing and maintaining error free software remains a difficult problem. Not only is software getting more complex, its correct functioning often depends on highly specific domain knowledge of its purpose and its environment. Therefor manually checking the correctness of the software is expensive in both time and available expertise.&lt;&#x2F;p&gt;
&lt;p&gt;A potential solution that was proposed for this is using machine understandable domain knowledge to help automate software maintenance tasks. Formal ontologies have been proposed and used in various fields to make domain knowledge explicit and usable for both humans and software. In particular during my thesis work, titled &amp;quot;An Ontology Based Framework for Specification Mining and Dynamic Program Analysis&amp;quot;, the use of ontologies to help understand the working of a running program were successfully explored. In particular it was shown that for a category of bugs, general ontologies such as WordNet, can be used find issues issues from program traces.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Integrating Existing Large Scale Medical Laboratory Data into the Semantic Web Framework</title>
        <published>2014-10-27T00:00:00+00:00</published>
        <updated>2014-10-27T00:00:00+00:00</updated>
        <author>
          <name>Newres Al Haider</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/publication/medical-data-semantic/" type="text/html"/>
        <id>https://www.newresalhaider.com/publication/medical-data-semantic/</id>
        
        <content type="html">&lt;h1 id=&quot;abstract&quot;&gt;Abstract&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;Semantic Web technologies have shown to have great potential in many different domains, to facilitate knowledge representation, exchange and reasoning, in a formal and yet both human and machine understandable way. In particular, within the health domain, they enable knowledge integration and understanding by explicitly defining and linking concepts and relationships using ontologies to information within clinical knowledge bases. This additional metadata also allows for automated decision support and semantic based analytics to be implemented, that facilitate improved healthcare at a lower cost. Unfortunately many existing datasets in healthcare environments are still stored in relational databases, as opposed to using semantic technologies. Due to this, the link with explicit metadata is often lacking or non-existent. Furthermore, both the databases and the clinical terminologies can be considerably large, making the mapping and subsequent uses of the information a difficult process. In a full fledged decision support system the level and accuracy of the mapping can greatly influence the effectiveness of any subsequent analysis and decision support tasks. This is especially true in clinical scenarios, where very large and complex sets of terms need to be mapped to relational databases. In this paper we aim to provide a general approach for interlinking relational data with clinical ontology based metadata that allows for a fine grade evaluation, with respect to the mapping&#x27;s impact on analytics. We evaluate our approach by mapping information from clinical terminologies, such as SNOMED CT, to a large laboratory dataset contained in a relational database, with the goal of creating a full fledged, semantically enabled, analytics and decision support system.&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;authors&quot;&gt;Authors&lt;&#x2F;h1&gt;
&lt;p&gt;Newres Al Haider, Samina Abidi,  William Van Woensel, Syed SR Abid&lt;&#x2F;p&gt;
&lt;h1 id=&quot;appeared&quot;&gt;Appeared&lt;&#x2F;h1&gt;
&lt;p&gt;In 2014 IEEE International Conference on Big Data (Big Data)
&lt;a href=&quot;http:&#x2F;&#x2F;ieeexplore.ieee.org&#x2F;abstract&#x2F;document&#x2F;7004338&#x2F;&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-project&quot;&gt;Related Project&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;project&#x2F;clinical-semantics&#x2F;&quot;&gt;Clinical Semantics&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Cross-Platform Benchmark Framework for Mobile Semantic Web Reasoning Engines</title>
        <published>2014-10-19T00:00:00+00:00</published>
        <updated>2014-10-19T00:00:00+00:00</updated>
        <author>
          <name>Unknown</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/publication/benchmark-framework/" type="text/html"/>
        <id>https://www.newresalhaider.com/publication/benchmark-framework/</id>
        
        <content type="html">&lt;h1 id=&quot;abstract&quot;&gt;Abstract&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;Semantic Web technologies are used in a variety of domains for their ability to facilitate data integration, as well as enabling expressive, standards-based reasoning. Deploying Semantic Web reasoning processes directly on mobile devices has a number of advantages, including robustness to connectivity loss, more timely results, and reduced infrastructure requirements. At the same time, a number of challenges arise as well, related to mobile platform heterogeneity and limited computing resources. To tackle these challenges, it should be possible to benchmark mobile reasoning performance across different mobile platforms, with rule- and datasets of varying scale and complexity and existing reasoning process flows. To deal with the current heterogeneity of rule formats, a uniform rule- and data-interface on top of mobile reasoning engines should be provided as well. In this paper, we present a cross-platform benchmark framework that supplies 1) a generic, standards-based Semantic Web layer on top of existing mobile reasoning engines; and 2) a benchmark engine to investigate and compare mobile reasoning performance.&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;authors&quot;&gt;Authors&lt;&#x2F;h1&gt;
&lt;p&gt;William Van Woensel, Newres Al Haider, Ahmad Marwan Ahmad, Syed SR Abidi&lt;&#x2F;p&gt;
&lt;h1 id=&quot;appeared&quot;&gt;Appeared&lt;&#x2F;h1&gt;
&lt;p&gt;In WODA &#x27;10 Proceedings of the Eighth International Workshop on Dynamic Analysis &lt;a href=&quot;https:&#x2F;&#x2F;web.cs.dal.ca&#x2F;~woensel&#x2F;paper&#x2F;A%20Cross-Platform%20Benchmark%20Framework%20for%20Mobile%20Semantic%20Web%20Reasoning%20Engines.pdf&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-project&quot;&gt;Related Project&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;project&#x2F;clinical-semantics&#x2F;&quot;&gt;Clinical Semantics&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Comparison of Mobile Rule Engines for Reasoning on Semantic Web Based Health Data</title>
        <published>2014-08-11T00:00:00+00:00</published>
        <updated>2014-08-11T00:00:00+00:00</updated>
        <author>
          <name>William Van Woensel</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/publication/comparison-mobile-rule-engines/" type="text/html"/>
        <id>https://www.newresalhaider.com/publication/comparison-mobile-rule-engines/</id>
        
        <content type="html">&lt;h1 id=&quot;abstract&quot;&gt;Abstract&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;Semantic Web technology is used extensively in the health domain, due to its ability to specify expressive, domain-specific data, as well as its capacity to facilitate data integration between heterogeneous, health-related sources. In the health domain, mobile devices are an essential part of patient self-management approaches, where local clinical decision support is applied to ensure that patients receive timely clinical findings. Currently, increases in mobile device capabilities have enabled the deployment of Semantic Web technologies on mobile platforms, enabling the consumption of rich, semantically described health data. To make this semantic health data available to local decision support as well, Semantic Web reasoning should be deployed on mobile platforms. However, there is currently a lack of software solutions and performance analysis of mobile, Semantic Web reasoning engines. This paper presents and compares the mobile benchmarks of 4 reasoning engines, applied on a dataset and rule set for patients with A trial Fibrillation (AF). In particular, these benchmarks investigate the scalability of the mobile reasoning processes, and study reasoning performance for different process flows in decision support. For the purpose of these benchmarks, we extended a number of existing rule engines and RDF stores with Semantic Web reasoning capabilities.&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;authors&quot;&gt;Authors&lt;&#x2F;h1&gt;
&lt;p&gt;William Van Woensel, Newres Al Haider, Patrice C Roy, Ahmad Marwan Ahmad, Syed SR Abidi&lt;&#x2F;p&gt;
&lt;h1 id=&quot;appeared&quot;&gt;Appeared&lt;&#x2F;h1&gt;
&lt;p&gt;publication = &amp;quot;In Proceedings of the 2014 IEEE&#x2F;WIC&#x2F;ACM International Joint Conferences on Web Intelligence (WI) and Intelligent Agent Technologies (IAT)-Volume 01&amp;quot;
&lt;a href=&quot;https:&#x2F;&#x2F;dl.acm.org&#x2F;citation.cfm?id=2682782&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-project&quot;&gt;Related Project&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;project&#x2F;clinical-semantics&#x2F;&quot;&gt;Clinical Semantics&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Towards Guideline Compliant Clinical Decision Support System Integration in Smart and Mobile Environments: Formalizing and Using Clinical Guidelines For Diagnosing Sleep Apnea</title>
        <published>2014-06-18T00:00:00+00:00</published>
        <updated>2014-06-18T00:00:00+00:00</updated>
        <author>
          <name>Patrice C Roy</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/publication/towards-cdss-mobile/" type="text/html"/>
        <id>https://www.newresalhaider.com/publication/towards-cdss-mobile/</id>
        
        <content type="html">&lt;h1 id=&quot;abstract&quot;&gt;Abstract&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;Assistive technologies in smart environments were developed in order to maintain and improve the quality of life of people with dementia or other health problems. In order to provide adequate support at the opportune moment, it is necessary to deploy ambient services, such as activity recognition and assistance planning. Clinical Decision Support Systems (CDSS) that implement clinical guidelines, allow for the right clinical decisions, such as diagnoses and treatment choices, to be made automatically based on patient data and other health information. While data derived from smart home services can be used in these CDSS, smart homes can be used to provide services related to clinical decisions. Mobile devices can be used in conjunction with the smart home services and a remote CDSS for sending notifications or retrieve data from wearable or built-in sensors. However, in a context where smart homes interacts with a remote CDSS, we must take into account mobility (e.g. outdoors, work), failure tolerance (e.g. connection issues with remote CDSS) and privacy concerns in order to provide minimum quality of service. Thus, CDSS must be locally deployed as a smart home service and on a mobile device. In this paper we investigate a scenario where due to the above mentioned reasons a clinical guideline compliant CDSS needs to be deployed on a mobile device and as a smart home service, where clinical data can come from either the patient (manual input) or the smart home and mobile sensors. In particular we focus on implementing and evaluating a guideline for the diagnosis of sleep apnea. Sleep apnea diagnosis is a well-suited task for this purpose as attributes for the execution of the guideline could be collected both from the patient and sensor data inside or outside the smart home environment. In order to illustrate the feasibility of CDSS as smart home service and on mobile device, the Sleep Apnea CDSS is validated on an Android smartphone and show promising results.&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;authors&quot;&gt;Authors&lt;&#x2F;h1&gt;
&lt;p&gt;Patrice C Roy, Newres Al Haider, William Van Woensel, Ahmad Marwan Ahmad, Syed SR Abidi&lt;&#x2F;p&gt;
&lt;h1 id=&quot;appeared&quot;&gt;Appeared&lt;&#x2F;h1&gt;
&lt;p&gt;In The Twenty-Eighth AAAI Conference on Artificial Intelligence (AAAI-14), Québec City, Canada
&lt;a href=&quot;http:&#x2F;&#x2F;www.aaai.org&#x2F;ocs&#x2F;index.php&#x2F;WS&#x2F;AAAIW14&#x2F;paper&#x2F;download&#x2F;8828&#x2F;8376&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-project&quot;&gt;Related Project&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;project&#x2F;clinical-semantics&#x2F;&quot;&gt;Clinical Semantics&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Execution Trace Exploration and Analysis using Ontologies</title>
        <published>2011-09-27T00:00:00+00:00</published>
        <updated>2011-09-27T00:00:00+00:00</updated>
        <author>
          <name>Newres Al Haider</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/publication/execution-trace-analysis/" type="text/html"/>
        <id>https://www.newresalhaider.com/publication/execution-trace-analysis/</id>
        
        <content type="html">&lt;h1 id=&quot;abstract&quot;&gt;Abstract&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;Dynamic analysis is the analysis of the properties of a running program. In order to perform dynamic analysis, information about the running program is often collected through execution traces. Exploring and analyzing these traces can be an issue due to their size and that knowledge of a human expert is often needed to derive the required conclusions. In this paper we provide a framework in which the semantics of execution traces, as well as that of dynamic analyses, are formally represented through ontologies. In this framework the exploration and analysis of the traces is enabled through semantic queries, and enhanced further through automated reasoning on the ontologies. We will also provide ontologies to represent traces and some basic dynamic analysis techniques, along with semantic queries that enable these techniques. Finally we will illustrate our approach through an example.&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;authors&quot;&gt;Authors&lt;&#x2F;h1&gt;
&lt;p&gt;Newres Al Haider, Benoit Gaudin, John Murphy&lt;&#x2F;p&gt;
&lt;h1 id=&quot;appeared&quot;&gt;Appeared&lt;&#x2F;h1&gt;
&lt;p&gt;In RV &#x27;11 International Conference on Runtime Verification
&lt;a href=&quot;https:&#x2F;&#x2F;www.researchgate.net&#x2F;profile&#x2F;Joshua_Taylor2&#x2F;publication&#x2F;236133160_Software_Analysis_in_the_Semantic_Web&#x2F;links&#x2F;5491a3b40cf269b0486167ff.pdf&quot;&gt;Link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-project&quot;&gt;Related Project&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;project&#x2F;semantic-software-analysis&#x2F;&quot;&gt;Semantic Software Analysis&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>An Approach for Modeling Dynamic Analysis using Ontologies</title>
        <published>2010-07-12T00:00:00+00:00</published>
        <updated>2010-07-12T00:00:00+00:00</updated>
        <author>
          <name>Newres Al Haider</name>
        </author>
        <link rel="alternate" href="https://www.newresalhaider.com/publication/approach-da-ontologies/" type="text/html"/>
        <id>https://www.newresalhaider.com/publication/approach-da-ontologies/</id>
        
        <content type="html">&lt;h1 id=&quot;abstract&quot;&gt;Abstract&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;In this paper we present the possibility of using an ontology based framework in order to model Dynamic Analysis techniques. This work relies on similar ideas applied to the case of Static Analysis, in which ontologies are used to represent some knowledge about the programs to be analyzed. In the approach proposed in this paper we describe how ontologies can be applied to Dynamic Analysis by modeling both the information collected from the system, as well as some requirements about the type of analysis to be performed. Both of these ontologies can be designed by integrating ontologies previously defined during the software development cycle, allowing for re-usability. Finally, these ontologies make it possible to reason about concepts related to Dynamic Analysis and offer tools that facilitate automation. This paper presents the main ideas of the proposed approach and illustrates them with an example related to Frequency Spectrum Analysis.&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;authors&quot;&gt;Authors&lt;&#x2F;h1&gt;
&lt;p&gt;Newres Al Haider, Benoit Gaudin, Paddy Nixon&lt;&#x2F;p&gt;
&lt;h1 id=&quot;appeared&quot;&gt;Appeared&lt;&#x2F;h1&gt;
&lt;p&gt;&amp;quot;In WODA &#x27;10 Proceedings of the Eighth International Workshop on Dynamic Analysis&amp;quot;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;related-project&quot;&gt;Related Project&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.newresalhaider.com&#x2F;project&#x2F;semantic-software-analysis&#x2F;&quot;&gt;Semantic Software Analysis&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
