News for hackers. Stuff that matters.
Re:GHC 9.0, supporting linear types by juancampa on Wednesday December 30, 2020 @01:45PM (#25584616)
> Safe zero-copy data (de)serialization, a notoriously difficult endeavour that is in fact so error prone without linear types that most production systems today typically avoid it.

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.

Re:GHC 9.0, supporting linear types by sa1 on Wednesday December 30, 2020 @01:59PM (#25584754)
Rust has affine types (values can be used upto 1 time), which are similar to linear types(values can be used exactly once). Affine types are slightly more general, but offer less guarantees about your program. To add back some guarantees, Rust also has lifetime analysis.

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.

Re:GHC 9.0, supporting linear types by platz on Wednesday December 30, 2020 @02:19PM (#25584971)
GHC models linearity differently. Rust puts linearity on the types, GHC puts linearity on the arrows. https://i.imgur.com/s0Mxhcr_d.webp?maxwidth=640&shape=thumb&...
Re:GHC 9.0, supporting linear types by the_duke on Wednesday December 30, 2020 @02:24PM (#25585014)
Do you know the reasoning why?

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.

Re:GHC 9.0, supporting linear types by platz on Wednesday December 30, 2020 @02:27PM (#25585042)
The core logic theory behind linearity on the arrows is provably sound and more well understood theoretically, and it fits better with Haskell's existing semantics.

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.

Re:GHC 9.0, supporting linear types by Ericson2314 on Thursday December 31, 2020 @12:02AM (#25589760)
This has been expressed, but I do not think it is true. Yes, the original linear logic doesn't have anything like a "linear kind", but that's mainly because it's a logic not a type theory. The classic A → B = !A ⊸ B could be interpreted as something like A → B = GC A ⊸ B, i.e. making the ! an in-language type constructor not some sort erased modality.

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!

Re:GHC 9.0, supporting linear types by the_duke on Wednesday December 30, 2020 @03:30PM (#25585707)
So to be clear:

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.

Re:GHC 9.0, supporting linear types by mauricioc on Wednesday December 30, 2020 @03:49PM (#25585931)
There is no such thing as a "linear value" per se, since linearity is on the arrow. But speaking of linear values makes sense when there's an ambient monad, and all of the restrictions you expect are enforced; see Section 2.7 (and the last paragraph of Section 6.1) of https://arxiv.org/pdf/1710.09756.pdf for details.
Re:GHC 9.0, supporting linear types by nimih on Wednesday December 30, 2020 @07:10PM (#25587951)
The general idea is that the discipline is automatically enforced via the type system, e.g. you have a library which provides a type FragileAndDangerousResource, but the only way to interact with such a thing is via (linear) functions along the lines of

    withFADR :: (FragileAndDangerousResource #-> a) -> a
    
    addToFADR :: Int -> FragileAndDangerousResource #-> FragileAndDangerousResource
In other words, linear types are purportedly useful precisely because they make it reasonable to ask for that extreme discipline on the programmer's part, because the compiler is right there to tell them when they screw it up--ideally with a helpful error message.
Re:GHC 9.0, supporting linear types by javbit on Thursday December 31, 2020 @12:51AM (#25589988)
Would

  withFADR' :: (FragileAndDangerousResource #-> IO ()) -> IO ()
be a better type? My reasoning is if I use the linear identity function

  lid :: forall a. a #-> a
in the following

  withFADR lid
I should be able to “break out of” the managed context [1] by the types. Using

  withFADR'' :: (FragileAndDangerousResource #-> ()) -> ()
is obviously not right since nothing could be done with the `FragileAndDangerousResource`, so I think side effects are necessary.

---

[1]: I'm assuming that's what you're going for with `withFADR`.

Re:GHC 9.0, supporting linear types by tome on Thursday December 31, 2020 @06:30AM (#25591725)
Good observation. I believe you would use

  withFADR :: (FragileAndDangerousResource #-> Unrestricted a) -> Unrestricted a
One can't tag "linear" values in the type system but one can tag non-linear "unrestricted" values. See "borrow" in Section 7.2, page 5:26 of https://arxiv.org/pdf/1710.09756.pdf
Re:GHC 9.0, supporting linear types by nimih on Thursday December 31, 2020 @01:42AM (#25590194)
Yeah, that seems right. I'd also believe you if you told me that the second arrow also needs to be linear, since I can't really give a good reason why it should be one way or the other off the top of my head.

I admittedly don't really have a great intuition for how to work with this machinery, since I haven't had a chance/excuse to actually play around with it, to be honest; I'm just going off of a SPJ talk on this stuff I watched many months ago, which I mostly understood at the time, and the paper associated with the GHC implementation, which I understood substantially less of.

Re:GHC 9.0, supporting linear types by whateveracct on Wednesday December 30, 2020 @03:33PM (#25585734)
No that's not the case. I am fairly sure that situation you describe results in a type error. The extension does in fact enforce linearity, and does not have such a trivial & universal escape hatch afaiu.

Feel free to give GHC 9.0 a try with a minimal example of your scenario!

Re:GHC 9.0, supporting linear types by ghostwriter on Wednesday December 30, 2020 @03:44PM (#25585866)
There is a recent article about these scenarios, you may find it helpful - https://tek.brick.do/64693fb8-39b4-40a5-8762-768009eeed91
Re:GHC 9.0, supporting linear types by Ericson2314 on Wednesday December 30, 2020 @11:40PM (#25589653)
There are a number of Haskellers with similar misgivings. The authors of the current extension thought that this fit most nicely with the current language / implementation.
Re:GHC 9.0, supporting linear types by tome on Thursday December 31, 2020 @06:34AM (#25591744)
I don't see how, in the presence of unrestricted function types, you can tag values as linear unless you have a separate kind for linear values. I'd be interested to know how Rust does it.
Re:GHC 9.0, supporting linear types by kimundi on Thursday December 31, 2020 @08:26AM (#25592450)
Affinity is a property of the type in Rust, so any given value is affine or not depending on which type it has.
Re:GHC 9.0, supporting linear types by tome on Thursday December 31, 2020 @11:51AM (#25594332)
I see, and if you have a polymorphic type then presumably you get to know its affinity too?
Re:GHC 9.0, supporting linear types by ric2b on Wednesday December 30, 2020 @05:06PM (#25586728)
What is "the arrow" in this context?
Re:GHC 9.0, supporting linear types by tremon on Wednesday December 30, 2020 @05:34PM (#25587044)
"the arrow" is an expression of a function operation, or rather, how a function transforms its arguments. In Haskell, the following signature:

  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.
Re:GHC 9.0, supporting linear types by paavohtl on Wednesday December 30, 2020 @05:54PM (#25587256)
I don't find this answer satisfactory, because you also need to specify in Rust whether or not a function moves its argument(s).

  // 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 line
Re:GHC 9.0, supporting linear types by Ericson2314 on Wednesday December 30, 2020 @11:39PM (#25589649)
From the perspective of Rust, Linear Haskell is going to seem quite weird, and I say that as Haskeller wishing that language extension was more like Rust.

I'm not really sure how to explain it, but basically only functions that are themselves linear can have linear parameters, It's vaguely like "`Fn(A) -> B` only if `A: Copy`", and `FnOnce(A) -> B` only if `A: !Copy`, except there is no `Copy` or `!Copy` and the only say to determine it is `Fn` vs `FnOnce`.

Re:GHC 9.0, supporting linear types by Raphael_Amiard on Wednesday December 30, 2020 @01:59PM (#25584748)
Affine types, which Rust more or less implements with its ownership system are very close to linear types, see https://en.m.wikipedia.org/wiki/Substructural_type_system#Af...
Re:GHC 9.0, supporting linear types by KirillPanov on Wednesday December 30, 2020 @06:18PM (#25587489)
For the most part they accomplish the same goals, but in very different ways.

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.