Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement with keyword #1685

Merged
merged 29 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ebdd159
Implement `with` keyword
Gabriella439 Feb 25, 2020
f1eae40
Implement new desugaring logic
Gabriella439 Feb 26, 2020
9e0c689
Update `dhall-lang` reference
Gabriella439 Feb 26, 2020
8d73c27
Update to reflect new `with` syntax
Gabriella439 Feb 29, 2020
a6c504a
Update to latest draft of proposal
Gabriella439 Mar 1, 2020
487456f
Switch to simpler desugaring
Gabriella439 Mar 2, 2020
2be646a
Relocate `desugarWith` back to `Dhall.Syntax` module
Gabriella439 Mar 2, 2020
574793b
Fix downstream build failures
Gabriella439 Mar 2, 2020
1ad54ae
Merge branch 'master' into gabriel/with_2
Gabriella439 Mar 2, 2020
f7f3e94
Update `dhall-lang` to `master`
Gabriella439 Mar 3, 2020
a0fbaaa
Merge branch 'master' into gabriel/with_2
Gabriella439 Mar 3, 2020
cd81cab
Improve formatting of `with` expressions
Gabriella439 Mar 9, 2020
b227046
Merge branch 'master' into gabriel/with_2
Gabriella439 Mar 9, 2020
5c314ab
Fix warnings
Gabriella439 Mar 9, 2020
e10c816
Merge branch 'gabriel/with_2' of github.com:dhall-lang/dhall-haskell …
Gabriella439 Mar 9, 2020
8f5d124
Merge branch 'master' into gabriel/with_2
Gabriella439 Mar 12, 2020
6bfe2ce
Improve error messages
Gabriella439 Mar 12, 2020
42fd753
Use a `PreferAnnotation` type
Gabriella439 Mar 13, 2020
a85065e
Preserve the original `with` expression
Gabriella439 Mar 13, 2020
498523e
Fix error message for `MustCombineRecord`
Gabriella439 Mar 13, 2020
763c614
Fix build failure for benchmark suite
Gabriella439 Mar 13, 2020
e35205e
Fix test build failure
Gabriella439 Mar 13, 2020
635d39b
Fix property test failure
Gabriella439 Mar 13, 2020
1d1f2ae
Fix benchmark build failure
Gabriella439 Mar 13, 2020
9b94d67
Add missing documentation for `PreferAnnotation`
Gabriella439 Mar 13, 2020
348e55b
Add tutorial documentation for `with`
Gabriella439 Mar 13, 2020
6a5b109
Fix `dhall-nix` build failure
Gabriella439 Mar 14, 2020
eb040ee
Merge branch 'master' into gabriel/with_2
mergify[bot] Mar 15, 2020
aa8114c
Merge branch 'master' into gabriel/with_2
sjakobi Mar 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dhall-bash/src/Dhall/Bash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ dhallToStatement expr0 var0 = go (Dhall.Core.normalize expr0)
go e@(Project {}) = Left (UnsupportedStatement e)
go e@(Assert {}) = Left (UnsupportedStatement e)
go e@(Equivalent {}) = Left (UnsupportedStatement e)
go e@(With {}) = Left (UnsupportedStatement e)
go e@(ImportAlt {}) = Left (UnsupportedStatement e)

{-| Compile a Dhall expression to a Bash expression
Expand Down
12 changes: 9 additions & 3 deletions dhall-json/src/Dhall/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -996,11 +996,11 @@ convertToHomogeneousMaps (Conversion {..}) e0 = loop (Core.normalize e0)
a' = loop a
b' = loop b

Core.Prefer a b ->
Core.Prefer a' b'
Core.Prefer a b c ->
Core.Prefer a b' c'
where
a' = loop a
b' = loop b
c' = loop c

Core.RecordCompletion a b ->
Core.RecordCompletion a' b'
Expand Down Expand Up @@ -1042,6 +1042,12 @@ convertToHomogeneousMaps (Conversion {..}) e0 = loop (Core.normalize e0)
a' = loop a
b' = loop b

Core.With a b c ->
Core.With a' b c'
where
a' = loop a
c' = loop c

Core.ImportAlt a b ->
Core.ImportAlt a' b'
where
Expand Down
20 changes: 15 additions & 5 deletions dhall-nix/src/Dhall/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ import Data.Fix (Fix(..))
import Data.Traversable (for)
import Data.Typeable (Typeable)
import Data.Void (Void, absurd)
import Dhall.Core (Chunks(..), Const(..), DhallDouble(..), Expr(..), MultiLet(..), Var(..))
import Dhall.Core
( Chunks(..)
, Const(..)
, DhallDouble(..)
, Expr(..)
, MultiLet(..)
, PreferAnnotation(..)
, Var(..)
)
import Nix.Atoms (NAtom(..))
import Nix.Expr
( Antiquoted(..)
Expand Down Expand Up @@ -522,12 +530,12 @@ dhallToNix e = loop (Dhall.Core.normalize e)
let map_ = Fix (NBinary NApp "map" (Fix (NAbs "k" (Fix (NSet NNonRecursive setBindings)))))
let toMap = Fix (NAbs "kvs" (Fix (NBinary NApp map_ ks)))
return (Fix (NBinary NApp toMap a'))
loop (Prefer a b) = do
a' <- loop a
loop (Prefer _ b c) = do
b' <- loop b
return (Fix (NBinary NUpdate a' b'))
c' <- loop c
return (Fix (NBinary NUpdate b' c'))
loop (RecordCompletion a b) = do
loop (Annot (Prefer (Field a "default") b) (Field a "Type"))
loop (Annot (Prefer PreferFromCompletion (Field a "default") b) (Field a "Type"))
loop (Field (Union kts) k) =
case Dhall.Map.lookup k kts of
-- If the selected alternative has an associated payload, then we
Expand Down Expand Up @@ -561,6 +569,8 @@ dhallToNix e = loop (Dhall.Core.normalize e)
return untranslatable
loop (Equivalent _ _) = do
return untranslatable
loop a@With{} = do
loop (Dhall.Core.desugarWith a)
loop (ImportAlt a _) = loop a
loop (Note _ b) = loop b
loop (Embed x) = absurd x
2 changes: 1 addition & 1 deletion dhall/benchmark/deep-nested-large-record/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ unionPerformance prelude = Gauge.whnf TypeCheck.typeOf expr
Nothing
(prelude `Core.Field` "types" `Core.Field` "Big")
)
(Core.Prefer "big" "big")
(Core.Prefer Core.PreferFromSource "big" "big")
)
)
"x"
Expand Down
2 changes: 1 addition & 1 deletion dhall/dhall-lang
Submodule dhall-lang updated 40 files
+5 −3 .github/CONTRIBUTING.md
+8 −6 README.md
+1 −0 docs/howtos/index.md
+56 −0 docs/howtos/migrations/Deprecation-of-quoted-paths-in-URLs.md
+3 −3 nixops/dhall-haskell.json
+27 −2 release.nix
+9 −4 standard/dhall.abnf
+64 −0 standard/record.md
+6 −3 tests/README.md
+12 −0 tests/normalization/success/WithRecordValueA.dhall
+1 −0 tests/normalization/success/WithRecordValueB.dhall
+1 −1 tests/normalization/success/unit/TextShowInterpolatedA.dhall
+1 −1 tests/normalization/success/unit/TextShowInterpolatedB.dhall
+1 −0 tests/normalization/success/unit/WithA.dhall
+1 −0 tests/normalization/success/unit/WithB.dhall
+4 −0 tests/normalization/success/unit/WithChainedA.dhall
+1 −0 tests/normalization/success/unit/WithChainedB.dhall
+12 −0 tests/normalization/success/unit/WithNestedA.dhall
+1 −0 tests/normalization/success/unit/WithNestedB.dhall
+6 −0 tests/normalization/success/unit/WithPriorityA.dhall
+1 −0 tests/normalization/success/unit/WithPriorityB.dhall
+1 −0 tests/parser/failure/assertBinding.dhall
+5 −0 tests/parser/success/text/singleQuoteConcatA.dhall
+1 −0 tests/parser/success/text/singleQuoteConcatB.dhallb
+1 −0 tests/parser/success/text/singleQuoteConcatB.diag
+6 −0 tests/parser/success/unit/WithA.dhall
+1 −0 tests/parser/success/unit/WithB.dhallb
+1 −0 tests/parser/success/unit/WithB.diag
+8 −0 tests/parser/success/unit/WithMultipleA.dhall
+1 −0 tests/parser/success/unit/WithMultipleB.dhallb
+1 −0 tests/parser/success/unit/WithMultipleB.diag
+13 −0 tests/parser/success/unit/WithPrecedenceA.dhall
+1 −0 tests/parser/success/unit/WithPrecedenceB.dhallb
+1 −0 tests/parser/success/unit/WithPrecedenceB.diag
+6 −0 tests/type-inference/failure/unit/WithInvalidOverrideA.dhall
+5 −0 tests/type-inference/failure/unit/WithUnderscore.dhall
+4 −0 tests/type-inference/success/unit/WithNewFieldA.dhall
+1 −0 tests/type-inference/success/unit/WithNewFieldB.dhall
+4 −0 tests/type-inference/success/unit/WithNewTypeA.dhall
+1 −0 tests/type-inference/success/unit/WithNewTypeB.dhall
14 changes: 9 additions & 5 deletions dhall/src/Dhall/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Dhall.Syntax
, ImportMode(..)
, ImportType(..)
, MultiLet(..)
, PreferAnnotation(..)
, Scheme(..)
, URL(..)
, Var(..)
Expand All @@ -65,10 +66,10 @@ import qualified Data.ByteString
import qualified Data.ByteString.Lazy
import qualified Data.Sequence
import qualified Data.Text as Text
import qualified Dhall.Syntax
import qualified Dhall.Crypto
import qualified Dhall.Map
import qualified Dhall.Set
import qualified Dhall.Syntax as Syntax
import qualified Text.Printf as Printf

{-| Supported version strings
Expand Down Expand Up @@ -306,7 +307,7 @@ decodeExpressionInternal decodeEmbed = go
6 -> return TextAppend
7 -> return ListAppend
8 -> return (Combine Nothing)
9 -> return Prefer
9 -> return (Prefer PreferFromSource)
10 -> return CombineTypes
11 -> return ImportAlt
12 -> return Equivalent
Expand Down Expand Up @@ -759,7 +760,7 @@ encodeExpressionInternal encodeEmbed = go
Combine _ l r ->
encodeOperator 8 l r

Prefer l r ->
Prefer _ l r ->
encodeOperator 9 l r

CombineTypes l r ->
Expand Down Expand Up @@ -910,7 +911,7 @@ encodeExpressionInternal encodeEmbed = go
: concatMap encodeBinding (toList as) ++ [ go b₁ ]
)
where
MultiLet as b₁ = Dhall.Syntax.multiLet a₀ b₀
MultiLet as b₁ = Syntax.multiLet a₀ b₀

encodeBinding (Binding _ x _ mA₀ _ a) =
[ Encoding.encodeString x
Expand Down Expand Up @@ -939,6 +940,9 @@ encodeExpressionInternal encodeEmbed = go
(go t)
(go _T)

a@With{} ->
go (Syntax.desugarWith a)

Note _ b ->
go b

Expand Down Expand Up @@ -1104,7 +1108,7 @@ encodeImport import_ =
Nothing ->
Encoding.encodeNull
Just h ->
encodeExpressionInternal encodeImport (Dhall.Syntax.denote h)
encodeExpressionInternal encodeImport (Syntax.denote h)

scheme₁ = case scheme₀ of
HTTP -> 0
Expand Down
5 changes: 4 additions & 1 deletion dhall/src/Dhall/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Dhall.Core (
, Binding(..)
, makeBinding
, Chunks(..)
, PreferAnnotation(..)
, Expr(..)

-- * Normalization
Expand Down Expand Up @@ -66,6 +67,7 @@ module Dhall.Core (
, Eval.textShow
, censorExpression
, censorText
, Syntax.desugarWith
) where

import Control.Exception (Exception)
Expand All @@ -82,8 +84,9 @@ import Lens.Family (over)
import Prelude hiding (succ)

import qualified Control.Exception
import qualified Dhall.Eval as Eval
import qualified Data.Text
import qualified Dhall.Eval as Eval
import qualified Dhall.Syntax as Syntax

-- | Pretty-print a value
pretty :: Pretty a => a -> Text
Expand Down
45 changes: 31 additions & 14 deletions dhall/src/Dhall/Diff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ import Dhall.Set (Set)
import Dhall.Pretty.Internal (Ann)
import Numeric.Natural (Natural)

import qualified Data.Algorithm.Diff as Algo.Diff
import qualified Data.Algorithm.Diff as Algo.Diff
import qualified Data.List.NonEmpty
import qualified Data.Set
import qualified Data.Text
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Dhall.Normalize
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Dhall.Map
import qualified Dhall.Normalize as Normalize
import qualified Dhall.Pretty.Internal as Internal
import qualified Dhall.Set
import qualified Dhall.Pretty.Internal as Internal
import qualified Dhall.Syntax as Syntax

{-| This type is a `Doc` enriched with a `same` flag to efficiently track if
any difference was detected
Expand Down Expand Up @@ -158,8 +159,8 @@ rparen = token Internal.rparen
diffNormalized :: (Eq a, Pretty a) => Expr s a -> Expr s a -> Diff
diffNormalized l0 r0 = Dhall.Diff.diff l1 r1
where
l1 = Dhall.Normalize.alphaNormalize (Dhall.Normalize.normalize l0)
r1 = Dhall.Normalize.alphaNormalize (Dhall.Normalize.normalize r0)
l1 = Normalize.alphaNormalize (Normalize.normalize l0)
r1 = Normalize.alphaNormalize (Normalize.normalize r0)

diffPrimitive :: Eq a => (a -> Diff) -> a -> a -> Diff
diffPrimitive f l r
Expand Down Expand Up @@ -619,6 +620,12 @@ skeleton (Project {}) =
<> ignore
<> " "
<> rbrace
skeleton (With {}) =
ignore
<> " "
<> keyword "with"
<> " "
<> ignore
skeleton x = token (Pretty.pretty x)

mismatch :: Pretty a => Expr s a -> Expr s a -> Diff
Expand Down Expand Up @@ -729,8 +736,8 @@ diffAnnotatedExpression (Merge aL bL cL) (Merge aR bR cR) = align doc
where
doc = keyword "merge"
<> " "
<> format " " (diffImportExpression aL aR)
<> format " " (diffImportExpression bL bR)
<> format " " (diffWithExpression aL aR)
<> format " " (diffWithExpression bL bR)
<> diffMaybe (colon <> " ") diffApplicationExpression cL cR
diffAnnotatedExpression l@(Merge {}) r =
mismatch l r
Expand All @@ -740,7 +747,7 @@ diffAnnotatedExpression (ToMap aL bL) (ToMap aR bR) = align doc
where
doc = keyword "toMap"
<> " "
<> format " " (diffImportExpression aL aR)
<> format " " (diffWithExpression aL aR)
<> diffMaybe (colon <> " ") diffApplicationExpression bL bR
diffAnnotatedExpression l@(ToMap {}) r =
mismatch l r
Expand Down Expand Up @@ -897,7 +904,7 @@ diffPreferExpression :: (Eq a, Pretty a) => Expr Void a -> Expr Void a -> Diff
diffPreferExpression l@(Prefer {}) r@(Prefer {}) =
enclosed' " " (operator "⫽" <> " ") (docs l r)
where
docs (Prefer aL bL) (Prefer aR bR) =
docs (Prefer _ aL bL) (Prefer _ aR bR) =
Data.List.NonEmpty.cons (diffCombineTypesExpression aL aR) (docs bL bR)
docs aL aR =
pure (diffCombineTypesExpression aL aR)
Expand Down Expand Up @@ -989,26 +996,36 @@ diffApplicationExpression l@(App {}) r@(App {}) =
enclosed' mempty mempty (Data.List.NonEmpty.reverse (docs l r))
where
docs (App aL bL) (App aR bR) =
Data.List.NonEmpty.cons (diffImportExpression bL bR) (docs aL aR)
Data.List.NonEmpty.cons (diffWithExpression bL bR) (docs aL aR)
docs (Some aL) (Some aR) =
diffImportExpression aL aR :| [ builtin "Some" ]
diffWithExpression aL aR :| [ builtin "Some" ]
docs aL aR@(Some {}) =
pure (mismatch aL aR)
docs aL@(Some {}) aR =
pure (mismatch aL aR)
docs aL aR =
pure (diffImportExpression aL aR)
pure (diffWithExpression aL aR)
diffApplicationExpression l@(App {}) r =
mismatch l r
diffApplicationExpression l r@(App {}) =
mismatch l r
diffApplicationExpression (Some l) (Some r) =
enclosed' mempty mempty (builtin "Some" :| [ diffImportExpression l r ])
enclosed' mempty mempty (builtin "Some" :| [ diffWithExpression l r ])
diffApplicationExpression l@(Some {}) r =
mismatch l r
diffApplicationExpression l r@(Some {}) =
mismatch l r
diffApplicationExpression l r =
diffWithExpression l r

diffWithExpression :: (Eq a, Pretty a) => Expr Void a -> Expr Void a -> Diff
diffWithExpression l@With{} r@With{} =
diffWithExpression (Syntax.desugarWith l) (Syntax.desugarWith r)
diffWithExpression l r@With{} =
mismatch l r
diffWithExpression l@With{} r =
mismatch l r
diffWithExpression l r =
diffImportExpression l r

diffImportExpression :: (Eq a, Pretty a) => Expr Void a -> Expr Void a -> Diff
Expand Down
24 changes: 14 additions & 10 deletions dhall/src/Dhall/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ import Data.Semigroup (Semigroup(..))
import Data.Sequence (Seq, ViewL(..), ViewR(..))
import Data.Text (Text)
import Data.Void (Void)
import Dhall.Map (Map)
import Dhall.Set (Set)
import GHC.Natural (Natural)
import Prelude hiding (succ)

import Dhall.Syntax
( Binding(..)
, Expr(..)
, Chunks(..)
, Const(..)
, DhallDouble(..)
, PreferAnnotation(..)
, Var(..)
)

import Dhall.Map (Map)
import Dhall.Set (Set)
import GHC.Natural (Natural)
import Prelude hiding (succ)

import qualified Data.Char
import qualified Data.Sequence as Sequence
import qualified Data.Set
Expand Down Expand Up @@ -688,10 +688,10 @@ eval !env t0 =
vCombine mk (eval env t) (eval env u)
CombineTypes t u ->
vCombineTypes (eval env t) (eval env u)
Prefer t u ->
Prefer _ t u ->
vPrefer env (eval env t) (eval env u)
RecordCompletion t u ->
eval env (Annot (Prefer (Field t "default") u) (Field t "Type"))
eval env (Annot (Prefer PreferFromCompletion (Field t "default") u) (Field t "Type"))
Merge x y ma ->
case (eval env x, eval env y, fmap (eval env) ma) of
(VRecordLit m, VInject _ k mt, _)
Expand Down Expand Up @@ -736,6 +736,8 @@ eval !env t0 =
VAssert (eval env t)
Equivalent t u ->
VEquivalent (eval env t) (eval env u)
e@With{} ->
eval env (Syntax.desugarWith e)
Note _ e ->
eval env e
ImportAlt t _ ->
Expand Down Expand Up @@ -1117,7 +1119,7 @@ quote !env !t0 =
VCombineTypes t u ->
CombineTypes (quote env t) (quote env u)
VPrefer t u ->
Prefer (quote env t) (quote env u)
Prefer PreferFromSource (quote env t) (quote env u)
VMerge t u ma ->
Merge (quote env t) (quote env u) (fmap (quote env) ma)
VToMap t ma ->
Expand Down Expand Up @@ -1296,8 +1298,8 @@ alphaNormalize = goEnv EmptyNames
Combine m (go t) (go u)
CombineTypes t u ->
CombineTypes (go t) (go u)
Prefer t u ->
Prefer (go t) (go u)
Prefer b t u ->
Prefer b (go t) (go u)
RecordCompletion t u ->
RecordCompletion (go t) (go u)
Merge x y ma ->
Expand All @@ -1312,6 +1314,8 @@ alphaNormalize = goEnv EmptyNames
Assert (go t)
Equivalent t u ->
Equivalent (go t) (go u)
With e k v ->
With (go e) k (go v)
Note s e ->
Note s (go e)
ImportAlt t u ->
Expand Down
Loading