As I understand the Type Class is that it's not something concrete but just a construct for ad-hoc and parametric polymorphism. Eq and Semigroup are examples of type classes. On the other hand, there is Algebraic data type, which is a concrete composite type, for example, Either and Maybe. And they are also Functors.
So, there is a specification for Algebraic data types for JavaScript: https://github.com/fantasyland/fantasy-land. On this page, Setoid(Eq), Ord and Semigroup are also ADT. But is it correct? If so, they are composite of what types?
I also find out this article regarding type classes, and here Functors and Monads are type classes. https://typelevel.org/cats/typeclasses.html#type-classes-in-cats. And, is it means that Either and Maybe are type classes as well?
Is Eq a type class or algebraic data type? Or both? Same for Functor
We collect data (values, terms) into types (data types). We can collect types into type classes.
1
,"a"
,true
,Some(1)
,None
, ... are values. They belong to types,Int
,String
,Boolean
,Option[Int]
, ... Types can belong to type classes,Eq
,Ord
,Functor
, ...ADT (algebraic data type) is a data type constructed via sum and product
Here
MyTrait
is a sum ofMyClass1
andMyClass2
.MyClass1
is a product ofInt
andString
.MyClass2
is a product of single multiplierBoolean
.There are also data types that are not ADT. E.g. function types
A => B
,union types, intersection typesA | B
A & B
orA with B
, existential types, etc. Scala ADT are automatically generalized ADT (GADT) in Haskell terminology. There are generalizations of ADT in dependently typed languages, Sigma- and Pi-types (i.e. dependent sum and product).Type classes is FP way to describe a behavior (via ad hoc polymorphism, early binding, static/compile-time dispatch). We can make a type an instance of a type class
Here
MyClass1
,MyClass2
,MyTrait
(in Haskell onlyMyTrait
) are types, they are instances of the type classMyTypeclass
.An alternative to typeclasses (another way to describe a behavior) is OOP inheritance (via subtyping polymorphism, late binding, dynamic/runtime dispatch)
We can lift an ADT into a type class. For example standard ADT
can become a type class and its instances
Normally,
Eq
,Ord
,Semigroup
are not ADT. They are not constructed via sum and product. They are type classes. They describe behavior, namely how to compare elements for equality, how to compare them for order, how to add elements, what is unit element. They "consists" of all types declared as their instances, e.g.Int
,String
etc. (i.e. where the corresponding behavior is implemented in some specific way).Normally,
Either
andMaybe
are not type classes, they are ADT. But if we really want we can lift ADT into a type class as I showed above.Eq
andFunctor
are type classes, not ADT.