-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.tex
2025 lines (1709 loc) · 98 KB
/
types.tex
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = reference.tex
\chapter{Types}
\label{types}
\index{type system}
\Sr is a strongly, statically, typed language. That means that all values and all variables must have a unique well-defined type that is determinable by inspecting the text of the program -- effectively at `compile time'.
The type system of \Sr consists of a method for declaring new types, for annotating variables (and by extension programs) with their types and a system of verifying the type consistency of programs.
\section{What is a Type?}
A \ntRef{Type} is an expression that denotes a set of values.
\begin{aside}
Although a type is an expression, type expressions should not be confused with normal expressions. Types generally play no part in evaluation.
\end{aside}
Viewed as sets, types have some particular properties: no value may be a member of more than one type set; all values are members of exactly one type set.
A \ntRef{TypeDefinition} introduces a new type and defines what values belong to the type. A \ntRef{TypeAnnotation} is an assertion that a particular expression has a certain type.
For many simple cases, a type is denoted by an identifier. For example, the type identifier \q{string} denotes the set of all strings. More explicitly, a value has type \q{string} iff\footnote{The term `iff' means `if and only if'.} it belongs to the set denoted by the symbol \q{string}.
Many value-sets are effectively infinite in size: the size of the set of \q{string}s is essentially unbounded; as is the set of \q{integer} values.
In addition to sets of values denoted by identifiers; there are other kinds of value sets that have more complex type expressions. For example, the set of \emph{function values} is denoted not by a single type expression but a \emph{schema} of type expressions -- each taking a form such as:
\begin{alltt}
(t\sub1\sequence{,}t\subn)=>t
\end{alltt}
For example, the type expression
\begin{alltt}
(integer)=>string
\end{alltt}
denotes the set of functions that take an \q{integer} as an argument and produce a \q{string} value. Like the set of all integers, this set is also infinite in size.
The language for denoting types is quite expressive. It is possible to have types that are parameterized; that is they are composed from other type expressions. It is also possible to have types that are not explicitly named but are defined by constraints.
A simple example of a parameterized type is the \q{cons} type: a \q{cons} type expression always involves the mention of another type -- the type of elements of the list. The type expression
\begin{alltt}
cons of string
\end{alltt}
denotes the type expression associated with lists whose elements are all string values. Other examples of \q{cons} type include lists of integers:
\begin{alltt}
cons of integer
\end{alltt}
and even lists of lists of string valued functions:
\begin{alltt}
cons of cons of ((integer)=>string)
\end{alltt}
\noindent\begin{aside}
Technically, the \q{cons} symbol in:
\begin{alltt}
cons of integer
\end{alltt}
\index{type function}
is a \ntRef{TypeConstructor}: it takes a type as an argument and returns another type as result.
\end{aside}
Often it is convenient to be able to `talk' about types without being specific about the type itself; for this purpose we use \ntRef{TypeVariable}s.
There are two ways in which a type variable is distinguished: either by an explicit quantifier (\ntRef{UniversalType} or \ntRef{ExistentialType}) or by prefixing a \q{\pcent{}} in front of the identifier. The type expression:
\begin{alltt}
cons of \pcent{}t
\end{alltt}
denotes a list type of some unspecified element type.
\begin{aside}
The value set associated with this type expression is a little more difficult to visualize than the set of lists of integers (say). \q{cons of \pcent{}t} denotes a set of list values; but without more information \emph{we cannot say} what the complete values look like -- it is dependent on the meaning of the type variable \q{\pcent{}t}.
\end{aside}
\begin{aside}
In order to properly understand the interpretation of a type variable one must understand how the type variable is 'bound'. In general, there are three possibilities: the type variable may be identified with (equal to) another type; the type variable may be bound by a universal quantifier or by an existential quantifier.
A universally quantified type (see Section~\vref{universalType}) denotes a type that allows all possible instantiations for the type variable. For example, function types often involve universal types. A universally typed function is expected to work `for all values' of the type variable -- which, in turn, means that the function definition can make no assumptions about the actual type.
Existentially quantified types (see Section~\vref{existentialType}) are used to denote \emph{abstract types}; i.e., the existential quantifier signals that there is a type that should be treated as an opaque `black box'.
\end{aside}
\begin{aside}
It is not required to annotate a type variable with a leading \q{\pcent{}}. If an identifier is identified as a type variable by virtue of the fact that it is bound by an explicit quantifier then the leading \q{\pcent{}} is not required.
However, for exposition purposes, especially where it is not clear what the binding of a type variable may be, we will use an explicit \q{\pcent{}} to identify type variables.
\end{aside}
\subsubsection{Type Safety}
The connection between the argument type of a \q{cons} type expression and the actual elements of lists is denoted by a \emph{type inference rule}. Type inference rules are rules for relating expressions and statements in the language to the types associated with that statement. For example, the rule:
\begin{prooftree}
\AxiomC{\typeprd{E}{El\sub1}{T}\sequence{\ }\typeprd{E}{El\subn}{T}}
\UnaryInfC{\typeprd{E}{\q{cons of [}El\sub1\sequence{,}El\subn\q{]}}{\q{cons of }T}}
\end{prooftree}
says that if the expressions \emph{El\sub1} through \emph{El\subn} all have type \emph{T}, then the list expression
\begin{alltt}
cons of [\emph{El\sub1}\sequence{,}\emph{El\subn}]
\end{alltt}
has type \q{cons of} T. This is the formal way of stating that all elements of a list must have the same type.
The general form of a type inference rule that is determining a type (sometimes called a type judgment) is:
\begin{prooftree}
\AxiomC{\mbox{\emph{Condition}}}
\UnaryInfC{\typeprd{E}{X}{T}}
\end{prooftree}
This should be read as
\begin{quote}
If \emph{Condition} is satisfied, then we can infer from the context \emph{E} that \emph{X} has type \emph{T}
\end{quote}
where the symbol \tinfers{} can be read as `type implication'. In general, the type of an expression depends on the context that it is found.
\paragraph{Type Annotations}
In most cases it is not necessary to explicitly declare the type of a variable. However, it is good practice to declare explicitly the types of programs; especially within \ntRef{ThetaEnvironment}s.
For example, a generic function \q{consLength} that takes a \q{cons} list and returns an integer would have the declaration:
\begin{lstlisting}
consLength has type
for all t such that (cons of t)=>integer
\end{lstlisting}
This is an example of a universally quantified type -- see Section~\vref{universalType} for more details.
\paragraph{Kind Annotations}
\label{kindAnnotation}
Just as values have types, and the language system arranges to ensure that types are preserved, so types have \emph{kinds}. A \ntRef{Kind} is a `kind of type'.
\begin{aside}
Type \ntRef{Kind}s allow the language to keep track of the expected arity of a type: i.e., how many type arguments the type expected.
\end{aside}
\section{Type Expressions}
\label{typeExpressions}
\index{forms of types}
\index{type expressions}
Figure~\vref{typeFig} illustrates the top-levels of the different kinds of type expressions that the \Sr programmer will encounter.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{Type}&\arrow&\ntRef{TypeExpression}\\
&\choice&\ntRef{TypeVariable}\ \choice\ \ntRef{ReferenceType}\\
&\choice&\ntRef{TupleType}\\
&\choice&\ntRef{RecordType}\\
&\choice&\ntRef{FunctionType}\ \choice\ \ntRef{PatternType}\ \choice\ \ntRef{ConstructorType}\\
&\choice&\ntRef{UniversalType}\ \choice\ \ntRef{ExistentialType}\\
&\choice&\ntRef{Type}\ \q{where}\ \ntRef{TypeConstraint}\\
&\choice&\q{(}\ \ntRef{Type}\ \q{)}\\
&\choice&\ntRef{EncapsulatedType}
\end{eqnarray*}
\caption{Types of Types}
\label{typeFig}
\end{figure}
There are two main kinds of type expressions -- so-called \emph{structural} type expressions and \emph{named} type expression. A structural type expression encodes by convention the permitted \emph{forms} of values of that type. By contrast, a named type expression is defined via some form of \ntRef{TypeDefinition}.
A classic example of a structural type expression is the function type. A function type expression defines both the types of the arguments and result type of the function. But, more importantly, it signals that the value is a function.
A good example of a named type is the standard \q{integer} type. The word \q{integer} does not signal by itself that the allowable operations on integer values include arithmetic, comparison and so on. That information must come from additional statements and declarations.
One of the other differences between structural and named type expressions is that the latter may be used to denote \emph{recursive} types, whereas the former cannot.
\begin{aside}
A recursive type is one whose values may contain elements that are themselves of the same type. For example, in a \q{tree} type: the nodes of the tree are typically themselves trees.
\end{aside}
\subsection{Type Expressions}
\label{typeNames}
\index{type}
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{TypeExpression}&\arrow&\ntRef{TypeConstructor}\ \q{of}\,\ntRef{TypeArgument}\\
&\choice&\ntRef{Identifier}\\
\ntDef{TypeArgument}&\arrow&\ntRef{Type}\\
&\choice&\q{(}\,\ntRef{Type}\sequence{,}\ntRef{Type}\q{)}\\
\ntDef{TypeConstructor}&\arrow&\ntRef{Identifier}\\
&\choice&\ntRef{TypeVar}
\end{eqnarray*}
\caption{Type Expressions}
\label{typeExpressionFig}
\end{figure}
A \ntRef{TypeExpression} is a term that identifies a class of values by name. The name may or may not have \ntRef{TypeArgument}s -- in which case, the type is said to be \emph{parameterized}.
\index{type!parameterized}
\index{parameterized types}
\subsubsection{Simple Types}
\label{simpleType}
\index{type!simple}
A simple type is \ntRef{TypeExpression} with no type arguments. Some simple types are pre-defined, Table~\vref{predefinedTypes} gives a table of such types.
\index{standard!simple types}
\begin{table}[h]
\begin{center}
\caption{Standard Pre-defined Types\label{predefinedTypes}}
\begin{tabular}{|ll|}
\hline
Type&Description\\
\hline
\tt boolean&used for logical values and conditions\\
\tt float&type of floating point numbers\\
\tt integer&type of 32-bit integer values\\
\tt long&type of 64-bit integer values\\
\tt decimal&type of decimal values\\
\tt string&type of string values\\
\tt quoted&type of abstract syntax\\
\tt astLocation&type of location marker\\
\tt exception&type of exception token\\
\hline
\end{tabular}
\end{center}
\end{table}
\subsubsection{Parameterized Types}
\label{parameterizedType}
\index{type!parameterized}
\index{parameterized type}
A parameterized \ntRef{TypeExpression} consists of a \ntRef{TypeConstructor} applied to one of more \ntRef{Type} arguments. For example, the standard \q{cons} type constructor has one type argument -- the type of elements of the \q{cons}.
Where a parameterized type has one type argument, the argument may be written without parentheses.
\begin{aside}
Excepting if the argument type is itself a tuple; in which case two parentheses will be needed.
\end{aside}
If the type name has two or more arguments, then the type arguments are enclosed in parentheses and separated by commas.
A parameterized type has a \emph{type arity} -- the number of type arguments it expects. This is defined when the type itself is defined. It is an error to write a type expression involving an incorrect number of type arguments.
Parameterized types may be defined using a \ntRef{TypeDefinition} statement.
\subsubsection{Variable Type Constructors}
\label{variableConstructor}
\index{type!variable constructor}
\index{type constructor expression}
A type expression of the form:
\begin{lstlisting}[mathescape=true]
%c of (%t$\sub1\sequence{,}$%t$\subn$)
\end{lstlisting}
denotes a rather special form of type: a type constructor expression. Like other parameterized type expressions, this expression does not denote a single type; but a set of types. For example, the type expression:
\begin{lstlisting}
%c of integer
\end{lstlisting}
denotes a type `something of \q{integer}'.
A subsequent constraint on \q{\pcent{}c} may cause it to be bound to the \ntRef{TypeConstructor} \q{cons} (say), in which case the type expression becomes ground to the parameterized type expression `\q{cons of integer}'.
Such type expressions are of most use in certain forms of \ntRef{Contract} where the contract is about a certain form of parameterized type.
\begin{aside}
Unlike parameterized type expressions, it is not possible to `define' a variable type constructor type. I.e., while we can define the \q{cons} type (see above) with a \ntRef{TypeDefinition} statement, we cannot define the type \q{\pcent{}c of integer} with an analogous statement.
\end{aside}
\begin{aside}
A variable constructor is equivalent to a regular type variable with a \ntRef{HasKind} constraint. I.e.,
\begin{lstlisting}[mathescape=true]
%c of (%t$\sub1\sequence{,}$%t$\subn$)
\end{lstlisting}
is equivalent to:
\begin{lstlisting}[mathescape=true]
%c of (%t$\sub1\sequence{,}$%t$\subn$) where %c has kind type of (type$\sequence{,}$type)
\end{lstlisting}
A \q{\pcent{}c} appearing on its own is assumed to have unknown arity, compatible with
\begin{lstlisting}
c where c has kind type
\end{lstlisting}
in cases where the type variable \q{c} is explicitly bound by a quantifier.
This is described more fully in Section~\vref{hasKindConstraint}.
\end{aside}
\subsection{Tuple Types}
\label{tupleType}
A tuple type is a tuple of types; written as a sequence of type expressions enclosed in parentheses.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{TupleType}&\arrow&\q{()}\\
&\choice&\q{((}\ntRef{Type}\q{))}\\
&\choice&\q{(}\ntRef{Type}\sequence{,}\ntRef{Type}\,\q{)}\plustwo
\end{eqnarray*}
\caption{Tuple Type}
\label{tupleTypeFig}
\end{figure}
A tuple type denotes a fixed grouping of elements. Each element of the tuple may have a different type.
There are two special cases in \ntRef{TupleType}s: the empty tuple and the singleton tuple type.
\subsubsection{Empty Tuple}
\index{tuple!empty tuple type}
The empty tuple type:
\begin{lstlisting}
()
\end{lstlisting}
refers to the empty tuple. It is useful primarily for writing function types where the function has no arguments:
\begin{lstlisting}
()=>string
\end{lstlisting}
When used as the return type of a function, the \q{()} type denotes a void result:
\begin{lstlisting}
(integer)=>()
\end{lstlisting}
\begin{aside}
The \q{()} type -- sometimes referred to as the \emph{unit type} -- is also used to denote the return type of some actions.
\end{aside}
\subsubsection{Singleton Tuple}
\index{tuple!singleton tuple type}
A singleton tuple must be written with two parentheses. This is to disambiguate such terms from simple expression parentheses. A type expression of the form:
\begin{lstlisting}
(integer)
\end{lstlisting}
is equivalent to just the \q{integer} type; whereas
\begin{lstlisting}
((integer))
\end{lstlisting}
denotes the single element tuple type whose element type is \q{integer}.
\subsection{Record Type}
\label{recordType}
A \ntRef{RecordType} is a type expression that denotes a named association of fields and types. A record type is written as a sequence of type annotations enclosed in braces.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{RecordType}&\arrow&\q{\{}\,\ntRef{Annotation}\sequence{;}\ntRef{Annotation}\ \q{\}}\\
\ntDef{TypeEquality}&\arrow&\q{type}\ \ntRef{Identifier}\ \q{=}\ \ntRef{Type}
\end{eqnarray*}
\caption{Record Type}
\label{recordTypeFig}
\end{figure}
Record types are used as the type of anonymous records (see Section~\vref{anonRecord}). They are also the basis of other features of the type language -- including the \ntRef{ConstructorType} and \ntRef{Contract}s.
Two record types are equivalent if their elements are pair-wise equivalent. Note that the \emph{order} of elements is not important. For example, given the types:
\begin{lstlisting}
{a has type string ; b has type integer }
\end{lstlisting}
and
\begin{lstlisting}
{b has type integer ; a has type %t }
\end{lstlisting}
these types unify, provided that \q{\pcent{}t} is unifiable with \q{string}.
\begin{aside}
\begin{aside}
All user-defined types -- i.e., types defined by an \ntRef{AlgebraicType} definition -- have a \ntRef{RecordType} interface associated with them. This, as is detailed in Section~\vref{algebraicInterface}, defines a type for all of the fields in any of the constructors for the type. In turn, this permits a \ntRef{RecordAccess} expression to apply to a user-defined type as well as a \ntRef{RecordType}.
\end{aside}
\end{aside}
\subsection{Function Type}
\label{functionType}
\index{function type}
\index{type!function}
A function type denotes a function value. It takes the form of a possibly empty sequence of argument types -- denoting the types of the arguments to the function -- enclosed in parentheses; followed by the result type of the function. Figure~\vref{functionTypeFig} highlights the form of the function type.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{FunctionType}&\arrow&\q{(}\,\ntRef{Type}\sequence{,}\ntRef{Type}\ \q{) =>}\ \ntRef{Type}
\end{eqnarray*}
\caption{Function Type}
\label{functionTypeFig}
\end{figure}
For example, a function of two arguments -- an \q{integer} and a \q{string} that returns a list of \q{string}s has a type that takes the form:
\begin{lstlisting}
(integer,string) => cons of string
\end{lstlisting}
\subsubsection{Procedure Type}
\label{procedureType}
\index{procedure type}
\index{type!procedure}
A procedure is an abstraction of an action. I.e., a procedure is a function that does not return a value but is executed purely for its side effect(s). This is expressed in the form of procedure types, which take the form of a function type that returns an empty tuple:
\begin{lstlisting}[mathescape=true]
($\ntRef{Type}\sub1\sequence{,}\ntRef{Type}\subn$)=>()
\end{lstlisting}
For example, a procedure that takes \q{string} and \q{integer} arguments would have the type signature:
\begin{lstlisting}
(string,integer)=>()
\end{lstlisting}
And the type:
\begin{lstlisting}
()=>()
\end{lstlisting}
denotes the type of a procedure that takes no arguments.
\subsection{Pattern Abstraction Type}
A \ntRef{PatternAbstraction} is an abstraction of a pattern. Pattern abstractions allow patterns to be treated as first class values -- i.e., passed in as arguments to programs and bound to variables -- and they may be applied in contexts where patterns are valid.
The form of a pattern abstraction type is defined in Figure~\vref{patternAbTypeFig}.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{PatternType}&\arrow&\q{(}\,\ntRef{Type}\sequence{,}\ntRef{Type}\ \q{) <= }\ntRef{Type}
\end{eqnarray*}
\caption{Pattern Type}
\label{patternAbTypeFig}
\end{figure}
Pattern abstractions match a pattern, and `extract' values from that pattern; values that, in turn, may be matched against where the pattern abstraction is applied.
For example, a \ntRef{PatternAbstraction} that matches \q{string}s that are intended to denote \q{integer} literals, and extracts such an \q{integer} would have the type
\begin{lstlisting}
(integer) <= string
\end{lstlisting}
\subsection{Constructor Type}
\label{constructorType}
\index{constructor type}
\index{type!constructor}
A constructor is a special function that is introduced in an \ntRef{AlgebraicType} definition.
\begin{aside}
Constructors are special because they can be viewed simultaneously as a function and as a pattern. Hence the form of the constructor reflects that bidirectionality.
\end{aside}
The form of a constructor type is given in Figure~\vref{constructorTypeFig}.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{ConstructorType}&\arrow&\ntRef{Type}\ \q{<=>}\ \ntRef{Type}
\end{eqnarray*}
\caption{Constructor Type}
\label{constructorTypeFig}
\end{figure}
The left hand side of a constructor type should either be a \ntRef{TupleType} or an \ntRef{RecordType} -- depending on whether the denoted constructor is a labeled tuple constructor or a record constructor.
\begin{aside}
\ntRef{ConstructorType}s are most used in the context of the signatures of \emph{abstract data types}: where a type and its constructors are `exported' from a record.
\end{aside}
\subsection{Reference Type}
\label{referenceType}
\index{reference type}
\index{type!ref@\q{ref}}
A re-assignable variable is given a \q{ref}erence type.
\begin{figure}[H]
\begin{eqnarray*}
\ntDef{ReferenceType}&\arrow&\q{ref}\ \ntRef{Type}
\end{eqnarray*}
\caption{Reference Type}
\label{referenceTypeFig}
\end{figure}
Reference types allow the programmer to distinguish re-assignable variables from other values; in particular they allow one to distinguish between binding to the \emph{value} of a re-assignable variable or to its \emph{name}.
\begin{aside}
The latter is not as common, but is important to support abstractions involving re-assignable variables.
\end{aside}
\subsection{Type Variables}
\label{typeVariable}
\index{type!variable}
A type variable is a variable which may be bound to a type. Depending on whether the scope of a type variable is explicitly determined or implicitly determined, type variables may be written as regular identifiers or as identifiers prefixed by a \q{\pcent{}} or \q{\pcent{}\pcent{}} mark.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{TypeVariable}&\arrow&\q{\pcent{}}\,\ntRef{Identifier}\\
&\choice&\ntRef{Identifier}
\end{eqnarray*}
\caption{Type Variables}
\label{typeVariableFig}
\end{figure}
\subsubsection{Type Variable Kind}
Type variables are associated with a \ntRef{Kind} -- which constrains the kinds (sic) of types that the type variables may be bound to. For example, a \ntRef{Kind} of \q{type} implies that the type variable may be bound to any valid type -- but may not be bound to a \ntRef{TypeConstructor}.
A type variable introduced using the \q{\pcent{}} notation has an implicit \ntRef{Kind} of \q{type}.
\begin{aside}
The different kinds of type variable may not be mixed: it is not permissible to bind a type variable to a \ntRef{TypeConstructor}, and vice versa.
For example, given:
\begin{lstlisting}
type cons of t is nil or cons(t, cons of t);
\end{lstlisting}
A type variable \q{\pcent{}s} may be bound to a type expression such as \q{cons of string}.
\end{aside}
\subsubsection{Scope of Type Variables}
\label{typeVarScope}
\index{type!variable!scope}
All type variables have a scope which generally follows the scoping rules for normal variables.
There are two particular cases that are important: type variables introduced via \ntRef{TypeDefinition}s and those introduced via explicitly quantified type expressions.
A variable introduced in the head of an \ntRef{AlgebraicType} definition, or in the head of a \ntRef{Contract} definition are in scope throughout the definition or contract respectively.
\subsection{Universal Types}
\label{universalType}
\index{types!universally quantified}
\index{universally quantified type}
\index{for all@\q{for all}}
A universal type denotes a type that is valid for all substitutions of a type variable.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{UniversalType}&\arrow&\q{for all}\ \ntRef{TypeVariable}\sequence{,}\ntRef{TypeVariable}\ \q{such that}\ \ntRef{Type}
\end{eqnarray*}
\caption{Universal Type Expression}
\label{universalTypeFig}
\end{figure}
In most situations, it is not necessary to explicitly annotate a type as universal. For example, universal types are automatically inferred for function definitions when they are determined to be parameterized.
One case where explicit universal quantification is necessary is when a function or other program element requires a function argument which is itself parameterized,\footnote{This can happen if function-valued argument to a function is going to be used in different situations within the function then that argument needs to explicitly marked as universal.} then the argument type must be explicitly marked as universal -- the type system cannot infer such usages.
For example, the \q{dblFilter} function in Program~\vref{dblFilter} applies a \q{map} function in two different situations -- one for each element of each pair in the input list.
\begin{program}
\begin{lstlisting}
dblFilter has type for all u,v such that
(for all t such that (t)=>t, cons of ((u,v)))=>cons of ((u,v))
fun dblFilter(M,cons of []) is cons of []
| dblFilter(M,cons of [(A,B),..L]) is
cons of [(M(A),M(B)),..dblFilter(M,L)]
\end{lstlisting}
\caption{A \q{double} filter}\label{dblFilter}
\end{program}
Without the explicit type annotation for \q{M}, type inference will infer that the type of \q{A} is the same as the type of \q{B} because \q{M} is applied to both.
It is important to note that any actual function argument supplied to \q{dblFilter} will itself have to be generic -- i.e., its type will also be universally quantified.
\begin{aside}
It not not necessary to use the \q{\pcent{}} prefix for type variables that are explicitly bound by a quantifier.
\end{aside}
\subsection{Existential Types}
\label{existentialType}
\index{types!existentially quantified}
\index{existentially quantified type}
\index{exists\q{exists}}
An existential type denotes an \emph{abstract} type.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{ExistentialType}&\arrow&\q{exists}\ \ntRef{TypeVariable}\sequence{,}\ntRef{TypeVariable}\ \q{such that}\ \ntRef{Type}
\end{eqnarray*}
\caption{Existential Type Expression}
\label{existentialTypeFig}
\end{figure}
An existentially quantified type denotes a type within which there is an \emph{abstract type}: i.e., the type exists but the expression is not explicit about which type.
Existential types are most often used in the type signatures of abstract data types. For example, the term in the statement:
\begin{lstlisting}
def R is {
type integer counts as el;
fun op(X,Y) is X+Y
}
\end{lstlisting}
has type:
\begin{lstlisting}
exists el such that { el has kind type; op has type (el,el)=>el }
\end{lstlisting}
\begin{aside}
Note that the fact that within the record the type \q{el} is identified as \q{integer} does not escape the record itself. Externally, the existence of the type is known but not what it is.
It is permissible to refer to the type within the record by a dot reference.
\end{aside}
\begin{aside}
Existentially quantified types are generally not inferred for variables: i.e., if a variable has an existential type then that must be explicitly annotated.
Existential types are inferred, however, for \ntRef{Record}s that contain a \ntRef{TypeDefinition} statement.
\end{aside}
\subsubsection{Encapsulated Types}
\label{encapsulatedType}
\index{encapsulated type}
\index{type!encapsulated in record}
\index{existential type}
\index{heterogenous types}
An \ntRef{EncapsulatedType} is a reference to a type that is embedded within a record.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{EncapsulatedType}&\arrow&\ntRef{Identifier}\sequence{\q{.}}\ntRef{Identifier}
\end{eqnarray*}
\caption{Encapsulated Type}
\label{encapsulatedTypeFig}
\end{figure}
As noted in Section~\vref{existentialType}, record literals may have types embedded within them. Such a record type is existentially quantified.
It is possible to access the type embedded within such a record -- albeit with some restrictions:
\begin{itemize}
\item The form of an \ntRef{EncapsulatedType} reference is limited to terms of the form:
\begin{lstlisting}
R.t
\end{lstlisting}
where \q{R} is a \ntRef{Variable} whose type interface contains the type \q{t}.
More generally, an \ntRef{EncapsulatedType} reference may involve a sequence of field names where each intermediate field name refers to a sub-record:
\begin{lstlisting}
R.f1.f2.t
\end{lstlisting}
\item The `value' of an encapsulated type is strictly opaque: it is assumed to be different to all other types. Which means that effectively \emph{only} the other fields of the record variable \q{R} contain functions and values that can be used in conjunction.
\end{itemize}
For example, consider the \q{group} type defined in Program~\vref{groupExample}.
\begin{program}
\begin{lstlisting}
type group is group{
el has kind type where equality over el
zero has type el
op has type (el,el)=>el
inv has type (el)=>el
}
\end{lstlisting}
\caption{The \q{group} Type}
\label{groupExample}
\end{program}
\begin{aside}
A \q{group} literal is analogous to a mathematical group: a set which is closed under a binary operation and whose elements have an inverse.
The contents of a \q{group} literal contain the definitions of the elements, the binary operation, the zero element and the inverse function.
\end{aside}
\begin{aside}
The qualification of the \q{el} type that it supports \q{equality} allows convenient access to equality of group elements. Without such a qualification, equality would not be possible for programs using \q{group} values.
\end{aside}
\begin{aside}
An additional requirement for a group is that its operation is associative. Such a property cannot be expressed in terms of type constraints.
\end{aside}
A \q{group} literal that implements the group for \q{integer}s is shown in Program~\vref{integerGroup}.
\begin{program}
\begin{lstlisting}
def IG is group{
type integer counts as el
def zero is 0
def op is (+)
fun inv(X) is -X
}
\end{lstlisting}
\caption{The \q{integer} \q{group} Record}
\label{integerGroup}
\end{program}
The \q{IG} value contains the elements of a group value. We can, for example, access the \q{zero} of \q{IG} using the statement:
\begin{lstlisting}
def IZ is IG.zero
\end{lstlisting}
If we wanted to explicitly declare the type of \q{IZ}, then we could use:
\begin{lstlisting}
IZ has type IG.el
\end{lstlisting}
This asserts that \q{IZ}'s type is whatever the encapsulated type within \q{IG} is.
It is possible to construct functions over \q{group}s that refer to encapsulated types. For example, the \q{invertGroup} function in Program~\vref{invertGroupProgram} constructs a new group by `inverting' the operation.
\begin{program}
\begin{lstlisting}
invertGroup has type (group)=>group
fun invertGroup(G) is group{
type G.el counts as el
def zero is G.zero
fun op(X,Y) is G.op(G.inv(X),G.inv(Y))
fun inv(X) is G.inv(X)
}
\end{lstlisting}
\caption{A \q{group} Inverting Function}
\label{invertGroupProgram}
\end{program}
\section{Type Constraints}
\label{typeConstraints}
\index{type!constraints}
A \ntRef{TypeConstraint} is a constraint on a \ntRef{Type}; usually implying a constraint on the possible binding of a \ntRef{TypeVariable}.
\begin{aside}
Even though they primarily affect \ntRef{TypeVariable}s, \ntRef{TypeConstraint}s are attached `on the end' of the type expression that references the constraint.
\end{aside}
Generally, a \ntRef{TypeConstraint} on a \ntRef{TypeVariable} restricts in some sense the possible bindings for that type variable. For example, a \ntRef{Contract} refers to a named collection of functions and a \ntRef{TypeVariable} constrained by a \ntRef{ContractConstraint} means that any concrete instantiation of the \ntRef{TypeVariable} must be to a \ntRef{Type} that \q{implement}s the \ntRef{Contract}.
Similarly, a \ntRef{FieldConstraint} constrains the \ntRef{TypeVariable} so that any binding must be to a \ntRef{Type} that has the named field in its definition.
For example, using \q{arithmetic} as a constraint allows us to say `the type can be anything that implements a form of arithmetic'. The type expression:
\begin{lstlisting}
%t where arithmetic over %t
\end{lstlisting}
denotes this kind of constrained type.
\begin{aside}
\begin{aside}
It is possible to view a type variable binding itself as a form of constraint: if we bind the type variable \q{\pcent{}t} to the type \q{integer} then we are constraining the type \q{\pcent{}t} to be equal to \q{integer}.
\end{aside}\end{aside}
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{TypeConstraint}&\arrow&\ntRef{ContractConstraint}\\
&\choice&\ntRef{FieldConstraint}\\
&\choice&\ntRef{InstanceConstraint}\\
&\choice&\ntRef{HasKindConstraint}\\
&\choice&\ntRef{TupleConstraint}\\
&\choice&\ntRef{TypeConstraint}\ \q{and}\ \ntRef{TypeConstraint}
\end{eqnarray*}
\caption{Type Constraints}
\label{typeConstraintFig}
\end{figure}
A type expression of the form:
\begin{lstlisting}
(%t)=>%t where comparable over %t and arithmetic over %t
\end{lstlisting}
denotes a unary function type for any type that implements both the \q{comparable} and the \q{arithmetic} contracts (see Sections~\vref{comparisonPredicates} and \vref{arithmeticContract}).
\begin{aside}
In many cases type inference will automatically result in constraints being added to type expressions.
\end{aside}
It is possible mix different forms of \ntRef{TypeConstraint}; for example, if a \ntRef{TypeVariable} must be bound to a type that implements the \q{comparable} contract as well as having the \q{integer}-typed \q{ident} attribute, the type expression:
\begin{lstlisting}
comparable over %t and %t implements { ident has type integer }
\end{lstlisting}
captures this.
\begin{aside}
If a constrained type variable is unified with another type variable, then the constraints of the two variables are merged. It may be that such a merging of constraints is not possible; in such a case, the unification will fail.
\end{aside}
\subsection{Contract Constraints}
\label{contractContraint}
\index{type!constraints!contract}
\index{contract constraint}
A \ntRef{ContractConstraint} is a requirement on a \ntRef{Type} -- or tuple of \ntRef{Type}s -- that whatever type it is, that there must exist an \q{implementation} of the \ntRef{Contract} for the \ntRef{Type} (see Section~\vref{contracts}).
For example, the type constraint expression:
\begin{lstlisting}
comparable over %t
\end{lstlisting}
means that the type variable \q{\pcent{}t} may only unify with concrete types that implement the \q{comparable} contract.
\begin{aside}
If \q{\pcent{}t} is unified with another type variable, then the constraints on both type variables are \emph{merged}.
\end{aside}
\begin{aside}
Since only named types may implement \ntRef{Contract}s, it is also not permissible to unify the constrained variable with an structural type -- such as a function type.
\end{aside}
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{ContractConstraint}&\arrow&\ntRef{Identifier}\ \q{over}\ \ntRef{TypeArgument}\ [\,\q{determines}\ \ntRef{TypeArgument}\,]
\end{eqnarray*}
\caption{Contract Constraint}
\label{contractConstraintFig}
\end{figure}
It is possible for \ntRef{ContractConstraint}s to reference more than one type. For example, the standard \q{coercion} contract (see Program~\vref{typeCoercionContractFig}) references two types. A \q{coercion} \ntRef{ContractConstraint} will therefore look like:
\begin{lstlisting}[mathescape=true]
coercion over ($\emph{srcType}$, $\emph{dstType}$)
\end{lstlisting}
where \q{\emph{srcType}} represents the `source' type of the coercion and \q{\emph{dstType}} represents the `destination' type.
If the \q{determines} clause is used, then the \ntRef{Contract} being referenced must have a \emph{functional dependency}
\index{functional dependency}
associated with it.
\begin{aside}
Conversely, if a \ntRef{Contract} has a functional dependency, then any constraint referring to it must also have a \q{determines} clause.
\end{aside}
The \q{determines} clause identifies which type(s) are `dependent' on the type argument(s) of the \ntRef{Contract}. (See Section~\vref{ContractFunctionalDependency}).
\subsection{Field Constraints}
\label{attributeConstraint}
\index{type!field}
\index{type!constraints!field}
A \emph{FieldConstraint} is a requirement on a variable that whatever type it is, it should have particular attributes of particular types defined for it.
\begin{figure}[H]
\begin{eqnarray*}
\ntDef{FieldConstraint}&\arrow&\ntRef{Type}\ \q{implements}\ \{\ntRef{TypeAnnotation}\sequence{;}\ntRef{Annotation}\}
\end{eqnarray*}
\caption{Field Constraint}
\label{attributeConstraintFig}
\end{figure}
For example, in
\begin{lstlisting}
%r implements { alpha has type string; beta has type long }
\end{lstlisting}
if \q{\pcent{}r} is unified against a concrete type then that type's \ntRef{RecordType} interface (see Section~\vref{algebraicInterface}) must contain both of \q{alpha} and \q{beta}. In addition, the fields must be of the right types.
\begin{aside}
It is also possible to require that an \ntRef{EncapsulatedType} exists. For example, the constraint:
\begin{lstlisting}
s implements { elem has kind type }
\end{lstlisting}
requires that any actual binding for type \q{s} must include the embedded type \q{elem}.
\end{aside}
\subsection{Instance Constraint}
\label{instanceConstraint}
\index{type!constraints!instance}
An \ntRef{InstanceConstraint} is a requirement on a variable that any instantiation of the variable is an `instance of' a type -- typically that is a universally quantified type.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{InstanceConstraint}&\arrow&\ntRef{TypeVar}\ \q{instance of}\ \ntRef{Type}
\end{eqnarray*}
\caption{Instance Type Constraint}
\label{instanceConstraintFig}
\end{figure}
For example, in
\begin{lstlisting}
%r instance of (for all t such that (t)=>t)
\end{lstlisting}
we establish a constraint on \q{\pcent{}r} that any binding of \q{\pcent{}r} must be some specialization of the function type:
\begin{lstlisting}
for all t such that (t)=>t
\end{lstlisting}
Note that this would permit, for example, \q{\pcent{}r} to be bound to the \q{integer} function type:
\begin{lstlisting}
(integer)=>integer
\end{lstlisting}
because this type is an instance of the \ntRef{UniversalType}.
\begin{aside}
\ntRef{InstanceConstraint}s typically arise from type inference itself; rather than being deliberately written by the programmer.
\end{aside}
\subsection{Has Kind Constraint}
\label{hasKindConstraint}
\index{type!constraints!has kind}
An \ntRef{HasKindConstraint} is a requirement on a variable that any instantiation of the variable `has the right kind'.
The kind of a type refers to whether the type is a regular type or a type constructor. It also encodes the expected number of type arguments -- in the case that the variable should be bound to a type constructor.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{HasKindConstraint}&\arrow&\ntRef{TypeVar}\ \q{has kind}\ \ntRef{Kind}
\end{eqnarray*}
\caption{Has Kind Type Constraint}
\label{hasKindConstraintFig}
\end{figure}
For example, in
\begin{lstlisting}
%c has kind type
\end{lstlisting}
we establish a constraint on \q{\pcent{}c} that any binding of \q{\pcent{}c} must be a \ntRef{Type} (in particular, it may not be bound to a type constructor.
The constraint:
\begin{lstlisting}
%d has kind type of (type,type)
\end{lstlisting}
establishes the constraint that \q{\pcent{}d} must be bound to a type constructor (\emph{not} a \ntRef{Type}) or arity two. Given this constraint, it would not be legal to bind \q{\pcent{}d} to the standard type constructor \q{cons} (say) -- because \q{cons} is a type constructor of one argument.
\subsection{Tuple Constraint}
\label{tupleConstraint}
\index{type!constraints!tuple}
A \emph{TupleConstraint} is a requirement on a variable that it is a tuple type.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{TupleConstraint}&\arrow&\ntRef{Type}\ \q{is tuple}
\end{eqnarray*}
\caption{Tuple Type Constraint}
\label{tupleConstraintFig}
\end{figure}
For example, in
\begin{lstlisting}
%r is tuple
\end{lstlisting}
if \q{\pcent{}r} is unified against a concrete type then that type must be a \ntRef{TupleType}.
\section{Type Annotations}
\label{typeAnnotation}
An \ntRef{Annotation} is a statement that declares a variable to have a certain \ntRef{Type} or a \ntRef{Type} to have a certain \ntRef{Kind}.
For example,
\begin{lstlisting}
alpha has type for all t such that (t)=>string
\end{lstlisting}
is a \ntRef{TypeAnnotation}, whereas
\begin{lstlisting}
el has kind type
\end{lstlisting}
is a \ntRef{KindAnnotation}.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{Annotation}&\arrow&\ntRef{TypeAnnotation}\ \choice\ \ntRef{KindAnnotation}\\
\ntDef{TypeAnnotation}&\arrow&\ntRef{Identifier}\ \q{has}\ \q{type}\ \ntRef{Type}\\
\ntDef{KindAnnotation}&\arrow&\ntRef{Identifier}\ \q{has}\ \q{kind}\ \ntRef{Kind}\ [\,\q{where}\ \ntRef{TypeConstraint}\,]\\
\ntDef{Kind}&\arrow&\q{type}\\
&\choice&\q{type of type}\\
&\choice&\q{type of (type}\sequence{,}\q{type)}
\end{eqnarray*}
\caption{Type Annotations}
\label{typeAnnotationFig}
\end{figure}
\section{Type Definitions}
\label{typeDefinitions}
\index{type!definition}
A type definition is a statement that introduces a new type into the current scope. There are two forms of type definition statement: the \ntRef{TypeAlias} definition and the \ntRef{AlgebraicType} definition. In addition, the \ntRef{TypeWitness} is used to `declare' a type.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{TypeDefinition}&\arrow&\ntRef{TypeAlias}\ \choice\ \ntRef{AlgebraicType}\ \choice\ \ntRef{TypeWitness}
\end{eqnarray*}
\caption{Type Definition Statements}
\label{typeDefinitionFig}
\end{figure}
\subsection{Type Alias}
\label{typeAlias}
\index{type!alias}
A type alias is a statement that introduces a new type name by mapping it to an existing type expression.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{TypeAlias}&\arrow&\q{type}\ \ntRef{TypeSpec}\ \q{is}\ \q{alias}\ \q{of}\ \ntRef{Type}
\end{eqnarray*}
\caption{Type Alias Definition Statement}
\label{typeAliasDefinitionFig}
\end{figure}
\begin{aside}
Type aliases may be parameterized -- in the sense that the type being defined may be parameterized and that the definiens may also be parameterized.
Note that the any type variables on the right hand side of a \ntRef{TypeAlias} statement must also have been mentioned on the left hand side.
\end{aside}
For example, the statement:
\begin{lstlisting}
type time is alias of integer
\end{lstlisting}
declares a new type that is an alias for \q{time} -- i.e., that it is actually equivalent to the \q{integer} type.
\begin{aside}
Type aliases allow the programmer to signal that a particular type is being used in a special way. In addition, during program development, type aliases are useful to provide markers for types that will be elaborated further with a regular algebraic definition.
\end{aside}
\begin{aside}
Type aliases have no run-time presence. In fact, they may be viewed as a simple form of type macro -- type expressions that match the left hand side are replaced by the type expression on the right hand side. However, type aliases have some definite constraints: a type alias may not be, directly or indirectly, recursive.
\end{aside}
\subsection{Algebraic Type Definitions}
\label{algebraicTypeDefinitions}
An algebraic type definition is a statement that introduces a new type; it also defines the possible values associated with the type.
As illustrated in Figure~\vref{algebraicDefinitionFig}, an algebraic type definition introduces the new type and defines one or more \ntRef{Constructor}s -- separated by the \q{or} keyword.
A \ntRef{Constructor} is a specification of a value of a type; i.e., constructors `paint a picture' of the shape of potential values of the type.
There are three kinds of \ntRef{Constructor}: enumerated symbols, labeled tuple constructors and labeled record constructors.
\begin{figure}[htbp]
\begin{eqnarray*}
\ntDef{AlgebraicType}&\arrow&\q{type}\ \ntRef{TypeSpec}\ \q{is}\,\ntRef{Constructor}\,\sequence{or}\,\ntRef{Constructor}\\
\ntDef{TypeSpec}&\arrow&\ntRef{Identifier}\ [\,\q{of}\ \ntRef{TypeArgSpec}\,]\\
\ntDef{TypeArgSpec}&\arrow&\ntRef{TypeVariable}\\
&\choice&\q{(}\,\ntRef{TypeVariable}\sequence{,}\ntRef{TypeVariable}\q{)}\\
&\choice&\ntRef{TypeArgSpec}\ \q{where}\ \ntRef{TypeConstraint}\\
\ntDef{Constructor}&\arrow&\ntRef{EnumeratedSymbol}\\
&\choice&\ntRef{LabeledTuple}\\
&\choice&\ntRef{RecordConstructor}
\end{eqnarray*}
\caption{Algebraic Type Definition Statement}
\label{algebraicDefinitionFig}
\end{figure}
\begin{aside}
Most standard built-in types have type-specific constructors. For example, lists have a list notation, \q{dictionary}s have a dictionary notation and so on. Such constructors may not be defined using the algebraic type definition notation -- for example, the constructors for the \q{integer} type are denoted by the normal decimal notation for integers.
\end{aside}
\begin{aside}
As elaborated below, each `arm' of an algebraic type definition defines a value or set of values that belong to the type. There is a slightly more formal way of expressing this: an algebraic type definition induces a set of free functions.
\begin{aside}
\index{constructor!bijection}
Free functions are technically bijections -- they are one-to-one -- i.e., they have inverses. In programming languages, free functions are used as data structuring tools; but mathematically they are functions.
\end{aside}
For example, the type definition:
\begin{lstlisting}
type person is someone(string,integer)
or noone
\end{lstlisting}
induces the constructor function for \q{someone}:
\begin{lstlisting}
someone has type (string,integer) <=> person;
\end{lstlisting}
The enumerated symbol has a simpler type:
\begin{lstlisting}
noone has type person;
\end{lstlisting}
The complete set of constructor functions introduced within an algebraic type definition is complete: i.e., they define all the possible values of the type.
\end{aside}
\begin{aside}
A given label, whether it is used as an \ntRef{EnumeratedSymbol}, the label of a \ntRef{LabeledType} or a \ntRef{LabeledRecord} can be defined only once. I.e., it is not permitted to `share' constructor labels across different types.
\end{aside}
\subsubsection{Enumerated Symbol}
\label{enumSymbol}
\index{constructor!enumerated symbol}
\index{enumerated symbol}
\index{type!enumerated}
An enumerated symbol is written as an identifier. The fact that an identifier has been mentioned in a type definition is sufficient to `mark' it as a value -- and not as a variable for example.
\begin{figure}[htbp]