1

I'm reading Learn You a Haskell and it seems that the author did not explicitly define what the term 'algebraic data type' is referring to. This term first appears on page 126 in the PDF version of the book and no explicit definition precedes its first appearance.

Could someone please fill in this missing definition in the spirit of the book?

In other words, could someone give a simple definition which is understandable based on the information contained in the first 126 pages of the book?

My suspicion is that an algebraic data type is anything defined by the keyword 'data' but I am not 100% certain.

I understand that there are lot of explanations of algebraic data types on the net but I just would like to know what algebraic data types mean in the context of this book. Thanks for reading.

EDIT : This is not a question about the name "algebraic". This is a question about the missing definition of the term ADT. In other words, the book does not clearly explain if ADT is the same as 'data'. Which is. But the book confusingly does not make this connection. This is what this question is about and not about the word 'algebraic'. The name could be also 'zebraic data types' the question would be the same.

jhegedus
  • 20,244
  • 16
  • 99
  • 167
  • 1
    Your suspicion *in the context of the book* is right on target, however, I do recommend to read other definitions, to get the bigger picture. – n. m. could be an AI Mar 01 '14 at 12:32
  • Thanks for the confirmation. I just needed a simple definition so that I can understand the sentences in the book in which this term appears. – jhegedus Mar 01 '14 at 12:48
  • "ADT" often refers to "abstract data type" which is not the same concept. This could lead to some confusion as well. – David Young Mar 01 '14 at 19:46
  • 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:35

1 Answers1

9

Algebraic data types are so called because they form an algebra of types. This algebra consist of two primitive operations:

  1. Product
  2. Sum

Here the product and sum operations are isomorphic to the logical conjunction (logical AND) and logical disjunction (logical OR) operations from boolean algebra.


A product of types is a logical conjunction of types. For example consider:

data Person = Person { name   :: String
                     , age    :: Int
                     , gender :: Char
                     }

Here the data constructor Person is a conjunction of the types String, Int and Char. It means that it is composed of a String and an Int and a Char. Hence it's a product of those types:

data Person = Person (String * Int * Char)

Such a data type is called a product type. Product types are available in almost every language. For example in C a product type is called a struct.


A sum of types is a logical disjunction of types. For example consider:

data Race = Dwarf | Elf | Hobbit | Human

Here the data type Race is a disjunction of the data constructors Dwarf, Elf, Hobbit and Human. It means that it may be either a Dwarf or an Elf or a Hobbit or a Human. Hence it's a sum of those types:

data Race = Dwarf + Elf + Hobbit + Human

Such a data type is called a sum type. Sum types are available in almost every language. For example in C a sum type is called a union. In Java it's called an enum.


Algebraic data types are powerful because they allow you to create product and sum types easily. For example consider:

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

Here the data type Shape is a sum of the data constructors Circle and Rectangle which in turn are products of the types Double ^ 3 and Double ^ 4 respectively.

Creating such "algebraic" data types in Haskell and other functional programming languages is laconic. However creating such data types in languages like C and Java is clumsy and verbose.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • Thanks for the detailed explanation. It's great! Sidenote: I don't really see the one-to-one correspondance between java enums and Haskell's 'sum type'. More specifically, let's consider the Shape type from http://learnyouahaskell.com/making-our-own-types-and-typeclasses#algebraic-data-types , I don't see immediately how a corresponding Java enum could be defined in such a way that different values of the enum have different conrete types (Circle, Rectangle). I have to admit I never really wrote very complicated enums in Java, so my knowledge in this area is slightly limited. – jhegedus Mar 01 '14 at 13:14
  • 2
    A Java `enum` is a weaker form of a sum type (one in which each term is a prime factor). For example the `Race` data type can be modeled as a Java enum. However the `Shape` data type cannot. Nevertheless an `enum` is still considered a sum type. It's not a sum of products, but it's still a sum. – Aadit M Shah Mar 01 '14 at 13:19