7 ms·
Ok, lets be specific. Lets write a comment to explain this function: https://github.com/dhall-lang/dhall-haskell/blob/master/dhall-json/src/Dhall/JSONToDhall.h
by spion 4y ago
Ok, lets be specific. Lets write a comment to explain this function:
https://github.com/dhall-lang/dhall-haskell/blob/master/dhall-json/src/Dhall/JSONToDhall.hs#L802-L809 https://github.com/dhall-lang/dhall-haskell/blob/master/dhal...
- rowanG077 4y agoThere is already a comment there? And the implementation doesn't look too hard. I don't feel like this is an example of a hard to understand Haskell function.
- spion 4y agoHow about you try your best to explain all of it? What exactly is that you understand here? What does Optics.rewriteOf do? What's the purpose of Lint.useToMap? How about D.subExpressions ? How does that composition work with the loop function? Even better. How would you go about finding the source code containing the definition of the function `D.subExpressions` ?
- rowanG077 4y agoNo I didn't understand every sub-function in this function. That isn't necessary to get an idea of what a function does. All of your questions can be answered incredible quickly though using https://hoogle.haskell.org/ https://hoogle.haskell.org/ > What does Optics.rewriteOf do? Hoogling show this is an alias of https://hackage.haskell.org/package/lens-5.1.1/docs/Control-Lens-Combinators.html#v:rewriteOf https://hackage.haskell.org/package/lens-5.1.1/docs/Control-.... > What's the purpose of Lint.useToMap? Hoogle again shows https://hackage.haskell.org/package/dhall-1.41.1/docs/Dhall-Lint.html#v:useToMap https://hackage.haskell.org/package/dhall-1.41.1/docs/Dhall-.... If your function is not indexed you can look at what is qualified as `Lint` and look it up that way. > How about D.subExpressions ? How does that composition work with the loop function? https://hackage.haskell.org/package/dhall-1.41.1/docs/Dhall-Core.html#v:subExpressions https://hackage.haskell.org/package/dhall-1.41.1/docs/Dhall-... All nicely documented... I'm not sure what you mean by composition with the loop function. First the loop function is executed and then afterwards over the result the expression in the first argument to fmap is applied. There is no weird interaction going on here. It's just run this over the result of the loop function if it didn't produce an error. > Even better. How would you go about finding the source code containing the definition of the function `D.subExpressions` ? Use hoogle or just look at the imports at the top of the file. Just like any other programming language. Or even better, use the language server to find it for you. For example there is an import here: `import qualified Dhall.Core as D` Now you know that function is in the `Dhal.Core` module. Going to that module you see the function is not actually there but is exported from the `Dhal.Syntax` module. It took me literally 5 seconds to find it: https://github.com/dhall-lang/dhall-haskell/blob/master/dhall/src/Dhall/Syntax.hs#L786 https://github.com/dhall-lang/dhall-haskell/blob/master/dhal...
- spion 4y ago> Hoogling show this is an alias of https://hackage.haskell.org/package/lens-5.1.1/docs/Control- https://hackage.haskell.org/package/lens-5.1.1/docs/Control-... That explains nothing? What does it do? How does it do it? What do the type variables `a b a b` mean? Why are there 4 of them? What is ASetter?
- spion 4y agoOh the documentation of Setter is fun: https://hackage.haskell.org/package/lens-5.1.1/docs/Control-Lens-Combinators.html#t:Setter https://hackage.haskell.org/package/lens-5.1.1/docs/Control-... Absolutely nothing explained whatsoever. Yup, sounds like like the typical, familiar Haskell experience of zero useful docs where you need them.
- axman6 4y agoThis argument is literally “the documentation for this domain I don’t understand is bad because I refuse to learn the domain”. I use the lens library all the time and those docs make perfect sense, particularly if you apply just the teensiest bit of logic and thing “Could a setter, perhaps, set something?”. It’s a DSL, and you don’t understand the domain - I’m sure you’d have a great time explaining this function from LLVM without referring to any other part of the documentation to give it context https://llvm.org/doxygen/group__LLVMCCoreValueInstructionGetElementPointer.html#ga1e623a49010f40ef27a4a7a955648898 https://llvm.org/doxygen/group__LLVMCCoreValueInstructionGet.... What’s a GEP? Under the rules you’re applying to the lens documentation, I’m not allowed to look that up, because it should be immediately obvious.
- spion 4y agoYes they actually document which bit is what > Get the source element type of the given GEP operator Notice the relentness repetition that provides precision and clarity? I now know the argument GEP is a GEP operator. Might seem redundant, but it allows a new reader to start from any point and find their way to the relevant terms / nouns and their types.
- spion 4y agoThe 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"
- spion 4y agoHere 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