Changeset f92aa32 for doc/rob_thesis/tuples.tex
- Timestamp:
- Apr 7, 2017, 6:25:23 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 2ccb93c
- Parents:
- c51b5a3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/rob_thesis/tuples.tex
rc51b5a3 rf92aa32 2 2 \chapter{Tuples} 3 3 %====================================================================== 4 5 \section{Introduction}6 % TODO: no passing input parameters by assignment, instead will have reference types => this is not a very C-like model and greatly complicates syntax for likely little gain (and would cause confusion with already supported return-by-reference)7 % TODO: benefits (conclusion) by Till: reduced number of variables and statements; no specified order of execution for multiple assignment (more optimzation freedom); can store parameter lists in variable; MRV routines (natural code); more convenient assignment statements; simple and efficient access of record fields; named return values more legible and efficient in use of storage8 4 9 5 \section{Multiple-Return-Value Functions} … … 12 8 This restriction results in code which emulates functions with multiple return values by \emph{aggregation} or by \emph{aliasing}. 13 9 In the former situation, the function designer creates a record type that combines all of the return values into a single type. 14 For example, consider a function returning the most frequently occuring letter in a string, and its frequency. 15 % TODO: consider simplifying the example! 16 % Two things I like about this example: 17 % * it uses different types to illustrate why an array is insufficient (this is not necessary, but is nice) 18 % * it's complicated enough to show the uninitialized pitfall that exists in the aliasing example. 19 % Still, it may be a touch too complicated. Is there a simpler example with these two properties? 10 For example, consider a function returning the most frequently occurring letter in a string, and its frequency. 11 This example is complex enough to illustrate that an array is insufficient, since arrays are homogeneous, and demonstrates a potential pitfall that exists with aliasing. 20 12 \begin{cfacode} 21 13 struct mf_ret { … … 87 79 The expression resolution phase of the \CFA translator ensures that the correct form is used depending on the values being returned and the return type of the current function. 88 80 A multiple-returning function with return type @T@ can return any expression that is implicitly convertible to @T@. 89 Using the running example, the @most_frequent@ function can be written inusing multiple return values as such,81 Using the running example, the @most_frequent@ function can be written using multiple return values as such, 90 82 \begin{cfacode} 91 83 [int, char] most_frequent(const char * str) { … … 282 274 These semantics allow cascading tuple assignment to work out naturally in any context where a tuple is permitted. 283 275 These semantics are a change from the original tuple design in \KWC \cite{Till89}, wherein tuple assignment was a statement that allows cascading assignments as a special case. 284 The \KWC semantics fix what was seen as a problem with assignment, wherein it can be used in many different locations, such as in function-call argument position. % TODO: remove?? 276 Restricting tuple assignment to statements was an attempt to to fix what was seen as a problem with assignment, wherein it can be used in many different locations, such as in function-call argument position. 285 277 While permitting assignment as an expression does introduce the potential for subtle complexities, it is impossible to remove assignment expressions from \CFA without affecting backwards compatibility. 286 278 Furthermore, there are situations where permitting assignment as an expression improves readability by keeping code succinct and reducing repetition, and complicating the definition of tuple assignment puts a greater cognitive burden on the user. … … 313 305 [S, S] z = x.0; // uses (4), (4), copy constructor 314 306 \end{cfacode} 315 In this example, @x@ is initialized by the multiple constructor calls @?{}(&x.0, 3)@ and @?{}(&x.1, 6.28)@, while @y@ is initi laized by two default constructor calls @?{}(&y.0)@ and @?{}(&y.1)@.307 In this example, @x@ is initialized by the multiple constructor calls @?{}(&x.0, 3)@ and @?{}(&x.1, 6.28)@, while @y@ is initialized by two default constructor calls @?{}(&y.0)@ and @?{}(&y.1)@. 316 308 @z@ is initialized by mass copy constructor calls @?{}(&z.0, x.0)@ and @?{}(&z.1, x.0)@. 317 309 Finally, @x@, @y@, and @z@ are destructed, i.e. the calls @^?{}(&x.0)@, @^?{}(&x.1)@, @^?{}(&y.0)@, @^?{}(&y.1)@, @^?{}(&z.0)@, and @^?{}(&z.1)@. … … 392 384 z.y; // ??? 393 385 \end{cfacode} 394 One possib lity is for @s.1@ to select the second member of @s@.386 One possibility is for @s.1@ to select the second member of @s@. 395 387 Under this interpretation, it becomes possible to not only access members of a struct by name, but also by position. 396 388 Likewise, it seems natural to open this mechanism to enumerations as well, wherein the left side would be a type, rather than an expression. 397 One benefit of this interpretation is familiar , since it is extremely reminiscent of tuple-index expressions.389 One benefit of this interpretation is familiarity, since it is extremely reminiscent of tuple-index expressions. 398 390 On the other hand, it could be argued that this interpretation is brittle in that changing the order of members or adding new members to a structure becomes a brittle operation. 399 391 This problem is less of a concern with tuples, since modifying a tuple affects only the code that directly uses the tuple, whereas modifying a structure has far reaching consequences for every instance of the structure. 400 392 401 As for @z.y@, aone interpretation is to extend the meaning of member tuple expressions.393 As for @z.y@, one interpretation is to extend the meaning of member tuple expressions. 402 394 That is, currently the tuple must occur as the member, i.e. to the right of the dot. 403 395 Allowing tuples to the left of the dot could distribute the member across the elements of the tuple, in much the same way that member tuple expressions distribute the aggregate across the member tuple. … … 430 422 p1.0 + p1.1 + p2.0 + p2.1; // equivalent 431 423 \end{cfacode} 432 In this simpler interpretation, a namedtuple type carries with it a list of possibly empty identifiers.424 In this simpler interpretation, a tuple type carries with it a list of possibly empty identifiers. 433 425 This approach fits naturally with the named return-value feature, and would likely go a long way towards implementing it. 434 426 435 Ultimately, the first two extensions introduce complexity into the model, with relatively little pe ceived benefit, and so were dropped from consideration.427 Ultimately, the first two extensions introduce complexity into the model, with relatively little perceived benefit, and so were dropped from consideration. 436 428 Named tuples are a potentially useful addition to the language, provided they can be parsed with a reasonable syntax. 437 429 … … 439 431 \section{Casting} 440 432 In C, the cast operator is used to explicitly convert between types. 441 In \CFA, the cast operator has a secondary use, which is type ascription .433 In \CFA, the cast operator has a secondary use, which is type ascription, since it force the expression resolution algorithm to choose the lowest cost conversion to the target type. 442 434 That is, a cast can be used to select the type of an expression when it is ambiguous, as in the call to an overloaded function. 443 435 \begin{cfacode} … … 515 507 \end{cfacode} 516 508 Note that due to the implicit tuple conversions, this function is not restricted to the addition of two triples. 509 A call to this plus operator type checks as long as a total of 6 non-tuple arguments are passed after flattening, and all of the arguments have a common type that can bind to @T@, with a pairwise @?+?@ over @T@. 517 510 For example, these expressions also succeed and produce the same value. 518 A call to this plus operator type checks as long as a total of 6 non-tuple arguments are passed after flattening, and all of the arguments have a common type that can bind to @T@, with a pairwise @?+?@ over @T@.519 511 \begin{cfacode} 520 512 ([x.0, x.1]) + ([x.2, 10, 20, 30]); // x + ([10, 20, 30]) … … 522 514 \end{cfacode} 523 515 This presents a potential problem if structure is important, as these three expressions look like they should have different meanings. 524 Furthermore, these calls can be made ambiguous by adding seemingly different functions.516 Furthermore, these calls can be made ambiguous by introducing seemingly different functions. 525 517 \begin{cfacode} 526 518 forall(otype T | { T ?+?(T, T); }) … … 630 622 g(h()); 631 623 \end{cfacode} 632 Inter ally, this is converted to psuedo-\CFA624 Internally, this is converted to pseudo-\CFA 633 625 \begin{cfacode} 634 626 void g(int, double); 635 627 [int, double] h(); 636 lazy [int, double] unq <0> = h();637 g(unq <0>.0, unq<0>.1);628 lazy [int, double] unq0 = h(); // deferred execution 629 g(unq0.0, unq0.1); // execute h() once 638 630 \end{cfacode} 639 631 That is, the function @h@ is evaluated lazily and its result is stored for subsequent accesses. … … 654 646 Every subsequent evaluation of the unique expression then results in an access to the stored result of the actual expression. 655 647 656 Currently, the \CFA translator has a very broad, imprecise definition of impurity (side-effects), where any function call is assumed to be impure.657 This notion could be made more precise for certain intrinsic, auto generated, and builtin functions, and could analyze function bodies, when they are available, to recursively detect impurity, to eliminate some unique expressions.648 Currently, the \CFA translator has a very broad, imprecise definition of impurity (side-effects), where every function call is assumed to be impure. 649 This notion could be made more precise for certain intrinsic, auto-generated, and built-in functions, and could analyze function bodies, when they are available, to recursively detect impurity, to eliminate some unique expressions. 658 650 It is possible that lazy evaluation could be exposed to the user through a lazy keyword with little additional effort. 659 651
Note: See TracChangeset
for help on using the changeset viewer.