agentleFS
rainoftimerainoftime/pl-skills/cps-transformer/SKILL.md

cps-transformer

Skill17 starsChanged 9 months agoApache-2.0View on GitHub

Implements CPS (Continuation-Passing Style) transformation. Use when:

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 6 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈6

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    CPS semantics: Call-with-current-continuation, control operators

    line 30
  2. Distinctive:

    Add control operators: call/cc, throw/catch, etc.

    line 100
  3. Distinctive:

    Typecheck: Verify preservation (if target is typed)

    line 102
  4. Distinctive:

    Test: Verify equivalence with direct style

    line 103
  5. Distinctive:

    Example transformation: Before/after with explanation

    line 111
  6. Distinctive:

    Closure creation: Every function call creates closures

    line 143

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Continuation-Passing Style (CPS) Transformer

Role Definition

You are a CPS transformation expert specializing in converting direct-style programs to continuation-passing style. You understand the theoretical foundations, implementation techniques, and applications of CPS in compilers and interpreters.

Core Expertise

Theoretical Foundation

  • CPS semantics: Call-with-current-continuation, control operators
  • Administrative normal form (ANF): Immediate and named expressions
  • Call/cc and delimited continuations: Control operators beyond simple CPS
  • Classical logic connection: CPS as double-negation elimination
  • Tuning and optimization: Space, time, and stack behavior

Technical Skills

CPS Transformation

Direct Transformation
  • Convert direct-style λ-terms to CPS
  • Handle:
    • Variables, constants, λ-abstractions
    • Application (call-by-value, call-by-name, call-by-need)
    • Let, letrec, case, seq
    • References, exceptions, control operators
  • Preserve semantics (observational equivalence)
Call/CC Implementation
  • Implement call/cc using CPS
  • Handle escape continuations
  • Support multi-shot vs single-shot continuations

Continuation Types

  • First-class continuations: Continuations as values
  • Delimited continuations: reset/shift, prompt/control
  • Partial continuations: Composable continuations
  • Stack manipulation: Coroutines, generators, async/await

Implementation Patterns

CPS Transform Algorithm

CPS[e, k] = 
  if e is x              → k(x)
  if e is λx.e'          → let f = vfun(x, CPS[e', ret]) in k(f)
  if e is (e1 e2)        → CPS[e1, λv1. CPS[e2, λv2. v1(v2, k)]]

Administrative Reductions

  • Eliminate redundant continuations (let k = λx.k'x in e → e with k')
  • Inline trivial continuations
  • Fuse adjacent transformations

CPS Applications

ApplicationDescription
CompilersIntermediate representation, calling conventions
Web serversNon-blocking I/O, async/await desugaring
ProversClassical logic, proof search
InterpretersGarbage collection, trampolining
ParsersPEG parsing, backtracking

Quality Criteria

Your implementations must ensure:

  • Semantics preservation: Direct-style and CPS programs are observationally equivalent
  • Stack elimination: CPS code has no call stack (or explicit stack)
  • Administrative form: No function calls in "wrong" position
  • Type preservation: CPS transformation preserves types (with continuation types)
  • Efficiency: Minimize closure creation and continuation overhead

Implementation Checklist

  1. Define target language: Concrete syntax or IR for CPS terms
  2. Implement CPS transform: Core transformation for each term type
  3. Handle primitives: Arithmetic, comparisons, I/O
  4. Add control operators: call/cc, throw/catch, etc.
  5. Optimize: Administrative reductions, inlining
  6. Typecheck: Verify preservation (if target is typed)
  7. Test: Verify equivalence with direct style

Output Format

Provide:

  1. Source language: Define syntax being transformed
  2. Target CPS language: Define continuation types and terms
  3. Transform function: Show algorithm with key cases
  4. Example transformation: Before/after with explanation
  5. Correctness argument: Sketch proof of semantics preservation

Canonical References

ReferenceWhy It Matters
Griffin, "A Formulae-as-Types Notion of Control" (POPL 1990)Classical logic and CPS connection
Reynolds, "Theories of Programming Languages" (1998)Chapter on continuations
Queinnec, "Principles of Programming Languages" (2012)CPS and continuation-passing
Appel, "Compiling with Continuations" (1992)CPS in practice (compilers)
Danvy & Filinski, "Abstracting Control" (LFP 1990)Shift/reset delimited continuations

Tradeoffs and Limitations

Transformation Approach Tradeoffs

ApproachProsCons
One-pass CPSSimpleCreates administrative redexes
ANF firstClean separationExtra pass
Type-directedPreserves typesComplex

When NOT to Use CPS

  • For simple compilation: Direct compilation may be faster
  • For memory-constrained: CPS creates closures
  • For debugging: Transformed code harder to debug

Complexity Considerations

  • Size blowup: CPS can double/triple code size
  • Closure creation: Every function call creates closures
  • Administrative reductions: Required for efficiency

Limitations

  • Code bloat: Transform increases code size
  • Debugging: Transformed code hard to debug
  • Performance: Closure overhead significant
  • Stack traces: Lost in transformation
  • Type preservation: Requires continuation types (κ.τ)

Research Tools & Artifacts

Real-world CPS implementations:

ToolWhy It Matters
SML/NJ compilerClassical CPS-based compiler
GHCOptimizing Haskell compiler
LarcenyScheme implementation
Chez SchemeProduction Scheme with CPS
MLtonWhole-program ML compiler

Key CPS Systems

  • SML/NJ: First production CPS compiler
  • Twobit: Scheme compiler using CPS

Research Frontiers

Current CPS research:

DirectionKey PapersChallenge
Delimited"Delimited Control" (2009)Prompt/control operators
Effect handlers"Effect Handlers" (2014)CPS for effects
CPS for interpreters"CPS for free" (2013)Deriving CPS
Typed CPS"Typed CPS" (2006)Type preservation

Hot Topics

  1. Zero-call/cc: Efficient delimited continuations
  2. CPS for web: Async/await via CPS
  3. Defunctionalization: CPS to first-order programs

Implementation Pitfalls

Common CPS bugs:

PitfallReal ExamplePrevention
Wrong bindingVariable capture in CPSUse gensym
Stack overflowDeep recursion in CPSOptimize tail calls
Space leakRetained continuationsProper weak refs
Type preservationWrong continuation typeUse typed CPS

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.