mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
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]>
783 lines
32 KiB
HTML
783 lines
32 KiB
HTML
<!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:
|
||
"""Value closure: function with captured environment"""
|
||
param: str
|
||
body: 'Expr'
|
||
env: Dict[str, 'Value'] # Captured environment
|
||
|
||
def __repr__(self):
|
||
return f"⟨λ{self.param}.{self.body}, {list(self.env.keys())}⟩"
|
||
|
||
@dataclass
|
||
class HoleClosure:
|
||
"""Hole with captured environment (from CMTT)"""
|
||
hole_name: str
|
||
expected_type: 'Type'
|
||
env: Dict[str, 'Value'] # What's in scope
|
||
|
||
def __repr__(self):
|
||
return f"⟨?{self.hole_name} : {self.expected_type}, {list(self.env.keys())}⟩"
|
||
|
||
# Example: Nested scope with hole
|
||
def example_hole_closure():
|
||
"""
|
||
(λx. λy. ?h) 5
|
||
|
||
After applying to 5:
|
||
λy. ?h where x=5 captured
|
||
|
||
Hole closure:
|
||
⟨?h : τ, {x: 5, y: ...}⟩
|
||
"""
|
||
x_val = 5
|
||
hole = HoleClosure(
|
||
hole_name="h",
|
||
expected_type=TString(),
|
||
env={'x': x_val} # x captured, y not yet bound
|
||
)
|
||
return hole
|
||
|
||
# Contextual modal type theory notation
|
||
"""
|
||
⊢ e : A | Ω (e has type A with free holes Ω)
|
||
|
||
Ω ::= · | Ω, u:A[Γ]
|
||
|
||
u:A[Γ] means: hole u expects type A with variables Γ available
|
||
"""
|
||
|
||
print("Hole closures capture environment for later filling")</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:
|
||
"""Variable pattern: x"""
|
||
name: str
|
||
|
||
@dataclass
|
||
class ConstructorPattern:
|
||
"""Constructor: C p₁ ... pₙ"""
|
||
constructor: str
|
||
sub_patterns: list['Pattern']
|
||
|
||
@dataclass
|
||
class PatternHole:
|
||
"""Pattern hole: _"""
|
||
expected_type: 'Type'
|
||
|
||
def __repr__(self):
|
||
return f"_ : {self.expected_type}"
|
||
|
||
Pattern = Union[VarPattern, ConstructorPattern, PatternHole]
|
||
|
||
# Match expression with holes
|
||
@dataclass
|
||
class Match:
|
||
"""match e with | p₁ → e₁ | ... | ?p → ?e"""
|
||
scrutinee: 'Expr'
|
||
branches: list[tuple[Pattern, 'Expr']]
|
||
|
||
# Typing rule for pattern holes
|
||
def type_pattern_match_with_holes(match_expr: Match, gamma: dict, expected: 'Type'):
|
||
"""
|
||
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
|
||
"""
|
||
scrutinee_type = synth(match_expr.scrutinee, gamma)
|
||
|
||
for (pattern, branch_expr) in match_expr.branches:
|
||
# Pattern typing: Γ ⊢ p ◁ τ ⊣ Γ'
|
||
# Produces new context Γ' 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: 'Type', gamma: dict) -> dict:
|
||
"""
|
||
Type pattern against scrutinee type
|
||
|
||
Returns extended context with pattern variables
|
||
"""
|
||
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"Pattern hole {expected} !~ {scrutinee_type}")
|
||
# No new bindings from hole
|
||
return gamma
|
||
|
||
case ConstructorPattern(ctor, sub_patterns):
|
||
# Decompose scrutinee type, type sub-patterns
|
||
# ...
|
||
pass
|
||
|
||
print("Pattern holes: incomplete patterns with typing")</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:
|
||
"""Marked expression: expression with blame label"""
|
||
expr: 'Expr'
|
||
mark: str # Blame label
|
||
|
||
def __repr__(self):
|
||
return f"{self.expr}^{self.mark}"
|
||
|
||
# Error localization strategy
|
||
class ErrorLocalizer:
|
||
"""
|
||
Total Type Error Localization (POPL 2024)
|
||
|
||
Key idea: Use holes + blame to pinpoint errors
|
||
"""
|
||
|
||
@staticmethod
|
||
def localize_error(program: 'Expr', gamma: dict) -> tuple['Expr', list[str]]:
|
||
"""
|
||
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 "argument of application"
|
||
"""
|
||
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: 'Expr'):
|
||
"""
|
||
Gradual bidirectional typing with hole filling
|
||
|
||
Generate constraints:
|
||
τ₁ ~ τ₂ (consistency)
|
||
τ₁ = τ₂ (equality where possible)
|
||
|
||
Solve constraints:
|
||
Replace ? with concrete types where possible
|
||
Keep ? where underspecified
|
||
"""
|
||
constraints = generate_constraints(program)
|
||
solution = solve_constraints(constraints)
|
||
return apply_solution(program, solution)
|
||
|
||
print("Error localization: holes mark exactly where type errors occur")</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:
|
||
"""∀α. τ - polymorphic type"""
|
||
type_var: str
|
||
body: 'Type'
|
||
|
||
def __repr__(self):
|
||
return f"∀{self.type_var}.{self.body}"
|
||
|
||
@dataclass
|
||
class TyAbs:
|
||
"""Λα. e - type abstraction"""
|
||
type_var: str
|
||
expr: 'Expr'
|
||
|
||
@dataclass
|
||
class TyApp:
|
||
"""e [τ] - type application"""
|
||
expr: 'Expr'
|
||
type_arg: 'Type'
|
||
|
||
# Polymorphic hole
|
||
@dataclass
|
||
class PolyHole:
|
||
"""Hole with polymorphic expected type"""
|
||
name: str
|
||
type_scheme: TForall # ∀α. τ
|
||
|
||
def __repr__(self):
|
||
return f"?{self.name} : {self.type_scheme}"
|
||
|
||
# Typing rule for polymorphic holes
|
||
def type_poly_hole(hole: PolyHole, instantiation: dict) -> 'Type':
|
||
"""
|
||
Instantiate polymorphic hole
|
||
|
||
?h : ∀α. α → α
|
||
|
||
Can be filled with:
|
||
- λx. x (id : ∀α. α → α)
|
||
- λx. ?body (?body : α with α free)
|
||
"""
|
||
# Instantiate type variables in scheme
|
||
return instantiate_type_scheme(hole.type_scheme, instantiation)
|
||
|
||
# Constraint generation
|
||
def generate_polymorphic_constraints(program: 'Expr'):
|
||
"""
|
||
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
|
||
"""
|
||
pass
|
||
|
||
print("Polymorphic holes: holes in polymorphic contexts")</code></pre>
|
||
<h3>Gradual Guarantees</h3>
|
||
<p><strong>Static and dynamic gradual guarantees</strong> for soundness</p>
|
||
<pre><code class="lang-python">class GradualGuarantees:
|
||
"""
|
||
Gradual typing guarantees (Siek et al.)
|
||
|
||
Applied to typed holes
|
||
"""
|
||
|
||
@staticmethod
|
||
def static_gradual_guarantee():
|
||
"""
|
||
Static Gradual Guarantee:
|
||
|
||
If Γ ⊢ e : τ, and e' is less precise than e
|
||
(i.e., e' has more holes), then Γ ⊢ e' : τ'
|
||
where τ' 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
|
||
"""
|
||
pass
|
||
|
||
@staticmethod
|
||
def dynamic_gradual_guarantee():
|
||
"""
|
||
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
|
||
"""
|
||
pass
|
||
|
||
@staticmethod
|
||
def precision_ordering():
|
||
"""
|
||
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
|
||
"""
|
||
pass
|
||
|
||
print("Gradual guarantees ensure soundness with holes")</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:
|
||
"""Blame label for tracking hole provenance"""
|
||
hole_id: str
|
||
position: str # Where in program
|
||
expected: 'Type'
|
||
actual: 'Type'
|
||
|
||
def __repr__(self):
|
||
return f"Blame({self.hole_id}): expected {self.expected}, got {self.actual}"
|
||
|
||
class BlameTracking:
|
||
"""Track blame through evaluation"""
|
||
|
||
def __init__(self):
|
||
self.blame_log = []
|
||
|
||
def record_blame(self, label: BlameLabel):
|
||
"""Record when hole causes type error"""
|
||
self.blame_log.append(label)
|
||
|
||
def evaluate_with_blame(self, expr: 'Expr', env: dict):
|
||
"""
|
||
Evaluate with blame tracking
|
||
|
||
If hole causes cast failure: record blame
|
||
"""
|
||
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: 'Expr', error: TypeError) -> BlameLabel:
|
||
"""
|
||
Determine which hole is responsible for error
|
||
|
||
Trace back through evaluation to find hole
|
||
"""
|
||
# Walk expression tree, find hole that led to type mismatch
|
||
pass
|
||
|
||
# Example usage
|
||
tracker = BlameTracking()
|
||
|
||
# Program with hole
|
||
program = """
|
||
(λx:Int → Int. x 5) ?f
|
||
"""
|
||
|
||
# ?f must have type Int → Int
|
||
# If we fill it with 42, we get blame:
|
||
# Blame(?f): expected Int → Int, got Int
|
||
|
||
print("Blame tracking: identify which hole caused problem")</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:
|
||
"""Type constraint"""
|
||
pass
|
||
|
||
@dataclass
|
||
class EqualityConstraint(Constraint):
|
||
"""τ₁ = τ₂"""
|
||
left: 'Type'
|
||
right: 'Type'
|
||
|
||
def __repr__(self):
|
||
return f"{self.left} = {self.right}"
|
||
|
||
@dataclass
|
||
class ConsistencyConstraint(Constraint):
|
||
"""τ₁ ~ τ₂"""
|
||
left: 'Type'
|
||
right: 'Type'
|
||
|
||
def __repr__(self):
|
||
return f"{self.left} ~ {self.right}"
|
||
|
||
class ConstraintSolver:
|
||
"""Solve type constraints with holes"""
|
||
|
||
def __init__(self):
|
||
self.constraints = []
|
||
self.substitution = {} # Unification substitution
|
||
|
||
def add_constraint(self, c: Constraint):
|
||
"""Add constraint to system"""
|
||
self.constraints.append(c)
|
||
|
||
def solve(self) -> dict:
|
||
"""
|
||
Solve constraints
|
||
|
||
Returns: Substitution mapping type variables to types
|
||
|
||
Algorithm:
|
||
1. Process equality constraints (unification)
|
||
2. Process consistency constraints (check, don't fail on holes)
|
||
3. Simplify: ? ~ τ always satisfies
|
||
"""
|
||
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: 'Type', t2: 'Type'):
|
||
"""
|
||
Unification with holes
|
||
|
||
? unifies with anything (most general)
|
||
"""
|
||
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: 'Type', t2: 'Type'):
|
||
"""
|
||
Check consistency (weaker than equality)
|
||
|
||
Succeeds if types are compatible modulo holes
|
||
"""
|
||
if not consistent(t1, t2):
|
||
raise TypeError(f"Inconsistent types: {t1} !~ {t2}")
|
||
|
||
print("Constraint solving: systematic type inference with holes")</code></pre>
|
||
<hr>
|
||
<h2>Patterns</h2>
|
||
<h3>Pattern 1: Gradual Type Migration</h3>
|
||
<pre><code class="lang-python">def gradual_migration_pattern():
|
||
"""
|
||
Migrate untyped code to typed using holes
|
||
|
||
Step 1: Replace all types with holes
|
||
f(x, y) → f(x: ?, y: ?) -> ?
|
||
|
||
Step 2: Run type inference
|
||
f(x: Int, y: ?) -> ? (inferred x is Int)
|
||
|
||
Step 3: Incrementally replace holes with concrete types
|
||
f(x: Int, y: String) -> String
|
||
|
||
Gradual guarantee: Always types at every step!
|
||
"""
|
||
print("Gradual migration: untyped → holes → typed")</code></pre>
|
||
<h3>Pattern 2: Error Recovery with Localization</h3>
|
||
<pre><code class="lang-python">def error_recovery_with_localization(program: 'Expr'):
|
||
"""
|
||
Localize type error, insert hole, continue
|
||
|
||
Type error: (λx:Int. x) "hello"
|
||
|
||
Step 1: Detect inconsistency
|
||
Argument "hello" : String
|
||
Expected: Int
|
||
|
||
Step 2: Insert hole at error location
|
||
(λx:Int. x) ⦇"hello"⦈
|
||
|
||
Step 3: Continue type checking rest of program
|
||
Other parts may be fine!
|
||
|
||
Result: Partial type checking instead of total failure
|
||
"""
|
||
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("Error recovery: localize → insert hole → continue")</code></pre>
|
||
<h3>Pattern 3: Hole-Driven Refactoring</h3>
|
||
<pre><code class="lang-python">def hole_driven_refactoring(old_function: 'Expr', new_signature: 'Type'):
|
||
"""
|
||
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't use x + 1 (Int operation)
|
||
Must produce String
|
||
→ show x, x ++ x, etc.
|
||
"""
|
||
# Create hole with new type
|
||
hole = Hole("body", new_signature.result)
|
||
|
||
# Get suggestions based on new context
|
||
suggestions = type_driven_suggestions(hole, new_signature.param)
|
||
|
||
print(f"Suggestions: {suggestions}")
|
||
|
||
print("Hole-driven refactoring: change type → fill holes")</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: τ₁ <: τ₂ (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'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> |