哈斯克尔的类型系统比较用C#,寻找类似物 [英] Comparing type system of Haskell with C#, looking for analogues

查看:106
本文介绍了哈斯克尔的类型系统比较用C#,寻找类似物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的一个Haskell编程。我试图处理其类,数据,实例和NEWTYPE。以下是我已经明白了:

I'm pretty new a Haskell programming. I'm trying to deal with its classes, data, instances and newtype. Here is what I've understood:

data NewData = Constr1 Int Int | Constr2 String Float



大约是一样的(Java或C#):

is about the same as (Java or C#):

class NewData {
  private int a, b;
  private string c;
  private float d;

  /* get'ers and set'ers for a, b, c and d
  ................
  */

  private NewData() { }

  private NewData(int a, int b) { 
    this.a = a;
    this.b = b;
  }

  private NewData(string c, float d) {
    this.c = c;
    this.d = d;
  }

  public static Constr1(int a, int b) {
    return new NewData(a, b);
  }

  public static Constr2(string c, float d) {
    return new NewData(c, d);
  }

}

class SomeClass a where
  method1 :: [a] -> Bool

interface SomeInterface<T> {
  public bool method1(List<T> someParam);
 }

// or

abstract class SomeClass<T> {
  public abstract bool method1(List<T> someParam);
}

instance SomeClass Int where
  method1 a = 5 == head a -- this doesn't have any meaning, though, but this is correct

   class SomeClassInstance<Int>: SomeClass {
     public bool method1(List<Int> param) {
       return param.first == 5; // I don't remember the method's name exactly, it doesn't matter
     }
   }

是所有这些是否正确?
约NEWTYPE什么,我怎么能代表它在C#或Java?

Are all these correct? What about newtype, how can I represent it in C# or Java?

推荐答案

正如其他人所说,它更像识别联合 - 这是一个不起眼的构造,唯一的C / C ++程序员很可能已经听说过

As others have said, it's more like a discriminated union - which is an obscure construct that only C / C++ programmers are likely to have heard of.

可以的样的的模拟这一个面向对象的语言通过拥有Haskell的类型的抽象基类,以及每个Haskell的构造的具体子类。特别是,你的代码片段说的每个 NewData 对象有四个字段;这是不正确。你可以做这样的事情:

You can kind of simulate this in an OO language by having an abstract base class for Haskell's "type", with a concrete subclass for each of Haskell's "constructor". In particular, your code fragment says that every NewData object has four fields; this is incorrect. You can do something like this:

data Stuff = Small Int | Big String Double Bool

现在如果我写小5 ,这是一个只有1里面领域的的东西值。 (它占用的内存量。)但是,如果我做大富7.3真,这是的类型<值code>的东西,但它包含3个字段(以及占用那么多内存)。

Now if I write Small 5, this is a Stuff value with only 1 field inside it. (It takes up that amount of RAM.) But if I do Big "Foo" 7.3 True, this is also a value of type Stuff, but it contains 3 fields (and takes up that much RAM).

注意,构造的名称的本身是数据的一部分。这就是为什么你可以这样做。

Notice that the constructor name itself is part of the data. That's why you can do something like

data Colour = Red | Green | Blue

现在有三个构造函数,每个零域。构造函数的本身的是数据。现在,C#让你做

Now there are three constructors, each with zero fields. The constructor itself is the data. Now, C# lets you do

enum Colour {Red, Green, Blue}

但是,这真的只是说

Colour = int;
const int Red = 0;
const int Green = 1;
const int Blue = 2;

请注意,特别是你可能会说

Note, in particular, you may say

Colour temp = 52;



相比之下,在Haskell类型的变量颜色可以的只有的包含红色绿色蓝色,而这些都是不以任何方式的整数。您可以定义的 的将其转换为整数,如果你喜欢的功能,但是这不是如何编译器将它们存储。

By contrast, in Haskell a variable of type Colour can only contain Red, Green or Blue, and these are not in any way integers. You can define a function to convert them to integers if you like, but that's not how the compiler stores them.

您关于干将注释, setter方法说明了这种方法的缺陷;在Haskell,我们通常不会担心getter和setter。简单地定义一个类型是足以产生该类型的值,并访问其内容。这有点像隐约一个C#结构与各界标记公共只读。 (当我们不担心干将,我们通常称之为投影函数...)

Your comment about getters and setters illustrates the pitfalls of this approach; in Haskell, we don't usually worry about getters and setters. Simply defining a type is sufficient to create values of that type and to access their contents. It's sort of vaguely like a C# struct with all fields marked public readonly. (When we do worry about getters, we usually call them "projection functions"...)

在面向对象的,你可以使用类封装。在Haskell中,你的模块做的。内部模块,任何事物都有获得的一切(很像一个类可以访问本身各部分)。您可以使用输出列表说什么模块的部分公众对外面的世界。特别是,你可以做一个类型的名称的公开,而完全隐藏其内部结构。那么唯一的方法来创建或操作类型的值是你从模块暴露功能。

In OO, you use classes for encapsulation. In Haskell, you do this with modules. Inside a module, everything has access to everything (much like a class can access every part of itself). You use an export list to say what parts of the module are public to the outside world. In particular, you can make a type name public, while completely hiding its internal structure. Then the only way to create or manipulate values of that type are the functions you expose from the module.

您问 NEWTYPE

确定, NEWTYPE 关键字定义新型的名字这实际上等同于一个古老的类型,但类型检查认为这是一些新的和不同的。例如,内部仅仅是一个正常的数字。但是,如果我这样做

OK, the newtype keyword defines a new type name which is actually identical to an old type, but the type checker thinks it's something new and different. For example, an Int is just a normal number. But if I do

newtype UserID = ID Int

现在用户名是一个全新的类型,完全无关的东西。但在幕后,它是真正的另一个名字好老内部。这意味着,你不能使用用户名其中的内部是必需的 - 你不能使用内部其中<需要code>用户名。所以你不能混了,只是因为他们俩都是整数,其他一些随机数的用户ID。

now UserID is a brand new type, completely unrelated to anything. But under the covers, it's really just another name for good old Int. What this means is that you can't use UserID where an Int is required - and you can't use Int where a UserID is required. So you can't mix up a user ID with some other random number just because they're both integers.

您可以做同样的事情与数据

You can do exactly the same thing with data:

data UserID = ID Int

不过,现在我们有一个无用的用户名只包含一个指向整数结构。如果我们使用 NEWTYPE 用户名的整数,而不是一个结构指向一个整数。但从程序员的角度来看,这两个定义是equivilent;但引擎盖下, NEWTYPE 更有效

However, now we have a useless UserID structure that just contains a pointer to an integer. If we use newtype then a UserID is an integer, not a structure pointing to an integer. From the programmer's point of view, both definitions are equivilent; but under the hood, newtype is more efficient.

(次要吹毛求疵:其实做那么的相同的你需要说

(Minor nit-pick: Actually to make then identical you need to say

data UserID = ID !Int

这意味着该整数字段是严,不要担心这个呢。)

which means that the integer field is "strict". Don't worry about this yet.)

这篇关于哈斯克尔的类型系统比较用C#,寻找类似物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆