信保是什么| 半衰期是什么意思| 孢子是什么东西| 陆家嘴为什么叫陆家嘴| 治字五行属什么| 重阳节是什么意思| 眼睛飞蚊症吃什么药| 尖货是什么意思| 大姨妈一直不干净是什么原因| 走马灯什么意思| 10点半是什么时辰| 福寿螺为什么不能吃| 六月初六是什么日子| 哈欠是什么意思| 外伤挂什么科| 出什么什么什么| 肾阴虚吃什么食物补| 暗戳戳是什么意思| 红枣泡水喝有什么功效| 脂溢性皮炎用什么药| 开小差是什么意思| 龙马精神代表什么生肖| 什么原因引起血糖高| 朱日和是什么意思| 贺喜是什么意思| 血糖突然升高是什么原因| 凝血功能障碍是什么病| dob值阳性是什么意思| 西洋参有什么作用| 一代宗师是什么意思| 肝是什么意思| 上面一个四下面一个正念什么| 什么是营养| 静静地什么| 空调不制冷是什么原因| 眼皮突然肿了是什么原因| 六月二七是什么星座| 红枸杞有什么功效| 亡羊补牢的亡是什么意思| 什么命要承受丧子之痛| 世界上最大的蛇是什么蛇| 海虾不能和什么一起吃| 立秋当天吃什么| 拉姆藏语什么意思| 学渣什么意思| 带状疱疹可以吃什么水果| 甲苯对人体有什么危害| 舒克是什么职业| 梦见发面是什么意思| 头发油性大是什么原因| 隔岸观火是什么意思| 11月12日什么星座| 小葱拌豆腐的下一句是什么| 胰腺炎有什么症状| 药店加盟需要什么条件| 1969属什么| 丝状疣是什么| 手心脚心发热是什么原因引起的| 静脉曲张有什么危害吗| 3朵玫瑰代表什么意思| 天津是什么省| 年轻人手抖是什么原因| 来月经期间吃什么最好| bc什么意思| 胃胀呕吐是什么原因| 手脚发麻是什么原因| 中伏是什么意思| 角是什么结构| 什么的蹲着| 女人的胸部长什么样| 为什么会得霉菌感染| 早孕什么意思| 灰指甲是什么样子的| psv是什么| 煊字五行属什么| 星期一左眼皮跳是什么预兆| 自律性是什么意思| pu是什么元素| 第二职业干点什么好呢| 夜里2点到3点醒什么原因| 梦见孩子被蛇咬是什么意思| 颜面扫地什么意思| 牙齿里面疼是什么原因| 烊化兑服是什么意思| 膝跳反射属于什么反射| 5月13日什么星座| 羊下面是什么生肖| 尾货是什么意思| 壁虎怕什么| 猪古代叫什么| 双侧筛窦粘膜增厚是什么意思| 实蛋是什么| 睡觉流鼻血是什么原因| 定妆用什么好| 什么是老年斑图片| hankook是什么轮胎| 今天属什么生肖日历| 卡宾男装属于什么档次| 过敏性紫癜是什么症状| cindy什么意思| 河南为什么简称豫| 鱼油什么人不能吃| 卧推60公斤什么水平| lg是什么| 梦到吵架是什么意思| 战五渣是什么意思| 什么叫智商| 改名字需要什么手续| 霉菌性阴道炎吃什么药| Joyce什么意思| 什么叫柞蚕丝| 莆田系是什么意思啊| 最高检检察长什么级别| 开什么店好赚钱| img是什么意思| 血糖偏高能吃什么水果| 晚饭吃什么减肥| 白细胞是什么| 什么是不动产权证| 树莓色是什么颜色| hbv是什么意思| 元帅相当于现在什么官| 喝椰子汁有什么好处| 朝什么暮什么| 小灶是什么意思| 来月经喝红糖水有什么好处| 推手是什么意思| 豆浆不能和什么一起吃| 爱情的故事分分合合是什么歌| 1月14日什么星座| 肛周湿疹用什么药| 杏干泡水喝有什么功效| 舌头破了是什么原因| bottle什么意思| 2003年的羊是什么命| 甲状腺和甲亢有什么区别| 爱到极致是什么| 痰多吃什么好| 大象吃什么| 什么身子| 腱鞘炎是什么病| 压力过大会有什么症状| 三和大神什么意思| tv是什么意思| 检查尿酸挂什么科| 四个日念什么| 水银中毒会出现什么状况| 周杰伦什么星座| 天蝎女喜欢什么样的男生| 耳垂后面疼是什么原因| 诸葛亮为什么气死周瑜| 天生丽质什么意思| 超生是什么意思| 月经咖啡色是什么原因| 预防水痘吃什么药| 什么是荨麻疹| 甲状腺弥漫性改变是什么意思| 一什么冰雹| 黄豆可以和什么一起打豆浆| 属兔的跟什么属相最配| 拆线挂什么科| 本命佛是什么意思| 抬头头晕是什么原因| 副歌部分是什么意思| 什么是童话| 世界上最难写的字是什么| 主动脉夹层a型是什么病| 喝蛋白粉有什么副作用| 丝瓜不能和什么食物一起吃| 二代身份证是什么意思| 剖腹产可以吃什么水果| 醋泡黑豆有什么功效| 917是什么意思| 8月24日是什么星座| 吃的多拉的少是什么原因| 耳根疼是什么原因| 七岁属什么生肖| 一根长寿眉预示什么| 斗牛为什么用红色的布| 七月十七是什么星座| 胰岛a细胞分泌什么激素| 人大代表是什么| 为什么老是头晕| 输卵管堵塞吃什么药能打通| 随喜功德是什么意思| 为什么耳朵后面会臭| 木加一笔变成什么字| 心肌酶是查什么的| 男士检查精子挂什么科| 嘴唇溃疡是什么原因| 道谢是什么意思| aoerbo是什么牌子的手表| 爱因斯坦是什么星座| 纪检是干什么的| 颠茄片是什么药| 黄芪煲汤和什么搭配| 便秘吃什么药没有依赖性| 肛瘘是什么原因引起的| 什么叫杵状指| 霉菌是什么引起的| 肝阳性是什么意思| 为什么受伤总是我| 88年出生属什么生肖| 梦见粽子是什么预兆| 花非花雾非雾什么意思| stomach什么意思| 腈纶是什么面料| 打胎药叫什么名字| 桑叶有什么作用| 左手中指麻木是什么原因| 白扁豆长什么样| 海棠花什么时候开| 风热感冒 吃什么| 快走对身体有什么好处| 气血不足喝什么| 附件炎是什么引起的| 吃什么排黑色素最强| 生气对身体有什么危害| 一个月的小猫吃什么| 食指长痣代表什么| 结婚28年是什么婚| 同房出血什么原因| 来来来喝完这杯还有三杯是什么歌| 燃面为什么叫燃面| 米糠是什么东西| 韩国买什么东西划算| 冰心的原名是什么| 钾是什么东西| 当归配什么不上火| 属相兔和什么属相最佳| 五险一金什么时候开始交| 咽喉肿痛吃什么药| 为什么头会一阵一阵的痛| 胃囊肿是什么病严重吗| 占有欲强是什么意思| 忌行丧是什么意思| m2是什么意思| 梦见自己怀孕大肚子是什么预兆| 三分钟热度是什么意思| 七宗罪都是什么| 胃肠炎可以吃什么水果| 霾是什么意思| 老子和孔子是什么关系| 星芒是什么意思| 阴虚什么意思| 甲亢属于什么科室| tag是什么意思| 球镜度数是什么意思| 直肠壁增厚一般是什么情况| 颈椎病挂号挂什么科| 连可以组什么词| 透亮是什么意思| 震颤是什么症状| 老花镜什么品牌好| 输血四项检查是什么| 度是什么意思| 什么是黑色素肿瘤| 宫内早孕什么意思| 手上长小水泡是什么原因| 薄凉是什么意思| 啤酒和什么不能一起吃| 天天喝牛奶有什么好处| 右眼跳是什么意思| 普外科是看什么病的| 百度Jump to content

每日福利送不停 性感翘臀撩人的黑丝大姐姐好诱惑!

From Wikipedia, the free encyclopedia
百度 事实上,1978年的中美建交公报中,不论是双方共识还是单方声明都没有任何直接或者间接地做出中国只能和平解决台湾问题的承诺,美国也没有做出和平解决台湾问题是中美建交前提的声明。

In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and a type variable a, and means that a can only be instantiated to a type whose members support the overloaded operations associated with T.

Type classes were first implemented in the Haskell programming language after first being proposed by Philip Wadler and Stephen Blott as an extension to "eqtypes" in Standard ML,[1][2] and were originally conceived as a way of implementing overloaded arithmetic and equality operators in a principled fashion.[3][2] In contrast with the "eqtypes" of Standard ML, overloading the equality operator through the use of type classes in Haskell does not need extensive modification of the compiler frontend or the underlying type system.[4]

Overview

[edit]

Type classes are defined by specifying a set of function or constant names, together with their respective types, that must exist for every type that belongs to the class. In Haskell, types can be parameterized; a type class Eq intended to contain types that admit equality would be declared in the following way:

class Eq a where
  (==) :: a -> a -> Bool
  (/=) :: a -> a -> Bool

where a is one instance of the type class Eq, and a defines the function signatures for 2 functions (the equality and inequality functions), which each take 2 arguments of type a and return a Boolean.

The type variable a has kind ( is also known as Type in the latest Glasgow Haskell Compiler (GHC) release),[5] meaning that the kind of Eq is

Eq :: Type -> Constraint

The declaration may be read as stating a "type a belongs to type class Eq if there are functions named (==), and (/=), of the appropriate types, defined on it". A programmer could then define a function elem (which determines if an element is in a list) in the following way:

elem :: Eq a => a -> [a] -> Bool
elem y []     = False
elem y (x:xs) = (x == y) || elem y xs

The function elem has the type a -> [a] -> Bool with the context Eq a, which constrains the types which a can range over to those a which belong to the Eq type class. (Haskell => can be called a 'class constraint'.)

Any type t can be made a member of a given type class C by using an instance declaration that defines implementations of all of C's methods for the particular type t. For example, if a new data type t is defined, this new type can be made an instance of Eq by providing an equality function over values of type t in any way that is useful. Once this is done, the function elem can be used on [t], that is, lists of elements of type t.

Type classes are different from classes in object-oriented programming languages. In particular, Eq is not a type: there is no such thing as a value of type Eq.

Type classes are closely related to parametric polymorphism. For example, the type of elem as specified above would be the parametrically polymorphic type a -> [a] -> Bool were it not for the type class constraint "Eq a =>".

Higher-kinded polymorphism

[edit]

A type class need not take a type variable of kind Type but can take one of any kind. These type classes with higher kinds are sometimes called constructor classes (the constructors referred to are type constructors such as Maybe, rather than data constructors such as Just). An example is the Monad class:

class Monad m where
  return :: a -> m a
  (>>=)  :: m a -> (a -> m b) -> m b

That m is applied to a type variable indicates that it has kind Type -> Type, i.e., it takes a type and returns a type, the kind of Monad is thus:

Monad :: (Type -> Type) -> Constraint

Multi-parameter type classes

[edit]

Type classes permit multiple type parameters, and so type classes can be seen as relations on types.[6] For example, in the GHC standard library, the class IArray expresses a general immutable array interface. In this class, the type class constraint IArray a e means that a is an array type that contains elements of type e. (This restriction on polymorphism is used to implement unboxed array types, for example.)

Like multimethods,[citation needed] multi-parameter type classes support calling different implementations of a method depending on the types of multiple arguments, and indeed return types. Multi-parameter type classes do not require searching for the method to call on every call at runtime;[7]:?minute 25:12? rather the method to call is first compiled and stored in the dictionary of the type class instance, just as with single-parameter type classes.

Haskell code that uses multi-parameter type classes is not portable, as this feature is not part of the Haskell 98 standard. The popular Haskell implementations, GHC and Hugs, support multi-parameter type classes.

Functional dependencies

[edit]

In Haskell, type classes have been refined to allow the programmer to declare functional dependencies between type parameters—a concept inspired from relational database theory.[8][9] That is, the programmer can assert that a given assignment of some subset of the type parameters uniquely determines the remaining type parameters. For example, a general monad m which carries a state parameter of type s satisfies the type class constraint Monad.State s m. In this constraint, there is a functional dependency m -> s. This means that for a given monad m of type class Monad.State, the state type accessible from m is uniquely determined. This aids the compiler in type inference, as well as aiding the programmer in type-directed programming.

Simon Peyton Jones has objected to the introduction of functional dependencies in Haskell on grounds of complexity.[10]

Type classes and implicit parameters

[edit]

Type classes and implicit parameters are very similar in nature, although not quite the same. A polymorphic function with a type class constraint such as:

sum :: Num a => [a] -> a

can be intuitively treated as a function that implicitly accepts an instance of Num:

sum_ :: Num_ a -> [a] -> a

The instance Num_ a is essentially a record that contains the instance definition of Num a. (This is in fact how type classes are implemented under the hood by the Glasgow Haskell Compiler.)

However, there is a crucial difference: implicit parameters are more flexible; different instances of Num Int can be passed. In contrast, type classes enforce the so-called coherence property, which requires that there should only be one unique choice of instance for any given type. The coherence property makes type classes somewhat antimodular, which is why orphan instances (instances that are defined in a module that neither contains the class nor the type of interest) are strongly discouraged. However, coherence adds another level of safety to a language, providing a guarantee that two disjoint parts of the same code will share the same instance.[11]

As an example, an ordered set (of type Set a) requires a total ordering on the elements (of type a) to function. This can be evidenced by a constraint Ord a, which defines a comparison operator on the elements. However, there can be numerous ways to impose a total order. Since set algorithms are generally intolerant of changes in the ordering once a set has been constructed, passing an incompatible instance of Ord a to functions that operate on the set may lead to incorrect results (or crashes). Thus, enforcing coherence of Ord a in this particular scenario is crucial.

Instances (or "dictionaries") in Scala type classes are just ordinary values in the language, rather than a completely separate kind of entity.[12][13] While these instances are by default supplied by finding appropriate instances in scope to be used as the implicit parameters for explicitly-declared implicit formal parameters, that they are ordinary values means that they can be supplied explicitly, to resolve ambiguity. As a result, Scala type classes do not satisfy the coherence property and are effectively a syntactic sugar for implicit parameters.

This is an example taken from the Cats documentation:[14]

// A type class to provide textual representation
trait Show[A] {
  def show(f: A): String
}

// A polymorphic function that works only when there is an implicit 
// instance of Show[A] available
def log[A](a: A)(implicit s: Show[A]) = println(s.show(a))

// An instance for String
implicit val stringShow = new Show[String] {
  def show(s: String) = s
}

// The parameter stringShow was inserted by the compiler.
scala> log("a string")
a string

Rocq (previously known as Coq), version 8.2 onward, also supports type classes by inferring the appropriate instances.[15] Recent versions of Agda 2 also provide a similar feature, called "instance arguments".[16]

Other approaches to operator overloading

[edit]

In Standard ML, the mechanism of "equality types" corresponds roughly to Haskell's built-in type class Eq, but all equality operators are derived automatically by the compiler. The programmer's control of the process is limited to designating which type components in a structure are equality types and which type variables in a polymorphic type range over equality types.

SML's and OCaml's modules and functors can play a role similar to that of Haskell's type classes, the principal difference being the role of type inference, which makes type classes suitable for ad hoc polymorphism.[17] The object oriented subset of OCaml is yet another approach which is somewhat comparable to the one of type classes.

[edit]

An analogous notion for overloaded data (implemented in GHC) is that of type family.[18]

In C++, notably C++20, has support for type classes using Concepts (C++). As an illustration, the above mentioned Haskell example of typeclass Eq would be written as

template <typename T>
concept Equal =
      requires (T a, T b) {
            { a == b } -> std::convertible_to<bool>;
            { a != b } -> std::convertible_to<bool>;
};

In Clean typeclasses are similar to Haskell, but have a slightly different syntax.

Rust supports traits, which are a limited form of type classes with coherence.[19]

Mercury has typeclasses, although they are not exactly the same as in Haskell.[further explanation needed]

In Scala, type classes are a programming idiom that can be implemented with existing language features such as implicit parameters, not a separate language feature per se. Because of the way they are implemented in Scala, it is possible to explicitly specify which type class instance to use for a type at a particular place in the code, in case of ambiguity. However, this is not necessarily a benefit as ambiguous type class instances can be error-prone.

The proof assistant Rocq has also supported type classes in recent versions. Unlike in ordinary programming languages, in Rocq, any laws of a type class (such as the monad laws) that are stated within the type class definition, must be mathematically proved of each type class instance before using them.

References

[edit]
  1. ^ Morris, John G. (2013). Type Classes and Instance Chains: A Relational Approach (PDF) (PhD). Department of Computer Science, Portland State University. doi:10.15760/etd.1010.
  2. ^ a b Wadler, P.; Blott, S. (1989). "How to make ad-hoc polymorphism less ad hoc". Proceedings of the 16th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL '89). Association for Computing Machinery. pp. 60–76. doi:10.1145/75277.75283. ISBN 0897912942. S2CID 15327197.
  3. ^ Kaes, Stefan (March 1988). "Parametric overloading in polymorphic programming languages". Proceedings 2nd European Symposium on Programming Languages. doi:10.1007/3-540-19027-9_9.
  4. ^ Appel, A.W.; MacQueen, D.B. (1991). "Standard ML of New Jersey". In Maluszyński, J.; Wirsing, M. (eds.). Programming Language Implementation and Logic Programming. PLILP 1991. Lecture Notes in Computer Science. Vol. 528. Springer. pp. 1–13. CiteSeerX 10.1.1.55.9444. doi:10.1007/3-540-54444-5_83. ISBN 3-540-54444-5.
  5. ^ Type from Data.Kind appeared in version 8 of the Glasgow Haskell Compiler
  6. ^ Haskell' page MultiParamTypeClasses.
  7. ^ In GHC, the C Core uses Girard & Reynold's System F type signatures to identify a typed case for processing in the optimization phases. -- Simon Peyton-Jones "Into the Core - Squeezing Haskell into Nine Constructors" Erlang User Conference, Sep 14, 2016
  8. ^ Jones, Mark P. (2000). "Type Classes with Functional Dependencies". In Smolka, G. (ed.). Programming Languages and Systems. ESOP 2000. Lecture Notes in Computer Science. Vol. 1782. Springer. pp. 230–244. CiteSeerX 10.1.1.26.7153. doi:10.1007/3-540-46425-5_15. ISBN 3-540-46425-5.
  9. ^ Haskell' page FunctionalDependencies.
  10. ^ Peyton Jones, Simon (2006). "MPTCs and functional dependencies". Haskell-prime mailing list.
  11. ^ Kmett, Edward (January 21, 2015). Type Classes vs. the World (video). Boston Haskell Meetup. Archived from the original on 2025-08-06.
  12. ^ Oliveira, Bruno C.d.S.; Moors, Adriaan; Odersky, Martin (2010). "Type Classes as Objects and Implicits" (PDF). Proceedings of the ACM International Conference on Object Oriented Programming Systems Languages and Applications (OOPSLA '10). Association for Computing Machinery. pp. 341–360. CiteSeerX 10.1.1.205.2737. doi:10.1145/1869459.1869489. ISBN 9781450302036. S2CID 207183083.
  13. ^ "The Neophyte's Guide to Scala Part 12: Type classes - Daniel Westheide".
  14. ^ typelevel.org, Scala Cats
  15. ^ Castéran, P.; Sozeau, M. (2014). "A Gentle Introduction to Type Classes and Relations in Coq" (PDF). CiteSeerX 10.1.1.422.8091.
  16. ^ "Modelling Type Classes With Instance Arguments".
  17. ^ Dreyer, Derek; Harper, Robert; Chakravarty, Manuel M.T. (2007). "Modular Type Classes". Proceedings of the 34th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL '07). pp. 63–70. See p. 63. doi:10.1145/1190216.1190229. ISBN 978-1595935755. S2CID 1828213. TR-2006-03.
  18. ^ "GHC/Type families - HaskellWiki".
  19. ^ Turon, Aaron (2017). Specialization, coherence, and API evolution (Report).
[edit]
支原体感染咳嗽吃什么药 嘌呤高会引起什么症状 肺炎不能吃什么 忌入宅是什么意思 艾附暖宫丸什么时候吃
什么原因导致胎停 慢性咽喉炎吃什么药 悔教夫婿觅封侯是什么意思 野合什么意思 白痰吃什么药
富裕是什么意思 残疾证有什么补贴 油腔滑调指什么生肖 肝最怕什么 circle是什么意思
乐捐是什么意思 茯苓什么味道 卵泡长得慢是什么原因造成的 翡翠和玉有什么区别 维生素ad和d3有什么区别
社康是什么hcv8jop6ns9r.cn 石斛有什么作用hcv8jop4ns7r.cn 佛性是什么意思hcv8jop2ns9r.cn 一命呜呼是什么意思0735v.com 拍拖什么意思hcv8jop1ns7r.cn
双脚发热是什么原因hcv8jop4ns0r.cn 林伽是什么hcv9jop7ns4r.cn 木薯粉可以做什么美食hcv8jop9ns2r.cn 胆囊炎不能吃什么hcv8jop3ns4r.cn 长期缺铁性贫血会导致什么后果fenrenren.com
蛇和什么相冲1949doufunao.com 草酸对人体有什么危害hcv7jop6ns8r.cn 营养神经吃什么药效果好hcv9jop1ns3r.cn 往生咒是什么意思xinmaowt.com 什么叫做脂肪肝hcv9jop5ns3r.cn
黄鼠狼喜欢吃什么东西hcv9jop2ns6r.cn 长脸适合什么短头发hcv8jop4ns3r.cn 鼠是什么命hcv8jop9ns2r.cn 三国是什么朝代hcv9jop4ns9r.cn 什么叫专科hcv8jop8ns9r.cn
百度