dhall-1.29.0: A configuration language guaranteed to terminate

Safe HaskellNone
LanguageHaskell2010

Dhall.TH

Contents

Description

Template Haskell utilities

Synopsis

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 #

Arguments

:: Text

Name of the generated Haskell type

-> Text

Dhall code that evaluates to a union type

-> Q [Dec] 

Generate a Haskell datatype declaration from a Dhall union type where each union alternative corresponds to a Haskell constructor

This comes in handy if you need to keep a Dhall type and Haskell type in sync. You make the Dhall type the source of truth and use Template Haskell to generate the matching Haskell type declaration from the Dhall type.

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

If you are starting from an existing record type that you want to convert to a Haskell type, wrap the record type in a union with one alternative, like this:

Dhall.TH.makeHaskellTypeFromUnion "T" "< A : ./recordType.dhall >"

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.makeHaskellTypeFromUnion  "T" "< A : { x : Bool } | B >"

deriving instance Generic   T
deriving instance FromDhall T