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
21
votes
2 answers

Do algebraic datatypes in Haskell equal discriminated unions in F#?

I am learning Haskell and would like to know whether the constructs known in Haskell as algebraic datatypes are the same that discriminated unions in F# or there are some subtle differences between them. I would also appreciate much a good…
21
votes
2 answers

Idris vectors vs linked lists

Does Idris do any kind of optimization under the hood of vectors? Because from the looks of it, an Idris vector is just a linked list with known size (known at compile time). In fact, in general it seems like you could express the following…
limp_chimp
  • 9,927
  • 12
  • 51
  • 102
20
votes
1 answer

Performance of struct tuples

The following F# program defines a function that returns the smaller of two pairs of ints represented as struct tuples and it takes 1.4s to run: let [] main _ = let min a b : int = if a < b then a else b let min (struct(a1, b1))…
J D
  • 46,493
  • 12
  • 162
  • 266
18
votes
2 answers

Data Structure Differentiation, Intuition Building

According to this paper differentiation works on data structures. According to this answer: Differentiation, the derivative of a data type D (given as D') is the type of D-structures with a single “hole”, that is, a distinguished location not…
dsg
  • 12,504
  • 19
  • 64
  • 109
18
votes
1 answer

How to create ADT in Haskell?

In Scala I can describe such ADT: sealed trait Foo case class A(a: Int) extends Foo case class B(b: String) extends Foo case class C(a: A, b: B) extends Foo How can I do the same in Haskell? data Foo = A Int | B String | C A B It doesn't work,…
Andrew
  • 183
  • 4
18
votes
4 answers

C++ equivalent of algebraic datatype?

Let's say I have this Haskell code: data RigidBody = RigidBody Vector3 Vector3 Float Shape -- position, velocity, mass and shape data Shape = Ball Float -- radius | ConvexPolygon [Triangle] What would be the best way to express this in…
user3133295
  • 293
  • 2
  • 8
17
votes
1 answer

Algebraic data types in Kotlin

I am trying to figure out how to use algebraic data types in Kotlin, so I'm trying to implement a basic BinaryTree type the following way. sealed class Tree{ class Node(val left: Tree, val right: Tree): Tree() class Leaf(val…
pjozsef
  • 765
  • 7
  • 14
17
votes
3 answers

Is there any algebraic structures used in functional programming other then monoid?

I recently getting to know about functional programming (in Haskell and Scala). It's capabilities and elegance is quite charming. But when I met Monads, which makes use of an algebraic structure named Monoid, I was surprised and glad to see the…
17
votes
1 answer

How do functional languages represent algebraic data types in memory?

If you were writing a bioinformatics algorithm in Haskell, you'd probably use an algebraic data type to represent the nucleotides: data Nucleotide = A | T | C | G You'd do similarly in Standard ML or OCaml, I assume (I've never really used…
Mike
  • 1,509
  • 2
  • 14
  • 20
17
votes
1 answer

Type algebra and Knuth's up arrow notation

Reading through this question and this blog post got me thinking more about type algebra and specifically how to abuse it. Basically, 1) We can think of the Either A B type as addition: A+B 2) We can think of the ordered pair (A,B) as…
Mike Izbicki
  • 6,126
  • 1
  • 19
  • 45
16
votes
3 answers

What do Haskell (data) constructors construct?

Haskell enables one to construct algebraic data types using type constructors and data constructors. For example, data Circle = Circle Float Float Float and we are told this data constructor (Circle on right) is a function that constructs a circle…
Ashley Aitken
  • 2,137
  • 2
  • 15
  • 23
16
votes
2 answers

Why do we use folds to encode datatypes as functions?

Or to be specific, why do we use foldr to encode lists and iteration to encode numbers? Sorry for the longwinded introduction, but I don't really know how to name the things I want to ask about so I'll need to give some exposition first. This draws…
16
votes
2 answers

Laziness/strictness between data and newtype

I'm struggling to understand why these two snippets produce different results under the so-called "poor man's strictness analysis". The first example uses data (assuming a correct Applicative instance): data Parser t a = Parser { getParser…
Matt Fenwick
  • 44,546
  • 19
  • 115
  • 184
16
votes
1 answer

How to add fields that only cache something to ADT?

Often I'm in the need of adding fields to an ADT that only memoize some redundant information. But I haven't figured out completely how to do it nicely and efficiently. The best way to show the problem is to make an example. Suppose we're working…
Petr
  • 60,177
  • 8
  • 136
  • 295
16
votes
2 answers

Why inductive datatypes forbid types like `data Bad a = C (Bad a -> a)` where the type recursion occurs in front of ->?

Agda manual on Inductive Data Types and Pattern Matching states: To ensure normalisation, inductive occurrences must appear in strictly positive positions. For instance, the following datatype is not allowed: data Bad : Set where bad : (Bad →…
Petr
  • 60,177
  • 8
  • 136
  • 295
1
2
3
29 30