haskell - What is Ord type? -


is every class not type in haskell :

prelude> :t max max :: ord => -> -> prelude> :t ord  <interactive>:1:1: not in scope: data constructor ‘ord’ prelude>  

why not print ord type signature ?

okay, there's couple of things going on here.

first when write :t ord you're looking called ord in value namespace; have constructor, since name starts capital letter.

haskell keeps types , values separate; there no relationship between name of type , names of type's constructors. when there's 1 constructor, people use same name type. example being data foo = foo int. declares two new named entities: type foo , constructor foo :: int -> foo.

it's not idea think of making type foo can used both in type expressions , construct foos. because common declarations data maybe = nothing | a. here there 2 different constructors maybe a, , maybe isn't name of @ @ value level.

so because you've seen ord in type expression doesn't mean there name ord @ value level ask type of :t. if there were, wouldn't related top type-level name ord.

the second point needs clarifying no, classes not in fact types. class set of types (which support interface defined in class), not type itself.

in vanilla haskell type classes "extra" things. can declare them class declaration, instantiate them instance declaration, , use them in special syntax attached types (the stuff left of => arrow) constraints on type variables. don't interact rest of language, , cannot use them in main part of type signature (the stuff right of `=> arrow).

however, constraintkinds extension on, type classes do become ordinary things exist in type namespace, maybe. still not types in sense there can never values have them types, can't use ord or ord int argument or return type in function, or have [ord a] or that.

in bit type constructors maybe. maybe name bound in type namespace, not type such; there no values type maybe, maybe can used part of expression defining type, in maybe int.

if you're not familiar kinds, ignore i've said constraintkinds onwards; you'll learn kinds eventually, they're not feature need know beginner. if are, however, constraintkinds make special kind constraint , have type class constraints (left of => arrow) ordinary type-level things of kind constraint instead of special purpose syntax. means ord type-level thing, , can ask it's kind :k command in ghci:

prelude> :k ord * -> constraint 

which makes sense; max had type ord => -> -> a, ord a must have kind constraint. if ord can applied ordinary type yield constraint, must have kind * -> constraint.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -