I know (->)
type is defined as data (->) t1 t2
. I wonder is there a data constructor function for the (->)
type?
A data constructor is a "function" that takes 0 or more values and gives you back a new value. A type constructor is a "function" that takes 0 or more types and gives you back a new type.
A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.
Data constructors are first class values in Haskell and actually have a type. For instance, the type of the Left constructor of the Either data type is: Left :: 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.
What's a typeclass in Haskell? A typeclass defines a set of methods that is shared across multiple types. For a type to belong to a typeclass, it needs to implement the methods of that typeclass. These implementations are ad-hoc: methods can have different implementations for different types.
No, (->)
does not have a data constructor, as functions are not algebraic data types. The fact that ghci says
Prelude> :inf (->)
data (->) t1 t2 -- Defined in ‘GHC.Prim’
is a bit of a lie, because that is not how (->)
is being defined. In fact, there is no definition around, as functions are a very primitive notion.
In some sense, a lambda expression \x -> e
(or, equivalently, a function definition) is the “constructor” of values of type a -> b
, but you cannot pattern match on that. Instead, you use function applications (f e
) to deconstruct (“use”) a function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With