Commit Graph

1153 Commits

Author SHA1 Message Date
Gabriel Gonzalez
354346be91 Pretty-print output of {json,yaml}-to-dhall (#1150)
This enables syntax highlighting and formatted output for these two
programs
2019-07-24 13:33:41 +00:00
Frederik Ramcke
4faf25bbbe Load imports recursively (#1128)
* Load imports recursively

This is the big change that enables us to implement 'semi-semantic'
caching.

* Use `throwM` instead of `liftIO . throwIO`

* Fix build with __GHCJS__

* Fix exceptions in Dhall.Import

* Fix dhall-lsp-server

* Revert exception behaviour on typecheck errors

This is one for a separate pull request!

* Make sure loadImportFresh returns alpha-normal expression

As caught by @Gabriel439, `loadImportFresh` violated the invariant that
`ImportSemantics` should be alpha-beta-normal. This fix also means that
we don't have to alpha-normalise again in `loadImportWithSemanticCache`.

* Remove old comment

* Fix regression test for issue 216

Turns out the test was testing the wrong thing, because it was
pretty-printing an import. This worked previously because when importing
uncached expressions we would not alpha-normalise them.

* Restore `dhall freeze` bevhaviour

Newly frozen imports should also be present in the cache.
2019-07-23 16:11:33 +00:00
Ollie Charles
d55bf8f3a3 Add instance Lift (Expr s a) (#1119)
This allows Exprs to be lifted in template-haskell. This is useful to
build a [dhall||] quasiquoter
2019-07-21 12:10:53 -07:00
Simon Jakobi
c8a0df3748 Specialize Expr's ToTerm instance to (Expr X a) (#1143)
* This simplifies pattern matching during encoding.
2019-07-21 15:17:41 +00:00
Simon Jakobi
88d6671e38
Enable the fixed potPourri test (#1144)
Context: https://github.com/dhall-lang/dhall-lang/pull/655
2019-07-21 15:21:20 +02:00
Gabriel Gonzalez
0120f2650d Document that normalize is partial (#1140)
* Document that `normalize` is partial

Fixes https://github.com/dhall-lang/dhall-haskell/issues/1138

* Document other partial functions

... as suggested by @sjakobi
2019-07-20 20:14:52 +00:00
Gabriel Gonzalez
0c61b0d048 Forbid invalid codepoints (#1104)
* Forbid invalid codepoints

... as standardized in https://github.com/dhall-lang/dhall-lang/pull/640

* Don't validate code points outside of escape sequences

... as suggested by @sjakobi

It's not necessary (since the `text` package forbids invalid UTF8) and
slows down performance as verified by the `dhall-parser` comment parsing
benchmarks

* Restore `nonCharacter` parsing test
2019-07-20 19:41:18 +00:00
Simon Jakobi
0ee6ce6f6f Implement "Generalize empty list annotations" (#1112)
…as standardized in https://github.com/dhall-lang/dhall-lang/pull/630

Also:

* Update the dhall-lang submodule to the state of
  https://github.com/dhall-lang/dhall-lang/pull/654.

* Skip the nonCharacter test for now.

Fixes #414.
Fixes #770.
2019-07-20 18:05:23 +00:00
Gabriel Gonzalez
cbd6a6543d Support lens-family-core-2.0.0 (#1141)
Related to https://github.com/commercialhaskell/stackage/issues/4725
2019-07-20 12:18:49 +00:00
Simon Jakobi
8aa8add453 Catch errors in normalizeWithM when testing it (#1139)
Context: #1138
2019-07-20 04:30:42 +00:00
Simon Jakobi
482984eb6d Allow only valid HTTP(S) reg-names (#1124)
Corresponding change to the standard:
https://github.com/dhall-lang/dhall-lang/pull/627

The potPourri parser test remains disabled due to
https://github.com/dhall-lang/dhall-lang/pull/655.

Fixes https://github.com/dhall-lang/dhall-haskell/issues/1110.
2019-07-20 00:54:27 +00:00
Simon Jakobi
e40e847730 Always typecheck the type annotation on 'toMap' (#1132)
Previously, ill-typed expressions like this one got into normalization:

    toMap {=} : <>.x

Also:

* Tweak Expr's Arbitrary instance:
  - Boring nullary constructors don't need to be so frequent.
  - Large NaturalLits can cause normalization to OOM, which we don't
    want when running the testsuite.

* Add property test to check that all well-typed expressions can be
  normalized.
2019-07-19 23:14:01 +00:00
Gabriel Gonzalez
f0d017d7c5 Fix test suite to accept expected decoding failures (#1136)
This will be necessary for https://github.com/dhall-lang/dhall-lang/pull/640
2019-07-19 21:11:22 +00:00
Gabriel Gonzalez
a440c6cd28 Improve error message for missing constructor (#1137)
Fixes https://github.com/dhall-lang/dhall-haskell/issues/1130
2019-07-19 20:17:14 +00:00
Frederik Ramcke
7e9728f0e9 dhall-lsp-server: Freezing imports (#1123)
* Implement dhall.freezeImport and dhall.freezeAllImports

* Remove old (broken) test suite

* Rename `relativePosition` to `subtractPosition`

as suggested by @Gabriel439

* Add doctest for `subtractPosition`

as suggested by @Gabriel439

* Simplify getImportHashPosition

As spotted by @Gabriel439

* Use `forM` instead of `mapM` for prettier code

As suggested by @Gabriel439
2019-07-19 17:24:11 +00:00
Simon Jakobi
7dc7856d10 dhall-json: Allow empty alternative for Nesting.Inline (#1122)
Fixes #1092.
2019-07-19 16:19:19 +00:00
Simon Jakobi
b4f71b2aa2 Check normalizeWithM for consistency with normalize (#1126)
* Check normalizeWithM for consistency with normalize

* Implements constant folding of Natural/fold applications normalizeWithM.

* Changes the Arbitrary Var instance to generate only non-negative indices.
  Otherwise failures like this one would pop up:

    normalizeWithM should be consistent with normalize: FAIL (8.51s)
          *** Failed! Falsified (after 318133 tests and 6 shrinks):
          Let (Binding {variable = "", annotation = Nothing, value = List} :| []) (Var (V "" (-1)))
          Var (V "" (-1)) /= Var (V "" (-2))
          Use --quickcheck-replay=180244 to reproduce.

Fixes https://github.com/dhall-lang/dhall-haskell/issues/1114.

* QuickCheck tests: Specialize the 'natural' Gen to Naturals

Previously it could produce about any number, not just non-negative
ones.
2019-07-19 15:33:59 +00:00
quasicomputational
0df6362a9d Make the tests build with GHC 7.10. (#1134) 2019-07-18 23:11:51 +00:00
Gabriel Gonzalez
1a27cad137 Add --file option to dhall-json executables (#1107)
* Add `--file` option to `dhall-json` executables

Fixes https://github.com/dhall-lang/dhall-haskell/issues/1096

* s/JSON/expression/

... as caught by @sjakobi

Co-Authored-By: Simon Jakobi <simon.jakobi@gmail.com>

* s/expression/YAML expression/

... as caught by @sjakobi

Co-Authored-By: Simon Jakobi <simon.jakobi@gmail.com>

* Change the program descriptions for `{json,yaml}-to-dhall`

... as suggested by @sjakobi
2019-07-18 19:08:47 +00:00
Simon Jakobi
40ec83743d Make isNormalized consistent with normalize (#1115)
To be more precise (citing the haddocks):

    Given a well-typed expression e, (isNormalized e) is equivalent to
    (e == normalize e).

    Given an ill-typed expression, isNormalized may return True or False.

An even closer correspondence between isNormalized and 'normalize' is
currently tricky to achieve as 'normalize' returns errors only for some
ill-typed expressions. Once 'normalize' is more principled in this
regard, isNormalized could be changed to return a (Maybe Bool).

This re-enables a property test checking this consistency. Since
'normalize' returns errors for some ill-typed expressions, we
catch have to catch these, which requires an NFData for Expr.
2019-07-17 22:19:41 +00:00
Frederik Ramcke
d5d0224bc3 Preparing Dhall.Import for "Semi-semantic" caching (#1113)
* Fix misleading comment

* Add `Chained` type to capture fully chained imports

Until now we used `Import` two mean two different things:
- The syntactic construct; e.g. `./a.dhall` corresponds to the following
  AST:
  ```
  Embed
    (Import
      (ImportHashed Nothing (Local Here (Directory ["."]) "a.dhall"))
      Code)
  ```

- The physical location the import is pointing to, computed by
'chaining' the syntactical import with the the 'physical' parent import.
For example the syntactic import `./a.dhall` might actually refer to the
remote file `http://host/directory/a.dhall`.

This commit adds a `Chained` newtype on top of `Import` to make this
distinction explicit at type level.

* Use `HTTPHeaders` alias for binary headers

I claim that `HTTPHeaders` is more readable and informative than the
unfolded type `(CI ByteString, ByteString)`.

* Typecheck and normalise http headers earlier

Previously we would typecheck and normalise http headers in
`exprFromImport`, i.e. while loading the import. This commit adds the
invariant that any headers in 'Chained' imports are already typechecked
and normalised, and moves this step into `loadWith` accordingly.

This causes a subtle difference in behaviour when importing remote files
with headers `as Location`: previously, nonsensical expressions like
`http://a using 0 0 as Location` were valid, while they would now cause
a type error.

* Fix dhall-lsp-server

* Fix Dhall.Import API regarding `Chained` imports

Do not expose the `Chained` constructor; we don't want external code
breaking our invariants! Also further clarifies the comment describing
the `Chained` type.

* Fix dhall-lsp-server

Since we are no longer able to construct `Chained` imports directly we
need to export a few additional helper functions from Dhall.Import.
Furthermore, since VSCode (and presumably the other editors out there
implementing the LSP protocol) does not support opening remote files
anyway we can get rid of some complications by dropping support for
remote files entirely on the back-end.

* Generalise decodeExpression, fixes TODO

* Fix tests

* Fix benchmarks

* Remove Travis cache for `~/.local/bin`

* Fix copy-pasted comment

Thanks to @Gabriel439 for spotting this!

* Add clarifying comment to `toHeaders`
2019-07-17 15:20:48 +00:00
Frederik Ramcke
6534ce85ed Fix incorrect Canonicalize law (#1118)
It is not the case that
    canonicalize (a <> b) = canonicalize a <> canonicalize b.
For example
    canonicalize (Directory ["asd"] <> Directory [".."])
    = Directory [],
but
    canonicalize (Directory ["asd"]) <> canonicalize (Directory [".."])
    = Directory ["..", "asd"].

The law we want instead is:
    canonicalize (a <> b)
    = canonicalize (canonicalize a <> canonicalize b)
2019-07-17 13:13:40 +00:00
Frederik Ramcke
33ebf7ee71 dhall-lsp-server: Turn imports into clickable links (#1121)
* Expose `localToPath` in Dhall.Import

Also modifies `localToPath` to return a relative path if the input was
relative, rather than resolving relative paths by appending the current
directory.

* Turn imports into clickable links

This implements a handler for 'Document Link' requests. As a result,
imports are now clickable!

* Recover original behaviour
2019-07-17 08:54:56 +00:00
Simon Jakobi
e044b4ab68
toMap normalization: Misc improvements (#1116)
…addressing my own comments on
https://github.com/dhall-lang/dhall-haskell/pull/1041.
2019-07-15 23:50:40 +02:00
Simon Jakobi
3856612763
Sort the fields of a record projection during normalization (#1111)
…as standardized in https://github.com/dhall-lang/dhall-lang/pull/625.
2019-07-15 20:55:00 +02:00
Gabriel Gonzalez
81ef514652 Move more dhall-json test logic into data files (#1108)
* Move more `dhall-json` test logic into data files

... as suggested @sjakobi in https://github.com/dhall-lang/dhall-haskell/pull/1094#pullrequestreview-261053408

* Fix CI failure

... as caught by @sjakobi

* Fix missing import of `(<>)`

... as noted by @sjakobi
2019-07-15 17:08:38 +00:00
Mario
8aa2ac3ce9 Implementation of toMap (#1041)
... as standardized in https://github.com/dhall-lang/dhall-lang/pull/610
2019-07-15 08:28:29 -07:00
Fabrizio Ferrai
0506c13822
Build macOS binaries with Travis (#1101) 2019-07-15 09:43:59 +03:00
Simon Jakobi
b2cbc8227f Remove usage of legacy Optional syntax (#1091)
* Remove usage of legacy Optional syntax

* Dhall.JSON: Use multilet in examples

* Update dhall-json/src/Dhall/JSON.hs

Co-Authored-By: Gabriel Gonzalez <Gabriel439@gmail.com>

* Update dhall-json/src/Dhall/JSON.hs

Co-Authored-By: Gabriel Gonzalez <Gabriel439@gmail.com>

* Update dhall-json/src/Dhall/JSON.hs

Co-Authored-By: Gabriel Gonzalez <Gabriel439@gmail.com>

* Update dhall-nix/src/Dhall/Nix.hs

Co-Authored-By: Gabriel Gonzalez <Gabriel439@gmail.com>

* Update dhall-nix/src/Dhall/Nix.hs

Co-Authored-By: Gabriel Gonzalez <Gabriel439@gmail.com>

* Revert changes to travis.yml

* Lint travis.dhall
2019-07-14 18:36:41 +00:00
Gabriel Gonzalez
e49710166e
Add dhall-json support for unions as keys (#1094)
This is inspired by this StackOverflow question:

https://stackoverflow.com/questions/56967374/dynamic-records-key-type
2019-07-12 22:25:46 -07:00
Gabriel Gonzalez
e0bd21e25d
Update web page (#1079)
The main improvements are:

* Add one-line summary of what Dhall is

  This is based on user feedback that some people could not tell what
  Dhall was based on the home page

* Replace "Tough on messes" section with "Integration friendly" section

  This is based on user feedback that the nethack example wasn't working
  for them and also because of a large number of questions in online
  discussions about how to integrate Dhall

* Fix tutorial examples to put public/private keys under `~/.ssh`

* Link to newly-added Wiki pages
2019-07-09 11:15:07 -07:00
Gabriel Gonzalez
90b62ee9d7 Replace dhall-to-text with dhall text subcommand (#1090)
Fixes #1087
2019-07-08 17:18:09 +00:00
Frederik Ramcke
8ae7b603fe
dhall-lsp-server: Fix cache to correctly invalidate transitive dependencies (#1069)
* Move "Dot" import graph generation to Dhall.Main

Previously `Dhall.Import` would generate the import graph in "dot"
format while resolving imports. This change simplifies `Dhall.Import` to
only keep track of the adjacency list representing the import graph,
moving the logic for generating "dot" files to Dhall.Main.

This change will allow us to implement proper cache invalidation for
`dhall-lsp-server`.

* Correctly invalidate transitive dependencies

Fixes dhall-lsp-server`s caching behaviour to correctly invalidate
cached imports that (possibly indirectly) depend on the changed file.

Example:

Suppose we have the following three files:
{- In A.dhall -} 2 : ./B.dhall
{- In B.dhall -} ./C.dhall
{- In C.dhall -} Natural

Previously, changing C.dhall to `Text` would not cause `A.dhall` to stop
type-checking, since the old version of `B.dhall` (which evaluated to
`Natural`) would still have been in the cache. This change fixes that
behaviour.

* Make edges of import graph self-documenting

As suggested by @Gabriel439

* Don't cache expressions manually

After computing the diagnostics for a given file we added its normal
form to the cache, but forgot to add its dependencies to the dependency
graph. This bug points out that keeping the import graph consistent
manually is probably not a good idea. With this commit we never mess
with the import cache manually; this means that files are only cached
once they are depended upon by some other file, potentially causing us
to duplicate work (but no more than once).

* Fix left-overs from previous commit
2019-07-08 10:55:15 +00:00
Gabriel Gonzalez
aff138e192 Handle empty alternatives when converting from JSON (#1083)
* Handle empty alternatives when converting from JSON

Fixes https://github.com/dhall-lang/dhall-haskell/issues/1074

`dhall-to-{json,yaml}` treat empty alternatives as strings of the
same name, but `{json,yaml}-to-dhall` were missing the corresponding
logic to decode strings to empty alternatives, which this change
fixes.

This also ensures that the conversion logic contains no more
`undefined`s, by making the union conversion branch total.

* Clarify successful cases for union handling

Co-Authored-By: Simon Jakobi <simon.jakobi@gmail.com>

* Add missing trailing newline

... as caught by @sjakobi
2019-07-08 03:09:24 +00:00
Simon Jakobi
5339f1893d
Mergify: Add "merged" condition for "delete_head_branch" action (#1089)
I came accross https://medium.com/mergify/keep-your-repository-clean-delete-your-merged-branches-4b779f4848ae
which points out that I need a condition here!
2019-07-08 04:20:47 +02:00
Simon Jakobi
22428f9e1f
Reapply "Mergify: Delete head branch after merge (#1064)" (#1086)
This reverts commit 4004d62f28.
2019-07-07 19:56:39 +02:00
Gabriel Gonzalez
635a4489d2 Fix unexpected decoding successes in tests (#1088)
Fixes https://github.com/dhall-lang/dhall-haskell/issues/1082

All of the successes are due to accepting expressions that would not
have been produced by a compliant encoder, such as:

* Expressions that use a variable name of `_` that could have been
  encoded more compactly

* An expression tagged as a `Natural` number storing a negative number

* An expression encoding a function appled to 0 arguments
2019-07-07 17:22:53 +00:00
Simon Jakobi
f1234c4efa Don't typecheck the head of a list twice. (#1085) 2019-07-07 16:50:06 +00:00
Simon Jakobi
e40d0bc624 Simplify typechecking of Combine and CombineTypes (#1067)
Also add Dhall.Map.outerJoin, inspired by Semialign.align.
2019-07-07 16:00:25 +00:00
Simon Jakobi
d7114a32de Simplify normalization of record projection (#1071)
…via new functions Dhall.Map.{keysSet,restrictKeys} and
Dhall.Set.fromSet.

Also apply these functions in a few more cases.
2019-07-07 14:33:07 +00:00
Gabriel Gonzalez
462f82a0f8 Build against optparse-applicative-0.15 (#1081)
Related to https://github.com/commercialhaskell/stackage/issues/4693
2019-07-07 11:49:11 +00:00
Simon Jakobi
f873b0d66e Increase coverage of acceptance tests (#1072)
* dhall:tasty: Unskip a few tests

These remain broken:

- tests/parser/failure/annotation
- tests/parser/failure/missingSpace
- tests/parser/failure/unit/ImportEnvWrongEscape
- tests/parser/failure/unit/ListLitEmptyAnnotation
- tests/parser/failure/unit/ListLitEmptyPrecedence
- tests/parser/success/unit/MergeParenAnnotation

* Typo

* Add binary-decode failure tests

A few pass, but these are broken:

- tests/binary-decode/failure/unit/ApplyNoArgs
- tests/binary-decode/failure/unit/LambdaExplicitlyNamedUnderscore
- tests/binary-decode/failure/unit/NaturalNegativeOne
- tests/binary-decode/failure/unit/PiExplicitlyNamedUnderscore
- tests/binary-decode/failure/unit/VariableExplicitlyNamedUnderscore

* dhall:tasty: Add semantic-hash tests
2019-07-07 11:00:55 +00:00
Gabriel Gonzalez
b8e19d614a Fix dhall-json's support for preserving alternative names (#1080)
* Fix `dhall-json`'s support for preserving alternative names

... as caught by @sjakobi in the course of #1077

The code has to be updated to reflect the new normal forms for
union literals

* Add tests

... as suggested by @sjakobi
2019-07-07 10:27:54 +00:00
Simon Jakobi
3b4f826eda Dhall.Map: Remove the keys list when sorting (#1066)
Also add the unorderedSingleton and unorderedFromList functions.

This speeds up the "issue 412" benchmark by ~9% and "union performance"
by ~2%.
2019-07-07 07:21:04 +00:00
Gabriel Gonzalez
c116207663 Update to latest dhall-lang (#1084)
* Update to latest `dhall-lang`

The main updates are to support the following two changes to the
standard:

* https://github.com/dhall-lang/dhall-lang/pull/604
* https://github.com/dhall-lang/dhall-lang/pull/611

* `s/fields/fails/`

... as caught by @sjakobi

Co-Authored-By: Simon Jakobi <simon.jakobi@gmail.com>
2019-07-07 06:08:58 +00:00
Frederik Ramcke
2cd4ed948f dhall-lsp-server: Improve type-on-hover behaviour over binders (#1062)
* Improve type-on-hover behaviour over binders

Dhall's parser does not generate `Src` annotations for the names in
binders, which means that it's not quite as straight-forward to check
whether the user meant to hover over it and see its type as one would
hope. As a result, the current "naive" implementation sometimes behaves
slightly unintuitively and returns the type of the entire expression,
rather than the type of the bound variable. E.g.
  let x = 2 in "text"
      ~
would result in the type `Text` being displayed, rather than `Natural`
as one would expect. Since the language server also marks the expression
whose type is being displayed, the behaviour isn't quite that bad in
practice (we aren't falsely led to believe that `x` has type `Text`).

This change recovers the `Src` information describing the bound variable
names and handles those cases in a way that should be more in line with
the expectations of the user.

* Fix exprAt not handling multi-lets correctly

* Fix merge left-overs
2019-07-06 23:17:52 +00:00
Simon Jakobi
76efe630b2 Remove cruft related to the removed 'constructors' keyword (#1077) 2019-07-06 22:38:02 +00:00
Simon Jakobi
9a2e25b92d Dhall.Map.fromList: Fix docs (#1078)
Closes #1052.
2019-07-06 20:58:32 +00:00
Simon Jakobi
28fc9e1870 Dhall.Eval.eqMapsBy: Compare map sizes before contents (#1065)
Data.Map.size is O(1), so we expose it via Dhall.Map.
2019-07-05 04:04:08 +00:00
Frederik Ramcke
53e1967754
dhall-lsp-server: Normalize types before displaying them to the user (#1060)
* Normalize types before displaying them to the user

Previously, the type displayed when hovering over the arrow in a lambda
(or forall) binder would not always be normal, e.g.
  \(n : {Left = Natural, Right = Natural}.Left) -> n + n
                                                ~
would result in
  forall (n : {Left = Natural, Right = Natural}.Left) -> Natural
being displayed.

The (expected) normal type would be:
  forall (n : Natural) -> Natural

This fix normalises types before displaying them to the user (fixes both
the type-on-hover and annotate-let behaviour).

* Move normalisation from typeAt' to typeAt

This way we only have to worry about it one place, rather than  in each
clause of `typeAt'`.
2019-07-05 03:58:44 +00:00