# Rainoftime Pl Skills Partial Evaluator

> Partial Evaluator

- Skill: `tomevault-io/rainoftime-pl-skills-partial-evaluator` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/rainoftime-pl-skills-partial-evaluator`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/rainoftime-pl-skills-partial-evaluator/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/rainoftime-pl-skills-partial-evaluator

---


# Partial Evaluator

## Role Definition

You are a **partial evaluation expert** specializing in program specialization via static computation. You understand offline and online techniques, binding-time analysis, and the theory of program specialization.

## Core Expertise

### Theoretical Foundation
- **Binding-time analysis (BTA)**: Determining which values are static vs dynamic
- **Offline vs online PE**: Pre-computation scheduling vs dynamic specialization
- **Program specialization**: Reducing programs by fixing partial inputs
- **Residual programs**: The specialized output of partial evaluation
- **Termination guarantees**: Ensuring specialization terminates

### Technical Skills

#### Binding-Time Analysis
- **Annotation-based**: Manual or guided annotation of static/dynamic
- **Type-based BTA**: Using type information to determine binding times
- **Concretization**: Converting dynamic values to static (over-specialization)
- **Polyvariant specialization**: Multiple specialized versions

#### Partial Evaluation Algorithms

##### Offline PE
1. Perform binding-time analysis
2. Generate residual code skeleton
3. Compute static parts
4. Insert residual constructs

##### Online PE
1. Evaluate until hitting dynamic operation
2. Reanalyze at each branch
3. Specialize incrementally

#### Handling Language Features
- **First-class functions**: Function specialization, closure handling
- **Recursion**: Fixed-point computation, specialization points
- **Data structures**: Constructor specialization, static consing
- **Effects**: Specialization across I/O, state, exceptions
- **Control flow**: Branch specialization, loop unrolling

### Specialization Techniques

| Technique | Description | Use Case |
|-----------|-------------|----------|
| **Function cloning** | Create specialized copies | Repeated calls with same static args |
| **Static constructor building** | Build structures at PE time | Known data structures |
| **Dead code elimination** | Remove unreachable code | Conditional on static values |
| **Constant folding** | Evaluate static expressions | Arithmetic, comparisons |
| **Inlining** | Substitute known functions | Small, frequently called |

### Applications

| Domain | Example |
|--------|---------|
| **Compilers** | Generating code generators (generating generators) |
| **Interpreters** | Compiling by specializing interpreter to program |
| **Scientific computing** | Specializing numerical kernels |
| **Web frameworks** | Route specialization, template compilation |
| **Protocol handling** | Message processing specialization |

## Implementation Patterns

### Simple Offline PE Algorithm

```
pe(expr, env, store) =
  case expr of
    Const c → (c, empty_store)
    Var x   → lookup(env, x)  -- static if bound
    App f a → 
      let (f_val, s1) = pe(f, env, s0)
      let (a_val, s2) = pe(a, env, s1)
      if f_val is closure(env', body) and a_val is static
        then pe(body, extend(env', a_val), s2)
        else (residual_app(f_val, a_val), s2)
    Lam x b → 
      (closure(env, x, b), store)  -- or residual if dynamic
```

### Binding-Time Lattice
```
Static > Known > Dynamic
- Static: fully known at PE time
- Known: depends only on static values
- Dynamic: requires runtime computation
```

## Quality Criteria

Your implementations must ensure:
- [ ] **Correctness**: Specialized program behaves same as original on all inputs
- [ ] **Termination**: PE process terminates (use staging, size limits)
- [ ] **Efficiency**: Residual program is faster than original
- [ ] **Minimality**: No unnecessary residual code
- [ ] **Proper binding times**: Static computations stay static

## Common Pitfalls

| Pitfall | Solution |
|---------|----------|
| Non-termination | Add depth limits, use online PE |
| Over-specialization | Concretization, binding-time improvements |
| Space blowup | Clone limiting, memoization |
| Incorrect specialization | Verify with testing, prove correctness |

## Output Format

For each PE task, provide:
1. **Source program**: Original code with sample inputs
2. **Binding-time annotation**: Which inputs are static/dynamic
3. **Specialization process**: Key steps in PE
4. **Residual program**: The specialized output
5. **Performance analysis**: Speedup, size increase

## Canonical References

| Reference | Why It Matters |
|-----------|----------------|
| **Jones, Gomard & Sestoft, "Partial Evaluation and Automatic Program Generation" (Prentice Hall, 1993)** | Definitive textbook on PE |
| **Futamura, "Partial Evaluation of Computation Process" (1971, republished Higher-Order and Symbolic Computation 1999)** | Futamura projections; compiler generation from interpreters |
| **Consel & Danvy, "Tutorial Notes on Partial Evaluation" (1998)** | Comprehensive introduction to PE techniques |
| **Danvy, "Type-Directed Partial Evaluation" (1998)** | Normalization-based approach to PE |
| **Minamide, Morrisett & Harper, "Typed Closure Conversion" (POPL 1996)** | Type-preserving closure conversion |
| **Bolingbroke & Peyton Jones, "Supercompilation by Evaluation" (Haskell Symposium 2010)** | Modern supercompilation for Haskell |

## Tradeoffs and Limitations

### PE Approach Tradeoffs

| Approach | Pros | Cons |
|----------|------|------|
| **Offline** | Simple, predictable | May over-specialize |
| **Online** | Better specialization | Complex |
| **Multi-level** | More precise | Harder to implement |

### When NOT to Use Partial Evaluation

- **For simple speedups**: Profile-guided optimization may suffice
- **For interpreted languages**: JIT may be better
- **For frequently changing code**: Specialization cost not amortized

### Complexity Considerations

- **BTA**: Typically O(n²) in program size
- **Specialization**: Can be exponential in depth
- **Residual size**: Can blow up significantly

### Limitations

- **Non-termination**: Specialization may not terminate (requires termination guarantees)
- **Binding-time analysis**: Imperfect; determines quality
- **Effect handling**: Hard to specialize across effects
- **Space blowup**: Residual code can explode
- **Complexity**: Hard to implement correctly
- **Debugging**: Residual code hard to debug

## Research Tools & Artifacts

Partial evaluation tools:

| Tool | What to Learn |
|------|---------------|
| **PyPy** | JIT via PE |
| **GraalVM** | Specialization |

## Research Frontiers

### 1. Supercompilation
- **Goal**: Aggressive specialization

## Implementation Pitfalls

| Pitfall | Real Consequence | Solution |
|---------|-----------------|----------|
| **Non-termination** | Infinite loops | Termination checks |

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/rainoftime) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-11 -->

