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

A type level equivalent of the with keyword #1378

Open
Tristano8 opened this issue May 21, 2024 · 11 comments
Open

A type level equivalent of the with keyword #1378

Tristano8 opened this issue May 21, 2024 · 11 comments

Comments

@Tristano8
Copy link

When interacting with a library that may not have the most ergonomic types, a useful escape hatch is to overwrite fields using with. This gets unwieldy very quickly, though, when I need to reach for a function like List/Map to apply this same transformer items in a list; you need to supply the input and output types as arguments to the function. If i'm decorating/changing an existing type, the only thing I know to do here is infer the new decorated type with the compiler, and then paste that definition as an argument to List/Map. If with had a type level equivalent, the type-level and term-level implementations would match and this would be much more ergonomic.

@JackKelly-Bellroy
Copy link
Contributor

Does this also mean that we probably want a typelevel version of //?

@winitzki
Copy link
Contributor

Can you give a code example that shows what the proposal would look like?

@JackKelly-Bellroy
Copy link
Contributor

The primary driver of this request is that the types in https://github.com/jcouyang/dhall-aws-cloudformation/ are sometimes out of date, or AWS's own definitions are incorrect, and we need to overwrite the type of a single (possibly nested) field. This means we cannot use //\\ because Dhall will complain about a record field collision.

Given a type x = { a : A, r : { b : B, c : C }, I want to be able to construct a type { a : A, r : { b : B', c : C } without having to rewrite the entire structure by hand. One way would be to extend the with syntax, so that `x with a.b = B' works on record types.

Additionally, a type-level version of // could behave something like: { a : A, b : B } /// { b : B', c : C } = { a : A, b : B', c : C }.

@winitzki
Copy link
Contributor

Makes sense to me.

@Gabriella439
Copy link
Contributor

Yeah, this seems like a reasonable request

@winitzki
Copy link
Contributor

The with keyword will then work in the same way for record values and for record types. This makes sense to me, but this contradicts the current design of Dhall, where record values and record types require different operations, for example /\ and //\\.

Record types are always distinct from record values, already at the level of syntax. Do we actually need separate operators like // and /// for them?

Right now Dhall has separate operators /\ and //\\ for record values and record types. Can we not collapse them into a single operator /\? What is the motivation for keeping them separate?

Should we simplify the design of Dhall and have just one set of operations that works both for record types and record values?

@JackKelly-Bellroy
Copy link
Contributor

I think something with-like is useful but I don't have a strong opinion on whether it should overload the with keyword or introduce another keyword. A fresh keyword would be more in keeping with the language's current design, I agree.

@Gabriella439
Copy link
Contributor

I think it would be okay to combine and , too, but still support for backwards compatibility

@JackKelly-Bellroy
Copy link
Contributor

Should the . operator then be allowed to index into record types?

Currently:

⊢ { A : Text, B : Natural }.A

Error: Not a record or a union

@Gabriella439
Copy link
Contributor

I think so, and there's actually already an issue for that, too: #1079

@winitzki
Copy link
Contributor

I'd say, we should go through the entire records / unions syntax to see if things can be simplified. I could do that given time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants