Questions tagged [algebraic-data-types]

Algebraic data types are data structures built from sums, products and recursive types, admitting an algebra. Descriptions of types given as ADTs may be manipulated symbolically to derive other related data structures. The logic for building types algebraically is related to combinatorial species in combinatorial mathematics.

On the algebraic notation for such types:

  • X for the singleton container type
  • + for choice between two types (tagged unions)
  • for the ordered product of two types
  • and a least fixed-point operator, that may be implicit.

Additionally, we have:

  • 0 for the empty type
  • 1 for unit
  • X^2 for X•X

Thus, we can describe e.g. the type of binary trees as:

  • B = 1 + X • B^2

or the type of inductive lists:

  • L = 1 + X • L

Regular data structures described in this form are the core of algebraic data types as found in Haskell or ML.

References

The Algebra of Algebraic Data Types

447 questions
299
votes
7 answers

Abusing the algebra of algebraic data types - why does this work?

The 'algebraic' expression for algebraic data types looks very suggestive to someone with a background in mathematics. Let me try to explain what I mean. Having defined the basic types Product • Union + Singleton X Unit 1 and using the shorthand…
Chris Taylor
  • 44,831
  • 12
  • 101
  • 146
128
votes
8 answers

How do you represent a graph in Haskell?

It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag =…
125
votes
2 answers

Memory footprint of Haskell data types

How can I find the actual amount of memory required to store a value of some data type in Haskell (mostly with GHC)? Is it possible to evaluate it at runtime (e.g. in GHCi) or is it possible to estimate memory requirements of a compound data type…
sastanin
  • 36,792
  • 11
  • 94
  • 126
68
votes
2 answers

What is the DataKinds extension of Haskell?

I am trying to find an explanation of the DataKinds extension that will make sense to me having come from only having read Learn You a Haskell. Is there a standard source that will make sense to me with what little I've learned? Edit: For example…
user782220
  • 9,199
  • 16
  • 64
  • 127
60
votes
8 answers

Haskell's algebraic data types

I'm trying to fully understand all of Haskell's concepts. In what ways are algebraic data types similar to generic types, e.g., in C# and Java? And how are they different? What's so algebraic about them anyway? I'm familiar with universal algebra…
50
votes
3 answers

Kotlin and discriminated unions (sum types)

Does Kotlin have anything like discriminated unions (sum types)? What would be the idiomatic Kotlin translation of this (F#): type OrderMessage = | New of Id: int * Quantity: int | Cancel of Id: int let handleMessage msg = match msg…
ehnmark
  • 2,406
  • 3
  • 23
  • 20
48
votes
4 answers

Java tagged union / sum types

Is there any way to define a sum type in Java? Java seems to naturally support product types directly, and I thought enums might allow it to support sum types, and inheritance looks like maybe it could do it, but there is at least one case I can't…
Zoey Hewll
  • 3,400
  • 2
  • 16
  • 30
47
votes
4 answers

What are "sums-and-products" data structures?

A recent blog post on William Cook's Fusings mentions: The key point is that structures in Ensō are viewed holistically as graphs, not as individual values or traditional sums-and-products data structures. What are the traditional…
40
votes
1 answer

Haskell record pattern matching

I'm looking for a way to simplify function patterns when the actual data is not required: data X = A | B String | C Int Int String myfn :: X -> Int myfn A = 50 myfn (B _) = 200 myfn (C _ _ _) = 500 Is there a way to make a simpler pattern for…
theduke
  • 2,702
  • 4
  • 26
  • 28
35
votes
1 answer

How can I see the full expanded contract of a Typescript type?

If I have a collection of types that looks a bit like this, only more verbose: type ValidValues = string | number | null type ValidTypes = "text" | "time" | "unknown" type Decorated = { name?: string | null type?: ValidTypes value?:…
suddjian
  • 1,032
  • 9
  • 23
35
votes
5 answers

Best way to define algebraic data types in Python?

I know that Python is NOT Haskell or Ocaml, but which is the best way to define algebraic data types in Python (2 or 3)?
user1234299
  • 689
  • 1
  • 5
  • 14
33
votes
3 answers

Is there a Haskell equivalent of OOP's abstract classes, using algebraic data types or polymorphism?

In Haskell, is it possible to write a function with a signature that can accept two different (although similar) data types, and operate differently depending on what type is passed in? An example might make my question clearer. If I have a function…
31
votes
2 answers

How to decode an ADT with circe without disambiguating objects

Suppose I've got an ADT like this: sealed trait Event case class Foo(i: Int) extends Event case class Bar(s: String) extends Event case class Baz(c: Char) extends Event case class Qux(values: List[String]) extends Event The default generic…
Travis Brown
  • 135,682
  • 12
  • 352
  • 654
29
votes
3 answers

What is idiomatic modern C++ for algebraic data types?

Suppose, for example, you want to implement a spreadsheet Cell in C++. A cell can be either a string, a number, or perhaps empty. Ignore other cases, like it being a formula. In Haskell, you might do something like: data Cell = CellStr String |…
blippy
  • 1,270
  • 1
  • 10
  • 16
25
votes
2 answers

Is it possible to generate and execute Rust code at runtime?

Using C, at runtime, I can: Create the source code of a function, Call out to gcc to compile it to a .so (Linux) (or use llvm, etc.), Load the .so, and Call the function. Is a similar thing possible in Rust? In particular I want to use Algebraic…
fadedbee
  • 37,386
  • 39
  • 142
  • 236
1
2 3
29 30