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

Generics goals #24

Merged
merged 119 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
ef784a9
First draft of Generics principle doc
josh11b May 21, 2020
b537005
Ran prettier
josh11b May 21, 2020
0b3e05f
Reorganize so proposal is separate from proposed doc.
josh11b May 21, 2020
4c3ffed
Move proposals/ dir to location suggested by #11.
josh11b May 21, 2020
28806ed
Merge pull request #1 from carbon-language/master
josh11b May 21, 2020
a7abd57
Merge branch 'master' into principle-generics
josh11b May 21, 2020
18e73ed
Update header, removing fields we no longer want.
josh11b May 21, 2020
c1dd521
prettier take 2, now using the config file.
josh11b May 22, 2020
150d239
Update to new wrapping of license text header.
josh11b May 27, 2020
54f6626
Add link to latest generics doc.
josh11b May 30, 2020
5ae9e9b
Merge remote-tracking branch 'upstream/master'
josh11b May 31, 2020
9796b47
Merge branch 'master' into principle-generics
josh11b May 31, 2020
d4e1541
Apply suggestions from code review
josh11b May 31, 2020
75ac57e
Update link
josh11b Jun 5, 2020
33dd06c
Add coherence.
josh11b Feb 24, 2021
0bbca0c
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Feb 24, 2021
b3c4adc
Fix presubmits.
josh11b Feb 25, 2021
4eaba40
Incorporate feedback and reformat.
josh11b Mar 8, 2021
45118b2
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Mar 9, 2021
fcbce40
Update to current proposal template.
josh11b Mar 9, 2021
0dd005a
Fix proposals_test.
josh11b Mar 9, 2021
bb7fa5f
Use "parameter" instead "argument" in several places.
josh11b Mar 9, 2021
22094ee
Missed lexer changes in earlier commit.
josh11b Mar 15, 2021
209fca6
Move & expand expressive principle into the goal and new closed overl…
josh11b Mar 15, 2021
c3f4138
Resolve conflicts
josh11b Mar 15, 2021
ace9751
Update docs/project/principles/principle-generics.md
josh11b Mar 17, 2021
be7a752
Update name lookup section.
josh11b Mar 17, 2021
5c0bdc0
Clarify difference between generics and templates. Add TOC.
josh11b Mar 18, 2021
2e8b955
Address some suggestions, reorganize content, expand on interop.
josh11b Mar 18, 2021
36f6382
Add "learn from others".
josh11b Mar 19, 2021
d22e540
Rewrite dispatch control based on feedback that it was unclear.
josh11b Mar 19, 2021
199a014
One more sentence to clarify about overloading.
josh11b Mar 19, 2021
6b78a49
Summarize content from `generics/motivation.md` as "use cases".
josh11b Mar 19, 2021
d20146b
Change from a principle to "generics goals".
josh11b Mar 19, 2021
e223b74
Attempt to fix test failures.
josh11b Mar 19, 2021
ca8071f
Relative path should be more robust.
josh11b Mar 19, 2021
c99c8f7
Stop calling inheritance evil.
josh11b Mar 22, 2021
a182fd9
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Mar 22, 2021
0b7b196
README.md files are required for every docs/ subdirectory.
josh11b Mar 22, 2021
8dcbae7
Redo performance goal. Refine terminology around template typing.
josh11b Mar 23, 2021
26f3cd1
Checkpoint progress.
josh11b Mar 23, 2021
138e242
Use rephrase suggestion.
josh11b Mar 23, 2021
c560eab
Use @zygoloid 's suggested text re: JIT specialization strategy.
josh11b Mar 23, 2021
7b15140
Rewrote text re: type constraints used for templates too.
josh11b Mar 23, 2021
689c693
Identify specific improvements in "better compiler experience" section.
josh11b Mar 23, 2021
d33428b
Expand dependency injection. Add "predictability" section.
josh11b Mar 23, 2021
ff74a55
Move open overloading. Mark nice to have. Name conflicts from evolution.
josh11b Mar 23, 2021
843e68e
More rewriting "interop and evolution".
josh11b Mar 23, 2021
ac4bd13
Include suggestion about constraints on multiple parameters.
josh11b Mar 25, 2021
a723a5e
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Mar 25, 2021
0ec3dad
Merge branch 'principle-generics' of github.com:josh11b/carbon-lang i…
josh11b Mar 25, 2021
647dac1
Apply suggestions.
josh11b Mar 25, 2021
9fe02ac
Accept suggestion
josh11b Mar 25, 2021
0399b33
Incorporate suggestions.
josh11b Mar 25, 2021
6e9f191
Rewrite coherence property in terms of interfaces.
josh11b Mar 28, 2021
3b32fd0
Clarify some template language.
josh11b Mar 29, 2021
f0047f0
Link to Rust's doc on coherence and orphan rules.
josh11b Mar 29, 2021
8eaa09e
Clarify pros/cons of coherence.
josh11b Mar 29, 2021
ddae41d
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Mar 29, 2021
62ac9d2
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Mar 29, 2021
f2d7f70
Remove 'Notably'
josh11b Mar 29, 2021
80b141f
Remove slashes
josh11b Mar 29, 2021
14e6c48
Restructure non-goal section.
josh11b Mar 30, 2021
d1c490e
Remove parens. Move text about templates to background.
josh11b Mar 30, 2021
a57373b
The expression problem is in tension with coherence.
josh11b Apr 1, 2021
c2f8107
Add scoped conformances to coherence section.
josh11b Apr 1, 2021
52f6ec5
Undecidability that is expected and ok vs. proof search at compile time.
josh11b Apr 2, 2021
068d072
Coherence useful for templates.
josh11b Apr 2, 2021
4fe9a7e
More about evolution.
josh11b Apr 2, 2021
194791a
Link to Rust's newtype pattern for controlling interface impl.
josh11b Apr 2, 2021
6c58d72
C++ extension bridge
josh11b Apr 2, 2021
989a438
Add defaults to "learn from others". Link for ADL.
josh11b Apr 3, 2021
3ecfbdf
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Apr 3, 2021
590fe54
Clean up "Upgrade path from C++ abstract interfaces".
josh11b Apr 3, 2021
aca1ebd
Add the tricky `Abs` case as something to solve.
josh11b Apr 3, 2021
4cc5657
Link to example using defaults.
josh11b Apr 5, 2021
a145346
Generics definition section revamp.
josh11b Apr 6, 2021
b8073d5
Clarify functions with any template parameter has the template limita…
josh11b Apr 6, 2021
dc56a7b
Update "Learn from others".
josh11b Apr 6, 2021
51145cb
Reorganize out-of-scope / non-goals.
josh11b Apr 6, 2021
66058ee
Address code review comments.
josh11b Apr 8, 2021
6474c73
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Apr 9, 2021
5be10ec
Commit suggestion
josh11b Apr 9, 2021
26c6f09
Commit suggestion
josh11b Apr 9, 2021
b6a2499
Merge branch 'principle-generics' of github.com:josh11b/carbon-lang i…
josh11b Apr 9, 2021
8ece23f
Update in response to feedback.
josh11b Apr 9, 2021
3e85927
Small update to README.
josh11b Apr 12, 2021
f5b1fa7
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Apr 13, 2021
74fe0c9
Proposals now include a rationale.
josh11b Apr 13, 2021
0d0c3fa
Commit suggestion
josh11b Apr 16, 2021
fad5e7d
Commit suggestion
josh11b Apr 16, 2021
5ea59fd
Commit suggestion
josh11b Apr 16, 2021
1d7e679
Commit suggestion
josh11b Apr 16, 2021
bfed53e
Fix formatting.
josh11b Apr 16, 2021
b10c8b2
Accept suggestion
josh11b Apr 16, 2021
b7acfe2
Commit suggestion
josh11b Apr 16, 2021
b633c5f
Markdown instead of HTML strikethrough.
josh11b Apr 16, 2021
b703086
Commit suggestion
josh11b Apr 18, 2021
559e987
Commit suggestion
josh11b Apr 18, 2021
c6bc500
Apply suggestions from code review
josh11b Apr 18, 2021
51bd5a1
Apply suggestions from code review
josh11b Apr 18, 2021
038afc0
Apply suggestions from code review
josh11b Apr 18, 2021
f12a5c1
Implement suggestion and fix formatting.
josh11b Apr 18, 2021
88bef3e
Text rewrites from open suggestions.
josh11b Apr 20, 2021
731308a
Test relative vs. absolute links.
josh11b Apr 20, 2021
db4f940
Switch to absolute links to avoid "../../".
josh11b Apr 20, 2021
8a147e6
Commit suggestion
josh11b Apr 20, 2021
6c59a7c
Make changes in response to suggestions.
josh11b Apr 20, 2021
a03d7b5
Weaken requirement to support dynamic strategy.
josh11b Apr 20, 2021
0034bea
Apply suggestions from code review
josh11b Apr 21, 2021
e41441e
Move bit about customization points to the extension points section.
josh11b Apr 21, 2021
9b4a064
Rewrite unclear sentence.
josh11b Apr 22, 2021
3d5bce6
Address some feedback.
josh11b Apr 22, 2021
4e0e0aa
Differences between dynamic and static strategy unobservable.
josh11b Apr 22, 2021
c53033a
Address more feedback.
josh11b Apr 22, 2021
38845e2
Add text from blocking issue #477.
josh11b Apr 22, 2021
4b731bb
Apply suggestions from code review
josh11b Apr 23, 2021
5135a99
Formatting.
josh11b Apr 23, 2021
2cc6115
Merge remote-tracking branch 'upstream/trunk' into principle-generics
josh11b Apr 23, 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
154 changes: 154 additions & 0 deletions docs/project/principles/principle-generics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<!--
Part of the Carbon Language, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-->

# Carbon principle: Generics

## Principle

**Goal:** Our goal for generics support in Carbon is to get most of the
josh11b marked this conversation as resolved.
Show resolved Hide resolved
expressive benefits of C++ templates -- an abstraction mechanism that optimizes
for performance,
[the top priority for Carbon](https://docs.google.com/document/d/1MJvVIDXQrhIj6hZ7NwMDbDch9XLO2VaYrGq29E57meU/edit#heading=h.hntrglpoczk)
-- with fewer downsides. In particular:

- Reduce build times, particularly when developing.
- Clearer and earlier error reporting.
- Encapsulation of logic (with a template, the implementation is part of the
interface).
- Enable simple user control of whether to use dynamic or static dispatch.
- Provide better code generation (both code size and speed) in all
cases.
chandlerc marked this conversation as resolved.
Show resolved Hide resolved

**Semantics:** A generic function will take some "generic arguments", which will
josh11b marked this conversation as resolved.
Show resolved Hide resolved
frequently be types, and in some cases will be implicit / inferred from the
chandlerc marked this conversation as resolved.
Show resolved Hide resolved
types of the values of explicit arguments to the function. If a generic argument
is a type, the generic function's signature can specify constraints that the
caller's type must satisfy (this should also be legal for template arguments,
but less needed and less common). For example, a resizable array type (like
C++'s `std::vector`) might have a generic type parameter with the constraint
that the type must be movable. A sort function might apply to any array whose
elements are comparable and movable.

The key difference between templates and generics is that a generic function is
type checked when it is defined, and type checking can't use any information
that is only known when the function is instantiated (such as the exact type).

**Implementation strategy:** There are two strategies for generating code for
generic functions:

- Static specialization strategy: Like template arguments, the values for
generic arguments must be statically known at the callsite, or known to be a
generic argument to the calling function. This can generate separate,
specialized versions of each combination of generic & template arguments, in
order to optimize for those types / values.
- Dynamic strategy: Unlike template arguments, we require that it be possible to
generate a single version of the function that uses runtime dispatch to get
something semantically equivalent to separate instantiation, but likely with
different size, build time, and performance characteristics.
chandlerc marked this conversation as resolved.
Show resolved Hide resolved

The strategy used by the compiler should not be semantically visible to the
user. Users should be able to write a generic version of a function once, and
then be able to control the mix of strategies used to generate the code for that
function. For example, this can be to trade off binary size vs speed (maybe some
specific specializations are needed for performance, but others would just be
code bloat), or support dynamic dispatch when types are not known at compile
time.

**Desiderata:** We want there to be a natural upgrade path from templated code
to generic code, which gives us these additional principles:

- Converting from a template to a generic argument should be safe -- it should
josh11b marked this conversation as resolved.
Show resolved Hide resolved
either fail to compile or work, never silently change semantics.
- We should minimize the effort to convert from template code to generic code.
Ideally it should just require specifying the type constraints, affecting just
the signature of the function, not its body.
- It should be legal to call templated code from generic code when it would have
the same semantics as if called from non-generic code, and an error otherwise.
This is to allow more templated functions to be converted to generics, instead
of requiring them to be converted specifically in bottom-up order.

### Caveats

- Don't need to provide full flexibility of templates from generics.
- We still intend to provide templates for those exceptional cases that don't
fit inside generics.
- If you want [duck](https://en.wikipedia.org/wiki/Duck_typing) /
[structural](https://en.wikipedia.org/wiki/Structural_type_system) typing,
that is available via templates.
- Notably, there is no need to allow a specialization of some generic
interface for some particular type to actually expose a _different_
interface (different set of methods or types within the interface for
example).
- No novel (generic specific) rules for name lookup.
- An example of these would be the name lookup rules inside of Rust's traits.
- Instead, structure generics in a way that reuses existing name lookup
facilities of the language.
josh11b marked this conversation as resolved.
Show resolved Hide resolved
- Features of generics that e.g. allow you to put constraints on the types
passed in are presented as part of generics since that is where they are most
useful, but are still intended to be available for template functions.

## Applications of these principles

We write a type constraint in Carbon by saying we restrict to types that
implement specific _Interfaces_. Interfaces serve several purposes:

- They specify a set of functions (names and signatures) that must be available
for any type being passed to a generic function, and therefore the only
functions that may be called in the body of that function.
- **MAYBE:** They will allow other type constraints such as on size, prefix of
the data layout, specified method implementations, tests that must pass, etc.
- They allow a set of constraints to be given a name so they can be reused.
- **MAYBE:** They implicitly specify the intended semantics and invariants of
and between those functions. Unlike the function signatures, this contract is
between the implementers and the consumers of interfaces and is not enforced
by Carbon itself. For example, a _Draw_ method would mean different things
when it is part of a _GameResult_ interface vs. a _2DImage_ interface, even if
those methods happen to have the same signature.
- **MAYBE:** They allow multiple implementations of an interface for a given
type. For example, a _Song_ might support multiple orderings (by title, by
artist, etc.), which would be represented by having multiple implementations
of a _Comparable_ interface.
- **MAYBE:** They allow you to define a set of constraints by composing multiple
interfaces.
- **MAYBE:** Have mechanisms to support evolution, such as allowing new
additions to an interface to have default implementations or be marked
"upcoming" to allow for a period of transition.
- In order to allow libraries to be composed, there must be some way of saying a
type implements an interface that is in another package that the authors of
the type were unaware of. This is especially important since the library a
chandlerc marked this conversation as resolved.
Show resolved Hide resolved
type is defined in may not be able to see the interface definition without
creating a dependency cycle or layering violation.

What are we NOT doing with generics, particularly things that some other
josh11b marked this conversation as resolved.
Show resolved Hide resolved
language does?

- Cannot add features to generics that inherently require monomorphization (or
creating differently specialized code)
- MAYBE can add such features to a restricted subset, but would create a
significantly restricted subset.
- Cannot defer type checking of generic definitions (an implementation strategy
used by some C++ compilers where the tokens are stashed and replayed on use).
- ...

## Proposals relevant to these principles

From newest to oldest (and most likely to be out of date):

- [Carbon Generics](https://github.com/josh11b/carbon-lang/blob/generics-docs/docs/designs/generics-overview.md)
- [Carbon meeting Nov 27, 2019 on Generics & Interfaces (TODO)](#broken-links-footnote)<!-- T:Carbon meeting Nov 27, 2019 on Generics & Interfaces -->
- [Carbon generic -> template function calls (TODO)](#broken-links-footnote)<!-- T:Carbon generic -> template function calls -->
- [Carbon closed function overloading proposal (TODO)](#broken-links-footnote)<!-- T:Carbon closed function overloading proposal -->
- [Carbon: facet types and interfaces (TODO)](#broken-links-footnote)<!-- T:Carbon: facet types and interfaces --><!-- A:#heading=h.cg5jp928f02n -->
- [Carbon: types as function tables, interfaces as type-types (TODO)](#broken-links-footnote)<!-- T:Carbon: types as function tables, interfaces as type-types -->
- [Carbon pervasive generics (TODO)](#broken-links-footnote)<!-- T:Carbon pervasive generics -->
- [Carbon templates and generics (TODO)](#broken-links-footnote)<!-- T:Carbon templates and generics -->

## Broken links footnote

Some links in this document aren't yet available, and so have been directed here
until we can do the work to make them available.

We thank you for your patience.
45 changes: 45 additions & 0 deletions proposals/p0023.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!--
Part of the Carbon Language, under the Apache License v2.0 with LLVM Exceptions.
See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-->

# Carbon: First version of "Principles: Generics"

- **Authors:** Josh Levenberg, Chandler Carruth
- **[Tracking issue](https://github.com/carbon-language/carbon-lang/issues/23)**
- **Status:** Draft
- **Created:** 2019-05-21

**_PLEASE_ DO NOT SHARE OUTSIDE CARBON FORUMS**

## Problem

We would like a set of documents explaining the principles by which we judge
proposed changes to the language. This is to achieve many goals:

- Make sure we agree on the approach we are taking.
- Clearly communicate expectations to contributors.
- Encourage us to solve problems in a consistent way.
- Provide a yardstick for measuring different proposals.

While there are many such documents we would like, this specific proposal starts
with a single one on how we would like to tackle "generics".

## Background

The question of what a generics feature would look like in Carbon has been an
ongoing discussion. This document was formed to codify some of the different
possible goals that we thought were worth looking at.

## Proposal

See the generics principles document.

## Alternatives considered

### Further polish

This doc is a starting point, and aims to be a good starting point rather than
definitive. I am aiming for velocity over perfection here, so the main
alternative is to resolve more questions before trying to publish this.