Saturday 30 April 2016

Haskell: Types of constructors


Haskell data keyword introduces two types of constructors.
a.   Type Constructor
b.   Data Constructor

Type Constructors
For example, consider following snippet.

Sample.hs
{- Define an employee type -}
data Employee = Engineer {name :: String, engineerId :: String}
                | Manager {name :: String, managerId :: Integer}
                | Director {name :: String, directorId :: Integer}
                deriving Show


In above snippet, Employee is the Type constructor, where as Engineer, Manager, Director are the data constructors.

Let us define 3 variables of type Employee.
Prelude> :load Sample.hs
[1 of 1] Compiling Main             ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> let engineer1 = Engineer "Hari" "E43215"
*Main> let manager1 = Manager "Anand" 123
*Main> let director1 = Director "Suhas" 321
*Main> 
*Main> :t engineer1
engineer1 :: Employee
*Main> 
*Main> :t manager1
manager1 :: Employee
*Main> 
*Main> :t director1
director1 :: Employee


Observe above snippet, when I query for types of variable engineer1, manager1, director1, it gives me  “engineer1 :: Employee”, which specifies engineer1 is of type Employee.

Nullary type constructor
A type constructor can have zero (or) more arguments. If it has zero arguments, then it is called Nullary type constructor. For example, Week with seven nullary data constructors Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.
data Week = Sunday | Monday | Tuesday | Wednesday | Thurusday | Friday | Saturday
 
Unary type constructor
When you define a type constructor with single type variable, then it is called unary type constructor.
data List a = Empty | Node a (List a)


List is a unary type constructor.  List Integer represent integer list, where as List String represents list is of tyoe string. Have a look at following snippet.
*Main> data List a = Empty | Node a (List a) deriving Show
*Main> 
*Main> let var1 = Node 10 Empty
*Main> let var2 = Node 20 var1
*Main> 
*Main> var2
Node 20 (Node 10 Empty)
*Main> 
*Main> var1
Node 10 Empty
*Main> 
*Main> :t var1
var1 :: Num a => List a
*Main> 
*Main> :t var2
var2 :: Num a => List a
*Main> 
*Main> 
*Main> let var3 = Node "Krishna" Empty
*Main> let var4 = Node "Hari" var3
*Main> 
*Main> :t var3
var3 :: List [Char]
*Main> 
*Main> :t var4
var4 :: List [Char]
*Main> 
*Main> var4
Node "Hari" (Node "Krishna" Empty)


Data Constructor
A Data constructor can take zero (or) more arguments.

Sample.hs
{- Define an employee type -}
data Employee = Engineer {name :: String, engineerId :: String}
                | Manager {name :: String, managerId :: Integer}
                | Director {name :: String, directorId :: Integer}
                deriving Show


Engineer, Manager and Director are data constructors. Where Engineer takes two string arguments, Manager and Director takes one string, integer arguments.

Nullary Data constructor (or) Constant
A data constructor that takes zero arguments is called nullary data constructor (also called as constant).

data Bool = True | False

True and False are examples of nullary data constructor.

data List a = Empty | Node a (List a)

Empty is a nullary data constructor
List is unary type constructor
Node is a data constructor with two arguments.

Reference






 
Previous                                                 Next                                                 Home

No comments:

Post a Comment