Files
skills/public/typed-holes-semantics.html
zeekayandHanzo Dev 9e10c1c10e Each llms index points at the other two
hanzoskills.com/llms.txt now opens with the install line, the API
reference index and the company index, so an agent that lands on any
one of the three can find the rest.

robots.txt claimed 537 documents while llms.txt counts 920. The number
is gone from robots.txt; llms.txt derives it.

public/ is regenerated from scripts/site.py, which it had fallen
behind: the copy-prompt menu and agent bar were in the generator and
not in the committed pages. tests/site.mjs passes against the result.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-11 00:27:59 -07:00

783 lines
32 KiB
HTML
Raw Permalink 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"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>typed-holes-semantics</title>
<meta name="color-scheme" content="dark">
<style>
/* Committed to one visual world: a dark instrument panel. No light variant —
the dot field and the hairline rules only read on black, and a naive
inversion would wash both out. Every colour is painted explicitly so the page
never borrows a host background. */
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
:root{
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
--font-zen-pixel-square:'Zen Pixel Square',monospace;
--mono:var(--font-zen-mono);
--sans:var(--font-zen-sans);
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
--r:999px; --r-soft:14px;
}
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
/* The dot field. A halftone made of one repeating radial-gradient, faded out
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
/* No overflow clip here: the dot field is inset to the box already, and a clip
would cut off the copy-prompt menu. */
.hero{position:relative;border-bottom:1px solid var(--line)}
.hero::before{content:"";position:absolute;inset:0;
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
background-size:7px 7px;
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
pointer-events:none}
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
nav{border-bottom:1px solid var(--line)}
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
justify-content:space-between;align-items:center;gap:1rem}
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--ink);text-decoration:none;
display:inline-flex;align-items:center;gap:.6rem}
.brand i{font-style:normal;color:var(--faint)}
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--soft);text-decoration:none}
nav a.up:hover{color:var(--ink)}
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
h1 em{font-style:normal;color:var(--faint)}
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
line-height:1.2;font-weight:400}
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
.btn.solid:hover{background:#d8d8d4}
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
/* Copy-prompt selector. Each row copies the prompt for its own target. */
.copy{position:relative;display:inline-flex}
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
padding:.4rem;display:flex;flex-direction:column;
box-shadow:0 12px 32px rgba(0,0,0,.75)}
.menu[hidden]{display:none}
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
white-space:nowrap}
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
.mi:hover .mark{color:var(--ink)}
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
letter-spacing:.1em;text-transform:uppercase}
/* Works-with bar: one scrolling row, name and mark per agent. */
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
display:flex;align-items:center}
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
-webkit-overflow-scrolling:touch;
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
.bar::-webkit-scrollbar{display:none}
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
text-decoration:none;background:var(--ground)}
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
.agent .mark{width:.95rem;height:.95rem}
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
p{margin:0 0 1rem;max-width:70ch}
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
li{margin:.22rem 0}
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
text-decoration-color:var(--line-hi)}
a:hover{text-decoration-color:var(--ink)}
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
display:block;overflow-x:auto}
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
.curl{display:block;background:var(--panel);border:1px solid var(--line);
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
white-space:pre;line-height:1.9}
.curl b{color:var(--ink);font-weight:400}
.curl i{font-style:normal;color:var(--faint)}
/* index */
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
#q:focus{outline:none;border-color:var(--soft)}
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
text-transform:uppercase;margin:0 0 2rem}
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
padding-bottom:.45rem}
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
border-radius:var(--r-soft);text-decoration:none}
.row:hover{background:#0a0a0a}
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
color:var(--faint);font-size:.85rem}
footer a{color:var(--soft)}
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
margin:0 0 2rem;font-size:.85rem}
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
border-bottom:1px solid var(--line);padding-bottom:.4rem}
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
.fmap a:hover{text-decoration:underline}
.fmap div{color:var(--faint);line-height:1.5}
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
@media (max-width:640px){
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
.btns{gap:.5rem}
.btn{padding:.75rem 1.15rem;font-size:.68rem}
.copy,.copy .btn{width:100%;justify-content:center}
.menu{left:0;right:0;min-width:0}
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
.strip b{border-right:0;padding-right:0}
.bar{width:100%}
}
</style>
<link rel="alternate" type="text/markdown" href="/typed-holes-semantics.md">
<link rel="icon" href="/favicon.svg">
<link rel="canonical" href="https://hanzoskills.com/typed-holes-semantics">
</head>
<!-- Reading this with a machine? Every page here is also plain markdown.
This one: https://hanzoskills.com/typed-holes-semantics.md
Index: https://hanzoskills.com/llms.txt
Corpus: https://hanzoskills.com/all.md
Append .md to any path for the markdown twin. -->
<body>
<nav><div class="in">
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
<a class="up" href="/">All skills</a>
</div></nav>
<div class="wrap">
<h1>typed-holes-semantics</h1><p class="lede">Advanced typed holes semantics - hole closures, pattern matching with holes, type error localization, polymorphism, gradual guarantees, blame tracking</p><h1>Typed Holes: Advanced Semantics</h1>
<p><strong>Scope</strong>: Hole closures, pattern matching, error localization (POPL 2024), polymorphism (TFP 2024), gradual guarantees, blame <strong>Lines</strong>: ~400 <strong>Last Updated</strong>: 2025-10-25</p>
<h2>When to Use This Skill</h2>
<p>Activate this skill when:</p>
<ul><li>Designing advanced type systems with holes</li><li>Implementing pattern matching with incomplete patterns</li><li>Building sophisticated error localization and recovery</li><li>Adding polymorphic holes to type systems</li><li>Ensuring gradual typing guarantees</li><li>Implementing blame tracking for hole-induced errors</li></ul>
<h2>Core Concepts</h2>
<h3>Hole Closures and Closure Types</h3>
<p><strong>Hole closure</strong>: Hole with captured environment (from contextual modal type theory)</p>
<pre><code class="lang-python">from dataclasses import dataclass
from typing import Union, Optional, Dict
# Closure representation
@dataclass
class Closure:
&quot;&quot;&quot;Value closure: function with captured environment&quot;&quot;&quot;
param: str
body: &#x27;Expr&#x27;
env: Dict[str, &#x27;Value&#x27;] # Captured environment
def __repr__(self):
return f&quot;⟨λ{self.param}.{self.body}, {list(self.env.keys())}⟩&quot;
@dataclass
class HoleClosure:
&quot;&quot;&quot;Hole with captured environment (from CMTT)&quot;&quot;&quot;
hole_name: str
expected_type: &#x27;Type&#x27;
env: Dict[str, &#x27;Value&#x27;] # What&#x27;s in scope
def __repr__(self):
return f&quot;⟨?{self.hole_name} : {self.expected_type}, {list(self.env.keys())}⟩&quot;
# Example: Nested scope with hole
def example_hole_closure():
&quot;&quot;&quot;
(λx. λy. ?h) 5
After applying to 5:
λy. ?h where x=5 captured
Hole closure:
⟨?h : τ, {x: 5, y: ...}⟩
&quot;&quot;&quot;
x_val = 5
hole = HoleClosure(
hole_name=&quot;h&quot;,
expected_type=TString(),
env={&#x27;x&#x27;: x_val} # x captured, y not yet bound
)
return hole
# Contextual modal type theory notation
&quot;&quot;&quot;
⊢ e : A | Ω (e has type A with free holes Ω)
Ω ::= · | Ω, u:A[Γ]
u:A[Γ] means: hole u expects type A with variables Γ available
&quot;&quot;&quot;
print(&quot;Hole closures capture environment for later filling&quot;)</code></pre>
<h3>Pattern Matching with Typed Holes (POPL 2023)</h3>
<p><strong>Pattern holes</strong>: Incomplete patterns that still type-check</p>
<pre><code class="lang-python"># Patterns with holes
@dataclass
class VarPattern:
&quot;&quot;&quot;Variable pattern: x&quot;&quot;&quot;
name: str
@dataclass
class ConstructorPattern:
&quot;&quot;&quot;Constructor: C p₁ ... pₙ&quot;&quot;&quot;
constructor: str
sub_patterns: list[&#x27;Pattern&#x27;]
@dataclass
class PatternHole:
&quot;&quot;&quot;Pattern hole: _&quot;&quot;&quot;
expected_type: &#x27;Type&#x27;
def __repr__(self):
return f&quot;_ : {self.expected_type}&quot;
Pattern = Union[VarPattern, ConstructorPattern, PatternHole]
# Match expression with holes
@dataclass
class Match:
&quot;&quot;&quot;match e with | p₁ → e₁ | ... | ?p → ?e&quot;&quot;&quot;
scrutinee: &#x27;Expr&#x27;
branches: list[tuple[Pattern, &#x27;Expr&#x27;]]
# Typing rule for pattern holes
def type_pattern_match_with_holes(match_expr: Match, gamma: dict, expected: &#x27;Type&#x27;):
&quot;&quot;&quot;
Pattern holes contribute to hole context
match (x, y) with
| (0, _) → 1 -- _ : Int (inferred from (x,y) : Int × Int)
| _ → ?e -- ?e : Int (result type), _ : Int × Int
Gradual type theory + contextual modal type theory
&quot;&quot;&quot;
scrutinee_type = synth(match_expr.scrutinee, gamma)
for (pattern, branch_expr) in match_expr.branches:
# Pattern typing: Γ ⊢ p ◁ τ ⊣ Γ&#x27;
# Produces new context Γ&#x27; with pattern variables
new_gamma = type_pattern(pattern, scrutinee_type, gamma)
# Check branch expression
check(branch_expr, new_gamma, expected)
def type_pattern(pattern: Pattern, scrutinee_type: &#x27;Type&#x27;, gamma: dict) -&gt; dict:
&quot;&quot;&quot;
Type pattern against scrutinee type
Returns extended context with pattern variables
&quot;&quot;&quot;
match pattern:
case VarPattern(x):
# x : scrutinee_type
return gamma | {x: scrutinee_type}
case PatternHole(expected):
# Hole must be consistent with scrutinee type
if not consistent(expected, scrutinee_type):
raise TypeError(f&quot;Pattern hole {expected} !~ {scrutinee_type}&quot;)
# No new bindings from hole
return gamma
case ConstructorPattern(ctor, sub_patterns):
# Decompose scrutinee type, type sub-patterns
# ...
pass
print(&quot;Pattern holes: incomplete patterns with typing&quot;)</code></pre>
<h3>Type Error Localization with Holes (POPL 2024)</h3>
<p><strong>Marked lambda calculus</strong>: Systematic error localization via holes</p>
<pre><code class="lang-python">@dataclass
class Marked:
&quot;&quot;&quot;Marked expression: expression with blame label&quot;&quot;&quot;
expr: &#x27;Expr&#x27;
mark: str # Blame label
def __repr__(self):
return f&quot;{self.expr}^{self.mark}&quot;
# Error localization strategy
class ErrorLocalizer:
&quot;&quot;&quot;
Total Type Error Localization (POPL 2024)
Key idea: Use holes + blame to pinpoint errors
&quot;&quot;&quot;
@staticmethod
def localize_error(program: &#x27;Expr&#x27;, gamma: dict) -&gt; tuple[&#x27;Expr&#x27;, list[str]]:
&quot;&quot;&quot;
Transform program to mark error locations
Returns: (marked program, error locations)
Example:
(λx:Int. x) true
Localized:
(λx:Int. x) ⦇true⦈^m1 -- m1 marks type error
Blame: m1 at position &quot;argument of application&quot;
&quot;&quot;&quot;
try:
check(program, gamma, synth(program, gamma))
return (program, []) # No errors
except TypeError:
# Insert holes at inconsistencies
marked_program = insert_holes_at_errors(program, gamma)
blame_labels = extract_blame_labels(marked_program)
return (marked_program, blame_labels)
@staticmethod
def constraint_solving_with_holes(program: &#x27;Expr&#x27;):
&quot;&quot;&quot;
Gradual bidirectional typing with hole filling
Generate constraints:
τ₁ ~ τ₂ (consistency)
τ₁ = τ₂ (equality where possible)
Solve constraints:
Replace ? with concrete types where possible
Keep ? where underspecified
&quot;&quot;&quot;
constraints = generate_constraints(program)
solution = solve_constraints(constraints)
return apply_solution(program, solution)
print(&quot;Error localization: holes mark exactly where type errors occur&quot;)</code></pre>
<h3>Polymorphism with Typed Holes (TFP 2024)</h3>
<p><strong>Polymorphic holes</strong>: Holes in polymorphic contexts</p>
<pre><code class="lang-python"># System F-style polymorphism with holes
@dataclass
class TForall:
&quot;&quot;&quot;∀α. τ - polymorphic type&quot;&quot;&quot;
type_var: str
body: &#x27;Type&#x27;
def __repr__(self):
return f&quot;∀{self.type_var}.{self.body}&quot;
@dataclass
class TyAbs:
&quot;&quot;&quot;Λα. e - type abstraction&quot;&quot;&quot;
type_var: str
expr: &#x27;Expr&#x27;
@dataclass
class TyApp:
&quot;&quot;&quot;e [τ] - type application&quot;&quot;&quot;
expr: &#x27;Expr&#x27;
type_arg: &#x27;Type&#x27;
# Polymorphic hole
@dataclass
class PolyHole:
&quot;&quot;&quot;Hole with polymorphic expected type&quot;&quot;&quot;
name: str
type_scheme: TForall # ∀α. τ
def __repr__(self):
return f&quot;?{self.name} : {self.type_scheme}&quot;
# Typing rule for polymorphic holes
def type_poly_hole(hole: PolyHole, instantiation: dict) -&gt; &#x27;Type&#x27;:
&quot;&quot;&quot;
Instantiate polymorphic hole
?h : ∀α. α → α
Can be filled with:
- λx. x (id : ∀α. α → α)
- λx. ?body (?body : α with α free)
&quot;&quot;&quot;
# Instantiate type variables in scheme
return instantiate_type_scheme(hole.type_scheme, instantiation)
# Constraint generation
def generate_polymorphic_constraints(program: &#x27;Expr&#x27;):
&quot;&quot;&quot;
Generate constraints for polymorphic program with holes
let id = Λα. λx:α. ?body in
id [Int] 5
Constraints:
- ?body : α (in context α, x:α)
- When instantiated with Int: ?body : Int
&quot;&quot;&quot;
pass
print(&quot;Polymorphic holes: holes in polymorphic contexts&quot;)</code></pre>
<h3>Gradual Guarantees</h3>
<p><strong>Static and dynamic gradual guarantees</strong> for soundness</p>
<pre><code class="lang-python">class GradualGuarantees:
&quot;&quot;&quot;
Gradual typing guarantees (Siek et al.)
Applied to typed holes
&quot;&quot;&quot;
@staticmethod
def static_gradual_guarantee():
&quot;&quot;&quot;
Static Gradual Guarantee:
If Γ ⊢ e : τ, and e&#x27; is less precise than e
(i.e., e&#x27; has more holes), then Γ ⊢ e&#x27; : τ&#x27;
where τ&#x27; is consistent with τ
Example:
λx:Int. x + 1 : Int → Int ✓
λx:Int. ?h + 1 : Int → Int ✓ (with ?h : Int)
λx:Int. ?h : Int → ? ✓ (with ?h : ?)
Less precision → More programs accepted
&quot;&quot;&quot;
pass
@staticmethod
def dynamic_gradual_guarantee():
&quot;&quot;&quot;
Dynamic Gradual Guarantee:
If e₁ evaluates to v₁, and e₂ is less precise than e₁,
then e₂ evaluates to v₂ where:
- v₂ is less precise than v₁, OR
- v₂ is indeterminate (contains holes)
Example:
(λx. x + 1) 5 ⇓ 6
(λx. x + ?h) 5 ⇓ ⊥h (indeterminate)
Adding holes → Same behavior or indeterminate
&quot;&quot;&quot;
pass
@staticmethod
def precision_ordering():
&quot;&quot;&quot;
Precision ordering: e₁ ⊑ e₂
e₁ is less precise than e₂ if e₁ has more holes
Examples:
- ?h ⊑ 5
- ?h + 1 ⊑ 5 + 1
- λx. ?h ⊑ λx. x
Reflexive, transitive ordering
&quot;&quot;&quot;
pass
print(&quot;Gradual guarantees ensure soundness with holes&quot;)</code></pre>
<h3>Blame Tracking</h3>
<p><strong>Blame labels</strong>: Track which hole caused error</p>
<pre><code class="lang-python">@dataclass
class BlameLabel:
&quot;&quot;&quot;Blame label for tracking hole provenance&quot;&quot;&quot;
hole_id: str
position: str # Where in program
expected: &#x27;Type&#x27;
actual: &#x27;Type&#x27;
def __repr__(self):
return f&quot;Blame({self.hole_id}): expected {self.expected}, got {self.actual}&quot;
class BlameTracking:
&quot;&quot;&quot;Track blame through evaluation&quot;&quot;&quot;
def __init__(self):
self.blame_log = []
def record_blame(self, label: BlameLabel):
&quot;&quot;&quot;Record when hole causes type error&quot;&quot;&quot;
self.blame_log.append(label)
def evaluate_with_blame(self, expr: &#x27;Expr&#x27;, env: dict):
&quot;&quot;&quot;
Evaluate with blame tracking
If hole causes cast failure: record blame
&quot;&quot;&quot;
try:
return self.eval(expr, env)
except TypeError as e:
# Find which hole caused error
blame = self.find_responsible_hole(expr, e)
self.record_blame(blame)
raise
def find_responsible_hole(self, expr: &#x27;Expr&#x27;, error: TypeError) -&gt; BlameLabel:
&quot;&quot;&quot;
Determine which hole is responsible for error
Trace back through evaluation to find hole
&quot;&quot;&quot;
# Walk expression tree, find hole that led to type mismatch
pass
# Example usage
tracker = BlameTracking()
# Program with hole
program = &quot;&quot;&quot;
(λx:Int → Int. x 5) ?f
&quot;&quot;&quot;
# ?f must have type Int → Int
# If we fill it with 42, we get blame:
# Blame(?f): expected Int → Int, got Int
print(&quot;Blame tracking: identify which hole caused problem&quot;)</code></pre>
<h3>Constraint Generation and Solving</h3>
<p><strong>Constraint-based typing</strong> with holes</p>
<pre><code class="lang-python">@dataclass
class Constraint:
&quot;&quot;&quot;Type constraint&quot;&quot;&quot;
pass
@dataclass
class EqualityConstraint(Constraint):
&quot;&quot;&quot;τ₁ = τ₂&quot;&quot;&quot;
left: &#x27;Type&#x27;
right: &#x27;Type&#x27;
def __repr__(self):
return f&quot;{self.left} = {self.right}&quot;
@dataclass
class ConsistencyConstraint(Constraint):
&quot;&quot;&quot;τ₁ ~ τ₂&quot;&quot;&quot;
left: &#x27;Type&#x27;
right: &#x27;Type&#x27;
def __repr__(self):
return f&quot;{self.left} ~ {self.right}&quot;
class ConstraintSolver:
&quot;&quot;&quot;Solve type constraints with holes&quot;&quot;&quot;
def __init__(self):
self.constraints = []
self.substitution = {} # Unification substitution
def add_constraint(self, c: Constraint):
&quot;&quot;&quot;Add constraint to system&quot;&quot;&quot;
self.constraints.append(c)
def solve(self) -&gt; dict:
&quot;&quot;&quot;
Solve constraints
Returns: Substitution mapping type variables to types
Algorithm:
1. Process equality constraints (unification)
2. Process consistency constraints (check, don&#x27;t fail on holes)
3. Simplify: ? ~ τ always satisfies
&quot;&quot;&quot;
for c in self.constraints:
match c:
case EqualityConstraint(left, right):
self.unify(left, right)
case ConsistencyConstraint(left, right):
self.check_consistency(left, right)
return self.substitution
def unify(self, t1: &#x27;Type&#x27;, t2: &#x27;Type&#x27;):
&quot;&quot;&quot;
Unification with holes
? unifies with anything (most general)
&quot;&quot;&quot;
if isinstance(t1, THole) or isinstance(t2, THole):
return # Success - holes unify with everything
if isinstance(t1, TVar):
self.substitution[t1.name] = t2
return
# ... standard unification
def check_consistency(self, t1: &#x27;Type&#x27;, t2: &#x27;Type&#x27;):
&quot;&quot;&quot;
Check consistency (weaker than equality)
Succeeds if types are compatible modulo holes
&quot;&quot;&quot;
if not consistent(t1, t2):
raise TypeError(f&quot;Inconsistent types: {t1} !~ {t2}&quot;)
print(&quot;Constraint solving: systematic type inference with holes&quot;)</code></pre>
<hr>
<h2>Patterns</h2>
<h3>Pattern 1: Gradual Type Migration</h3>
<pre><code class="lang-python">def gradual_migration_pattern():
&quot;&quot;&quot;
Migrate untyped code to typed using holes
Step 1: Replace all types with holes
f(x, y) → f(x: ?, y: ?) -&gt; ?
Step 2: Run type inference
f(x: Int, y: ?) -&gt; ? (inferred x is Int)
Step 3: Incrementally replace holes with concrete types
f(x: Int, y: String) -&gt; String
Gradual guarantee: Always types at every step!
&quot;&quot;&quot;
print(&quot;Gradual migration: untyped → holes → typed&quot;)</code></pre>
<h3>Pattern 2: Error Recovery with Localization</h3>
<pre><code class="lang-python">def error_recovery_with_localization(program: &#x27;Expr&#x27;):
&quot;&quot;&quot;
Localize type error, insert hole, continue
Type error: (λx:Int. x) &quot;hello&quot;
Step 1: Detect inconsistency
Argument &quot;hello&quot; : String
Expected: Int
Step 2: Insert hole at error location
(λx:Int. x) ⦇&quot;hello&quot;⦈
Step 3: Continue type checking rest of program
Other parts may be fine!
Result: Partial type checking instead of total failure
&quot;&quot;&quot;
try:
typecheck(program)
except TypeError as e:
# Localize error
error_loc = find_error_location(e)
# Insert hole
program_with_hole = insert_hole_at(program, error_loc)
# Continue
return typecheck_with_holes(program_with_hole)
print(&quot;Error recovery: localize → insert hole → continue&quot;)</code></pre>
<h3>Pattern 3: Hole-Driven Refactoring</h3>
<pre><code class="lang-python">def hole_driven_refactoring(old_function: &#x27;Expr&#x27;, new_signature: &#x27;Type&#x27;):
&quot;&quot;&quot;
Refactor by changing signature, filling holes
Old: f : Int → Int
f x = x + 1
New signature: f : String → String
Step 1: Replace body with hole
f : String → String
f x = ?body -- ?body : String (x : String)
Step 2: Type-directed filling
Can&#x27;t use x + 1 (Int operation)
Must produce String
→ show x, x ++ x, etc.
&quot;&quot;&quot;
# Create hole with new type
hole = Hole(&quot;body&quot;, new_signature.result)
# Get suggestions based on new context
suggestions = type_driven_suggestions(hole, new_signature.param)
print(f&quot;Suggestions: {suggestions}&quot;)
print(&quot;Hole-driven refactoring: change type → fill holes&quot;)</code></pre>
<hr>
<h2>Quick Reference</h2>
<h3>Advanced Concepts</h3>
<p>| Concept | Notation | Meaning | |---------|----------|---------| | Hole closure | ⟨?h, Γ⟩ | Hole with captured environment | | Pattern hole | _ | Incomplete pattern | | Blame label | e^m | Expression with blame mark | | Precision | e₁ ⊑ e₂ | e₁ less precise than e₂ | | Consistency | τ ~ σ | Types consistent modulo holes |</p>
<h3>Gradual Guarantees</h3>
<pre><code>Static Gradual Guarantee:
More holes → Still types (or more general type)
Dynamic Gradual Guarantee:
More holes → Same behavior or indeterminate</code></pre>
<h3>Constraint Forms</h3>
<pre><code>Equality: τ₁ = τ₂ (Unification)
Consistency: τ₁ ~ τ₂ (Gradual typing)
Subtyping: τ₁ &lt;: τ₂ (With holes)</code></pre>
<hr>
<h2>Anti-Patterns</h2>
<p>❌ <strong>Failing on first type error</strong>: Prevents finding other errors ✅ Insert holes, localize, continue checking</p>
<p>❌ <strong>Losing hole provenance</strong>: Can&#x27;t track which hole caused issue ✅ Use blame tracking, maintain hole closures</p>
<p>❌ <strong>Ignoring gradual guarantees</strong>: Breaks soundness ✅ Verify static/dynamic gradual guarantees hold</p>
<p>❌ <strong>Over-constraining holes</strong>: Makes system rigid ✅ Use consistency (~) not equality (=)</p>
<hr>
<h2>Related Skills</h2>
<ul><li><code>typed-holes-foundations.md</code> - Basic concepts and theory</li><li><code>hazelnut-calculus.md</code> - Structure editor implementation</li><li><code>type-systems.md</code> - Gradual typing, subtyping</li><li><code>dependent-types.md</code> - Holes in dependent contexts</li><li><code>program-verification.md</code> - Holes in proofs</li><li><code>typed-holes-interaction.md</code> - Practical IDE integration</li></ul>
<hr>
<p><strong>Last Updated</strong>: 2025-10-25 <strong>Format Version</strong>: 1.0 (Atomic)</p>
<footer>
<div class="fmap">
<div><b>Build</b>
<a href="https://hanzo.app">hanzo.app</a> app builder
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
<div><b>Run</b>
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
<div><b>Account</b>
<a href="https://hanzo.id">hanzo.id</a> sign in
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
<div><b>Company</b>
<a href="https://hanzo.ai">hanzo.ai</a> canonical
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
</div>
<div class="fend"><a href="/typed-holes-semantics.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
</footer>
</div></body></html>