Re:Dhall: A Gateway Drug to Haskell
by spion on Tuesday June 07, 2022 @08:42PM
(#31661856)
The density of that code is just insanely high. In most language that single fmap line would likely be at least 5 lines of code with lots of variable names to aid in understanding. The map rewrite traversal and actual "compiler" loop part would be separated for sure, and `(D.alphaNormalize (D.normalize expressionType))` would be assigned to another intermediate variable as a "preprocessing step"
[ Parent
| on HN ]
Here is the documentation I would write for a function like this, in a normal language:
{-| Given `Conversion` options and a Dhall type `ExprX`,
try to convert the JSON value `Value` to a Dhall expression of the specified type.
Additionally, if the conversion is successful, apply a rewrite on all maps in the key-value map pair format to map nodes
(i.e. `[{ mapKey = "foo", mapValue = 1 }] -> toMap {foo = 1}`) -}
And here is how I would decompose the first part dhallFromJSON (Conversion {..}) expressionType value = do
let normalizedType = D.alphaNormalize (D.normalize expressionType)
dhallExpression <- loop [] normalizedType value
-- Rewrite maps
return $ Optics.rewriteOf D.subExpressions Lint.useToMap dhallExpression