News for hackers. Stuff that matters.
Re:How Rust Gets Polymorphism Right [video] by myst on Monday November 13, 2017 @02:16AM (#15684822)
I’d love to see comparison between Rust and Haskell/ML WRT polymorphism. I wonder if “gets right” would appear there and on what side.
Re:How Rust Gets Polymorphism Right [video] by jmite on Monday November 13, 2017 @03:36AM (#15685129)
Rust's traits are basically Haskell typeclasses, with a few extensions enabled, like multiple parameters.

Trait objects are basically existential types, with a few more restrictions on them. But in essence polymorphism in Haskell and Rust are very similar.

The big thing Rust is missing is higher kinded polymorphism, ie the ability to abstract over type constructors. This makes it easy, for example, to abstract over a container type while putting different things in that container.

This has get approved in the Associated Type Constructors RFC, but it's still in the works for actually landing.

Re:How Rust Gets Polymorphism Right [video] by moomin on Monday November 13, 2017 @03:46AM (#15685179)
One thing no-one else has mentioned yet: in Haskell you can’t provide a specialised implementation of a generic function. In Rust you can.

Whilst this is obviously useful, it makes it impossible to perform parametricity type-based reasonbig.

Re:How Rust Gets Polymorphism Right [video] by hiker on Monday November 13, 2017 @06:56AM (#15685838)
Of course you can. The good old {-# LANGUAGE OverlappingInstances #-}.
Re:How Rust Gets Polymorphism Right [video] by jeremyjh on Monday November 13, 2017 @09:00AM (#15686549)
In Haskell you have to supply the entire instance, versus just specializing one method as you can do in Rust. Also in Haskell this extension is not recommended for general use and you won’t often find specialization like this in the wild.
Re:How Rust Gets Polymorphism Right [video] by ves on Monday November 13, 2017 @11:59AM (#15687876)
The typical way to do multiple instances for a given type is with trivial newtype wrappers for each instance. See the "Sum" and "Product" monoid instances (although these wrappers are also necessary for instance decidability). This is fine "in the wild."

Also, I'm not sure why you would ever want a "partial instance," as it would mean that your program could be unsafe but still typecheck (I haven't checked out Rust's implementation or motivation). Just use more specific classes, and you can even use a "generic" "instances of the components imply an instance of the whole" class and instance.

Re:How Rust Gets Polymorphism Right [video] by jeremyjh on Monday November 13, 2017 @05:59PM (#15691082)
When you specialize an instance in Rust, by definition it already has a valid instance that covers that type. So implementing a single method is fine - the other ones are provided by the instance you are specializing.

Newtypes also exist in Rust but this is an alternative that is not the same as the overlapping instances extension.

Re:How Rust Gets Polymorphism Right [video] by marcosdumay on Monday November 13, 2017 @12:03PM (#15687910)
Rust will find out why as soon as this gets widespread.

You can either have overloading classes or you can have your classes declaration independent of the type declarations. You can not have both.

Re:How Rust Gets Polymorphism Right [video] by domlebo70 on Monday November 13, 2017 @04:07AM (#15685246)
I think you can using GHC Pragmas.
Re:How Rust Gets Polymorphism Right [video] by axlprose on Monday November 13, 2017 @03:35AM (#15685125)
Here's a post that compares the two pretty well, though it assumes you know a bit of some ML-style language:

http://science.raphael.poss.name/rust-for-functional-program...

Re:How Rust Gets Polymorphism Right [video] by jmcomets on Monday November 13, 2017 @02:46AM (#15684925)
Apples & Oranges.

Although Rust heavily borrows from the ML family, the fact it has mutability and imperative reasoning makes it hard to compare them.

But if you want a short one:

- Sum Types EDIT: AND Product Types (silly me)

- function-objects & closures

- typeclasses (as far as I can tell), but no higher kinded types

- no monads, functors nor monoids built-in

- trait objects (eg. runtime polymorphism, think "abstract classes")

Re:How Rust Gets Polymorphism Right [video] by MHordecki on Monday November 13, 2017 @03:09AM (#15685020)
There are other imperative languages in the ML family too, right? OCaml has an imperative mode.

I don't think Rust is that much different from other functional languages. The main critical difference is the manual memory management feature - the way it's acknowledged and used throughout the language and the ecosystem means that the functional patterns commonly used in garbage collected languages don't look nearly as natural in Rust.

Re:How Rust Gets Polymorphism Right [video] by ketralnis on Monday November 13, 2017 @03:07AM (#15685006)
> - Sum Types but no Product Types

Maybe I'm missing something, but aren't tuples and structs/records examples of product types? Which both rust and haskell have?

Re:How Rust Gets Polymorphism Right [video] by mathw on Monday November 13, 2017 @03:20AM (#15685065)
Yes I always thought tuples were the archetypal product type, which is why in some ML dialects they're notated as "Type1 * Type2".

Rust most certainly has them, as does Haskell. Records/structs of course are just the same thing with names for the components.

Thus, even Java has product types.

Re:How Rust Gets Polymorphism Right [video] by masklinn on Monday November 13, 2017 @03:44AM (#15685166)
> Thus, even Java has product types.

Even C has product types. Which is why I'm always bothered when people claim language X lacks algebraic data types.

They probably lack sum types, but are very unlikely to lack product types.

Re:How Rust Gets Polymorphism Right [video] by kod on Monday November 13, 2017 @08:26AM (#15686355)
Product types without sum types and pattern matching are basically useless for expressing ADTs. That's what people mean.
Re:How Rust Gets Polymorphism Right [video] by masklinn on Monday November 13, 2017 @08:31AM (#15686382)
> Product types without sum types and pattern matching are basically useless for expressing ADTs.

That makes no sense. Algebraic data types are not expressed, they're a concrete thing, a classification of composite types (by multiplication (hence product types) or addition (hence sum types)).

Re:How Rust Gets Polymorphism Right [video] by kod on Monday November 13, 2017 @01:56PM (#15689026)
Sure it does, languages allow you to express things.

Some shitty languages don't have a concise way for you to express particular things (like sum types), so you have to rely on idioms or "patterns".

Re:How Rust Gets Polymorphism Right [video] by beojan on Monday November 13, 2017 @05:45AM (#15685557)
C also has sum types with unions (though without any indication of which of the alternatives is actually present).
Re:How Rust Gets Polymorphism Right [video] by red75prime on Monday November 13, 2017 @06:43AM (#15685779)
They are mix types then.
Re:How Rust Gets Polymorphism Right [video] by bjz_ on Monday November 13, 2017 @07:44AM (#15686055)
And in SML tuples are just records with numeric fields in a special order. Enter this in the repl:

    { 1 = "hi", 2 = "howdy" };
And get:

    val it = ("hi","howdy") : string * string
Re:How Rust Gets Polymorphism Right [video] by kornish on Monday November 13, 2017 @03:44AM (#15685168)
Oh wow, never realized the connection between product types and the ML type notation. Thanks for the insight!
Re:How Rust Gets Polymorphism Right [video] by jmcomets on Monday November 13, 2017 @04:09AM (#15685254)
Thanks for the insight on this, I've edited my response. Are there any extra features in Haskell with product types? I've always understood that there was better composition support for product types in Haskell
Re:How Rust Gets Polymorphism Right [video] by bjz_ on Monday November 13, 2017 @09:15AM (#15686638)
Not really. Haskell has support for higher kinded types that allow it to support lenses, which are a powerful way of declaratively traversing through data structures, so perhaps that's what you mean. On the other hand Haskell's record support is pretty woeful in comparison to languages like Ocaml, Elm, and Purescript that have support for row polymorphism. Depending on the sophistication of their implementation, row polymorphism will allow you to add and remove fields from records in a statically type checked way, and some may even allow you to pass labels around at the type level. They can even be used to model extensible variants and extensible effect types. There are creative ways to encode this in Haskell, but they all have their drawbacks, can be painful to work with, and really don't feel like a natural part of the language.
Re:How Rust Gets Polymorphism Right [video] by kazagistar on Monday November 13, 2017 @11:05AM (#15687417)
Well, there is per-field lazyness (with the ability to opt out). So product types can let each consumer choose which parts of the result to actually evaluate and discard the rest.