Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r508cff0 r29c8675  
    11\chapter{Related Work}
    22\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}
    193
    204Enumeration-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} \see{discussion in \VRef{s:AlgebraicDataType}}, Java~\cite{Java}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.
     
    4529type Boolean = ( false, true );
    4630\end{pascal}
    47 The enumeration ordering supports the relational operators @=@, @<>@, @<@, @<=@, @>=@, and @>@, provided both operands are the same (sub)type.
     31The enumeration supports the relational operators @=@, @<>@, @<@, @<=@, @>=@, and @>@, interpreted as as comparison in terms of declaration order.
    4832
    4933The following auto-generated pseudo-functions exist for all enumeration types:
     
    6549         wend : Weekend;
    6650\end{pascal}
    67 Hence, the ordering of the enumerators is crucial to provide the necessary ranges.
     51Hence, declaration order of enumerators is crucial to provide the necessary ranges.
    6852There is a bidirectional assignment between the enumeration and its subranges.
    6953\begin{pascal}
     
    151135
    152136The 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.
    153 The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
     137The integral size can be explicitly specified using compiler directive \$@PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
    154138\begin{pascal}
    155139Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three );
     
    199183\end{figure}
    200184
    201 Enumerators without initialization are auto-initialized from left to right, starting at zero, incrementing by 1.
     185Enumerators without initialization are auto-initialized from left to right, starting at zero and incrementing by 1.
    202186Enumerators with initialization must set \emph{all} enumerators in \emph{ascending} order, \ie there is no auto-initialization.
    203187\begin{ada}
     
    382366\end{c++}
    383367\CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct})\footnote{
    384 The use of keyword \lstinline[language=c++]{class} is resonable because default visibility is \lstinline[language=c++]{private} (scoped).
     368The use of keyword \lstinline[language=c++]{class} is reasonable because default visibility is \lstinline[language=c++]{private} (scoped).
    385369However, default visibility for \lstinline[language=c++]{struct} is \lstinline[language=c++]{public} (unscoped) making it an odd choice.},
    386370where the enumerators are accessed using type qualification.
     
    396380E e = A;    e = B;                                              $\C{// direct access}$
    397381\end{c++}
    398 \CC{11} added the ability to explicitly declare only an underlying \emph{integral} type for \lstinline[language=c++]{enum class}.
     382\CC{11} added the ability to explicitly declare an underlying \emph{integral} type for \lstinline[language=c++]{enum class}.
    399383\begin{c++}
    400384enum class RGB @: long@ { Red, Green, Blue };
     
    430414\end{tabular}
    431415\end{cquote}
    432 However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values.
     416However, there is no mechanism to iterate through an enumeration.
     417A common workaround is to iterate over enumerator as integral values, but it only works if
     418enumerators resemble a sequence of natural, i.e., enumerators are auto-initialized.
     419Otherwises, the iteration would have integers that are not enumeration values.
    433420\begin{c++}
    434421enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
     
    449436% https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/enums
    450437
    451 \Csharp is a dynamically-typed programming language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}.
     438\Csharp is a programming language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}.
    452439\begin{csharp}
    453440enum Week : @long@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }
     
    508495\end{cquote}
    509496
    510 To indirectly enumerate, \Csharp's Enum library has @Enum.GetValues@, a pseudo-method that retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).
     497To indirectly enumerate, \Csharp's Enum library provides @Enum.GetValues@,
     498% a pseudo-method that retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).
     499a static memeber of abstract Enum type that return a reference to an array of all enumeration constants.
     500Internally, a Enum type has a static member called @fieldInfoHash@, a @Hashtable@ that stores enumerators information. The field is populated on-demand:
     501it only contains information if a @reflection@ like @GetValues@ is called. But the information will be cached, so the cost of reflection is paid only
     502once throughout the lifetime of a program. @GetValues@ then converts a @Hashtable@ to an @Array@, which supports enumerating.
    511503\begin{csharp}
    512504foreach ( Week d in @Enum.GetValues@( typeof(Week) ) ) {
     
    535527\label{s:Go}
    536528
    537 Go has a no enumeration.
    538 It has @const@ aliasing declarations, similar to \CC \see{\VRef{s:C++RelatedWork}}, for basic types with type inferencing and static initialization (constant expression).
     529Go has @const@ aliasing declarations, similar to \CC \see{\VRef{s:C++RelatedWork}}, for basic types with type inferencing and static initialization (constant expression).
     530The most basic form of constant definition is a @const@ keyword, followed by the name of constant, an optional type declaration of the constant, and a mandatory initialize.
     531For exmaple:
    539532\begin{Go}
    540533const R @int@ = 0;  const G @uint@ = 1;  const B = 2; $\C{// explicit typing and type inferencing}$
     
    544537const V = 3.1;  const W = 3.1;
    545538\end{Go}
    546 Since these declarations are immutable variables, they are unscoped and Go has no overloading.
    547 
    548 Go provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.
     539Since these declarations are immutable variables, they are unscoped and Go has no overloading. If no type declaration provided, Go infers
     540type from the initializer expression.
     541
     542% Go provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.
     543These named constants can be grouped together in one @const@ declaration block and introduces a form of auto-initialization.
    549544\begin{Go}
    550545const ( R = 0; G; B )                                   $\C{// implicit initialization: 0 0 0}$
    551546const ( Fred = "Fred"; Mary = "Mary"; Jane = "Jane" ) $\C{// explicit initialization: Fred Mary Jane}$
    552 const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// type change, implicit/explicit: 0 0 USA USA 3.1 3.1}$
     547const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// implicit/explicit: 0 0 USA USA 3.1 3.1}$
    553548\end{Go}
    554549The first identifier \emph{must} be explicitly initialized;
    555550subsequent identifiers can be implicitly or explicitly initialized.
    556 Implicit initialization is the \emph{previous} (predecessor) identifier value.
    557 
    558 Each @const@ declaration provides an implicit integer counter starting at zero, called \lstinline[language=Go]{iota}.
     551Implicit initialization always uses the \emph{previous} (predecessor) constant expression initializer.
     552
     553% Each @const@ declaration provides an implicit integer counter starting at zero, called \lstinline[language=Go]{iota}.
     554Each const declaration is often paired with a const expression \lstinline[language=Go]{iota} to re-define its
     555implicit initialization. \lstinline[language=Go]{iota} represents an sequence of natural number starting from zero.
     556Every implicit or explicit \lstinline[language=Go]{iota} increments the value of the expression by one.
    559557Using \lstinline[language=Go]{iota} outside of a @const@ block always sets the identifier to zero.
    560558\begin{Go}
    561559const R = iota;                                                 $\C{// 0}$
    562560\end{Go}
    563 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.
     561% 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.
     562Inside a @const@ block, if a constant has \lstinline[language=Go]{iota} initializer, its successor will also use \lstinline[language=Go]{iota} initializer.
     563\lstinline[language=Go]{iota} is no different than other constant expression when it is used in implicit initialization, but
     564thanks to the increment natural of \lstinline[language=Go]{iota}, the successor will have a value equal to its predecessor plus 1.
    564565\begin{Go}
    565566const ( R = @iota@; G; B )                              $\C{// implicit: 0 1 2}$
     567% const ( C = @iota + B + 1@; G; Y )            $\C{// implicit: 3 4 5}$
     568\end{Go}
     569The constant blocks from the previous example is equivalanet to:
     570\begin{Go}
     571const ( R = @iota@; G=@iota@; B=@iota@ )                                $\C{// implicit: 0 1 2}$
     572\end{Go}
     573R, G, B have values 0, 1, 2, respectively, because \lstinline[language=Go]{iota} is an increasing.
     574
     575Similarly,
     576\begin{Go}
    566577const ( C = @iota + B + 1@; G; Y )              $\C{// implicit: 3 4 5}$
    567578\end{Go}
    568 An underscore \lstinline[language=golang]{const} identifier advances \lstinline[language=Go]{iota}.
     579can be rewritten as:
    569580\begin{Go}
    570 const ( O1 = iota + 1; @_@; O3; @_@; O5 ) // 1, 3, 5
     581const ( C = @iota + B + 1@; G = @iota + B + 1@; Y = @iota + B + 1@ )            $\C{// implicit: 3 4 5}$
    571582\end{Go}
    572 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.
     583Go's grouped constants do not define a new type, and constants in the same block can have heterogeneous types.
     584These two characteristics differs a grouped constant from an enumeration, but also gives a direction on approximating enumeration in Go:
     585first to define a new type externally, and make sure all constants in the same group will have the new type.
    573586\begin{Go}
    574 const ( Mon = iota; Tue; Wed; // 0, 1, 2
    575                 @Thu = 10@; Fri; Sat; @Sun = itoa@ ) $\C{// 10, 10, 10, {\color{red}6}}$
     587type Language int64
     588const (
     589        C Language = iota
     590        CPP
     591        CSharp
     592        CFA
     593        Go
     594)
    576595\end{Go}
    577 Auto-initialization from \lstinline[language=Go]{iota} is restarted and \lstinline[language=Go]{iota} reinitialized with an expression containing at most \emph{one} \lstinline[language=Go]{iota}.
    578 \begin{Go}
    579 const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@ + 1; V5 ) // 0 1 7 4 5
    580 const ( Mon = iota; Tue; Wed; // 0, 1, 2
    581                 @Thu = 10;@ Fri = @iota@ - Wed + Thu - 1; Sat; Sun ) // 10, 11, 12, 13
    582 \end{Go}
    583 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 initialization expressions containing \lstinline[language=Go]{iota}.
    584 Note, because \lstinline[language=Go]{iota} is incremented for an explicitly initialized identifier or @_@,
    585 at @Fri@ \lstinline[language=Go]{iota} is 4 requiring the minus one to compute the value for @Fri@.
     596By typing the first constant as @Language@ and assigning initializer with \lstinline[language=Go]{iota}, all other constants will have the same type
     597and the same initialzer. It is a close approximation, but it is not a real enumeration. The definition of the "enumerated type" is separate from
     598the "enumerator definition", and nothing stop outside constants to have the type @Language@.
     599
     600% An underscore \lstinline[language=golang]{const} identifier advances \lstinline[language=Go]{iota}.
     601% \begin{Go}
     602% const ( O1 = iota + 1; @_@; O3; @_@; O5 ) // 1, 3, 5
     603% \end{Go}
     604% 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.
     605% \begin{Go}
     606% const ( Mon = iota; Tue; Wed; // 0, 1, 2
     607%               @Thu = 10@; Fri; Sat; @Sun = itoa@ ) $\C{// 10, 10, 10, {\color{red}6}}$
     608% \end{Go}
     609% Auto-initialization from \lstinline[language=Go]{iota} is restarted and \lstinline[language=Go]{iota} reinitialized with an expression containing at most \emph{one} \lstinline[language=Go]{iota}.
     610% \begin{Go}
     611% const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@ + 1; V5 ) // 0 1 7 4 5
     612% const ( Mon = iota; Tue; Wed; // 0, 1, 2
     613%               @Thu = 10;@ Fri = @iota@ - Wed + Thu - 1; Sat; Sun ) // 10, 11, 12, 13
     614% \end{Go}
     615% 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 initialization expressions containing \lstinline[language=Go]{iota}.
     616% Note, because \lstinline[language=Go]{iota} is incremented for an explicitly initialized identifier or @_@,
     617% at @Fri@ \lstinline[language=Go]{iota} is 4 requiring the minus one to compute the value for @Fri@.
     618
    586619
    587620Basic switch and looping are possible.
     
    610643\end{tabular}
    611644\end{cquote}
    612 However, the loop prints the values from 0 to 13 because there is no actual enumeration.
     645However, the loop in this example prints the values from 0 to 13 because there is no actual enumeration.
    613646
    614647A constant variable can be used as an array dimension or a subscript.
     
    627660Week day = Week.Sat;
    628661\end{Java}
    629 The enumerator's members are scoped and cannot be made \lstinline[language=java]{public}, hence requiring qualification.
     662The enumerator's members are scoped and requires qualification.
    630663The value of an enumeration instance is restricted to its enumerators.
    631664
     
    663696If 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@.
    664697The implementation constructor must be private since it is only used internally to initialize the enumerators.
    665 Initialization occurs at the enumeration-type declaration for each enumerator in the first line.
     698Initialization occurs at the enumeration-type declaration.
    666699
    667700Enumerations can be used in the @if@ and @switch@ statements but only for equality tests.
     
    691724
    692725There are no arithmetic operations on enumerations, so there is no arithmetic way to iterate through an enumeration without making the implementation type \lstinline[language=Java]{public}.
    693 Like \Csharp, looping over an enumeration is done using method @values@, which returns an array of enumerator values (expensive operation).
     726Like \Csharp, looping over an enumeration is done using static method @values@, which returns an array of enumerator values.
     727Unfortunately, @values@ is an expensive @O(n)@ operation because it creates a new array every time it is called.
    694728\begin{Java}
    695729for ( Week d : Week.values() ) {
     
    700734Like \Csharp, enumerating is supplied indirectly through another enumerable type, not via the enumeration.
    701735
     736% 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.
     737% There is also a specialized version of @HashMap@ with enumerator keys, which has performance benefits.
     738Java provides @EnumSet@, an auxiliary data structure that takes an enum @class@ as parameter (Week.class) for its construction, and it contains members only with the supplied
     739enum type. @EnumSet@ is enumerable because it extends @AbstractSet@ interfaces and thus supports direct enumerating via @forEach@. It also has subset operation
     740@range@ and it is possible to add to and remove from members of the set.
     741@EnumSet@ supports more enumeration features, but it is not an enumeration type: it is a set of enumerators from a pre-define enum.
     742
     743
    702744An enumeration type cannot declare an array dimension nor can an enumerator be used as a subscript.
    703745Enumeration inheritence is disallowed because an enumeration is \lstinline[language=Java]{final}.
    704 
    705 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.
    706 There is also a specialized version of @HashMap@ with enumerator keys, which has performance benefits.
    707 
    708746
    709747\section{Rust}
     
    733771adt = ADT::S(s);  println!( "{:?}", adt );
    734772@match@ adt {
    735         ADT::I( i ) => println!( "{:}", i ),
    736         ADT::F( f ) => println!( "{:}", f ),
    737         ADT::S( s ) => println!( "{:} {:}", s.i, s.j ),
     773        ADT::I( i ) $=>$ println!( "{:}", i ),
     774        ADT::F( f ) $=>$ println!( "{:}", f ),
     775        ADT::S( s ) $=>$ println!( "{:} {:}", s.i, s.j ),
    738776}
    739777\end{rust}
     
    757795let mut week : Week = Week::Mon;
    758796match week {
    759         Week::Mon => println!( "Mon" ),
     797        Week::Mon $=>$ println!( "Mon" ),
    760798        ...
    761         Week::Sun => println!( "Sun" ),
     799        Week::Sun $=>$ println!( "Sun" ),
    762800}
    763801\end{rust}
     
    813851        Week::Mon | Week:: Tue | Week::Wed | Week::Thu
    814852                | Week::Fri => println!( "weekday" ),
    815         Week::Sat | Week:: Sun => println!( "weekend" ),
     853        Week::Sat | Week:: Sun $=>$ println!( "weekend" ),
    816854}
    817855\end{c++}
    818856\end{tabular}
    819857\end{cquote}
    820 However, there is no mechanism to iterate through an enumeration without casting to integral and positions versus values is not handled.
     858% However, there is no mechanism to iterate through an enumeration without casting to integral and positions versus values is not handled.
     859Like C/\CC, there is no mechanism to iterate through an enumeration. It can only be approximated
     860by a loop over a range of enumerator and will not work if enumerator values is a sequence of natural numbers.
    821861\begin{c++}
    822862for d in Week::Mon as isize ..= Week::Sun as isize {
     
    8258650 1 2 @3 4 5 6 7 8 9@ 10 11 12 13
    826866\end{c++}
    827 An enumeration type cannot declare an array dimension nor as a subscript.
    828 There is no mechanism to subset or inherit from an enumeration.
     867% An enumeration type cannot declare an array dimension nor as a subscript.
     868There is no direct way to harmonize an enumeration and another data structure. For example,
     869there is no mapping from an enumerated type to an array type.
     870In terms of extensibility, there is no mechanism to subset or inherit from an enumeration.
    829871
    830872
    831873\section{Swift}
    832 
     874\label{s:Swift}
    833875% https://www.programiz.com/swift/online-compiler
    834 
    835 Like Rust, Swift @enum@ provides two largely independent mechanisms from a single language feature: an ADT and an enumeration.
     876Despite being named as enumeration, A Swift @enum@ is in fact a ADT: cases (enumerators) of an @enum@ can have heterogeneous types and be recursive.
     877% Like Rust, Swift @enum@ provides two largely independent mechanisms from a single language feature: an ADT and an enumeration.
    836878When @enum@ is an ADT, pattern matching is used to discriminate among the variant types.
    837879\begin{cquote}
     
    875917\end{tabular}
    876918\end{cquote}
    877 Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.
    878 
    879 An enumeration is created when \emph{all} the enumerators are unit-type, which is like a scoped, opaque enumeration.
     919% Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.
     920Normally an enumeration case needs a type qualification. But in the example when pattern matching @adt@, which
     921has a type @ADT@, the context provides that the cases refer to @ADT@'s cases and no explicit type qualification is required.
     922
     923% An enumeration is created when \emph{all} the enumerators are unit-type, which is like a scoped, opaque enumeration.
     924Without type declaration for enumeration cases, a Swift enum syntax defined a unit-type enumeration, which is like a scoped, opaque enumeration.
    880925\begin{swift}
    881926enum Week { case Mon, Tue, Wed, Thu, Fri, Sat, Sun }; // unit-type
    882927var week : Week = @Week.Mon@;
    883928\end{swift}
    884 As well, it is possible to type \emph{all} the enumerators with a common type, and set different values for each enumerator;
    885 for integral types, there is auto-incrementing.
     929% As well, it is possible to type \emph{all} the enumerators with a common type, and set different values for each enumerator;
     930% for integral types, there is auto-incrementing.
     931As well, it is possible to type associated values of enumeration cases with a common types.
     932When enumeration cases are typed with a common integral type, Swift auto-initialize enumeration cases following the same initialization scheme as C language.
     933If enumeration is typed with @string@, its cases are auto-initialized to case names (labels).
    886934\begin{cquote}
    887935\setlength{\tabcolsep}{15pt}
     
    933981\end{tabular}
    934982\end{cquote}
    935 Enumerating is accomplished by inheriting from @CaseIterable@ without any associated values.
     983Enumerating is accomplished by inheriting from @CaseIterable@ protocol, which has a static
     984@enum.allCases@ property that returns a collection of all the cases for looping over an enumeration type or variable.
     985Like \CFA, Swift's default enumerator output is the case name (label). An enumerator of a typed enumeration has attribute
     986@rawValue@ that return its case value.
    936987\begin{swift}
    937988enum Week: Comparable, @CaseIterable@ {
     
    943994Mon Tue Wed Thu Fri Sat Sun
    944995\end{swift}
    945 The @enum.allCases@ property returns a collection of all the cases for looping over an enumeration type or variable (expensive operation).
    946 
    947 A typed enumeration is accomplished by inheriting from any Swift type, and accessing the underlying enumerator value is done with the attribute @rawValue@.
    948 Type @Int@ has auto-incrementing from the previous enumerator;
    949 type @String@ has auto-incrementing of the enumerator label.
     996
     997
    950998\begin{cquote}
    951999\setlength{\tabcolsep}{15pt}
     
    9751023\end{cquote}
    9761024
    977 There is a bidirectional conversion from typed enumerator to @rawValue@ and vice versa.
     1025There is a safe bidirectional conversion from typed enumerator to @rawValue@ and vice versa.
    9781026\begin{swift}
    979 var weekInt : WeekInt = WeekInt.Mon;
    9801027if let opt = WeekInt( rawValue: 0 ) {  // test optional return value
    981         print( weekInt.rawValue, opt )  // 0 Mon
     1028        print( opt.rawValue, opt )  // 0 Mon
    9821029} else {
    9831030        print( "invalid weekday lookup" )
    9841031}
    9851032\end{swift}
    986 Conversion from @rawValue@ to enumerator may fail (bad lookup), so the result is an optional value.
    987 
     1033% Conversion from @rawValue@ to enumerator may fail (bad lookup), so the result is an optional value.
     1034In the previous exmaple, the initialization of @opt@ fails when there is no enumeration cases has value equals 0, resulting in a
     1035@nil@ value. Initialization from a raw value is considered a expensive operation because it requires a value lookup.
    9881036
    9891037\section{Python 3.13}
     
    10041052class Week(!Enum!): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
    10051053\end{python}
    1006 and/or explicitly auto-initialized, \eg:
     1054and/or explicitly auto-initialized with @auto@ method, \eg:
    10071055\begin{python}
    10081056class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = !auto()!; Sat = 4; Sun = !auto()!
    10091057Mon : 1 Tue : 2 Wed : 3 Thu : 10 Fri : !11! Sat : 4 Sun : !12!
    10101058\end{python}
    1011 where @auto@ increments by 1 from the previous @auto@ value \see{Go \lstinline[language=Go]{iota}, \VRef{s:Go}}.
    1012 @auto@ is controlled by member @_generate_next_value_()@, which can be overridden:
     1059@auto@ is controlled by member @_generate_next_value_()@, which by default return one plus the highest value among enumerators, and can be overridden:
    10131060\begin{python}
    10141061@staticmethod
     
    11971244% https://dev.realworldocaml.org/runtime-memory-layout.html
    11981245
    1199 Like Haskell, OCaml @enum@ provides two largely independent mechanisms from a single language feature: an ADT and an enumeration.
     1246Like Swift (\VRef{s:Swift}) and Haskell (\VRef{s:AlgebraicDataType}), OCaml @enum@ provides two largely independent mechanisms from a single language feature: an ADT and an enumeration.
    12001247When @enum@ is an ADT, pattern matching is used to discriminate among the variant types.
    12011248\begin{cquote}
     
    12361283\end{tabular}
    12371284\end{cquote}
    1238 (Note, after an @adtv@'s type is know, the enumerator is inferred without qualification, \eg @I(3)@.)
     1285% (Note, after an @adtv@'s type is know, the enumerator is inferred without qualification, \eg @I(3)@.)
     1286
    12391287The type names are independent of the type value and mapped to an opaque, ascending, integral tag, starting from 0, supporting relational operators @<@, @<=@, @>@, and @>=@.
    12401288\begin{cquote}
     
    12781326
    12791327While OCaml enumerators have an ordering following the definition order, they are not enumerable.
    1280 To iterate over all enumerators, an OCaml type needs to derive from the @enumerate@ preprocessor, which appends a list of all enumerators to the program abstract syntax tree (AST).
    1281 However, the list of values may not persist in the defined ordering.
    1282 As a consequence, there is no meaningful enumerating mechanism.
    1283 
    1284 Enumeration subsetting is allowed but inheritance is restricted to classes not types.
    1285 \begin{ocaml}
    1286 type weekday = Mon | Tue | Wed | Thu | Fri
    1287 type weekend = Sat | Sun
    1288 type week = Weekday of weekday | Weekend of weekend
    1289 let day : week = Weekend Sun
    1290 \end{ocaml}
     1328To iterate over all enumerators, an OCaml type needs to derive from the @enumerate@ PPX (Pre-Preocessor eXtension), which appends a list of all enumerators to the program abstract syntax tree (AST).
     1329However, as stated in the documentation, @enumerate@ PPX does not guarantee the order of the list.
     1330PPX is beyond the scope of OCaml native language and it is a preprocessor directly modifying a parsed AST. In conclusion, there is no enumerating mechanism within the scope of OCaml language.
    12911331
    12921332%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    15271567opaque                  & \CM    &                              &                  & \CM    & \CM   &             & \CM         & \CM                   &               &               &               & \CM   \\
    15281568\hline
    1529 typed                   & Int    & Int                  & Integral  & H     & U     & H       & U/H         & U/H           & H     & Int       & Integral& U   \\
     1569typed                   & Int    & Int                  & Int     & H     & U     & H       & U/H         & U/H           & H       & Int       & Int   & U     \\
    15301570\hline
    15311571safety          & \CM   & \CM                   &          & \CM        & \CM   &                 & \CM                 & \CM                   &               &               & \CM   & \CM   \\
     
    15331573posn ordered    & Implied & Implied     &          & \CM    &       &             &                         &                   &       &           &       & \CM       \\
    15341574\hline
    1535 unique values   & \CM   & \CM           &           &           &       &      &                            & \CM               &       &           &       &     \\
     1575unique values   & \CM   & \CM           &           &\CM        &       &      &                            & \CM               &       &           &       &     \\
    15361576\hline
    1537 auto-init               & \CM   & all or none   & \CM      &     &       & \CM     & \CM           & \CM               & \CM   & \CM   & \CM   & \CM   \\
     1577auto-init               & \CM   & all or none   & \CM      & N/A &       & \CM     & \CM            & \CM               & \CM   & \CM   & \CM   & \CM   \\
    15381578\hline
    15391579(Un)Scoped              & U     & U                     & S        & S      & S         & U       & S               & S                         & S     & U             & U/S   & U/S   \\
     
    15451585arr. dim.               & \CM   & \CM           &                  &              &                 &           &                 &                         &            &      &               & \CM \\
    15461586\hline
    1547 subset                  & \CM   & \CM                   &         & \CM     &           &                 &                         &                   &               &               &               & \CM   \\
     1587subset                  & \CM   & \CM                   &         &      &              &                 &                         &                   &               &               &               & \CM   \\
    15481588\hline
    15491589superset                &               &                               &                 &                 &           &                 &                         &               &           &               &               & \CM   \\
     
    15591599Position ordered is implied if the enumerator values must be strictly increasingly.
    15601600\item unique value: enumerators must have a unique value.
    1561 \item auto-init: Values are auto-initializable by language specification, often being "+1" of the predecessor.
     1601\item auto-init: Values are auto-initializable by language specification. \\
     1602It is not appliable to OCaml because OCaml enumeration has unit type.
    15621603\item (Un)Scoped: U $\Rightarrow$ enumerators are projected into the containing scope.
    15631604S $\Rightarrow$ enumerators are contained in the enumeration scope and require qualification.
Note: See TracChangeset for help on using the changeset viewer.