1

I'm just starting to learn about functional programming and one of the things that I still don't get is the adjective "algebraic" in the expression algebraic data types.

Reading the first few sections of the Wikipedia article on the subject, I see that linked lists are one example of such ADT. Another example that is given are trees and, to be honest, I can't see much more algebra in them than I can see in a "vanilla" hierarchy of classes like toy examples like the familiar Animal class with, say, a subclass Cat and another one being Dog. I can, for instance, pattern match on all these types with, say, Scala.

So, what is the secret sauce that I am certainly missing here?

  • 1
    `ADT` stands for **Abstract** Data Type, not **Algebraic** Data Type. – Barmar Jul 31 '14 at 08:10
  • Hmm, it seems that I may be wrong. In FP, ADT does indeed stand for Algebraic. – Barmar Jul 31 '14 at 08:13
  • This question appears to be off-topic because it would be more appropriate for cs.stackexchange.com. – Barmar Jul 31 '14 at 08:16
  • @Barmar, I would definitely love to see non-technical non-technical explanations to this question. I feel that it is elementary for people who program in functional languages. – funcprogrammingnewbie Jul 31 '14 at 08:21
  • Basically, I think it means that new types are built up from simpler types using algebra similar to set theory. – Barmar Jul 31 '14 at 08:23
  • E.g. `NewType = OldType1 | OldType2`, which corresponds to the set `UNION` operator. – Barmar Jul 31 '14 at 08:25
  • So, any composite type would be algebraic? – funcprogrammingnewbie Jul 31 '14 at 08:26
  • _composite type_ usually refers to types that group elements, e.g. structures and arrays. Algebraic types allow for other types of composition, like the OR operator in my previous example. – Barmar Jul 31 '14 at 08:30
  • E.g. a linked list is either `NULL` (representing an empty list) **OR** a composite type containing an element (the _head_) and another linked list (the _rest_ of the list) – Barmar Jul 31 '14 at 08:32
  • And or would be expressed, say, via inheritance, like my example of the Animal Animal class? – funcprogrammingnewbie Jul 31 '14 at 08:33
  • It's bedtime here. I will come back after I wake up. – funcprogrammingnewbie Jul 31 '14 at 08:36
  • No, this is different from inheritance. In OOP, you start with a base class, and then refine and extend it with subclasses. In ADT, you start with primitive types, and combine them into more complex types. – Barmar Jul 31 '14 at 08:36
  • Please write an answer and I may select it once I digest the nuances. – funcprogrammingnewbie Jul 31 '14 at 08:39
  • I'd prefer it if someone with real experience in FP would write a more precise answer. – Barmar Jul 31 '14 at 08:41
  • In short, it's because you can define an algebra *over types* instead of mere numbers - hence the terms "sum type" and "product type", those really are addition and multiplication of types (and functions are exponentiation). Check out [the second half of the first answer here](http://stackoverflow.com/questions/9190352/abusing-the-algebra-of-algebraic-data-types-why-does-this-work) and [this blog post](http://www.haskellforall.com/2012/01/haskell-for-mainstream-programmers.html). – molbdnilo Jul 31 '14 at 14:16
  • possible duplicate of [Missing definition of 'algebraic data type' in the book "Learn You a Haskell for Great Good"](http://stackoverflow.com/questions/22114230/missing-definition-of-algebraic-data-type-in-the-book-learn-you-a-haskell-for) – Aadit M Shah Aug 26 '15 at 20:57
  • possible duplicate of [Why "Algebraic data type" use "Algebraic" in the name?](http://stackoverflow.com/questions/25061345/why-algebraic-data-type-use-algebraic-in-the-name) – Mirzhan Irkegulov Aug 26 '15 at 22:30
  • read everything in http://stackoverflow.com/q/9190352/2007884 along with possible further links in the description - including the question itself. That should answer your question. – kram1032 Sep 18 '15 at 13:28

1 Answers1

0

I have answered this question before[1]. Nevertheless, I'll briefly reiterate why we describe data types as being "algebraic" in this answer.

First, let's understand what the word "algebra" means. The word "algebra" is derived from the Arabic phrase "al-jabr" which means "reunion of broken parts"[2]. The theme of breaking down problems into simpler problems (decomposition) and then reintegrating the solutions to these problems into the final solution (recomposition) is central to several fields including programming[3].

The phrase "al-jabr" was a part of the title of a famous paper on equations[4] written by the Persian mathematician Muhammad al-Khwarizmi, better known as Algoritmi in Latin. The word "algorithm" was derived from his name. The title of the paper was "Kitab al-Jabr w'al-Muqabala" which translates to "Rules of Reintegration and Reduction". It was this paper that introduced Arabic numerals to the West.

Algebra is all about reintegration (i.e. combining parts of an object into the whole). Hence, algebraic data types are about combining simpler data types into more complex data types. You can think of a data type as a set. For example, the data type Int is the set of all integers. We can combine simpler types into more complex types using the cartesian product operation and the disjoint union operation.

The cartesian product operation produces product types[5]. For example, if we have two data types, Int and Char, then the cartesian product Int × Char is the set of all the possible pairs of integers and characters. A value of the type Int × Char is the pair (0, 'a').

The disjoint union operation produces sum types[6]. For example, if we have two data types, Int and Char, then the disjoint union Int + Char is the set of all integers tagged with Int or characters tagged with Char. Values of the type Int + Char are the pairs (0, Int) and ('a', Char).

Algebraic data types allow you to define data types algebraically. For example, the following Shape data type is a disjoint union of the Circle and Rectangle types which are both cartesian products of the Double type:

data Shape = Circle    { x  :: Double, y  :: Double, r  :: Double }
           | Rectangle { x1 :: Double, y1 :: Double, x2 :: Double, y2 :: Double }

This is the reason why they are called "algebraic" data types.

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299