<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://cbdumas.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://cbdumas.github.io/" rel="alternate" type="text/html" /><updated>2024-12-16T05:50:30+00:00</updated><id>https://cbdumas.github.io/feed.xml</id><title type="html">Chris Dumas</title><subtitle>Exploration without expectation</subtitle><entry><title type="html">Puppy Training Journal</title><link href="https://cbdumas.github.io/2023/04/17/puppy_journal.html" rel="alternate" type="text/html" title="Puppy Training Journal" /><published>2023-04-17T00:00:00+00:00</published><updated>2023-04-17T00:00:00+00:00</updated><id>https://cbdumas.github.io/2023/04/17/puppy_journal</id><content type="html" xml:base="https://cbdumas.github.io/2023/04/17/puppy_journal.html"><![CDATA[<p>We got a puppy! This is Nora:</p>

<p><img src="/assets/nora_pup.webp" alt="Puppy Tax" /></p>

<p>We got her from PAWS in Lynwood on Friday March 24, 2023.
The shelter told us she was 10 weeks old at that time, and she weighed about 18 pounds.
Today just over three weeks later she weighs over 25 pounds!
It’s been a whirlwind three weeks with a lot of hard work, a few setbacks, and so much change.</p>

<h2 id="week-1">Week 1</h2>

<h3 id="sleep">Sleep</h3>

<p>Week one was very tough. The first night we tried to have her sleep in our room which was a mistake.
She had diarrhea on the floor several times and no one slept more than an hour or two. 
The following nights that week were a slight improvement, as we had decided to have her sleep in her crate in the living
room while we took turns sleeping on the couch nearby.
However she still had diarrhea for the first few days,
and had to go out about every 90 minutes around the clock so again no one was really sleeping.</p>

<h3 id="behavior">Behavior</h3>

<p>The first week once she settled in she was biting absolutely everything, making no distinction between toys, furniture, shoes, toes, and hands.
She was so rowdy and eager to play (in short puppy stretches) and explore our whole space, but we blocked off the
top floor to give the cats some refuge. She was also nervous about going down the stairs to go outside, so we had
to carry her the first couple of days of potty training.</p>

<p>On the subject of potty training, week one was definitely hardest but overall has not been too bad.
During the day we took her out every hour, always after meals and play sessions as well.
She did have a few accidents in the house but learned quickly that she was supposed to go outside.
The hardest part of potty training was definitely the diarrhea, but by the end of week one that had started to clear up.</p>

<p>Since day one we have been working on her training.
I love dogs but being around untrained, poorly behaved dogs is a chore at best.
No one thinks your out of control dog is as cute as you do.
This first week we were finding training tips online on Reddit, Youtube, and Instagram.
She came to us knowing how to sit, and for this first week that was good enough in terms of commands.
The main thing we wanted to work with her on at this point was biting.
She was biting us quite a lot so we started to interrupt and redirect that behavior whenever she started.</p>

<p>Nora also came to us exhibiting a kind of aggressive over-excitement behavior, mostly when we were outside on the leash.
She would suddenly go from normal puppy biting to growly and lunging in a flash, which we really wanted to get ahead of.</p>

<h3 id="broken-tooth">Broken Tooth</h3>

<p>Our big setback week one was a broken tooth. She was very eager to play tug, which is one of my favorite games to play with a dog.
So we were playing tug with a rope toy (in a way I didn’t think was too hard) and suddenly the rope came free from her mouth
with snap and a small yelp of surprise. I heard a piece of the tooth hit the floor or wall, and immediately ran after her to see
if she was OK. She seemed alright, but later that night I notices some signs of pain and so the next day the vet had a look and
decided the broken tooth (her top right canine) had better be extracted, which it was, that same day (Saturday).</p>

<h2 id="week-2">Week 2</h2>

<h3 id="sleep-1">Sleep</h3>

<p>For most of this week we were taking turns sleeping on the couch next to the crate. She was sleeping much better though the night but still had to go out in the middle of the night at least once.</p>

<h3 id="puppy-class">Puppy Class</h3>

<p>On Monday of week two Nora attended her first of six weekly ‘puppy kindergarten’ classes.
She had a great time playing with the other dogs:</p>

<video loop="" controls="" autoplay="" muted="" playsinline="" width="100%">
  <source src="/assets/Nora_Miles.webm" type="video/webm" />
  <source src="/assets/Nora_Miles_HEVC.mp4" type="video/mp4; codecs=&quot;hevc1&quot;" />
</video>

<p>Her prime socialization period is winding down in the next few weeks so we wanted to get her around other puppys to learn good manners when playing.
We also learned some great tips on training her not to jump on guests as well as basic commands, I really can’t recommend a puppy class highly enough.</p>

<h3 id="broken-tooth-recovery">Broken Tooth recovery</h3>

<p>The hardest part about having her tooth extracted was that during recovery she wasn’t supposed to have any chew toys.
At first the vet had asked for a complete ban on chewing but that just wasn’t possible.
She wanted to chew everything she could reach, to the point where every time we left the house to walk her
she would find the first stick she could grab and just sit down to chew it.
When she was in the house she would go after feet, furniture, the bars of her crate etc.
We did make it through most of week two with her only having access to plush chew toys.</p>

<h2 id="week-3">Week 3</h2>

<h3 id="solo-dog-duty">Solo Dog Duty</h3>

<p>This week Harmony was away from Wednesday to Sunday, meaning I’d have to manage Nora and the cats on my own.
By this time we had made enough progress that I wasn’t too worried, though I was a little apprehensive about finding enough time for sleep and work.
She ended up sticking to the 11-7 sleep schedule fairly well, although on most mornings the cats woke up around 5:30 for their breakfast,
which woke Nora up, which meant everybody was up 😴</p>

<h3 id="sleep-2">Sleep</h3>
<p>Our current sleeping arrangement has her sleeping in her crate from 11:00 PM to 7:00 AM, which she can usually do without any issues.
Occasionally she wakes up around 5:00 and needs to go out,
 but if she goes right back into her crate she’ll typically fall back asleep until the designated wake up time.
My own sleep situation during this time was not great, between her and the cats and me being on my own I was only sleeping maybe five or six hours per night for most of this week.</p>

<h3 id="broken-tooth-recovery-1">Broken Tooth Recovery</h3>
<p>By the middle of this week I had broken down and we were giving her some chewing time on her softer chews.
I was checking her gums every day and she seemed to be healing very well, and by the end of the week her two week recovery period had passed.
She back to eating kibble, chewing on digestible chew toys, and playing some (gentle) tug in the living room.
She was so happy to be able to chew again!
Her adult teeth will be coming in over the next couple of months, so we’ll have to keep redirecting her chewing to the proper outlets (squeak toys and beef tendon are her favorites).</p>

<h3 id="personality">Personality</h3>
<p>It feels like her personality is really starting to come through, and she is such a fun dog!
She wants to meet everyone she sees when we go out walking, dogs and humans.
We are working on her greeting people respectfully but when we run in to strangers outside she gets too excited.</p>

<p>When we are inside and free of distractions her training is going great, though transferring that to other settings
and with distractions is not really in the cards yet.
Sometimes inside though she can be very willful and wants to ignore commands, but I’ve found that if I wait her out she will eventually comply.</p>

<p>She doesn’t like cuddling, or getting on furniture.
In fact she won’t even sleep on the rug, when she goes down to sleep at night she always moves under the table on the bamboo floor before I put her in her crate.
But that’s not to say she ignores us, when she is out of her crate she is constantly by our sides and if we leave her downstairs to get something upstairs
she will often whine and bark and run around until we come back down.</p>

<h3 id="puppy-class-1">Puppy Class</h3>
<p>Her first week of puppy class she got to play in a milieu of all the puppys, but now by week three she is too big for most of them.
She got segmented off with another large pup last night, but unfortunately the two of them did not get along well.</p>

<h2 id="to-be-continued-">To Be Continued 🐶</h2>
<p>I guess we’re dog people now.
Life with Nora has been challenging, and rewarding; sometimes fun, sometimes boring; constantly changing and I can’t wait to see what’s next!</p>]]></content><author><name></name></author><summary type="html"><![CDATA[We got a puppy! This is Nora:]]></summary></entry><entry><title type="html">Church-encoded datatypes in Haskell, part 2</title><link href="https://cbdumas.github.io/2015/12/27/church_encoding2.html" rel="alternate" type="text/html" title="Church-encoded datatypes in Haskell, part 2" /><published>2015-12-27T00:00:00+00:00</published><updated>2015-12-27T00:00:00+00:00</updated><id>https://cbdumas.github.io/2015/12/27/church_encoding2</id><content type="html" xml:base="https://cbdumas.github.io/2015/12/27/church_encoding2.html"><![CDATA[<p><a href="/2015/12/19/church_encoding.html">Last time</a> we covered Haskells Maybe type encoded as a function.
This post will introduce two recursively defined structures, lists and natural numbers. Here I give
the normal ADT representation of a list, and a corresponding Church encoding.</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">data</span> <span class="kt">List</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">Nil</span> <span class="o">|</span> <span class="kt">Cons</span> <span class="n">a</span> <span class="p">(</span><span class="kt">List</span> <span class="n">a</span><span class="p">)</span>

<span class="kr">newtype</span> <span class="kt">ListC</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">ListC</span> <span class="p">{</span><span class="n">unList</span> <span class="o">::</span> <span class="n">forall</span> <span class="n">r</span><span class="o">.</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">a</span> <span class="o">-&gt;</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">}</span>
</code></pre></div></div>

<p>Just like MaybeC, ListC is a funciton that takes a default value and another function. In the case
of an empty list, it returns the default value. To understand the recursive (non-empty) case it is
helpful to note the similarity between the type of ListC and the type of foldr on regular lists,
and then to see how cons is implemented for Church lists:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">foldr</span> <span class="o">::</span> <span class="p">(</span><span class="n">a</span> <span class="o">-&gt;</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="o">-&gt;</span> <span class="n">r</span>
</code></pre></div></div>

<p>In fact, the types of foldr and ListC are so similar because Church encoded lists are exactly
functions which fold over the lists they encode. To see how, observe the cons function given here:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">-- nil is a fold that does nothing but return the zero value supplied to it</span>
<span class="n">nil</span> <span class="o">::</span> <span class="kt">ListC</span> <span class="n">a</span>
<span class="n">nil</span> <span class="o">=</span> <span class="kt">ListC</span> <span class="o">$</span> <span class="nf">\</span><span class="n">z</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="n">z</span>

<span class="c1">-- cons take an element to add and a list, and build a new function which</span>
<span class="c1">-- folds over the new (longer) list</span>
<span class="n">cons</span> <span class="o">::</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">ListC</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">ListC</span> <span class="n">a</span>
<span class="n">cons</span> <span class="n">head</span> <span class="p">(</span><span class="kt">ListC</span> <span class="n">tail</span><span class="p">)</span> <span class="o">=</span> <span class="kt">ListC</span> <span class="o">$</span> <span class="nf">\</span><span class="n">z</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="n">head</span> <span class="p">`</span><span class="n">f</span><span class="p">`</span> <span class="p">(</span><span class="n">tail</span> <span class="n">z</span> <span class="n">f</span><span class="p">)</span>
</code></pre></div></div>

<p>To give a concrete example, let’s look at the list <code class="language-plaintext highlighter-rouge">[1, 2]</code> and use some equational reasoning to figure
out how its Church encoding works.</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">-- Ordinary representation, then with explicit (:) calls</span>
<span class="n">list</span> <span class="o">::</span> <span class="p">[</span><span class="kt">Int</span><span class="p">]</span>
<span class="n">list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
     <span class="c1">-- With explicit constructors:</span>
<span class="n">list</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">:</span> <span class="p">(</span><span class="mi">2</span> <span class="o">:</span> <span class="kt">[]</span><span class="p">)</span>

<span class="c1">-- Then the Church encoding, using substitution to expand the definitions</span>
<span class="c1">-- newtype wrapping and unwrapping removed for clarity</span>
<span class="n">church</span> <span class="o">::</span> <span class="kt">ListC</span> <span class="kt">Int</span>
<span class="n">church</span> <span class="o">=</span> <span class="mi">1</span> <span class="p">`</span><span class="n">cons</span><span class="p">`</span> <span class="p">(</span><span class="mi">2</span> <span class="p">`</span><span class="n">cons</span><span class="p">`</span> <span class="n">nil</span><span class="p">)</span>
       <span class="c1">-- using the definition of cons:</span>
       <span class="o">=</span> <span class="mi">1</span> <span class="p">`</span><span class="n">cons</span><span class="p">`</span> <span class="p">(</span><span class="nf">\</span><span class="n">z</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="mi">2</span> <span class="p">`</span><span class="n">f</span><span class="p">`</span> <span class="p">(</span><span class="n">nil</span> <span class="n">z</span> <span class="n">f</span><span class="p">))</span>
       <span class="c1">-- by def. of nil, with capture avoiding substitution:</span>
       <span class="o">=</span> <span class="mi">1</span> <span class="p">`</span><span class="n">cons</span><span class="p">`</span> <span class="p">(</span><span class="nf">\</span><span class="n">z</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="mi">2</span> <span class="p">`</span><span class="n">f</span><span class="p">`</span> <span class="p">((</span><span class="nf">\</span><span class="n">z'</span> <span class="n">f'</span> <span class="o">-&gt;</span> <span class="n">z'</span><span class="p">)</span> <span class="n">z</span> <span class="n">f</span><span class="p">))</span>
       <span class="c1">-- by applying the inner lambda:</span>
       <span class="o">=</span> <span class="mi">1</span> <span class="p">`</span><span class="n">cons</span><span class="p">`</span> <span class="p">(</span><span class="nf">\</span><span class="n">z</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="mi">2</span> <span class="p">`</span><span class="n">f</span><span class="p">`</span> <span class="n">z</span><span class="p">)</span>
       <span class="c1">-- by def. of cons:</span>
       <span class="o">=</span> <span class="nf">\</span><span class="n">z</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="mi">1</span> <span class="p">`</span><span class="n">f</span><span class="p">`</span> <span class="p">((</span><span class="nf">\</span><span class="n">z'</span> <span class="n">f'</span> <span class="o">-&gt;</span> <span class="mi">2</span> <span class="p">`</span><span class="n">f'</span><span class="p">`</span> <span class="n">z'</span><span class="p">)</span> <span class="n">z</span> <span class="n">f</span>
       <span class="c1">-- by applying the inner lambda:</span>
<span class="n">church</span> <span class="o">=</span> <span class="nf">\</span><span class="n">z</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="mi">1</span> <span class="p">`</span><span class="n">f</span><span class="p">`</span> <span class="p">(</span><span class="mi">2</span> <span class="p">`</span><span class="n">f</span><span class="p">`</span> <span class="n">z</span><span class="p">)</span>
</code></pre></div></div>

<p>As the last line shows, the Church encoded version is a function which folds over the list from the
right.</p>

<p>And we can observe the parallels between these constructors and the definition of the foldr function
for lists in <a href="https://hackage.haskell.org/package/base-4.8.1.0/docs/src/GHC.Base.html#foldr">the Prelude</a>:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">foldr</span> <span class="n">k</span> <span class="n">z</span> <span class="o">=</span> <span class="n">go</span>
        <span class="kr">where</span>
          <span class="n">go</span> <span class="kt">[]</span>     <span class="o">=</span> <span class="n">z</span>           <span class="c1">--nil</span>
          <span class="n">go</span> <span class="p">(</span><span class="n">y</span><span class="o">:</span><span class="n">ys</span><span class="p">)</span> <span class="o">=</span> <span class="n">y</span> <span class="p">`</span><span class="n">k</span><span class="p">`</span> <span class="n">go</span> <span class="n">ys</span> <span class="c1">--cons</span>
</code></pre></div></div>

<p>So if we want to define the Foldable instance for ListC, we need only define foldr as follows:</p>
<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">instance</span> <span class="kt">Foldable</span> <span class="kt">ListC</span> <span class="kr">where</span>
    <span class="n">foldr</span> <span class="n">f</span> <span class="n">z</span> <span class="p">(</span><span class="kt">ListC</span> <span class="n">l</span><span class="p">)</span> <span class="o">=</span> <span class="n">l</span> <span class="n">z</span> <span class="n">f</span>

<span class="c1">-- and a couple of useful instances follow from that directly:</span>

<span class="kr">instance</span> <span class="kt">Functor</span> <span class="kt">ListC</span> <span class="kr">where</span>
    <span class="n">fmap</span> <span class="n">f</span> <span class="o">=</span> <span class="n">foldr</span> <span class="p">(</span><span class="n">cons</span> <span class="o">.</span> <span class="n">f</span><span class="p">)</span> <span class="n">nil</span>

<span class="kr">instance</span> <span class="kt">Show</span> <span class="n">a</span> <span class="o">=&gt;</span> <span class="kt">Show</span> <span class="p">(</span><span class="kt">ListC</span> <span class="n">a</span><span class="p">)</span> 
    <span class="n">show</span> <span class="o">=</span> <span class="n">foldr</span> <span class="p">(</span><span class="nf">\</span><span class="n">head</span> <span class="n">tail</span> <span class="o">-&gt;</span> <span class="n">show</span> <span class="n">head</span> <span class="o">++</span> <span class="s">","</span> <span class="o">++</span> <span class="n">tail</span><span class="p">)</span> <span class="s">"End"</span>
</code></pre></div></div>

<h1 id="defining-nat">Defining Nat</h1>

<p>Having seen how ListC works to encode a recursive type, we can define our first primitive type
(rather than a type constructor like ListC or MaybeC), NatC. As usual, we’ll look at the (recursive)
ADT and the Church encoding side by side:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">data</span> <span class="kt">Nat</span> <span class="o">=</span> <span class="kt">Zero</span> <span class="o">|</span> <span class="kt">Succ</span> <span class="kt">Nat</span>

<span class="kr">newtype</span> <span class="kt">NatC</span> <span class="o">=</span> <span class="kt">NatC</span> <span class="p">{</span><span class="n">unNat</span> <span class="o">::</span> <span class="n">forall</span> <span class="n">r</span><span class="o">.</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">r</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">}</span>
</code></pre></div></div>

<p>This looks exactly like the ListC type signature, but missing the type that goes inside the list.
Looking at the constructors for NatC:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">zero</span> <span class="o">::</span> <span class="kt">NatC</span>
<span class="n">zero</span> <span class="o">=</span> <span class="kt">NatC</span> <span class="o">$</span> <span class="nf">\</span><span class="n">x</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="n">x</span>

<span class="n">succ</span> <span class="o">::</span> <span class="kt">NatC</span> <span class="o">-&gt;</span> <span class="kt">NatC</span>
<span class="n">succ</span> <span class="p">(</span><span class="kt">NatC</span> <span class="n">n</span><span class="p">)</span> <span class="o">=</span> <span class="kt">NatC</span> <span class="o">$</span> <span class="nf">\</span><span class="n">x</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="n">f</span> <span class="p">(</span><span class="n">n</span> <span class="n">x</span> <span class="n">f</span><span class="p">)</span> 
</code></pre></div></div>

<p>So <code class="language-plaintext highlighter-rouge">zero</code> applies the function f to x zero times, <code class="language-plaintext highlighter-rouge">succ zero</code> applies the function f to x one time,
and so on. This can be directly shown be equational reasoning similar to the example of the <code class="language-plaintext highlighter-rouge">cons</code>
function above.</p>

<p>To define the sum of two Church numerals, use one to apply <code class="language-plaintext highlighter-rouge">succ</code> to the other repeatedly, as
follows:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="o">+</span><span class="p">)</span> <span class="o">::</span> <span class="kt">NatC</span> <span class="o">-&gt;</span> <span class="kt">NatC</span> <span class="o">-&gt;</span> <span class="kt">NatC</span>
<span class="p">(</span><span class="kt">NatC</span> <span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="n">m</span> <span class="o">=</span> <span class="n">n</span> <span class="n">m</span> <span class="n">succ</span>
</code></pre></div></div>

<p>And multiplication can be defined similarly, in terms of addition:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="o">*</span><span class="p">)</span> <span class="o">::</span> <span class="kt">NatC</span> <span class="o">-&gt;</span> <span class="kt">NatC</span> <span class="o">-&gt;</span> <span class="kt">NatC</span>
<span class="p">(</span><span class="kt">NatC</span> <span class="n">n</span><span class="p">)</span> <span class="o">*</span> <span class="n">m</span> <span class="o">=</span> <span class="n">n</span> <span class="n">zero</span> <span class="p">(</span><span class="o">+</span><span class="n">m</span><span class="p">)</span>
</code></pre></div></div>

<p>Which can both be used to give Nat instances of Monoid, as necessary.</p>

<p>One last note is the interesting parallel between ListC and NatC. Observe their types:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">newtype</span> <span class="kt">ListC</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">ListC</span> <span class="p">{</span><span class="n">unList</span> <span class="o">::</span> <span class="n">forall</span> <span class="n">r</span><span class="o">.</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">a</span> <span class="o">-&gt;</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">}</span>

<span class="kr">newtype</span> <span class="kt">NatC</span> <span class="o">=</span> <span class="kt">NatC</span> <span class="p">{</span><span class="n">unNat</span> <span class="o">::</span> <span class="n">forall</span> <span class="n">r</span><span class="o">.</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">r</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">}</span>
</code></pre></div></div>

<p>The only difference is that ListC has a type parameter <code class="language-plaintext highlighter-rouge">a</code> where NatC does not. Indeed NatC can be
defined in terms of ListC by stating:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">newtype</span> <span class="kt">NatC</span> <span class="o">=</span> <span class="kt">ListC</span> <span class="nb">()</span>
</code></pre></div></div>

<p>Where we can then reuse some of the machinery for ListC to define operations on NatC, using
concatenation of lists for addition, fmap and concatenation for multiplication, etc. Especially
challenging is the definition of <code class="language-plaintext highlighter-rouge">tail</code> for ListC or <code class="language-plaintext highlighter-rouge">pred</code> for NatC.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Last time we covered Haskells Maybe type encoded as a function. This post will introduce two recursively defined structures, lists and natural numbers. Here I give the normal ADT representation of a list, and a corresponding Church encoding.]]></summary></entry><entry><title type="html">Church-encoded datatypes in Haskell</title><link href="https://cbdumas.github.io/2015/12/19/church_encoding.html" rel="alternate" type="text/html" title="Church-encoded datatypes in Haskell" /><published>2015-12-19T00:00:00+00:00</published><updated>2015-12-19T00:00:00+00:00</updated><id>https://cbdumas.github.io/2015/12/19/church_encoding</id><content type="html" xml:base="https://cbdumas.github.io/2015/12/19/church_encoding.html"><![CDATA[<p>Expressions in  λ-calculus are very tersely defined. Per <a href="https://en.wikipedia.org/wiki/Lambda_calculus#Definition">Wikipedia</a>:</p>

<blockquote>
  <p>The set of lambda expressions, Λ, can be defined inductively:</p>

  <ol>
    <li>If x is a variable, then x ∈ Λ</li>
    <li>If x is a variable and M ∈ Λ, then (λx.M) ∈ Λ</li>
    <li>If M, N ∈ Λ, then (M N) ∈ Λ</li>
  </ol>
</blockquote>

<p>Practical programming languages almost always define data primitives <em>and</em> function primitives, but
lambda calculus provides only unnamed functions. The Church encoding is one way of defining data types
in lambda calculus, and this post will explore what it means to treat functions as data with the
Church encoding in Haskell.</p>

<p>The first Church encoded type that really clicked for me was Maybe. Starting with Maybe
is a bit of a cop out, because it’s only useful if you have some other data to wrap in it, but bear
with me!</p>

<p>Maybe is given here with its usual algebraic data type notation and its corresponding Church-encoding:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">-- As defined in the Prelude:</span>
<span class="kr">data</span> <span class="kt">Maybe</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">Nothing</span> <span class="o">|</span> <span class="kt">Just</span> <span class="n">a</span>

<span class="c1">-- And an equivalent type, represented as a function:</span>
<span class="kr">newtype</span> <span class="kt">MaybeC</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">MaybeC</span> <span class="p">{</span> <span class="n">runMaybe</span> <span class="o">::</span> <span class="n">forall</span> <span class="n">r</span><span class="o">.</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">a</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">r</span> <span class="p">}</span>
</code></pre></div></div>

<p>From the definition of Maybe we know that <code class="language-plaintext highlighter-rouge">Maybe a</code> is either wrapping an <code class="language-plaintext highlighter-rouge">a</code> or it’s nothing. Inspecting the type
of MaybeC the intuition is that it takes a function <code class="language-plaintext highlighter-rouge">f :: (a -&gt; r)</code> and a default value, and returns a value of <code class="language-plaintext highlighter-rouge">r</code>, 
either by applying <code class="language-plaintext highlighter-rouge">f</code> to the wrapped <code class="language-plaintext highlighter-rouge">a</code>, or returning the default. This is possible without knowledge of the type <code class="language-plaintext highlighter-rouge">r</code>,
 thus the universal quantification. The signature again, with the parts labelled:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">--                       output</span>
<span class="c1">--                 f       |</span>
<span class="c1">--      default    |       |</span>
<span class="cd">--        |        |       |</span>
<span class="n">forall</span> <span class="n">r</span><span class="o">.</span> <span class="n">r</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">a</span> <span class="o">-&gt;</span> <span class="n">r</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">r</span>
</code></pre></div></div>

<p>Translating this intuition into Haskell gives the data constructors:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">-- When we do have a value, apply the given function to it</span>
<span class="n">just</span> <span class="o">::</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">MaybeC</span> <span class="n">a</span>
<span class="n">just</span> <span class="n">val</span> <span class="o">=</span> <span class="kt">MaybeC</span> <span class="o">$</span> <span class="nf">\</span><span class="kr">_</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="n">f</span> <span class="n">val</span>

<span class="c1">-- In the case where we have no value to work with, return the default</span>
<span class="n">nothing</span> <span class="o">::</span> <span class="kt">MaybeC</span> <span class="n">a</span>
<span class="n">nothing</span> <span class="o">=</span> <span class="kt">MaybeC</span> <span class="o">$</span> <span class="nf">\</span><span class="n">x</span> <span class="kr">_</span> <span class="o">-&gt;</span> <span class="n">x</span>
</code></pre></div></div>

<p>And all of the usual instances for Maybe can be defined for MaybeC as well. For playing with this
type in GHCi it is especially useful to have an instance of Show (it is also useful to remember to
enable RankNTypes to get it to compile). Instances for Show and Functor can be defined
as follows:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">instance</span> <span class="kt">Show</span> <span class="n">a</span> <span class="o">=&gt;</span> <span class="kt">Show</span> <span class="p">(</span><span class="kt">MaybeC</span> <span class="n">a</span><span class="p">)</span> <span class="kr">where</span>
    <span class="n">show</span> <span class="p">(</span><span class="kt">MaybeC</span> <span class="n">m</span><span class="p">)</span> <span class="o">=</span> <span class="n">m</span> <span class="s">"Empty"</span> <span class="p">(</span><span class="nf">\</span><span class="n">x</span> <span class="o">-&gt;</span> <span class="s">"Contains: "</span> <span class="o">++</span> <span class="n">show</span> <span class="n">x</span><span class="p">)</span>

<span class="kr">instance</span> <span class="kt">Functor</span> <span class="kt">MaybeC</span> <span class="kr">where</span>
    <span class="n">fmap</span> <span class="n">f</span> <span class="p">(</span><span class="kt">MaybeC</span> <span class="n">m</span><span class="p">)</span> <span class="o">=</span> <span class="n">m</span> <span class="n">nothing</span> <span class="p">(</span><span class="n">just</span> <span class="o">.</span> <span class="n">f</span><span class="p">)</span>
</code></pre></div></div>

<p>The Show instance provides a good example of how to use a MaybeC value by passing in a
function and a default value. Here the default is the string “Empty”, and the the function prints
the value insdie the MaybeC otherwise.</p>

<p>In order to prove that Maybe and MaybeC are ismorphic, we first write the functions that map
between them:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">churchToMaybe</span> <span class="o">::</span> <span class="kt">MaybeC</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Maybe</span> <span class="n">a</span>
<span class="n">churchToMaybe</span> <span class="p">(</span><span class="kt">MaybeC</span> <span class="n">m</span><span class="p">)</span> <span class="o">=</span> <span class="n">m</span> <span class="kt">Nothing</span> <span class="kt">Just</span>

<span class="c1">-- Going in the other direction, note that this function can also be</span>
<span class="c1">-- written by slightly rearranging the arguments to `maybe` in Data.Maybe</span>
<span class="n">maybeToChurch</span> <span class="o">::</span> <span class="kt">Maybe</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">MaybeC</span> <span class="n">a</span>
<span class="n">maybeToChurch</span> <span class="kt">Nothing</span> <span class="o">=</span> <span class="n">nothing</span>
<span class="n">maybeToChurch</span> <span class="p">(</span><span class="kt">Just</span> <span class="n">x</span><span class="p">)</span> <span class="o">=</span> <span class="n">just</span> <span class="n">x</span>
</code></pre></div></div>

<p>Isomorphism further requires that those functions are mutually inverse. That is:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">churchToMaybe</span> <span class="o">.</span> <span class="n">maybeToChurch</span> <span class="o">=</span> <span class="n">id</span>
<span class="c1">-- AND that</span>
<span class="n">maybeToChurch</span> <span class="o">.</span> <span class="n">churchToMaybe</span> <span class="o">=</span> <span class="n">id</span>
</code></pre></div></div>

<p>Which can be proved by equational reasoning, but I’ll not write that out here. The next installment
will introduce the Church numerals, and Church encoded lists, and then we can really start to use
Church encoded data for something interesting!</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Expressions in λ-calculus are very tersely defined. Per Wikipedia:]]></summary></entry></feed>