Files
rlp/doc/build/html/references/gm-state-transitions.html
crumbtoo cb0de3b26b bhick
2023-12-04 19:52:35 -07:00

559 lines
14 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>G-Machine State Transition Rules &#8212; rl&#39; documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=4f649999" />
<link rel="stylesheet" type="text/css" href="../_static/alabaster.css?v=039e1c02" />
<script src="../_static/documentation_options.js?v=5929fcd5"></script>
<script src="../_static/doctools.js?v=888ff710"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Template Instantiator State Transition Rules" href="ti-state-transitions.html" />
<link rel="prev" title="The Template Instantiator" href="../commentary/ti.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="g-machine-state-transition-rules">
<h1>G-Machine State Transition Rules<a class="headerlink" href="#g-machine-state-transition-rules" title="Link to this heading"></a></h1>
<section id="core-transition-rules">
<h2>Core Transition Rules<a class="headerlink" href="#core-transition-rules" title="Link to this heading"></a></h2>
<ol class="arabic">
<li><p>Lookup a global by name and push its value onto the stack</p>
<div class="math">
<p><img src="../_images/math/60f03d9ff8b2e1f335d16830ee203156e6d3a0e0.svg" alt="\gmrule
{ \mathtt{PushGlobal} \; f : i
&amp; s
&amp; d
&amp; h
&amp; m
\begin{bmatrix}
f : a
\end{bmatrix}
}
{ i
&amp; a : s
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>Allocate an int node on the heap, and push the address of the newly created
node onto the stack</p>
<div class="math">
<p><img src="../_images/math/0fabf97e10f5c112eb5642765c57eee467a61a24.svg" alt="\gmrule
{ \mathtt{PushInt} \; n : i
&amp; s
&amp; d
&amp; h
&amp; m
}
{ i
&amp; a : s
&amp; d
&amp; h
\begin{bmatrix}
a : \mathtt{NNum} \; n
\end{bmatrix}
&amp; m
}"/></p>
</div></li>
<li><p>Allocate an application node on the heap, applying the top of the stack to
the address directly below it. The address of the application node is pushed
onto the stack.</p>
<div class="math">
<p><img src="../_images/math/9d2842813d1c6c9703d6ebe1e7eeaf0d7ae121a6.svg" alt="\gmrule
{ \mathtt{MkAp} : i
&amp; f : x : s
&amp; d
&amp; h
&amp; m
}
{ i
&amp; a : s
&amp; d
&amp; h
\begin{bmatrix}
a : \mathtt{NAp} \; f \; x
\end{bmatrix}
&amp; m
}"/></p>
</div></li>
<li><p>Push a functions argument onto the stack</p>
<div class="math">
<p><img src="../_images/math/172a9e6498882cf2bfd038cf83d83f03e4c0313f.svg" alt="\gmrule
{ \mathtt{Push} \; n : i
&amp; a_0 : \ldots : a_n : s
&amp; d
&amp; h
&amp; m
}
{ i
&amp; a_n : a_0 : \ldots : a_n : s
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>Tidy up the stack after instantiating a supercombinator</p>
<div class="math">
<p><img src="../_images/math/4924a6c6d457db21cd5e02efdc8efd2af4210a19.svg" alt="\gmrule
{ \mathtt{Slide} \; n : i
&amp; a_0 : \ldots : a_n : s
&amp; d
&amp; h
&amp; m
}
{ i
&amp; a_0 : s
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>If the top of the stack is in WHNF (currently this just means a number) is on
top of the stack, <code class="code docutils literal notranslate"><span class="pre">Unwind</span></code> considers evaluation complete. In the case
where the dump is <strong>not</strong> empty, the instruction queue and stack is restored
from the top.</p>
<div class="math">
<p><img src="../_images/math/efc4a40c79bae27675f22ac6011dde9806c845b9.svg" alt="\gmrule
{ \mathtt{Unwind} : \nillist
&amp; a : s
&amp; \langle i', s' \rangle : d
&amp; h
\begin{bmatrix}
a : \mathtt{NNum} \; n
\end{bmatrix}
&amp; m
}
{ i'
&amp; a : s'
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>Bulding on the previous rule, in the case where the dump <strong>is</strong> empty, leave
the machine in a halt state (i.e. with an empty instruction queue).</p>
<div class="math">
<p><img src="../_images/math/fd11e2b1ffa20589ec72bb3f1ba70c82573f3f84.svg" alt="\gmrule
{ \mathtt{Unwind} : \nillist
&amp; a : s
&amp; \nillist
&amp; h
\begin{bmatrix}
a : \mathtt{NNum} \; n
\end{bmatrix}
&amp; m
}
{ \nillist
&amp; a : s
&amp; \nillist
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>Again, building on the previous rules, this rule makes the machine consider
unapplied supercombinators to be in WHNF</p>
<div class="math">
<p><img src="../_images/math/36a64918095793044bc603da838873e5db145350.svg" alt="\gmrule
{ \mathtt{Unwind} : \nillist
&amp; a_0 : \ldots : a_n : \nillist
&amp; \langle i, s \rangle : d
&amp; h
\begin{bmatrix}
a_0 : \mathtt{NGlobal} \; k \; c
\end{bmatrix}
&amp; m
}
{ i
&amp; a_n : s
&amp; d
&amp; h
&amp; m \\
\SetCell[c=2]{c}
\text{when $n &lt; k$}
}"/></p>
</div></li>
<li><p>If an application is on top of the stack, <code class="code docutils literal notranslate"><span class="pre">Unwind</span></code> continues unwinding</p>
<div class="math">
<p><img src="../_images/math/428dd88b1a0f02d8880a57efc2aff1aff732e58b.svg" alt="\gmrule
{ \mathtt{Unwind} : \nillist
&amp; a : s
&amp; d
&amp; h
\begin{bmatrix}
a : \mathtt{NAp} \; f \; x
\end{bmatrix}
&amp; m
}
{ \mathtt{Unwind} : \nillist
&amp; f : a : s
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>When a supercombinator is on top of the stack (and the correct number of
arguments have been provided), <code class="code docutils literal notranslate"><span class="pre">Unwind</span></code> sets up the stack and jumps to
the supercombinators code (<img class="math" src="../_images/math/4e04b3f70eef23aff9592405c5b32293f5f8f3d0.svg" alt="\beta" style="vertical-align: -4px"/>-reduction)</p>
<div class="math">
<p><img src="../_images/math/022f085a854e1a5039b4ce9ff514ae19e6e5731d.svg" alt="\gmrule
{ \mathtt{Unwind} : \nillist
&amp; a_0 : \ldots : a_n : s
&amp; d
&amp; h
\begin{bmatrix}
a_0 : \mathtt{NGlobal} \; n \; c \\
a_1 : \mathtt{NAp} \; a_0 \; e_1 \\
\vdots \\
a_n : \mathtt{NAp} \; a_{n-1} \; e_n \\
\end{bmatrix}
&amp; m
}
{ c
&amp; e_1 : \ldots : e_n : a_n : s
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>Pop the stack, and update the nth node to point to the popped address</p>
<div class="math">
<p><img src="../_images/math/1e7730846a205754c25caa9bc83215d4c9a2fd0f.svg" alt="\gmrule
{ \mathtt{Update} \; n : i
&amp; e : f : a_1 : \ldots : a_n : s
&amp; d
&amp; h
\begin{bmatrix}
a_1 : \mathtt{NAp} \; f \; e \\
\vdots \\
a_n : \mathtt{NAp} \; a_{n-1} \; e_n
\end{bmatrix}
&amp; m
}
{ i
&amp; f : a_1 : \ldots : a_n : s
&amp; d
&amp; h
\begin{bmatrix}
a_n : \mathtt{NInd} \; e
\end{bmatrix}
&amp; m
}"/></p>
</div></li>
<li><p>Pop the stack.</p>
<div class="math">
<p><img src="../_images/math/a8350470d1099f2c9d25787f232cd6d48128e2b9.svg" alt="\gmrule
{ \mathtt{Pop} \; n : i
&amp; a_1 : \ldots : a_n : s
&amp; d
&amp; h
&amp; m
}
{ i
&amp; s
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>Follow indirections while unwinding</p>
<div class="math">
<p><img src="../_images/math/7b51ed63c4f01626ae64caca9b8aa37ef8c04e3e.svg" alt="\gmrule
{ \mathtt{Unwind} : \nillist
&amp; a : s
&amp; d
&amp; h
\begin{bmatrix}
a : \mathtt{NInd} \; a'
\end{bmatrix}
&amp; m
}
{ \mathtt{Unwind} : \nillist
&amp; a' : s
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>Allocate uninitialised heap space</p>
<div class="math">
<p><img src="../_images/math/59c33027716f0d0c66a4cabdbb821415f2b561d5.svg" alt="\gmrule
{ \mathtt{Alloc} \; n : i
&amp; s
&amp; d
&amp; h
&amp; m
}
{ i
&amp; a_1 : \ldots : a_n : s
&amp; d
&amp; h
\begin{bmatrix}
a_1 : \mathtt{NUninitialised} \\
\vdots \\
a_n : \mathtt{NUninitialised} \\
\end{bmatrix}
&amp; m
}"/></p>
</div></li>
<li><p>Evaluate the top of the stack to WHNF</p>
<div class="math">
<p><img src="../_images/math/99ee76fb86a35795feb0b604d9fa8f46e6bcbbc8.svg" alt="\gmrule
{ \mathtt{Eval} : i
&amp; a : s
&amp; d
&amp; h
&amp; m
}
{ \mathtt{Unwind} : \nillist
&amp; a : \nillist
&amp; \langle i, s \rangle : d
&amp; h
&amp; m
}"/></p>
</div></li>
<li><p>Reduce a primitive binary operator <img class="math" src="../_images/math/a5e51bd4f30d980b8599ff18ffd339c6d844d9e8.svg" alt="*" style="vertical-align: 0px"/>.</p>
<div class="math">
<p><img src="../_images/math/6618a79153ccc671049ed4fb1f9cbf31b272d53c.svg" alt="\gmrule
{ * : i
&amp; a_1 : a_2 : s
&amp; d
&amp; h
\begin{bmatrix}
a_1 : x \\
a_2 : y
\end{bmatrix}
&amp; m
}
{ i
&amp; a' : s
&amp; d
&amp; h
\begin{bmatrix}
a' : (x * y)
\end{bmatrix}
&amp; m
}"/></p>
</div></li>
<li><p>Reduce a primitive unary operator <img class="math" src="../_images/math/2d33478fc695c01e3ba58758bbdc809235dc6a3e.svg" alt="\neg" style="vertical-align: 0px"/>.</p>
<div class="math">
<p><img src="../_images/math/fd61456b36d2327ea4950605d2e282d6a362685c.svg" alt="\gmrule
{ \neg : i
&amp; a : s
&amp; d
&amp; h
\begin{bmatrix}
a : x
\end{bmatrix}
&amp; m
}
{ i
&amp; a' : s
&amp; d
&amp; h
\begin{bmatrix}
a' : (\neg x)
\end{bmatrix}
&amp; m
}"/></p>
</div></li>
<li><p>Pack a constructor if there are sufficient arguments</p>
<div class="math">
<p><img src="../_images/math/ceba2ed1f0be639cb0841ed19d1980c77657c74b.svg" alt="\gmrule
{ \mathtt{Pack} \; t \; n : i
&amp; a_1 : \ldots : a_n : s
&amp; d
&amp; h
&amp; m
}
{ i
&amp; a : s
&amp; d
&amp; h
\begin{bmatrix}
a : \mathtt{NConstr} \; t \; [a_1,\ldots,a_n]
\end{bmatrix}
&amp; m
}"/></p>
</div></li>
<li><p>Evaluate a case</p>
<div class="math">
<p><img src="../_images/math/23e5e14aff97d56bdcb7e5847a5610baaa4b3040.svg" alt="\gmrule
{ \mathtt{CaseJump} \begin{bmatrix} t \to c \end{bmatrix} : i
&amp; a : s
&amp; d
&amp; h
\begin{bmatrix}
a : \mathtt{NConstr} \; t \; v
\end{bmatrix}
&amp; m
}
{ c \concat i
&amp; d
&amp; h
&amp; m
}"/></p>
</div></li>
</ol>
</section>
<section id="extension-rules">
<h2>Extension Rules<a class="headerlink" href="#extension-rules" title="Link to this heading"></a></h2>
<ol class="arabic">
<li><p>A sneaky trick to enable sharing of <code class="code docutils literal notranslate"><span class="pre">NNum</span></code> nodes. We note that the
global environment is a mapping of plain old strings to heap addresses.
Strings of digits are not considered valid identifiers, so putting them on
the global environment will never conflict with a supercombinator! We abuse
this by modifying Core Rule 2 to update the global environment with the new
nodes address. Consider how this rule might impact garbage collection
(remember that the environment is intended for <em>globals</em>).</p>
<div class="math">
<p><img src="../_images/math/e8e88c3770a0c3cf4169abbee703c4a6bcffc0f5.svg" alt="\gmrule
{ \mathtt{PushInt} \; n : i
&amp; s
&amp; d
&amp; h
&amp; m
}
{ i
&amp; a : s
&amp; d
&amp; h
\begin{bmatrix}
a : \mathtt{NNum} \; n
\end{bmatrix}
&amp; m
\begin{bmatrix}
n' : a
\end{bmatrix}
\\
\SetCell[c=5]{c}
\text{where $n'$ is the base-10 string rep. of $n$}
}"/></p>
</div></li>
<li><p>In order for the previous rule to be effective, we are also required to take
action when a number already exists in the environment:</p>
<div class="math">
<p><img src="../_images/math/321b47cc7986e581fdc26fb6b63195c1cf5409a2.svg" alt="\gmrule
{ \mathtt{PushInt} \; n : i
&amp; s
&amp; d
&amp; h
&amp; m
\begin{bmatrix}
n' : a
\end{bmatrix}
}
{ i
&amp; a : s
&amp; d
&amp; h
&amp; m
\\
\SetCell[c=5]{c}
\text{where $n'$ is the base-10 string rep. of $n$}
}"/></p>
</div></li>
</ol>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">rl'</a></h1>
<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Index</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Commentary</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../commentary/gm.html">The <em>G-Machine</em></a></li>
<li class="toctree-l1"><a class="reference internal" href="../commentary/layout-lexing.html">Lexing, Parsing, and Layouts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../commentary/ti.html">The <em>Template Instantiator</em></a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">References</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">G-Machine State Transition Rules</a></li>
<li class="toctree-l1"><a class="reference internal" href="ti-state-transitions.html">Template Instantiator State Transition Rules</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li>Previous: <a href="../commentary/ti.html" title="previous chapter">The <em>Template Instantiator</em></a></li>
<li>Next: <a href="ti-state-transitions.html" title="next chapter">Template Instantiator State Transition Rules</a></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2023, madeleine sydney ślaga.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 7.2.6</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
|
<a href="../_sources/references/gm-state-transitions.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>