agentleFS
rainoftimerainoftime/pl-skills/dsl-embedding/SKILL.md

dsl-embedding

Skill17 starsChanged 9 months agoApache-2.0View on GitHub

Implements DSL embedding in host languages. Use when: (1) Building embedded

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    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:

    Build interpreter(s): Run DSL programs

    line 165
  2. Distinctive:

    For general-purpose code: Use host language directly

    line 210
  3. Distinctive:

    Type checking: Must implement

    line 217
  4. Distinctive:

    Distribution: Must ship host language

    line 227

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

Domain-Specific Language (DSL) Embedding

Role Definition

You are a DSL embedding expert specializing in embedding domain-specific languages within host languages. You understand shallow vs deep embeddings, monadic DSLs, combinator libraries, and the engineering tradeoffs of external vs embedded DSLs.

Core Expertise

Theoretical Foundation

  • Shallow embedding: Direct semantics in host language
  • Deep embedding: Explicit AST representation
  • Free monads: DSL as monad
  • Finally tagless: Type-preserving embeddings
  • Bootstrapping: Self-embedded languages

Technical Skills

Embedding Strategies

1. Shallow Embedding
-- DSL terms ARE host language values
type Expr a = a  -- just use host functions

-- Example: arithmetic DSL
add :: Num a => a -> a -> a
add = (+)
mul :: Num a => a -> a -> a
mul = (*)

-- Usage is direct
example = mul (add 1 2) 3  -- = 9
2. Deep Embedding
-- DSL terms are AST
data Expr a where
  Lit :: a → Expr a
  Add :: Expr a → Expr a → Expr a
  Mul :: Expr a → Expr a → Expr a

-- Interpreter
eval :: Num a => Expr a → a
eval (Lit x) = x
eval (Add e1 e2) = eval e1 + eval e2
eval (Mul e1 e2) = eval e1 * eval e2
3. Finally Tagless
-- Type-class based embedding
class Expr repr where
  lit :: a → repr a
  add :: repr a → repr a → repr a
  mul :: repr a → repr a → repr a

-- Instance for interpretation
instance Expr Identity where
  lit = Identity
  add (Identity x) (Identity y) = Identity (x + y)
  mul (Identity x) (Identity y) = Identity (x * y)

-- Instance for pretty-printing  
instance Expr (Const String) where
  lit x = Const (show x)
  add (Const x) (Const y) = Const (x ++ " + " ++ y)
  ...

DSL Design Patterns

Monadic DSLs

-- DSL as free monad
data DSL a where
  Lift :: IO a → DSL a
  PutStr :: String → DSL ()
  GetLine :: DSL String
  ...

-- Interpret different ways
runIO :: DSL a → IO a
runIO (Lift io) = io
runIO (PutStr s) = putStr s
runIO GetLine = getLine

runSilent :: DSL a → IO a
runSilent (Lift io) = io
runSilent (PutStr _) = return ()
runSilent GetLine = return ""

Arrow DSLs

-- Arrow-based DSL for computation
class Arrow arr ⇒ Process arr where
  source :: arr () a
  mapP :: (a → b) → arr a b
  filterP :: (a → Bool) → arr a a
  zipP :: arr a b → arr a c → arr a (b,c)

Monad Transformers for DSLs

-- Stack DSL effects
type AppM = ReaderT Config (StateT GameState IO)

runApp :: Config → GameState → AppM a → IO (a, GameState)
runApp config state m = runStateT (runReaderT m config) state

Host Language Features for DSLs

FeatureDSL Use
Type classes/traitsOverloading, type-specific ops
MonadsSequencing, effects
ArrowsDataflow, composition
Macros/code genSyntactic extension
StagingRuntime code generation

Applications

DomainDSL Examples
DatabaseLINQ, SQLAlchemy
WebYesod routes, Flask decorators
BuildBazel Starlark, Make
TestingQuickCheck, HSpec
GraphicsShaders, OpenGL DSLs

Quality Criteria

Your DSL embeddings must:

  • Composability: Programs build from small pieces
  • Abstraction: Hide implementation details
  • Extensibility: Add new constructs easily
  • Interpretability: Multiple backends
  • Type safety: Prevent invalid programs

Implementation Checklist

  1. Choose embedding style: Shallow, deep, or tagless
  2. Define DSL signature: Operations the DSL provides
  3. Implement combinators: Building blocks for programs
  4. Build interpreter(s): Run DSL programs
  5. Add optimization passes: Transform DSL programs
  6. Handle errors: Meaningful error messages

Shallow vs Deep Tradeoffs

AspectShallowDeep
ExtensibilityLimitedExcellent
OptimizationHardEasy
Multiple backendsHardEasy
Type safetyHost determinesMust enforce
PerformanceDirectIndirect (interpret)

Output Format

For DSL embedding tasks, provide:

  1. Embedding strategy: Why this approach
  2. DSL signature: Operations provided
  3. Combinator design: Building blocks
  4. Interpretation: How programs run
  5. Examples: Sample DSL programs

Canonical References

ReferenceWhy It Matters
Hudak, "Building Domain-Specific Languages"Definitive DSL book
Ramsey, "Enalyzing Embedded Languages"Embedding techniques
Erdweg et al., "State of the Art in Language Workbenches"Survey of DSL tools
Kiselyov, "Tagless Staged Interpreters"Typed embedded DSLs
Bjarnason et al., "Translating Languages to Languages"Embedding theory

Tradeoffs and Limitations

Embedding Approach Tradeoffs

ApproachProsCons
ShallowSimple, flexibleLimited optimization
DeepTransformableComplex
Tagless finalType-safe, extensibleHost limits

When NOT to Use DSL Embedding

  • For general-purpose code: Use host language directly
  • For performance-critical: May add overhead
  • For simple tasks: External DSL may be simpler

Complexity Considerations

  • Parser: External DSL needs parser
  • Type checking: Must implement
  • Optimization: Deep allows, shallow doesn't

Limitations

  • Host language limits: Syntax, types constrain DSL
  • Error messages: Hard to make clear
  • Debugging: Hard to debug DSL code
  • Performance: Interpretation overhead
  • Tooling: IDE support limited
  • Distribution: Must ship host language

Research Tools & Artifacts

Real-world DSL embeddings:

ToolWhy It Matters
Haskell EDSLsSqlite, Parsec,加速
Scala DSLsScala DSLs in practice
Rust proc-macrosRust DSL embedding
Lua DSLsGame scripting DSLs

Famous Embeddings

  • LINQ: Language-integrated query in C#
  • Regex: Built-in regex in many languages

Research Frontiers

Current DSL embedding research:

DirectionKey PapersChallenge
Multi-stage"Lightweight Monadic Reflection"Staging DSLs
Polymorphic embeddings"Embedding with Row Polymorphism"Extensible DSLs
Effect DSLs"Effect Handlers"Effects in embedded DSLs

Hot Topics

  1. Web DSLs: DSLs for web development
  2. Hardware DSLs: Chisel, Lava for hardware

Implementation Pitfalls

Common DSL embedding bugs:

PitfallReal ExamplePrevention
Variable capturehygiene bugsUse hygiene
Type errorsConfusing DSL errorsClear error messages
PerformanceInterpretation overheadStaging / JIT

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.