Skip to content

Commit cc1814b

Browse files
Gabriella439mergify[bot]
authored andcommitted
Add Dhall.function (#1507)
... which is the non-typeclass version of the `Interpret` instance for `a -> b`
1 parent 5c0c1f4 commit cc1814b

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

dhall/src/Dhall.hs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module Dhall
7272
, sequence
7373
, list
7474
, vector
75+
, function
7576
, setFromDistinctList
7677
, setIgnoringDuplicates
7778
, hashSetFromDistinctList
@@ -820,6 +821,30 @@ list = fmap Data.Foldable.toList . sequence
820821
vector :: Decoder a -> Decoder (Vector a)
821822
vector = fmap Data.Vector.fromList . list
822823

824+
{-| Decode a Dhall function into a Haskell function
825+
826+
>>> f <- input (function defaultInterpretOptions inject bool) "Natural/even" :: IO (Natural -> Bool)
827+
>>> f 0
828+
True
829+
>>> f 1
830+
False
831+
-}
832+
function
833+
:: InterpretOptions
834+
-> Encoder a
835+
-> Decoder b
836+
-> Decoder (a -> b)
837+
function options (Encoder {..}) (Decoder extractIn expectedIn) =
838+
Decoder extractOut expectedOut
839+
where
840+
normalizer_ = Just (inputNormalizer options)
841+
842+
extractOut e = pure (\i -> case extractIn (Dhall.Core.normalizeWith normalizer_ (App e (embed i))) of
843+
Success o -> o
844+
Failure _e -> error "FromDhall: You cannot decode a function if it does not have the correct type" )
845+
846+
expectedOut = Pi "_" declared expectedIn
847+
823848
{-| Decode a `Set` from a `List`
824849
825850
>>> input (setIgnoringDuplicates natural) "[1, 2, 3]"
@@ -1126,20 +1151,8 @@ instance (Eq k, Hashable k, FromDhall k, FromDhall v) => FromDhall (HashMap k v)
11261151
autoWith opts = Dhall.hashMap (autoWith opts) (autoWith opts)
11271152

11281153
instance (ToDhall a, FromDhall b) => FromDhall (a -> b) where
1129-
autoWith opts = Decoder extractOut expectedOut
1130-
where
1131-
normalizer_ = Just (inputNormalizer opts)
1132-
1133-
-- ToDo
1134-
extractOut e = pure (\i -> case extractIn (Dhall.Core.normalizeWith normalizer_ (App e (embed i))) of
1135-
Success o -> o
1136-
Failure _e -> error "FromDhall: You cannot decode a function if it does not have the correct type" )
1137-
1138-
expectedOut = Pi "_" declared expectedIn
1139-
1140-
Encoder {..} = injectWith opts
1141-
1142-
Decoder extractIn expectedIn = autoWith opts
1154+
autoWith opts =
1155+
function opts (injectWith opts) (autoWith opts)
11431156

11441157
instance (FromDhall a, FromDhall b) => FromDhall (a, b)
11451158

0 commit comments

Comments
 (0)