January 16 2007  火曜日

本文探讨了 Haskell 中的类概念及其与 C++ 模板的相似之处,并通过实例展示了如何定义泛型容器类型,使函数如 fmap 能够统一地应用于各种数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值