Programatic Dhall text template usage from Haskell

I’m trying to render Dhall text templates programmatically from Haskell, building up the necessary context from Haskell. Which APIs should I be using?

I tried to look at how it’s done for the dhall text comment, and I believe the below code is responsible for running that subcommand. Are those the APIs that I should be using? How can I provide the context from haskell instead of another dhall file?

           expression <- getExpression file
            resolvedExpression <-
                Dhall.Import.loadRelativeTo (rootDirectory file) UseSemanticCache expression
            _ <- Dhall.Core.throws (Dhall.TypeCheck.typeOf (Annot resolvedExpression Dhall.Core.Text))

            let normalizedExpression = Dhall.Core.normalize resolvedExpression

            case normalizedExpression of
                Dhall.Core.TextLit (Dhall.Core.Chunks [] text) ->
                    let write = case output of
                          StandardOutput -> Data.Text.IO.putStr
                          OutputFile file_ -> Data.Text.IO.writeFile file_
                    in write text
                _ -> do
                    let invalidDecoderExpected :: Expr Void Void
                        invalidDecoderExpected = Dhall.Core.Text

                    let invalidDecoderExpression :: Expr Void Void
                        invalidDecoderExpression = normalizedExpression

                    Control.Exception.throwIO (Dhall.InvalidDecoder {..})

See: How to add a new built-in function — Dhall documentation

Note that the above instructions can be used to add anything to the context (not just functions)

Thanks. That looks like exactly what I’m looking for.

1 Like