Search code examples
scala

What is the difference between Abstract Data Types and Algebraic Data Types


Could someone please tell me, what is the difference between Abstract Data Types and Algebraic Data Types?


Solution

  • An algebraic data type is a type that consists of sums and products (different constructors and constructors with multiple fields). An abstract data type is a data type that has its implementation abstracted over by an API that is defined... Usually with some sort of encapsulation over its implementation.

    For example, a priority queue is an abstract data type. In memory it might be implemented as an array, or as allocated cells on the heap, or with a struct in C, or as a Shakespearean sonnet. However, you abstract over it with a well defined interface: push and pop. you can push items into the queue with priority, and you can pop the highest priority item out at any time. Another example is the associative map or dictionary, which can be implemented with hashing, binary search trees, or well trained sea otters. What matters is that you define lookup, insert, and delete operations that abstract over the underlying implementation.

    So they really talk about fundamentally different things. Some data structures can actually be seen as abstract data types implemented as algebraic data types! Like the commom linked list abstract data type in Prelude, which is implemented as the [] algebraic data type. the interface it offers is consing and unconsing, but it's implemented as multiple constructors with one having multiple fields -- sums and products.