<?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://abhiroop.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://abhiroop.github.io/" rel="alternate" type="text/html" /><updated>2026-02-08T00:41:57+00:00</updated><id>https://abhiroop.github.io/feed.xml</id><title type="html">Abhiroop Sarkar</title><subtitle>λ | Computation</subtitle><entry><title type="html">Remember why you started</title><link href="https://abhiroop.github.io/remember/" rel="alternate" type="text/html" title="Remember why you started" /><published>2025-07-09T00:00:00+00:00</published><updated>2025-07-09T00:00:00+00:00</updated><id>https://abhiroop.github.io/remember</id><content type="html" xml:base="https://abhiroop.github.io/remember/"><![CDATA[<blockquote>
  <p>I think that it’s extraordinarily important that we in computer science keep fun in computing. When it started out, it was an awful lot of fun. Of course, the paying customers got shafted every now and then, and after a while we began to take their complaints seriously. We began to feel as if we really were responsible for the successful, error-free perfect use of these machines. I don’t think we are. <strong>I think we’re responsible for stretching them, setting them off in new directions, and keeping fun in the house. I hope the field of computer science never loses its sense of fun.</strong> Above all, I hope we don’t become missionaries. Don’t feel as if you’re Bible salesmen. The world has too many of those already. What you know about computing other people will learn. Don’t feel as if the key to successful computing is only in your hands. What’s in your hands, I think and hope, is intelligence: the ability to see the machine as more than when you were first led up to it, that you can make it more.</p>
</blockquote>

<p>—Alan J. Perlis (April 1, 1922 – February 7, 1990)</p>

<p>From the “Dedication” paragraph of the legendary <em>Structure and Interpretation of Computer Programs</em> book, read 8 years ago!</p>]]></content><author><name></name></author><summary type="html"><![CDATA[I think that it’s extraordinarily important that we in computer science keep fun in computing. When it started out, it was an awful lot of fun. Of course, the paying customers got shafted every now and then, and after a while we began to take their complaints seriously. We began to feel as if we really were responsible for the successful, error-free perfect use of these machines. I don’t think we are. I think we’re responsible for stretching them, setting them off in new directions, and keeping fun in the house. I hope the field of computer science never loses its sense of fun. Above all, I hope we don’t become missionaries. Don’t feel as if you’re Bible salesmen. The world has too many of those already. What you know about computing other people will learn. Don’t feel as if the key to successful computing is only in your hands. What’s in your hands, I think and hope, is intelligence: the ability to see the machine as more than when you were first led up to it, that you can make it more.]]></summary></entry><entry><title type="html">On Equality</title><link href="https://abhiroop.github.io/Equality/" rel="alternate" type="text/html" title="On Equality" /><published>2025-03-19T00:00:00+00:00</published><updated>2025-03-19T00:00:00+00:00</updated><id>https://abhiroop.github.io/Equality</id><content type="html" xml:base="https://abhiroop.github.io/Equality/"><![CDATA[<p>One of the most fundamental notions in type theory is equality. There are various forms of equality, including <a href="https://ncatlab.org/nlab/show/equality#propositional_equality">propositional equality</a>, <a href="https://ncatlab.org/nlab/show/equality#judgmental_equality">judgmental equality</a>, <a href="https://ncatlab.org/nlab/show/equality#typal_equality">typal equality</a>, <a href="https://en.wikipedia.org/wiki/Extensionality">extensional equality</a>, <a href="https://en.wikipedia.org/wiki/Intension">intensional equality</a>, <a href="https://en.wikipedia.org/wiki/Homotopy_type_theory#Equality">path equality</a> (a key concept in homotopy type theory), among others. The list goes on, and any deep exploration of the concept of equality could easily expand to book length. Instead of attempting to cover all these notions at once, I believe it’s best to build this blog post incrementally.</p>

<h2 id="martin-löf-equality">Martin-Löf Equality</h2>

<p>The Swedish logician <a href="https://en.wikipedia.org/wiki/Per_Martin-L%C3%B6f">Per Martin-Löf</a> pretty much introduced<sup><a href="#fn1" id="ref1">1</a></sup> the notion of <a href="https://en.wikipedia.org/wiki/Dependent_type"><em>dependent typing</em></a> (eponymously known as Martin-Löf type theory) to the world. Every theorem prover/proof assistant in existence owes its origin to Martin-Löf’s seminal work.</p>

<div style="text-align: center;">
    <img src="/images/per.jpg" alt="Per Martin-Löf" width="320" height="400" />
</div>

<p>A key concept in his type theory is what we call <em>Martin-Löf equality</em>, a dependent type that takes a fixed term <code class="language-plaintext highlighter-rouge">x : A</code> and maps any term <code class="language-plaintext highlighter-rouge">y : A</code> to the type <code class="language-plaintext highlighter-rouge">x ≡ y</code>. I will be using Agda in this blog post. The equality definition in Agda looks like the following:</p>

<pre><code class="language-agda">data _≡_ {A : Set} (x : A) : A → Set where
  refl : x ≡ x
</code></pre>

<p>The <code class="language-plaintext highlighter-rouge">{A : Set}</code> is a type parameter, meaning the equality type is polymorphic over any type <code class="language-plaintext highlighter-rouge">A</code> in the universe <code class="language-plaintext highlighter-rouge">Set</code>. <code class="language-plaintext highlighter-rouge">refl : x ≡ x</code> is the sole constructor of the equality type. It states that the only way to construct a proof of equality is by showing that a term is equal to itself (reflexivity).</p>

<h2 id="leibniz-equality">Leibniz Equality</h2>

<p>One of the earliest notions of equality was given by the philosopher and mathematician <a href="https://en.wikipedia.org/wiki/Gottfried_Wilhelm_Leibniz">Gottfried Wilhelm Leibniz</a>. Leibniz in his 1686 treatise<sup><a href="#fn2" id="ref2">2</a></sup>, <em>Discourse on Metaphysics</em>, asserted the identity of indiscernibles - <em>two objects are equal if and only if they satisfy the same properties</em>.</p>

<div style="text-align: center;">
    <img src="/images/Leibniz.jpg" alt="Leibniz" width="320" height="400" />
</div>

<p>The above is known as Leibniz Equality. Leibniz equality is usually formalised to state that <code class="language-plaintext highlighter-rouge">x ≐ y</code> holds if every property <code class="language-plaintext highlighter-rouge">P</code> that holds of x also holds of <code class="language-plaintext highlighter-rouge">y</code>. Or,</p>

<pre><code class="language-agda">_≐_ : ∀ {A : Set} (x y : A) → Set₁
_≐_ {A} x y = ∀ (P : A → Set) → P x → P y
</code></pre>

<p>Note the use of <code class="language-plaintext highlighter-rouge">Set₁</code>, which represents a hierarchy of types where <code class="language-plaintext highlighter-rouge">Set : Set₁</code>, <code class="language-plaintext highlighter-rouge">Set₁ : Set₂</code>, etc to bypass <a href="https://en.wikipedia.org/wiki/Russell%27s_paradox">Russell’s Paradox</a> and <a href="https://en.wikipedia.org/wiki/System_U#Girard's_paradox">Girard’s Paradox</a>. A <a href="https://homepages.inf.ed.ac.uk/wadler/papers/leibniz/leibniz.pdf">recently published work</a> showed that Leibniz Equality is isomorphic to Martin-Löf Equality. The fact that Martin-Löf equality implies Leibniz equality, and vice versa is short enough to be shown here as follows:</p>

<pre><code class="language-agda">≡-implies-≐ : ∀ {A : Set} {x y : A}
  → x ≡ y
    -----
  → x ≐ y
≡-implies-≐ x≡y = λ P Px → subst P x≡y Px

≐-implies-≡ : ∀ {A : Set} {x y : A}
  → x ≐ y
    -----
  → x ≡ y
≐-implies-≡ {A} {x} {y} x≐y = Qy
  where
    Q : A → Set
    Q z = x ≡ z
    Qx : Q x
    Qx = refl
    Qy : Q y
    Qy = x≐y Q Qx
</code></pre>

<h4 id="footnotes">Footnotes</h4>
<ol>
  <li id="fn1"> Work by <a href="https://en.wikipedia.org/wiki/William_Alvin_Howard">Howard</a> and <a href="https://en.wikipedia.org/wiki/Nicolaas_Govert_de_Bruijn">de Bruijn</a> predates Martin-Löf's work, however, Martin-Löf is credited with formalising dependent types into a coherent framework for constructive mathematics <a href="#ref1">↩</a></li>
  <li id="fn2"> A work from 1686 is by far the oldest paper that I have referred to in my academic career  <a href="#ref2">↩</a></li>
</ol>]]></content><author><name></name></author><summary type="html"><![CDATA[One of the most fundamental notions in type theory is equality. There are various forms of equality, including propositional equality, judgmental equality, typal equality, extensional equality, intensional equality, path equality (a key concept in homotopy type theory), among others. The list goes on, and any deep exploration of the concept of equality could easily expand to book length. Instead of attempting to cover all these notions at once, I believe it’s best to build this blog post incrementally.]]></summary></entry><entry><title type="html">20 Films That Inspire, Teach and Move</title><link href="https://abhiroop.github.io/Films/" rel="alternate" type="text/html" title="20 Films That Inspire, Teach and Move" /><published>2025-02-09T00:00:00+00:00</published><updated>2025-02-09T00:00:00+00:00</updated><id>https://abhiroop.github.io/Films</id><content type="html" xml:base="https://abhiroop.github.io/Films/"><![CDATA[<p>Inspired by Satyajit Ray’s <a href="https://www.goodreads.com/book/show/13385130-deep-focus">Deep Focus</a>—a compulsory reading at FTII Pune and NSD Delhi—I was struck by the depth and nuance with which Ray analyses his favourite films and directors. This led me to finally compile a list of my own 10 (+10) favourite films. However, I soon realised that rather than ranking them purely on cinematic merit, my choices were more personal—a reflection of the life lessons they offer.</p>

<p>Some of my favourite films, like Pulp Fiction or any Christopher Nolan film, don’t necessarily impart a life lesson—nor is it the director’s duty to teach one. But in the end, I felt compelled to share this list anyway. Below, I’ve written a few lines about what drew me to these works. To the reader, I can assure you that each of these films will be a moving experience if you take the time to watch them—and I hope you enjoy them as much as I did!</p>

<h3 id="1-12-angry-men-usa">1. 12 Angry Men (USA)</h3>

<p><img src="/images/12_Angry_Men.jpg" alt="12 Angry Men" width="300" height="500" /></p>

<p>Surprisingly, the easiest pick for me was number one. From the moment I began this list, I knew that the 1957 film - 12 Angry Men undeniably belonged atleast in the top three. The film casts a sharp eye on the justice system, exploring its fragility and biases.</p>

<p>A phrase that originated in the legal system and has since become a mainstay in everyday conversation—”<em>beyond reasonable doubt</em>“—is central to the film. 12 Angry Men takes the concept of “reasonable doubt” and turns it on its head, exposing how deeply personal prejudices shape our judgment, often influenced by the social strata we belong to. It challenges us to question our assumptions and remain wary of absolute truths in society.</p>

<p>At its core, the film is also a sobering reflection on how our justice system hangs by a thread—a thread held by the jury. Their decisions, often swayed by personal moods or circumstances, can mean the difference between life and death for a defendant.</p>

<p><strong>Keywords: Reasonable Doubt, Truth, Law, Justice, Ethics</strong></p>

<h3 id="2-incendies-canada">2. Incendies (Canada)</h3>

<p><img src="/images/Incendies.jpg" alt="Incendies" width="300" height="500" /></p>

<p>To this day, the climax of Incendies gives me goosebumps. Based on Wajdi Mouawad’s play of the same name, this screen adaptation weaves a non-linear narrative around the Lebanese Civil War of 1975 and its profound impact on Nawal, an Arab immigrant in Canada.</p>

<p>As Nawal’s children uncover her past, piecing together the fragments of her life, we come face to face with the harsh realities of war—the suffering of ordinary people, the state of women in a war-torn society, honor killings, and other deeply powerful themes. The film has often been compared to Greek tragedies, and its climax remains one of my all-time favorites. It is the kind of ending that compels you to sit in silence, reflecting on what you have just witnessed.</p>

<p><strong>Keyword: Anti-war</strong></p>

<h3 id="3-aamis-india">3. Aamis (India)</h3>

<p><img src="/images/Aamis.jpg" alt="Aamis" width="300" height="500" /></p>

<p>Aamis (English: Ravening) is probably the only non-commercial, slightly artsy film on this list. This ingenious hidden gem takes a seemingly simple concept—the food we eat—something innocuous and uncontroversial, and pushes it to an extreme that challenges our fundamental beliefs. To what degree do we pursue hedonism without realizing how far we are pushing the boundaries of what we consider normal?</p>

<p>At the same time, Aamis is one of the few Indian films that shines a light on the often-overlooked region of Northeast India and its food traditions. However, does its imagery reinforce binary stereotypes? I came across this critique in a <a href="https://www.tandfonline.com/doi/abs/10.1080/14746689.2022.2089462?tab=permissions&amp;scroll=top">social-theory paper on Aamis</a>. Regardless, this film is so thought-provoking that it will undoubtedly leave you reflecting on your own belief systems.</p>

<p><strong>Keywords: Food Habits, Belief system, Hedonism, Prejudice</strong></p>

<h3 id="4-whiplash-usa">4. Whiplash (USA)</h3>

<p><img src="/images/Whiplash.jpg" alt="Whiplash" width="300" height="500" /></p>

<p>Whiplash explores a complex and fascinating topic—the relentless pursuit of perfection and the lengths one will go to achieve it. Set in a fictionalised version of the legendary Juilliard Music School, New York, the film follows a ruthless teacher (J.K. Simmons) who pushes his students to their limits in pursuit of excellence.</p>

<p>While this list focuses on broader themes rather than individual performances, Simmons gives a performance of a lifetime as a perfectionist jazz teacher. The film delivers a crucial message: perfection comes at a cost. It breaks you in ways unimaginable at the start, but the view from that mythical peak is one most of us can only dream of—and in the climax, the film gives us a small taste of it.</p>

<p><strong>Keywords: Perfection, Excellence, Toxic Relations, Teacher</strong></p>

<h3 id="5-princess-mononoke-japan">5. Princess Mononoke (Japan)</h3>

<p><img src="/images/Mononoke.jpg" alt="Mononoke" width="400" height="500" /></p>

<p>And finally, we arrive at a Studio Ghibli film. Miyazaki’s movies are literal works of art, with every frame painstakingly hand-drawn by animators. Princess Mononoke stands as a testament to Hollywood and the world at large on the power of strong female leads and leadership. The film evokes nostalgia for pre-industrial Japan—a time of harmony between people and nature—while serving as a stark reminder of how the relentless march of industrialisation has left us increasingly desolate and disconnected.</p>

<p>Beyond the eponymous Princess Mononoke, the character of Lady Eboshi is perhaps my favourite grey character in any film. She is strong and independent, offering refuge to society’s outcasts—former prostitutes and people with leprosy—yet she is also the ruthless leader of Irontown. Her character serves as a reminder of the difficult balance a just leader must strike in times of conflict.</p>

<p>Every scene in this film feels like a stroll through an art gallery—an absolute masterpiece and a true delight to watch!</p>

<p><strong>Keywords: Nature, Greed, Pacifism, Industrialisation</strong></p>

<h3 id="6-mother-korea">6. Mother (Korea)</h3>

<p><img src="/images/Mother.jpg" alt="Mother" width="325" height="500" /></p>

<p>We arrive at Bong Joon-ho, who, in my opinion, is one of the greatest living directors. Mother is an oft-overlooked masterpiece that I personally consider his best. It is also one of his more complex films, layered with multiple underlying themes. At its core, it portrays a disconnected society—one where the murdered body of a young girl lies in broad daylight, yet, because of her social status, no one looks closely or intervenes.</p>

<p>As the title suggests, the film is ultimately about a mother’s love—the lengths to which she will go to protect and care for her child. It also sheds light on the bullying and mistreatment children endure in their daily lives. However, in classic Bong Joon-ho fashion, these plot threads are twisted in unexpected ways, culminating in a devastating climax.</p>

<p>By the end, you are left conflicted, confronting one of my favourite themes: what is truth? Are we truly seeing it, or are we merely looking through a blurred window?</p>

<p><strong>Keywords: Truth, Mother, Disconnected society, Single Parenthood, Blind Love</strong></p>

<h3 id="7-spirited-away-japan">7. Spirited Away (Japan)</h3>

<p><img src="/images/SpiritedAway.jpg" alt="Spirited Away" width="350" height="500" /></p>

<p>Widely regarded as Miyazaki’s masterpiece and the film that introduced many Western audiences to the magic of Studio Ghibli, Spirited Away is a work of unparalleled beauty. Its themes are too numerous to outline fully, but for me, the most striking is the innocence of childhood. Even today, when I watch Spirited Away, I find myself connecting with Chihiro—her mixture of awe and fear as she navigates an unfamiliar world resonates deeply.</p>

<p>The film powerfully explores separation anxiety and the pain of losing loved ones. In fact, the more I reflect on it, Spirited Away feels like a modern existentialist work. It aligns with Sartre’s notion that “existence precedes essence”—a lifelong struggle to define who we are. Yet, unlike the bleak conclusions of some French existentialists, Miyazaki offers a more hopeful vision. He shows that friendship, courage, and kindness can help us navigate even the most unfathomable worlds. Above all, the film reminds us of the importance of treating every life form with compassion.</p>

<p>If you haven’t yet watched Spirited Away, I can only say—I envy you. You have the rare joy of discovering this masterpiece for the first time. And of course, I can’t resist sharing the iconic train scene:</p>

<p><img src="/images/spirited_away_2.jpg" alt="Spirited Away" width="500" height="300" /></p>

<p><strong>Keywords: Separation Anxiety, Alienation, Bravery, Valiance, Belief in Oneself</strong></p>

<h3 id="8-up-usa">8. Up (USA)</h3>

<p><img src="/images/Up.jpg" alt="Up" width="300" height="500" /></p>

<p>Of course, no list of moving films would be complete without a Pixar entry. Among Pixar’s many inspiring works, I personally consider Up the best (sorry, Wall-E and others!).</p>

<p>Although marketed as a children’s film, Up tackles profound and often painful themes right from the start. Within its opening moments, it addresses the struggles of a couple facing fertility challenges, followed shortly by the devastating loss of a partner and the heartbreak that follows. The short montage Married Life has already achieved cult status as a cinematic masterpiece—and incredibly, this is just the beginning of the film!</p>

<p>The rest of Up lives up to that emotional depth, exploring the disillusionment that comes from meeting our childhood heroes and realising their fallibility. Along the way, the characters, through kindness, loyalty, courage, and wit, learn one of life’s most powerful lessons: the importance of letting go. The film delivers one of the most poignant depictions of this theme, reminding us that it is never too late to embrace new adventures. It brings to mind the saying, “You can never cross the ocean until you have the courage to lose sight of the shore.”</p>

<p><strong>Keywords: Letting go, Kindness, Loyalty, It is never too late, Dont meet your heroes</strong></p>

<h3 id="9-three-billboards-outside-ebbing-missourie-usa">9. Three Billboards Outside Ebbing Missourie (USA)</h3>

<p><img src="/images/3Billboards.jpg" alt="3 Billboards" width="300" height="500" /></p>

<p>Three Billboards Outside Ebbing, Missouri powerfully explores the theme of navigating rage. At its core, the film follows two unlikely companions on a shared journey—Frances McDormand’s and Sam Rockwell’s characters—both grappling with the loss of loved ones. One has lost her daughter, while the other lost his father at a young age. The film subtly reveals how these tragedies have transformed them into figures consumed by anger, resentful of the unfair hand they believe the world has dealt them. Yet, over time, they both come to understand the profound power of forgiveness and the peace it can bring.</p>

<p>Sam Rockwell’s character arc is one of the most compelling anti-hero transformations I’ve ever seen. A particularly moving moment is the series of letters from Chief Willoughby, in which he asks for forgiveness—not just for himself, but as an appeal for others to embrace forgiveness and find inner peace.</p>

<p>Another central theme is the elusive nature of justice. As Willoughby profoundly reflects in one of his letters: “What you need to become a detective is love. Because through love comes calm, and through calm comes thought. And you need thought to detect stuff sometimes…” With stellar performances and a powerful narrative, Three Billboards Outside Ebbing, Missouri stays with you long after the credits roll.</p>

<p><strong>Keywords: Forgiveness, Love, Kindness, Justice, Rage</strong></p>

<h3 id="10-the-wailing-korea">10. The Wailing (Korea)</h3>

<p><img src="/images/The_Wailing.jpg" alt="The Wailing" width="300" height="500" /></p>

<p>Another Korean masterpiece, The Wailing evokes horror without relying on jump scares or other traditional horror tropes. It is a richly layered film, filled with subliminal messages so intricate that capturing them all is nearly impossible. Themes such as the generational divide, the struggle between tradition and modernity, xenophobia, and the tension between Christianity and traditional Korean and Japanese shamanism run deep throughout the story.</p>

<p>At its core, the film explores how mistrust within an isolated community—fueled by prejudice—can lead to societal collapse, ultimately allowing a symbolic <em>Satan</em> to take hold. Even years after its release, audiences continue to analyse its symbolism, debate its ambiguous climax, and grapple with the question of who is truly good or evil. I highly recommend checking out multiple analyses of the film—it’s a mystery that keeps unfolding with every rewatch.</p>

<p><strong>Keywords: Superstition, Mistrust</strong></p>

<h3 id="11-tumbbad-india">11. Tumbbad (India)</h3>

<p><img src="/images/Tumbbad.jpg" alt="Tumbbad" width="325" height="500" /></p>

<p>In a single sentence, Tumbbad is the ultimate personification of greed. And in 2025, as we witness those in power shaping policies under the guise of good intentions and the greater good, Tumbbad lays bare what truly lurks beneath the surface. It’s unlikely that any follow-up film could so vividly capture and embody the essence of greed and its transformative grip on a person.</p>

<p><strong>Keyword: Greed</strong></p>

<h3 id="12-parasite-korea">12. Parasite (Korea)</h3>

<p><img src="/images/Parasite.jpg" alt="Parasite" width="325" height="500" /></p>

<p>The first and only non-English film to win an Oscar for Best Picture, Parasite is perhaps Bong Joon-Ho’s most well-known film in recent years. Much has already been said and written about this masterpiece, where nearly every scene serves as a symbol of economic disparity between the wealthy and the working class in Korea. For one family, the rain is a beautiful spectacle; for another, it brings devastation as they struggle to save their home. The film masterfully portrays this divide through its literal uptown-downtown contrast. Parasite is probably the most commercial and easily accessible film on this list.</p>

<p><strong>Keywords: Class Struggle, Inequality</strong></p>

<h3 id="13-swades-india">13. Swades (India)</h3>

<p><img src="/images/Swades.jpg" alt="Swades" width="325" height="500" /></p>

<p>Swades is the most personal film on this list. I don’t think I can do justice to everything it represents in just a few lines—maybe a full blog post or even an academic paper would be more fitting. Beyond being an emotional journey into the heartland of India, Swades is also a cinematically brilliant film.</p>

<p>Mohan (an allusion to Mohandas Karamchand Gandhi) and Kaveri Ammi (symbolic of the river Cauvery) have long been separated, yet his inner guilt and sense of duty pull him back to her village. Most intriguingly, the film presents two versions of Mohan. When he first arrives, he is not the typical caricature of an NRI disconnected from India. He knows the country well—its Constitution, its major rivers, its politics, its corruption, its strengths, and its flaws. I personally related the most to this version of him. But then something happens, and we see a transformation. This time, Mohan doesn’t just <em>know</em>—he <em>understands</em>. He witnesses firsthand the inequality and systemic casteism tearing India apart and realises that to make a difference, he must be much more than who he was when he arrived.</p>

<p>It’s the difference between knowledge and wisdom, much like the one Hermann Hesse speaks about in <a href="https://www.goodreads.com/book/show/52036.Siddhartha">Siddhartha</a>. I highly recommend watching this film—but preferably under my supervision, so I can relentlessly comment on every frame and dissect every hidden meaning along the way :) .</p>

<p><strong>Keywords: India, Caste system, Inequality, <a href="https://en.wikipedia.org/wiki/Gandhism">Gandhism</a>, Connecting to your roots</strong></p>

<h3 id="14-the-shawshank-redemption-usa">14. The Shawshank Redemption (USA)</h3>

<p><img src="/images/Shawshank.jpg" alt="Shawshank" width="300" height="500" /></p>

<p><img src="/images/Shawshank2.jpg" alt="Shawshank2" width="500" height="300" /></p>

<p>Thank you, Stephen King!</p>

<p><strong>Keyword: Hope</strong></p>

<h3 id="15-agantuk-india">15. Agantuk (India)</h3>

<p><img src="/images/Agantuk.jpg" alt="Agantuk" width="325" height="500" /></p>

<p>We arrive at the maestro—Satyajit Ray. Agantuk (English: The Stranger) is set in 1991 Bengal, a pivotal year marked by India’s liberalisation. The film portrays a middle-class nuclear family grappling with distrust when a mysterious stranger arrives at their doorstep, claiming to be a long-lost relative. However, this stranger unsettles their deeply held beliefs, challenging the very foundations upon which their society stands.</p>

<p>Through thought-provoking discussions, the film raises questions about the Western definition of “civilisation.” The uncle expresses regret that, shaped by Marx, Freud, and even Rabindranath Tagore, he has become entangled in the pursuit of being “civilised” while fearing the Western construct of the “savage”. His love for indigenous cultures and his fear of becoming a kupomanduk (a frog in a well) are understood only by the young protégé—a reflection of Ray’s brilliance in capturing the depth and sensitivity of children on screen.</p>

<p>Agantuk is Ray’s final film, and while his later works are often considered weaker due to their emphasis on dialogue over imagery, I believe this is one of the most accessible entry points into his cinematic world. I leave you with <a href="https://www.youtube.com/watch?v=XhGlyaNCSqU">this iconic clip from Agantuk on civilisation.</a></p>

<p><strong>Keywords: Identity, Materialism, Civilisation</strong></p>

<h3 id="16-howls-moving-castle-japan">16. Howl’s Moving Castle (Japan)</h3>

<p><img src="/images/Howl.jpg" alt="Howl" width="325" height="500" /></p>

<p>The third Miyazaki film on this list, Howl’s Moving Castle, primarily explores themes of vanity and outward appearance. It reflects our tendency to judge a book by its cover, yet in Howl’s, nothing is as it seems—old people appear young, young people appear old, a prince takes the form of a scarecrow, and unlikely heroes seem evil. As in many of Miyazaki’s works, the film features a strong female lead who, with quiet confidence, authenticity, and kindness, remains unfazed by outward appearances. Instead of being deceived by illusions, she embarks on a journey to help both herself and those around her. Beneath its fantasy elements, the movie delivers a profound message about self-awareness and the power of staying true to oneself.</p>

<p><strong>Keywords: Idenity, Vanity, Pacifism, Authenticity</strong></p>

<h3 id="17-coco-usa">17. Coco (USA)</h3>

<p><img src="/images/Coco.jpg" alt="Coco" width="325" height="500" /></p>

<p>As C.S. Lewis put it, “A children’s story that can only be enjoyed by children is not a good children’s story.” In that spirit, Coco, though more of a children’s film than Up, is one that any adult can appreciate. The overarching theme is “<em>Don’t meet your heroes</em>”, as those we idolise for their perfection and infallibility may, in reality, be deeply flawed and more susceptible to corruption than we ever imagined. Along the way, the film offers a breathtaking visual spectacle that makes Coco an unforgettable experience.</p>

<p><strong>Keywords: DONT MEET YOUR HEROES!</strong></p>

<h3 id="18-the-iron-giant-usa">18. The Iron Giant (USA)</h3>

<p><img src="/images/iron_giant.jpg" alt="Iron Giant" width="400" height="500" /></p>

<p>Another children’s film, The Iron Giant, explores our fear of the unknown—a theme reminiscent of the <em>AI taking over</em> trope. Set during the Cold War, with tensions between the U.S. and the Soviet Union at their peak, the film follows the eponymous giant, a being created as a weapon. Yet, through the kindness of a young boy, he learns that we are not bound by our origins—that we can choose who we become, no matter how we were made.</p>

<p><strong>Keywords: Kindness, Fear of Unknown</strong></p>

<h3 id="19-sairat-india">19. Sairat (India)</h3>

<p><img src="/images/Sairat.jpg" alt="Sairat" width="325" height="500" /></p>

<p>Please avoid the many remakes and stick to the original 2016 Marathi film, Sairat. Director Nagraj Manjule casts a revealing eye on the regressive laws and institutionalised norms of the caste system that shape the lives of men and women in cross-caste relationships. Watching Sairat felt as though the director had captured an entire TV series within the film’s runtime. If you decide to watch it, I highly recommend going in without reading any detailed analyses. Since seeing this film, I have become an ardent admirer of Manjule’s work, and I leave you with <a href="https://www.youtube.com/watch?v=JyFjpiqK3ho&amp;t=1375s">this clip where he delivers a stellar response to why he isn’t making movies like Godzilla or King Kong in India.</a></p>

<p><strong>Keywords: Class Struggle, Caste System</strong></p>

<h3 id="20-rang-de-basanti-india">20. Rang De Basanti (India)</h3>

<p><img src="/images/rdb.jpg" alt="Sairat" width="300" height="500" /></p>

<p>Lastly, I conclude my list with Rang De Basanti, a once-in-a-generation film that challenged both millennials and the generations before them in their perception of a commercial Hindi movie with a message. Rang De Basanti features one of the best anti-hero character arcs in recent memory—Laxman Pandey, whose steadfast belief in the ideals of Hindutva turns to heartbreak as he sees the hollow shell of the movement and what it truly stands for, followed by his rebellion for what he believes is a just cause. One must also note the ingenious device of comparing each character’s arc with their corresponding counterpart from the Indian freedom struggle.</p>

<p>The film addresses the generational divide about the teenagers and the twenty somethings during the freedom revolution of India, how they thought and worked and how disconnected the youth today is from the youth then. But somewhere when an oppressing force casts us and lays bare our society with corruption, the youth have the fire within them to stand up and realise something which they didnt know lives within them. As written by Prasoon Joshi “अभी-अभी हुआ यक़ीन की आग है मुझमें कही” (Translation: “Only now did I believe that there lives a fire inside me somewhere”)</p>

<p><strong>Keywords: Youth, Rebellion</strong></p>]]></content><author><name></name></author><summary type="html"><![CDATA[Inspired by Satyajit Ray’s Deep Focus—a compulsory reading at FTII Pune and NSD Delhi—I was struck by the depth and nuance with which Ray analyses his favourite films and directors. This led me to finally compile a list of my own 10 (+10) favourite films. However, I soon realised that rather than ranking them purely on cinematic merit, my choices were more personal—a reflection of the life lessons they offer.]]></summary></entry><entry><title type="html">Square Limit by M.C. Escher</title><link href="https://abhiroop.github.io/Square-Limit/" rel="alternate" type="text/html" title="Square Limit by M.C. Escher" /><published>2024-11-04T00:00:00+00:00</published><updated>2024-11-04T00:00:00+00:00</updated><id>https://abhiroop.github.io/Square-Limit</id><content type="html" xml:base="https://abhiroop.github.io/Square-Limit/"><![CDATA[<p><img src="https://raw.githubusercontent.com/Abhiroop/Abhiroop.github.io/01b52606b3a75bd26574bc22cb36eb6d39383c35/art/sqlimit.svg" alt="Escher" /></p>

<p>This is a digital rendition of <a href="https://www.wikiart.org/en/m-c-escher/square-limit">M.C. Escher’s square limit</a> artwork. The most surprising thing about this piece
is that it is composed of only four basic tiles shown below:</p>

<p><img src="https://raw.githubusercontent.com/Abhiroop/Abhiroop.github.io/eed5bc0c912e0cff256c677415675b1be26228f7/art/tile.svg" alt="Escher Tiles" /></p>

<p>The entirety of this art is generated by taking these four tiles and using the power of Functional Programming à la function composition to create several powerful combinators that can render the infinite shapes. This technique was illustrated by Peter Henderson in his 1982 seminal paper - <a href="https://dl.acm.org/doi/10.1145/800068.802148">Functional Geometry</a>. The paper mentions Mary Sheeran (who happened to be my PhD supervisor!) as the original implementor in UCSD Pascal. I have implemented the same in Haskell using the wonderful <a href="https://hackage.haskell.org/package/diagrams">diagrams</a> library.</p>

<p>The code intentionally avoids complicated rasterization and related techniques from computer graphics, so that the simple (yet powerful) essence of <em>functional geometry</em> is evident at a glance. The heart of the logic is this:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">squareLimit</span> <span class="o">::</span> <span class="kt">Diagram</span> <span class="kt">B</span>
<span class="n">squareLimit</span> <span class="o">=</span> <span class="n">cycle</span> <span class="n">corner</span>

<span class="n">corner</span> <span class="o">=</span> <span class="n">nonet</span> <span class="n">corner2</span> <span class="n">side2</span> <span class="n">side2</span>
               <span class="p">(</span><span class="n">rot</span> <span class="n">side2</span><span class="p">)</span> <span class="n">uTile</span> <span class="p">(</span><span class="n">rot</span> <span class="n">tTile</span><span class="p">)</span>
               <span class="p">(</span><span class="n">rot</span> <span class="n">side2</span><span class="p">)</span> <span class="p">(</span><span class="n">rot</span> <span class="n">tTile</span><span class="p">)</span> <span class="p">(</span><span class="n">rot</span> <span class="n">qTile</span><span class="p">)</span>

<span class="n">nonet</span> <span class="n">p1</span> <span class="n">p2</span> <span class="n">p3</span> <span class="n">p4</span> <span class="n">p5</span> <span class="n">p6</span> <span class="n">p7</span> <span class="n">p8</span> <span class="n">p9</span>
  <span class="o">=</span> <span class="n">onTopOf</span> <span class="mi">1</span> <span class="mi">2</span>  <span class="p">(</span><span class="n">nextTo</span> <span class="mi">1</span> <span class="mi">2</span> <span class="n">p1</span> <span class="p">(</span><span class="n">nextTo</span> <span class="mi">1</span> <span class="mi">1</span> <span class="n">p2</span> <span class="n">p3</span><span class="p">))</span>
   <span class="p">(</span><span class="n">onTopOf</span> <span class="mi">1</span> <span class="mi">1</span>  <span class="p">(</span><span class="n">nextTo</span> <span class="mi">1</span> <span class="mi">2</span> <span class="n">p4</span> <span class="p">(</span><span class="n">nextTo</span> <span class="mi">1</span> <span class="mi">1</span> <span class="n">p5</span> <span class="n">p6</span><span class="p">))</span>
                 <span class="p">(</span><span class="n">nextTo</span> <span class="mi">1</span> <span class="mi">2</span> <span class="n">p7</span> <span class="p">(</span><span class="n">nextTo</span> <span class="mi">1</span> <span class="mi">1</span> <span class="n">p8</span> <span class="n">p9</span><span class="p">)))</span>

<span class="n">nextTo</span> <span class="n">m</span> <span class="n">n</span> <span class="n">d1</span> <span class="n">d2</span> <span class="o">=</span> <span class="n">scaledD1</span> <span class="o">|||</span> <span class="n">scaledD2</span>
  <span class="kr">where</span>
    <span class="n">scaledD1</span> <span class="o">=</span> <span class="n">d1</span> <span class="o">#</span> <span class="n">scaleX</span> <span class="p">(</span><span class="n">m</span> <span class="o">/</span> <span class="n">total</span><span class="p">)</span>
    <span class="n">scaledD2</span> <span class="o">=</span> <span class="n">d2</span> <span class="o">#</span> <span class="n">scaleX</span> <span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">total</span><span class="p">)</span>
    <span class="n">total</span> <span class="o">=</span> <span class="n">m</span> <span class="o">+</span> <span class="n">n</span>

<span class="n">onTopOf</span> <span class="n">m</span> <span class="n">n</span> <span class="n">d1</span> <span class="n">d2</span> <span class="o">=</span> <span class="n">scaledD1</span> <span class="o">===</span> <span class="n">scaledD2</span>
  <span class="kr">where</span>
    <span class="n">scaledD1</span> <span class="o">=</span> <span class="n">d1</span> <span class="o">#</span> <span class="n">scaleY</span> <span class="p">(</span><span class="n">m</span> <span class="o">/</span> <span class="n">total</span><span class="p">)</span>
    <span class="n">scaledD2</span> <span class="o">=</span> <span class="n">d2</span> <span class="o">#</span> <span class="n">scaleY</span> <span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">total</span><span class="p">)</span>
    <span class="n">total</span> <span class="o">=</span> <span class="n">m</span> <span class="o">+</span> <span class="n">n</span>

<span class="n">side2</span>   <span class="o">=</span> <span class="n">quartet</span> <span class="n">side1</span> <span class="n">side1</span> <span class="p">(</span><span class="n">rot</span> <span class="n">tTile</span><span class="p">)</span> <span class="n">tTile</span>
<span class="n">corner2</span> <span class="o">=</span> <span class="n">quartet</span> <span class="n">corner1</span> <span class="n">side1</span> <span class="p">(</span><span class="n">rot</span> <span class="n">side1</span><span class="p">)</span> <span class="n">uTile</span>

<span class="n">uTile</span> <span class="o">=</span> <span class="n">cycle</span> <span class="o">$</span> <span class="n">rot</span> <span class="n">qTile</span>
<span class="n">tTile</span> <span class="o">=</span> <span class="n">quartet</span> <span class="n">pTile</span> <span class="n">qTile</span> <span class="n">rTile</span> <span class="n">sTile</span>

<span class="n">cycle</span> <span class="n">p1</span> <span class="o">=</span> <span class="n">quartet</span> <span class="n">p1</span> <span class="p">(</span><span class="n">rot</span> <span class="o">$</span> <span class="n">rot</span> <span class="o">$</span> <span class="n">rot</span> <span class="n">p1</span><span class="p">)</span> <span class="p">(</span><span class="n">rot</span> <span class="n">p1</span><span class="p">)</span> <span class="p">(</span><span class="n">rot</span> <span class="o">$</span> <span class="n">rot</span> <span class="n">p1</span><span class="p">)</span>

<span class="n">quartet</span> <span class="n">p</span> <span class="n">q</span> <span class="n">r</span> <span class="n">s</span> <span class="o">=</span> <span class="n">scale</span> <span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span> <span class="o">$</span> <span class="n">centerXY</span> <span class="p">((</span><span class="n">p</span> <span class="o">|||</span> <span class="n">q</span><span class="p">)</span> <span class="o">===</span> <span class="p">(</span><span class="n">r</span> <span class="o">|||</span> <span class="n">s</span><span class="p">))</span>

<span class="n">rot</span> <span class="n">p</span> <span class="o">=</span> <span class="n">rotate</span> <span class="p">(</span><span class="mi">90</span> <span class="o">@@</span> <span class="n">deg</span><span class="p">)</span> <span class="n">p</span>

<span class="n">pTile</span> <span class="o">=</span> <span class="n">makeTile</span> <span class="n">markingsP</span>
<span class="n">qTile</span> <span class="o">=</span> <span class="n">makeTile</span> <span class="n">markingsQ</span>
<span class="n">rTile</span> <span class="o">=</span> <span class="n">makeTile</span> <span class="n">markingsR</span>
<span class="n">sTile</span> <span class="o">=</span> <span class="n">makeTile</span> <span class="n">markingsS</span>
</code></pre></div></div>

<p>The complete executable code is available <a href="https://github.com/Abhiroop/geofunc">here</a>. To the best of my knowledge, this is one of the best illustrations of the power of function composition to create complex software. There is not a single wasted line, and the essence of the code is as clear and declarative as possible.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry><entry><title type="html">Solving Monty Hall with the List monad (or Probabilistic Programming)</title><link href="https://abhiroop.github.io/Monty-Hall-Probabilistic/" rel="alternate" type="text/html" title="Solving Monty Hall with the List monad (or Probabilistic Programming)" /><published>2024-07-25T00:00:00+00:00</published><updated>2024-07-25T00:00:00+00:00</updated><id>https://abhiroop.github.io/Monty-Hall-Probabilistic</id><content type="html" xml:base="https://abhiroop.github.io/Monty-Hall-Probabilistic/"><![CDATA[<p>Continuing from my <a href="https://abhiroop.github.io/Monty-Hall/">Bayes Theorem-based blog post</a> on solving the Monty-Hall problem, I will now show a rather elegant way to model the solution that exploits
the humble List monad. To remind the reader, the Monty Hall problem involves a contestant initially choosing one of three doors, hoping to find a car behind it. Monty Hall then opens a different door, revealing a goat, and asks the contestant <em>if they want to switch their initial choice</em>. The goal of the solution is to guide the contestants to make this switching choice such that they have a higher probability of winning. This post was inspired by a <a href="https://dennybritz.com/posts/probability-monads-from-scratch/">less-than 100 lines implementation of a probabilistic programming monad</a> in Haskell</p>

<p>We will begin by representing discrete distributions as a Haskell datatype:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">type</span> <span class="kt">Prob</span> <span class="o">=</span> <span class="kt">Double</span> <span class="c1">-- probability of an outcome</span>

<span class="kr">newtype</span> <span class="kt">Dist</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">Dist</span> <span class="p">{</span> <span class="n">unpackDist</span> <span class="o">::</span> <span class="p">[(</span><span class="n">a</span><span class="p">,</span> <span class="kt">Prob</span><span class="p">)]</span> <span class="p">}</span>
</code></pre></div></div>

<p>This is a nicer reformulation of distributions that the List monad could naturally model. For example, in an experiment with 5 tosses (modelled as <code class="language-plaintext highlighter-rouge">data Toss = Head | Tail</code>), that leads to 
4 Heads and 1 Tail, our <code class="language-plaintext highlighter-rouge">Dist</code> type will model this as <code class="language-plaintext highlighter-rouge">Dist [(Head, 0.8), (Tail, 0.2)]</code>. However, we could have modelled this as a plain list as <code class="language-plaintext highlighter-rouge">[Head, Head, Head, Head, Tail]</code>. It is simply that the <code class="language-plaintext highlighter-rouge">Dist</code> type is nicer to work with (but it is a wrapper on the plain <code class="language-plaintext highlighter-rouge">List</code>).</p>

<p>Next, to compute probabilities accurately, it is easier for our representation if we normalise the distribution. We can do that using:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">normP</span> <span class="o">::</span> <span class="p">[(</span><span class="n">a</span><span class="p">,</span> <span class="kt">Prob</span><span class="p">)]</span> <span class="o">-&gt;</span> <span class="p">[(</span><span class="n">a</span><span class="p">,</span> <span class="kt">Prob</span><span class="p">)]</span>
<span class="n">normP</span> <span class="n">xs</span> <span class="o">=</span> <span class="n">map</span> <span class="p">(</span><span class="nf">\</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">/</span> <span class="p">(</span><span class="n">sumP</span> <span class="n">xs</span><span class="p">)))</span> <span class="n">xs</span>
  <span class="kr">where</span>
    <span class="n">sumP</span> <span class="o">::</span> <span class="p">[(</span><span class="n">a</span><span class="p">,</span> <span class="kt">Prob</span><span class="p">)]</span> <span class="o">-&gt;</span> <span class="kt">Prob</span>
    <span class="n">sumP</span> <span class="o">=</span> <span class="n">sum</span> <span class="o">.</span> <span class="n">map</span> <span class="n">snd</span>
</code></pre></div></div>

<p>Given the above, we can now define the uniform distribution:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">uniform</span> <span class="o">::</span> <span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="o">-&gt;</span> <span class="kt">Dist</span> <span class="n">a</span>
<span class="n">uniform</span> <span class="o">=</span> <span class="kt">Dist</span> <span class="o">.</span> <span class="n">normP</span> <span class="o">.</span> <span class="n">map</span> <span class="p">(,</span> <span class="mf">1.0</span><span class="p">)</span>

<span class="o">&gt;</span> <span class="n">uniform</span> <span class="p">[</span><span class="kt">Head</span><span class="p">,</span> <span class="kt">Head</span><span class="p">,</span> <span class="kt">Head</span><span class="p">,</span> <span class="kt">Head</span><span class="p">,</span> <span class="kt">Tail</span><span class="p">]</span>
<span class="kt">Head</span> <span class="o">|</span> <span class="mf">0.8000</span>
<span class="kt">Tail</span> <span class="o">|</span> <span class="mf">0.2000</span>
</code></pre></div></div>
<p>The <code class="language-plaintext highlighter-rouge">Show</code> instance of <code class="language-plaintext highlighter-rouge">Dist</code> ensures that it sums up the probabilities of the same outcome, giving the above. We don’t show the <code class="language-plaintext highlighter-rouge">Show</code> instance here, that and other useful probabilistic combinators are 
presented in this <a href="https://dennybritz.com/posts/probability-monads-from-scratch/">blog post</a>.</p>

<h4 id="monad-instance">Monad instance</h4>

<p>The Monad instance of <code class="language-plaintext highlighter-rouge">Dist</code> is the most important construct for modelling Monty Hall. We will use the Monad instance to model conditional discrete distribution. Given two discrete random variables <code class="language-plaintext highlighter-rouge">X</code> and <code class="language-plaintext highlighter-rouge">Y</code>,
we can compute their joint distribution (given that they are not mutually independent) as follows:</p>

\[Pr({X = x} \cap {Y = y}) = Pr(X = x | Y = y) . Pr (Y = y)\]

<p>We can convert this idea to the Monad instance quite naturally:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">instance</span> <span class="kt">Monad</span> <span class="kt">Dist</span> <span class="kr">where</span>
  <span class="p">(</span><span class="kt">Dist</span> <span class="n">xs</span><span class="p">)</span> <span class="o">&gt;&gt;=</span> <span class="n">f</span> <span class="o">=</span> <span class="kt">Dist</span> <span class="o">$</span> <span class="kr">do</span>
    <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">p</span><span class="p">)</span>  <span class="o">&lt;-</span> <span class="n">xs</span>
    <span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">p'</span><span class="p">)</span> <span class="o">&lt;-</span> <span class="n">unpackDist</span> <span class="p">(</span><span class="n">f</span> <span class="n">x</span><span class="p">)</span>
    <span class="n">return</span> <span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">p</span> <span class="o">*</span> <span class="n">p'</span><span class="p">)</span>
</code></pre></div></div>
<p>The <code class="language-plaintext highlighter-rouge">p * p'</code> is multiplying the probabilities of the two outcomes. The <code class="language-plaintext highlighter-rouge">join</code> function of the Monad instance can allow us to consider an alternate view of the above. Considering a <em>distribution of distributions</em> (please don’t try to visualise),
we want to flatten it into a single distribution as follows:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">join</span> <span class="o">::</span> <span class="kt">Dist</span> <span class="p">(</span><span class="kt">Dist</span> <span class="n">a</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Dist</span> <span class="n">a</span>
<span class="n">join</span> <span class="p">(</span><span class="kt">Dist</span> <span class="n">dist</span><span class="p">)</span> <span class="o">=</span> <span class="kt">Dist</span> <span class="o">$</span> <span class="kr">do</span> <span class="c1">-- dist  :: [(Dist a, Prob)]</span>
  <span class="p">(</span><span class="kt">Dist</span> <span class="n">dista</span><span class="p">,</span> <span class="n">prob</span><span class="p">)</span> <span class="o">&lt;-</span> <span class="n">dist</span> <span class="c1">-- dista :: [(a, Prob)]</span>
  <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">prob2</span><span class="p">)</span>         <span class="o">&lt;-</span> <span class="n">dista</span>
  <span class="n">return</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">prob</span> <span class="o">*</span> <span class="n">prob2</span><span class="p">)</span>
</code></pre></div></div>

<h4 id="monty-hall">Monty Hall</h4>

<p>Armed with this small set of combinators above, we now model the Monty Hall problem. We start by defining the outcome type:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">data</span> <span class="kt">Outcome</span> <span class="o">=</span> <span class="kt">Win</span> <span class="o">|</span> <span class="kt">Loss</span> <span class="kr">deriving</span> <span class="p">(</span><span class="kt">Show</span><span class="p">,</span> <span class="kt">Eq</span><span class="p">,</span> <span class="kt">Ord</span><span class="p">)</span>
</code></pre></div></div>

<p>And now, let’s intuitively specify the switching strategy : <em>If our first choice is the winning door</em>, followed by which Monty Hall opens a door that contains a goat, 
<em>if we switch, then we will certainly lose</em>. That is because we already selected the winning door, and the switch will cost us the victory. However, <em>if our first choice is the losing door</em>,
followed by Monty Hall opening the door containing a goat, <em>if we switch, then we certainly win</em>. Because we chose the losing door, Monty Hall opened the other losing door and the
remaining door certainly assures our victory.</p>

<p>In the specification above we used the language of <em>certain victory</em> or <em>certain loss</em>, so we need to model certainty. And that is modelled as:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">certainly</span> <span class="o">::</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Dist</span> <span class="n">a</span>
<span class="n">certainly</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">Dist</span> <span class="p">[(</span><span class="n">a</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)]</span>
</code></pre></div></div>

<p>So, one outcome that has a probability of 1.0. And, with that, we can easily translate the English specification above, to the Haskell snippet below:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">switching</span> <span class="o">::</span> <span class="kt">Dist</span> <span class="kt">Outcome</span>
<span class="n">switching</span> <span class="o">=</span> <span class="kr">do</span>
  <span class="n">firstChoice</span> <span class="o">&lt;-</span> <span class="n">uniform</span> <span class="p">[</span><span class="kt">Win</span><span class="p">,</span><span class="kt">Loss</span><span class="p">,</span><span class="kt">Loss</span><span class="p">]</span>
  <span class="kr">if</span> <span class="p">(</span><span class="n">firstChoice</span> <span class="o">==</span> <span class="kt">Win</span><span class="p">)</span>
  <span class="kr">then</span> <span class="cm">{- switching will -}</span> <span class="n">certainly</span> <span class="kt">Loss</span>
  <span class="kr">else</span> <span class="cm">{- switching will -}</span> <span class="n">certainly</span> <span class="kt">Win</span>
</code></pre></div></div>

<p>The comment <code class="language-plaintext highlighter-rouge">{- switching will -}</code> is added such that the specification almost translates to <em>literal</em> English. Now, we can observe the distributions:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">&gt;</span> <span class="n">switching</span>
 <span class="kt">Win</span> <span class="o">|</span> <span class="mf">0.6667</span>
<span class="kt">Loss</span> <span class="o">|</span> <span class="mf">0.3333</span>
</code></pre></div></div>

<p>This shows us that switching our choice will allow us to win 66% of the time while losing 33% of the time. The first choice has the following distributions:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">&gt;</span> <span class="n">uniform</span> <span class="p">[</span><span class="kt">Win</span><span class="p">,</span> <span class="kt">Loss</span><span class="p">,</span> <span class="kt">Loss</span><span class="p">]</span>
 <span class="kt">Win</span> <span class="o">|</span> <span class="mf">0.3333</span>
<span class="kt">Loss</span> <span class="o">|</span> <span class="mf">0.3333</span>
<span class="kt">Loss</span> <span class="o">|</span> <span class="mf">0.3333</span>
</code></pre></div></div>

<p>Our computation proceeds by <em>certainly</em> losing if we switch after winning the first round, while <em>certainly</em> winning if we switch after losing the first round. Hence the calculation proceeds as:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kt">Win</span> <span class="o">~&gt;</span> <span class="n">certainly</span> <span class="kt">Loss</span><span class="o">|</span> <span class="mf">0.3333</span> <span class="o">*</span> <span class="mf">1.0</span>
<span class="kt">Loss</span> <span class="o">~&gt;</span> <span class="n">certainly</span> <span class="kt">Win</span> <span class="o">|</span> <span class="mf">0.3333</span> <span class="o">*</span> <span class="mf">1.0</span>
<span class="kt">Loss</span> <span class="o">~&gt;</span> <span class="n">certainly</span> <span class="kt">Win</span> <span class="o">|</span> <span class="mf">0.3333</span> <span class="o">*</span> <span class="mf">1.0</span>

<span class="o">==&gt;</span>

<span class="kt">Loss</span> <span class="o">|</span> <span class="mf">0.3333</span>
<span class="kt">Win</span>  <span class="o">|</span> <span class="mf">0.3333</span>
<span class="kt">Win</span>  <span class="o">|</span> <span class="mf">0.3333</span>

<span class="o">==&gt;</span>

 <span class="kt">Win</span> <span class="o">|</span> <span class="mf">0.6667</span>
<span class="kt">Loss</span> <span class="o">|</span> <span class="mf">0.3333</span>
</code></pre></div></div>

<p>Hence, the contestant should switch!</p>

<p>Source code for the above is available at <a href="https://github.com/Abhiroop/bayes">Abhiroop/bayes</a></p>]]></content><author><name></name></author><summary type="html"><![CDATA[Continuing from my Bayes Theorem-based blog post on solving the Monty-Hall problem, I will now show a rather elegant way to model the solution that exploits the humble List monad. To remind the reader, the Monty Hall problem involves a contestant initially choosing one of three doors, hoping to find a car behind it. Monty Hall then opens a different door, revealing a goat, and asks the contestant if they want to switch their initial choice. The goal of the solution is to guide the contestants to make this switching choice such that they have a higher probability of winning. This post was inspired by a less-than 100 lines implementation of a probabilistic programming monad in Haskell]]></summary></entry><entry><title type="html">The Monty Hall Problem and Bayes Theorem</title><link href="https://abhiroop.github.io/Monty-Hall/" rel="alternate" type="text/html" title="The Monty Hall Problem and Bayes Theorem" /><published>2024-06-28T00:00:00+00:00</published><updated>2024-06-28T00:00:00+00:00</updated><id>https://abhiroop.github.io/Monty-Hall</id><content type="html" xml:base="https://abhiroop.github.io/Monty-Hall/"><![CDATA[<p>Today, while reading Steven Pinker’s latest book on critical thinking, <em>Rationality</em>, I was scribbling a solution to the Monty Hall problem. The Monty Hall problem is probably one of the most well-studied 
pop-science problem statement with different intuitive explanations of the solution. In case, dear reader, you have been living under a rock here is the problem statement:</p>

<p><strong>Setup</strong>: A game show where you as a contestant have to guess to win a prize. The game comprises three doors. Behind one door is a car (the prize), and behind the other two doors are goats.</p>

<p><strong>Initial Choice</strong>: You choose one of the three doors.</p>

<p><strong>Host’s Action</strong>: The host, Monty Hall, who knows what is behind each door, opens one of the remaining two doors, revealing a goat.</p>

<p><strong>Decision Point</strong>: You are then allowed to stick with your original choice or switch to the other unopened door.</p>

<p><strong>Question: How should you maximise your chances of winning the car?</strong></p>

<p>Now, somehow through various coffee table discussions, I always knew the answer was that the contestant should switch their choice. So many times I have had this encounter that I realised I never bothered to 
calculate or question my intuition on why the contestant should switch. However, the book informed me that even some of the leading mathematicians and statisticians of the era, including the legendary 
<a href="https://en.wikipedia.org/wiki/Paul_Erd%C5%91s">Paul Erdős</a>, were stumped by the answer to the problem. Erdős was convinced of the solution only after he witnessed simulations rather than a proof or calculation!</p>

<p>So, I decided to reuse my ninth-standard probability lessons of Bayes Theorem to craft a solution. I have not at this point read through any solution, except the one that Pinker provides in his book.
Pinker’s solution (I am sure this particular solution was not Pinker’s proposal but his explanation of a known solution) is quite different and uses a gameplay simulation and informal intuitions to drive home the result (and I think his explanation works). The Goodreads reviews particularly point out that and 
appreciate Pinker’s lucid and intuitive explanation. If you are interested in reading more about his solution, I encourage you to purchase the <a href="https://stevenpinker.com/publications/rationality-what-it-why-it-seems-so-scarce-and-why-it-matters">book.</a>
However, I will use an established result of the Bayes theorem of conditional probability, which states:</p>

\[Pr(A|B) =  \frac{Pr(B|A) . Pr (A)}{Pr(B)}\]

<p>I have written enough PL papers that I am itching to start explaining the notation and domains, but it is 3:25 am and I am hoping that the reader can follow along with a basic ninth standard notation. If not, please jump to the wiki article on <a href="https://en.wikipedia.org/wiki/Bayes%27_theorem">Bayes Theorem</a>.</p>

<p>Now, let’s consider the Monty Hall problem and assume that among the three doors, the contestant chooses the first one. Seeing this, Monty Hall opens the third door to reveal a goat. Then, Monty Hall asks 
the contestant if they want to switch. Let me draw the scenario at this point</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   ---         ---         ---
  | C |       |   |       | M |
  Door 1      Door 2      Door 3
</code></pre></div></div>

<p>The <code class="language-plaintext highlighter-rouge">C</code> on Door 1 marks that the contestant chooses the door and the <code class="language-plaintext highlighter-rouge">M</code> on Door 3 marks that Monty Hall opened it to reveal a goat. 
The contestant now needs to maximise their probability of winning a car. Should they stick with Door 1 or switch to Door 2? In effect, they need to calculate:</p>

\[Pr(car\ in\ 2|my\ choice\ 1,\ monty\ opened\ 3) =  \frac{Pr(my\ choice\ 1,\ monty\ opened\ 3|car\ in\ 2) . Pr (car\ in\ 2)}{Pr(my\ choice\ 1,\ monty\ opened\ 3)}\]

<p>Let’s calculate the probabilities on the right side. <code class="language-plaintext highlighter-rouge">Pr(car in 2)</code> is the simplest one. Given that it is not constrained by any conditions, the probability that the car is in one of the three doors (in this case door 2) is simply <code class="language-plaintext highlighter-rouge">1/3</code></p>

<p>Next, <code class="language-plaintext highlighter-rouge">Pr(my choice 1, monty opened 3|car in 2)</code>. This one is interesting. Given that the car is in door 2, what is the probability that Monty Hall opened door 3, while the contestant chose door 1? This
one is interesting because the problem statement makes certain assumptions - (1) <em>Monty Hall actually knows where the car is</em>, (2) <em>Monty Hall wants the show to last at least two rounds</em>. Holding assumption 2, Monty Hall will intentionally open a door with a goat such that the show can progress to the next round. Given that, in our conditional probability scenario, Monty can never open door 2, because the car will be revealed (remember assumption 1 that Monty knows where the car is) and the show won’t progress to the next round. Similarly, Monty Hall can never open door 1, because the contestant has already made door 1 their choice. If door 1 houses a goat or a car, in both cases assumption 2 is invalidated and the show abruptly ends.
Hence, the only choice that Monty Hall has is to open Door 3. So, the overall conditional probability <code class="language-plaintext highlighter-rouge">Pr(my choice 1, monty opened 3|car in 2)</code> is in fact <code class="language-plaintext highlighter-rouge">1</code>! Door 3 is the only choice Monty Hall has.</p>

<p>Finally, <code class="language-plaintext highlighter-rouge">Pr(my choice 1, monty opened 3)</code>. This is a weaker version of the previous probability, where the condition <code class="language-plaintext highlighter-rouge">car in 2</code> is no longer in place. So, now Monty Hall cannot open door 1 because the
contestant has already chosen door 1 and opening door 1 will abruptly end the show (as explained earlier). Now, the probability of Monty opening door 3 is 50% or <code class="language-plaintext highlighter-rouge">1/2</code> because the constraint of the car is gone, and
Monty Hall needs to choose 1 out of 2 doors (Doors 2 and 3).</p>

<p>With the above, we will have:</p>

\[Pr(car\ in\ 2|my\ choice\ 1,\ monty\ opened\ 3) =  \frac{Pr(my\ choice\ 1,\ monty\ opened\ 3|car\ in\ 2) . Pr (car\ in\ 2)}{Pr(my\ choice\ 1,\ monty\ opened\ 3)}\]

\[Pr(car\ in\ 2|my\ choice\ 1,\ monty\ opened\ 3) =  \frac{1 . \frac{1}{3}}{\frac{1}{2}}\]

\[Pr(car\ in\ 2|my\ choice\ 1,\ monty\ opened\ 3) =  \frac{2}{3}\]

<p>Equivalently, we can calculate the probability of the car being behind Door 1 as simply <code class="language-plaintext highlighter-rouge">1 - 2/3 = 1/3</code>. Hence, <strong>the contestant should switch</strong>, as they have a 66.67% probability of the car being behind Door 2.</p>

<p>Given that Steven Pinker’s book is about <em>thinking styles</em>, I realised that I attempt to approach most problems quite <em>mechanically</em>. I observed this pattern while solving the other puzzles in the book, where I typically try to abstract the problem into a logical/math domain and apply established/known results to answer the question, rather than using physical intuition.
Quiet often this style is described as <em>dry</em> and <em>procedural</em>, as opposed to Pinker’s <em>lucid</em> and <em>intuitive</em> style, but there is sometimes merit in dryness.</p>

<blockquote>
  <p>Mathematics is a game played according to certain simple rules with meaningless marks on paper - David Hilbert</p>
</blockquote>]]></content><author><name></name></author><summary type="html"><![CDATA[Today, while reading Steven Pinker’s latest book on critical thinking, Rationality, I was scribbling a solution to the Monty Hall problem. The Monty Hall problem is probably one of the most well-studied pop-science problem statement with different intuitive explanations of the solution. In case, dear reader, you have been living under a rock here is the problem statement:]]></summary></entry><entry><title type="html">Overleaf submission to arXiV</title><link href="https://abhiroop.github.io/Overleaf/" rel="alternate" type="text/html" title="Overleaf submission to arXiV" /><published>2024-01-17T00:00:00+00:00</published><updated>2024-01-17T00:00:00+00:00</updated><id>https://abhiroop.github.io/Overleaf</id><content type="html" xml:base="https://abhiroop.github.io/Overleaf/"><![CDATA[<p>After scouring through the entire web for close to 5 years and giving up out of sheer frustration every time, I was finally able to upload my TeX sources to arXiv (as of 17.01.2024). The solution was buried as a comment to one of the lesser upvoted answers in a less popular StackExchange thread. I hope Google will push this post high up so that it can help Overleaf (and <code class="language-plaintext highlighter-rouge">minted</code>) users.</p>

<ol>
  <li>Compile with <code class="language-plaintext highlighter-rouge">\usepackage[finalizecache,cachedir=.]{minted}</code></li>
  <li>Go to logs and output files &gt; other logs and files and download everything with pyg. (all the <code class="language-plaintext highlighter-rouge">*.pygtex</code> and the <code class="language-plaintext highlighter-rouge">*.pygfile</code>)</li>
  <li>Compile now with <code class="language-plaintext highlighter-rouge">\usepackage[frozencache,cachedir=.]{minted}</code></li>
  <li>Download the complete project <code class="language-plaintext highlighter-rouge">.zip</code> by submitting it to arXiV</li>
  <li>Unzip and put all the <code class="language-plaintext highlighter-rouge">*.pygtex</code> files and the <code class="language-plaintext highlighter-rouge">*.pygfile</code> into the folder</li>
  <li>Zip again and upload to arXiv; Works for me!</li>
</ol>

<p><a href="https://tex.stackexchange.com/questions/280590/work-around-for-minted-code-highlighting-in-arxiv#comment1207889_414781">Answer source</a> (User: <a href="https://tex.stackexchange.com/users/23084/rhombidodecahedron">rhombidodecahedron</a>)</p>]]></content><author><name></name></author><summary type="html"><![CDATA[After scouring through the entire web for close to 5 years and giving up out of sheer frustration every time, I was finally able to upload my TeX sources to arXiv (as of 17.01.2024). The solution was buried as a comment to one of the lesser upvoted answers in a less popular StackExchange thread. I hope Google will push this post high up so that it can help Overleaf (and minted) users.]]></summary></entry><entry><title type="html">Opinion piece on Capitalism and Communism</title><link href="https://abhiroop.github.io/Capitalism-Communism/" rel="alternate" type="text/html" title="Opinion piece on Capitalism and Communism" /><published>2021-07-19T00:00:00+00:00</published><updated>2021-07-19T00:00:00+00:00</updated><id>https://abhiroop.github.io/Capitalism-Communism</id><content type="html" xml:base="https://abhiroop.github.io/Capitalism-Communism/"><![CDATA[<p>Recently in a private discussion forum, there was a topic under consideration which went something like this:</p>

<blockquote>
  <p>“Capitalism is pregnant with communism. The next country to become a communist state will be the United States.”</p>
</blockquote>

<p>I wrote a slightly descriptive answer to this and feel the response might be worth sharing. I am simply copy-pasting my answer from the forum verbatim below.</p>

<hr />

<blockquote>
  <p>“Capitalism is pregnant with communism. The next country to become a communist state will be the United States.”<br />
Any thoughts on it?</p>
</blockquote>

<p>Sadly, I would have to (mostly) disagree with both of these statements. Especially I believe if any of the nations were to approach a Communist-style of governance, the US will most likely be the last country to adopt it in its entirety. But this thought is based on assuming that the author is talking about a <em>purely</em> Marxist brand of communism.</p>

<p>Would America have state-controlled production? I cannot see that in the near future. On the other hand, if the implication was that there will be influences of certain ideas born and bred in the Communist and Socialist schools, well they are already there in place. For eg, the US has antitrust laws in place which are fundamentally incompatible with a pure laissez-faire capitalist system. An example of it being used in action is the dissolution of the AT&amp;T corporation in 1982[1] owing to it becoming a monopoly in telephone services.</p>

<p>On a much smaller scale, an example of a centralised control regulating a free market is the recent fiasco with the Gamestop stock price skyrocketing and then Robinhood delisting the stocks from being traded. There were lots of complaints and criticism towards Robinhood broadly for:</p>

<ol>
  <li>not allowing the free market to thrive and</li>
  <li>decapitating small individual investment parties.</li>
</ol>

<p>and Robinhood was labelled as a giant capitalist bully. The irony here is that if you read these two points again and <em>carefully</em>, you will realise that most Marxist schools strive exactly for outcome 1 and partially for outcome 2. So it would seem that the lines between the two ideologies are slightly blurred in the more modern contexts.</p>

<p>Marx has criticised the liberty of an individual within a bourgeois society[2] as “the liberty of man viewed as an isolated monad, withdrawn into himself. […] The practical application of the right of liberty is the right of private property”. While phrased very sharply, this could be a slippery slope. In most implementations of Marxist communism, the value of an individual is severely diminished at the cost of the community (or society or state). Now anybody who has remotely been engaged with any form of creative expression (like art, music, poetry, novels, cinema) will recognize the dangers of diminishing the power of an individual.</p>

<p>On the other hand unchecked capitalism, as perhaps we all know, can lead to the commodification of, well, everything! Furthermore, perhaps, the commodification of an individual as well! This is partly the reason that antitrust laws exist in even the most neoliberal countries (like the US and UK). This is why corporations like Amazon, Microsoft, Oracle get repeatedly being hit by lawsuits (especially in the more left-leaning Europe).</p>

<p>While I personally do not understand economics very well and work very little towards bridging that gap (mostly because my topic of PhD is a very different subject :)), I do keep an active interest in education and wonder what the outcomes of Capitalistic and Communist policies are (or would be) on our Universities and academic institutions. The American model of private universities has led to a huge student-loan debt in that country and I can see the flurry of private universities rising in India following that same path. On the other hand, European countries seemed to have done surprisingly well with their state institutions, with free education and unimaginable policies like paying the students instead to attend colleges (Sweden).</p>

<p>I wonder what needs to be done in India to emulate the Europen model. It is a complex question. With such a vast population of youth in our country (isn’t India’s average age close to 25?) we want education to spread fast. If we over-rely on the European model of prestigious state institutions, it gives rise to a select class of elite institutes like the IITs - which are notoriously hard to get into and the entrance exams cause an unprecedented amount of mental turmoil within the youth. If on the other hand, we promote private universities within a free market economy, we inevitably gravitate towards the American model and its outcome of insurmountable student loans.</p>

<p>It seems, under all circumstances, the mass of students suffers. How do we cope with this? What is the ideal solution? I have no answers but only questions.</p>

<p>[1] <a href="https://en.wikipedia.org/wiki/Breakup_of_the_Bell_System">Breakup of the Bell System</a><br />
[2] Marx, Karl. Karl Marx: selected writings.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Recently in a private discussion forum, there was a topic under consideration which went something like this:]]></summary></entry><entry><title type="html">Unfolding Fix</title><link href="https://abhiroop.github.io/Fix/" rel="alternate" type="text/html" title="Unfolding Fix" /><published>2019-12-20T00:00:00+00:00</published><updated>2019-12-20T00:00:00+00:00</updated><id>https://abhiroop.github.io/Fix</id><content type="html" xml:base="https://abhiroop.github.io/Fix/"><![CDATA[<p>In pure lambda calculus (untyped or typed) the <code class="language-plaintext highlighter-rouge">fix</code> combinator is used to encode recursion. <code class="language-plaintext highlighter-rouge">fix</code> represents the least fixed point of a function <code class="language-plaintext highlighter-rouge">f</code> i.e the least defined <code class="language-plaintext highlighter-rouge">x</code> for which <code class="language-plaintext highlighter-rouge">f x = x</code>. An important concept is that a function <strong>need not have a least fixed point</strong>. But for those functions which do, the least fixed point is the base case for recursion. An intuition about the least fixed point is :</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>f (f (f ( f x))) = x
</code></pre></div></div>

<p>This is a convenient way to represent recursion in a language which doesn’t support naming functions. The base case of the recursion is determined by the actual least point of the function. Here we will take the most popular example of encoding fix which is a factorial function :</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">factorial</span> <span class="n">n</span> <span class="o">=</span> <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
              <span class="kr">then</span> <span class="mi">1</span>
              <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">factorial</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
</code></pre></div></div>

<p>and demonstrate the stack trace of how the fix function unfolds this recursion. First lets define <code class="language-plaintext highlighter-rouge">fix</code> in Haskell.</p>

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

<p>Owing to the laziness of Haskell we can define such an expression which doesn’t fully evaluate the <code class="language-plaintext highlighter-rouge">fix f</code> call in the right hand side but rather creates a thunk.</p>

<p>Now writing the <code class="language-plaintext highlighter-rouge">factorial</code> function using <code class="language-plaintext highlighter-rouge">fix</code> :</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">factorial</span> <span class="o">=</span> <span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                    <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                    <span class="kr">then</span> <span class="mi">1</span> 
                    <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span>
</code></pre></div></div>

<p>or</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">factorial</span> <span class="n">x</span> <span class="o">=</span> 
  <span class="p">(</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
          <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
          <span class="kr">then</span> <span class="mi">1</span> 
          <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)))</span> <span class="n">x</span>
</code></pre></div></div>

<p>Lets try calculating <code class="language-plaintext highlighter-rouge">factorial 3</code></p>

<p>Unfolding:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="n">f</span> <span class="p">(</span><span class="n">fix</span> <span class="n">f</span><span class="p">))</span> <span class="mi">3</span>

<span class="c1">-- substituting f</span>

<span class="p">((</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
            <span class="kr">then</span> <span class="mi">1</span> 
            <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> 
            <span class="p">(</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                    <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                    <span class="kr">then</span> <span class="mi">1</span> 
                    <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))))</span> 
<span class="mi">3</span>

<span class="c1">-- beta reduction</span>

<span class="p">(</span><span class="nf">\</span><span class="n">n</span> <span class="o">-&gt;</span> <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span>
       <span class="kr">then</span> <span class="mi">1</span>
       <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="p">((</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                         <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                         <span class="kr">then</span> <span class="mi">1</span> 
                         <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)))</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span>
<span class="p">)</span> <span class="mi">3</span>

<span class="c1">-- beta reduction</span>

<span class="kr">if</span> <span class="mi">3</span> <span class="o">&lt;=</span> <span class="mi">1</span>
<span class="kr">then</span> <span class="mi">1</span>
<span class="kr">else</span> <span class="mi">3</span> <span class="o">*</span> <span class="p">((</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                 <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                 <span class="kr">then</span> <span class="mi">1</span> 
                 <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)))</span> <span class="mi">2</span><span class="p">)</span>

<span class="c1">-- evaluating if</span>

<span class="mi">3</span> <span class="o">*</span> <span class="p">((</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                 <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                 <span class="kr">then</span> <span class="mi">1</span> 
                 <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)))</span> <span class="mi">2</span><span class="p">)</span>

<span class="c1">-- substituting f</span>

<span class="mi">3</span> <span class="o">*</span> 
<span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
           <span class="kr">then</span> <span class="mi">1</span> 
           <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> 
           <span class="p">(</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                   <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                   <span class="kr">then</span> <span class="mi">1</span> 
                   <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)))</span> 
 <span class="mi">2</span><span class="p">)</span>

<span class="c1">-- beta reduction</span>

<span class="mi">3</span> <span class="o">*</span>
<span class="p">((</span><span class="nf">\</span><span class="n">n</span> <span class="o">-&gt;</span> <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span>
       <span class="kr">then</span> <span class="mi">1</span>
       <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                        <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                        <span class="kr">then</span> <span class="mi">1</span> 
                        <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)))</span> <span class="mi">2</span><span class="p">)</span>

<span class="c1">-- beta reduction</span>

<span class="mi">3</span> <span class="o">*</span>
<span class="kr">if</span> <span class="mi">2</span> <span class="o">&lt;=</span> <span class="mi">1</span>
<span class="kr">then</span> <span class="mi">1</span>
<span class="kr">else</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                 <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                 <span class="kr">then</span> <span class="mi">1</span> 
                 <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> <span class="mi">1</span><span class="p">)</span>

<span class="c1">-- evaluating if</span>

<span class="mi">3</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                <span class="kr">then</span> <span class="mi">1</span> 
                <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> <span class="mi">1</span><span class="p">)</span>

<span class="c1">-- substituting f</span>

<span class="mi">3</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span>
<span class="p">(((</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
     <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
     <span class="kr">then</span> <span class="mi">1</span> 
     <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> 
     <span class="p">(</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
             <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
             <span class="kr">then</span> <span class="mi">1</span> 
             <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))))</span> <span class="mi">1</span><span class="p">)</span>

<span class="c1">-- beta reduction</span>

<span class="mi">3</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span>
<span class="p">((</span><span class="nf">\</span><span class="n">n</span> <span class="o">-&gt;</span> <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span>
       <span class="kr">then</span> <span class="mi">1</span>
       <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="p">((</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                         <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                         <span class="kr">then</span> <span class="mi">1</span> 
                         <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> <span class="p">)</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)))</span> <span class="mi">1</span><span class="p">)</span>

<span class="c1">-- beta reduction</span>

<span class="mi">3</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> 
<span class="kr">if</span> <span class="mi">1</span> <span class="o">&lt;=</span> <span class="mi">1</span>
<span class="kr">then</span> <span class="mi">1</span> <span class="c1">-- hit the base case</span>
<span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="p">((</span><span class="n">fix</span> <span class="p">(</span><span class="nf">\</span><span class="n">fac</span> <span class="n">n</span> <span class="o">-&gt;</span> 
                  <span class="kr">if</span> <span class="n">n</span> <span class="o">&lt;=</span> <span class="mi">1</span> 
                  <span class="kr">then</span> <span class="mi">1</span> 
                  <span class="kr">else</span> <span class="n">n</span> <span class="o">*</span> <span class="n">fac</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> <span class="p">)</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)))</span>

<span class="c1">-- evaluating if</span>

<span class="mi">3</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> <span class="mi">1</span> <span class="o">=</span> <span class="mi">6</span>
</code></pre></div></div>]]></content><author><name></name></author><summary type="html"><![CDATA[In pure lambda calculus (untyped or typed) the fix combinator is used to encode recursion. fix represents the least fixed point of a function f i.e the least defined x for which f x = x. An important concept is that a function need not have a least fixed point. But for those functions which do, the least fixed point is the base case for recursion. An intuition about the least fixed point is :]]></summary></entry><entry><title type="html">Persistent Red Black Trees in Haskell</title><link href="https://abhiroop.github.io/Haskell-Red-Black-Tree/" rel="alternate" type="text/html" title="Persistent Red Black Trees in Haskell" /><published>2017-10-28T00:00:00+00:00</published><updated>2017-10-28T00:00:00+00:00</updated><id>https://abhiroop.github.io/Haskell-Red-Black-Tree</id><content type="html" xml:base="https://abhiroop.github.io/Haskell-Red-Black-Tree/"><![CDATA[<p><img src="/images/RBTree.png" alt="an image alt text" title="RB Tree" /></p>

<p>While Haskell is steadily gaining mainstream adoption in the industry, it still remains one of the most viable languages used as a teaching medium. Even if we strip the fancy type level features in Haskell, algebraic data types and pattern matching are quite expressive enough to represent a lot of ideas.</p>

<p>In this post I will be looking at the construction and operations of Red Black trees. Of special interest here would be the deletion of nodes, as the operation of <code class="language-plaintext highlighter-rouge">delete</code> is inherently opposed to Haskell’s fundamentals of immutability. We will look at these various operations as creating a new version of the tree rather than mutating an existing version, which gives the persistent quality to our data structure.</p>

<p>This post doesn’t use any fancy type level features of Haskell(except at the very end) and assumes only basic familiarity with the language. However the deletion operation is quite involved and requires the reader to be fairly meticulous so as to not miss any of the possible cases.</p>

<p>A red black tree is a type of a binary search tree with the ability to self balance itself. This property of self balancing is highly desirable as a plain binary search tree reduces to <code class="language-plaintext highlighter-rouge">O(n)</code> worst case time complexity for <code class="language-plaintext highlighter-rouge">search</code>, <code class="language-plaintext highlighter-rouge">insert</code> and <code class="language-plaintext highlighter-rouge">delete</code> operations. The balancing nature of red black tree gives it a worst case time complexity of <code class="language-plaintext highlighter-rouge">O(log n)</code>. It is not truly balanced, in the sense that the heights of the various subtrees can differ, but the height of the longest subtree would be a maximum of <code class="language-plaintext highlighter-rouge">2log(n+1)</code>, which effectively gives it a balanced nature.</p>

<p>From a practical perspective, it is not expected of you to code out a fully functional red black tree every other day, but it is helpful to know the implementations and understand the trade-offs made so that performance analysis on some critical code can be made.</p>

<p>Notably, <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/TreeMap.java">TreeMap</a> from the Java collections library is implemented using a Red Black Tree(however it is not a persistent implementation). In practice, most implementations of <code class="language-plaintext highlighter-rouge">maps</code>, <code class="language-plaintext highlighter-rouge">sets</code> and other useful structures are implemented using balanced binary search trees. Hence without any further ado lets jump into the datatypes.</p>

<hr />

<h2 id="defining-the-tree">Defining the tree</h2>

<p>We will start by defining 2 colors red and black and use that as a metadata in our actual tree type.</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kr">data</span> <span class="kt">Color</span> <span class="o">=</span> <span class="kt">R</span> <span class="o">|</span> <span class="kt">B</span> <span class="kr">deriving</span> <span class="kt">Show</span>

<span class="kr">data</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">E</span> <span class="o">|</span> <span class="kt">T</span> <span class="kt">Color</span> <span class="p">(</span><span class="kt">Tree</span> <span class="n">a</span><span class="p">)</span> <span class="n">a</span> <span class="p">(</span><span class="kt">Tree</span> <span class="n">a</span><span class="p">)</span> <span class="kr">deriving</span> <span class="kt">Show</span>
</code></pre></div></div>

<p>Now for a red black tree to be balanced it needs to follow a set of invariants. These invariants technically can be encoded into the Haskell type system but to keep the implementation simpler we define it using functions and verify them at runtime. The invariants are:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>1. No red node has a red parent or a red node has only black children.
2. Every path from the root node to an empty node contains the same number of black nodes.
3. The root and leaves of the tree are black.
</code></pre></div></div>

<p>Take some time to internalize these invariants, as they should not be broken under any circumstances. Throughout the rest of the article, these invariants are referred to a number of times by just referring to their serial number. We can relax some of them locally in certain cases but we make sure some other functions at the global level handles the violation and rectifies it. We are going to start by implementing the <code class="language-plaintext highlighter-rouge">member</code> function, which basically tells us if the element belongs to the tree or not. This is not very different from a regular <code class="language-plaintext highlighter-rouge">BST</code>.</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">member</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="n">member</span> <span class="n">x</span> <span class="kt">E</span>    <span class="o">=</span> <span class="kt">False</span>
<span class="n">member</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kr">_</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span><span class="p">)</span>
  <span class="o">|</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">y</span>     <span class="o">=</span> <span class="n">member</span> <span class="n">x</span> <span class="n">a</span>
  <span class="o">|</span> <span class="n">x</span> <span class="o">==</span> <span class="n">y</span>    <span class="o">=</span> <span class="kt">True</span>
  <span class="o">|</span> <span class="n">otherwise</span> <span class="o">=</span> <span class="n">member</span> <span class="n">x</span> <span class="n">b</span>
</code></pre></div></div>

<p>This should be self explanatory. If we find the element we report <code class="language-plaintext highlighter-rouge">True</code> otherwise we recurse down the left or right subtree.</p>

<hr />

<h2 id="insertion">Insertion</h2>

<p>Now lets look at one of the more involved operations: <code class="language-plaintext highlighter-rouge">insert</code>.</p>

<p>The insertion operation was described by Chris Okasaki in his classic book “Purely Functional Data Structures”. The insertion of a node is colored red in the beginning so as not to affect the height. However this might end up violating the first invariant. To restore the first invariant we have to balance the tree recursively. The function looks something like this:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">insert</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">insert</span> <span class="n">x</span> <span class="n">s</span> <span class="o">=</span> <span class="n">makeBlack</span> <span class="o">$</span> <span class="n">ins</span> <span class="n">s</span>
  <span class="kr">where</span> <span class="n">ins</span> <span class="kt">E</span>  <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="kt">E</span> <span class="n">x</span> <span class="kt">E</span>
        <span class="n">ins</span> <span class="p">(</span><span class="kt">T</span> <span class="n">color</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span><span class="p">)</span>
          <span class="o">|</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">y</span>  <span class="o">=</span> <span class="n">balance</span> <span class="n">color</span> <span class="p">(</span><span class="n">ins</span> <span class="n">a</span><span class="p">)</span> <span class="n">y</span> <span class="n">b</span>
          <span class="o">|</span> <span class="n">x</span> <span class="o">==</span> <span class="n">y</span> <span class="o">=</span> <span class="kt">T</span> <span class="n">color</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span>
          <span class="o">|</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="n">y</span>  <span class="o">=</span> <span class="n">balance</span> <span class="n">color</span> <span class="n">a</span> <span class="n">y</span> <span class="p">(</span><span class="n">ins</span> <span class="n">b</span><span class="p">)</span>
        <span class="n">makeBlack</span> <span class="p">(</span><span class="kt">T</span> <span class="kr">_</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span>
</code></pre></div></div>

<p>The points to notice here in this definition are the functions <code class="language-plaintext highlighter-rouge">makeBlack</code> and <code class="language-plaintext highlighter-rouge">balance.</code> Minus those functions the insertion is exactly similar to an insert in a <code class="language-plaintext highlighter-rouge">BST</code>. The <code class="language-plaintext highlighter-rouge">makeBlack</code> function is also a fairly simple function, given a Node it changes the color of the node to black irrespective of the node’s color. The purpose of this function is that, after applying <code class="language-plaintext highlighter-rouge">balance</code> recursively the final tree might have 2 consecutive red nodes at the top of the tree. This would be a violation of invariant 1. By blackening the root we restore invariant 1 as well as we invariant 2 stays intact, as only the root of the tree gets colored. A more apt name for the function, perhaps should have been <code class="language-plaintext highlighter-rouge">blackenRoot</code>. However this is quite simple.</p>

<p>The trick is now writing and understanding the <code class="language-plaintext highlighter-rouge">balance</code> function. Now we know  that the insertion might have violated invariant 1 and as a result of which it might have created a tree with 2 consecutive red nodes. So all we have to think is, given the original balanced tree with no violations, what are the possible ways in which a red node might have been inserted which breaks the invariant 1. Let us see:
(Due to lack of a red pen I am using a blue pen to represent red nodes. So this is technically a blue black tree. But you get the point):</p>

<p><img src="/images/Insertion.jpg" alt="an image alt text" title="Insertion" /></p>

<p>Now algebraic data types and pattern matching makes it very easy to express each case. So if we write out the tree structure as demonstrated in the figure, the 4 cases would look like this:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">a</span> <span class="n">x</span> <span class="n">b</span><span class="p">)</span> <span class="n">y</span> <span class="n">c</span><span class="p">)</span> <span class="n">z</span> <span class="n">d</span>  
<span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">a</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">b</span> <span class="n">y</span> <span class="n">c</span><span class="p">))</span> <span class="n">z</span> <span class="n">d</span>  
<span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">b</span> <span class="n">y</span> <span class="n">c</span><span class="p">)</span> <span class="n">z</span> <span class="n">d</span><span class="p">)</span>  
<span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">b</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">c</span> <span class="n">z</span> <span class="n">d</span><span class="p">))</span> 
</code></pre></div></div>

<p>So all we have to do is find a way to balance each of the cases. As it is given in the figure, the solution to balancing each of the 4 cases is the exact same. By using this transformation, the tree restores the invariant 1. And invariant 2 stays intact too(Invariant 3 is almost always intact and easiest to enforce). So what happens for the other configurations of the tree? We return the nodes intact. So writing the same in Haskell:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">balance</span> <span class="o">::</span> <span class="kt">Color</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">balance</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">a</span> <span class="n">x</span> <span class="n">b</span><span class="p">)</span> <span class="n">y</span> <span class="n">c</span><span class="p">)</span> <span class="n">z</span> <span class="n">d</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">x</span> <span class="n">b</span><span class="p">)</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">c</span> <span class="n">z</span> <span class="n">d</span><span class="p">)</span>
<span class="n">balance</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">a</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">b</span> <span class="n">y</span> <span class="n">c</span><span class="p">))</span> <span class="n">z</span> <span class="n">d</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">x</span> <span class="n">b</span><span class="p">)</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">c</span> <span class="n">z</span> <span class="n">d</span><span class="p">)</span>
<span class="n">balance</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">b</span> <span class="n">y</span> <span class="n">c</span><span class="p">)</span> <span class="n">z</span> <span class="n">d</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">x</span> <span class="n">b</span><span class="p">)</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">c</span> <span class="n">z</span> <span class="n">d</span><span class="p">)</span>
<span class="n">balance</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">b</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">c</span> <span class="n">z</span> <span class="n">d</span><span class="p">))</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">x</span> <span class="n">b</span><span class="p">)</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">c</span> <span class="n">z</span> <span class="n">d</span><span class="p">)</span>
<span class="n">balance</span> <span class="n">color</span> <span class="n">a</span> <span class="n">x</span> <span class="n">b</span> <span class="o">=</span> <span class="kt">T</span> <span class="n">color</span> <span class="n">a</span> <span class="n">x</span> <span class="n">b</span>
</code></pre></div></div>

<p>Languages like OCaml and SML support something called <code class="language-plaintext highlighter-rouge">OR patterns</code> which make it even simpler to write the function definition where multiple patterns have the same answer. And in fact there is a <a href="https://github.com/ghc-proposals/ghc-proposals/pull/43">GHC proposal</a> in progress about adding OR patterns to Haskell. This implementation is much more readable, expressive and most importantly intuitive compared to any other imperative language implementations of the same idea. Writing a red black tree balancing algorithm is a big deal in such languages but with ADTs and pattern matching it literally begs to follow the diagram given above.</p>

<hr />

<h2 id="deletion">Deletion</h2>

<p>Now moving on to the <code class="language-plaintext highlighter-rouge">delete</code> operation. This one is lot more involved and we will do it step by step.</p>

<p>First a deletion followed by any kind of balancing has the possibility of bubbling a red node to the top, so we need the <code class="language-plaintext highlighter-rouge">makeBlack</code> function that we used in the <code class="language-plaintext highlighter-rouge">insert</code> function, followed by that we can call an auxiliary <code class="language-plaintext highlighter-rouge">del</code> function which will effectively delete the node and balance the tree. In Haskell:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">delete</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">delete</span> <span class="n">x</span> <span class="n">t</span> <span class="o">=</span> <span class="n">makeBlack</span> <span class="o">$</span> <span class="n">del</span> <span class="n">x</span> <span class="n">t</span>
  <span class="kr">where</span> <span class="n">makeBlack</span> <span class="p">(</span><span class="kt">T</span> <span class="kr">_</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span>
</code></pre></div></div>

<p>This is fairly simple. Let us explore the <code class="language-plaintext highlighter-rouge">del</code> function in depth.</p>

<p>So, in case of the <code class="language-plaintext highlighter-rouge">insert</code> function, the balancing of the trees, conveniently unified into a single transformation but that is not the case for delete. The cases of balancing are different but symmetric to each other for the left and right subtrees and we have to handle the 2 cases separately. So we will declare 2 separate function <code class="language-plaintext highlighter-rouge">delL</code> and <code class="language-plaintext highlighter-rouge">delR</code> for the left and right subtree respectively. And what about the case when we actually arrive at the node which we want to delete? In that case we remove that node and <code class="language-plaintext highlighter-rouge">fuse</code> the left and right subtree together. We will look at the <code class="language-plaintext highlighter-rouge">fuse</code> function in detail at a later part of this article. So writing the <code class="language-plaintext highlighter-rouge">del</code> function:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">del</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">del</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kr">_</span> <span class="n">l</span> <span class="n">y</span> <span class="n">r</span><span class="p">)</span>
  <span class="o">|</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">y</span> <span class="o">=</span> <span class="n">delL</span> <span class="n">x</span> <span class="n">t</span>
  <span class="o">|</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="n">y</span> <span class="o">=</span> <span class="n">delR</span> <span class="n">x</span> <span class="n">t</span>
  <span class="o">|</span> <span class="n">otherwise</span> <span class="o">=</span> <span class="n">fuse</span> <span class="n">l</span> <span class="n">r</span>
</code></pre></div></div>

<p>which literally translates from what we described in the earlier paragraph. So now before delving into the <code class="language-plaintext highlighter-rouge">delL</code> and <code class="language-plaintext highlighter-rouge">delR</code> function let us try to think about the balancing first. So for the corresponding <code class="language-plaintext highlighter-rouge">delL</code> and <code class="language-plaintext highlighter-rouge">delR</code> function we will have a <code class="language-plaintext highlighter-rouge">balL</code> and <code class="language-plaintext highlighter-rouge">balR</code> function which balance the left and right subtrees respectively, when one is shorter than the other. The signature of <code class="language-plaintext highlighter-rouge">balL</code> and <code class="language-plaintext highlighter-rouge">balR</code> should be dead simple. Given an unbalanced tree it recolors and balances the trees and outputs a balanced tree.</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">balL</span> <span class="o">::</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>

<span class="n">balR</span> <span class="o">::</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
</code></pre></div></div>

<p>Now as red nodes don’t contribute to the height of the tree and given invariant 1, that red nodes can only have black child, when a deletion of a node occurs, the violation of invariant 2 can happen and the left and right subtree might not be of equal height(Remember only black nodes contribute to the height of the tree.)</p>

<p><code class="language-plaintext highlighter-rouge">balL</code> concerns the cases where deletion has occurred from the left subtree. Hence in <code class="language-plaintext highlighter-rouge">balL</code> we can assume that the left subtree is shorter than the right subtree. What are the possible cases?</p>

<p>Case 1: Root node is black and left subtree root is red</p>

<p><img src="/images/Deletion1.jpg" alt="an image alt text" title="Deletion1" /></p>

<p>Coloring <code class="language-plaintext highlighter-rouge">y</code> red and <code class="language-plaintext highlighter-rouge">x</code> black we increase the height of left subtree by 1 and the height of the right subtree remains unchanged and hence it gets balanced. So translating the diagram to Haskell:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">balL</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="n">y</span> <span class="n">t3</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="n">y</span> <span class="n">t3</span>
</code></pre></div></div>

<p>Case 2: Root node is black, left subtree root is black.</p>

<p>If the left subtree root is black we can’t touch the left tree as it is already shorter and altering the black node would shorten the height more. We need to look at the right subtree.</p>

<p>So depending on the color of the root node there are 2 subcases in this:</p>

<p>Case 2. i. Root node is black, right subtree root is black</p>

<p><img src="/images/Deletion2.jpg" alt="an image alt text" title="Deletion2" /></p>

<p>Coloring <code class="language-plaintext highlighter-rouge">z</code> as red reduces the height of the right subtree by 1 but it might end up violating invariant 1. In which case we have to call the old <code class="language-plaintext highlighter-rouge">balance</code> function that we used in case of invariant 1 violation. We can define a simple helper <code class="language-plaintext highlighter-rouge">balance'</code> which takes the entire node instead of passing the left subtree, right subtree, color etc. So this branch becomes:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">balL</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t2</span> <span class="n">z</span> <span class="n">t3</span><span class="p">))</span> <span class="o">=</span> <span class="n">balance'</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t2</span> <span class="n">z</span> <span class="n">t3</span><span class="p">))</span>
</code></pre></div></div>

<p>Case 2. ii. Root node is black, right subtree root is red..</p>

<p><img src="/images/Deletion3.jpg" alt="an image alt text" title="Deletion3" /></p>

<p>This is the most involved case. Here after rebalancing the tree the only issue is <code class="language-plaintext highlighter-rouge">t4</code>’s  height is still <code class="language-plaintext highlighter-rouge">n+1</code> which can be resolved by coloring its root red. However we need to rebalance the subtree of <code class="language-plaintext highlighter-rouge">(t3 z t4)</code> to resolve any violations of invariant 1.</p>

<p>Hence the code translates to:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">balL</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t2</span> <span class="n">u</span> <span class="n">t3</span><span class="p">)</span> <span class="n">z</span> <span class="n">t4</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">l</span> <span class="n">value</span> <span class="n">r</span><span class="p">)))</span> <span class="o">=</span>
  <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="n">u</span> <span class="p">(</span><span class="n">balance'</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t3</span> <span class="n">z</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">l</span> <span class="n">value</span> <span class="n">r</span><span class="p">)))</span>
</code></pre></div></div>

<p>This concludes our cases for <code class="language-plaintext highlighter-rouge">balL</code>. And <code class="language-plaintext highlighter-rouge">balR</code> is exactly symmetric to the cases of <code class="language-plaintext highlighter-rouge">balL</code>, except now the right subtree would be shorter. I am adding the code for that part just for reference.</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">balR</span> <span class="o">::</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">balR</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t2</span> <span class="n">x</span> <span class="n">t3</span><span class="p">))</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t2</span> <span class="n">x</span> <span class="n">t3</span><span class="p">)</span>
<span class="n">balR</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">z</span> <span class="n">t2</span><span class="p">)</span> <span class="n">y</span> <span class="n">t3</span><span class="p">)</span> <span class="o">=</span> <span class="n">balance'</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">z</span> <span class="n">t2</span><span class="p">)</span> <span class="n">y</span> <span class="n">t3</span><span class="p">)</span>
<span class="n">balR</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">l</span> <span class="n">value</span> <span class="n">r</span><span class="p">)</span> <span class="n">z</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t2</span> <span class="n">u</span> <span class="n">t3</span><span class="p">))</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span> <span class="o">=</span>
  <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="n">balance'</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">l</span> <span class="n">value</span> <span class="n">r</span><span class="p">)</span> <span class="n">z</span> <span class="n">t2</span><span class="p">))</span> <span class="n">u</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t3</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span>
</code></pre></div></div>

<p>Now that we have <code class="language-plaintext highlighter-rouge">balL</code> and <code class="language-plaintext highlighter-rouge">balR</code> sorted we can start thinking about the <code class="language-plaintext highlighter-rouge">delL</code> and <code class="language-plaintext highlighter-rouge">delR</code> function. The signature of these functions are simple enough, given a node and tree it returns a tree with that node removed:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">delL</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>

<span class="n">delR</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
</code></pre></div></div>

<p>So if you look at the cases of balancing above, balancing is required only when there is a black root node, in case the node is red we just recurse down the path. So the red case is simple:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">delL</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="n">del</span> <span class="n">x</span> <span class="n">t1</span><span class="p">)</span> <span class="n">y</span> <span class="n">t2</span>

<span class="n">delR</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="n">del</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span>
</code></pre></div></div>

<p>Now in case the root node is black we just balance the entire tree after the delete:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">delL</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="o">=</span> <span class="n">balL</span> <span class="o">$</span> <span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="n">del</span> <span class="n">x</span> <span class="n">t1</span><span class="p">)</span> <span class="n">y</span> <span class="n">t2</span>

<span class="n">delR</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="o">=</span> <span class="n">balR</span> <span class="o">$</span> <span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="n">del</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span>
</code></pre></div></div>

<p>And thats all, these are the possible cases of <code class="language-plaintext highlighter-rouge">delL</code> and <code class="language-plaintext highlighter-rouge">delR</code>.</p>

<p>So moving to the final case of the deletion, which is fusing the 2 subtrees when the node is found.</p>

<p>The cases are really simple when the color of 2 roots are different i.e. black and red or red and black.</p>

<p><img src="/images/Fuse1.jpg" alt="an image alt text" title="Fuse1" /></p>

<p>which again translates very easily to the code:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">fuse</span> <span class="n">t1</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="kr">_</span> <span class="kr">_</span> <span class="kr">_</span><span class="p">)</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t3</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="n">fuse</span> <span class="n">t1</span> <span class="n">t3</span><span class="p">)</span> <span class="n">y</span> <span class="n">t4</span>
<span class="n">fuse</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="n">t3</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="kr">_</span> <span class="kr">_</span> <span class="kr">_</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="p">(</span><span class="n">fuse</span> <span class="n">t2</span> <span class="n">t3</span><span class="p">)</span>
</code></pre></div></div>

<p>The difficulty arises when the color of the roots are same.</p>

<p>Consider the case when both the roots are red:</p>

<p><img src="/images/Fuse2.jpg" alt="an image alt text" title="Fuse2" /></p>

<p>The transformation above are captured in the code below:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">fuse</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t3</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span>  <span class="o">=</span>
  <span class="kr">let</span> <span class="n">s</span> <span class="o">=</span> <span class="n">fuse</span> <span class="n">t2</span> <span class="n">t3</span>
  <span class="kr">in</span> <span class="kr">case</span> <span class="n">s</span> <span class="kr">of</span>
       <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">s1</span> <span class="n">z</span> <span class="n">s2</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">s1</span><span class="p">)</span> <span class="n">z</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">s2</span> <span class="n">y</span> <span class="n">t4</span><span class="p">))</span>
       <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="kr">_</span> <span class="kr">_</span> <span class="kr">_</span><span class="p">)</span>   <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">s</span> <span class="n">y</span> <span class="n">t4</span><span class="p">))</span>
</code></pre></div></div>

<p>Any violation of invariant 1 is handled by the balance functions at the upper layers of the recursion.</p>

<p>Similarly the case when both roots are black:</p>

<p><img src="/images/Fuse3.jpg" alt="an image alt text" title="Fuse3" /></p>

<p>if the top node of <code class="language-plaintext highlighter-rouge">s</code> is black we need to use <code class="language-plaintext highlighter-rouge">balL</code> because the height of the right subtree has increased. And if it is red we follow the transformation given in the figure above:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">fuse</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t3</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span>  <span class="o">=</span>
  <span class="kr">let</span> <span class="n">s</span> <span class="o">=</span> <span class="n">fuse</span> <span class="n">t2</span> <span class="n">t3</span>
  <span class="kr">in</span> <span class="kr">case</span> <span class="n">s</span> <span class="kr">of</span>
       <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">s1</span> <span class="n">z</span> <span class="n">s2</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">s1</span><span class="p">)</span> <span class="n">z</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">s2</span> <span class="n">y</span> <span class="n">t4</span><span class="p">))</span> <span class="c1">-- consfusing case</span>
       <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">s1</span> <span class="n">z</span> <span class="n">s2</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">balL</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">s</span> <span class="n">y</span> <span class="n">t4</span><span class="p">))</span>
</code></pre></div></div>

<p>Thats all. Putting together the entire code for delete we have:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">delete</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">delete</span> <span class="n">x</span> <span class="n">t</span> <span class="o">=</span> <span class="n">makeBlack</span> <span class="o">$</span> <span class="n">del</span> <span class="n">x</span> <span class="n">t</span>
  <span class="kr">where</span> <span class="n">makeBlack</span> <span class="p">(</span><span class="kt">T</span> <span class="kr">_</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">B</span> <span class="n">a</span> <span class="n">y</span> <span class="n">b</span>
        <span class="n">makeBlack</span> <span class="kt">E</span>           <span class="o">=</span> <span class="kt">E</span>

<span class="n">del</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">del</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kr">_</span> <span class="n">l</span> <span class="n">y</span> <span class="n">r</span><span class="p">)</span>
  <span class="o">|</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">y</span> <span class="o">=</span> <span class="n">delL</span> <span class="n">x</span> <span class="n">t</span>
  <span class="o">|</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="n">y</span> <span class="o">=</span> <span class="n">delR</span> <span class="n">x</span> <span class="n">t</span>
  <span class="o">|</span> <span class="n">otherwise</span> <span class="o">=</span> <span class="n">fuse</span> <span class="n">l</span> <span class="n">r</span>

<span class="n">delL</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">delL</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="o">=</span> <span class="n">balL</span> <span class="o">$</span> <span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="n">del</span> <span class="n">x</span> <span class="n">t1</span><span class="p">)</span> <span class="n">y</span> <span class="n">t2</span>
<span class="n">delL</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="n">del</span> <span class="n">x</span> <span class="n">t1</span><span class="p">)</span> <span class="n">y</span> <span class="n">t2</span>

<span class="n">balL</span> <span class="o">::</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">balL</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="n">y</span> <span class="n">t3</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="n">y</span> <span class="n">t3</span>
<span class="n">balL</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t2</span> <span class="n">z</span> <span class="n">t3</span><span class="p">))</span> <span class="o">=</span> <span class="n">balance'</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t2</span> <span class="n">z</span> <span class="n">t3</span><span class="p">))</span>
<span class="n">balL</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t2</span> <span class="n">u</span> <span class="n">t3</span><span class="p">)</span> <span class="n">z</span> <span class="n">t4</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">l</span> <span class="n">value</span> <span class="n">r</span><span class="p">)))</span> <span class="o">=</span>
  <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="n">u</span> <span class="p">(</span><span class="n">balance'</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t3</span> <span class="n">z</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">l</span> <span class="n">value</span> <span class="n">r</span><span class="p">)))</span>

<span class="n">delR</span> <span class="o">::</span> <span class="p">(</span><span class="kt">Ord</span> <span class="n">a</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">delR</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="o">=</span> <span class="n">balR</span> <span class="o">$</span> <span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="n">del</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span>
<span class="n">delR</span> <span class="n">x</span> <span class="n">t</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">y</span> <span class="n">t2</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="n">del</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span>

<span class="n">balR</span> <span class="o">::</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">balR</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t2</span> <span class="n">x</span> <span class="n">t3</span><span class="p">))</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">y</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t2</span> <span class="n">x</span> <span class="n">t3</span><span class="p">)</span>
<span class="n">balR</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">z</span> <span class="n">t2</span><span class="p">)</span> <span class="n">y</span> <span class="n">t3</span><span class="p">)</span> <span class="o">=</span> <span class="n">balance'</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">z</span> <span class="n">t2</span><span class="p">)</span> <span class="n">y</span> <span class="n">t3</span><span class="p">)</span>
<span class="n">balR</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">l</span> <span class="n">value</span> <span class="n">r</span><span class="p">)</span> <span class="n">z</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t2</span> <span class="n">u</span> <span class="n">t3</span><span class="p">))</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span> <span class="o">=</span>
  <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="n">balance'</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">l</span> <span class="n">value</span> <span class="n">r</span><span class="p">)</span> <span class="n">z</span> <span class="n">t2</span><span class="p">))</span> <span class="n">u</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t3</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span>

<span class="n">fuse</span> <span class="o">::</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="n">a</span>
<span class="n">fuse</span> <span class="kt">E</span> <span class="n">t</span> <span class="o">=</span> <span class="n">t</span>
<span class="n">fuse</span> <span class="n">t</span> <span class="kt">E</span> <span class="o">=</span> <span class="n">t</span>
<span class="n">fuse</span> <span class="n">t1</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="kr">_</span> <span class="kr">_</span> <span class="kr">_</span><span class="p">)</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t3</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="n">fuse</span> <span class="n">t1</span> <span class="n">t3</span><span class="p">)</span> <span class="n">y</span> <span class="n">t4</span>
<span class="n">fuse</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="n">t3</span><span class="o">@</span><span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="kr">_</span> <span class="kr">_</span> <span class="kr">_</span><span class="p">)</span> <span class="o">=</span> <span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="p">(</span><span class="n">fuse</span> <span class="n">t2</span> <span class="n">t3</span><span class="p">)</span>
<span class="n">fuse</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t3</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span>  <span class="o">=</span>
  <span class="kr">let</span> <span class="n">s</span> <span class="o">=</span> <span class="n">fuse</span> <span class="n">t2</span> <span class="n">t3</span>
  <span class="kr">in</span> <span class="kr">case</span> <span class="n">s</span> <span class="kr">of</span>
       <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">s1</span> <span class="n">z</span> <span class="n">s2</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">s1</span><span class="p">)</span> <span class="n">z</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">s2</span> <span class="n">y</span> <span class="n">t4</span><span class="p">))</span>
       <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="kr">_</span> <span class="kr">_</span> <span class="kr">_</span><span class="p">)</span>   <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">t1</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">s</span> <span class="n">y</span> <span class="n">t4</span><span class="p">))</span>
<span class="n">fuse</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">t2</span><span class="p">)</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t3</span> <span class="n">y</span> <span class="n">t4</span><span class="p">)</span>  <span class="o">=</span>
  <span class="kr">let</span> <span class="n">s</span> <span class="o">=</span> <span class="n">fuse</span> <span class="n">t2</span> <span class="n">t3</span>
  <span class="kr">in</span> <span class="kr">case</span> <span class="n">s</span> <span class="kr">of</span>
       <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="n">s1</span> <span class="n">z</span> <span class="n">s2</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">R</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">x</span> <span class="n">s1</span><span class="p">)</span> <span class="n">z</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">s2</span> <span class="n">y</span> <span class="n">t4</span><span class="p">))</span>
       <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">s1</span> <span class="n">z</span> <span class="n">s2</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">balL</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">t1</span> <span class="n">x</span> <span class="p">(</span><span class="kt">T</span> <span class="kt">B</span> <span class="n">s</span> <span class="n">y</span> <span class="n">t4</span><span class="p">))</span>
</code></pre></div></div>

<p>The entire code is available <a href="https://github.com/Abhiroop/okasaki/blob/master/app/RedBlackTree.hs">here</a>.</p>

<p>The above algorithm was first devised by Stefan Kahrs from University of Kent (Thanks to Dr. Venanzio Capretta for explaining the cases).There is an alternate red black deletion algorithm devised by Matt Might which uses auxiliary colors to simplify the cases. He has blogged about it in great detail <a href="http://matt.might.net/articles/red-black-delete/">here</a>.</p>

<p>While the case of deletion is quite involved, if you take a look at the entire code for the red black tree, its hardly 100 lines of Haskell. And it is persistent in nature by default. It would be much more difficult designing a thread safe red black tree in any other imperative language. Most importantly, when teaching someone data structures for the first time, the syntax never intrudes in the way of the logic of the program. I have been working on this as part of a course on Advanced Data Structures and Algorithms that I am taking, and using Haskell has made understanding the logic dead simple.</p>

<hr />

<h2 id="type-level-trees">Type Level Trees</h2>

<p>In addition if we want, we can encode these invariants at the type level. Writing an entire type level Red Black Tree would require another post.</p>

<p>Stephanie Weirich from University of Pennsylvania has done lots of work in that area and there are many of her <a href="https://www.youtube.com/watch?v=n-b1PYbRUOY">presentations available online</a>.</p>

<p>As a small taste of what we can do, we can encode the simple invariant that “The difference in height between the 2 subtrees is maximum 1” to ensure that a <code class="language-plaintext highlighter-rouge">BST</code> is balanced. Fot that, what we need, is to raise a number to the type level and say that “if the height of left subtree is <code class="language-plaintext highlighter-rouge">n</code> then the height of the right subtree is either <code class="language-plaintext highlighter-rouge">n + 1</code> or <code class="language-plaintext highlighter-rouge">n - 1</code> and vice versa”.</p>

<p>We can represent numbers simply using Peano numerals. But first we need a couple of language extensions:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">{-# LANGUAGE GADTs, DataKinds  #-}</span>
</code></pre></div></div>

<p>Followed by that define the Peano numerals and capture the invariant in the tree:</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">data</span> <span class="kt">T</span> <span class="n">n</span> <span class="n">a</span> <span class="o">=</span> <span class="kt">NodeR</span> <span class="p">(</span><span class="kt">Tree</span> <span class="n">n</span> <span class="n">a</span><span class="p">)</span> <span class="n">a</span> <span class="p">(</span><span class="kt">Tree</span> <span class="p">(</span><span class="kt">Succ</span> <span class="n">n</span><span class="p">)</span> <span class="n">a</span><span class="p">)</span> <span class="c1">-- right subtree has height + 1</span>
           <span class="o">|</span> <span class="kt">NodeL</span> <span class="p">(</span><span class="kt">Tree</span> <span class="p">(</span><span class="kt">Succ</span> <span class="n">n</span><span class="p">)</span> <span class="n">a</span><span class="p">)</span> <span class="n">a</span> <span class="p">(</span><span class="kt">Tree</span> <span class="n">n</span> <span class="n">a</span><span class="p">)</span> <span class="c1">-- left subtree has height + 1</span>
           <span class="o">|</span> <span class="kt">Node</span> <span class="p">(</span><span class="kt">Tree</span> <span class="n">n</span> <span class="n">a</span><span class="p">)</span> <span class="n">a</span> <span class="p">(</span><span class="kt">Tree</span> <span class="n">n</span> <span class="n">a</span><span class="p">)</span>     <span class="c1">-- both subtrees are of equal height</span>

<span class="kr">data</span> <span class="kt">Tree</span> <span class="n">n</span> <span class="n">a</span> <span class="kr">where</span>
  <span class="kt">Branch</span> <span class="o">::</span> <span class="kt">T</span> <span class="n">n</span> <span class="n">a</span> <span class="o">-&gt;</span> <span class="kt">Tree</span> <span class="p">(</span><span class="kt">Succ</span> <span class="n">n</span><span class="p">)</span> <span class="n">a</span>
  <span class="kt">Leaf</span> <span class="o">::</span> <span class="kt">Tree</span> <span class="kt">Zero</span> <span class="n">a</span>
</code></pre></div></div>

<p>We can also raise relational operators to the type level using the <code class="language-plaintext highlighter-rouge">singletons</code> package by Richard Eisenberg and capture further invariants in the type level. But that will be material for another post. Till then enjoy writing more Haskell :)</p>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry></feed>