Constructor
From HaskellWiki
(Difference between revisions)
(start) |
(Data Constructors are first class values, category, links.) |
||
| Line 1: | Line 1: | ||
| + | [[Category:Language]] | ||
'''Constructor''' can mean: | '''Constructor''' can mean: | ||
* Type constructor | * Type constructor | ||
| Line 4: | Line 5: | ||
== Type constructor == | == Type constructor == | ||
| - | A type constructor is used to construct new types from given ones. | + | A [[type]] constructor is used to construct new types from given ones. |
<haskell> | <haskell> | ||
data Tree a = Tip | Node a (Tree a) (Tree a) | data Tree a = Tip | Node a (Tree a) (Tree a) | ||
| Line 21: | Line 22: | ||
and for all practical purposes you can just think of them as ''constants'' belonging to a type. | and for all practical purposes you can just think of them as ''constants'' belonging to a type. | ||
On the other hand, <hask>Node</hask> contains other data. The types of those data are its parameters. The first one has type <hask>a</hask>, so it's just a value of the parameter type <hask>a</hask>. This one is the value the tree node holds in it. The remaining two are the branches. Each of them have type <hask>Tree a</hask>, naturally. | On the other hand, <hask>Node</hask> contains other data. The types of those data are its parameters. The first one has type <hask>a</hask>, so it's just a value of the parameter type <hask>a</hask>. This one is the value the tree node holds in it. The remaining two are the branches. Each of them have type <hask>Tree a</hask>, naturally. | ||
| + | |||
| + | ===Data constructors as first class values=== | ||
| + | Data constructors are first class values in Haskell and actually have a [[type]]. For instance, the type of the <hask>Left</hask> constructor of the <hask>Either</hask> data type is: | ||
| + | <haskell> | ||
| + | Left :: forall b a. a -> Either a b | ||
| + | </haskell> | ||
| + | |||
| + | As first class values, they may be passed to functions, held in a list, be data elements of other algebraic data types and so forth. | ||
| + | |||
=== Note === | === Note === | ||
| Line 36: | Line 46: | ||
* You can declare a constructor (for both type and data) to be infix, and this can make your code a lot more readable. | * You can declare a constructor (for both type and data) to be infix, and this can make your code a lot more readable. | ||
* Tuples are a built in feature of the syntax but are plain old algebraic data types! They have only one constructor though. Having the same name as their types (don't freak out, it's just a matter of convenience, as the type constructors and the data constructors have separate namespaces). So, <hask>(4, True)</hask> is really a value of the form <hask>(,) 4 True</hask> having the type <hask>(,) Int Bool</hask>, which, too, is written conveniently as <hask>(Int, Bool)</hask> to make it more readable. Incidentally, the empty tuple type <hask>()</hask> with its only value <hask>()</hask> is used throughout, and is called ''unit''. | * Tuples are a built in feature of the syntax but are plain old algebraic data types! They have only one constructor though. Having the same name as their types (don't freak out, it's just a matter of convenience, as the type constructors and the data constructors have separate namespaces). So, <hask>(4, True)</hask> is really a value of the form <hask>(,) 4 True</hask> having the type <hask>(,) Int Bool</hask>, which, too, is written conveniently as <hask>(Int, Bool)</hask> to make it more readable. Incidentally, the empty tuple type <hask>()</hask> with its only value <hask>()</hask> is used throughout, and is called ''unit''. | ||
| - | * You can, in fact, name the values grouped together, using the record syntax, <haskell> | + | * You can, in fact, name the values grouped together, using the [[record]] syntax, <haskell> |
data Person = Person { name :: String, age :: Int, address :: String } | data Person = Person { name :: String, age :: Int, address :: String } | ||
</haskell> | </haskell> | ||
so that for a person <hask>p</hask>, you can say <hask>age p</hask> to select his/her age, without resorting to pattern matching. | so that for a person <hask>p</hask>, you can say <hask>age p</hask> to select his/her age, without resorting to pattern matching. | ||
Revision as of 03:41, 20 December 2006
Constructor can mean:
- Type constructor
- Data constructor
Contents |
1 Type constructor
A type constructor is used to construct new types from given ones.
data Tree a = Tip | Node a (Tree a) (Tree a)
Tree
a
Integer
Maybe String
Tree b
b
a
Tree Int
Tree (Tree Boolean)
2 Data constructor
A data constructor groups values together and tags alternatives in an algebraic data type,
data Tree a = Tip | Node a (Tree a) (Tree a)
Tip
Node
Tree a
Tip
Node
Tip
Tip
Bool
data Bool = True | False
and for all practical purposes you can just think of them as constants belonging to a type.
On the other hand,Node
a
a
Tree a
2.1 Data constructors as first class values
Data constructors are first class values in Haskell and actually have a type. For instance, the type of theLeft
Either
Left :: forall b a. a -> Either a b
As first class values, they may be passed to functions, held in a list, be data elements of other algebraic data types and so forth.
2.2 Note
Data constructors are not types! They denote values. It would be illegal to writeNode a (Node a) (Node a)
Tree
Node
3 Deconstructing data constructors
All a data constructor does is holding values together. But you want to separate them if you want to use them. This is done via pattern matching,
depth Tip = 0 depth (Node _ l r) = 1 + max (depth l) (depth r)
_
l
r
4 Notes
- You can declare a constructor (for both type and data) to be infix, and this can make your code a lot more readable.
- Tuples are a built in feature of the syntax but are plain old algebraic data types! They have only one constructor though. Having the same name as their types (don't freak out, it's just a matter of convenience, as the type constructors and the data constructors have separate namespaces). So, is really a value of the form(4, True)having the type(,) 4 True, which, too, is written conveniently as(,) Int Boolto make it more readable. Incidentally, the empty tuple type(Int, Bool)with its only value()is used throughout, and is called unit.()
- You can, in fact, name the values grouped together, using the record syntax,
data Person = Person { name :: String, age :: Int, address :: String }
p
age p
