甲状腺囊实性结节是什么意思| 白细胞偏低吃什么药| 苹果什么时候吃最好| 抽电子烟有什么危害| 梦见自己娶媳妇是什么意思| 属兔的和什么属相最配| 桃李是什么意思| 木耳菜是什么菜| 暑假让孩子学点什么好| afd是什么意思| 小孩老咳嗽是什么原因| 白是什么结构的字| 高职本科什么意思| 88年五行属什么| 自我为中心是什么意思| 萝卜喝醉了会变成什么| 1977属什么生肖| 一个壳一个心念什么| 后巩膜葡萄肿是什么意思| 东倒西歪的动物是什么生肖| 康健是什么意思| 大芒果是什么品种| 胃溃疡吃什么好| 微信限额是什么意思| 痔疮饮食要注意什么| 浓鼻涕吃什么药| 梦见考试是什么意思| 左眼皮一直跳是什么原因| 凉拌菜用什么醋好| 成字五行属什么| 支气管炎吃什么药有效| 麦冬有什么作用与功效| 热疹用什么药| 莲子不能和什么一起吃| 吃芒果对人有什么好处| 杧果是什么| 一什么叮咛| 随喜功德是什么意思| 骶髂关节炎吃什么药| 兰花长什么样| 喉咙痰多是什么原因造成的| 药店属于什么单位性质| 薤白是什么东西| 玻尿酸有什么作用| 毛细血管扩张是什么原因引起的| 室性逸搏是什么意思| 减肥吃什么药| 瓜皮什么意思| 爱拍马屁的动物是什么生肖| 腋下有疙瘩是什么原因| 可乐煮姜有什么作用| 6月24日是什么日子| 拌凉菜需要什么调料| 鸭吃什么食物| 电视开不了机是什么原因| 卵巢炎症有什么症状| 县常委什么级别| 没经验的人开什么店好| kappa属于什么档次| 朦胧是什么意思| 一生辛苦不得财是什么生肖| 发蜡和发泥有什么区别| 广菜是什么菜| 长疖子用什么药| 红细胞计数偏高是什么意思| 泌尿系彩超主要是检查什么| 面部脂溢性皮炎用什么药| 怀孕哭对宝宝有什么影响| 黄鼠狼为什么叫黄大仙| 奕五行属什么| onlycook是什么牌子| 血糖高是什么原因引起的| 梦见门坏了什么意思| 苯磺酸氨氯地平片什么时候吃最好| 岍是什么意思| 血糖高的人吃什么好| 小孩肚子痛吃什么药| 骨折有什么忌口| 丁胺卡那又叫什么药名| 男女授受不亲是什么意思| 总蛋白低是什么意思| 溥仪什么时候去世的| hsv病毒是什么意思| 脾胃虚弱吃什么食物| 食道炎症吃什么药最好| 卫生纸筒可以做什么| 水肿吃什么药| 金针菇为什么不能消化| 葛根有什么作用| 白带是绿色的是什么原因| 补充蛋白质提高免疫力吃什么| 公分是什么意思| 胆固醇高不可以吃什么| 小蛇吃什么| 脖子肿大是什么病的症状| 睡觉脚麻是什么原因| 夏枯草治什么病| 自然人是什么意思| m标志是什么牌子衣服| 光屏是什么| 吃什么食物补钾最快| 社保缴费基数是什么意思| 玫瑰红是什么颜色| 6月25日是世界什么日| iphone5什么时候出的| 测骨龄挂什么科| 疙瘩是什么意思| 什么是穿堂风| 大便真菌阳性说明什么| 丑是什么生肖| 喝什么利尿效果最好| 睡觉张嘴巴是什么原因| 寂是什么意思| ercp是什么检查| 解表化湿是什么意思| 做梦梦见搬家是什么意思| 2月2日是什么星座| no是什么气体| 甲功五项是什么意思| 为什么会高反| 包皮炎用什么药| 1221是什么星座| 花容月貌是什么意思| 梦见大火是什么意思| 什么东西嘴里没有舌头| 血常规主要检查什么| 梦见给别人剪头发是什么意思| 什么叫钝痛| 送老师什么礼物最好| 四维和大排畸有什么区别| 口语化是什么意思| 5.11什么星座| 旮旯是什么意思| 冒菜是什么菜| 包涵是什么意思| 沙龙会是什么意思| 笑面虎比喻什么样的人| 五郎属什么生肖| 洛阳有什么山| 女生为什么会痛经| d g是什么牌子| 成人改名字需要什么手续| 欢喜是什么意思| 尿检潜血是什么意思| 浮尘是什么意思| 血管瘤是什么引起的| 内痔有什么症状与感觉| 什么叫保守治疗| 红细胞计数偏高是什么意思| 医保和农村合作医疗有什么区别| 眼压低是什么原因| 指甲硬是什么原因| con是什么意思| 肝内高回声结节是什么意思| 脖子上长个包挂什么科| 低压高用什么药| 荨麻疹是什么| 心直口快是什么意思| 瑗是什么意思| 出去玩带什么| 的确什么意思| 过意不去是什么意思| 火腿是什么动物的腿| a21和以纯什么关系| 肺部结节灶是什么意思啊| 痛风为什么要禁欲| 后中长是什么意思| 胃酸吃什么可以缓解| 牙齿冷热都疼是什么原因| 槟榔为什么上瘾| 不感冒是什么意思| 腰椎间盘突出压迫神经吃什么药| 6.1号是什么星座| 看破红尘什么意思| 4月29号是什么星座的| 血糖高能喝什么粥| 羊绒和羊毛有什么区别| 激素六项什么时候查最准| 望子成龙是什么意思| 维生素d3吃多了有什么副作用| hpv低危型是什么意思| 猫腻是什么意思| 三更是什么生肖| 新股配号数量是什么意思| 女生做彩超是检查什么| 妹妹是什么意思| 高招是什么意思| 上火吃什么水果好| 冬瓜炖什么好吃| ckd3期是什么意思| 什么食物含叶酸| 哺乳期吃什么下奶| 同人文什么意思| 户籍地填什么| 糖化血红蛋白是查什么的| 肚子咕咕叫是什么原因| 肺和大肠相表里是什么意思| 用什么药可以缩阴紧致| 为什么屁多| 1月19日什么星座| 波涛澎湃是什么意思| 手外科属于什么科| 日加西念什么| 慢性肠炎有什么症状| 哎呀是什么意思| 腺癌是什么原因引起的| 蛏子是什么| 报道是什么意思| 促甲状腺素高是什么意思| 抽电子烟有什么危害| 体检要带什么| 重庆沱茶属于什么茶| 肝硬化什么症状| 潘氏试验阳性说明什么| 谷氨酰基转移酶低是什么原因| 梁五行属什么| c12是什么| 新生儿黄疸高有什么风险| 绿鼻涕是什么原因| 躺平什么意思| 范仲淹世称什么| 黑管是什么乐器| 吃芒果后不能吃什么| 蹲不下去是什么原因| 免疫力低吃什么补| 质是什么意思| 中国国菜是什么菜| 嘴巴干是什么原因| 朝鲜的货币叫什么| 植物纤维是什么面料| 包皮过长有什么影响| holly是什么意思| 非分之想是什么意思| 起大运是什么意思| 吃槟榔有什么好处和坏处| 李世民和武则天什么关系| 枸杞和红枣泡水喝有什么好处| 什么叫种水| 蜗牛是什么生肖| 提手旁的字有什么| 补充公积金是什么意思| 地三鲜是什么| 八九不离十是什么意思| 小米不能和什么一起吃| 大圆脸适合什么发型| 肺纹理增多什么意思| 宫颈醋酸白色上皮是什么病变| 多囊为什么要吃避孕药| 姨妈期吃什么水果| 断眉有什么说法| 色素沉着有什么办法可以去除| 什么病不能吃鸡蛋| 看头发挂什么科| 为什么老是肚子疼| 忠实的什么| 场记是做什么的| 成吉思汗是什么族| 盐为什么要加碘| 娃娃鱼用什么呼吸| 大红袍是什么茶类| 王菲什么星座| 胃不消化吃什么药效果最好| 多吃洋葱有什么好处| 立flag什么意思| 百度

东营市实验中学教师走志愿服务校外解答家长教育困惑

百度 该书立足中国经验,通过界定政府与市场、社会关系,提出了政府职能的“兜底性”特征,明确了政府职能的内容及其优先次序,发展了关于国家角色和政府职能的理论。

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-08.
  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
伤口拆线挂什么科 肠炎吃什么药效果最好 肺部气肿吃什么药能治好 广东省省长是什么级别 鱼露是什么东西
杨梅泡酒有什么功效和作用 2028是什么年 消防支队长是什么级别 什么是血脂 什么什么为笑
声带白斑是什么病严重吗 粿条是什么 男性腰疼挂什么科 阴盛格阳是什么意思 忌日是什么意思
左旋延胡索乙素是什么 排卵期后是什么期 美尼尔综合征吃什么药 到底什么是爱 不寐病属于什么病症
my什么牌子hcv9jop2ns7r.cn 孕妇感染弓形虫有什么症状hcv8jop7ns1r.cn 怀孕了为什么还会出血hcv9jop1ns4r.cn 93属什么生肖hcv7jop9ns1r.cn whatsapp是什么bjcbxg.com
驴友是什么意思hcv8jop4ns9r.cn 什么病会传染人hcv7jop6ns8r.cn 摩羯座和什么座最配hcv9jop0ns6r.cn 吃什么可以丰胸hcv8jop0ns7r.cn 女性睾酮低意味着什么hcv9jop2ns1r.cn
十一月三号是什么星座hcv9jop4ns6r.cn 一直咳嗽不见好是什么原因hcv8jop8ns2r.cn PSV是什么意思hcv9jop7ns0r.cn 眼睛痒是什么原因引起的hcv9jop7ns4r.cn 十月十六号是什么星座hcv7jop9ns9r.cn
属猪的跟什么属相最配hcv9jop2ns2r.cn 王字旁的字有什么hanqikai.com 女性白带有血丝是什么原因hcv8jop0ns9r.cn 喉咙痛挂什么科hcv8jop5ns0r.cn 小弟一阵阵的疼什么原因520myf.com
百度