Comments (6)
I recently added support for this by introducing a new //
operator in dhall-1.3.0
which does a right-biased and non-recursive record merge. You can use this operator to implement option defaulting by first defining a record containing default options like this:
$ cat ./defaults
{ foo = 1, bar = "ABC", baz = True }
... and then you can override those defaults using //
, like this:
$ dhall <<< './defaults // { foo = 2 }'
{ bar : Text, baz : Bool, foo : Integer }
{ bar = "ABC", baz = True, foo = 2 }
from dhall-haskell.
Yes, it looks like //
operator solves the problem for the flat records.
What do you think can be done for the nested records, e.g. div
tag can have other tags as children?
from dhall-haskell.
For nested records you would have to do something like this:
λ(defaults : { foo : { bar : Integer }, baz : Bool })
→ defaults // { foo = defaults.foo // { bar = 1 } }
from dhall-haskell.
I am afraid this defaults solution looks a bit verbose :(
Do you think, having some implicit field construct in the language is overkill?
Anyhow, thanks for your answers! Feel free to close the issue if you think that nothing can be done.
from dhall-haskell.
The main reason why Dhall does not have implicit parameters is because Dhall has a firm "no subtyping" rule to keep the language easy to teach. Implicit parameters are one form of subtyping, because multiple record types would be able to type check as valid arguments to the same function. For example:
-- This would be valid:
(λ(r : { foo = 1 }) → r) {=}
-- ... and so would this:
(λ(r : { foo = 1 }) → r) { foo = 2 }
In order for the above type-application to work we'd need to:
- add subtyping rules so that
{} ⊂ { foo = 1 }
,{ foo : Integer } ⊂ { foo = 1 }
- teach new users how to reason about these new subtyping rules, which is difficult (I'm not even sure I understand what they should be)
... and now a subexpression {=}
would no longer have a unique type or even a principal type
Reducing verbosity is very intentionally not one of the design priorities of the language. You can find the list of design priorities here:
Also, in this specific case it conflicts with two other priorities which are:
- Beginner friendliness - Adding this makes it more difficult to teach beginners how to reason about Dhall's type-checking semantics so that they can understand type errors
- Consistency - Now there would be two separate ways of doing the same thing (providing defaults). One being the
(//)
operator and the other being default fields
from dhall-haskell.
This makes perfect sense. Thanks!
from dhall-haskell.
Related Issues (20)
- Can not install dhall-lsp-server HOT 2
- Allow hnix 0.17
- Hackage build failed for dhall-toml-1.0.3
- Get back into Stackage Nightly with GHC 9.8 HOT 1
- Missing binaries in release HOT 1
- Hackage release for dhall-lsp-server HOT 1
- Build failure on GHC 9.8.1: 'Illegal invisible type variable binder'
- Report imports HOT 2
- dhall-docs: Prelude.head: empty list
- Suggesting a new construct for dealing with optional fields in Dhall defaults
- dhall-json bound on aeson can be relaxed
- Questions regarding the right strategy for upgrading to ghc-9.8
- Build failure on macos-latest stack.yaml HOT 3
- Is there a specification for dhall-to-{json,yaml} and {json,yaml}-to-dhall? HOT 2
- Pretty-printer: escape unprintable characters HOT 2
- `with` expressions regressed HOT 10
- to-directory-tree with Empty Map causes an error
- Guide to compile dhall to JS backend, wasm backend and GHCJS HOT 1
- dhall format produces unparseable file when `Some` is used
- Issue on install git bash on windows 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dhall-haskell.