Headline

A special form of algebraic data type in Language:Haskell

Illustration

Consider the following declaration of a salary type:

type Salary = Float

This declaration introduces merely a type synonym, but it enforces no type distinction. Floats and salaries are compatible in the sense of structural typing. If we were to enforce a type distinction, then the following type declaration could be preferred instead:

data Salary = Salary Float

Thus, salaries and floats are no longer compatible at a typing level; a float may be "wrapped" as a salary; a salary may be "unwrapped" to retrieve a float. Indeed, this special case of using algebraic data types just for making type distinctions is specifically supported by newtypes in Haskell. Accordingly, the following type declaration uses a newtype:

newtype Salary = Salary Float

Syntactically, a newtype is an algebraic data type with only one data constructor with in turn only one constructor component. Semantically, this restriction implies that we can think of the constructor as serving for type distinction only without any semantical purpose such as grouping data.

Consider this program:

data X = X ()
newtype Y = Y ()
f (X _) = True
g (Y _) = True

When f is applied to undefined, then an exception is thrown, as proper pattern matching (term deconstruction) has to be performed in order to confirm the equation. When g is applied to undefined, then the equation is soundly applied (such that True) is returned because no pattern has to be matched and the undefined argument of Y is not inspected.

*Main> f undefined
*** Exception: Prelude.undefined
*Main> g undefined
True
*Main> f (X undefined)
True


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.