added doi
This commit is contained in:
91
dist/Coding/Haskell/Code Snippets/Morphisms.html
vendored
91
dist/Coding/Haskell/Code Snippets/Morphisms.html
vendored
@ -2,7 +2,7 @@
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||||
|
||||
<meta charset="utf-8">
|
||||
<meta name="generator" content="quarto-1.7.23">
|
||||
<meta name="generator" content="quarto-1.8.17">
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||||
|
||||
@ -68,17 +68,19 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<meta name="quarto:offset" content="../../../">
|
||||
<script src="../../../site_libs/quarto-html/quarto.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/tabsets/tabsets.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/axe/axe-check.js" type="module"></script>
|
||||
<script src="../../../site_libs/quarto-html/popper.min.js"></script>
|
||||
<script src="../../../site_libs/quarto-html/tippy.umd.min.js"></script>
|
||||
<script src="../../../site_libs/quarto-html/anchor.min.js"></script>
|
||||
<link href="../../../site_libs/quarto-html/tippy.css" rel="stylesheet">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-dark-2c84ecb840a13f4c7993f9e5648f0c14.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-6cf5824034cebd0380a5b9c74c43f006.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-ff1c8a91a872873ab9706226229f9e30.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-dark-04b38c9dba5d7c0f1fe2b621b39e7e62.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
|
||||
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting-ff1c8a91a872873ab9706226229f9e30.css" rel="stylesheet" class="quarto-color-scheme-extra" id="quarto-text-highlighting-styles">
|
||||
<script src="../../../site_libs/bootstrap/bootstrap.min.js"></script>
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-ec71cb1e120c0dd41819aca960e74e38.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-dark-716ed94a23403968eaa6fe981e0cbf91.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-ec71cb1e120c0dd41819aca960e74e38.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-9b56b6674ace31b1957ea5f5ef42286f.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-dark-3c8015a3c2181875b3e8a350495bd83c.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
|
||||
<link href="../../../site_libs/bootstrap/bootstrap-9b56b6674ace31b1957ea5f5ef42286f.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
|
||||
<script id="quarto-search-options" type="application/json">{
|
||||
"location": "navbar",
|
||||
"copy-button": false,
|
||||
@ -133,7 +135,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
toggleBodyColorMode(bsSheetEl);
|
||||
}
|
||||
}
|
||||
window.setColorSchemeToggle = (alternate) => {
|
||||
const setColorSchemeToggle = (alternate) => {
|
||||
const toggles = window.document.querySelectorAll('.quarto-color-scheme-toggle');
|
||||
for (let i=0; i < toggles.length; i++) {
|
||||
const toggle = toggles[i];
|
||||
@ -166,7 +168,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
}
|
||||
manageTransitions('#quarto-margin-sidebar .nav-link', true);
|
||||
// Switch the toggles
|
||||
window.setColorSchemeToggle(alternate)
|
||||
setColorSchemeToggle(alternate)
|
||||
// Hack to workaround the fact that safari doesn't
|
||||
// properly recolor the scrollbar when toggling (#1455)
|
||||
if (navigator.userAgent.indexOf('Safari') > 0 && navigator.userAgent.indexOf('Chrome') == -1) {
|
||||
@ -206,7 +208,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
const isFileUrl = () => {
|
||||
return window.location.protocol === 'file:';
|
||||
}
|
||||
window.hasAlternateSentinel = () => {
|
||||
const hasAlternateSentinel = () => {
|
||||
let styleSentinel = getColorSchemeSentinel();
|
||||
if (styleSentinel !== null) {
|
||||
return styleSentinel === "alternate";
|
||||
@ -234,7 +236,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
const baseTheme = document.querySelector('#giscus-base-theme')?.value ?? 'light';
|
||||
const alternateTheme = document.querySelector('#giscus-alt-theme')?.value ?? 'dark';
|
||||
let newTheme = '';
|
||||
if(darkModeDefault) {
|
||||
if(authorPrefersDark) {
|
||||
newTheme = isAlternate ? baseTheme : alternateTheme;
|
||||
} else {
|
||||
newTheme = isAlternate ? alternateTheme : baseTheme;
|
||||
@ -257,17 +259,20 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
changeGiscusTheme();
|
||||
}
|
||||
};
|
||||
const authorPrefersDark = false;
|
||||
const queryPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const darkModeDefault = queryPrefersDark.matches;
|
||||
document.querySelector('link.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
document.querySelector('link#quarto-text-highlighting-styles.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
document.querySelector('link#quarto-bootstrap.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
let localAlternateSentinel = darkModeDefault ? 'alternate' : 'default';
|
||||
// Dark / light mode switch
|
||||
window.quartoToggleColorScheme = () => {
|
||||
// Read the current dark / light value
|
||||
let toAlternate = !window.hasAlternateSentinel();
|
||||
let toAlternate = !hasAlternateSentinel();
|
||||
toggleColorMode(toAlternate);
|
||||
setStyleSentinel(toAlternate);
|
||||
toggleGiscusIfUsed(toAlternate, darkModeDefault);
|
||||
window.dispatchEvent(new Event('resize'));
|
||||
};
|
||||
queryPrefersDark.addEventListener("change", e => {
|
||||
if(window.localStorage.getItem("quarto-color-scheme") !== null)
|
||||
@ -278,7 +283,7 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
toggleGiscusIfUsed(alternate, darkModeDefault);
|
||||
});
|
||||
// Switch to dark mode if need be
|
||||
if (window.hasAlternateSentinel()) {
|
||||
if (hasAlternateSentinel()) {
|
||||
toggleColorMode(true);
|
||||
} else {
|
||||
toggleColorMode(false);
|
||||
@ -290,6 +295,8 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
<nav class="navbar navbar-expand-lg " data-bs-theme="dark">
|
||||
<div class="navbar-container container-fluid">
|
||||
<div class="navbar-brand-container mx-auto">
|
||||
<a href="../../../index.html" class="navbar-brand navbar-brand-logo">
|
||||
</a>
|
||||
<a class="navbar-brand" href="../../../index.html">
|
||||
<span class="navbar-title">Nicole Dresselhaus</span>
|
||||
</a>
|
||||
@ -301,12 +308,12 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
<div class="collapse navbar-collapse" id="navbarCollapse">
|
||||
<ul class="navbar-nav navbar-nav-scroll me-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="../../../index.html"> <i class="bi bi-house" role="img">
|
||||
<a class="nav-link active" href="../../../index.html" aria-current="page"> <i class="bi bi-house" role="img">
|
||||
</i>
|
||||
<span class="menu-text">Home</span></a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" href="../../../About/index.html" aria-current="page"> <i class="bi bi-file-person" role="img">
|
||||
<a class="nav-link" href="../../../About/index.html"> <i class="bi bi-file-person" role="img">
|
||||
</i>
|
||||
<span class="menu-text">About</span></a>
|
||||
</li>
|
||||
@ -344,6 +351,10 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
|
||||
<!-- sidebar -->
|
||||
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation docked overflow-auto">
|
||||
<div class="pt-lg-2 mt-2 text-left sidebar-header">
|
||||
<a href="../../../index.html" class="sidebar-logo-link">
|
||||
</a>
|
||||
</div>
|
||||
<div class="sidebar-menu-container">
|
||||
<ul class="list-unstyled mt-1">
|
||||
<li class="sidebar-item sidebar-item-section">
|
||||
@ -387,12 +398,6 @@ Knowing that this might sound daunting to many dabbling Haskellers (like I am),
|
||||
<a href="../../../Writing/Obsidian-RAG.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text">RAG für eine Obsidian-Wissensdatenbank: Technische Ansätze</span></a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="sidebar-item">
|
||||
<div class="sidebar-item-container">
|
||||
<a href="../../../Obsidian-Articles/Artikelbewertung durch LLM.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text">Artikelbewertung durch ein Large Language Model (LLM) - Ein Beispiel aus der Praxis</span></a>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
@ -679,45 +684,45 @@ Note
|
||||
<p>For those who need a refresher: MergeSort works by creating a balanced binary tree from the input list and directly collapsing it back into itself while treating the children as sorted lists and merging these with an O(n) algorithm.</p>
|
||||
<hr>
|
||||
<p>First the usual prelude:</p>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# LANGUAGE DeriveFunctor #-}</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# LANGUAGE DeriveFunctor #-}</span></span>
|
||||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="ot">{-# LANGUAGE TypeFamilies #-}</span></span>
|
||||
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.Functor.Foldable</span></span>
|
||||
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.List</span> (splitAt, unfoldr)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.List</span> (splitAt, unfoldr)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>We will use a binary tree like this. Note that there is no explicit recursion used, but <code>NodeF</code> has two <em>holes</em>. These will eventually filled later.</p>
|
||||
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">TreeF</span> c f <span class="ot">=</span> <span class="dt">EmptyF</span> <span class="op">|</span> <span class="dt">LeafF</span> c <span class="op">|</span> <span class="dt">NodeF</span> f f</span>
|
||||
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Functor</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">TreeF</span> c f <span class="ot">=</span> <span class="dt">EmptyF</span> <span class="op">|</span> <span class="dt">LeafF</span> c <span class="op">|</span> <span class="dt">NodeF</span> f f</span>
|
||||
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Functor</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>Aside: We could use this as a <em>normal</em> binary tree by wrapping it in <code>Fix</code>: <code>type Tree a = Fix (TreeF a)</code> But this would require us to write our tree like <code>Fix (NodeF (Fix (LeafF 'l')) (Fix (LeafF 'r')))</code> which would get tedious fast. Luckily Edward build a much better way to do this into <em>recursion-schemes</em>. I will touch on this later.</p>
|
||||
<hr>
|
||||
<p>Without further ado we start to write a Coalgebra, which in my book is just a scary name for “function that is used to construct datastructures”.</p>
|
||||
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ot">unflatten ::</span> [a] <span class="ot">-></span> <span class="dt">TreeF</span> a [a]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ot">unflatten ::</span> [a] <span class="ot">-></span> <span class="dt">TreeF</span> a [a]</span>
|
||||
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>unflatten ( []) <span class="ot">=</span> <span class="dt">EmptyF</span></span>
|
||||
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>unflatten (x<span class="op">:</span>[]) <span class="ot">=</span> <span class="dt">LeafF</span> x</span>
|
||||
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>unflatten ( xs) <span class="ot">=</span> <span class="dt">NodeF</span> l r <span class="kw">where</span> (l,r) <span class="ot">=</span> <span class="fu">splitAt</span> (<span class="fu">length</span> xs <span class="ot">`div`</span> <span class="dv">2</span>) xs</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>unflatten ( xs) <span class="ot">=</span> <span class="dt">NodeF</span> l r <span class="kw">where</span> (l,r) <span class="ot">=</span> <span class="fu">splitAt</span> (<span class="fu">length</span> xs <span class="ot">`div`</span> <span class="dv">2</span>) xs</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>From the type signature it’s immediately obvious, that we take a list of ’a’s and use it to create a part of our tree.</p>
|
||||
<p>The nice thing is that due to the fact that we haven’t commited to a type in our tree nodes we can just put lists in there.</p>
|
||||
<hr>
|
||||
<p>Aside: At this point we could use this Coalgebra to construct (unsorted) binary trees from lists:</p>
|
||||
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>example1 <span class="ot">=</span> ana unflatten [<span class="dv">1</span>,<span class="dv">3</span>] <span class="op">==</span> <span class="dt">Fix</span> (<span class="dt">NodeF</span> (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">1</span>)) (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">3</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>example1 <span class="ot">=</span> ana unflatten [<span class="dv">1</span>,<span class="dv">3</span>] <span class="op">==</span> <span class="dt">Fix</span> (<span class="dt">NodeF</span> (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">1</span>)) (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">3</span>)))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>On to our sorting, tree-collapsing Algebra. Which again is just a creepy word for “function that is used to deconstruct datastructures”.</p>
|
||||
<p>The function <code>mergeList</code> is defined below and just merges two sorted lists into one sorted list in O(n), I would probably take this from the <code>ordlist</code> package if I were to implement this <em>for real</em>.</p>
|
||||
<p>Again we see that we can just construct our sorted output list from a <code>TreeF</code> that apparently contains just lists.</p>
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">flatten ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> <span class="dt">TreeF</span> a [a] <span class="ot">-></span> [a]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">flatten ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> <span class="dt">TreeF</span> a [a] <span class="ot">-></span> [a]</span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>flatten <span class="dt">EmptyF</span> <span class="ot">=</span> []</span>
|
||||
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>flatten (<span class="dt">LeafF</span> c) <span class="ot">=</span> [c]</span>
|
||||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>flatten (<span class="dt">NodeF</span> l r) <span class="ot">=</span> mergeLists l r</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>flatten (<span class="dt">NodeF</span> l r) <span class="ot">=</span> mergeLists l r</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>Aside: We could use a Coalgebra to deconstruct trees:</p>
|
||||
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>example2 <span class="ot">=</span> cata flatten (<span class="dt">Fix</span> (<span class="dt">NodeF</span> (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">3</span>)) (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">1</span>)))) <span class="op">==</span> [<span class="dv">1</span>,<span class="dv">3</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>example2 <span class="ot">=</span> cata flatten (<span class="dt">Fix</span> (<span class="dt">NodeF</span> (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">3</span>)) (<span class="dt">Fix</span> (<span class="dt">LeafF</span> <span class="dv">1</span>)))) <span class="op">==</span> [<span class="dv">1</span>,<span class="dv">3</span>]</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>Now we just combine the Coalgebra and the Algebra with one from the functions from Edwards <code>recursion-schemes</code> library:</p>
|
||||
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mergeSort ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mergeSort ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a]</span>
|
||||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>mergeSort <span class="ot">=</span> hylo flatten unflatten</span>
|
||||
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>example3 <span class="ot">=</span> mergeSort [<span class="dv">5</span>,<span class="dv">2</span>,<span class="dv">7</span>,<span class="dv">9</span>,<span class="dv">1</span>,<span class="dv">4</span>] <span class="op">==</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">7</span>,<span class="dv">9</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>example3 <span class="ot">=</span> mergeSort [<span class="dv">5</span>,<span class="dv">2</span>,<span class="dv">7</span>,<span class="dv">9</span>,<span class="dv">1</span>,<span class="dv">4</span>] <span class="op">==</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">7</span>,<span class="dv">9</span>]</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<hr>
|
||||
<p>What have we gained?</p>
|
||||
<p>We have implemented a MergeSort variant in 9 lines of code, not counting the <code>mergeLists</code> function below. Not bad, but <a href="http://en.literateprograms.org/Merge_sort_(Haskell)">this implementation</a> is not much longer.</p>
|
||||
@ -726,7 +731,7 @@ Note
|
||||
<hr>
|
||||
<p>At this point I wasn’t sure if this was useful or remotely applicable. Telling someone “I spend a whole weekend learning about Hylomorphism” isn’t something the cool developer kids do.</p>
|
||||
<p>It appeared to me that maybe I should have a look at the Core to see what the compiler finally comes up with (edited for brevity):</p>
|
||||
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ot"> mergeSort ::</span> [<span class="dt">Integer</span>] <span class="ot">-></span> [<span class="dt">Integer</span>]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ot"> mergeSort ::</span> [<span class="dt">Integer</span>] <span class="ot">-></span> [<span class="dt">Integer</span>]</span>
|
||||
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> mergeSort <span class="ot">=</span></span>
|
||||
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> \ (<span class="ot">x ::</span> [<span class="dt">Integer</span>]) <span class="ot">-></span></span>
|
||||
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> x <span class="kw">of</span> wild {</span>
|
||||
@ -753,17 +758,17 @@ Note
|
||||
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> p <span class="kw">of</span> _ { (ds1, y) <span class="ot">-></span> mergeSort y }))</span>
|
||||
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a> }</span>
|
||||
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a> }</span>
|
||||
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a> end <span class="dt">Rec</span> }</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a> end <span class="dt">Rec</span> }</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>While I am not really competent in reading Core and this is actually the first time I bothered to try, it is immediately obvious that there is no trace of any intermediate tree structure.</p>
|
||||
<p>This is when it struck me. I was dazzled and amazed. And am still. Although we are writing our algorithm as if we are working on a real tree structure the library and the compiler are able to just remove the whole intermediate step.</p>
|
||||
<hr>
|
||||
<p>Aftermath:</p>
|
||||
<p>In the beginning I promised a way to work on non-functor data structures. Actually that was how I began to work with the <code>recursion-schemes</code> library.</p>
|
||||
<p>We are able to create a ‘normal’ version of our tree from above:</p>
|
||||
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Tree</span> c <span class="ot">=</span> <span class="dt">Empty</span> <span class="op">|</span> <span class="dt">Leaf</span> c <span class="op">|</span> <span class="dt">Node</span> (<span class="dt">Tree</span> c) (<span class="dt">Tree</span> c)</span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Tree</span> c <span class="ot">=</span> <span class="dt">Empty</span> <span class="op">|</span> <span class="dt">Leaf</span> c <span class="op">|</span> <span class="dt">Node</span> (<span class="dt">Tree</span> c) (<span class="dt">Tree</span> c)</span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>But we can not use this directly with our (Co-)Algebras. Luckily Edward build a little bit of type magic into the library:</p>
|
||||
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Base</span> (<span class="dt">Tree</span> c) <span class="ot">=</span> (<span class="dt">TreeF</span> c)</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Base</span> (<span class="dt">Tree</span> c) <span class="ot">=</span> (<span class="dt">TreeF</span> c)</span>
|
||||
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Unfoldable</span> (<span class="dt">Tree</span> c) <span class="kw">where</span></span>
|
||||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> embed <span class="dt">EmptyF</span> <span class="ot">=</span> <span class="dt">Empty</span></span>
|
||||
@ -773,10 +778,10 @@ Note
|
||||
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Foldable</span> (<span class="dt">Tree</span> c) <span class="kw">where</span></span>
|
||||
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> project <span class="dt">Empty</span> <span class="ot">=</span> <span class="dt">EmptyF</span></span>
|
||||
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a> project (<span class="dt">Leaf</span> c) <span class="ot">=</span> <span class="dt">LeafF</span> c</span>
|
||||
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> project (<span class="dt">Node</span> l r) <span class="ot">=</span> <span class="dt">NodeF</span> l r</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> project (<span class="dt">Node</span> l r) <span class="ot">=</span> <span class="dt">NodeF</span> l r</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Without going into detail by doing this we establish a relationship between <code>Tree</code> and <code>TreeF</code> and teach the compiler how to translate between these types.</p>
|
||||
<p>Now we can use our Alebra on our non functor type:</p>
|
||||
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>example4 <span class="ot">=</span> cata flatten (<span class="dt">Node</span> (<span class="dt">Leaf</span> <span class="ch">'l'</span>) (<span class="dt">Leaf</span> <span class="ch">'r'</span>)) <span class="op">==</span> <span class="st">"lr"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>example4 <span class="ot">=</span> cata flatten (<span class="dt">Node</span> (<span class="dt">Leaf</span> <span class="ch">'l'</span>) (<span class="dt">Leaf</span> <span class="ch">'r'</span>)) <span class="op">==</span> <span class="st">"lr"</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>The great thing about this is that, looking at the Core output again, there is no traces of the <code>TreeF</code> structure to be found. As far as I can tell, the algorithm is working directly on our <code>Tree</code> type.</p>
|
||||
<hr>
|
||||
<p>Literature:</p>
|
||||
@ -788,13 +793,13 @@ Note
|
||||
</ul>
|
||||
<hr>
|
||||
<p>Appendix:</p>
|
||||
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mergeLists ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a] <span class="ot">-></span> [a]</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="ot">mergeLists ::</span> <span class="dt">Ord</span> a <span class="ot">=></span> [a] <span class="ot">-></span> [a] <span class="ot">-></span> [a]</span>
|
||||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>mergeLists <span class="ot">=</span> <span class="fu">curry</span> <span class="op">$</span> unfoldr c <span class="kw">where</span></span>
|
||||
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> c ([], []) <span class="ot">=</span> <span class="dt">Nothing</span></span>
|
||||
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> c ([], y<span class="op">:</span>ys) <span class="ot">=</span> <span class="dt">Just</span> (y, ([], ys))</span>
|
||||
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a> c (x<span class="op">:</span>xs, []) <span class="ot">=</span> <span class="dt">Just</span> (x, (xs, []))</span>
|
||||
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a> c (x<span class="op">:</span>xs, y<span class="op">:</span>ys) <span class="op">|</span> x <span class="op"><=</span> y <span class="ot">=</span> <span class="dt">Just</span> (x, (xs, y<span class="op">:</span>ys))</span>
|
||||
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> x <span class="op">></span> y <span class="ot">=</span> <span class="dt">Just</span> (y, (x<span class="op">:</span>xs, ys))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> x <span class="op">></span> y <span class="ot">=</span> <span class="dt">Just</span> (y, (x<span class="op">:</span>xs, ys))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
|
||||
|
||||
|
||||
@ -813,7 +818,7 @@ Note
|
||||
a.appendChild(i);
|
||||
window.document.body.appendChild(a);
|
||||
}
|
||||
window.setColorSchemeToggle(window.hasAlternateSentinel())
|
||||
setColorSchemeToggle(hasAlternateSentinel())
|
||||
const icon = "";
|
||||
const anchorJS = new window.AnchorJS();
|
||||
anchorJS.options = {
|
||||
|
Reference in New Issue
Block a user