Skip to content

Commit

Permalink
GitBook: [#62] No subject
Browse files Browse the repository at this point in the history
  • Loading branch information
dostrelith678 authored and gitbook-bot committed Oct 16, 2022
1 parent f402b11 commit a549f6f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cutom-types/declaring-types/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Declaring Types

We have already explored the basic types of Haskell in [chapter 2](../../types-in-haskell/basic-types.md), but now we will look into how we can define our own types. There are three different ways in which we can define new types in Haskell – **Type synonyms** (or **Type aliases**), **Data declarations**, and **Newtype declarations**.
We have already explored the basic types of Haskell in [Basic Types](../../types-in-haskell/basic-types.md), so now we will look into how we can define our own types. There are three different ways in which we can define new types in Haskell – **Type synonyms** (or **Type aliases**), **Data declarations**, and **Newtype declarations**.
10 changes: 5 additions & 5 deletions cutom-types/declaring-types/data-declarations.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Data Declarations

With the keyword `Data`, we can define new types rather than just synonyms for already existing ones. Types declared with `data` are called **algebraic**, referencing **"sum"** and **"product"**. In data types **"sum"** is alteration (`A | B`, meaning `A` or `B` but not both), and **"product"** is combination (`A B`, meaning `A` and `B` together).
With the keyword `Data`, we can define new types rather than just synonyms for already existing ones. Types declared with `data` are called **algebraic**, referencing **"sum"** and **"product"**. In data types, **"sum"** means _alteration_ (`A | B`, meaning `A` or `B` but not both), and **"product"** means _combination_ (`A B`, meaning `A` and `B` together).

For example, we can create a type that represents a card suit:

Expand All @@ -11,15 +11,15 @@ data Suit = Hearts
| Clubs
```

`Suit` is the **type constructor** in this definition, and the values (`Hearts`, `Diamonds`, `Spades` or `Clubs`) are **data constructors**. It states that the new type `Suit` can have **one of the four values** (the `|` symbol stands for "or"). This means `Suit` is a **sum-type**, which is any type that has multiple possible representations. Another example of a sum-type would be `Bool` which can be either `True` or `False:`
`Suit` is the **type constructor** in this definition, and the values (`Hearts`, `Diamonds`, `Spades` or `Clubs`) are **data constructors**. It states that the new type `Suit` can have **one of the four values** (the `|` symbol stands for "or"). This means that `Suit` is a **sum type**, which is any type that has multiple possible representations. Another example of a sum type would be `Bool` which can be either `True` or `False:`

```haskell
data Bool = True | False
```

**Data constructors** can have zero or more arguments. `Hearts | Diamonds / True | False` are all examples of data constructors that have zero arguments - **nullary data constructors**. Likewise, **type constructors** can have zero or more arguments. `Suit` and `Bool` are examples or **nullary type constructors**.
**Data constructors** can have zero or more arguments. `Hearts | Diamonds` and `True | False` are examples of data constructors that have zero arguments - **nullary data constructors**. Likewise, **type constructors** can have zero or more arguments. `Suit` and `Bool` are examples of **nullary type constructors**.

Let's take a look at some type and data constructors that have more than zero arguments. Here is an example of an error type from the [cardano-node repository](https://github.com/input-output-hk/cardano-node/blob/master/cardano-node/src/Cardano/Node/Types.hs):
Let's take a look at some type- and data- constructors that have more than zero arguments. Here is an example of an error type from the [cardano-node repository](https://github.com/input-output-hk/cardano-node/blob/85d05720da7b0c02dfa890079e568c8499a027a7/cardano-node/src/Cardano/Node/Types.hs#L49-L52):

```haskell
data ConfigError =
Expand All @@ -30,7 +30,7 @@ data ConfigError =

`ConfigError` is a nullary type constructor, and its data constructors are `ConfigErrorFileNotFound FilePath`, and `ConfigErrorNoEKG`. `ConfigErrorFileNotFound FilePath` is a **unary** data constructor meaning that it actually holds some data, in this case of type `FilePath`. Nullary type constructors we have seen so far contain no data aside from their names.

Both the type name and its constructors must begin with a capital letter, and **constructors must be unique** to that type, i.e. the same constructor cannot be defined in more than one type. Once a new type is defined, it can be used in functions just like any other data type in Haskell. Just to illustrate, a very simple function to get the card suit in a string format would be:
Both the type name and its constructors must begin with a capital letter, and **constructors must be unique** to that type, i.e. the same constructor cannot be defined in more than one type. Once a new type is defined, it can be used in functions just like any other data type in Haskell. To illustrate, a very simple function to get the card suit in a string format using pattern matching would be:

```haskell
suitStr :: Suit -> String
Expand Down
4 changes: 2 additions & 2 deletions cutom-types/declaring-types/type-synonyms.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type Synonyms

**Type synonyms** are the simplest way to declare a new type as they simply provide an alias for one of the already existing types. For example, we already know that `String` is actually just a synonym for list of `Chars`, and it is defined as:
**Type synonyms** are the simplest way to declare a new type as they simply provide an alias for an already existing type. For example, we already know that `String` is actually just a synonym for a list of `Chars`, and it is defined as:

```haskell
type String = [Char]
Expand All @@ -12,7 +12,7 @@ We can use the declared type synonyms to define other types as well. We can defi
type StringList = [String]
```

It is important to note that the type synonyms and their base types are interchangeable in almost all cases. That means that any function that has a type signature including a list of strings (`[String]`) could be used on an element that has the type of `StringList` as they are just synonyms:
It is important to note that the **type synonyms and their base types are interchangeable** in almost all cases. That means that any function that has a type signature including a list of strings (`[String]`) could be used on an element that has the type of `StringList` as they are just synonyms:

```haskell
reverseStringList :: StringList -> StringList
Expand Down

0 comments on commit a549f6f

Please sign in to comment.