362 points · by anp · 10y ago · 250 comments
Small vs large standard library:
A small standard library with most functionality in independent, community-maintained packages has given us API friction as types, traits (type classes), etc are hard to coordinate across maintainers and separate package release cycles. We ended up with lots of uncomfortable conversions at API boundaries.
Here's a number of examples of problems we currently have:
- Conversions between our 5(!) string types are very common.
- Standard library I/O modules cannot use new, de-facto standard string types (i.e. `Text` and `ByteString`) defined outside it because of dependency cycle.
- Standard library cannot use containers, other than lists, for the same reason.
- No standard traits for containers, like maps and sets, as those are defined outside the standard library. Result is that code is written against one concrete implementation.
- Newtype wrapping to avoid orphan instances. Having traits defined in packages other than the standard library makes it harder to write non-orphan instances.
- It's too difficult to make larger changes as we cannot atomically update all the packages at once. Thus such changes don't happen.
Empirically, languages that have large standard libraries (e.g. Java, Python, Go) seem to do better than their competitors.
> - Conversions between our 5(!) string types are very common.
> - Standard library I/O modules cannot use new, de-facto standard string types (i.e. `Text` and `ByteString`) defined outside it because of dependency cycle.
We have one string type defined in std, and nobody is defining new ones (modulo special cases for legacy encodings which would not be worth polluting the default string type with).
> - Standard library cannot use containers, other than lists, for the same reason.
> - No standard traits for containers, like maps and sets, as those are defined outside the standard library. Result is that code is written against one concrete implementation.
Hash maps and trees are in the standard library already. Everyone uses them.
> - Newtype wrapping to avoid orphan instances. Having traits defined in packages other than the standard library makes it harder to write non-orphan instances.
This is true, but this hasn't been much of a problem in Rust thus far.
> - It's too difficult to make larger changes as we cannot atomically update all the packages at once. Thus such changes don't happen.
That only matters if you're breaking public APIs, right? That seems orthogonal to the small-versus-large-standard-library debate. Even if you have a large standard library, if you promised it's stable you still can't break APIs.
If you have 100 different libs that are basically "standard" (who doesn't have `mtl` in their applications at this point), now you have to coordinate 100 different library updates roughly at the same time. If you forget even one of them, then you've broken everything.
I think the argument for a large Prelude/standard lib is similar to Google's "single repo" argument: Easy to catch usages and fix them all at once. Plus you're making the language more useful out of the box. People coming from python can understand this feeling of opening a python shell and being productive super quickly form the get-go.
Arguments for small std lib exist, of course. But Giant standard libraries are more useful than not.
EDIT: I think the failure of the Haskell Platform has a lot more to do with how Haskell deals with dependencies, and the difficulties it entails, than with the "batteries included" approach itself.
The higher level the library (e.g. containing content specific to an application domain), the more magical-seeming libraries can be added to the ecosystem. The counter-risk is the standard library growing in undesirable directions that you can never change because you can't remove stuff.
The interstitial glue that lets third party libraries integrate with one another and be usable by your app: that's the single biggest reason for having a bigger standard library than a smaller one. It has very little to do with including the batteries in the box.
If you think it has something to do with including the batteries in the box, you'll be lured into the trap of making it easy to fetch the batteries from across the internet (that's almost the same, right?). The trouble is, the internet has 100 different batteries to choose from, and not only have you offloaded the choice onto the user, but the batteries use mutually incompatible terminals and you have to jerry-rig interfaces between them. Let a thousand flowers bloom, say some people: trouble is, waiting for the biggest flower can take years, and people pick different ones in the early days. A bad choice is better than indecision.
Low effort updates are even less what large standard libraries are about. Large standard libraries are much harder to update, not easier: there's much more surface area, so it's far easier to break an application - and since every application uses the standard library, you could potentially break them all. Easier versioning and updates are a strong argument for extracting out things into third-party libraries.
see: javascript
We have a policy of no breakage for stable libraries post 1.0 (as does Python, and Go, etc.). So no, we can't.
The size of the standard library has nothing to do with it.
The standard library also includes Path/PathBuf and OsStr/OsString. And third-party libraries also use [u8] for bytestrings.
It'd be nice to improve handling for user-supplied text where you can't assume UTF-8. For instance, git2-rs provides the contents of diffs as [u8], because it can't assume the diffed files use UTF-8. That led to this commit today: https://github.com/ogham/rust-ansi-term/pull/19/commits/a0da...
That felt like a lot of boilerplate to abstract between str and [u8]. Is there a better way to solve that problem?
(As much as I'd love to just say "use UTF-8", that would break on many git repositories, including git.git and linux.git.)
Right. And you want people to explicitly convert between them.
Having to convert between string types isn't a problem. String encoding is hard, and you're going to have to pay that cost somehow.
This doesn't have anything to do with large vs. small standard libraries, because all of these string types are defined in libstd.
What Rust has right now is pretty amazing. What needs to happen is a way for devs to easily break the dependency cycle and include multiple versions of the same crate. Something that has plagued Haskell. I dunno what the answer is, trait only crates, struct only crates?
If people want to 'curate' (shop) a set of packages, they can make a meta package that exports its deps.
There is literally no reason to ship libs with the compiler aside from the basic verbs and nouns.
With verioned and properly name-spaced imports, one could use different curated libs.
It's also one of the features I sorely miss when using Rust. Luckily, Rust's stdlib is starting to tend towards being more practical with recent additions like system time.
There's also `inlinable_string`, `string_cache`, `tendril`, `intern` if you need inlining for performance.
The bigger problem is with other things like 2D/3D points which can be (f32, f32), [f32; 2] or a custom struct.
All things you can delegate to third parties, but not without lots of cross-talk and confusion until things settle down to winners and losers, which may be a long time in the future. Indecision can be costly.
Consider standard library profiles, with progressively higher levels of abstraction supported. It's the right decision for creating a good ecosystem. C and C++ took decades to build consensus on the more complicated libraries, and C++ eventually grew a pseudo-standard library in the form of boost to centralize efforts, simply because it is more efficient that way.
> Empirically, languages that have large standard
> libraries (e.g. Java, Python, Go) seem to do better than
> their competitors.
You seem to be overlooking the ultimate counterexample: C. :PTo me, C did have a standard library: Unix. It's a runtime system too! Due to the nature of the original C bootstrapping process it just happens to be possible to remove this standard library, and Windows was evidence of this.
There is another interesting potential counter example: Lua. It's minimalistic standard library is part of what makes it so attractive for embedding, eg. in game engines. However, Lua's embedding API is so good, you could almost say that it comes with a large standard library too: Your existing C code!
I guess my larger point is that languages rarely are able to stand completely on their own. They need some sort of valuable body of code to justify people to choose the language and libraries together. It might have been the case 40 years ago that you'd reasonably choose to build something "from scratch", but today, if you start on an island, you need to build a bridge, lest you remain on an island forever. Better to start on the mainland.
It's one thing to build a layered system with a small core. It's another thing to completely ignore the fact that the libraries and community _are_ the language, in the only ways that actually matter.
Fully agree. We just ended up with ANSI C + POSIX, because the standard bodies refused to put everything into the same bag.
In the early days, most C compilers were anyway shipping partial UNIX APIs on top of their K&R and ANSI implementations.
Things have been getting better lately because of Luarocks but its still an uphill battle.
> It's another thing to completely ignore the fact that
> the libraries and community _are_ the language, in the
> only ways that actually matter.
I'm unclear, what is this aimed at? Who's ignoring anything?Fitness for purpose is relative to the other options.
C would not have gotten where it is today if it wasn't for the rise of UNIX in the industry, fueled up by free UNIX clones.
I think one reason (of many) that C++ has replaced C almost completely for new development is the STL. Of course, the STL fundamentally depends on the language feature of templates, which you can only approximate in C, but considering that Java and Objective-C, among other languages, lasted pretty long with no generics and only non-type-safe containers, I think C could have benefitted greatly from basic things like resizable arrays, hash tables, trees, better strings, etc. in the standard library. Now it is probably too late for it to matter (which most people consider a good thing).
It has slightly awkward but very simple API, and it's very fast.
For example check Turbo Pascal libraries, including Turbo Vision, already on MS-DOS.
C took off thanks to UNIX's adoption, like JavaScript on browsers nowadays, it became the language to use for anyone working on the enterprise on those new shiny UNIX boxes.
In Europe it was just another systems language to choose from, back when CP/M and other 8 / 16 bit systems were common.
This is precisely why shipping things in the standard library is a bad idea. It ends up full of cruft that no-one uses because there are better alternatives.
Why? What does that gain?
The standard platform provides Data.Map for maps, Data.Vector for arrays, and Data.Sequence for fast-edit sequences.
It's not even clear what a "built-in" array or map in Haskell would even look like, or what semantics it should have. Especially in a pure functional language, you need to be clearer about what your intentions are. A regular mutable packed array won't work most of the time.
> This is exactly the main problem with Haskell.
> A stunning language with a lousy standard library.
I dream of the day where we can say that the main problem of Haskell is which libraries are included in the standard library. To me, we would already have reached programming nirvana at that point.Thanks to GHC's extensions, Haskell has become a ridiculously powerful language in exactly the same way C++ has: by sacrificing elegance. The principled approach would've been to admit that, while type classes are good for a few use cases, (say, overloading numeric literals, string literals and sequences), they have unacceptable limitations as a large-scale program structuring construct. And instead use an ML-style module system for that purpose. But it's already too late to do that.
> The principled approach
You're dead wrong. The principled approach here is dependent types and full-featured type-level functions. Fundeps are a hack that let you implement a small subset of such functions (while type families gets us a bit closer to the ideal).
> they have unacceptable limitations as a large-scale program structuring construct.
Such as?
> And instead use an ML-style module system for that purpose.
How about we just use C macros for parametricity?
ML-style modules have their uses, but they aren't nearly as elegant as a clean type-level solution.
It's not about how much you can do (otherwise, just use a dynamic language, you can do everything, even shoot yourself in the foot!), it's about whether the result makes sense, and how much effort it takes to make sense of it.
> You're dead wrong. The principled approach here is dependent types and full-featured type-level functions. Fundeps are a hack that let you implement a small subset of such functions (while type families gets us a bit closer to the ideal).
You wanna play the dependent type theory card? Type families as provided in Haskell are incompatible with univalence.
type instance Foo Bool = Int
type instance Foo YesNo = String
Please kindly provide the isomorphism between `Int` and `String`.Case analysis only makes sense when performed on the cases of an inductive type, which the kind of all types is not.
> Such as?
The insistence on globally unique instances?
> How about we just use C macros for parametricity?
What does this even mean?
> ML-style modules have their uses, but they aren't nearly as elegant as a clean type-level solution.
See here for how modular type classes, as proposed for ML, would actually prevent the issues caused by Haskell-style type classes: http://blog.ezyang.com/2014/09/open-type-families-are-not-mo...
(0) It means that libraries in general won't play nicely with each other, unless they're explicitly designed to do so.
(1) It can't be fixed without throwing away code.
How about: "std is where code goes when it's done".
As is, really done. The API's won't need changing.
There are a number of counterpoints in Python, in fact, which epitomizes the "standard library is where code goes to die" thing. Adding modules to the standard library in Python is, more often than not, overall a bad thing for the module. Python has not historically been awesome with standard library quality, either; see Java-style logging and unittest (I mean naming, not "Java idiomatic," which I think is fine for both).
This comes down to release cycles for the language, mostly. So I think API stability is a bit of a red herring when discussing Python, at least.
I tend to appreciate languages where I can remove the entire standard library and "start over," like C. (Yes, you can.) This can be good for a number of things: porting, embedding, frameworks, and so on.
[0]: http://artem.krylysov.com/blog/2015/09/29/benchmark-python-j...
[1]: https://github.com/search?q=simplejson+ImportError&type=Code...
I haven't seen anybody say otherwise.
New standard library APIs are stabilized at a steady clip with every new release: https://github.com/rust-lang/rust/blob/master/RELEASES.md
Standard doesn't mean completely done. Standard should be able to accomodate things like HTTP2, as Go has done, whether that means expanding the API or whatever.
It does all the wrong things; singletons, no testability, cgo for implementations, side effects and you have to use every database differently based on their individual semantics.
Virtually everyone I've ever spoken to either uses a high level wrapper around the sql library or a no-sql solution.
That's the definition of 'stdlib is where packages go to die'.
It's not that the API is unusable, it's just basically not used by the community because there are other better things out there...but you're stuck with it forever, because it's there and some people do use it, and changing or removing it would be a breaking change.
Anyhow, we're just speculating. Does anyone actually collect metrics about the usage of different parts of the stdlib for any language?
Without hard data to back it up, you couldn't really make a strong argument either way.
Rust does this well with iterators, for example.
All five of those string types do different things. This isn't a problem; we just have increased expressivity. We couldn't fix this by having a more coordinated standard library. 5 is also a very manageable number IMO.
>It's too difficult to make larger changes as we cannot atomically update all the packages at once.
That's what Stack is for, no?
For a language to remain relevant in the long term, a system must be capable of evolving. Going out there and blessing potentially conceptually incorrect packages such as Mio is therefore not a good idea. The notion of "platforms" best resides in userland, where collections can compete and evolve.
By keeping platforms / package collections in userland we can get organic collections of stuff that make sense to bundle together. Imagine a "server platform", an "event loop platform", a "calculus platform", a "programming language platform". It would be detrimental to creativity to have all these collections live in the shadow of the "rust committee blessed platform".
But so yeah, I'm not opposed to the idea of platforms - I just don't think blessing userland stuff from the top down is the right play in the long term.
Tl;Dr: package collections sound like a cool idea, but if you care about the long term, imposing them from the top down is bad
It sounds like the proposal in the OP avoids this problem by having versioned platforms that are independent of the Rust version. So if something turns out to be a bad idea, it can be stripped out of later platform versions and replaced with something better without disrupting users of the older platforms.
I don't see why that couldn't be worked into this idea.
Completely agree. Don't underestimate the amount of effort it costs them though.
That's exactly what we want to do. We don't have the domain expertise in many of these areas, but we want to enable those ecosystems to help develop their own platforms. It's one of our goals to make sure any infrastructure we develop for the "Rust Platform" is usable throughout our community.
Write standalone tools, but don't create a "platform". Don't make the use of the language dependent on your tools.
C++ does not have a "platform". Nor does it need one.
Helpfully, many of these platforms reinvent basic things like strings [1] and reference counted smart pointers [2] in incompatible ways.
Wouldn't it be better if there were just one platform?
[0] https://en.wikipedia.org/wiki/C%2B%2B11#C.2B.2B_standard_lib...
I work on multiple medium-sized projects that disagree. If you're not writing GUI code, it's quite possible to write 99% platform agnostic code without the help of a 3rd standard library supplement, especially with C++11.
I think the ability to opt out of the rust-platform metapackage is a great feature.
Then, of course, the other installers will gradually break and be abandoned. The effect is that users must run the "Rust Platform", unless they have the resources to build their own distro.
Is there a monetization scheme behind this? Does someone aspire to be the Canonical of the Rust ecosystem?
> Is there a monetization scheme behind this? Does someone aspire to be the Canonical of the Rust ecosystem?
This seems like a complete non-sequitur. I have no idea what you're trying to suggest here.
Of course not. Spreading conspiracy theories isn't appreciated.
What other installers are you referring to, exactly? The old rustup.sh which couldn't support multiple toolchains installed alongside each other? multirust which didn't work on Windows? rustup is a massive improvement over both, IMO.
In a high-level language like Python there's always a right solution. It's just rarely well-tailored to the problem.
I'm not at all surprised C++ is still muddling around in the middle somewhere.
Sure it does, it is called C++ Standard Library and ISO C++ catching up to what other languages already offer.
My suggestion, look at how Stack (the tool) and Stackage (the platform) work:
https://docs.haskellstack.org/en/stable/README/
Haskell Platform is a perfectly adequate example to take high-level inspiration for the core of this idea: use Cargo (which is like Stack/Stackage for rust) to help bootstrap Rust libraries when using rustup to install and upgrade (mostly equivalent to `stack setup`).
In Cargo jargon, a solutuon would be for metapackages could double as sources: `foo = { metapackage = bar }` to use foo from bar.
[dependencies]
hyper = { metapackage = { rust-platform = "2.7" } }
I like the general theme of having the platform be just a set of known-compatible versions, but on the other hand this feels like it loses out on many of the ease-of-use advantages of just specifying a platform version and knowing that you have all the crates inside of it.I don't think we need to pick one way exclusivity. My specific use-case is making PRs for packages to work in kernelspace / in unikernels. The library night initially be packaged the easy way but then I'd use this. I don't want the PR recipient to also worry they might get out of sync with the platform as a side effect.
But we can do even better. In practice, while code will continue working
with an old metapackage version, people are going to want to upgrade.
We can smooth that process by allowing metapackage dependencies to be
overridden if they appear explicitly in the Cargo.toml file.The problem with Haskell Platform came down to a set of intersecting issues:
- A culture of setting very strict & narrow version bounds. (Based on known-good rather than based on avoiding known-broken)
- Tools (Cabal) that enforce dependency version bounds. If there's mutual/transitive incompatibiltiy in version bounds - the build fails, period. You had to figure out the problem and fix it yourself if there was a truly irreconcilable issue.
- Recommended installer on the website (Platform) was unnecessarily installing packages into the global package database, making you "stuck" with those versions for _all_ packages you attempted to build. Cabal was restricted to finding builds that abide by dependency versions narrowed to the ones provided by the global package database.
These problems led to beginners being confused by seemingly spurious build failures because Platform would fall out of date with the rest of the ecosystem. Cabal would be unable to find sets of compatible dependencies and say it couldn't build the package.
What non-beginners were doing to avoid these problems was:
1. Install the bare compiler, no Platform
2. Use package database sandboxes for each project
All of these (UX and technical) problems were solved without compromising dependency conflict enforcement via Stackage and Stack.
Speaking hypothetically, if Cargo behaves like Maven or Ivy and pulls in two dependencies who want conflicting versions (1.1 and 1.2, say) of a particular library and just picks a winner, then you'll never see something like this.
My email is what finally moved the committee and GHC devs on including Stack with the Platform and on some other decisions concerning the website.
Like I said, not all the downsides may be applicable to how Cargo works or what aturon has in mind, but please don't cite it as an exemplar of anything.
OTOH, compared to e.g. C++, where even strings aren't standard across multiple real large code bases (e.g. Gecko and Qt) having standard types especially for foundational stuff can be very useful if the foundation is right or right enough. Java having a single (though not optimal) foundational notion for Unicode (UTF-16 even though UTF-8 would be optimal) and a common efficient and spec-wise correct (though not design-wise optimal) XML API (SAX) was a key reason to develop Validator.nu in Java as opposed to e.g. Python (whose notion of Unicode varied between UTF-16 and UTF-32 based on how the interpreter was compiled!).
Still, at this early stage, I'd point to crates.io in general as the "batteries" instead of creating a layer of official approval on top. But I'm biased, because lately, I've been writing a second-to-market crate for a topic for which a crate already exists.
As for dropping extern crate, it either marries rust to cargo, or it makes imports implicit. Neither is desirable.
Basically: people knowing will still have freedom, people not knowing will have an easier life.
And in the end, in 2016, you generally don't care if you download a few extra 100Mo. If you do, you will just find spend the time to find to have a smaller setup.
I suggest a trip to anywhere outside the western world.
- choosing a bare bone small manual install; - making the download in advance in a place where you can or during the night;
It's not like you have less option. You just have a clear default choice.
P.S. Thanks for all your work Steve, it's really appreciated.
Personally, I think that letting users (especially beginners) opt in to a larger set of starting libraries could be very beneficial for adoption.
People then gain experience with it and sometimes subtle issues emerge. Those issues can be fixed, either in Boost itself or when libraries move from Boost to stdlib (and sometimes the issues inform language features).
While C++'s standard library is not "batteries included" like Python's, it's been very gratifying to see it expand slowly and surely over the last few years.
One of the main constraints that would guide selection of crates for this platform metapackage is that they must have compatible dependencies. So package A depending on Bv1 and package C depending on Bv2 wouldn't work because they Bv1 and Bv2 would both have to be included, leading to a conflict.
But this information is (in theory) encoded in semantic versioning. Assuming proper semantic versions for crates, a target set of crates to be included could be specified, and then automatically the various sets of crate versions that do not have conflicting dependencies could be calculated.
These compatible crate/version sets could be automatically generated and published as metacrates.
Consider the following crate/version dependencies:
Av1 -> Bv1
Cv1 -> Bv1
Av2 -> Bv2
Cv2 -> Bv1
Av3 -> Bv2
Cv3 -> Bv2
compatible_sets({'A', 'C'}) returns {{'Av1','Cv1'},{'Av1','Cv2'},{'Av2','Cv3'},{'Av3','Cv3'}}By imposing an ordering on these compatible sets they could be automatically identified. compatible_A_C_0 is {'Av1','Cv1'}, compatible_A_C_1 is {'Av1','Cv2'}, and so on.
Obviously the semver could be wrong and unexpected incompatibilities could crop up. But couldn't these just be autogenerated and then voted on? Then the top best compatible sets will filter to the top and, de facto, the Rust Platform has been autogenerated?
> So package A depending on Bv1 and package C depending on Bv2 wouldn't
> work because they Bv1 and Bv2 would both have to be included,
> leading to a conflict.
As mentioned in this thread, this already works just fine. Rust can handle both versions.Furthermore, it's more than just a constraint problem; there's also integration issues, testing the whole thing together, etc.
Are metapackages going to be available for others to utilize? If so, how will conflicts be resolved if packages require two different versions of the same package?