by juancampa · 5y ago · 0 comments
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.
I'm not expert at this stuff, but when I was interrogating this before, I found the thesis behind https://ncatlab.org/nlab/show/linear-non-linear+logic as a quite old example (1994, only a few years after the first LL paper from Girard in 1987) of separating the linear and non-linear worlds as two categories with an adjudication between them. This is very compatible with linearity in the types.
That all said, I will admit that the "quantitative type theory" line of research gives the current LinearHaskell a big leg up on integrating with dependent types. See https://ncatlab.org/nlab/show/linear-non-linear+logic for the latest plans on that.
I certainly have no retort. My big complaint about the status quo is we can't even have things like Rust's `Box`, but I have no idea what the operational semantics of `f : pi (b : Box<T>) ...` might be, let alone the static semantics!
Let's say a function returns a linear value. If I then feed this value into a function that does not specify linearity, all linearity is lost and the function can treat it as a regular non-linear type?
If yes that would seem to require extreme discipline by the programmer to remain useful.
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. // This moves the argument
fn func(a: A) -> B { .. }
func(x); // x is not accessible after this line
// This borrows the argument, and therefore won't move it
fn func(a: &A) -> B { .. }
func(&x); // x remains accessible after this lineLinear 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.