Changeset 3b10778 for doc/theses/jiada_liang_MMath
- Timestamp:
- Aug 7, 2024, 9:12:14 AM (4 months ago)
- Branches:
- master
- Children:
- 4e09af2
- Parents:
- f6bbc92
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
rf6bbc92 r3b10778 202 202 An enumeration's type can be another enumeration. 203 203 \begin{cfa} 204 enum( char ) Letter { A = 'A', ... }; 205 enum( @Letter@ ) Greek { Alph = A, Beta = B, ... }; // alphabet intersection 206 207 void foo(Letter l); 208 foo(Beta); $\C{// foo(value(Beta))}$ 209 \end{cfa} 210 Enumeration @Greek@ may have more or less enumerators than @Letter@, but the enum values \emph{must} be of a member of @Letter@. 211 Therefore, the set of @Greek@ enum value in a subset of the value set of type @Letter@. 212 @Letter@ is type compatible with enumeration @Letter@ thanks to \CFA inserts value conversion whenever @Letter@ be used 213 in place of @Greek@. On the other hand, @Letter@ enums are not type compatible with enumeration @Greek@. 214 As a result, @Greek@ becomes a logical subtype of @Letter@. 215 216 Subset defines an implicit subtyping relationship between two \CFA enumerations. \CFA also has 217 containment inheritance for \CFA enumerations where subtyping is explicitly structured. 204 enum( char ) Letter { A = 'A', ..., Z = 'Z' }; 205 enum( @Letter@ ) Greek { Alph = @A@, Beta = @B@, Gamma = @G@, ..., Zeta = @Z@ }; // alphabet intersection 206 \end{cfa} 207 Enumeration @Greek@ may have more or less enumerators than @Letter@, but its enumerator values \emph{must} be from @Letter@. 208 Therefore, the set of @Greek@ enumerator values in a subset of the @Letter@ enumerator values. 209 @Letter@ is type compatible with enumeration @Letter@ because value conversions are inserted whenever @Letter@ is used in place of @Greek@. 210 \begin{cfa} 211 Letter l = A; $\C{// allowed}$ 212 Greek g = Alph; $\C{// allowed}$ 213 l = Alph; $\C{// allowed, conversion to base type}$ 214 g = A; $\C{// {\color{red}disallowed}}$ 215 void foo( Letter ); 216 foo( Beta ); $\C{// allowed, conversion to base type}$ 217 void bar( Greek ); 218 bar( A ); $\C{// {\color{red}disallowed}}$ 219 \end{cfa} 220 Hence, @Letter@ enumerators are not type compatible with the @Greek@ enumeration but the reverse is true. 221 218 222 219 223 \section{Inheritance} … … 223 227 Containment is nominative: an enumeration inherits all enumerators from another enumeration by declaring an @inline statement@ in its enumerator lists. 224 228 \begin{cfa} 225 enum( char * ) Names { /* $\see{\VRef[Figure]{f:EumeratorTyping}}$ */ };229 enum( char * ) Names { /* $\see{\VRef[Figure]{f:EumeratorTyping}}$ */ }; 226 230 enum( char * ) Names2 { @inline Names@, Jack = "JACK", Jill = "JILL" }; 227 231 enum( char * ) Names3 { @inline Names2@, Sue = "SUE", Tom = "TOM" }; … … 244 248 However, the position of the underlying representation is the order of the enumerator in the new enumeration. 245 249 \begin{cfa} 246 enum() E1 { B }; $\C{// B}$ 247 enum() E2 { C, D }; $\C{// C D}$ 248 enum() E3 { inline E1, inline E2, E }; $\C{// {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E}$ 249 enum() E4 { A, inline E3, F}; $\C{// A {\color{blue}[\(_{E3}\)} {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E {\color{blue}]} F }$ 250 \end{cfa} 251 In the example above, @B@ has the position 0 in @E1@ and @E3@, but it at the position 1 in @E4@ as @A@ taking the 0 in @E4@. 252 @C@ is at the position 0 in @E2@, 1 in @E3@ and 2 in E4. @D@ is at the position 1 in @E2@, 2 in @E3@ and 3 in @E4@. 253 254 A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost. Such conversion is an @enumeration conversion@. 250 enum() E1 { B }; $\C{// B}$ 251 enum() E2 { C, D }; $\C{// C D}$ 252 enum() E3 { inline E1, inline E2, E }; $\C{// {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E}$ 253 enum() E4 { A, inline E3, F}; $\C{// A {\color{blue}[\(_{E3}\)} {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E {\color{blue}]} F}$ 254 \end{cfa} 255 In the example, @B@ is at position 0 in @E1@ and @E3@, but position 1 in @E4@ as @A@ takes position 0 in @E4@. 256 @C@ is at position 0 in @E2@, 1 in @E3@, and 2 in @E4@. 257 @D@ is at position 1 in @E2@, 2 in @E3@, and 3 in @E4@. 258 259 A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost, called \newterm{enumeration conversion}. 255 260 \begin{cfa} 256 261 enum E2 e2 = C; 257 262 posn( e2 ); $\C[1.75in]{// 0}$ 258 263 enum E3 e3 = e2; 259 posn( e2 ); $\C{// 1 }$264 posn( e2 ); $\C{// 1 cost}$ 260 265 void foo( E3 e ); 261 266 foo( e2 ); 262 posn( (E3)e2 ); $\C{// 1 }$267 posn( (E3)e2 ); $\C{// 1 cost}$ 263 268 E3 e31 = B; 264 posn( e31 ); $\C{// 0 }\CRT$269 posn( e31 ); $\C{// 0 cost}\CRT$ 265 270 \end{cfa} 266 271 The last expression is unambiguous. 267 While both @E2.B@ and @E3.B@ are valid candidate , @E2.B@ has an associated safe cost and \CFA selects the zero cost candidate @E3.B@.272 While both @E2.B@ and @E3.B@ are valid candidates, @E2.B@ has an associated safe cost, so \CFA selects the zero cost candidate. 268 273 269 274 For the given function prototypes, the following calls are valid. … … 287 292 Note, the validity of calls is the same for call-by-reference as for call-by-value, and @const@ restrictions are the same as for other types. 288 293 294 289 295 \subsection{Offset Calculation} 296 290 297 As discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of a \CFA enumeration variable. 291 Because enumerators has different @position@ between subtype and supertype, \CFA might need to manipulate the representation whenever a cast or 292 implicit conversion involves two \CFA enums. \CFA determine how a position is going to change with an @offset calculation@ function, reflects to 293 enumerator as an arithmetic expression. Casting an enumeration of subtype to a supertype, the position can be unchanged or increase. The change 294 of position is an @offset@. 298 When a cast or implicit conversion moves an enumeration from subtype to supertype, the position can be unchanged or increase. 299 \CFA determines the position offset with an \newterm{offset calculation} function. 295 300 296 301 \begin{figure} … … 301 306 pair<bool, int> calculateEnumOffset(CFAEnum src, CFAEnum dst) { 302 307 int offset = 0; 303 if ( src == dst) return make_pair(true, 0);304 for ( auto v : dst.members) {305 if ( holds_alternative<Enumerator>(v)) {308 if ( src == dst ) return make_pair(true, 0); 309 for ( auto v : dst.members ) { 310 if ( holds_alternative<Enumerator>(v) ) { 306 311 offset++; 307 312 } else { 308 313 auto m = get<CFAEnum>(v); 309 if ( m == src) return make_pair(true, offset);310 auto dist = calculateEnumOffset( src, m);311 if ( dist.first) {312 return make_pair(true, offset + dist.second);314 if ( m == src ) @return@ make_pair( true, offset ); 315 auto dist = calculateEnumOffset( src, m ); 316 if ( dist.first ) { 317 @return@ make_pair( true, offset + dist.second ); 313 318 } else { 314 319 offset += dist.second; … … 316 321 } 317 322 } 318 return make_pair(false, offset);323 @return@ make_pair( false, offset ); 319 324 } 320 325 \end{cfa} … … 323 328 \end{figure} 324 329 325 Figure~\ref{s:OffsetSubtypeSuperType} shows a minimum of @offset calculation@, written in \CC. CFAEnum represents \CFA enumeration 326 which has a vector of variants of Enumerator or CFAEnum. Two CFAEnums are differentiable by their unquie name. 327 The algorithm takes two CFAEnums as parameters @src@ and @dst@, with @src@ being type of expression the conversion applies on, 328 and @dst@ being type that the expression cast into. The algorithm returns a pair of value: when @src@ is convertible to @dst@ (@src@ is a subtype of @dst@), 329 it returns boolean true and the offset. Otherwise, it returns false and the size of @src@ (total number of enumerators in @src@). The offset between a type and itself is defined 330 as 0. 331 332 The algorithm iterates over members in @dst@ to find @src@. If a member is an enumerator of @dst@, the positions of all subsequent members 333 increment by one. If the current member is @dst@, the function returns true indicating "found" and the accumulated offset. Otherwise, the algorithm recurse 334 into the current CFAEnum @m@ and find out if the @src@ is convertible to @m@. The @src@ being convertible to the current member @m@ means @src@ 335 is a "subtype of subtype" of @dst@. The offset between @src@ and @dst@ is the sum of the offset of @m@ in @dst@ and the offset of 336 @src@ in @m@. If @src@ is not a subtype of @m@, the loop continue but with offset shifted by the size of @m@. The procedure reaches the end 337 of the loop proves @src@ is not convertible to @dst@. It returns size of @src@ in case it is in a recurse innvocation and size is needed for offset update. 330 Figure~\ref{s:OffsetSubtypeSuperType} shows an outline of the offset calculation in \CC. 331 Structure @CFAEnum@ represents the \CFA enumeration with a vector of variants of @CFAEnum@ or @Enumerator@. 332 The algorithm takes two @CFAEnums@ parameters, @src@ and @dst@, with @src@ being the type of expression the conversion applies to, and @dst@ being the type the expression is cast to. 333 The algorithm iterates over the members in @dst@ to find @src@. 334 If a member is an enumerator of @dst@, the positions of all subsequent members is increment by one. 335 If the current member is @dst@, the function returns true indicating \emph{found} and the accumulated offset. 336 Otherwise, the algorithm recurses into the current @CFAEnum@ @m@ to check if its @src@ is convertible to @m@. 337 If @src@ is convertible to the current member @m@, this means @src@ is a subtype-of-subtype of @dst@. 338 The offset between @src@ and @dst@ is the sum of the offset of @m@ in @dst@ and the offset of @src@ in @m@. 339 If @src@ is not a subtype of @m@, the loop continues but with the offset shifted by the size of @m@. 340 If the loop ends, than @src@ is not convertible to @dst@, and false is returned. 341 338 342 339 343 \section{Control Structures} -
doc/theses/jiada_liang_MMath/background.tex
rf6bbc92 r3b10778 486 486 For example, the conversion cost from @int@ to a @struct S@ is @infinite@. 487 487 488 In \CFA, the meaning of a C style cast is determined by its @Cast Cost@. For most cast expression resolution, a cast cost is equal to a conversion cost. 489 Cast cost exists as an independent matrix for conversion that cannot happen implicitly, while being possible with an explicit cast. These conversions are often defined to have 490 infinite conversion cost and non-infinite cast cost. 488 In \CFA, the meaning of a C-style cast is determined by its @Cast Cost@. 489 For most cast-expression resolution, a cast cost is equal to a conversion cost. 490 Cast cost exists as an independent matrix for conversion that cannot happen implicitly, while being possible with an explicit cast. 491 These conversions are often defined to have infinite conversion cost and non-infinite cast cost. -
doc/theses/jiada_liang_MMath/intro.tex
rf6bbc92 r3b10778 307 307 generalization: Support all language types for enumerators with associated values providing enumeration constants for any type. 308 308 \item 309 inheritance: Implement subtypingand containment inheritance for enumerations.309 reuse: Implement subset and containment inheritance for enumerations. 310 310 \item 311 311 control flow: Extend control-flow structures making it safer and easier to enumerate over an enumeration.
Note: See TracChangeset
for help on using the changeset viewer.