A domain-specific language (DSL) gives a focused notation for one kind of problem instead of exposing every internal constructor and data structure. In Haskell, a DSL can be embedded directly in ordinary code: custom operators build a domain model, and an interpreter turns that model into a result. section 11.3 section 11.4
Music composition illustrates the approach. A composition is represented as a musical structure, but the composer writes notes and combinations of notes rather than manually assembling a large syntax tree. The resulting program acts as a composition together with the synthesizer that plays it. section 11.0 section 11.3
An embedded DSL can be built incrementally:
- Choose data types that describe the domain, such as structures for notes, groups, and sequences. section 11.0 section 11.3.3
- Define operators as ordinary functions so common domain actions read naturally; `. |` replaces a verbose note constructor. section 11.3 section 11.4
- Set operator associativity and precedence with `infix`, `infixl`, or `infixr` declarations so compound expressions group as intended. section 11.3 section 11.4
- Keep internal representation details behind the notation, then interpret the completed structure to produce the domain result. section 11.3.3 section 11.4
Music notation with operators
A note operator can accept a pitch-like value and a note length, producing a `NoteStructure`. With fixities, operators for sequencing, grouping, and repetition form a compact composition expression while still constructing ordinary Haskell data. section 11.3 section 11.3.3
module Composition.Note where ... (.|) :: (Pitchable a, Show a) => a -> Notelength -> NoteStructure (.|) p l = Note l (Pitch p) infixr 4 .|
This operator definition shows the core embedded-DSL move: give a domain constructor a concise infix spelling, retain type constraints, and declare how it associates. section 11.3
The DSL remains extendable because it is embedded in Haskell: new capabilities implemented in the surrounding framework can become new notation or interpretation behavior. For example, more varied techniques such as random compositions can be added without requiring DSL users to learn its internal structures. section 11.3.3 section 1.3.1









