Live data from Hacker News

Sum Types in Julia and Rust

andreaskroepelin.de

21–30 of 70 posts

Re: Sum Types in Julia and Rust

#21
post #3

I may be wrong, but I don't think this is what people mean by 'sum types'. I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... ) I guess enums are a type of sum type, but seems this is more specifically about enums

Simple way to think about it is plus. Sum plus sum increases, union plus union does nothing. 1. A union type is like int | double. It means the value is one of a set of possible types. And it's a true set: `int | int | double` is indistinguishable from `int | double`. 2. A sum type is a new type built from a list of other types, which assigns a 'tag' to each possible list element, like a Rust enum. The tags are not t…

It is possible (and popular) to have sum types in TypeScript by manually adding a tag, though.

  type Maybe =
    | { kind: "Some", value: T }
    | { kind: "None" }

Re: Sum Types in Julia and Rust

#22
post #16

Two thoughts: For this problem, I would use union types in Julia. Union types are a sort of sum, but they are amalgamated sums whilst sums types in PL semantics usually means disjoint sums. The difference is that disjoint sums 'mark' whether a value is of the left or right type, while with amalgamated sums the value may belong unmarked to the intersection. The distinction does not matter in the example the post gives…

Julia is a dynamic language, I don't think having the rigor of a compiled language (probably the most rigorous out there) is a reasonable expectation. It would be nice to have as an external linter service, probably.

I think you are mixing two different things.

Julia is a compiled language and Rust is too. Julia is dynamically typed while Rust is statically typed.

Re: Sum Types in Julia and Rust

#23

Earlier quoted context omitted.

Simple way to think about it is plus. Sum plus sum increases, union plus union does nothing. 1. A union type is like int | double. It means the value is one of a set of possible types. And it's a true set: `int | int | double` is indistinguishable from `int | double`. 2. A sum type is a new type built from a list of other types, which assigns a 'tag' to each possible list element, like a Rust enum. The tags are not t…

It is possible (and popular) to have sum types in TypeScript by manually adding a tag, though. type Maybe = | { kind: "Some", value: T } | { kind: "None" }

This is also why sum types are often referred to as tagged unions. They are a special case of a union, in Rust's case with support from the type system.

Re: Sum Types in Julia and Rust

#24

Earlier quoted context omitted.

Simple way to think about it is plus. Sum plus sum increases, union plus union does nothing. 1. A union type is like int | double. It means the value is one of a set of possible types. And it's a true set: `int | int | double` is indistinguishable from `int | double`. 2. A sum type is a new type built from a list of other types, which assigns a 'tag' to each possible list element, like a Rust enum. The tags are not t…

It is possible (and popular) to have sum types in TypeScript by manually adding a tag, though. type Maybe = | { kind: "Some", value: T } | { kind: "None" }

Yep, Typescript's control flow analysis, type literals, and union types allow us have tagged unions/sum types. Probably its best feature.

Re: Sum Types in Julia and Rust

#25

Earlier quoted context omitted.

Rust enums are full union types. i.e. while each possibility in a (say) Java enum must be of the same type, each possibility in a Rust enum can be of a different type. i.e. https://play.rust-lang.org/?version=stable&mode=debug&editio...

However, Rust's enum-type members are still named - to my knowledge you can't have anonymous enum type members in Rust. In the example you gave, the type-of `left` is still `SumType::Left` instead of being just `String`. I'm not too familiar with Rust to say, but I don't consider this to be a syntactically zero-cost abstraction (even if the wrapper-types are elided by the compiler) because we still have more keyboard…

Ah, that's to enable you to have multiple members that could hold `String` values. Yeah, it comes with the (syntactic) cost that you have to identify the member.

Re: Sum Types in Julia and Rust

#26
> Use subtyping in Julia and enums in Rust.

I would use enums in Julia, making the enum you define a field of the PlayerClass:

  @enum PlayerStar Sol Pol Cent
  struct PlayerClass
      star::PlayerStar
      # other fields
  end
Julia's compiler will split small unions into static dispatches behind branches, and can also decide whether or not to specialize on types or create a generic functions.

But if your code is performance sensitive, I'm more comfortable controlling the behavior than relying on these optimizations. The problem is, dispatch is often a more convenient coding style than long branches.

Re: Sum Types in Julia and Rust

#27
post #3

I may be wrong, but I don't think this is what people mean by 'sum types'. I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... ) I guess enums are a type of sum type, but seems this is more specifically about enums

A sum type is a tagged union [1]. In C, a sum type can be modeled by the combination of an enum and a union. The enum would tell you which field of the union you should use. In Rust, enum's subsume unions and enums of C. This is analogous to a a disjoint union [2] in set theory. Notice that in both cases you have a pair of data. Sum types and disjoint unions are both instances of coproducts in category theory, by the way.

1. https://en.wikipedia.org/wiki/Tagged_union

2. https://en.wikipedia.org/wiki/Disjoint_union

Re: Sum Types in Julia and Rust

#30
post #3

I may be wrong, but I don't think this is what people mean by 'sum types'. I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... ) I guess enums are a type of sum type, but seems this is more specifically about enums

Simple way to think about it is plus. Sum plus sum increases, union plus union does nothing. 1. A union type is like int | double. It means the value is one of a set of possible types. And it's a true set: `int | int | double` is indistinguishable from `int | double`. 2. A sum type is a new type built from a list of other types, which assigns a 'tag' to each possible list element, like a Rust enum. The tags are not t…

TypeScript does have tagged unions in addition to untagged ones https://www.typescriptlang.org/docs/handbook/release-notes/t...
Post reply on HN