agentleFS
rainoftimerainoftime/pl-skills/gadt-implementer/SKILL.md

gadt-implementer

Skill17 starsChanged 9 months agoApache-2.0View on GitHub

Implement Generalized Algebraic Data Types (GADTs) for expressive type-safe data structures.

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Type Indexing: Use type parameters to track additional information

    line 26
  2. Very specific:

    Use {-# LANGUAGE GADTs #-} or {-# LANGUAGE GADTSyntax #-} in GHC

    line 43
  3. Very specific:

    Use TypeApplications for disambiguation

    line 45
  4. Very specific:

    Consider using ExistentialQuantification with GADTs

    line 47
  5. Distinctive:

    When simple algebraic data types suffice

    line 84
  6. Distinctive:

    When type error messages are critical for users

    line 86

≈ “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

GADT Implementer

Generalized Algebraic Data Types (GADTs) extend ordinary algebraic data types by allowing constructors to have different return types, enabling more precise type indexing and type-safe encodings.

When to Use This Skill

  • Implementing typed abstract syntax trees (ASTs)
  • Building type-safe embedded domain-specific languages
  • Creating phantom-typed data structures
  • Implementing well-typed interpreters
  • Encoding invariants in types

What This Skill Does

  1. GADT Declaration: Define data types where constructors return different type indices
  2. Type Indexing: Use type parameters to track additional information
  3. Pattern Matching with Type Refinement: Refine types in pattern branches
  4. Existential Quantification: Hide type parameters in constructors
  5. Type-Level Programming: Encode properties in the type system

Key Concepts

ConceptDescription
Type IndexType parameter that tracks additional information
Type RefinementPattern matching reveals more specific types
ExistentialConstructors can hide type arguments
Phantom TypesType parameters with no runtime representation
Type WitnessesValues that witness type equalities

Tips

  • Use {-# LANGUAGE GADTs #-} or {-# LANGUAGE GADTSyntax #-} in GHC
  • Combine with DataKinds for type-level programming
  • Use TypeApplications for disambiguation
  • Pattern matching provides type equalities - use them!
  • Consider using ExistentialQuantification with GADTs

Common Use Cases

  • Implementing typed ASTs for compilers and interpreters
  • Building type-safe protocol implementations
  • Creating well-typed generic traversals
  • Encoding state machines in types
  • Implementing finally tagless interpreters

Related Skills

  • type-checker-generator - Type checking for GADTs
  • dependent-type-implementer - More expressive than GADTs
  • existential-types - Existential quantification
  • simply-typed-lambda-calculus - Foundation for typed ASTs

Canonical References

ReferenceWhy It Matters
Xi, Chen & Chen, "Guarded Recursive Datatype Constructors" (POPL 2003)Original paper introducing GADTs in ML/Haskell
Peyton Jones, Vytiniotis, Weirich & Shields, "Practical Type Inference for Arbitrary-Rank Types" (JFP 2007)Type inference for GADTs and rank-N polymorphism
GHC User's Guide "GADTs"Practical implementation details

Tradeoffs and Limitations

Approach Tradeoffs

ApproachProsCons
GADTsPrecise types, no runtime checksComplex type errors
Phantom typesSimplerLess expressive
Finally taglessNo GADT syntax neededDifferent style

When NOT to Use This Skill

  • When simple algebraic data types suffice
  • For performance-critical code (extra indirection)
  • When type error messages are critical for users

Limitations

  • Type inference is less complete than for normal ADTs
  • Cannot use deriving clauses for all type classes
  • Pattern matching must be complete for type safety

Assessment Criteria

A high-quality implementation should have:

CriterionWhat to Look For
Type SafetyPattern matching refines types correctly
CompletenessHandles all GADT features (existentials, equality)
Error MessagesClear indication of type mismatches
InferenceInfers types where possible

Quality Indicators

✅ Good: Type-safe evaluator with no runtime checks, clear GADT structure ⚠️ Warning: Overuses GADTs when simpler types would work ❌ Bad: GADT structure doesn't actually provide type safety benefits

Research Tools & Artifacts

GADT implementations:

ToolLanguageWhat to Learn
GHCHaskellProduction GADTs
OCamlOCamlGADTs
RustRustGADTs

Research Frontiers

1. Dependent Types

  • Approach: GADTs as mini dependent types

Implementation Pitfalls

PitfallReal ConsequenceSolution
Type errorsConfusing errorsBetter error messages

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.