Raw input (#85)

Added a function that allows doing `input` from a closed
Dhall `Expr` instead of text. This is hugely useful when
working with Dhall in AST level (for example, using custom
types / normalization). For example, suppose that you have
built a custom primitive that requires a record as an argument.
With `rawInput` you can just extract the record into a Haskell
data type and process it without need to work with maps and other
bits of AST.

For context of this commit, see #79 (and also #26).
This commit is contained in:
Ville Tirronen 2017-07-05 19:05:47 +03:00 committed by Gabriel Gonzalez
parent 7a0c03df57
commit 4707a8ff62

View File

@ -34,6 +34,8 @@ module Dhall
, maybe
, vector
, GenericInterpret(..)
-- * Miscellaneous
, rawInput
-- * Re-exports
, Natural
@ -42,7 +44,7 @@ module Dhall
, Generic
) where
import Control.Applicative (empty, liftA2, (<|>))
import Control.Applicative (empty, liftA2, (<|>), Alternative)
import Control.Exception (Exception)
import Data.Monoid ((<>))
import Data.Text.Buildable (Buildable(..))
@ -142,6 +144,27 @@ input (Type {..}) txt = do
Just x -> return x
Nothing -> Control.Exception.throwIO InvalidType
-- | Use this function to extract Haskell values directly from Dhall AST.
-- The intended use case is to allow easy extraction of Dhall values for
-- making the function `Dhall.Core.normalizeWith` easier to use.
--
-- For other use cases, use `input` from `Dhall` module. It will give you
-- a much better user experience.
rawInput
:: Alternative f
=> Type a
-- ^ The type of value to decode from Dhall to Haskell
-> Expr s X
-- ^ a closed form Dhall program, which evaluates to the expected type
-> f a
-- ^ The decoded value in Haskell
rawInput (Type {..}) expr = do
case extract (Dhall.Core.normalize expr) of
Just x -> pure x
Nothing -> empty
{-| Use this to provide more detailed error messages
>> input auto "True" :: IO Integer