frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Algebraic Types are not Scary

https://blog.aiono.dev/posts/algebraic-types-are-not-scary,-actually.html
32•Bogdanp•2d ago

Comments

raxxorraxor•2d ago
> Algebraic Types are Just Elementary School Algebra

My math prof did say the exact same while torturing students with question about proofs about their arcane set of arbitrary numbers and if they can be considered a field or ring or a group or everything at the same time.

Sure, just some + and *...

And sure, for a programmer it is mostly about which operations are defined on the type. But with just a few tweaks here and there you can transform a tool into a torture device...

Jokes aside, I think this is a good explanation about the concepts and parallels.

HelloNurse•1d ago
> their arcane set of arbitrary numbers and if they can be considered a field or ring or a group or everything at the same time.

A toolbag of abstract theory and tools that can be straightforwardly applied to any "arcane set" is the value that upgrading from arithmetic to algebra provides.

ekidd•2h ago
"Sum types" (aka "Rust enums") are something that I've really come to love for representing the idea "There are 3 possible cases here, here's the data we need in each case, and please remember that these cases are strictly mutually exclusive."

Making this super easy and short to express makes a lot of designs clearer.

There's also an interesting tradeoff here between sum types and abstract interfaces:

- Abstract interfaces: "We have an unknown number of cases/subclasses, but they each support this fixed set of methods." The set of implementations is open, but the set of operations is harder to update.

- Sum types (aka Rust enums): "We know all the cases, but there may be many different methods." The set of cases is harder to extend, but the set of methods manipulating them is completely open.

A good example of the latter pattern is actually LLVM! LLVMs' intermediate representation is a essentially a small assembly language with a fixed set of operations (and an escape hatch). But because the set of basic instructions is small and known, it's possible to write dozens and dozens of optimization passes that each manipulate that small set of instructions.

And a surprising number of programs work well with an LLVM-like structure.

So in a language like Rust, which supports both abstract interfaces (via traits) and sum types (via enums), the tradeoff is whether you think you have many different types of values that you'll manipulate in a limited number of ways, or a small number of types of values that you'll manipulate in many different ways.

(Oh, and the metaphor of "algebraic types" goes deeper than you might think; there are some super interesting data structures based on taking the "derivatives" of simple algebraic types.)

phkahler•1h ago
Sum type sounds like the old Variant from VB.

We use something similar in Solvespace. Where one might have used polymorphism, there is one class with a type enum and a bunch of members that can be used for different things depending on the type. Definitely some disadvantages (what does valA hold in this type?) but also some advantages from the consistency.

ulrikrasmussen•58m ago
The lack of sum types should really be considered a weird omission rather than a strange fancy language feature. I suspect that their omission from popular languages is mostly due to the historical belief that object-orientation was the way forward and that "everything is an object", leading many programmers to represent things that are decidedly not objects using abstractions designed for objects. An object is a stateful thing which exists over a period of time (sometimes unbounded) and whose identity is characterized by its observable behavior. Abstractions for objects consist of interfaces which expose only a slice of the observable behavior to consumers. On the other hand, a value is really just that; a value. It does not have state, it does not start or stop existing, and its identity is defined only by its attributes. You can use objects to model values (poorly), but you often end up doing a lot of extra work to stop your "fake value" objects from behaving like objects, e.g. by being careful to make all fields immutable and implementing proper deep equality. Abstractions for values are not naturally expressed using interfaces because interfaces force the implementation to be tied to the instance of an object, but since values do not have a lifetime this restriction is a huge disadvantage and often leads to clunky and inflexible abstractions. For example, consider the Comparable interface in Java which tells you that an object is modeling a value which can be compared to other values of the same type. It would be awfully nice if List<A> could implement this interface, but it cannot because doing so will mean that you can only create lists of things (of some type A) which have a total order defined on them.

However, if you consider programming to not only be about expressing what you can do to stateful objects but also expressing values and their operations, then algebraic data types and traits/modules/type classes become the natural basic vocabulary as opposed to classes and interfaces. When dealing with first-order algebraic data types, products (i.e. records) and coproducts (i.e. sum types) are unavoidable. A list is one of the simplest algebraic data types which use both products and coproducts:

data List a = Nil | Cons a (List a)

One trait of lists is that lists of totally ordered things are themselves totally ordered using lexicographic ordering, and this is in Haskell expressed as a type class instance

instance Ord a => Ord (List a) where ...

Crucially, the type class is removed from the definition of what a list is, which enables us to still talk about lists of e.g. functions which do not have an order on them. These lists do not have the Ord trait, but they are still lists.

Another important distinction between traits and interfaces is that some behavior of value types consist of simply identifying special values. For example, the Monoid type class in Haskell is

    typeclass Monoid a where
      mempty :: a
      mappend :: a -> a -> a
It is impossible to express Monoid as an interface because one of the features of a monoid is that you have a special element `mempty`, but consumers cannot get this value without first having another value of type `a` on their hands.

Many languages now have proper support for both objects and values in that they have better primitives for defining both products and coproducts, but I still think that most mainstream languages are missing proper primitives for defining traits.

GhosT078•3m ago
This tradeoff sounds similar to the choice to use "tagged types" versus "variant records" in Ada. Ada has provided variant records since 1983 and tagged types since 1995 (and both with a very nice syntax).
zk108•1h ago
...and a monad is just a monoid in the category of endofunctors of some fixed category

Just kidding but algebraic types are a great abstraction paradigm,

zokier•41m ago
ADTs are just structs and tagged unions. Tbh I don't understand why there is such fetishization of them considering how mudane they are.
aiono•4m ago
That's the point I tried to make. All my bachelor education there was no single mention of sum types even though we learned about the visitor pattern. Instead of having to model sum types with object hierarchies, the language should provide a straightforward way to represent them since it's a very basic concept. I think that while things have improved a lot compared to past, sum types concept is still not known as much as objects for instance. Today all mainstream languages added direct support for sum types but awareness of it lacks.

Apple Photos App Corrupts Images

https://tenderlovemaking.com/2025/09/17/apple-photos-app-corrupts-images/
275•pattyj•2h ago•79 comments

Determination of the fifth Busy Beaver value

https://arxiv.org/abs/2509.12337
110•marvinborner•3h ago•22 comments

GNU Midnight Commander

https://midnight-commander.org/
376•pykello•9h ago•212 comments

PureVPN IPv6 Leak

https://anagogistis.com/posts/purevpn-ipv6-leak/
72•todsacerdoti•3h ago•9 comments

Firefox 143 for Android to introduce DoH

https://blog.mozilla.org/en/firefox/dns-android/
35•HieronymusBosch•33m ago•12 comments

Shai-Hulud malware attack: Tinycolor and over 40 NPM packages compromised

https://socket.dev/blog/ongoing-supply-chain-attack-targets-crowdstrike-npm-packages
1100•jamesberthoty•1d ago•908 comments

Tau² Benchmark: How a Prompt Rewrite Boosted GPT-5-Mini by 22%

https://quesma.com/blog/tau2-benchmark-improving-results-smaller-models/
12•blndrt•44m ago•0 comments

Notion API importer, with Databases to Bases conversion bounty

https://github.com/obsidianmd/obsidian-importer/issues/421
141•twapi•8h ago•40 comments

Why We're Building Stategraph: Terraform State as a Distributed Systems Problem

https://stategraph.dev/blog/why-stategraph/
59•lawnchair•5h ago•36 comments

Alibaba's New AI Chip Unveiled: Key Specifications Comparable to H20

https://news.futunn.com/en/post/62202518/alibaba-s-new-ai-chip-unveiled-key-specifications-compar...
110•dworks•4h ago•108 comments

Things you can do with a Software Defined Radio (2024)

https://blinry.org/50-things-with-sdr/
861•mihau•23h ago•139 comments

You can't test if quantum uses complex numbers

https://algassert.com/post/2501
17•EvgeniyZh•2d ago•5 comments

Murex – An intuitive and content aware shell for a modern command line

https://murex.rocks/
68•modinfo•7h ago•35 comments

Doom crash after 2.5 years of real-world runtime confirmed on real hardware

https://lenowo.org/viewtopic.php?t=31
338•minki_the_avali•16h ago•108 comments

Procedural Island Generation (III)

https://brashandplucky.com/2025/09/17/procedural-island-generation-iii.html
9•ibobev•1h ago•0 comments

How to make the Framework Desktop run even quieter

https://noctua.at/en/how-to-make-the-framework-desktop-run-even-quieter
300•lwhsiao•19h ago•106 comments

The Asus Gaming Laptop ACPI Firmware Bug: A Deep Technical Investigation

https://github.com/Zephkek/Asus-ROG-Aml-Deep-Dive
281•signa11•9h ago•139 comments

Algebraic Types are not Scary

https://blog.aiono.dev/posts/algebraic-types-are-not-scary,-actually.html
32•Bogdanp•2d ago•9 comments

Denmark close to wiping out cancer-causing HPV strains after vaccine roll-out

https://www.gavi.org/vaccineswork/denmark-close-wiping-out-leading-cancer-causing-hpv-strains-aft...
844•slu•19h ago•314 comments

I got the highest score on ARC-AGI again swapping Python for English

https://jeremyberman.substack.com/p/how-i-got-the-highest-score-on-arc-agi-again
122•freediver•11h ago•56 comments

XeroxNostalgia.com

https://xeroxnostalgia.com/
8•surprisetalk•2d ago•0 comments

DataTables CDN Outage – post incident review

https://datatables.net/blog/2025/july-29-outage
30•cristoperb•18h ago•17 comments

Oh no, not again a meditation on NPM supply chain attacks

https://tane.dev/2025/09/oh-no-not-again...-a-meditation-on-npm-supply-chain-attacks/
107•theycameback•3h ago•134 comments

A dumb introduction to z3

https://asibahi.github.io/thoughts/a-gentle-introduction-to-z3/
228•kfl•2d ago•32 comments

AMD Open Source Driver for Vulkan project is discontinued

https://github.com/GPUOpen-Drivers/AMDVLK/discussions/416
116•haunter•13h ago•29 comments

EU Chat Control: Germany's position has been reverted to UNDECIDED

https://mastodon.social/@chatcontrol/115215006562371435
207•doener•3h ago•156 comments

Samsung 870 QVO 4TB SATA SSD-s: how are they doing after 4 years of use?

https://ounapuu.ee/posts/2025/09/15/samsung-870-qvo/
56•furkansahin•2d ago•22 comments

Normal-order syntax-rules and proving the fix-point of call/cc

https://okmij.org/ftp/Scheme/callcc-calc-page.html
33•Bogdanp•3d ago•1 comments

Waymo has received our pilot permit allowing for commercial operations at SFO

https://waymo.com/blog/#short-all-systems-go-at-sfo-waymo-has-received-our-pilot-permit
667•ChrisArchitect•21h ago•682 comments

I built my own phone because innovation is sad rn [video]

https://www.youtube.com/watch?v=qy_9w_c2ub0
276•Timothee•2d ago•53 comments