Oto-Obong Eshiett
Oto-Obong Eshiett

Reputation: 577

What is the type keyword in Haskell

Stumbled on the type keyword in Haskell:

type Item = String

but not sure what it does, how to use it or how it is different from data. The online google search has been of no help.

I tried implementing it in a code like this:

    import System.IO

main = do  
        putStrLn "Hello, what's your name?"  
        type Item = String
        let test :: Item
        test = "chris"
        putStrLn test  

but I got an error

parse error on input ‘type’

Please in a lay man's term what is type and how can it be used and how is it different from data?

Upvotes: 2

Views: 293

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476547

It is a type alias. It means that you can use Item in your code where you can use String instead.

A type alias is often used when you for example want to give a name to more complex types. For example:

import Data.Map(Map)

type Dictionary = Map String String

here you thus can use Dictionary instead of each time writing Map String String.

It is furthermore often used if you want to specify that you are working with Items, the alias is then used in the type signature and in the documentation, which is often better than writing String.

It is also used if you do not yet know what type you will use for a specific object. By using a type alias, you can the work with Item, and later if you change your made define a type for Item or make it an alias of another type. This makes it more convenient to change the types.

I tried implementing it in a code like this:
import System.IO

main = do  
    putStrLn "Hello, what's your name?"  
    type Item = String
    let test :: Item
    test = "chris"
    putStrLn test

A type alias is defined at the top level, so not in a do block, that would make a type definition locally scoped. While, like @moonGoose says, there are some proposals to make type definitions more locally scoped, currently it is not the case.

You can define the type alias like:

import System.IO

type Item = String

main = do  
    putStrLn "Hello, what's your name?"  
    let test :: Item
        test = "chris"
    putStrLn test

Upvotes: 3

leftaroundabout
leftaroundabout

Reputation: 120711

type A = B

means exactly the same as

typedef B A

in C or C++, and it behaves basically the same as simply

a = b

except that A and B are type-level entities, not value-level ones. For example

Prelude> type A = Int
Prelude> :i A
type A = Int    -- Defined at <interactive>:1:1

Prelude> a = 37
Prelude> a
37

Because now A = Int, I can then use the type identifier A exactly everywhere I could also use Int directly:

Prelude> 37 :: Int
37
Prelude> 37 :: A
37

and even

Prelude> (37 :: Int) :: A
37

Note that there is no type conversion going on here, like you might have in other languages. Int and A are simply different names for the same type, so annotating with both is merely a tautology.

Contrast this with data (or newtype), which define a new, separate type which just happens to contain the, well, data of the specified type.

Prelude> data A' = A' { getA :: Int }
Prelude> (37 :: Int) :: A'

<interactive>:12:2: error:
    • Couldn't match expected type ‘A'’ with actual type ‘Int’
    • In the expression: (37 :: Int) :: A'
      In an equation for ‘it’: it = (37 :: Int) :: A'

Upvotes: 2

Related Questions