简体   繁体   English

将小数类型设置为num

[英]Setting type of fractional to num

Could someone please explain why this compiles 有人可以解释一下为什么编译

Prelude> 1 :: Num a => a

and this doesn't 而且这不

Prelude> 1.0 :: Num a => a

Second example would work with Fractional , but Num is superclass of Fractional . 第二个例子适用于Fractional ,但NumFractional Num的超类。 Just like it's superclass of Integral . 就像它是Integral的超类一样。

If we have 如果我们有

x :: Num a => a

the user of x can pick a as wanted. x的用户可以根据需要选择a Eg 例如

x :: Int

What would this evaluate to if x = 1.5 ? 如果x = 1.5这将如何评估?

For this reason a floating point literal can't be given the polytype Num a => a , since its value won't (in general) fit all Num eric types. 因此,不能为浮点文字提供多型Num a => a ,因为它的值(通常)不适合所有Num eric类型。

Integral literals instead fit every numeric type, so they are allowed. 相反,整数文字适合所有数字类型,因此允许使用它们。

The superclass relationship between type classes does not establish relationships between types. 型类之间的关系,超建立类型之间的关系。 It tells us only that if type t is a member of type class C , and B is a super class of C , then t will also be member of B . 它仅告诉我们,如果类型t是类型C的成员,而类型BC的超类,则t也将是B的成员。

It doesn't say that each value v::t can be used at any type that is also member of B . 并不是说每个值v :: t都可以用于也是B成员的任何类型。 But this is what you are stating with: 但这是您要说明的内容:

3.14159 :: Num a => a

It's important to distinguish between polymorphism in OO languages and polymorphism in Haskell. 区分OO语言中的多态性和Haskell中的多态性很重要。 OO polymorphism is covariant, while Haskell's parametric polymorphism is co ntra variant . OO多态性是协变的,而Haskell的参数多态是CO NTRA变种

What this means is: in an OO language, if you have 这意味着:用OO语言,如果您有

class A {...}
class B: A {...}

ie A is a superclass of B , then any value of type B is also a value of type A . AB的超类,则类型B任何值也是类型A的值。 (Note that any particular value is actually not polymorphic but has a concrete type!) Thus, if you had (请注意,任何特定实际上不是多态的,而是具体的类型!)因此,如果您有

class Num {...}
class Fractional: Num {...}

then a Fractional value could indeed be used as a Num value. 那么Fractional值的确可以用作Num值。 That's roughly what covariant means: any subclass value is also a superclass value ; 这就是协变量的大致含义:任何子类值也是超类值 the values hierarchy goes the same direction as the type hierarchy. 值层次结构与类型层次结构的方向相同。

In Haskell, class es are different. 在Haskell中, class es是不同的。 There is no such thing as a “value of type Num ”, only values of concrete types a . 没有“ Num类型的值”之类的东西,只有具体类型a值。 That type may be in the Num class. 类型可能在Num类中。

Unlike in OO languages, a value like 1 :: Num a => a is polymorphic: it can take on whatever type the environment demands, provided the type is in the Num class. 与OO语言不同,像1 :: Num a => a这样的值多态的:只要该类型在Num类中,它就可以采用环境所需的任何类型。 (Actually that syntax is just shorthand for 1 :: ∀ a . Num a => a , to be read as “for all types a , you can have a value 1 of type a .) For example, (实际上,语法只是1 :: ∀ a . Num a => a简写1 :: ∀ a . Num a => a读作“对于所有类型a ,您可以将值1a类型。)例如,

Prelude> let x = 1 :: Num a => a
Prelude> x :: Int
1
Prelude> x :: Double
1.0

You can also give x a more specific constraint of Fractional , since that's a subclass of Num . 您还可以给x一个更具体的Fractional约束,因为那是Num的子类。 That just restricts what type the polymorphic value can be instantiated to: 那只是限制了可以将多态值实例化为哪种类型:

Prelude> let x = 1 :: Fractional a => a
Prelude> x :: Int

<interactive>:6:1:
    No instance for (Fractional Int) arising from a use of ‘x’
    ...
Prelude> x :: Double
1.0

because Int is not a fractional type. 因为Int不是小数类型。

Thus, Haskell's polymorphism is contravariant: polymorphic values restricted to a superclass can also be restricted to a subclass instead, but not the other way around. 因此,Haskell的多态性是对立的:限制为超类的多态值也可以限制为子类,但反之则不行。 In particular, you can obviously have 特别是,您显然可以拥有

Prelude> let y = 1.0 :: Fractional a => a

( y is the same as x' ), but you can not generalise this to y' = 1.0 :: Num a => a . yx'相同),但是您不能将其概括为y' = 1.0 :: Num a => a Which is a good thing as Ingo remarked since otherwise it would be possible to do 正如Ingo所说,这是一件好事,因为否则有可能做

Prelude> 3.14159 :: Int
  ????

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM