简体   繁体   English

Haskell - 非法多态类型?

[英]Haskell - Illegal Polymorphic type?

Why does the single usage of this type compile, but putting it into a list fails? 为什么这种类型的单一用法编译,但将其放入列表失败?

ft1  :: (Foldable t, Num a) => t a -> a
ft1   =   (F.foldl (+)  0)

fTest :: [(Foldable t, Num a) => t a -> a ]
fTest = [ F.foldl (+)  0 ]

The latter gives the error: 后者给出错误:

folding.hs:80:10:
    Illegal polymorphic or qualified type:
      (Foldable t, Num a) => t a -> a
    Perhaps you intended to use ImpredicativeTypes
    In the type signature for `fTest':
      fTest :: [(Foldable t, Num a) => t a -> a]

Simliarly, trying to name it fails (differently): Simliarly,尝试命名它失败(不同):

type Ftst t a = (Foldable t, Num a) => t a -> a

folding.hs:80:1:
    Illegal polymorphic or qualified type:
      (Foldable t, Num a) => t a -> a
    Perhaps you intended to use RankNTypes or Rank2Types
    In the type declaration for `Ftst'

This restriction on Haskell's type system is in place to simplify type inference and checking. 对Haskell类型系统的这种限制是为了简化类型推断和检查。 Type inference with predicative rank-1 types (see below) is decidable and has a relatively simple implementation. 具有预测秩-1类型的类型推断(见下文)是可判定的并且具有相对简单的实现。 Type inference with rank-2 types is decidable but quite complicated, to the point that I don't know any language with an implementation of rank-2 type inference. 具有rank-2类型的类型推断是可判定的但非常复杂,以至于我不知道任何具有rank-2类型推断的实现的语言。 Type inference for types of rank 3 and above is flat-out undecidable. 3级及以上类型的类型推断是不可判定的。 Impredicative types also complicate things considerably; 令人印象深刻的类型也使事情变得复杂; GHC used to have an implementation which allowed type-checking (and some very limited inference) with impredicative types, but it was so complicated that it was ripped out later. GHC曾经有一个实现,它允许使用impredicative类型进行类型检查(以及一些非常有限的推断),但是它太复杂了以至于后来被删除了。 (Some values which only typecheck with impredicative types are still accepted by GHC at the moment, but I think this is not considered a "stable" feature.) (目前GHC仍然接受了一些仅具有预测类型的类型值,但我认为这不是一个“稳定”特征。)

Quick definitions: rank-1 types have all quantification and class constraints "outside" the type, so all rank-1 types are of the form 快速定义:rank-1类型具有“外部”类型的所有量化和类约束,因此所有rank-1类型都具有

forall a_1 ... a_m. (C_1, ..., C_n) => t

Rank-2 types allow function arguments to have rank-1 types; Rank-2类型允许函数参数具有rank-1类型; and in general rank n types allow function arguments to have rank (n-1) types. 一般来说,n个类型允许函数参数具有rank(n-1)类型。

Predicativity answers the question of what types can be substituted for type variables. Predicativity回答了什么类型可以替代类型变量的问题。 If only monomorphic types (though potentially with type variables!) can be substituted, you are in a predicative system; 如果只能替换单态类型(尽管可能带有类型变量!),那么您就处于一个预测系统中; impredicative types allow you to substitute a polymorphic type for a type variable. impredicative类型允许您将多态类型替换为类型变量。 By extension, parametric data types in predicative systems can only accept monomorphic types as arguments. 通过扩展,预测系统中的参数数据类型只能接受单态类型作为参数。 Thus, for example, your example which applies the [] type constructor to the type forall t a. (Foldable t, Num a) => ta -> a 因此,例如,您的示例将[]类型构造函数应用于类型forall t a. (Foldable t, Num a) => ta -> a forall t a. (Foldable t, Num a) => ta -> a is trying to apply a constructor to a polymorphic type, hence is only valid in an impredicative system. forall t a. (Foldable t, Num a) => ta -> a试图将构造函数应用于多态类型,因此仅在impredicative系统中有效。

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

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