News for hackers. Stuff that matters.
Re:GHC 9.0, supporting linear types by chrisaycock on Wednesday December 30, 2020 @01:57PM (#25584736)
I first became fascinated by linear [1] and uniqueness [2] types with Clean [3], a purely functional language that handles state and IO without monads.

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

[3] https://clean.cs.ru.nl/Clean

[4] https://arxiv.org/abs/1710.09756

Re:GHC 9.0, supporting linear types by dmix on Wednesday December 30, 2020 @04:43PM (#25586523)
Man, I wish I understood all of this stuff. I have a general enough notion from digging into Haskell and reading into types (including dependent types). I see the value of uniqueness just from using Typescript and having dumb clashes of type variable names, which may be unrelated.

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

Re:GHC 9.0, supporting linear types by yodsanklai on Wednesday December 30, 2020 @05:08PM (#25586755)
> But like a lot of Haskell much of this still goes over my head.

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.

Re:GHC 9.0, supporting linear types by roflc0ptic on Wednesday December 30, 2020 @04:50PM (#25586574)
I have a degree in geography, and learned to do web development largely on the job. It took me about 5 years before I ventured into learning functional programming, and in retrospect, I feel I could’ve been 5 years farther ahead today. It makes you a better programmer, and while it might only make you marginally better at the tasks you currently do, it will open up a wide expanse of other possibilities you didn’t think yourself capable of.
Re:GHC 9.0, supporting linear types by whateveracct on Wednesday December 30, 2020 @05:16PM (#25586841)
> it will open up a wide expanse of other possibilities you didn’t think yourself capable of

Haskell has a tendency to make every problem seem exactly the same :)

Re:GHC 9.0, supporting linear types by solomonb on Wednesday December 30, 2020 @10:17PM (#25589217)
"Did you try traverse?" :D
Re:GHC 9.0, supporting linear types by didibus on Thursday December 31, 2020 @07:52AM (#25592185)
It's not as complicated as the lingo. A type is just metadata about variables and values tracked by the compiler on the static code, as it compiles it.

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.

Re:GHC 9.0, supporting linear types by dmix on Friday January 01, 2021 @07:08PM (#25608743)
Wow this was a great break down. I've never really heard it in this sort of fashion and it's an interesting approach to teaching types. Language and 'lingo' really does muddy the waters educationally.

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.

Re:GHC 9.0, supporting linear types by timhwang21 on Thursday December 31, 2020 @10:10AM (#25593319)
Fantastic comment! Thank you so much for this.
Re:GHC 9.0, supporting linear types by auslegung on Wednesday December 30, 2020 @08:38PM (#25588588)
Have you tried purescript? As a frontend developer you might have more opportunity to use it, and it is heavily influenced by Haskell.
Re:GHC 9.0, supporting linear types by dmix on Friday January 01, 2021 @06:56PM (#25608645)
Yes I tried Purescript in the early days and it was buggy as hell with little documentation (even blog post tutorials or githubb repos to learn from). So I decided to wait a couple years before checking it out again, which is coming soon I hope...
Re:GHC 9.0, supporting linear types by Kaze404 on Wednesday December 30, 2020 @10:12PM (#25589185)
There's also Elm.
Re:GHC 9.0, supporting linear types by auslegung on Thursday December 31, 2020 @09:56AM (#25593195)
I love Elm but if the poster wants Haskell-ish experience purescript is much more similar. Otoh Elm is simpler and therefore might be a better next step. But since they already seem to have Haskell experience I’d generally recommend purescript if they just want to learn. If they’re choosing a technology for a production product then that’s a completely different consideration
Re:GHC 9.0, supporting linear types by dmix on Friday January 01, 2021 @06:57PM (#25608652)
I just want to say I concur with this. I don't consider Elm a full alternative to Purescript, even though I understand the draw.

Purescript is more in line with the harder core FP or frontend Haskell I was seeking.

On another note, Rust does types and pattern matching quite well where I see that as another draw away from Haskell. But I feel like Haskell is the motherland for this stuff so might as well figure it out eventually.

Re:GHC 9.0, supporting linear types by japgolly on Thursday December 31, 2020 @01:58AM (#25590286)
> linear-typed arrays can perform in-place updates if there are no external pointers to the data.

Holy crap! That's amazing and I hadn't thought of that!