In Haskell language it seems to tell me another explanation what is a class. The class concept in Haskell
is alike the template in C++ language. Here is a example.
class MFunctor f where
fmap :: (a->b) -> f a -> f b
An instance of MFunctor for type Tree would be:
instance MFunctor Tree where
fmap f (Leaf x) = Leaf (f x)
fmap f (Branch t1 t2) = Branch (fmap f t1) (fmap f t2)
This instance declaration declares that Tree, rather than Tree a, is an instance of Functor.
This capability is quite useful, and here demonstrates the ability to describ e generic "container" types,
allowing functions such as fmap to work uniformly over arbitrary trees, lists, and other data types.
[Type applications are written in the same manner as function applications. The type T a b is
parsed as (T a) b. Types such as tuples which use special syntax can be written in an alternative
style which allows currying. For functions, (->) is a type constructor; the types f -> g and (->) f g
are the same. Similarly, the types [a] and [] a are the same. For tuples, the type constructors
(as well as the data constructors) are (,), (,,), and so on.]
Yes, it is also alike the macro in lisp. They are set family, it is also a kind of set of sets.
So,how do es Haskell detect malformed type expressions? The answer is a second type system which
ensures the correctness of types! Each type has an associated kind which ensures that the type is
used correctly.
Type expressions are classified into different kinds which take one of two possible forms:
1. The symbol * represents the kind of type associated with concrete data objects. That is, if
the value v has type t, the kind of v must be *.
2. If k1 and k2 are kinds, then k1 -> k2 is the kind of types that take a type of kind k1 and
return a type of kind k2.
Kinds do not appear directly in Haskell programs. The compiler infers kinds b efore doing type
checking without any need for 'kind declarations'. Kinds stay in the background of a Haskell
program except when an erroneous type signature leads to a kind error. Kinds are simple enough
that compilers should be able to provide descriptive error messages when kind con
flicts occur.
We have shown how parametric polymorphism is useful in defining families of types by universally
quantifying overall types. Sometimes, however, that universal quantification is too broad -- we
wish to quantify over some smaller set of types, such as those types whose elements can be compared
for equality. Type classes can be seen as providinga structured way to do just this. Indeed, we
can think of parametric polymorphism as a kind of overloading too! It's just that the overloading
occurs implicitly over all types instead of a constrained set of types (i.e. a type class).
The classes used by Haskell are similar to those used in other object-oriented languages such as
C++ and Java. However,there are somesignificant differences:
1. Haskell separates the definition of a type from the definition of the methods associated with
that type. A class in C++ or Java usually defines both a data structure (the member variables) and
the functions associated with the structure (the methods). In Haskell, these definitions are separated.
So, I said that classes in Haskell is more alike template in C++ language. Types are set of data.
For example, integer represents 1, 2, 3 ... We usually defined some operations on that, such as +, -,
*, /. So, we can considered a set as a element in a bigger set, set family. In same way, we also can
define operations on that. Those operations and set is called as algebra system in mathematics. What
is a instance of a class in Haskell? It is so easy to answer, a element in a set family. As matter of
a fact, it is just common abstract thing from types.
2. The class methods defined by a Haskell class correspond to virtual functions in a C++ class.
Each instance of a class provides its own definition for each method; class defaults correspond to default
definitions for a virtual function in the base class.
3. Haskell classes are roughly similar to a Java interface. Like an interface declaration, a Haskell
class declaration defines a protocol for using an object rather than defining an object itself.
4. Haskell does not supportthe C++ overloading style in which functions with different types share a
common name.
5. The type of a Haskell object cannot be implicitly coerced; there is no universal base class such
as "Object" which values can be projected into or out of.
6. C++ and Java attach identifying information (such as a VTable) to the runtime representation of
an object. In Haskell,such information is attached logically instead of physically to values, through
the type system.
7. There is no access control(such as public or private class constituents) built into the Haskell
class system. Instead, the module system must be used to hide or reveal components of a class.