Changeset 022bce0 for doc/theses
- Timestamp:
- Feb 25, 2024, 6:13:37 PM (7 months ago)
- Branches:
- master
- Children:
- 1bb76ad, a4da45e
- Parents:
- 6c68893
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/intro.tex
r6c68893 r022bce0 3 3 Naming values is a common practice in mathematics and engineering, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MHz (1E6), etc. 4 4 Naming is also commonly used to represent many other numerical phenomenon, such as days of the week, months of a year, floors of a building (basement), specific times (noon, New Years). 5 Many programming languages capture this important software -engineering capability through a mechanism called an \Newterm{enumeration}.5 Many programming languages capture this important software engineering capability through a mechanism called an \Newterm{enumeration}. 6 6 An enumeration is similar to other programming-language types by providing a set of constrained values, but adds the ability to name \emph{all} the values in its set. 7 7 Note, all enumeration names must be unique but different names can represent the same value (eight note, quaver), which are synonyms. -
doc/theses/jiada_liang_MMath/relatedwork.tex
r6c68893 r022bce0 25 25 There is no notion of an ordered set, modulo the \lstinline[language=pascal]{set of} type. 26 26 27 Free Pascal is a modern, object-oriented version of classic Pascal, with a C-style enumeration type.28 Enumerators must be inassigned in ascending numerical order with a constant expression and the range can be non-consecutive.27 Free Pascal~\cite[\S~3.1.1]{FreePascal} is a modern, object-oriented version of classic Pascal, with a C-style enumeration type. 28 Enumerators must be assigned in ascending numerical order with a constant expression and the range can be non-consecutive. 29 29 \begin{pascal} 30 30 Type EnumType = ( one, two, three, forty @= 40@, fortyone ); 31 31 \end{pascal} 32 32 Pseudo-functions @Pred@ and @Succ@ can only be used if the range is consecutive. 33 The underlying type is an implementation-defined integral 34 The size underlying integral type can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg:33 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. 34 The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg: 35 35 \begin{pascal} 36 36 Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three ); … … 64 64 @for RGB use ( Red => 10, Green => 20, Blue => 30 );@ -- ascending order 65 65 \end{ada} 66 Hence, the position, value, label tuples are: 67 \begin{ada} 68 (0, 10, RED) (1, 20, GREEN) (2, 30, BLUE) 69 \end{ada} 66 70 67 71 Like C, Ada enumerators are unscoped, \ie enumerators declared inside of an enum are visible (projected) into the enclosing scope. … … 74 78 \end{ada} 75 79 Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded names. 76 Figure~\VRef{f:AdaEnumeration} shows how ambiguities arehandled using a cast, \eg \lstinline[language=ada]{RGB'(Red)}.80 \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \eg \lstinline[language=ada]{RGB'(Red)}. 77 81 78 82 \begin{figure} … … 81 85 procedure test is 82 86 type RGB is ( @Red@, Green, Blue ); 83 type Traffic_Light is ( @Red@, Yellow, Green ); 84 procedure @Red@( Colour : RGB ) is begin 87 type Traffic_Light is ( @Red@, Yellow, Green ); -- overload 88 procedure @Red@( Colour : RGB ) is begin -- overload 85 89 Put_Line( "Colour is " & RGB'Image( Colour ) ); 86 90 end Red; 87 procedure @Red@( TL : Traffic_Light ) is begin 91 procedure @Red@( TL : Traffic_Light ) is begin -- overload 88 92 Put_Line( "Light is " & Traffic_Light'Image( TL ) ); 89 93 end Red; … … 98 102 \end{figure} 99 103 100 Ada provides an alias mechanism called \lstinline[language=ada]{renames} for aliasing types, especially to shorten package type names.104 Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package names. 101 105 \begin{ada} 102 106 OtherRed : RGB renames Red; 103 107 \end{ada} 104 which suggests the \CFA extension to @typedef@.108 which suggests a possible \CFA extension to @typedef@. 105 109 \begin{cfa} 106 110 typedef RGB.Red OtherRed; 107 111 \end{cfa} 108 112 109 There are four enumeration pseudo functions (attributes): @'Pos@, @'Val@, @'Image@, and @'Value@. 110 Function @Pos@ returns the enumerator position; the inverse function @Val@ returns the corresponding enumerator. 111 \begin{ada} 112 RGB'Pos(Red) = 0 -- '=' => equality 113 RGB'Val(0) = Red 114 \end{ada} 115 Funcion @Image@ returns the enumerator label (in capital letters); the inverse function @Value@ returns the corresponding enumerator. 116 \begin{ada} 117 RGB'Image( Red ) = "RED" 118 RGB'Value("Red") = Red 119 \end{ada} 120 These attributes are important for simple IO (there are more elaborate IO facilities in @Ada.Text_IO@ for enumeration types). 121 As well, an enumeration type @T@ has the additional 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. 122 123 Ada leverages enumerations as the bases for character and boolean types. 124 Rather unique to Ada is the use of character literals as enumeration literals: 125 \begin{ada} 126 type ABC is ('A', 'B', 'C'); 127 \end{ada} 128 This literal @'A'@ has nothing in common with the literal @'A'@ of the predefined type @Character@ (or @Wide_Character@). 129 130 Every type that has at least one character literal is a character type. 131 For every character type, string literals and the concatenation operator @"&"@ are also implicitly defined. 132 \begin{ada} 133 type My_Character is (No_Character, 'a', Literal, 'z'); 134 type My_String is array (Positive range <>) of My_Character; 135 136 S: My_String := "aa" & Literal & "za" & 'z'; 137 T: My_String := ('a', 'a', Literal, 'z', 'a', 'z'); 138 \end{ada} 139 In this example, @S@ and @T@ have the same value. 140 141 Ada's @Character@ type is defined that way. 142 See Ada Programming/Libraries/Standard. 143 144 The boolean type is defined as an enumeration: 113 There are three pairs of inverse enumeration pseudo-functions (attributes): @'Pos@ and @'Val@, @'Enum_Rep@ and @'Enum_Val@, and @'Image@ and @'Value@, 114 \begin{cquote} 115 \lstDeleteShortInline@ 116 \setlength{\tabcolsep}{15pt} 117 \begin{tabular}{@{}ll@{}} 118 \begin{ada} 119 RGB'Pos( Red ) = 0; 120 RGB'Enum_Rep( Red ) = 10; 121 RGB'Image( Red ) = "RED"; 122 \end{ada} 123 & 124 \begin{ada} 125 RGB'Val( 0 ) = Red 126 RGB'Enum_Val( 10 ) = Red 127 RGB'Value( "Red" ) = Red 128 \end{ada} 129 \end{tabular} 130 \lstMakeShortInline@ 131 \end{cquote} 132 These attributes are important for IO. 133 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. 134 135 Ada allows the enumerator label to be a character constant. 136 \begin{ada} 137 type Operator is ( '+', '-', '*', '/' ); 138 Op : Operator; 139 \end{ada} 140 which is syntactic sugar for the label and not character literals from the predefined type @Character@. 141 The purpose is readability using character literals rather than names. 142 \begin{ada} 143 Op := '+'; 144 case Op is -- all enumerators must appear 145 when '+' => ... ; 146 when '-' => ... ; 147 when '*' => ... ; 148 when '/' => ... ; 149 end case; 150 \end{ada} 151 Arrays of character enumerators can be treated as strings. 152 \begin{ada} 153 Ops : array( 0..3 ) of Operator; 154 Ops := @"+-*/"@; -- string assignment to array elements 155 Ops := @"+-" & "*/"@; -- string concatenation and assignment 156 for Op of Ops loop 157 Put_Line( Operator'Image( Op ) ); 158 end loop; 159 \end{ada} 160 Ada's @Character@ type is defined as a character enumeration across all Latin-1 characters. 161 162 Ada's boolean type is defined as a special enumeration, which can be used in conditions. 145 163 \begin{ada} 146 164 type Boolean is (False, True); -- False / True not keywords 147 165 @Flag@ : Boolean; 148 \end{ada} 149 which can be used in conditions. 150 \begin{ada} 151 if @Flag@ then ... 166 if @Flag@ then ... -- conditional 152 167 \end{ada} 153 168 Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially a builtin type. 154 169 155 Ada provides a simple \emph{consecutive} subset (subtype) of an enumeration using \lstinline[language=ada]{range}. 156 \begin{ada} 157 type BW is ( Cyan, Megenta, Yellow, Red, Green, Blue ); 158 type CMY is new BW @range@ Cyan .. Yellow; 159 type RGB is new BW @range@ Red .. Blue; 170 Ada provides \emph{consecutive} subtyping of an enumeration using \lstinline[language=ada]{range}. 171 \begin{ada} 172 type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); 173 subtype Weekday is Week @range Mon .. Fri@; 174 subtype Weekend is Week @range Sat .. Sun@; 175 Day : Week; 160 176 \end{ada} 161 177 Hence, the ordering of the enumerators is crucial to provide the necessary ranges. 162 178 163 \begin{ada} 164 type Subtype_Name is (Id1, Id2, Id3 ... ); 165 \end{ada} 166 where @Id1@, @Id2@, etc. are identifiers or characters literals. 167 In either case, the legal values of the type are referred to as "enumeration literals." 168 Each of these values has a "position number" corresponding to its position in the list such that @Id1@ has position 0, @Id2@ has position 1, and the Nth value has position N-1. 169 170 The enumeration attributes @First@ and @Succ@ allow stepping through a subsequence of the values. 171 \begin{ada} 172 case Day_Of_Week'First is 173 when Sunday => 174 ISO (False); 175 when Day_Of_Week'Succ(Sunday) => 176 ISO (True); 177 when Tuesday .. Saturday => 178 raise Program_Error; 179 An enumeration type can be used in the Ada \lstinline[language=ada]{case} (switch) and iterating constructs. 180 \begin{cquote} 181 \lstDeleteShortInline@ 182 \setlength{\tabcolsep}{15pt} 183 \begin{tabular}{@{}ll@{}} 184 \begin{ada} 185 case Day is 186 when @Mon .. Fri@ => ... ; 187 when @Sat .. Sun@ => ... ; 179 188 end case; 180 189 \end{ada} 181 A loop will automatically step through the values of the subtype's range. 182 Filtering week days to include only working days with an even position number: 183 \begin{ada} 184 for Day in Working_Day loop 185 if Day_Of_Week'Pos(Day) mod 2 = 0 then 186 Work_In_Backyard; 187 end if; 188 end loop; 189 \end{ada} 190 Enumeration types can be used as array index subtypes, yielding a table feature: 191 \begin{ada} 192 type Officer_ID is range 0 .. 50; 193 type Schedule is array (Working_Day) of Officer_ID; 190 & 191 \begin{ada} 192 case Day is 193 when @Weekday@ => ... ; -- subtype ranges 194 when @Weekend@ => ... ; 195 end case; 196 \end{ada} 197 \end{tabular} 198 \end{cquote} 199 200 \begin{cquote} 201 \setlength{\tabcolsep}{12pt} 202 \begin{tabular}{@{}lll@{}} 203 \begin{ada} 204 for Day in @Mon .. Sun@ loop 205 ... 206 end loop; 207 \end{ada} 208 & 209 \begin{ada} 210 for Day in @Weekday@ loop 211 ... 212 end loop; 213 \end{ada} 214 & 215 \begin{ada} 216 for Day in @Weekend@ loop 217 ... 218 end loop; 219 \end{ada} 220 \end{tabular} 221 \lstMakeShortInline@ 222 \end{cquote} 223 224 An enumeration type can be used as an array dimension and subscript. 225 \begin{ada} 226 Lunch : array( @Week@ ) of Time; 227 for Day in Week loop 228 Lunch( @Day@ ) := ... ; -- set lunch time 229 end loop; 194 230 \end{ada} 195 231 … … 248 284 int i = A; i = e; $\C{// implicit casts to int}$ 249 285 \end{c++} 250 \CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct}), so the enumerators are local to the enumeration and must be accessed using type qualification.286 \CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct}), where the enumerators are accessed using type qualification. 251 287 \begin{c++} 252 288 enum class E { A, B, C }; … … 254 290 e = B; $\C{// B not in scope}$ 255 291 \end{c++} 256 \CC{20} supports unscoped accesswith a \lstinline[language=c++]{using enum} declaration.292 \CC{20} supports explicit unscoping with a \lstinline[language=c++]{using enum} declaration. 257 293 \begin{c++} 258 294 enum class E { A, B, C }; … … 261 297 e = B; $\C{// direct access}$ 262 298 \end{c++} 263 \CC{11} added the ability to explicitly declare the underlying integraltype for \lstinline[language=c++]{enum class}.299 \CC{11} added the ability to explicitly declare the underlying \emph{integral} type for \lstinline[language=c++]{enum class}. 264 300 \begin{c++} 265 301 enum class RGB @: long@ { Red, Green, Blue }; … … 292 328 enum Weekday : byte { Monday, Tuesday, Wednesday, Thursday@ = 10@, Friday, Saturday, Sunday@,@ }; 293 329 \end{csharp} 294 The default underlying type is @int@, with auto-incrementing, implicit/explicit in tialization, terminator comma, and optional integral typing (default @int@)330 The default underlying type is @int@, with auto-incrementing, implicit/explicit initialization, terminator comma, and optional integral typing (default @int@) 295 331 A method cannot be defined in an enumeration type. 296 332 As well, there is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts.
Note: See TracChangeset
for help on using the changeset viewer.