-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.html
145 lines (134 loc) · 5.76 KB
/
types.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<!-- Bootstrap -->
<!-- <link href="css/bootstrap.min.css" rel="stylesheet"> -->
<link href="css/prism.css" rel="stylesheet">
<!-- <link href="https://fonts.googleapis.com/css?family=Athiti|Anonymous+Pro" rel="stylesheet"> -->
<link href="css/styles.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<title>Types</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
/* Reference: http://docs.mathjax.org/en/latest/options/SVG.html */
SVG: {
font: "TeX"
/* font: The possible values are TeX, STIX-Web, Asana-Math, Neo-Euler, Gyre-Pagella, Gyre-Termes and Latin-Modern. Default is "TeX" */
}
});
</script>
<script type="text/javascript" async
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_SVG">
</script>
</head>
<body>
<main>
<h3><a href="index.html">Table of Contents</a></h3>
<h2>Types</h2>
<p>
Types are Haskell's best approximations of the mathematical notion of Sets. In fact, Haskell takes the notion of types to a deeper level, associating even functions with their own 'types', we shall see soon now.
</p>
<h3>What is a type?</h3>
<blockquote>At its simplest, a <strong>type</strong> can be thought of as a <strong>set</strong> of related <em>values</em> sharing some common traits.</blockquote>
<p>
Some simple examples of types are <code>Bool</code> which is a set of only 2 values <em>viz.</em>, \(\{True, False\} \). Other common types are <code>Int</code> & <code>Integer</code> which represent the set of all <em>integers</em> which is shown in math by the symbol \(\mathbb{Z}\).
</p>
<p>
In Haskell, a function \(f\) that maps an argument of type \(Int\) to a return value of type \(Int\) is depicted as below:<br>
</p>
<p class="math">\(f :: Int \rightarrow Int\)</p>
<p>
In reality, however, we do have some issues. Although the set of integers is essentially <em>infinite</em>, the set of integers we have on computers is actually <em>finite</em>. You can think of this in terms of a concept in mathematics you might have come across in Calculus, particularly while dealing with the infinite set of the so called <strong>Real Numbers</strong> denoted by \( \mathbb{R} \) – called <strong>the interval</strong> of a <strong>set</strong>.
</p>
<blockquote>
An <strong>interval</strong> of a <strong>set</strong> is a <strong>subset</strong> of the set such that it contains atleast 2 members and contains all members of the set between any 2 elements belonging to the interval (<em>i.e.</em>, no member of the set is left out in the interval).
</blockquote>
<p>
<em>E.g.</em>, the set of all integers such that \( x > 8 \) is an interval. But the set of all non-zero integers is <strong>not</strong> an interval since it leaves out the element zero (\(0\)). Clearly an interval could be <strong>finite</strong> or <strong>infinite</strong>. Also, if the end-points of the interval belong to it, the interval is said to be <strong>closed</strong>; else it is called an <strong>open</strong> interval. Of course, as you must have guessed, an interval can be closed or opern at either end-points. Below are some examples in set notation.
</p>
<table summary="Examples of Finite Intervals of the set of Real Numbers">
<caption>Examples of Finite Intervals in \( \mathbb{R} \)</caption>
<thead>
<tr colspan="2">
<th>
Finite Intervals
</th>
</tr>
<tr>
<th>Interval</td>
<th>Set Notation</td>
</tr>
</thead>
<tbody class="math">
<tr>
<td>\((a, b)\)</td>
<td>\(\{ x | a < x < b\} \)</td>
</tr>
<tr>
<td>\([a, b]\)</td>
<td>\(\{ x | a \leq x \leq b\} \)</td>
</tr>
<tr>
<td>\([a, b)\)</td>
<td>\(\{ x | a \leq x < b\} \)</td>
</tr>
<tr>
<td>\((a, b]\)</td>
<td>\(\{ x | a < x \leq b\} \)</td>
</tr>
</tbody>
</table>
<br/>
<table summary="Examples of Infinite Intervals of the set of Real Numbers">
<caption>Examples of Infinite Intervals in \( \mathbb{R} \)</caption>
<thead>
<tr colspan="2">
<th>
Infinite Intervals
</th>
</tr>
<tr>
<th>Interval</td>
<th>Set Notation</td>
</tr>
</thead>
<tbody class="math">
<tr>
<td>\((a, \infty ) \)</td>
<td>\(\{ x | x > a \} \)</td>
</tr>
<tr>
<td>\([a, \infty ) \)</td>
<td>\(\{ x | x \geq a \} \)</td>
</tr>
<tr>
<td>\((-\infty, b ) \)</td>
<td>\(\{ x | x < b \} \)</td>
</tr>
<tr>
<td>\((-\infty, b ] \)</td>
<td>\(\{ x | x \leq b \} \)</td>
</tr>
<tr>
<td>\((-\infty, \infty ) \)</td>
<td>\( \mathbb{R} \) (the set of real numbers!)</td>
</tr>
</tbody>
</table>
<p>
In general, when we see something of the form \(v :: T\), it means a value \(v\) ``has type'' \(T\).
</p>
</main>
<!-- Load Prism.js for code highlighting -->
<script src="js/prism.js"></script>
</body>
</html>