@@ -72,6 +72,7 @@ module Dhall
72
72
, sequence
73
73
, list
74
74
, vector
75
+ , function
75
76
, setFromDistinctList
76
77
, setIgnoringDuplicates
77
78
, hashSetFromDistinctList
@@ -820,6 +821,30 @@ list = fmap Data.Foldable.toList . sequence
820
821
vector :: Decoder a -> Decoder (Vector a )
821
822
vector = fmap Data.Vector. fromList . list
822
823
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
+
823
848
{-| Decode a `Set` from a `List`
824
849
825
850
>>> input (setIgnoringDuplicates natural) "[1, 2, 3]"
@@ -1126,20 +1151,8 @@ instance (Eq k, Hashable k, FromDhall k, FromDhall v) => FromDhall (HashMap k v)
1126
1151
autoWith opts = Dhall. hashMap (autoWith opts) (autoWith opts)
1127
1152
1128
1153
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)
1143
1156
1144
1157
instance (FromDhall a , FromDhall b ) => FromDhall (a , b )
1145
1158
0 commit comments