-
Notifications
You must be signed in to change notification settings - Fork 57
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
Add record declaration syntax support #1641
Comments
After implementing we might not need or want a separate syntax for record declarations. With the proposed syntax for named arguments a record can be defined as: type R :=
Rctr : (cfield1 : X1) -> (cfield2 : X2) -> R; If there is only one constructor, accessor functions can be automatically generated, maybe with an additional specifier like record
type R :=
Rctr : (cfield1 : X1) -> (cfield2 : X2) -> R; |
But perhaps the named argument syntax becomes inconvenient with a large number of fields. So we may actually want the proposed syntactic sugar: record
type R := Rctr {
cfield1 : X1;
cfield2 : X2;
}; |
I actually think this is much more readable: trait
type Encoding (A : Type) := MkEncoding {
encode : A -> Field;
decode : Field -> A;
}; than this: trait
type Encoding (A : Type) :=
MkEncoding :
(encode : A -> Field) ->
(decode : Field -> A) ->
Encoding A; |
I agree with your points. A couple of extra comments:
|
We use braces often for blocks of declarations, e.g., in lets. The fields of a constructor are a bit like such a block of signature declarations. Maybe we could use trait
type Encoding (A : Type) := MkEncoding with {
encode : A -> Field;
decode : Field -> A;
}; |
we are not using braces in lets. What blocks of declarations are you referring to? |
I wouldn't strongly oppose to braces however. I think they have a big pro, which is that they are often used for record-like structures in other languages |
No? Damn, I'm not keeping up with the syntax. |
I think I got confused with Core syntax. But we're using braces in the lambdas, and that's a bit like introducing a block of clause definitions (but the analogy is weaker here). |
Braces seem nicer because multi-line parentheses look kind of weird. The parenthesisation is also easier to track visually if you follow the convention:
|
agreed |
- Closes #1641 This pr adds the option to declare constructors with fields. E.g. ``` type Pair (A B : Type) := | mkPair { fst : A; snd : B }; ``` Which is desugared to ``` type Pair (A B : Type) := | mkPair : (fst : A) -> (snd : B) -> Pair A B; ``` making it possible to write ` mkPair (fst := 1; snd := 2)`. Mutli-constructor types are also allowed to have fields.
Example:
The text was updated successfully, but these errors were encountered: