From 0beab7bb8e8505a39086bb35a7549060f59c8610 Mon Sep 17 00:00:00 2001
From: Chloe Brown <chloe.brown.00@outlook.com>
Date: Wed, 23 Apr 2025 17:05:17 +0100
Subject: Explain how to fold.

---
 sec/systemt.ltx | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

(limited to 'sec/systemt.ltx')

diff --git a/sec/systemt.ltx b/sec/systemt.ltx
index e66f499..8fcedd1 100644
--- a/sec/systemt.ltx
+++ b/sec/systemt.ltx
@@ -285,7 +285,39 @@ sums in order to store the data for different constructors within a single type.
 \TODO{
   \begin{itemize}
     \item create box-and-pointer diagram
-    \item explain how to fold
+  \end{itemize}
+}
+
+The recursive depth is essential for the fold operation. We iteratively
+construct a heap of values; the recursive depth is an upper bound on the number
+of steps it takes to converge on the ``true'' result of the fold. For recursive
+depth zero, we return an arbitrary value at each index, and as no inductive
+value is made from applying zero constructors, we have no soundness obligations.
+It is possible to return an arbitrary value as all System~T types are inhabited.
+For the successor case, at any given index we look up the constructor and
+associated data in the heap. We use the recursively-constructed return heap to
+replace inductive arguments with suitable values, and then apply the appropriate
+fold operation.
+
+\[
+\dofoldmatch*{t}{
+  \mathsf{leaf}(n).      f(n);
+  \mathsf{branch}(x, y). g(x, y)
+} \coloneq
+\dolet
+  {\tuple{depth, root, heap}}{t}
+  {go}*{
+    \doprimrec*{depth}
+      {\arb{}}
+      {h}{\lambda i.
+        \domatch*{heap~i}{
+          \mathsf{Left}(n).     f(n);
+          \mathsf{Right}(j, k). g(h~i, h~j)}}}
+  *{go~root}
+\]
+
+\TODO{
+  \begin{itemize}
     \item explain how to roll
   \end{itemize}
 }
-- 
cgit v1.2.3

