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

Constraints for generics (generics details 3) #818

Merged
merged 95 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
369d83d
Filling out template with PR 818
josh11b Sep 7, 2021
51c3348
Basic proposal
josh11b Sep 7, 2021
f5a72e1
Structural interface -> named constraints
josh11b Sep 7, 2021
872f7e4
Big dump of work-in-progress text
josh11b Sep 8, 2021
393eb1e
Checkpoint progress.
josh11b Sep 9, 2021
442f689
Checkpoint progress.
josh11b Sep 9, 2021
b96126c
Type checking by induction
josh11b Sep 9, 2021
0df8b9f
Checkpoint progress.
josh11b Sep 14, 2021
250c765
Checkpoint progress.
josh11b Sep 14, 2021
6600ab0
Checkpoint progress.
josh11b Sep 15, 2021
3112fad
Checkpoint progress.
josh11b Sep 15, 2021
b2c2914
Support == type
josh11b Sep 15, 2021
4243b78
Checkpoint progress.
josh11b Sep 15, 2021
65c2dcd
Checkpoint progress.
josh11b Sep 16, 2021
09cefc4
Checkpoint progress.
josh11b Sep 16, 2021
7810adf
Checkpoint progress.
josh11b Sep 18, 2021
691717a
Checkpoint progress.
josh11b Sep 20, 2021
2a24dae
Checkpoint progress.
josh11b Sep 20, 2021
7b96bc0
typeof -> __typeof__
josh11b Sep 20, 2021
203ebf0
Checkpoint progress.
josh11b Sep 21, 2021
74a9d9f
Filling out template with PR 818
josh11b Sep 7, 2021
85034d0
Basic proposal
josh11b Sep 7, 2021
14f9d3d
Structural interface -> named constraints
josh11b Sep 7, 2021
8d4be98
Big dump of work-in-progress text
josh11b Sep 8, 2021
9471f42
Checkpoint progress.
josh11b Sep 9, 2021
3889700
Checkpoint progress.
josh11b Sep 9, 2021
01a414b
Type checking by induction
josh11b Sep 9, 2021
e160c11
Checkpoint progress.
josh11b Sep 14, 2021
0c374cf
Checkpoint progress.
josh11b Sep 14, 2021
ae61a70
Checkpoint progress.
josh11b Sep 15, 2021
a4afaaf
Checkpoint progress.
josh11b Sep 15, 2021
361deef
Support == type
josh11b Sep 15, 2021
833340a
Checkpoint progress.
josh11b Sep 15, 2021
cb5c302
Checkpoint progress.
josh11b Sep 16, 2021
f6ed58b
Checkpoint progress.
josh11b Sep 16, 2021
49c3896
Checkpoint progress.
josh11b Sep 18, 2021
6790e14
Checkpoint progress.
josh11b Sep 20, 2021
c985ec0
Checkpoint progress.
josh11b Sep 20, 2021
a627074
typeof -> __typeof__
josh11b Sep 20, 2021
e8da578
Checkpoint progress.
josh11b Sep 21, 2021
27faccb
Must be a legal type argument constraint
josh11b Sep 22, 2021
e88b1a4
Merge remote-tracking branch 'upstream/trunk' into constraints
josh11b Sep 22, 2021
684023e
Merge branch 'constraints' of github.com:josh11b/carbon-lang into con…
josh11b Sep 22, 2021
0299c9d
Merge remote-tracking branch 'upstream/trunk' into constraints
josh11b Sep 22, 2021
ad8c8f9
Checkpoint progress.
josh11b Sep 22, 2021
18ee8cb
Checkpoint progress.
josh11b Sep 24, 2021
3cec416
Add Python script for testing arg passing algorithm
josh11b Sep 30, 2021
e01ae86
arg_pass_test updates, adding broken example
josh11b Oct 1, 2021
6588164
Checkpoint progress.
josh11b Oct 10, 2021
ed365d8
Checkpoint progress.
josh11b Oct 11, 2021
ee526b1
Merge remote-tracking branch 'upstream/trunk' into constraints
josh11b Oct 12, 2021
3d24e91
Checkpoint progress.
josh11b Oct 12, 2021
fb78d02
Remove dead ends
josh11b Oct 12, 2021
b015eb2
Checkpoint progress.
josh11b Oct 12, 2021
a542781
Checkpoint progress.
josh11b Oct 12, 2021
195f63e
Checkpoint progress.
josh11b Oct 12, 2021
fa180a2
Type equality examples and `observe` declarations
josh11b Oct 13, 2021
2b640d9
Checkpoint progress.
josh11b Oct 13, 2021
c470f05
Add regular_equivalence_classes.md
josh11b Oct 13, 2021
6b874cd
Checkpoint progress.
josh11b Oct 15, 2021
e2f2533
Merge remote-tracking branch 'upstream/trunk' into constraints
josh11b Oct 15, 2021
e30010e
Checkpoint progress.
josh11b Oct 15, 2021
f33b70e
Alternatives from #780.
josh11b Oct 15, 2021
34954df
Alternative keywords
josh11b Oct 15, 2021
7554f59
Alternatives to .Self
josh11b Oct 15, 2021
f9f3b7a
Last alternative
josh11b Oct 15, 2021
c1f4a4a
Merge remote-tracking branch 'upstream/trunk' into constraints
josh11b Oct 15, 2021
6bf442e
Another irregular equivalence class example
josh11b Oct 18, 2021
2e99b98
Open question
josh11b Oct 18, 2021
6127371
Checkpoint progress.
josh11b Oct 18, 2021
bc53ee0
Add keywords
josh11b Oct 21, 2021
224c72e
Apply suggestions from code review
josh11b Oct 21, 2021
8e1badc
Fix formatting
josh11b Oct 21, 2021
b32607f
Some examples are not regular
josh11b Oct 21, 2021
13e87fb
Checkpoint progress.
josh11b Oct 21, 2021
73bbc1c
Rationale for no opt-in implied constraints
josh11b Oct 21, 2021
32989f4
Incorporate feedback
josh11b Oct 21, 2021
a38324d
Incorporate feedback
josh11b Oct 21, 2021
ff087ae
Apply suggestions from code review
josh11b Oct 23, 2021
45623fe
Remove restating constraints
josh11b Oct 25, 2021
6737d74
Can't implement constraint type-of-types manually
josh11b Oct 26, 2021
9bf7ca4
Clarify what is allowed in named constraints for generics
josh11b Oct 26, 2021
769a387
A bit more
josh11b Oct 26, 2021
33c3a9e
Apply conclusions of Discord discussions.
josh11b Oct 27, 2021
6f98f05
Merge remote-tracking branch 'upstream/trunk' into constraints
josh11b Oct 27, 2021
5b0c887
Apply suggestions from code review
josh11b Oct 28, 2021
9b14d23
Fix formatting
josh11b Oct 28, 2021
d4bd3d2
Apply suggestions from code review
josh11b Oct 28, 2021
33accda
Template constraints
josh11b Oct 29, 2021
d072a57
Address comments from review
josh11b Oct 29, 2021
a56ef92
Implement suggestion from review
josh11b Oct 29, 2021
2e2b8a2
Update docs/design/generics/details.md
josh11b Oct 30, 2021
fd3b790
Clarify choice of `where`
josh11b Oct 30, 2021
1e2ac59
Merge branch 'constraints' of github.com:josh11b/carbon-lang into con…
josh11b Oct 30, 2021
6e568d3
Fix formatting
josh11b Oct 30, 2021
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,295 changes: 1,227 additions & 68 deletions docs/design/generics/details.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/design/generics/goals.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ are complicated and

### Interfaces are nominal

Interfaces can either be structural, as in Go, or nominal, as in Rust and Swift.
Interfaces can either be [structural](terminology.md#structural-interfaces), as
in Go, or [nominal](terminology.md#nominal-interfaces), as in Rust and Swift.
Structural interfaces match any type that has the required methods, whereas
nominal interfaces only match if there is an explicit declaration stating that
the interface is implemented for that specific type. Carbon will support nominal
Expand Down
49 changes: 36 additions & 13 deletions docs/design/generics/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ pointers to other design documents that dive deeper into individual topics.
- [Generic type parameters](#generic-type-parameters)
- [Requiring or extending another interface](#requiring-or-extending-another-interface)
- [Combining interfaces](#combining-interfaces)
- [Structural interfaces](#structural-interfaces)
- [Named constraints](#named-constraints)
- [Type erasure](#type-erasure)
- [Adapting types](#adapting-types)
- [Interface input and output types](#interface-input-and-output-types)
- [Associated types](#associated-types)
- [Parameterized interfaces](#parameterized-interfaces)
- [Constraints](#constraints)
- [Future work](#future-work)
- [References](#references)

Expand Down Expand Up @@ -84,9 +85,9 @@ Summary of how Carbon generics work:
- The `&` operation on type-of-types allows you conveniently combine
interfaces. It gives you all the names that don't conflict.
- You may also declare a new type-of-type directly using
["structural interfaces"](terminology.md#structural-interfaces). Structural
interfaces can express requirements that multiple interfaces be implemented,
and give you control over how name conflicts are handled.
["named constraints"](terminology.md#named-constraints). Named constraints
can express requirements that multiple interfaces be implemented, and give
you control over how name conflicts are handled.
- Alternatively, you may resolve name conflicts by using a qualified syntax to
directly call a function from a specific interface.

Expand Down Expand Up @@ -251,7 +252,7 @@ the constraint on the type is that it must implement the interface `Comparable`.
A type-of-type also defines a set of names and a mapping to corresponding
qualified names. You may combine interfaces into new type-of-types using
[the `&` operator](#combining-interfaces) or
[structural interfaces](#structural-interfaces).
[named constraints](#named-constraints).

### Generic functions

Expand Down Expand Up @@ -406,16 +407,16 @@ fn BothDraws[T:! Renderable & EndOfGame](game_state: T*) {
}
```

#### Structural interfaces
#### Named constraints

You may also declare a new type-of-type directly using
["structural interfaces"](terminology.md#structural-interfaces). Structural
interfaces can express requirements that multiple interfaces be implemented, and
give you control over how name conflicts are handled. Structural interfaces have
other applications and capabilities not covered here.
["named constraints"](terminology.md#named-constraints). Named constraints can
express requirements that multiple interfaces be implemented, and give you
control over how name conflicts are handled. Named constraints have other
applications and capabilities not covered here.

```
structural interface Combined {
constraint Combined {
impl as Renderable;
impl as EndOfGame;
alias Draw_Renderable = Renderable.Draw;
Expand All @@ -431,7 +432,7 @@ fn CallItAll[T:! Combined](game_state: T*, int winner) {
}
game_state->Draw_Renderable();
// Can still use qualified syntax for names
// not defined in the structural interface
// not defined in the named constraint
return game_state->(Renderable.Center)();
}
```
Expand Down Expand Up @@ -561,9 +562,30 @@ fn FindInVector[T:! Type, U:! Equatable(T)](v: Vector(T), needle: U)
fn CompileError[T:! Type, U:! Equatable(T)](x: U) -> T;
```

### Constraints

Type-of-types can be further constrained using a `where` clause:

```
fn FindFirstPrime[T:! Container where .Element == i32]
(c: T, i: i32) -> Optional(i32) {
// The elements of `c` have type `T.Element`, which is `i32`.
...
}

fn PrintContainer[T:! Container where .Element is Printable](c: T) {
// The type of the elements of `c` is not known, but we do know
// that type satisfies the `Printable` interface.
...
}
```

Constraints limit the types that the generic function can operate on, but
increase the knowledge that may be used in the body of the function to operate
on values of those types.

## Future work

- Other kinds of constraints will be finalized.
- Implementations can be parameterized to apply to multiple types. These
implementations would be restricted to various conditions are true for the
parameters. When there are two implementations that can apply, there is a
Expand All @@ -583,3 +605,4 @@ fn CompileError[T:! Type, U:! Equatable(T)](x: U) -> T;

- [#524: Generics overview](https://github.com/carbon-language/carbon-lang/pull/524)
- [#731: Generics details 2: adapters, associated types, parameterized interfaces](https://github.com/carbon-language/carbon-lang/pull/731)
- [#818: Constraints for generics (generics details 3)](https://github.com/carbon-language/carbon-lang/pull/818)
15 changes: 13 additions & 2 deletions docs/design/generics/terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- [Interface](#interface)
- [Structural interfaces](#structural-interfaces)
- [Nominal interfaces](#nominal-interfaces)
- [Named constraints](#named-constraints)
- [Associated entity](#associated-entity)
- [Impls: Implementations of interfaces](#impls-implementations-of-interfaces)
- [Compatible types](#compatible-types)
Expand Down Expand Up @@ -298,6 +299,14 @@ We use the "structural" versus "nominal" terminology as a generalization of the
same terms being used in a
[subtyping context](https://en.wikipedia.org/wiki/Subtyping#Subtyping_schemes).

### Named constraints

Named constraints are "structural" in the sense that they match a type based on
meeting some criteria rather than an explicit statement in the type's
definition. The criteria for a named constraint, however, are less focused on
the type's API and instead might include a set of nominal interfaces that the
type must implement.

## Associated entity

An _associated entity_ is a requirement in an interface that a type's
Expand All @@ -320,8 +329,10 @@ instead of associated entity.
An _impl_ is an implementation of an interface for a specific type. It is the
place where the function bodies are defined, values for associated types, etc.
are given. Impls are needed for [nominal interfaces](#nominal-interfaces);
[structural interfaces](#structural-interfaces) define conformance implicitly
instead of by requiring an impl to be defined.
[structural interfaces](#structural-interfaces) and
[named constraints](#named-constraints) define conformance implicitly instead of
by requiring an impl to be defined. In can still make sense to implement a named
constraint as a way to implement all of the interfaces it requires.

## Compatible types

Expand Down
3 changes: 3 additions & 0 deletions docs/design/lexical_conventions/words.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The following words are interpreted as keywords:
- `break`
- `case`
- `class`
- `constraint`
- `continue`
- `default`
- `else`
Expand All @@ -54,11 +55,13 @@ The following words are interpreted as keywords:
- `impl`
- `import`
- `interface`
- `is`
- `let`
- `library`
- `match`
- `namespace`
- `not`
- `observe`
- `or`
- `override`
- `package`
Expand Down
Loading