Domain-specific languages (DSLs) are everywhere, with applications in areas such as parser generation,
music synthesis, parallel programming and even the design of domain-specific languages.
However, while the pay-off in using a DSL may be substantial, the cost of introducing
a language may be made prohibitively high by the need to construct a supporting toolchain.
A common tactic is to embed a DSL into a general-purpose host programming language.
Existing infrastructure such as a language’s compiler or type system may be re-used, provided
that the embedding accurately captures the properties of the DSL. While the rich type systems
and orthogonal abstraction features of modern functional languages have proved particularly
capable in this regard, they are not without their shortcomings. Building type-safe functions
defined over an embedded DSL can introduce application-specific type constraints that end up
being imposed on the DSL data types themselves. At best, these constraints are unwieldy and
at worst they can limit the range of DSL expressions that can be built.
In this thesis we tackle the problem of accurately embedding a DSL’s type system into that
of the purely functional language Haskell. We present a framework for expressing application-specific
constraints at the point of a DSL expression’s use rather than when the DSL’s embedding
is defined. We show how our framework can be applied more generally to capture arbitrary
properties of a DSL expression and, in certain cases, how we may subsequently prove
additional safety properties such as the totality of a function which operates over DSL expressions.
We evaluate our techniques by illustrating their use in constructing a DSL for heterogeneous
parallel programming. However, our methods have potentially wider applications such
as context-dependent computation, which are also discussed