Safe Haskell | None |
---|---|
Language | Haskell2010 |
Template Haskell utilities
Synopsis
- staticDhallExpression :: Text -> Q Exp
- makeHaskellTypeFromUnion :: Text -> Text -> Q [Dec]
- makeHaskellTypes :: [HaskellType Text] -> Q [Dec]
- data HaskellType code
- = MultipleConstructors { }
- | SingleConstructor {
- typeName :: Text
- constructorName :: Text
- code :: code
Template Haskell
staticDhallExpression :: Text -> Q Exp Source #
This fully resolves, type checks, and normalizes the expression, so the resulting AST is self-contained.
This can be used to resolve all of an expression’s imports at compile time, allowing one to reference Dhall expressions from Haskell without having a runtime dependency on the location of Dhall files.
For example, given a file "./Some/Type.dhall"
containing
< This : Natural | Other : ../Other/Type.dhall >
... rather than duplicating the AST manually in a Haskell Type
, you can
do:
Dhall.Type (\case UnionLit "This" _ _ -> ... UnionLit "Other" _ _ -> ...) $(staticDhallExpression "./Some/Type.dhall")
This would create the Dhall Expr AST from the "./Some/Type.dhall"
file
at compile time with all imports resolved, making it easy to keep your Dhall
configs and Haskell interpreters in sync.
makeHaskellTypeFromUnion Source #
Generate a Haskell datatype declaration from a Dhall union type where each union alternative corresponds to a Haskell constructor
For example, this Template Haskell splice:
Dhall.TH.makeHaskellTypeFromUnion "T" "< A : { x : Bool } | B >"
... generates this Haskell code:
data T = A {x :: GHC.Types.Bool} | B
This is a special case of makeHaskellTypes
:
makeHaskellTypeFromUnion typeName code = makeHaskellTypes [ MultipleConstructors{..} ]
makeHaskellTypes :: [HaskellType Text] -> Q [Dec] Source #
Generate a Haskell datatype declaration with one constructor from a Dhall type
This comes in handy if you need to keep Dhall types and Haskell types in sync. You make the Dhall types the source of truth and use Template Haskell to generate the matching Haskell type declarations from the Dhall types.
For example, given this Dhall code:
-- ./Department.dhall < Sales | Engineering | Marketing >
-- ./Employee.dhall { name : Text, department : ./Department.dhall }
... this Template Haskell splice:
Dhall.TH.makeHaskellTypes [ MultipleConstructors "Department" "./tests/th/Department.dhall" , SingleConstructor "Employee" "MakeEmployee" "./tests/th/Employee.dhall" ]
... generates this Haskell code:
data Department = Engineering | Marketing | Sales data Employee = MakeEmployee {department :: Department, name :: Data.Text.Internal.Text}
Carefully note that the conversion makes a best-effort attempt to
auto-detect when a Dhall type (like ./Employee.dhall
) refers to another
Dhall type (like ./Department.dhall
) and replaces that reference with the
corresponding Haskell type.
To add any desired instances (such as FromDhall
/ToDhall
),
you can use the StandaloneDeriving
language extension, like this:
{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TemplateHaskell #-} Dhall.TH.makeHaskellTypes [ MultipleConstructors "Department" "./tests/th/Department.dhall" , SingleConstructor "Employee" "MakeEmployee" "./tests/th/Employee.dhall" ] deriving instance Generic Department deriving instance FromDhall Department deriving instance Generic Employee deriving instance FromDhall Employee
data HaskellType code Source #
Used by makeHaskellTypes
to specify how to generate Haskell types
MultipleConstructors | Generate a Haskell type with more than one constructor from a Dhall union type |
SingleConstructor | Generate a Haskell type with one constructor from any Dhall type To generate a constructor with multiple named fields, supply a Dhall record type. This does not support more than one anonymous field. |
|