7 ms·
Linear types are merged in GHC
- cosmic_quanta 6y agoThe proposal is described here: https://github.com/ghc-proposals/ghc-proposals/pull/111 https://github.com/ghc-proposals/ghc-proposals/pull/111
- greg7mdp 6y agoIncredible work, congrats, the evolution of GHC is quite impressive. A better description of what linear types can be useful for is at https://www.tweag.io/blog/2017-03-13-linear-types/ https://www.tweag.io/blog/2017-03-13-linear-types/.
- whateveracct 6y ago-XLinearTypes down, -XDependentTypes to go :)
- guerrilla 6y agoOnce that happens, the world will change :)
- QuinnWilton 6y agoWadler might argue that linear types being merged changed the world! http://www.cs.ioc.ee/ewscs/2010/mycroft/linear-2up.pdf http://www.cs.ioc.ee/ewscs/2010/mycroft/linear-2up.pdf
- nine_k 6y agoHaskell still has sort of a way to emulate dependent types in many practical cases, without a ton of work or a huge performance hit [1]. Direct support could be nice, though. [1]: http://okmij.org/ftp/Computation/lightweight-static-guarantees.html http://okmij.org/ftp/Computation/lightweight-static-guarante...
- mboes 6y agoTweag is onto that too. :) See https://www.reddit.com/r/haskell/comments/gtnayl/whats_the_status_on_dependent_types_in_ghc_as_of/ https://www.reddit.com/r/haskell/comments/gtnayl/whats_the_s... for a recent update.
- _bxg1 6y ago> the linear arrow says this: a function that has type A⊸BA⊸B is a function that has an AA available to compute with, but it can only use it once. Not twice, not zero times: just once. Superficially this sounds a lot like Rust's FnOnce. Is there a relation there? Edit: Actually it sounds like linear functions are more about using their arguments once, internally, as opposed to themselves being called once. So perhaps not. Still would appreciate any further commentary by someone with a better grasp on things than I have.
- archgoon 6y agoLinear types are stronger than FnOnce, in that a linear type must be used _exactly_ once. Rust doesn't enforce that if you pass in a function of type FnOnce that it is called.
- weavie 6y agoThats interesting. I don't quite understand how that would work. So can you work around that requirement by just going: if false then <use my type> else <do my things> Technically it is used exactly once, but really it will never be used..
- comex 6y agoThat wouldn't work, because the value must be used in all possible control paths. In this case, both the 'then' and 'else' branch would have to use the value.
- gnulinux 6y agoI hope they implement this in Agda as well.
- guerrilla 6y agoActually I wonder if its not already possible to implement it since it has had levels of universes for quite a while. Is that, with dependent types, not powerful enough?
- gnulinux 6y agoI remember asking this question on agda subreddit a while ago (maybe 3 years ago? I don't know) and they told me it's not possible. However, they implemented Cubical type theory and a few other stuff (universe levels? Has it been implemented last couple years?) so maybe it's possible now.m I love doing algebra proofs with agda as a hobby but I'm by no means, even remotely, experienced in everything agda capable of.
- cryptonector 6y agoNote that the focus here is to make it safe to have mutable data around, not to have manual memory management. Next up I hope: - https://www.youtube.com/watch?v=0jI-AlWEwYI https://github.com/lexi-lambda/ghc-proposals/blob/delimited-continuation-primops/proposals/0000-delimited-continuation-primops.md
- whateveracct 6y agoYou can build safe abstractions for manual memory management in userspace with LinearTypes. In fact Haskell already has plenty of manual memory management modules in its stdlib - it's just that they live in IO. You can do the unsafePerformIO internally and expose a pure API made safe by some combination of LinearTypes and the ST trick.
- cryptonector 6y agoTo use linear types for everything will require new libraries, no?
- whateveracct 6y agoDepends in your definition of "everything" If you want to manually manage all memory, then yes you'll have to not use `base` and any pure functional data structure. There's been a lot of "this feature is going to ruin the ecosystem" FUD but it's not much more that that imo
- xvilka 6y agoThere is `linear-base`[1] for this. [1] https://github.com/tweag/linear-base https://github.com/tweag/linear-base
- cryptonector 6y agoI wasn't suggesting this would "ruin the ecosystem". Rather, I was suggesting we might end up with two ecosystems.
- 6y ago
- AnimalMuppet 6y agoThis has been bugging me for a while: Why are type names what they are? I get why sum types and product types are named what they are. But linear types? A value can be used only once? Cool. We call that "linearity"? Um, what? What's linear about that?
- kinghajj 6y agohttps://en.wikipedia.org/wiki/Substructural_type_system#Linear_type_systems https://en.wikipedia.org/wiki/Substructural_type_system#Line...
- empath75 6y agoThat doesn’t really answer his question. https://math.stackexchange.com/questions/2339147/why-is-it-called-linear-logic/2339157 https://math.stackexchange.com/questions/2339147/why-is-it-c... Apparently there is some connection to vector spaces in the structure of linear logic which linear types are based on.
- nitroll 6y agoBecause you can see their usage as a series of "points" linked to exactly one other point, that forms a "line", as opposed to other systems where you could have more of a graph or tree.
- tathougies 6y agoThey're linear because they can only be used once. If you draw out the set of 'references' to the object in a timeline it'll always be a line, whereas with a non-linear object, it could be any DAG.
- neel_k 6y agoVector spaces and linear maps between them form a model of the linearly-typed lambda calculus. That is, each type can be interpreted as a vector space, with each well-typed term representing a linear map between vector spaces. 1. The linear tensor product A ⊗ B gets interpreted as the tensor product of vector spaces. 2. The linear function space A ⊸ B gets interpreted as the vector space of linear functions between A and B. 3. The Cartesian product A × B gets interpreted as the direct product of vector spaces (i.e., the categorical product). 4. The sum type A + B gets interpreted as the direct sum of vector spaces (i.e., the categorical coproduct). 5. The exponential !A gets interpreted by the Fock space construction. This explains why the choice of name is sensible, but I don't actually know if that was the reason why Jean-Yves Girard named it so. If memory serves, he invented linear logic after thinking about Berry's notion of stable functions, but I don't know for sure when the vector space model was invented. (It's not in his 1987 paper, but it can't have appeared very long after that.)
- centimeter 6y agoAwesome! Very impressive work! I can't wait until the kinks are ironed out and we start seeing resource management APIs using this. It surprised me how useful Rust's affine types were for resource management APIs, so I'm sure we'll see some cool applications in Haskell.
- phibz 6y agoWow very cool stuff. I'm wondering if these ideas could be integrated into the rust type system.
- steveklabnik 6y agoIt’s unclear if Rust can ever get linear types: https://gankra.github.io/blah/linear-rust/ https://gankra.github.io/blah/linear-rust/
- saurik 6y agoFWIW, from having built types like this in C++ before, the thought process at the end of how to handle assignment feels better to me to implement where instead of assigning Some(step1()) directly to token, you construct a new value and then "swap" (which is a C++ concept that is supposed to be guaranteed noexcept) the token variable and the value variable; you then would have until the end of the scope to call some function that asserts the value is None. Regardless, I agree with footnote 3: if you want to claim that linearity is somehow a bad default, then you should take a long hard look at move-only... but I like move-only, and I like linearity! ;P FWIW, I don't think this is that hard: require all values to be forgotten, allow types to provide deconstructors called on unwind / scope exit, require that deconstructor to eat the value, and if a type doesn't have a deconstructor then the user has to eat the value (and you get all the cool behavior of not being able to hold it across a panic without having some way to guarantee it will get eaten, such as by holding it in a container that has a deconstructor that will eat it). This would be epic and would be a pretty awesome sell to get me on Rust ASAP despite the devastating decision to not have implicit error propagation ;P. Like, seriously: I have way more concerns about linearity issues in my codebase--ensuring proper deconstruction of non-memory resources in an asynchronous context (which is where this gets hard)--than I do with my memory correctness... I can't begin to explain how much time I have spent over the past two months chasing deadlocks or premature synchronous deconstruction of an object that absolutely must deconstruct synchronously... I have these "deconstructor bombs" (as mentioned in this article as supposedly an ok solution: no, they really really suck...) throughout my codebase, but I have found it infuriating to come up with rigorous local heuristics to ensure that everything always gets closed in the right asynchronous context :( and so I would be willing to jump through almost any hurdle to use a language that could simultaneously promise me tight memory usage (which sadly Haskell isn't great at, due to its garbage collector, but I am going to seriously look at it again I guess?) and linear types (preferably universally, similar to focusing on movable types).
- Ericson2314 6y agoI am pessimistic about this one, but we'll see.
- sfvisser 6y agoIt’s obviously ok to be pessimistic, but at least explain why. Do you think the feature is not going to be very useful in a practical setting? Do you fear people are going to use it all over the place in libraries and make life harder for their users? Is there something wrong with this feature in a theoretically basis. Does it need more thought? Do you think the implementation is broken or not finished? Does it need more work? Something else?
- Ericson2314 6y agoYes to almost all of that. The proposal process has exhausted me, but I suppose I will write it again, since your questions there serve as excellent section headings :). > Do you think the feature is not going to be very useful in a practical setting? Yes. For one example, you cannot write anything like `malloc :: Word -> LinearPtr Void`. With, `malloc :: Word -o Ptr Void`, since Word is not duplicable, `Ptr Void` would be. The "solution" is to CPS everything, which is pointed out in the paper and they say it's OK cause some big honking monad can take care of that, but I don't buy it, and I don't believe in "zomg all the effects" monads either. Haskell is supposed to lead they way with type system features, but I think we are the ignorant followers on this one (At least Go outsourced generics!!). There's so much that can be learned from Rust, and yet I don't think this feature learns those lessons. In particular, anyone that spent any time with Rust would realize that borrowing / borrow checking is super important for making something actually usable. Unfortunately, regions/lifetimes are far less studied theoretically! So the first step would be not making some linear proposal that cherry-picks theory to make for an easier implementation, but go back to the ivory tower and figure out what the hell this borrow checking is and how to understand it better. > Do you fear people are going to use it all over the place in libraries and make life harder for their users? Yes, it is a fact that lot of Haskell users are magpies driven by boredom with the rest of the industry. That's certainly how I was when starting Haskell! I think there is a tendency to be attracted to shiny new features and have fun with them, which is fine itself. But this will lead to a bunch of PRs to all the core libraries, which is understandable too, since no feature works except for at scale. However, if this proves to be a bad feature as I fear, then we will be left with a bunch of newly-complicated core libraries on this debt-end route. And no language ecosystem I know I think has a good story for deprecating and cleaning up failed experiments in a timely matter. I think we are just too small and disorganized a community to risk our ecosystem like this. > Is there something wrong with this feature in a theoretically basis. Does it need more thought? Yes, I think linear types should be a separate kind, like runtime reps. Right now there is just a linear arrow with normal types. If you recall the relationship between --> in the language and |- in the meta language, it feels to me that the meta language has no kept up. I really do believe more in affine types, but think linearity is useful for fixing Rust's Drop to be a dual to Copy and Clone (Types are linear by default, you can right a designated destructor (like Clone) for RAII, or furthermore (in some cases) use a built-in special no-opt destructor (like Copy). https://arxiv.org/abs/1803.02796 https://arxiv.org/abs/1803.02796 is a paper from the OCaml community that I think is on much better footing. > Do you think the implementation is broken or not finished? Does it need more work? No. I haven't looked at the implementation but this doesn't worry me as much. I think the features were chosen to make an implementation without even more effort tractable---I'm sympathetic as it's enough work as it is and I do commend Tweag for marshaling the resources for our commons at this scale even though I do not agree with what is actually being done. Cleaning up GHC is easier than cleaning up the library ecosystem, and in this case a smaller implementation helps.
- rjeli 6y agoI wanted to try this out, but getting nightly GHC was a bit harder than expected. Threw up a repo on how to set up nightly binary on buster and compile with -XLinearTypes: https://github.com/rjeli/hs-linear-example https://github.com/rjeli/hs-linear-example
- pjmlp 6y agoLove it, the productivity of tracing GC with the tools to do more fine grained resource management when needed, congratulations!
- andybak 6y agoGHC?
- asplake 6y agoGlasgow Haskell Compiler https://en.m.wikipedia.org/wiki/Glasgow_Haskell_Compiler https://en.m.wikipedia.org/wiki/Glasgow_Haskell_Compiler
- c3534l 6y agoA Haskell compiler whose implementation of Haskell has essentially become the language itself. They add lots of experimental features for their users to play around with, such as linear types it would seem. Language extensions like this might make it into the official language, but probably not. I'd say don't use it in production, but if you're using Haskell, you're probably an academic, hobbyist, or mad scientist.
- andybak 6y agoTo be honest I was 80% sure it was Haskell but I do hate acronyms being used without sufficient context for the average reader to understand them. The post should have had the word "Haskell" in it. It certainly helps understand what "linear types" probably means.
- Tarean 6y agoAwesome work! I still kind of wish that something RAII-like that is safe in the presence of exceptions and & types had made it in. But considering the immense amounts of work and effort that this has taken adding some more sweeping changes is somewhat of a pipedream. The current version definitely looks practically useful, though kinda awkward given the syntax and polymorphism restrictions.
- krick 6y agoIs there a short and simple explanation, for what "linear types" are?