1 | \chapter{Related Work}
|
---|
2 | \label{s:RelatedWork}
|
---|
3 |
|
---|
4 | \begin{comment}
|
---|
5 | An algebraic data type (ADT) can be viewed as a recursive sum of product types.
|
---|
6 | A sum type lists values as members.
|
---|
7 | A member in a sum type definition is known as a data constructor.
|
---|
8 | For example, C supports sum types union and enumeration (enum).
|
---|
9 | An enumeration in C can be viewed as the creation of a list of zero-arity data constructors.
|
---|
10 | A union instance holds a value of one of its member types.
|
---|
11 | Defining a union does not generate new constructors.
|
---|
12 | The definition of member types and their constructors are from the outer lexical scope.
|
---|
13 |
|
---|
14 | In general, an \newterm{algebraic data type} (ADT) is a composite type, \ie, a type formed by combining other types.
|
---|
15 | Three common classes of algebraic types are \newterm{array type}, \ie homogeneous types, \newterm{product type}, \ie heterogeneous tuples and records (structures), and \newterm{sum type}, \ie tagged product-types (unions).
|
---|
16 | Enumerated types are a special case of product/sum types with non-mutable fields, \ie initialized (constructed) once at the type's declaration, possible restricted to compile-time initialization.
|
---|
17 | Values of algebraic types are access by subscripting, field qualification, or type (pattern) matching.
|
---|
18 | \end{comment}
|
---|
19 |
|
---|
20 | Enumeration-like features exist in many popular programming languages, both past and present, \eg Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp~\cite{Csharp}, OCaml~\cite{OCaml} \CC, Go~\cite{Go}, Haskell~\cite{Haskell}, Java~\cite{Java}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.
|
---|
21 | Among theses languages, there are a large set of overlapping features, but each language has its own unique extensions and restrictions.
|
---|
22 |
|
---|
23 | \section{Pascal}
|
---|
24 | \label{s:Pascal}
|
---|
25 |
|
---|
26 | Classic Pascal introduced the \lstinline[language=Pascal]{const} aliasing declaration binding a name to a constant literal/expression.
|
---|
27 | \begin{pascal}
|
---|
28 | const one = 0 + 1; Vowels = set of (A,E,I,O,U); NULL = NIL;
|
---|
29 | PI = 3.14159; Plus = '+'; Fred = 'Fred';
|
---|
30 | \end{pascal}
|
---|
31 | As stated, this mechanism is not an enumeration because there is no specific type (pseudo enumeration).
|
---|
32 | Hence, there is no notion of a (possibly ordered) set, modulo the \lstinline[language=pascal]{set of} type.
|
---|
33 | The type of each constant name (enumerator) is inferred from the constant-expression type.
|
---|
34 |
|
---|
35 | Free Pascal~\cite[\S~3.1.1]{FreePascal} is a modern, object-oriented version of classic Pascal, with a C-style enumeration type.
|
---|
36 | Enumerators must be assigned in ascending numerical order with a constant expression and the range can be non-consecutive.
|
---|
37 | \begin{pascal}
|
---|
38 | Type EnumType = ( one, two, three, forty @= 40@, fortyone );
|
---|
39 | \end{pascal}
|
---|
40 | Pseudo-functions @Pred@ and @Succ@ can only be used if the range is consecutive.
|
---|
41 | The underlying type is an implementation-defined integral-type large enough to hold all enumerated values; it does not have to be the smallest possible type.
|
---|
42 | The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
|
---|
43 | \begin{pascal}
|
---|
44 | Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three );
|
---|
45 | @{$\color{red}\$$PACKENUM 4}@ LargeEnum = ( BigOne, BigTwo, BigThree );
|
---|
46 | Var S : SmallEnum; { 1 byte }
|
---|
47 | L : LargeEnum; { 4 bytes}
|
---|
48 | \end{pascal}
|
---|
49 |
|
---|
50 |
|
---|
51 | \section{Ada}
|
---|
52 |
|
---|
53 | An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \newterm{literals}.\footnote{%
|
---|
54 | Ada is \emph{case-insensitive} so identifiers may appear in multiple forms and still be the same, \eg \lstinline{Mon}, \lstinline{moN}, and \lstinline{MON} (a questionable design decision).}
|
---|
55 | \begin{ada}
|
---|
56 | type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); -- literals (enumerators)
|
---|
57 | \end{ada}
|
---|
58 | Object initialization and assignment are restricted to the enumerators of this type.
|
---|
59 | While Ada enumerators are unscoped, like C, Ada enumerators are overloadable.
|
---|
60 | \begin{ada}
|
---|
61 | type RGB is ( @Red@, @Green@, Blue );
|
---|
62 | type Traffic_Light is ( @Red@, Yellow, @Green@ );
|
---|
63 | \end{ada}
|
---|
64 | Like \CFA, Ada uses a type-resolution algorithm including the left-hand side of assignmente to disambiguate among overloaded identifiers.
|
---|
65 | \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}.
|
---|
66 |
|
---|
67 | \begin{figure}
|
---|
68 | \begin{ada}
|
---|
69 | with Ada.Text_IO; use Ada.Text_IO;
|
---|
70 | procedure test is
|
---|
71 | type RGB is ( @Red@, Green, Blue );
|
---|
72 | type Traffic_Light is ( @Red@, Yellow, Green ); -- overload
|
---|
73 | procedure @Red@( Colour : RGB ) is begin -- overload
|
---|
74 | Put_Line( "Colour is " & RGB'Image( Colour ) );
|
---|
75 | end Red;
|
---|
76 | procedure @Red@( TL : Traffic_Light ) is begin -- overload
|
---|
77 | Put_Line( "Light is " & Traffic_Light'Image( TL ) );
|
---|
78 | end Red;
|
---|
79 | begin
|
---|
80 | @Red@( Blue ); -- RGB
|
---|
81 | @Red@( Yellow ); -- Traffic_Light
|
---|
82 | @Red@( @RGB'(Red)@ ); -- ambiguous without cast
|
---|
83 | end test;
|
---|
84 | \end{ada}
|
---|
85 | \caption{Ada Enumeration Overload Resolution}
|
---|
86 | \label{f:AdaEnumeration}
|
---|
87 | \end{figure}
|
---|
88 |
|
---|
89 | Enumerators without initialization are auto-initialized from left to right, starting at zero, incrementing by 1.
|
---|
90 | Enumerators with initialization must set \emph{all} enumerators in \emph{ascending} order, \ie there is no auto-initialization.
|
---|
91 | \begin{ada}
|
---|
92 | type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
|
---|
93 | for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
|
---|
94 | \end{ada}
|
---|
95 | The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of acsending enumerators.
|
---|
96 |
|
---|
97 | Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers.
|
---|
98 | \begin{ada}
|
---|
99 | @OtherRed@ : RGB renames Red;
|
---|
100 | \end{ada}
|
---|
101 | which suggests a possible \CFA extension to @typedef@.
|
---|
102 | \begin{cfa}
|
---|
103 | typedef RGB.Red OtherRed;
|
---|
104 | \end{cfa}
|
---|
105 |
|
---|
106 | There are three pairs of inverse enumeration pseudo-functions (attributes): @'Pos@ and @'Val@, @'Enum_Rep@ and @'Enum_Val@, and @'Image@ and @'Value@,
|
---|
107 | \begin{cquote}
|
---|
108 | \setlength{\tabcolsep}{15pt}
|
---|
109 | \begin{tabular}{@{}ll@{}}
|
---|
110 | \begin{ada}
|
---|
111 | RGB'Pos( Red ) = 0;
|
---|
112 | RGB'Enum_Rep( Red ) = 10;
|
---|
113 | RGB'Image( Red ) = "RED";
|
---|
114 | \end{ada}
|
---|
115 | &
|
---|
116 | \begin{ada}
|
---|
117 | RGB'Val( 0 ) = Red
|
---|
118 | RGB'Enum_Val( 10 ) = Red
|
---|
119 | RGB'Value( "Red" ) = Red
|
---|
120 | \end{ada}
|
---|
121 | \end{tabular}
|
---|
122 | \end{cquote}
|
---|
123 | These attributes are important for IO.
|
---|
124 | An enumeration type @T@ also has the following attributes: @T'First@, @T'Last@, @T'Range@, @T'Pred@, @T'Succ@, @T'Min@, and @T'Max@, producing an intuitive result based on the attribute name.
|
---|
125 |
|
---|
126 | Ada allows the enumerator label to be a character constant.
|
---|
127 | \begin{ada}
|
---|
128 | type Operator is ( '+', '-', '*', '/' );
|
---|
129 | \end{ada}
|
---|
130 | which is syntactic sugar for the label and not character literals from the predefined type @Character@.
|
---|
131 | The purpose is strictly readability using character literals rather than identifiers.
|
---|
132 | \begin{ada}
|
---|
133 | Op : Operator := '+';
|
---|
134 | if Op = '+' or else Op = '-' then ... ;
|
---|
135 | elsif Op = '*' or else Op = '/' then ... ; end if;
|
---|
136 | \end{ada}
|
---|
137 | Interestingly, arrays of character enumerators can be treated as strings.
|
---|
138 | \begin{ada}
|
---|
139 | Ops : array( 0..3 ) of Operator;
|
---|
140 | Ops := @"+-*/"@; -- string assignment to array elements
|
---|
141 | Ops := "+-" @&@ "*/"; -- string concatenation and assignment
|
---|
142 | \end{ada}
|
---|
143 | Ada's @Character@ type is defined as a character enumeration across all Latin-1 characters.
|
---|
144 |
|
---|
145 | Ada's boolean type is also a special enumeration, which can be used in conditions.
|
---|
146 | \begin{ada}
|
---|
147 | type Boolean is (False, True); -- False / True not keywords
|
---|
148 | @Flag@ : Boolean;
|
---|
149 | if @Flag@ then ... -- conditional
|
---|
150 | \end{ada}
|
---|
151 | Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially a builtin type.
|
---|
152 |
|
---|
153 | Ada provides \emph{consecutive} subtyping of an enumeration using \lstinline[language=ada]{range}.
|
---|
154 | \begin{ada}
|
---|
155 | type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
|
---|
156 | subtype Weekday is Week @range Mon .. Fri@;
|
---|
157 | subtype Weekend is Week @range Sat .. Sun@;
|
---|
158 | Day : Week;
|
---|
159 | \end{ada}
|
---|
160 | Hence, the ordering of the enumerators is crucial to provide the necessary ranges.
|
---|
161 |
|
---|
162 | An enumeration type can be used in the Ada \lstinline[language=ada]{case} (all enumerators must appear or a @default@) or iterating constructs.
|
---|
163 | \begin{cquote}
|
---|
164 | \setlength{\tabcolsep}{15pt}
|
---|
165 | \begin{tabular}{@{}ll@{}}
|
---|
166 | \begin{ada}
|
---|
167 | case Day is
|
---|
168 | when @Mon .. Fri@ => ... ;
|
---|
169 | when @Sat .. Sun@ => ... ;
|
---|
170 | end case;
|
---|
171 | \end{ada}
|
---|
172 | &
|
---|
173 | \begin{ada}
|
---|
174 | case Day is
|
---|
175 | when @Weekday@ => ... ; -- subtype ranges
|
---|
176 | when @Weekend@ => ... ;
|
---|
177 | end case;
|
---|
178 | \end{ada}
|
---|
179 | \end{tabular}
|
---|
180 | \end{cquote}
|
---|
181 |
|
---|
182 | \begin{cquote}
|
---|
183 | \setlength{\tabcolsep}{12pt}
|
---|
184 | \begin{tabular}{@{}lll@{}}
|
---|
185 | \begin{ada}
|
---|
186 | for Day in @Mon .. Sun@ loop
|
---|
187 | ...
|
---|
188 | end loop;
|
---|
189 | \end{ada}
|
---|
190 | &
|
---|
191 | \begin{ada}
|
---|
192 | for Day in @Weekday@ loop
|
---|
193 | ...
|
---|
194 | end loop;
|
---|
195 | \end{ada}
|
---|
196 | &
|
---|
197 | \begin{ada}
|
---|
198 | for Day in @Weekend@ loop
|
---|
199 | ...
|
---|
200 | end loop;
|
---|
201 | \end{ada}
|
---|
202 | \end{tabular}
|
---|
203 | \end{cquote}
|
---|
204 |
|
---|
205 | An enumeration type can be used as an array dimension and subscript.
|
---|
206 | \begin{ada}
|
---|
207 | Lunch : array( @Week@ ) of Time;
|
---|
208 | for Day in Week loop
|
---|
209 | Lunch( @Day@ ) := ... ; -- set lunch time
|
---|
210 | end loop;
|
---|
211 | \end{ada}
|
---|
212 |
|
---|
213 |
|
---|
214 | \section{\CC}
|
---|
215 | \label{s:C++RelatedWork}
|
---|
216 |
|
---|
217 | \CC enumeration is largely backwards compatible with C, so it inherited C's enumerations with some modifications and additions.
|
---|
218 |
|
---|
219 | \CC has aliasing using @const@ declarations, like C \see{\VRef{s:Cconst}}, with type inferencing, plus static/dynamic initialization.
|
---|
220 | (Note, a \CC @constexpr@ declaration is the same as @const@ with the restriction that the initialization is a compile-time expression.)
|
---|
221 | \begin{c++}
|
---|
222 | const @auto@ one = 0 + 1; $\C{// static initialization}$
|
---|
223 | const @auto@ NIL = nullptr;
|
---|
224 | const @auto@ PI = 3.14159;
|
---|
225 | const @auto@ Plus = '+';
|
---|
226 | const @auto@ Fred = "Fred";
|
---|
227 | const @auto@ Mon = 0, Tue = Mon + 1, Wed = Tue + 1, Thu = Wed + 1, Fri = Thu + 1,
|
---|
228 | Sat = Fri + 1, Sun = Sat + 1;
|
---|
229 | void foo() {
|
---|
230 | const @auto@ r = random(); $\C{// dynamic initialization}$
|
---|
231 | int va[r]; $\C{// VLA, auto scope only}$
|
---|
232 | }
|
---|
233 | \end{c++}
|
---|
234 | Statically initialized identifiers may appear in any constant-expression context, \eg @case@.
|
---|
235 | Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays.
|
---|
236 | Interestingly, global \CC @const@ declarations are implicitly marked @static@ (@r@, read-only local, rather than @R@, read-only external)
|
---|
237 | \begin{c++}
|
---|
238 | $\$$ nm test.o
|
---|
239 | 0000000000000018 @r@ Mon
|
---|
240 | \end{c++}
|
---|
241 | whereas C @const@ declarations without @static@ are marked @R@.
|
---|
242 |
|
---|
243 | The following \CC non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:c++}}.
|
---|
244 | \begin{cquote}
|
---|
245 | Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
|
---|
246 | In C, objects of enumeration type can be assigned values of any integral type. \\
|
---|
247 | Example:
|
---|
248 | \begin{c++}
|
---|
249 | enum color { red, blue, green };
|
---|
250 | color c = 1; $\C{// valid C, invalid c++}$
|
---|
251 | \end{c++}
|
---|
252 | \textbf{Rationale}: The type-safe nature of \CC. \\
|
---|
253 | \textbf{Effect on original feature}: Deletion of semantically well-defined feature. \\
|
---|
254 | \textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\
|
---|
255 | \textbf{How widely used}: Common.
|
---|
256 | \end{cquote}
|
---|
257 |
|
---|
258 | \begin{cquote}
|
---|
259 | Change: In \CC, the type of an enumerator is its enumeration.
|
---|
260 | In C, the type of an enumerator is @int@. \\
|
---|
261 | Example:
|
---|
262 | \begin{c++}
|
---|
263 | enum e { A };
|
---|
264 | sizeof(A) == sizeof(int) $\C{// in C}$
|
---|
265 | sizeof(A) == sizeof(e) $\C{// in c++}$
|
---|
266 | /* and sizeof(int) is not necessary equal to sizeof(e) */
|
---|
267 | \end{c++}
|
---|
268 | \textbf{Rationale}: In \CC, an enumeration is a distinct type. \\
|
---|
269 | \textbf{Effect on original feature}: Change to semantics of well-defined feature. \\
|
---|
270 | \textbf{Difficulty of converting}: Semantic transformation. \\
|
---|
271 | \textbf{How widely used}: Seldom. The only time this affects existing C code is when the size of an enumerator is taken.
|
---|
272 | Taking the size of an enumerator is not a common C coding practice.
|
---|
273 | \end{cquote}
|
---|
274 | Hence, the values in a \CC enumeration can only be its enumerators (without a cast).
|
---|
275 | While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@.
|
---|
276 | \begin{c++}
|
---|
277 | enum E { A, B, C };
|
---|
278 | E e = A;
|
---|
279 | int i = A; i = e; $\C{// implicit casts to int}$
|
---|
280 | \end{c++}
|
---|
281 | \CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct})\footnote{
|
---|
282 | The use of keyword \lstinline[language=c++]{class} is resonable because default visibility is \lstinline[language=c++]{private} (scoped).
|
---|
283 | However, default visibility for \lstinline[language=c++]{struct} is \lstinline[language=c++]{public} (unscoped) making it an odd choice.},
|
---|
284 | where the enumerators are accessed using type qualification.
|
---|
285 | \begin{c++}
|
---|
286 | enum class E { A, B, C };
|
---|
287 | E e = @E::@A; $\C{// qualified enumerator}$
|
---|
288 | e = B; $\C{// error: B not in scope}$
|
---|
289 | \end{c++}
|
---|
290 | \CC{20} supports explicit unscoping with a \lstinline[language=c++]{using enum} declaration.
|
---|
291 | \begin{c++}
|
---|
292 | enum class E { A, B, C };
|
---|
293 | @using enum E;@
|
---|
294 | E e = A; e = B; $\C{// direct access}$
|
---|
295 | \end{c++}
|
---|
296 | \CC{11} added the ability to explicitly declare only an underlying \emph{integral} type for \lstinline[language=c++]{enum class}.
|
---|
297 | \begin{c++}
|
---|
298 | enum class RGB @: long@ { Red, Green, Blue };
|
---|
299 | enum class rgb @: char@ { Red = 'r', Green = 'g', Blue = 'b' };
|
---|
300 | enum class srgb @: signed char@ { Red = -1, Green = 0, Blue = 1 };
|
---|
301 | \end{c++}
|
---|
302 | There is no implicit conversion from the \lstinline[language=c++]{enum class} type to its declared type.
|
---|
303 | \begin{c++}
|
---|
304 | rgb crgb = rgb::Red;
|
---|
305 | char ch = rgb::Red; ch = crgb; $\C{// error}$
|
---|
306 | \end{c++}
|
---|
307 | An enumeration can be used in the @if@ and @switch@ statements.
|
---|
308 | \begin{cquote}
|
---|
309 | \setlength{\tabcolsep}{15pt}
|
---|
310 | \begin{tabular}{@{}ll@{}}
|
---|
311 | \begin{c++}
|
---|
312 | if ( @day@ <= Fri )
|
---|
313 | cout << "weekday" << endl;
|
---|
314 |
|
---|
315 |
|
---|
316 |
|
---|
317 |
|
---|
318 | \end{c++}
|
---|
319 | &
|
---|
320 | \begin{c++}
|
---|
321 | switch ( @day@ ) {
|
---|
322 | case Mon: case Tue: case Wed: case Thu: case Fri:
|
---|
323 | cout << "weekday" << endl; break;
|
---|
324 | case Sat: case Sun:
|
---|
325 | cout << "weekend" << endl; break;
|
---|
326 | }
|
---|
327 | \end{c++}
|
---|
328 | \end{tabular}
|
---|
329 | \end{cquote}
|
---|
330 | However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values.
|
---|
331 | \begin{c++}
|
---|
332 | enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
|
---|
333 | for ( Week d = Mon; d <= Sun; d = @(Week)(d + 1)@ ) cout << d << ' ';
|
---|
334 | 0 1 2 @3 4 5 6 7 8 9@ 10 11 12 13
|
---|
335 | \end{c++}
|
---|
336 | An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript.
|
---|
337 | There is no mechanism to subtype or inherit from an enumeration.
|
---|
338 |
|
---|
339 |
|
---|
340 | \section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE
|
---|
341 | \label{s:Csharp}
|
---|
342 |
|
---|
343 | % https://www.tutorialsteacher.com/codeeditor?cid=cs-mk8Ojx
|
---|
344 | % https://learn.microsoft.com/en-us/dotnet/api/system.enum?view=net-8.0
|
---|
345 | % https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/enums
|
---|
346 |
|
---|
347 | \Csharp is a dynamically-typed programming-language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}.
|
---|
348 | \begin{csharp}
|
---|
349 | enum Week : @long@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun@,@ } // terminating comma
|
---|
350 | enum RGB { Red, Green, Blue }
|
---|
351 | \end{csharp}
|
---|
352 | The default underlying integral type is @int@ (no @char@), with auto-incrementing, implicit/explicit initialization, and terminating comma.
|
---|
353 | A method cannot be defined in an enumeration type (extension methods are possible).
|
---|
354 | There is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts.
|
---|
355 | \begin{csharp}
|
---|
356 | int iday = (int)Week.Fri; $\C{// day == 11}$
|
---|
357 | Week day = @(Week)@42; $\C{// day == 42, unsafe}$
|
---|
358 | string mon = Week.Mon.ToString(); $\C{// mon == "Mon"}$
|
---|
359 | RGB rgb = RGB.Red; $\C{// rgb == "Red"}$
|
---|
360 | day = @(Week)@rgb; $\C{// day == "Mon", unsafe}$
|
---|
361 | Console.WriteLine( Week.Fri ); $\C{// print label Fri}$
|
---|
362 | \end{csharp}
|
---|
363 | The majority of the integral operators (relational and arithmetic) work with enumerations, except @*@ and @/@.
|
---|
364 | \begin{csharp}
|
---|
365 | day = day++ - 5; $\C{// unsafe}$
|
---|
366 | day = day & day;
|
---|
367 | \end{csharp}
|
---|
368 |
|
---|
369 | An enumeration can be used in the @if@ and @switch@ statements.
|
---|
370 | \begin{cquote}
|
---|
371 | \setlength{\tabcolsep}{15pt}
|
---|
372 | \begin{tabular}{@{}ll@{}}
|
---|
373 | \begin{csharp}
|
---|
374 | if ( @day@ <= Week.Fri )
|
---|
375 | Console.WriteLine( "weekday" );
|
---|
376 |
|
---|
377 |
|
---|
378 |
|
---|
379 |
|
---|
380 |
|
---|
381 | \end{csharp}
|
---|
382 | &
|
---|
383 | \begin{csharp}
|
---|
384 | switch ( @day@ ) {
|
---|
385 | case Week.Mon: case Week.Tue: case Week.Wed:
|
---|
386 | case Week.Thu: case Week.Fri:
|
---|
387 | Console.WriteLine( "weekday" ); break;
|
---|
388 | case Week.Sat: case Week.Sun:
|
---|
389 | Console.WriteLine( "weekend" ); break;
|
---|
390 | }
|
---|
391 | \end{csharp}
|
---|
392 | \end{tabular}
|
---|
393 | \end{cquote}
|
---|
394 | However, there is no mechanism to iterate through an enumeration without an unsafe cast to increment and positions versus values is not handled.
|
---|
395 | \begin{csharp}
|
---|
396 | for ( Week d = Mon; d <= Sun; @d += 1@ ) {
|
---|
397 | Console.Write( d + " " );
|
---|
398 | }
|
---|
399 | Mon Tue Wed @3 4 5 6 7 8 9@ Thu Fri Sat Sun
|
---|
400 | \end{csharp}
|
---|
401 | The @Enum.GetValues@ pseudo-method retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).
|
---|
402 | \begin{csharp}
|
---|
403 | foreach ( Week d in @Enum.GetValues@( typeof(Week) ) ) {
|
---|
404 | Console.WriteLine( d + " " + (int)d + " " ); // label, position
|
---|
405 | }
|
---|
406 | Mon 0, Tue 1, Wed 2, Thu 10, Fri 11, Sat 12, Sun 13,
|
---|
407 | \end{csharp}
|
---|
408 | Hence, enumerating is not supplied directly by the enumeration, but indirectly through another enumerable type, array.
|
---|
409 |
|
---|
410 | An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript.
|
---|
411 | There is no mechanism to subtype or inherit from an enumeration.
|
---|
412 |
|
---|
413 | The @Flags@ attribute creates a bit-flags enumeration, making bitwise operators @&@, @|@, @~@ (complement), @^@ (xor) sensible.
|
---|
414 | \begin{csharp}
|
---|
415 | @[Flags]@ public enum Week {
|
---|
416 | None = 0x0, Mon = 0x1, Tue = 0x2, Wed = 0x4,
|
---|
417 | Thu = 0x8, Fri = 0x10, Sat = 0x20, Sun = 0x40,
|
---|
418 | Weekdays = @Mon | Tue | Wed | Thu | Fri@ $\C{// Weekdays == 0x1f}$
|
---|
419 | Weekend = @Sat | Sun@, $\C{// Weekend == 0x60}$
|
---|
420 | }
|
---|
421 | Week meetings = @Week.Mon | Week.Wed@; $\C{// 0x5}$
|
---|
422 | \end{csharp}
|
---|
423 |
|
---|
424 |
|
---|
425 | \section{Golang}
|
---|
426 | \label{s:Golang}
|
---|
427 |
|
---|
428 | Golang has a no enumeration.
|
---|
429 | It has @const@ aliasing declarations, similar to \CC \see{\VRef{s:C++RelatedWork}}, for basic types with type inferencing and static initialization (constant expression).
|
---|
430 | \begin{Go}
|
---|
431 | const R @int@ = 0; const G @uint@ = 1; const B = 2; $\C{// explicit typing and type inferencing}$
|
---|
432 | const Fred = "Fred"; const Mary = "Mary"; const Jane = "Jane";
|
---|
433 | const S = 0; const T = 0;
|
---|
434 | const USA = "USA"; const U = "USA";
|
---|
435 | const V = 3.1; const W = 3.1;
|
---|
436 | \end{Go}
|
---|
437 | Since these declarations are unmutable variables, they are unscoped and Golang has no overloading.
|
---|
438 |
|
---|
439 | Golang provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.
|
---|
440 | \begin{Go}
|
---|
441 | const ( R = 0; G; B ) $\C{// implicit initialization: 0 0 0}$
|
---|
442 | const ( Fred = "Fred"; Mary = "Mary"; Jane = "Jane" ) $\C{// explicit initialization: Fred Mary Jane}$
|
---|
443 | const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// type change, implicit/explicit: 0 0 USA USA 3.1 3.1}$
|
---|
444 | \end{Go}
|
---|
445 | The first identifier \emph{must} be explicitly initialized;
|
---|
446 | subsequent identifiers can be implicitly or explicitly initialized.
|
---|
447 | Implicit initialization is the \emph{previous} (predecessor) identifier value.
|
---|
448 |
|
---|
449 | Each @const@ declaration provides an implicit integer counter starting at zero, called \lstinline[language=Go]{iota}.
|
---|
450 | Using \lstinline[language=Go]{iota} outside of a @const@ block always sets the identifier to zero.
|
---|
451 | \begin{Go}
|
---|
452 | const R = iota; $\C{// 0}$
|
---|
453 | \end{Go}
|
---|
454 | Inside a @const@ block, \lstinline[language=Go]{iota} is implicitly incremented for each \lstinline[language=golang]{const} identifier and used to initialize the next uninitialized identifier.
|
---|
455 | \begin{Go}
|
---|
456 | const ( R = @iota@; G; B ) $\C{// implicit: 0 1 2}$
|
---|
457 | const ( C = @iota + B + 1@; G; Y ) $\C{// implicit: 3 4 5}$
|
---|
458 | \end{Go}
|
---|
459 | An underscore \lstinline[language=golang]{const} identifier advances \lstinline[language=Go]{iota}.
|
---|
460 | \begin{Go}
|
---|
461 | const ( O1 = iota + 1; @_@; O3; @_@; O5 ) // 1, 3, 5
|
---|
462 | \end{Go}
|
---|
463 | Auto-initialization reverts from \lstinline[language=Go]{iota} to the previous value after an explicit initialization, but auto-incrementing of \lstinline[language=Go]{iota} continues.
|
---|
464 | \begin{Go}
|
---|
465 | const ( Mon = iota; Tue; Wed; // 0, 1, 2
|
---|
466 | @Thu = 10@; Fri; Sat; Sun = itoa ) // 10, 10, 10, 6
|
---|
467 | \end{Go}
|
---|
468 | Auto-initialization from \lstinline[language=Go]{iota} is restarted and \lstinline[language=Go]{iota} reinitialized with an expression containing as most \emph{one} \lstinline[language=Go]{iota}.
|
---|
469 | \begin{Go}
|
---|
470 | const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@ + 1; V5 ) // 0 1 7 4 5
|
---|
471 | const ( Mon = iota; Tue; Wed; // 0, 1, 2
|
---|
472 | @Thu = 10;@ Fri = @iota - Wed + Thu - 1@; Sat; Sun ) // 10, 11, 12, 13
|
---|
473 | \end{Go}
|
---|
474 | Here, @V4@ and @Fri@ restart auto-incrementing from \lstinline[language=Go]{iota} and reset \lstinline[language=Go]{iota} to 4 and 11, respectively, because of the intialization expressions containing \lstinline[language=Go]{iota}.
|
---|
475 | Note, because \lstinline[language=Go]{iota} is incremented for an explicitly initialized identifier or @_@,
|
---|
476 | at @Fri@ \lstinline[language=Go]{iota} is 4 requiring the minus one to compute the value for @Fri@.
|
---|
477 |
|
---|
478 | Basic switch and looping are possible.
|
---|
479 | \begin{cquote}
|
---|
480 | \setlength{\tabcolsep}{20pt}
|
---|
481 | \begin{tabular}{@{}ll@{}}
|
---|
482 | \begin{Go}
|
---|
483 | day := Mon; // := $\(\Rightarrow\)$ type inferencing
|
---|
484 | switch @day@ {
|
---|
485 | case Mon, Tue, Wed, Thu, Fri:
|
---|
486 | fmt.Println( "weekday" );
|
---|
487 | case Sat, Sun:
|
---|
488 | fmt.Println( "weekend" );
|
---|
489 | }
|
---|
490 | \end{Go}
|
---|
491 | &
|
---|
492 | \begin{Go}
|
---|
493 |
|
---|
494 | for i := @Mon@; i <= @Sun@; i += 1 {
|
---|
495 | fmt.Println( i )
|
---|
496 | }
|
---|
497 |
|
---|
498 |
|
---|
499 |
|
---|
500 | \end{Go}
|
---|
501 | \end{tabular}
|
---|
502 | \end{cquote}
|
---|
503 | However, the loop prints the values from 0 to 13 because there is no actual enumeration.
|
---|
504 |
|
---|
505 | A constant variable can be used as an array dimension or a subscript.
|
---|
506 | \begin{Go}
|
---|
507 | var ar[@Sun@] int
|
---|
508 | ar[@Mon@] = 3
|
---|
509 | \end{Go}
|
---|
510 |
|
---|
511 |
|
---|
512 | \section{Java}
|
---|
513 |
|
---|
514 | Java provides an enumeration using a specialized class.
|
---|
515 | A basic Java enumeration is an opaque enumeration, where the enumerators are constants.
|
---|
516 | \begin{Java}
|
---|
517 | enum Week {
|
---|
518 | Mon, Tue, Wed, Thu, Fri, Sat, Sun;
|
---|
519 | }
|
---|
520 | Week day = Week.Sat;
|
---|
521 | \end{Java}
|
---|
522 | The enumerators members are scoped and cannot be made \lstinline[language=java]{public}, hence require qualification.
|
---|
523 | The value of an enumeration instance is restricted to its enumerators.
|
---|
524 |
|
---|
525 | The position (ordinal) and label are accessible but there is no value.
|
---|
526 | \begin{Java}
|
---|
527 | System.out.println( day.!ordinal()! + " " + !day! + " " + day.!name()! );
|
---|
528 | 5 Sat Sat
|
---|
529 | \end{Java}
|
---|
530 | Since @day@ has no value, it prints its label (name).
|
---|
531 | The member @valueOf@ is the inverse of @name@ converting a string to enumerator.
|
---|
532 | \begin{Java}
|
---|
533 | day = Week.valueOf( "Wed" );
|
---|
534 | \end{Java}
|
---|
535 | Extra members can be added to provide specialized operations.
|
---|
536 | \begin{Java}
|
---|
537 | public boolean isWeekday() { return !ordinal()! <= Fri.ordinal(); }
|
---|
538 | public boolean isWeekend() { return Sat.ordinal() <= !ordinal()!; }
|
---|
539 | \end{Java}
|
---|
540 | Notice the unqualified calls to @ordinal@ in the members implying a \lstinline[language=Java]{this} to some implicit implementation variable, likely an @int@.
|
---|
541 |
|
---|
542 | Enumerator values require an enumeration type (any Java type may be used) and implementation member.
|
---|
543 | \begin{Java}
|
---|
544 | enum Week {
|
---|
545 | Mon!(1)!, Tue!(2)!, Wed!(3)!, Thu!(4)!, Fri!(5)!, Sat!(6)!, Sun!(7)!; // must appear first
|
---|
546 | private !long! day; $\C{// enumeration type and implementation member}$
|
---|
547 | private Week( !long! d ) { day = d; } $\C{// enumerator initialization}$
|
---|
548 | };
|
---|
549 | Week day = Week.Sat;
|
---|
550 | \end{Java}
|
---|
551 | The position, value, and label are accessible.
|
---|
552 | \begin{Java}
|
---|
553 | System.out.println( !day.ordinal()! + " " + !day.day! + " " + !day.name()! );
|
---|
554 | 5 6 Sat
|
---|
555 | \end{Java}
|
---|
556 | If the implementation member is \lstinline[language=Java]{public}, the enumeration is unsafe, as any value of the underlying type can be assigned to it, \eg @day = 42@.
|
---|
557 | The implementation constructor must be private since it is only used internally to initialize the enumerators.
|
---|
558 | Initialization occurs at the enumeration-type declaration for each enumerator in the first line.
|
---|
559 |
|
---|
560 | Enumerations can be used in the @if@ and @switch@ statements but only for equality tests.
|
---|
561 | \begin{cquote}
|
---|
562 | \setlength{\tabcolsep}{15pt}
|
---|
563 | \begin{tabular}{@{}ll@{}}
|
---|
564 | \begin{Java}
|
---|
565 | if ( !day! == Week.Fri )
|
---|
566 | System.out.println( "Fri" );
|
---|
567 |
|
---|
568 |
|
---|
569 |
|
---|
570 |
|
---|
571 | \end{Java}
|
---|
572 | &
|
---|
573 | \begin{Java}
|
---|
574 | switch ( !day! ) {
|
---|
575 | case Mon: case Tue: case Wed: case Thu: case Fri:
|
---|
576 | System.out.println( "weekday" ); break;
|
---|
577 | case Sat: case Sun:
|
---|
578 | System.out.println( "weekend" ); break;
|
---|
579 | }
|
---|
580 | \end{Java}
|
---|
581 | \end{tabular}
|
---|
582 | \end{cquote}
|
---|
583 | Notice enumerators in the @switch@ statement do not require qualification.
|
---|
584 |
|
---|
585 | There are no arithemtic operations on enumerations, so there is no arithmetic way to iterate through an enumeration without making the implementation type \lstinline[language=Java]{public}.
|
---|
586 | Like \Csharp, looping over an enumeration is done using method @values@, which returns an array of enumerator values (expensive operation).
|
---|
587 | \begin{Java}
|
---|
588 | for ( Week d : Week.values() ) {
|
---|
589 | System.out.print( d.ordinal() + d.day + " " + d.name() + ", " );
|
---|
590 | }
|
---|
591 | 0 1 Mon, 1 2 Tue, 2 3 Wed, 3 4 Thu, 4 5 Fri, 5 6 Sat, 6 7 Sun,
|
---|
592 | \end{Java}
|
---|
593 | Like \Csharp, enumerating is supplied indirectly through another enumerable type, not via the enumeration.
|
---|
594 |
|
---|
595 | An enumeration type cannot declare an array dimension nor can an enumerator be used as a subscript.
|
---|
596 | Enumeration inheritence is disallowed because an enumeration is \lstinline[language=Java]{final}.
|
---|
597 |
|
---|
598 | Java provides an @EnumSet@ where the underlying type is an efficient set of bits, one per enumeration \see{\Csharp \lstinline{Flags}, \VRef{s:Csharp}}, providing (logical) operations on groups of enumerators.
|
---|
599 | There is also a specialized version of @HashMap@ with enumerator keys, which has performance benefits.
|
---|
600 |
|
---|
601 |
|
---|
602 | \section{Rust}
|
---|
603 |
|
---|
604 | % https://doc.rust-lang.org/reference/items/enumerations.html
|
---|
605 |
|
---|
606 | Rust @enum@ provides two largely independent mechanisms from a single language feature: an ADT and an enumeration.
|
---|
607 | When @enum@ is an ADT, pattern matching is used to discriminate among the variant types.
|
---|
608 | \begin{cquote}
|
---|
609 | \begin{tabular}{@{}l@{\hspace{30pt}}ll@{}}
|
---|
610 | \begin{rust}
|
---|
611 | struct S {
|
---|
612 | i : isize, j : isize
|
---|
613 | }
|
---|
614 | let mut s = S{ i : 3, j : 4 };
|
---|
615 | enum @ADT@ {
|
---|
616 | I( isize ), $\C[1in]{// int}$
|
---|
617 | F( f64 ), $\C{// float}$
|
---|
618 | S( S ), $\C{// struct}\CRT$
|
---|
619 | }
|
---|
620 | \end{rust}
|
---|
621 | &
|
---|
622 | \begin{rust}
|
---|
623 | let mut adt : ADT;
|
---|
624 | adt = ADT::I(3); println!( "{:?}", adt );
|
---|
625 | adt = ADT::F(3.5); println!( "{:?}", adt );
|
---|
626 | adt = ADT::S(s); println!( "{:?}", adt );
|
---|
627 | @match@ adt {
|
---|
628 | ADT::I( i ) => println!( "{:}", i ),
|
---|
629 | ADT::F( f ) => println!( "{:}", f ),
|
---|
630 | ADT::S( s ) => println!( "{:} {:}", s.i, s.j ),
|
---|
631 | }
|
---|
632 | \end{rust}
|
---|
633 | &
|
---|
634 | \begin{rust}
|
---|
635 | I(3)
|
---|
636 | F(3.5)
|
---|
637 | S(S { i: 3, j: 4 })
|
---|
638 | 3 4
|
---|
639 |
|
---|
640 |
|
---|
641 |
|
---|
642 |
|
---|
643 |
|
---|
644 | \end{rust}
|
---|
645 | \end{tabular}
|
---|
646 | \end{cquote}
|
---|
647 | Even when the variant types are the unit type, the ADT is still not an enumeration because there is no enumerating \see{\VRef{s:AlgebraicDataType}}.
|
---|
648 | \begin{rust}
|
---|
649 | enum Week { Mon, Tues, Wed, Thu, Fri, Sat, Sun@,@ } // terminating comma
|
---|
650 | let mut week : Week = Week::Mon;
|
---|
651 | match week {
|
---|
652 | Week::Mon => println!( "Mon" ),
|
---|
653 | ...
|
---|
654 | Week::Sun => println!( "Sun" ),
|
---|
655 | }
|
---|
656 | \end{rust}
|
---|
657 |
|
---|
658 | However, Rust allows direct setting of the ADT constructor, which means it is actually a tag.
|
---|
659 | \begin{cquote}
|
---|
660 | \setlength{\tabcolsep}{15pt}
|
---|
661 | \begin{tabular}{@{}ll@{}}
|
---|
662 | \begin{rust}
|
---|
663 | enum Week {
|
---|
664 | Mon, Tues, Wed, // start 0
|
---|
665 | Thu @= 10@, Fri,
|
---|
666 | Sat, Sun,
|
---|
667 | }
|
---|
668 |
|
---|
669 | \end{rust}
|
---|
670 | &
|
---|
671 | \begin{rust}
|
---|
672 | #[repr(u8)]
|
---|
673 | enum ADT {
|
---|
674 | I(isize) @= 5@, // ???
|
---|
675 | F(f64) @= 10@,
|
---|
676 | S(S) @= 0@,
|
---|
677 | }
|
---|
678 | \end{rust}
|
---|
679 | \end{tabular}
|
---|
680 | \end{cquote}
|
---|
681 | Through this integral tag, it is possible to enumerate, and when all tags represent the unit type, it behaves like \CC \lstinline[language=C++]{enum class}.
|
---|
682 | When tags represent non-unit types, Rust largely precludes accessing the tag because the semantics become meaningless.
|
---|
683 | Hence, the two mechanisms are largely disjoint, and ony the enumeration component is discussed.
|
---|
684 |
|
---|
685 | In detail, the @enum@ type has an implicit integer tag (discriminant), with a unique value for each variant type.
|
---|
686 | Direct initialization is by a compile-time expression generating a constant value.
|
---|
687 | Indirect initialization (without initialization, @Fri@/@Sun@) is auto-initialized: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@.
|
---|
688 | There is an explicit cast from the tag to integer.
|
---|
689 | \begin{rust}
|
---|
690 | let mut mon : isize = Week::Mon as isize;
|
---|
691 | \end{rust}
|
---|
692 | An enumeration can be used in the @if@ and \lstinline[language=rust]{match} (@switch@) statements.
|
---|
693 | \begin{cquote}
|
---|
694 | \setlength{\tabcolsep}{8pt}
|
---|
695 | \begin{tabular}{@{}ll@{}}
|
---|
696 | \begin{c++}
|
---|
697 | if @week as isize@ == Week::Mon as isize {
|
---|
698 | println!( "{:?}", week );
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 | \end{c++}
|
---|
703 | &
|
---|
704 | \begin{c++}
|
---|
705 | match @week@ {
|
---|
706 | Week::Mon | Week:: Tue | Week::Wed | Week::Thu
|
---|
707 | | Week::Fri => println!( "weekday" ),
|
---|
708 | Week::Sat | Week:: Sun => println!( "weekend" ),
|
---|
709 | }
|
---|
710 | \end{c++}
|
---|
711 | \end{tabular}
|
---|
712 | \end{cquote}
|
---|
713 | However, there is no mechanism to iterate through an enumeration without casting to integral and positions versus values is not handled.
|
---|
714 | \begin{c++}
|
---|
715 | for d in Week::Mon as isize ..= Week::Sun as isize {
|
---|
716 | print!( "{:?} ", d );
|
---|
717 | }
|
---|
718 | 0 1 2 @3 4 5 6 7 8 9@ 10 11 12 13
|
---|
719 | \end{c++}
|
---|
720 | An enumeration type cannot declare an array dimension nor as a subscript.
|
---|
721 | There is no mechanism to subtype or inherit from an enumeration.
|
---|
722 |
|
---|
723 |
|
---|
724 | \section{Swift}
|
---|
725 |
|
---|
726 | % https://www.programiz.com/swift/online-compiler
|
---|
727 |
|
---|
728 | Like Rust, Swift @enum@ provides two largely independent mechanisms from a single language feature: an ADT and an enumeration.
|
---|
729 | When @enum@ is an ADT, pattern matching is used to discriminate among the variant types.
|
---|
730 | \begin{cquote}
|
---|
731 | \setlength{\tabcolsep}{20pt}
|
---|
732 | \begin{tabular}{@{}l@{\hspace{55pt}}ll@{}}
|
---|
733 | \begin{swift}
|
---|
734 | struct S {
|
---|
735 | var i : Int, j : Int
|
---|
736 | }
|
---|
737 | var s = S( i : 3, j : 5 )
|
---|
738 | @enum@ ADT {
|
---|
739 | case I(Int) $\C[1.125in]{// int}$
|
---|
740 | case F(Float) $\C{// float}$
|
---|
741 | case S(S) $\C{// struct}\CRT$
|
---|
742 | }
|
---|
743 | \end{swift}
|
---|
744 | &
|
---|
745 | \begin{swift}
|
---|
746 | var adt : ADT
|
---|
747 | adt = .I( 3 ); print( adt )
|
---|
748 | adt = .F( 3.5 ); print( adt )
|
---|
749 | adt = .S( s ); print( adt )
|
---|
750 | @switch@ adt { // pattern matching
|
---|
751 | case .I(let i): print( i )
|
---|
752 | case .F(let f): print( f )
|
---|
753 | case .S(let s): print( s.i, s.j )
|
---|
754 | }
|
---|
755 | \end{swift}
|
---|
756 | &
|
---|
757 | \begin{swift}
|
---|
758 | I(3)
|
---|
759 | F(3.5)
|
---|
760 | S(S(i: 3, j: 5))
|
---|
761 | 3 5
|
---|
762 |
|
---|
763 |
|
---|
764 |
|
---|
765 |
|
---|
766 |
|
---|
767 | \end{swift}
|
---|
768 | \end{tabular}
|
---|
769 | \end{cquote}
|
---|
770 | (Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.)
|
---|
771 |
|
---|
772 | An enumeration is created when \emph{all} the enumerators are unit-type.
|
---|
773 | \begin{swift}
|
---|
774 | enum Week {
|
---|
775 | case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
|
---|
776 | };
|
---|
777 | var week : Week = Week.Mon;
|
---|
778 | \end{swift}
|
---|
779 | As well, it is possible to type \emph{all} the enumerators with a common type, and set different values for each enumerator;
|
---|
780 | for integral types, there is auto-incrementing.
|
---|
781 | \begin{cquote}
|
---|
782 | \setlength{\tabcolsep}{15pt}
|
---|
783 | \begin{tabular}{@{}lll@{}}
|
---|
784 | \begin{swift}
|
---|
785 | enum WeekInt: @Int@ {
|
---|
786 | case Mon, Tue, Wed, Thu = 10, Fri,
|
---|
787 | Sat = 4, Sun // auto-incrementing
|
---|
788 | };
|
---|
789 | \end{swift}
|
---|
790 | &
|
---|
791 | \begin{swift}
|
---|
792 | enum WeekStr: @String@ {
|
---|
793 | case Mon = "MON", Tue, Wed, Thu, Fri,
|
---|
794 | Sat = "SAT", Sun
|
---|
795 | };
|
---|
796 | \end{swift}
|
---|
797 | \end{tabular}
|
---|
798 | \end{cquote}
|
---|
799 | An enumeration only supports equality comparison between enumerator values, unless it inherits from @Comparable@, adding relational operators @<@, @<=@, @>@, and @>=@.
|
---|
800 |
|
---|
801 | An enumeration can have methods.
|
---|
802 | \begin{swift}
|
---|
803 | enum Week: Comparable {
|
---|
804 | case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
|
---|
805 | func @isWeekday() -> Bool@ { return self <= .Fri } // method
|
---|
806 | func @isWeekend() -> Bool@ { return .Sat <= self } // method
|
---|
807 | };
|
---|
808 | \end{swift}
|
---|
809 | An enumeration can be used in the @if@ and @switch@ statements, where @switch@ must be exhaustive or have a @default@.
|
---|
810 | \begin{cquote}
|
---|
811 | \setlength{\tabcolsep}{15pt}
|
---|
812 | \begin{tabular}{@{}ll@{}}
|
---|
813 | \begin{swift}
|
---|
814 | if @week <= .Fri@ {
|
---|
815 | print( "weekday" );
|
---|
816 | }
|
---|
817 |
|
---|
818 |
|
---|
819 | \end{swift}
|
---|
820 | &
|
---|
821 | \begin{swift}
|
---|
822 | switch @week@ {
|
---|
823 | case .Mon: print( "Mon" )
|
---|
824 | ...
|
---|
825 | case .Sun: print( "Sun" )
|
---|
826 | }
|
---|
827 | \end{swift}
|
---|
828 | \end{tabular}
|
---|
829 | \end{cquote}
|
---|
830 |
|
---|
831 | Enumerating is accomplished by inheriting from @CaseIterable@ without any associated values.
|
---|
832 | \begin{swift}
|
---|
833 | enum Week: Comparable, @CaseIterable@ {
|
---|
834 | case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
|
---|
835 | };
|
---|
836 | var weeki : Week = Week.Mon;
|
---|
837 | if weeki <= .Fri {
|
---|
838 | print( "weekday" );
|
---|
839 | }
|
---|
840 | for day in Week@.allCases@ {
|
---|
841 | print( day, terminator:" " )
|
---|
842 | }
|
---|
843 | weekday
|
---|
844 | Mon Tue Wed Thu Fri Sat Sun
|
---|
845 | \end{swift}
|
---|
846 | The @enum.allCases@ property returns a collection of all the cases for looping over an enumeration type or variable (expensive operation).
|
---|
847 |
|
---|
848 | A typed enumeration is accomplished by inheriting from any Swift type, and accessing the underlying enumerator value is done with attribute @rawValue@.
|
---|
849 | Type @Int@ has auto-incrementing from previous enumerator;
|
---|
850 | type @String@ has auto-incrementing of the enumerator label.
|
---|
851 | \begin{cquote}
|
---|
852 | \setlength{\tabcolsep}{15pt}
|
---|
853 | \begin{tabular}{@{}lll@{}}
|
---|
854 | \begin{swift}
|
---|
855 | enum WeekInt: @Int@, CaseIterable {
|
---|
856 | case Mon, Tue, Wed, Thu = 10, Fri,
|
---|
857 | Sat = 4, Sun // auto-incrementing
|
---|
858 | };
|
---|
859 | for day in WeekInt.allCases {
|
---|
860 | print( day@.rawValue@, terminator:" " )
|
---|
861 | }
|
---|
862 | 0 1 2 10 11 4 5
|
---|
863 | \end{swift}
|
---|
864 | &
|
---|
865 | \begin{swift}
|
---|
866 | enum WeekStr: @String@, CaseIterable {
|
---|
867 | case Mon = "MON", Tue, Wed, Thu, Fri,
|
---|
868 | Sat = "SAT", Sun
|
---|
869 | };
|
---|
870 | for day in WeekStr.allCases {
|
---|
871 | print( day@.rawValue@, terminator:" " )
|
---|
872 | }
|
---|
873 | MON Tue Wed Thu Fri SAT Sun
|
---|
874 | \end{swift}
|
---|
875 | \end{tabular}
|
---|
876 | \end{cquote}
|
---|
877 |
|
---|
878 | There is a bidirectional conversion from typed enumerator to @rawValue@ and vise versa.
|
---|
879 | \begin{swift}
|
---|
880 | var weekInt : WeekInt = WeekInt.Mon;
|
---|
881 | if let opt = WeekInt( rawValue: 0 ) { // test optional return value
|
---|
882 | print( weekInt.rawValue, opt ) // 0 Mon
|
---|
883 | } else {
|
---|
884 | print( "invalid weekday lookup" )
|
---|
885 | }
|
---|
886 | \end{swift}
|
---|
887 | Conversion from @rawValue@ to enumerator may fail (bad lookup), so the result is an optional value.
|
---|
888 |
|
---|
889 |
|
---|
890 | \section{Python 3.13}
|
---|
891 | % https://docs.python.org/3/howto/enum.html
|
---|
892 |
|
---|
893 | Python is a dynamically-typed reflexive programming language with multiple incompatible versions.
|
---|
894 | The generality of the language makes it is possible to extend existing or build new language features.
|
---|
895 | As a result, discussing Python enumerations is a moving target, because if a features does not exist, it can often be created with varying levels of complexity within the language.
|
---|
896 | Therefore, the following discussion is (mostly) restricted to the core enumeration features in Python 3.13.
|
---|
897 |
|
---|
898 | A Python enumeration is not a basic type;
|
---|
899 | it is a @class@ inheriting from the @Enum@ class.
|
---|
900 | The @Enum@ class presents a set of scoped enumerators, where each enumerator is a pair object with a \emph{constant} string name and arbitrary value.
|
---|
901 | Hence, an enumeration instance is a fixed type (enumeration pair), and its value is the type of one of the enumerator pairs.
|
---|
902 |
|
---|
903 | The enumerator value fields must be explicitly initialized and be \emph{unique}.
|
---|
904 | \begin{python}
|
---|
905 | class Week(!Enum!): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
|
---|
906 | \end{python}
|
---|
907 | and/or explicitly auto initialized, \eg:
|
---|
908 | \begin{python}
|
---|
909 | class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = !auto()!; Sat = 4; Sun = !auto()!
|
---|
910 | Mon : 1 Tue : 2 Wed : 3 Thu : 10 Fri : !11! Sat : 4 Sun : !12!
|
---|
911 | \end{python}
|
---|
912 | where @auto@ increments by 1 from the previous @auto@ value \see{Golang \lstinline[language=Go]{iota}, \VRef{s:Golang}}.
|
---|
913 | @auto@ is controlled by member @_generate_next_value_()@, which can be overridden:
|
---|
914 | \begin{python}
|
---|
915 | @staticmethod
|
---|
916 | def _generate_next_value_( name, start, count, last_values ):
|
---|
917 | return name
|
---|
918 | \end{python}
|
---|
919 |
|
---|
920 | There is no direct concept of restricting the enumerators in an enumeration \emph{instance} because the dynamic typing changes the type.
|
---|
921 | \begin{python}
|
---|
922 | class RGB(Enum): Red = 1; Green = 2; Blue = 3
|
---|
923 | day : Week = Week.Tue; $\C{\# type is Week}$
|
---|
924 | !day = RGB.Red! $\C{\# type is RGB}$
|
---|
925 | !day : Week = RGB.Red! $\C{\# type is RGB}$
|
---|
926 | \end{python}
|
---|
927 | The enumerators are constants and cannot be reassigned.
|
---|
928 | Hence, while enumerators can be different types,
|
---|
929 | \begin{python}
|
---|
930 | class Diff(Enum): Int = 1; Float = 3.5; Str = "ABC"
|
---|
931 | \end{python}
|
---|
932 | it is not an ADT because the enumerator names are not constructors.
|
---|
933 |
|
---|
934 | An enumerator initialized with the same value is an alias and invisible at the enumeration level, \ie the alias is substituted for its aliasee.
|
---|
935 | \begin{python}
|
---|
936 | class WeekD(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = !10!; Fri = !10!; Sat = !10!; Sun = !10!
|
---|
937 | \end{python}
|
---|
938 | Here, the enumeration has only 4 enumerators and 3 aliases.
|
---|
939 | An alias is only visible by dropping down to the @class@ level and asking for class members.
|
---|
940 | Aliasing is prevented using the @unique@ decorator.
|
---|
941 | \begin{python}
|
---|
942 | !@unique!
|
---|
943 | class DupVal(Enum): One = 1; Two = 2; Three = !3!; Four = !3!
|
---|
944 | ValueError: duplicate values found in <enum 'DupVal'>: Four -> Three
|
---|
945 | \end{python}
|
---|
946 |
|
---|
947 | \begin{lrbox}{\myboxA}
|
---|
948 | \begin{python}
|
---|
949 | def by_position(enum_type, position):
|
---|
950 | for index, value in enumerate(enum_type):
|
---|
951 | if position == index: return value
|
---|
952 | raise Exception("by_position out of range")
|
---|
953 | \end{python}
|
---|
954 | \end{lrbox}
|
---|
955 | There are bidirectional enumeration pseudo-functions for label and value, but there is no concept of access using ordering (position).\footnote{
|
---|
956 | There is an $O(N)$ mechanism to access an enumerator's value by position. \newline \usebox\myboxA}
|
---|
957 | \begin{cquote}
|
---|
958 | \setlength{\tabcolsep}{15pt}
|
---|
959 | \begin{tabular}{@{}ll@{}}
|
---|
960 | \begin{python}
|
---|
961 | Week.Thu.value == 4;
|
---|
962 | Week.Thu.name == "Thu";
|
---|
963 | \end{python}
|
---|
964 | &
|
---|
965 | \begin{python}
|
---|
966 | Week( 4 ) == Week.Thu
|
---|
967 | Week["Thu"].value == 4
|
---|
968 | \end{python}
|
---|
969 | \end{tabular}
|
---|
970 | \end{cquote}
|
---|
971 | @Enum@ only supports equality comparison between enumerator values.
|
---|
972 | There are multiple library extensions to @Enum@, \eg @OrderedEnum@ recipe class, adding relational operators @<@, @<=@, @>@, and @>=@.
|
---|
973 |
|
---|
974 | An enumeration \lstinline[language=python]{class} can have methods.
|
---|
975 | \begin{python}
|
---|
976 | class Week(!OrderedEnum!):
|
---|
977 | Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
|
---|
978 | def !isWeekday(self)!: # method
|
---|
979 | return Week(self.value) !<=! Week.Fri
|
---|
980 | def !isWeekend(self)!: # method
|
---|
981 | return Week.Sat !<=! Week(self.value)
|
---|
982 | \end{python}
|
---|
983 |
|
---|
984 | An enumeration can be used in the @if@ and @switch@ statements but only for equality tests, unless extended to @OrderedEnum@.
|
---|
985 | \begin{cquote}
|
---|
986 | \setlength{\tabcolsep}{12pt}
|
---|
987 | \begin{tabular}{@{}ll@{}}
|
---|
988 | \begin{python}
|
---|
989 | if day <= Week.Fri :
|
---|
990 | print( "weekday" );
|
---|
991 |
|
---|
992 |
|
---|
993 |
|
---|
994 | \end{python}
|
---|
995 | &
|
---|
996 | \begin{python}
|
---|
997 | match day:
|
---|
998 | case Week.Mon | Week.Tue | Week.Wed | Week.Thu | Week.Fri:
|
---|
999 | print( "weekday" );
|
---|
1000 | case Week.Sat | Week.Sun:
|
---|
1001 | print( "weekend" );
|
---|
1002 | \end{python}
|
---|
1003 | \end{tabular}
|
---|
1004 | \end{cquote}
|
---|
1005 | Looping is performed using the enumeration type or @islice@ from @itertools@ based on position.
|
---|
1006 | \begin{python}
|
---|
1007 | for day in !Week!: $\C[2.25in]{\# Mon : 1 Tue : 2 Wed : 3 Thu : 4 Fri : 5 Sat : 6 Sun : 7}$
|
---|
1008 | print( day.name, ":", day.value, end=" " )
|
---|
1009 | for day in !islice(Week, 0, 5)!: $\C{\# Mon : 1 Tue : 2 Wed : 3 Thu : 4 Fri : 5}$
|
---|
1010 | print( day.name, ":", day.value, end=" " )
|
---|
1011 | for day in !islice(Week, 5, 7)!: $\C{\# Sat : 6 Sun : 7}$
|
---|
1012 | print( day.name, ":", day.value, end=" " )
|
---|
1013 | for day in !islice(Week,0, 7, 2)!: $\C{\# Mon : 1 Wed : 3 Fri : 5 Sun : 7}\CRT$
|
---|
1014 | print( day.name, ":", day.value, end=" " )
|
---|
1015 | \end{python}
|
---|
1016 | Iterating that includes alias names only (strings) is done using attribute @__members__@.
|
---|
1017 | \begin{python}
|
---|
1018 | for day in WeekD.__members__:
|
---|
1019 | print( day, ":", end=" " )
|
---|
1020 | Mon : Tue : Wed : Thu : Fri : Sat : Sun
|
---|
1021 | \end{python}
|
---|
1022 |
|
---|
1023 | Enumeration subclassing is allowed only if the enumeration base-class does not define any members.
|
---|
1024 | \begin{python}
|
---|
1025 | class WeekE(OrderedEnum): !pass!; # no members
|
---|
1026 | class WeekDay(WeekE): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5;
|
---|
1027 | class WeekEnd(WeekE): Sat = 6; Sun = 7
|
---|
1028 | \end{python}
|
---|
1029 | Here, type @WeekE@ is an abstract type because the dynamic typing never uses it.
|
---|
1030 | \begin{cquote}
|
---|
1031 | \setlength{\tabcolsep}{25pt}
|
---|
1032 | \begin{tabular}{@{}ll@{}}
|
---|
1033 | \begin{python}
|
---|
1034 | print( type(WeekE) )
|
---|
1035 | day : WeekE = WeekDay.Fri # set type
|
---|
1036 | print( type(day), day )
|
---|
1037 | day = WeekEnd.Sat # set type
|
---|
1038 | print( type(day), day )
|
---|
1039 | \end{python}
|
---|
1040 | &
|
---|
1041 | \begin{python}
|
---|
1042 | <$class$ 'enum.EnumType'>
|
---|
1043 |
|
---|
1044 | <enum 'WeekDay'> WeekDay.Fri
|
---|
1045 |
|
---|
1046 | <enum 'WeekEnd'> WeekEnd.Sat
|
---|
1047 | \end{python}
|
---|
1048 | \end{tabular}
|
---|
1049 | \end{cquote}
|
---|
1050 |
|
---|
1051 | There are a number of supplied enumeration base-types: @IntEnum@, @StrEnum@, @IntFalg@, @Flag@, which restrict the values in an enum using multi-inheritance.
|
---|
1052 | @IntEnum@ is a subclass of @int@ and @Enum@, allowing enumerator comparison to @int@ and other enumerators of this type (like C enumerators).
|
---|
1053 | @StrEnum@ is the same as @IntEnum@ but a subclass of the string type \lstinline[language=python]{str}.
|
---|
1054 | @IntFlag@, is a restricted subclass of @int@ where the enumerators can be combined using the bitwise operators (@&@, @|@, @^@, @~@) and the result is an @IntFlag@ member.
|
---|
1055 | @Flag@ is the same as @IntFlag@ but cannot be combined with, nor compared against, any other @Flag@ enumeration, nor @int@.
|
---|
1056 | Auto increment for @IntFlag@ and @Flag@ is by powers of 2.
|
---|
1057 | Enumerators that are a combinations of single bit enumerators are aliases, and hence, invisible.
|
---|
1058 | The following is an example for @Flag@.
|
---|
1059 | \begin{python}
|
---|
1060 | class WeekF(Flag): Mon = 1; Tue = 2; Wed = 4; Thu = !auto()!; Fri = 16; Sat = 32; Sun = 64; \
|
---|
1061 | Weekday = Mon | Tue | Wed | Thu | Fri; \
|
---|
1062 | Weekend = Sat | Sun
|
---|
1063 | print( f"0x{repr(WeekF.Weekday.value)} 0x{repr(WeekF.Weekend.value)}" )
|
---|
1064 | 0x31 0x96
|
---|
1065 | \end{python}
|
---|
1066 | It is possible to enumerate through a @Flag@ enumerator (no aliases):
|
---|
1067 | \begin{python}
|
---|
1068 | for day in WeekF:
|
---|
1069 | print( f"{day.name}: {day.value}", end=" ")
|
---|
1070 | Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16 Sat: 32 Sun: 64
|
---|
1071 | \end{python}
|
---|
1072 | and a combined alias enumerator for @Flag@.
|
---|
1073 | \begin{cquote}
|
---|
1074 | \setlength{\tabcolsep}{15pt}
|
---|
1075 | \begin{tabular}{@{}ll@{}}
|
---|
1076 | \begin{python}
|
---|
1077 | weekday = WeekF.Weekday
|
---|
1078 | for day in weekday:
|
---|
1079 | print( f"{day.name}:"
|
---|
1080 | f" {day.value}", end=" " )
|
---|
1081 | Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16
|
---|
1082 | \end{python}
|
---|
1083 | &
|
---|
1084 | \begin{python}
|
---|
1085 | weekend = WeekF.Weekend
|
---|
1086 | for day in weekend:
|
---|
1087 | print( f"{day.name}:"
|
---|
1088 | f" {day.value}", end=" " )
|
---|
1089 | Sat: 32 Sun: 64
|
---|
1090 | \end{python}
|
---|
1091 | \end{tabular}
|
---|
1092 | \end{cquote}
|
---|
1093 |
|
---|
1094 |
|
---|
1095 | \section{OCaml}
|
---|
1096 |
|
---|
1097 | % https://ocaml.org/docs/basic-data-types#enumerated-data-types
|
---|
1098 | % https://dev.realworldocaml.org/runtime-memory-layout.html
|
---|
1099 |
|
---|
1100 | OCaml provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage.
|
---|
1101 | The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
|
---|
1102 |
|
---|
1103 | OCaml provides a variant (union) type, which is an aggregation of heterogeneous types.
|
---|
1104 | A basic variant is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
|
---|
1105 | \begin{ocaml}
|
---|
1106 | type week = Mon | Tue | Wed | Thu | Fri | Sat | Sun
|
---|
1107 | let day : week @= Mon@ $\C{(* bind *)}$
|
---|
1108 | let take_class( d : week ) =
|
---|
1109 | @match@ d with $\C{(* matching *)}$
|
---|
1110 | Mon | Wed -> Printf.printf "CS442\n" |
|
---|
1111 | Tue | Thu -> Printf.printf "CS343\n" |
|
---|
1112 | Fri -> Printf.printf "Tutorial\n" |
|
---|
1113 | _ -> Printf.printf "Take a break\n"
|
---|
1114 | let _ = take_class( Mon );
|
---|
1115 | @CS442@
|
---|
1116 | \end{ocaml}
|
---|
1117 | The only operations are binding and pattern matching (equality), where the variant name is logically the implementation tag stored in the union for discriminating the value in the object storage.
|
---|
1118 | After compilation, variant names are mapped to an opague ascending intergral type discriminants, starting from 0.
|
---|
1119 | Here, function @take_class@ has a @week@ parameter, and returns @"CS442"@, if the week value is @Mon@ or @Wed@, @"CS343"@, if the value is @Tue@ or @Thu@, and @"Tutorial"@ for @Fri@.
|
---|
1120 | The ``@_@'' is a wildcard matching any @week@ value, so the function returns @"Take a break"@ for values @Sat@ or @Sun@, which are not matched by the previous cases.
|
---|
1121 | Since the variant has no type, it has a \newterm{0-arity constructor}, \ie no parameters.
|
---|
1122 | Because @week@ is a union of values @Mon@ to @Sun@, it is a \newterm{union type} in turns of the functional-programming paradigm.
|
---|
1123 |
|
---|
1124 | Each variant can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value.
|
---|
1125 | \begin{ocaml}
|
---|
1126 | type colour = Red | Green of @string@ | Blue of @int * float@
|
---|
1127 | \end{ocaml}
|
---|
1128 | A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block.
|
---|
1129 | @colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@.
|
---|
1130 | (Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.)
|
---|
1131 | Hence, a variant type creates a sum of product of different types.
|
---|
1132 | \begin{ocaml}
|
---|
1133 | let c = Red $\C{(* 0-ary constructor, set tag *)}$
|
---|
1134 | let _ = match c with Red -> Printf.printf "Red, "
|
---|
1135 | let c = Green( "abc" ) $\C{(* 1-ary constructor, set tag *)}$
|
---|
1136 | let _ = match c with Green g -> Printf.printf "%s, " g
|
---|
1137 | let c = Blue( 1, 1.5 ) $\C{(* 2-ary constructor, set tag *)}$
|
---|
1138 | let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
|
---|
1139 | @Red, abc, 1 1.5@
|
---|
1140 | \end{ocaml}
|
---|
1141 |
|
---|
1142 | A variant type can have a recursive definition.
|
---|
1143 | \begin{ocaml}
|
---|
1144 | type @stringList@ = Empty | Pair of string * @stringList@
|
---|
1145 | \end{ocaml}
|
---|
1146 | which is a recursive sum of product of types, called an \newterm{algebraic data-type}.
|
---|
1147 | A recursive function is often used to pattern match against a recursive variant type.
|
---|
1148 | \begin{ocaml}
|
---|
1149 | let rec @len_of_string_list@( list : stringList ): int =
|
---|
1150 | match list with
|
---|
1151 | Empty -> 0 |
|
---|
1152 | Pair( _ , r ) -> 1 + @len_of_string_list@ r
|
---|
1153 | \end{ocaml}
|
---|
1154 | Here, the head of the recursive type is removed and the remainder is processed until the type is empty.
|
---|
1155 | Each recursion is counted to obtain the number of elements in the type.
|
---|
1156 |
|
---|
1157 | Note, the compiler statically guarantees that only the correct kind of type is used in the \lstinline[language=OCaml]{match} statement.
|
---|
1158 | However, the union tag is dynamically set on binding (and possible reset on assignment), so a \lstinline[language=OCaml]{match} statement is effectively doing RTTI to select the matching case clause.
|
---|
1159 |
|
---|
1160 | In summary, an OCaml variant is a singleton value rather than a set of possibly ordered values, and hence, has no notion of enumerabilty.
|
---|
1161 | Therefore it is not an enumeration, except for the simple opaque (nullary) case.
|
---|
1162 |
|
---|
1163 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
1164 |
|
---|
1165 | \begin{comment}
|
---|
1166 | Date: Wed, 13 Mar 2024 10:52:34 -0400
|
---|
1167 | Subject: Re: OCaml
|
---|
1168 | To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
|
---|
1169 | From: Gregor Richards <gregor.richards@uwaterloo.ca>
|
---|
1170 |
|
---|
1171 | On 3/12/24 18:34, Peter A. Buhr wrote:
|
---|
1172 | > Gregor, attached is a section Jiada wrote on OCaml (1-page).
|
---|
1173 | > Does it reflect our discussion about functional languages and enumerations?
|
---|
1174 |
|
---|
1175 | Yeah, I think so. The most important part, i.e., that once they're
|
---|
1176 | parameterized they're not really enumerations at all, is covered clearly
|
---|
1177 | enough.
|
---|
1178 |
|
---|
1179 | A couple quibbles:
|
---|
1180 |
|
---|
1181 | <<a list of untyped tags>>
|
---|
1182 |
|
---|
1183 | This is true, but leaking implementation details. These are nullary datatype
|
---|
1184 | constructors. Indeed, you later talk about "tagged variants", which are really
|
---|
1185 | just parameterized variants, using the term "tag" differently, confusing the
|
---|
1186 | term "tag" further.
|
---|
1187 |
|
---|
1188 | <<Because week is a summation of values Mon to Sun, it is a sum type in
|
---|
1189 | turns of the functional-programming paradigm>>
|
---|
1190 |
|
---|
1191 | It is a *union* of values and is a *union* type.
|
---|
1192 |
|
---|
1193 | With valediction,
|
---|
1194 | - Gregor Richards
|
---|
1195 |
|
---|
1196 |
|
---|
1197 | Date: Thu, 14 Mar 2024 21:45:52 -0400
|
---|
1198 | Subject: Re: OCaml "enums" do come with ordering
|
---|
1199 | To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
|
---|
1200 | From: Gregor Richards <gregor.richards@uwaterloo.ca>
|
---|
1201 |
|
---|
1202 | On 3/14/24 21:30, Peter A. Buhr wrote:
|
---|
1203 | > I've marked 3 places with your name to shows places with enum ordering.
|
---|
1204 | >
|
---|
1205 | > type week = Mon | Tue | Wed | Thu | Fri | Sat | Sun
|
---|
1206 | > let day : week = Mon
|
---|
1207 | > let take_class( d : week ) =
|
---|
1208 | > if d <= Fri then (* Gregor *)
|
---|
1209 | > Printf.printf "week\n"
|
---|
1210 | > else if d >= Sat then (* Gregor *)
|
---|
1211 | > Printf.printf "weekend\n";
|
---|
1212 | > match d with
|
---|
1213 | > Mon | Wed -> Printf.printf "CS442\n" |
|
---|
1214 | > Tue | Thu -> Printf.printf "CS343\n" |
|
---|
1215 | > Fri -> Printf.printf "Tutorial\n" |
|
---|
1216 | > _ -> Printf.printf "Take a break\n"
|
---|
1217 | >
|
---|
1218 | > let _ = take_class( Mon ); take_class( Sat );
|
---|
1219 | >
|
---|
1220 | > type colour = Red | Green of string | Blue of int * float
|
---|
1221 | > let c = Red
|
---|
1222 | > let _ = match c with Red -> Printf.printf "Red, "
|
---|
1223 | > let c = Green( "abc" )
|
---|
1224 | > let _ = match c with Green g -> Printf.printf "%s, " g
|
---|
1225 | > let c = Blue( 1, 1.5 )
|
---|
1226 | > let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
|
---|
1227 | >
|
---|
1228 | > let check_colour(c: colour): string =
|
---|
1229 | > if c < Green( "xyz" ) then (* Gregor *)
|
---|
1230 | > Printf.printf "green\n";
|
---|
1231 | > match c with
|
---|
1232 | > Red -> "Red" |
|
---|
1233 | > Green g -> g |
|
---|
1234 | > Blue(i, f) -> string_of_int i ^ string_of_float f
|
---|
1235 | > let _ = check_colour( Red ); check_colour( Green( "xyz" ) );
|
---|
1236 | >
|
---|
1237 | > type stringList = Empty | Pair of string * stringList
|
---|
1238 | > let rec len_of_string_list(l: stringList): int =
|
---|
1239 | > match l with
|
---|
1240 | > Empty -> 0 |
|
---|
1241 | > Pair(_ , r) -> 1 + len_of_string_list r
|
---|
1242 | >
|
---|
1243 | > let _ = for i = 1 to 10 do
|
---|
1244 | > Printf.printf "%d, " i
|
---|
1245 | > done
|
---|
1246 | >
|
---|
1247 | > (* Local Variables: *)
|
---|
1248 | > (* tab-width: 4 *)
|
---|
1249 | > (* compile-command: "ocaml test.ml" *)
|
---|
1250 | > (* End: *)
|
---|
1251 |
|
---|
1252 | My functional-language familiarity is far more with Haskell than OCaml. I
|
---|
1253 | mostly view OCaml through a lens of "it's Haskell but with cheating". Haskell
|
---|
1254 | "enums" (ADTs) aren't ordered unless you specifically and manually put them in
|
---|
1255 | the Ord typeclass by defining the comparators. Apparently, OCaml has some
|
---|
1256 | other rule, which I would guess is something like "sort by tag then by order of
|
---|
1257 | parameter". Having a default behavior for comparators is *bizarre*; my guess
|
---|
1258 | would be that it gained this behavior in its flirtation with object
|
---|
1259 | orientation, but that's just a guess (and irrelevant).
|
---|
1260 |
|
---|
1261 | This gives a total order, but not enumerability (which would still be
|
---|
1262 | effectively impossible or even meaningless since enums are just a special case
|
---|
1263 | of ADTs).
|
---|
1264 |
|
---|
1265 | With valediction,
|
---|
1266 | - Gregor Richards
|
---|
1267 |
|
---|
1268 | Date: Wed, 20 Mar 2024 18:16:44 -0400
|
---|
1269 | Subject: Re:
|
---|
1270 | To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
|
---|
1271 | From: Gregor Richards <gregor.richards@uwaterloo.ca>
|
---|
1272 |
|
---|
1273 |
|
---|
1274 | On 3/20/24 17:26, Peter A. Buhr wrote:
|
---|
1275 | > Gregor, everyone at this end would like a definition of "enumerability". Can
|
---|
1276 | > you formulate one?
|
---|
1277 |
|
---|
1278 | According to the OED (emphasis added to the meaning I'm after):
|
---|
1279 |
|
---|
1280 | enumerate (verb, transitive). To count, ascertain the number of; **more
|
---|
1281 | usually, to mention (a number of things or persons) separately, as if for the
|
---|
1282 | purpose of counting**; to specify as in a list or catalogue.
|
---|
1283 |
|
---|
1284 | With C enums, if you know the lowest and highest value, you can simply loop
|
---|
1285 | over them in a for loop (this is, of course, why so many enums come with an
|
---|
1286 | ENUM_WHATEVER_LAST value). But, I would be hesitant to use the word "loop" to
|
---|
1287 | describe enumerability, since in functional languages, you would recurse for
|
---|
1288 | such a purpose.
|
---|
1289 |
|
---|
1290 | In Haskell, in order to do something with every member of an "enumeration", you
|
---|
1291 | would have to explicitly list them all. The type system will help a bit since
|
---|
1292 | it knows if you haven't listed them all, but you would have to statically have
|
---|
1293 | every element in the enumeration. If somebody added new elements to the
|
---|
1294 | enumeration later, your code to enumerate over them would no longer work
|
---|
1295 | correctly, because you can't simply say "for each member of this enumeration do
|
---|
1296 | X". In Haskell that's because there aren't actually enumerations; what they use
|
---|
1297 | as enumerations are a degenerate form of algebraic datatypes, and ADTs are
|
---|
1298 | certainly not enumerable. In OCaml, you've demonstrated that they impose
|
---|
1299 | comparability, but I would still assume that you can't make a loop over every
|
---|
1300 | member of an enumeration. (But, who knows!)
|
---|
1301 |
|
---|
1302 | Since that's literally what "enumerate" means, it seems like a rather important
|
---|
1303 | property for enumerations to have ;)
|
---|
1304 |
|
---|
1305 | With valediction,
|
---|
1306 | - Gregor Richards
|
---|
1307 |
|
---|
1308 |
|
---|
1309 | From: Andrew James Beach <ajbeach@uwaterloo.ca>
|
---|
1310 | To: Gregor Richards <gregor.richards@uwaterloo.ca>, Peter Buhr <pabuhr@uwaterloo.ca>
|
---|
1311 | CC: Michael Leslie Brooks <mlbrooks@uwaterloo.ca>, Fangren Yu <f37yu@uwaterloo.ca>,
|
---|
1312 | Jiada Liang <j82liang@uwaterloo.ca>
|
---|
1313 | Subject: Re: Re:
|
---|
1314 | Date: Thu, 21 Mar 2024 14:26:36 +0000
|
---|
1315 |
|
---|
1316 | Does this mean that not all enum declarations in C create enumerations? If you
|
---|
1317 | declare an enumeration like:
|
---|
1318 |
|
---|
1319 | enum Example {
|
---|
1320 | Label,
|
---|
1321 | Name = 10,
|
---|
1322 | Tag = 3,
|
---|
1323 | };
|
---|
1324 |
|
---|
1325 | I don't think there is any way to enumerate (iterate, loop, recurse) over these
|
---|
1326 | values without listing all of them.
|
---|
1327 |
|
---|
1328 |
|
---|
1329 | Date: Thu, 21 Mar 2024 10:31:49 -0400
|
---|
1330 | Subject: Re:
|
---|
1331 | To: Andrew James Beach <ajbeach@uwaterloo.ca>, Peter Buhr <pabuhr@uwaterloo.ca>
|
---|
1332 | CC: Michael Leslie Brooks <mlbrooks@uwaterloo.ca>, Fangren Yu <f37yu@uwaterloo.ca>,
|
---|
1333 | Jiada Liang <j82liang@uwaterloo.ca>
|
---|
1334 | From: Gregor Richards <gregor.richards@uwaterloo.ca>
|
---|
1335 |
|
---|
1336 | I consider this conclusion reasonable. C enums can be nothing more than const
|
---|
1337 | ints, and if used in that way, I personally wouldn't consider them as
|
---|
1338 | enumerations in any meaningful sense, particularly since the type checker
|
---|
1339 | essentially does nothing for you there. Then they're a way of writing consts
|
---|
1340 | repeatedly with some textual indicator that these definitions are related; more
|
---|
1341 | namespace, less enum.
|
---|
1342 |
|
---|
1343 | When somebody writes bitfield members as an enum, is that *really* an
|
---|
1344 | enumeration, or just a use of the syntax for enums to keep related definitions
|
---|
1345 | together?
|
---|
1346 |
|
---|
1347 | With valediction,
|
---|
1348 | - Gregor Richards
|
---|
1349 |
|
---|
1350 |
|
---|
1351 | Date: Tue, 16 Apr 2024 11:04:51 -0400
|
---|
1352 | Subject: Re: C unnamed enumeration
|
---|
1353 | To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
|
---|
1354 | CC: <ajbeach@uwaterloo.ca>, <j82liang@uwaterloo.ca>, <mlbrooks@uwaterloo.ca>,
|
---|
1355 | <f37yu@uwaterloo.ca>
|
---|
1356 | From: Gregor Richards <gregor.richards@uwaterloo.ca>
|
---|
1357 |
|
---|
1358 | On 4/16/24 09:55, Peter A. Buhr wrote:
|
---|
1359 | > So what is a variant? Is it a set of tag names, which might be a union or is it
|
---|
1360 | > a union, which might have tag names?
|
---|
1361 |
|
---|
1362 | Your tagless variant bears no resemblance to variants in any functional
|
---|
1363 | programming language. A variant is a tag AND a union. You might not need to put
|
---|
1364 | anything in the union, in which case it's a pointless union, but the named tag
|
---|
1365 | is absolutely mandatory. That's the thing that varies.
|
---|
1366 |
|
---|
1367 | I was unaware of std::variant. As far as functional languages are concerned,
|
---|
1368 | std::variant IS NOT A VARIANT. Perhaps it would be best to use the term ADT for
|
---|
1369 | the functional language concept, because that term has no other meanings.
|
---|
1370 |
|
---|
1371 | An ADT cannot not have a named tag. That's meaningless. The tag is the data
|
---|
1372 | constructor, which is the thing you actually define when you define an ADT. It
|
---|
1373 | is strictly the union that's optional.
|
---|
1374 |
|
---|
1375 | With valediction,
|
---|
1376 | - Gregor Richards
|
---|
1377 | \end{comment}
|
---|
1378 |
|
---|
1379 |
|
---|
1380 | \section{Comparison}
|
---|
1381 |
|
---|
1382 | \VRef[Table]{t:FeatureLanguageComparison} shows a comparison of enumeration features and programming languages.
|
---|
1383 | The features are high level and may not capture nuances within a particular language
|
---|
1384 | The @const@ feature is simple macros substitution and not a typed enumeration.
|
---|
1385 |
|
---|
1386 | \begin{table}
|
---|
1387 | \caption{Enumeration Feature / Language Comparison}
|
---|
1388 | \label{t:FeatureLanguageComparison}
|
---|
1389 | \small
|
---|
1390 | \setlength{\tabcolsep}{3pt}
|
---|
1391 | \newcommand{\CM}{\checkmark}
|
---|
1392 | \begin{tabular}{r|c|c|c|c|c|c|c|c|c|c|c|c|c}
|
---|
1393 | &Pascal & Ada &\Csharp& OCaml & Java &Modula-3&Golang& Rust & Swift & Python& C & \CC & \CFA \\
|
---|
1394 | \hline
|
---|
1395 | @const@ & \CM & & & & & & \CM & & & & & \CM & \\
|
---|
1396 | \hline
|
---|
1397 | \hline
|
---|
1398 | opaque & & & & & & & & & & & & & \CM \\
|
---|
1399 | \hline
|
---|
1400 | typed & & & & & & & & & & & @int@ & integral & @T@ \\
|
---|
1401 | \hline
|
---|
1402 | safe & & & & & & & & & & & & \CM & \CM \\
|
---|
1403 | \hline
|
---|
1404 | ordered & & & & & & & & & & & \CM & \CM & \CM \\
|
---|
1405 | \hline
|
---|
1406 | dup. values & & & & & & & & & & alias & \CM & \CM & \CM \\
|
---|
1407 | \hline
|
---|
1408 | setable & & & & & & & & & & & \CM & \CM & \CM \\
|
---|
1409 | \hline
|
---|
1410 | auto-init & & & & & & & & & & & \CM & \CM & \CM \\
|
---|
1411 | \hline
|
---|
1412 | (Un)Scoped & & & & & & & & & & & U & U/S & U/S \\
|
---|
1413 | \hline
|
---|
1414 | overload & & \CM & & & & & & & & & & \CM & \CM \\
|
---|
1415 | \hline
|
---|
1416 | switch & & & & & & & & & & & \CM & \CM & \CM \\
|
---|
1417 | \hline
|
---|
1418 | loop & & & & & & & & & & & & & \CM \\
|
---|
1419 | \hline
|
---|
1420 | array/subscript & & & & & & & & & & & \CM & & \CM \\
|
---|
1421 | \hline
|
---|
1422 | subtype & & & & & & & & & & & & & \CM \\
|
---|
1423 | \hline
|
---|
1424 | inheritance & & & & & & & & & & & & & \CM \\
|
---|
1425 | \end{tabular}
|
---|
1426 | \end{table}
|
---|