Changeset b0fcd0e for doc/proposals
- Timestamp:
- Oct 2, 2024, 1:16:29 PM (12 months ago)
- Branches:
- master
- Children:
- 1b770e40
- Parents:
- 7968301
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/tuples.md
r7968301 rb0fcd0e 3 3 4 4 This proposal is to update tuples, as they were created by Rob in his thesis, 5 to address problems have appeared in their use since then. This proposal is6 an attempt to address some of those problems. Adding new functionality,5 to address problems that have appeared in their use since then. This proposal 6 is an attempt to address some of those problems. Adding new functionality, 7 7 updating existing features and removing some problematic features. 8 8 … … 56 56 57 57 Multi-Assignment assigns every element to the matching element of a source 58 tuple (both tuples must be the same si de).58 tuple (both tuples must be the same size). 59 59 60 60 [int, long, float] dst; … … 88 88 } 89 89 90 This is the unique feature of Cforall tuples. There are a few languages have90 This is the unique feature of Cforall tuples. There are a few languages with 91 91 multiple return values, but they are usually are a stand alone feature. 92 92 93 93 ### Tuple Casts 94 94 95 C-style casts can be used on tuples. These usually conversion casts (casts95 C-style casts can be used on tuples. These are usually conversion casts (casts 96 96 that preform operations on the cast type, as opposed to reinterpreting the 97 97 existing value). … … 104 104 105 105 This casts the first element type from a char to an int and drops the last 106 element. The second line can be replaced the following code, thatcreates107 a new tuple ofby casting the kept elements of the old tuple:106 element. The second line can be replaced with the following code, which creates 107 a new tuple by casting the kept elements of the old tuple: 108 108 109 109 [(int)x.0, (char)x.1]; … … 129 129 tuple is flattened, the second assertion "T max(Ts);" matches types with 130 130 multiple parameters, although it is used as a tuple function inside the 131 function body. For example, in th reeargument case (all three of the131 function body. For example, in the three-argument case (all three of the 132 132 same type), both assertions can match the same function. Then "Ts args" 133 introduces args as a tuple, where it is pas tto max.133 introduces args as a tuple, where it is passed to max. 134 134 135 135 Issues with the Current State 136 136 ----------------------------- 137 There are a v eriaty of problems with the current implementation which we137 There are a variety of problems with the current implementation which we 138 138 would like to fix. 139 139 … … 141 141 142 142 Spoilers, this proposal actually takes them further away from being objects. 143 But this ill istrates why the current version is not as useful is it could be.143 But this illustrates why the current version is not as useful is it could be. 144 144 145 145 Tuples do not have the lifetime operators (copy construction, copy assignment … … 150 150 This prevents tuples from being interwoven with regular polymorphic code. 151 151 152 ### Providing TType Arguments is Incon istent152 ### Providing TType Arguments is Inconsistent 153 153 154 154 The syntax for ttype arguments is slightly inconsistent. It hasn't come up 155 155 much yet because you do not directly provide ttype polymorphic arguments to 156 functions and the are very few existing use-cases for ttype structures.157 158 Passing arguments to a function inlines the arguments into inlines the159 argumentswhile passing them to a polymorphic type requires them to be156 functions and there are very few existing use-cases for ttype structures. 157 158 Passing arguments to a function inlines the arguments, 159 while passing them to a polymorphic type requires them to be 160 160 enclosed in a tuple. Compare `function(x, y, z)` with `Type(A, [B, C])`. 161 161 … … 167 167 168 168 The tuple syntax conflicts with designators and the new attribute syntax. 169 These conflicts break C compat ability goals of Cforall. Designators have had169 These conflicts break C compatibility goals of Cforall. Designators have had 170 170 to their syntax change and Cforall cannot parse the new attributes. 171 171 172 172 Although most of this redesign is about the semantics of tuples, but an 173 173 update to tuple syntax that removes these conflicts that would improve the 174 compat ability of Cforall going forward (and also open up the new attribute174 compatibility of Cforall going forward (and also open up the new attribute 175 175 syntax for cforall features). 176 176 … … 183 183 The existing tuples will be even more "unstructured" than they are now. 184 184 Tuples are considered a sequence of types or typed entities. These packs are 185 then unpacked into the sur ounding context. Put differently, tuples are now185 then unpacked into the surrounding context. Put differently, tuples are now 186 186 flattened as much as possible, with some places (like parameter lists) being 187 187 treated as an implicit tuple and the tuple being flattened into that. 188 188 189 Struct red tuples are now a separate feature, a structure called "tuple".190 These are polymorphic structures , an instance should act as a structure191 except that useindices instead of field names. These structures shouldn't189 Structured tuples are now a separate feature, a structure called "tuple". 190 These are polymorphic structures; an instance should act as a structure, 191 except that it uses indices instead of field names. These structures shouldn't 192 192 have to be used often, but fill in the use cases that unstructured tuples 193 193 no longer support. … … 197 197 Changed Features 198 198 ---------------- 199 Some of the concrete changes to the features of the language s.199 Some of the concrete changes to the features of the language. 200 200 201 201 ### Structured Tuple Type … … 261 261 262 262 These may not work exactly as given (for one, the copy assignment assertion 263 in the first function would likely be redund ent/conflicting with the implicit263 in the first function would likely be redundant/conflicting with the implicit 264 264 assertions on the parameters), but they show the pattern. Multi-assignment 265 isalso would be very hard to write with simple tuple types, because the265 also would be very hard to write with simple tuple types, because the 266 266 relationship between the two halves of the parameter list does have to line 267 267 up, that cannot be enforced with two different tuples. … … 272 272 polymorphic parameters as polymorphic type packs. The `Vars...` syntax 273 273 introduces a pack of types into scope. It can be used in many of the same 274 ways as a tuple tuple, but in some new ways to.275 276 The primary existing use remains ,you can use a polymorphic pack in a274 ways as a tuple, but in some new ways to. 275 276 The primary existing use remains; you can use a polymorphic pack in a 277 277 parameter list, both as part of an assertion and in the signature of the 278 main function. The difference ,is that this is not an enclosed tuple, but278 main function. The difference is that this is not an enclosed tuple, but 279 279 a series of types. The only effective difference this makes is it doesn't 280 280 prefer to match another tuple/pack. 281 281 282 282 This pattern continues to a parameter defined with a pack of types, which 283 is considered a pack of parameters, and the name ofit introduces is a pack283 is considered a pack of parameters, and the name it introduces is a pack 284 284 of variables, or a flattened tuple. 285 285 … … 287 287 void function(Params values); 288 288 289 New use scases include declarations of members and variables. For example,289 New use cases include declarations of members and variables. For example, 290 290 the creation the structured tuple structure: 291 291 … … 296 296 297 297 This is again, treated as a pack of members. They have the same layout as 298 if they were written out by hand. Now, the name get is still accessed is if298 if they were written out by hand. Now, the name get is still accessed as if 299 299 it was a regular, singular, member. The result of that expression is a 300 300 pack expression, a tuple of all the field accesses, which can be used with a … … 344 344 The unstructured tuple cannot represent all the types that the previous 345 345 semi-structured tuple could. These cases still exist in various ways, 346 special in the internals of a polymorphic type, but general should be346 special in the internals of a polymorphic type, but in general should be 347 347 considered in their reduced form. 348 348 … … 364 364 is to say a single type in an unstructured tuple is a no-op. 365 365 366 Lastly, nested tuples are always flattened into to form a one 366 Lastly, nested tuples are always flattened into to form a one-deep tuple. 367 367 This means that `[bool, [char, int], float]` is resolved as 368 368 `[bool, char, int, float]`, with the internal structure of the tuple ignored. … … 396 396 each element in the result tuple decided by the matching element of the 397 397 range, which gives the index of the original tuple to place there. 398 The type of the indices may be and inte rgal type, but all indices must be in398 The type of the indices may be and integral type, but all indices must be in 399 399 range, otherwise it is a compile time error. 400 400 … … 438 438 This would act much like a `FunctionDecl` except for tuples, narrowing the 439 439 possible types, to `TupleType` instances instead of `FunctionType` instances, 440 and storing some addit onal information, in this case the names of the440 and storing some additional information, in this case the names of the 441 441 elements of the tuples. 442 442 … … 447 447 ### Field Packs 448 448 Field packs in structures will probably have to be written out in full by 449 the specialization pass. If they are not it could have some negative effects449 the specialization pass. If they are not, it could have some negative effects 450 450 on layout, causing a structure to take up extra space. It may be able to 451 451 reuse some of the specialization code for the existing tuples. … … 469 469 tuple is a different polymorphic type. 470 470 471 Tuple types and expressions are written with a parenthesized, comma sep erated471 Tuple types and expressions are written with a parenthesized, comma separated 472 472 list of types or expressions. To avoid confusion with the grouping "(...)", 473 one 473 one-element tuples must have a trailing comma (and larger tuples may have a 474 474 trailing comma). 475 475 … … 479 479 480 480 Element access uses similar syntax to field access (that is "."), but uses an 481 inte rger literal instead of a field name (for example: "pair.1"). Tuples481 integer literal instead of a field name (for example: "pair.1"). Tuples 482 482 support pattern matching with similar syntax to their expression form. 483 483 … … 495 495 meta-programming. 496 496 497 C++ is also one of the few languages with support for var adic polymorphic497 C++ is also one of the few languages with support for variadic polymorphic 498 498 types, so there is one template that defines all the types. It is written 499 499 as `std::tuple<TYPE...>`, where "TYPE..." is any number of comma separated … … 501 501 502 502 There is no special syntax for member access of a tuple. You have to use a 503 template function for example `std::get<0>( tuple )`.503 template function, for example `std::get<0>( tuple )`. 504 504 505 505 C++ also has structured binding, a kind of limited pattern matching. In a … … 517 517 has functions for tuples of up to 7 elements. 518 518 519 The syntax for tuples is a comma sep erated list of elements. Either element519 The syntax for tuples is a comma separated list of elements. Either element 520 520 types to create a tuple type, or element values to create a tuple value. The 521 521 context decides which one we are looking for. Such as `(6, "six")` or … … 543 543 544 544 OCaml does not support 0 or 1 element tuples. It does however have the `unit` 545 type which has one value written `()`.545 type which has one value, written `()`. 546 546 547 547 - https://ocaml.org/docs/basic-data-types#tuples 548 548 549 549 #### Swift 550 Swift has tuple types that use the basic parenthesized list ofcomma550 Swift has tuple types that use the basic parenthesized, comma 551 551 separated list of types or values. It only supports 0 and 2 or more element 552 552 tuples (the `Void` type is an alias for the empty tuple type). … … 580 580 ### Packs 581 581 582 Packs (or unstructure stuples) are a much less common feature. In fact, there582 Packs (or unstructured tuples) are a much less common feature. In fact, there 583 583 might just be one language, C++, that supports packs. The most common use 584 584 case for unstructured tuples is returning multiple values, so there is a … … 604 604 605 605 #### Lua 606 Lua is a scripting languag ue, is dynamically typed and stack based. Although606 Lua is a scripting language, is dynamically typed and stack based. Although 607 607 the stack usually only directly visible in the C-API, it does allow any 608 608 function to return any number of values. Even in a single return, if the … … 635 635 public: 636 636 template<typename... Args> 637 Outer(Args&&... args) : inner(std::fo ward<Args>(args)...) {}637 Outer(Args&&... args) : inner(std::forward<Args>(args)...) {} 638 638 }; 639 639 ``` … … 641 641 In the first application, `Args&&... args` both uses a pack and introduces 642 642 another one. `Arg0&& arg0, Arg1&& arg1, Arg2&& arg2, ...` is the pattern 643 it exp lands too. The `&&` is used to copy the argument type's reference643 it expands too. The `&&` is used to copy the argument type's reference 644 644 qualifier (the default can strip references away). 645 645 646 646 The second application, `std::forward<Args>(args)...` uses two packs. These 647 are expanded in par ellel, and must be the same length (in this case they647 are expanded in parallel, and must be the same length (in this case they 648 648 always will be). It also helps show that the `...` is actually the bit doing 649 649 the expansion, it is a suffix "operator" to the expansion pattern. … … 653 653 element of the `args` pack together. 654 654 655 C++ about the best you could ask for in this area, but it does a lot of work655 C++ is about the best you could ask for in this area, but it does a lot of work 656 656 at compile time to make this happen. 657 657
Note:
See TracChangeset
for help on using the changeset viewer.