301 points · by cosmic_quanta · 5y ago · 130 comments
This Haskell extension uses linear types for resource safety and scoped effects [4]. For example, linear types can prevent use-after-free errors. Also, linear-typed arrays can perform in-place updates if there are no external pointers to the data.
Similar to linear types is the affine type, where a value is used at-most once. Rust's borrow checker is based on this notion, so that values are "moved" rather than "copied".
[1] https://en.wikipedia.org/wiki/Uniqueness_type
[2] https://en.wikipedia.org/wiki/Substructural_type_system
But like a lot of Haskell much of this still goes over my head.
I'm so conflicted and my intuition is to leave this to the CS university types and focus on IRL productivity in my own field. But I want to invest more time into FP in the future.
I'm also technically a "frontend developer" professionally :p
It's not that hard, but it's easier to get one concept at a time. I'm less familiar with Haskell, but languages like Scheme/OCaml are routinely used to teach programming to beginners.
Also there's no need to see a dichotomy between "IRL productivity" and "CS/FP". Even if you don't use Haskell directly, ideas from FP make their way to mainstream languages, either as constructs, or libraries, or simply programming style.
Overall, the more programming paradigms you know, the easier it gets to learn new languages.
Haskell has a tendency to make every problem seem exactly the same :)
Normally types are used to indicate the set of values a variable is allowed to contain or not. And that info can be used by the compiler to detect mismatch in the code, like if a value is being assigned to a variable that doesn't match its type.
But like I said, this is all just metadata about variables and values. So what else could you track and enforce? Well you could also track that some values are never assigned to more than one variable. That would be Uniqueness types. A value of such type is supposed to always have a unique variable pointing to it. So you can't have two variables pointing to it, if you did, the compiler would throw an error.
Linear types are similar, with some small differences in the edge case. Specifically, you can cast a value that is non linear to be linear. In effect it means a value that is made linear can not have more references to it after it has been made linear, but it could have many references pointing to it from before it was made linear. Where as a value of a unique type would not allow that.
To recap, Uniqueness guarantees that a value has no other references to it, while linearity guarantees that no more references can be made to a value.
Finally you have Affine types, which are also just a small difference to the above two. One detail I ommited is Linear and Unique types will also fail if the value is never assigned to any variable. They basically enforce that a value having a linear or unique type must be used at least once. An Affine type is like a linear type that doesn't have to be used at all, thus they say it needs to be used at most once.
I'll add one more, Ownership types, like in Rust. Those are like a mix of unique and affine. In that a value with an ownership type can only be refered to by its current owner, but doesn't have to be. It's different to unique types in that the owner is free to refer to it many times, but no other owners can refer to it at the same time.
So you see, all of this is just lingo to say, could we come up with some constraints around how many references to the same values we're allowed to have. Then create a type for them so we can tell the compiler to enforce those constraints on values of that type.
Now it gets complicated to actually find a way to implement a compiler to track these types and enforce their constraints. That's where the research and challenge is. And it's also tricky to figure out how the programming language can use these and how to make them pleasant to use and not feel like annoying constraint that prevent you from doing things easily.
I appreciate you writing this out for me. I understand the 3 types you described quite clearly.
The compiler stuff I fortunately won't have to care as much about (unless I do this fulltime or go even harder core into types) but I appreciate your approach to it.
I believe learning Rust has helped me understand Haskell and the utility of even basic things like Maybe/Option types. Compared to like Go or whatever I want it plus pattern matching in every language I use now (pattern matching is another thing I gleaned from Erlang/Elixir as extremely useful).
Anyway, I'll find time at some point down the road to understand this better.
Holy crap! That's amazing and I hadn't thought of that!
The ghc-bignum change is kind of boring, but it does help a lot with the long-standing problematic dependency on gmp.
Not to mention the stage being set for QuickLook impredicativity. That's something that I've been wishing we've had for a long time now.
Haskell remains at the bleeding edge of general-purpose programming - more than ever with each release.
Years ago when I was really trying to get into Haskell, I wanted its binaries to be as portable as those from Go. I was so sad to see that this _one_ library made that impossible.
https://gitlab.haskell.org/ghc/ghc/-/issues/17957 for example. You would think the Haskell compiler in Haskell would be a shining example of a pure functional programming, but you would be wrong!
(GHC most predates more modern ideas about how programs should leverage pure functional languages. I also speculate early on there was an "innovation budget" idea, conscious or otherwise, that if the language was novel, and the compilation analyses (e.g. strictness analysis) were novel, that was enough, and the domain-agnostic architecture of the compiler should be proven and bog-standard.)
The issue is that if you write a Haskell program, and you want to deploy it as a single executable, then GMP is included (statically linked), so the LGPL applies to the executable.
The two obvious ways to meet the LGPL license restrictions are:
* license your code in a way that is LGPL compatible (e.g. open source your code using GPL)
* you could provide the app in a relinkable form (ie a big .o file) as per section 6.a
Essentially there is no tidy way to distribute a closed source Haskell executable without removing the GMP library, which was the sole remaining LGPL dependency. It was not trivial to remove the dependency because of the library’s quality (amongst other reasons).
(Edited for clarity.)
This is worth pointing out one more time. As far as I'm concerned it's the most expressive language I've ever used, and is what I pick if I want to write correct software and am OK spending some time yak shaving (not that you have to with Haskell, I just know that I will).
Safe mutation because when I have the only reference to a thing, the rest of the system shouldn't care whether I change it in place or make a new one.
Better resource management because you can be sure opened files are closed exactly once and that nothing tries to use a file after close, even where a bracket pattern doesn't fit (or fits much more loosely).
In both cases, you're not doing anything you couldn't do before, but it lets you do it safely. People speak of performance because (especially in Haskell) mutating without being confident nothing else can see is sufficiently full of footguns that the community happily (... and rightly, IMO) pays small performance penalties for security against them. This lets us remove those performance penalties in more cases.
There's some room for the compiler to notice that something is only referenced in one place and safely convert some things to mutation; again this doesn't strictly need linear types, but linear types may let us do that reasoning locally instead of it requiring whole program analysis.
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
You pass a function to withFile, and the file handle is valid for the scope of the function. However, this means you can’t do the following: open file A
do stuff
open file B
do stuff
close file A
do stuff
close file B
Because the usage of files "A" and "B" don’t nest, you can’t achieve it with withFile. But you can get the same level of safety using linear types.Type states allow the state of the socket (unbound, bound, listening, closed) to be expressed as a part of the type (a type annotation, if you will) so the compiler can verify that you're not reusing a closed socket, or try to send data over an unbound socket.
Linear types then, allow to express in code how a type (annotation) changes over time (e.g., "bind() takes an unbound socket and returns it an a bound state") so the compiler can statically check that it's in a valid state for every operation.
Finally, it allows you to express more invariants about your data through the type system, hopefully reducing bug counts. As an example, soneone could "implement" a sorting function as `sort xs = []`. This has the correct type signature, but is not a correct implementation of sorting. With linear types you could express that the output has to access the values of the input. (Of course what we'd actually want is to express other invariants for sorting as well, but full dependent types are still some ways off)
There’s also quite a design space still to be explored for these ideas. Even / especially for languages like Haskell.
Now one can have mutation by simply returning the mutated value which internally can be implemented as mutation since the type system guarantees that the original value never be used after.
Conceptually, it's purely functional programming where a function consumes a value and returns a new one, under the hood it's understood to be mutation which is possible due to these constraints.
You can avoid a lot of copying and memory management if you know the value will be used a single time, and then generate another value, that will be used a single time again. It has a very large potential impact, mostly on IO.
There's many other such modal type systems/logics used for other special purposes like temporal logic.
I wonder how this compares to Rust's borrowing approach which can be used for the same goal. I don't know enough about Haskell to understand Linear types. Perhaps someone more knowledgeable can comment.
So for most common use-cases, you will be able to use Rust and Haskell for similar guarantees. But I’m also eager to see what unique use-cases will be enabled by each of them.
Not tying the linearity to the type seems quite a bit more confusing and error prone to me.
If a type can be used in different ways, I'd rather have two different types.
The rust way is probably more practically useful from a programmer's perspective, but it isn't as easily understood to be sound from a type theory perspective.
func :: A -> B
declares a function func that takes one argument of type A and returns a value of type B.I'm guessing a bit, I feel this goes over my head, but here's my layman's interpretation:
In Rust, in order to declare a linear type, you must do so when initializing the variable:
let x = vec![1, 2, 3];
Now x is declared as move-only, so that the vector cannot be duplicated by assigning it to a second variable; after assignment, accessing x will result in a compiler error.In Haskell, you declare a function
func :: A %1 -> B
to indicate that after this function call, accessing A should result in a compiler error.Linear types have a simpler mathematical underpinning and are much easier to prove things about. However they are notoriously difficult to use for actual programming, especially in larger projects.
Rust's borrow checker is approximately affine types plus regions. Its proof theory gets very complicated if you spell out all the details. However mere mortals can actually write large, complex, and correct programs with it, as has now been empirically demonstrated many times.
https://tontine.com is an award winning Fintech making the global pension system functional and is looking to add more senior Haskellers as well as some junior Haskellers ahead of our upcoming launch in 27 countries.
Please get in touch if you are interested to use your Haskell skills to make a positive change in the world.
Its hard to find real world use examples and learn about linear types when I search online. If anyone has links, that would help.
- A safe streaming library where effects are guaranteed to only be performed once: https://www.tweag.io/blog/2018-06-21-linear-streams/
- Efficient zero-copy network communication: https://www.tweag.io/blog/2017-08-24-linear-types-packed-dat...
- Safe manual memory management and prompt resource release: https://www.tweag.io/blog/2020-02-06-safe-inline-java/
- Explicit API design to guide developers on what function to call next when implementing a protocol like TCP: https://www.tweag.io/blog/2017-08-03-linear-typestates/
- Add correctness proofs to already existing algorithm implementations like merge sort: https://www.tweag.io/blog/2018-03-08-linear-sort/
But we already had ST for that, so the real advantage of LTs over ST is you can type-safely "freeze" and "thaw" your mutable structure without copying.
The mutable array example in the Linear Haskell paper is a simple comparison to what you're asking for.
This included a pretty rigorous community review. The extension did get some pushback & serious debate, and I honestly think the entire process was handled wonderfully. It speaks well of GHC's community culture (speaking as a sideline observer.)
Things are already quite 'read-only' in Haskell ;)
If you want to touch a list twice, don't mark it as linear.
That said, reading a list length (in O(n) time) and then performing an operation on it is usually an anti-pattern. Kind of like scanning a file to count how many lines it has, and then doing something to each line. So linearity could help you ensure that you don't make this mistake.
Of course it's unnatural for Haskell lists, but replace "list" with "Array" (or even just the height of a tree, which is merely O(log n)) and I feel that this is a very common and acceptable use.
> If you want to touch a list twice, don't mark it as linear.
But why are we counting how many times we "touch" the list? Wouldn't it be nice to have some set of operations which get data out of the list without "touching" it?
Anyway, the other commenter points out that you can return the array as you compute its length, and yes, that is a common trick with linear types (Idris' experimental linear arrays do the same). But isn't it better to express in the type system that you will use something "nondestructively"? As an added bonus, it'd probably be more ergonomic than constantly reassigning the list.
Well, right, exactly. Why did you ask for that? Probably there's a reason.
If I understand correctly, you are really asking why this needs to apply to all operations, and I think laziness is probably the answer. If I've asked for a linear list because I want to somewhere change it inline, and I ask for the length and "then" do something that changes that length, the value the length function returns would seem to depend on when (at run time) the change is forced vs the question (and they could possibly be overlapping, if you have sent the length to another thread!). That's not what we want.
Requiring you to pass back a "new" list forces the ordering - either my modification is of the list before I took the length, or the list after. In terms of sequencing, that's exactly what we want.
I do agree that it is a bit disappointing that the types don't tell us that length returns list unchanged.
That is how regular functions & data work now. It's completely ergonomic, no linearity required.
Do you know if the par operator can be expressed with this vector spaces over finite fields setup?