dhall-lang / dhall-json

This repository has moved to https://github.com/dhall-lang/dhall-haskell/tree/master/dhall-json
BSD 3-Clause "New" or "Revised" License
65 stars 6 forks source link

Valid Dhall not parsed by dhall-to-{json,yaml} #4

Closed chepner closed 7 years ago

chepner commented 7 years ago

Apologies if this has already been fixed, I'm having some issues getting the most recent versions compiled (tool chain issues on my end).

% dhall <<< '{foo="bar",}'
{ foo : Text }

{ foo = "bar" }
% src/Haskell-Dhall-JSON-Library/result/bin/dhall-to-yaml <<< '{foo="bar",}'

(stdin):1:1: error: expected: "[",
    "\8704", "\955", "\\", "forall",
    "if", "let", "merge"
{foo="bar",}
^
 % src/Haskell-Dhall-JSON-Library/result/bin/dhall-to-yaml <<< '{foo="bar"}'
foo: bar

Notice the trailing ,. While {"foo": "bar",} is not valid JSON, {foo = "bar",} is valid dhall and equivalent to {foo="bar"}, so I wouldn't expect the two to produce different JSON/Yaml, let alone have one be considered an error.

Gabriella439 commented 7 years ago

TL;DR: I believe the root cause of this is that the dhall-to-yaml executable you are using was built against an older version of the dhall package (specifically version 1.1.0 or older). If you explain how you installed the dhall-to-yaml executable I might be able to help you discover what version of dhall it was built against.

Longer explanation:

The dhall-to-yaml executable does not use the dhall command line executable. dhall-to-yaml uses the dhall Haskell library API. That means that the dhall executable and dhall-to-yaml executables on your system might not be using the same version of the dhall package since dhall-to-yaml might have been built against an older version of the dhall package. This is why they give inconsistent behavior in terms of how they support trailing commas, because the two executables are parsing two different versions of the language.

Support for trailing commas was added in dhall-1.2.0 and you can find dhall's change log here for future reference:

That means that your dhall executable was built against version 1.2.0 or newer of the dhall package, whereas your dhall-to-yaml executable was built against version 1.1.0 or older of the dhall package.

You are correct that in dhall-1.2.0 and later that { foo = "bar"} and { foo = "bar",} should be semantically identical (and they are). This means that even before generating JSON they are represented internally by Dhall as the exact same abstract syntax tree, so they should generate the exact same JSON (specifically: {"foo": "bar" }).

I built the most recent version of dhall-json-1.0.2 (which in turn built against dhall-1.3.0) and verified that it correctly handles trailing commas:

$ result/bin/dhall-to-yaml <<< '{foo="bar",}'
foo: bar

If you can let me know how you install the dhall-to-yaml executable I can give you more specific guidance about how to ensure that the executable is using the most recent version of dhall.

Judging by the fact that your example uses a result symlink, it sounds like you are using Nix to build dhall-json and while investigating this I realized that there are issues in the release.nix file that prevent the repository from being built against more recent nixpkgs. These might be the toolchain issues that you ran into. I will update the file so that it succeeds against more recent versions of nixpkgs.

chepner commented 7 years ago

OK, running nix-channel --update (and possibly unnecessarily running stack setup and stack install dhall to put dhall 1.4.2 into ~/.local/bin), I was able to build a good versions of dhall-to-{json,yaml} with the recommended nix-build command.