- Timestamp:
- Aug 25, 2022, 11:54:42 AM (2 years ago)
- Branches:
- ADT, ast-experimental, master, pthread-emulation
- Children:
- f6a4917
- Parents:
- fc6c410 (diff), 4d3666d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- doc
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/iterators.md
rfc6c410 rd2f09e4 2 2 ========= 3 3 This is the proposal for adding iterators to Cforall and the standard 4 lib ary. Iterators provide a common interface for sequences of values in4 library. Iterators provide a common interface for sequences of values in 5 5 the language. Many inputs and outputs can be described in terms of sequences, 6 6 creating a common interface that can be used in many places. … … 15 15 The operation is "get the next value in the sequence", but this actually has 16 16 several parts, in that it has to check if there are move values, return the 17 next one if there is , and update any internal information in the iterator.17 next one if there is one, and update any internal information in the iterator. 18 18 For example: `Maybe(Item) next(Iter &);`. 19 19 20 20 Now, iterators can have other operations. Notably, they are often also 21 iterables that return themselves. They can also have a veri aty of iterator21 iterables that return themselves. They can also have a verity of iterator 22 22 transformers built in. 23 23 … … 25 25 26 26 Anything that you can get an iterator from is called an iterable. There 27 is an operation to get an iterator from an itera tor.27 is an operation to get an iterator from an iterable. 28 28 29 29 Range For Loop … … 31 31 One part of the language that could be reworked to make good use of this is 32 32 for loops. In short, remove most of the special rules that can be done inside 33 the identif er and make it a generic range for loop:33 the identifier and make it a generic range for loop: 34 34 35 35 ``` … … 39 39 The common way to implement this is that expression produces an iterable. 40 40 The for loop gets an iterator from the iterable (which is why iterators are 41 often iterables, so they can be passed in with the same i terface) and stores41 often iterables, so they can be passed in with the same interface) and stores 42 42 it. Then, for each value in the iterator, the loop binds the value to the 43 43 identifier and then executes the statement. The loop exits after every value 44 has been used and the iterator is ex austed.44 has been used and the iterator is exhausted. 45 45 46 46 For the chained for loop (`for (i; _: j; _)`) can still have its existing 47 47 behaviour, advancing through each range in parallel and stopping as soon 48 as the first one is ex austed.48 as the first one is exhausted. 49 49 50 50 Ranges … … 54 54 logically instead of by copy. 55 55 56 The purpose of this container is to bridge the new iterator i terfaces with56 The purpose of this container is to bridge the new iterator interfaces with 57 57 the existing range syntax. The range syntax would become an operator that 58 58 returns a range object, which can be used as any other type. … … 91 91 pointers. 92 92 93 Rust also has a imp arative implementation of a functional style of iterators,93 Rust also has a imperative implementation of a functional style of iterators, 94 94 including a great number of standard transformers. Otherwise, it is very 95 95 similar to Python. -
doc/user/user.tex
rfc6c410 rd2f09e4 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Feb 14 17:20:39202214 %% Update Count : 5 38213 %% Last Modified On : Mon Aug 22 23:43:30 2022 14 %% Update Count : 5503 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 964 964 965 965 \begin{figure} 966 \begin{tabular}{@{}l@{\hspace{ 25pt}}|l@{}}967 \multicolumn{1}{@{}c@{\hspace{ 25pt}}|}{loop control} & \multicolumn{1}{c@{}}{output} \\966 \begin{tabular}{@{}l@{\hspace{40pt}}|l@{}} 967 \multicolumn{1}{@{}c@{\hspace{40pt}}|}{loop control} & \multicolumn{1}{c@{}}{output} \\ 968 968 \hline 969 969 \begin{cfa} 970 while ®($\,$)® { sout | "empty"; break; } 971 do { sout | "empty"; break; } while ®($\,$)®; 972 for ®($\,$)® { sout | "empty"; break; } 973 for ( ®0® ) { sout | "A"; } sout | "zero"; 974 for ( ®1® ) { sout | "A"; } 975 for ( ®10® ) { sout | "A"; } 976 for ( ®= 10® ) { sout | "A"; } 977 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; } 978 for ( ®10 -~= 1 ~ 2® ) { sout | "C"; } 979 for ( ®0.5 ~ 5.5® ) { sout | "D"; } 980 for ( ®5.5 -~ 0.5® ) { sout | "E"; } 981 for ( ®i; 10® ) { sout | i; } 982 for ( ®i; = 10® ) { sout | i; } 983 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; } 984 for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; } 985 for ( ®i; 0.5 ~ 5.5® ) { sout | i; } 986 for ( ®i; 5.5 -~ 0.5® ) { sout | i; } 987 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; } 988 for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; } 970 while () { sout | "empty"; break; } 971 do { sout | "empty"; break; } while (); 972 for () { sout | "empty"; break; } $\C[3in]{sout | nl | nlOff;}$ 973 974 for ( 0 ) { sout | "A"; } sout | "zero"; $\C{sout | nl;}$ 975 for ( 1 ) { sout | "A"; } $\C{sout | nl;}$ 976 for ( 10 ) { sout | "A"; } $\C{sout | nl;}$ 977 for ( ~= 10 ) { sout | "A"; } $\C{sout | nl;}$ 978 for ( 1 ~= 10 ~ 2 ) { sout | "B"; } $\C{sout | nl;}$ 979 for ( 1 -~= 10 ~ 2 ) { sout | "C"; } $\C{sout | nl;}$ 980 for ( 0.5 ~ 5.5 ) { sout | "D"; } $\C{sout | nl;}$ 981 for ( 0.5 -~ 5.5 ) { sout | "E"; } $\C{sout | nl;}$ 982 for ( i; 10 ) { sout | i; } $\C{sout | nl;}$ 983 for ( i; ~= 10 ) { sout | i; } $\C{sout | nl;}$ 984 for ( i; 1 ~= 10 ~ 2 ) { sout | i; } $\C{sout | nl;}$ 985 for ( i; 1 -~= 10 ~ 2 ) { sout | i; } $\C{sout | nl;}$ 986 for ( i; 0.5 ~ 5.5 ) { sout | i; } $\C{sout | nl;}$ 987 for ( i; 0.5 -~ 5.5 ) { sout | i; } $\C{sout | nl;}$ 988 for ( ui; 2u ~= 10u ~ 2u ) { sout | ui; } $\C{sout | nl;}$ 989 for ( ui; 2u -~= 10u ~ 2u ) { sout | ui; } $\C{sout | nl | nl | nl;}$ 990 989 991 enum { N = 10 }; 990 for ( ®N® ) { sout | "N"; } 991 for ( ®i; N® ) { sout | i; } 992 for ( ®i; N -~ 0® ) { sout | i; } 993 const int start = 3, comp = 10, inc = 2; 994 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; } 995 for ( i; 1 ~ ®@® ) { if ( i > 10 ) break; sout | i; } 996 for ( i; 10 -~ ®@® ) { if ( i < 0 ) break; sout | i; } 997 for ( i; 2 ~ ®@® ~ 2 ) { if ( i > 10 ) break; sout | i; } 998 for ( i; 2.1 ~ ®@® ~ ®@® ) { if ( i > 10.5 ) break; sout | i; i += 1.7; } 999 for ( i; 10 -~ ®@® ~ 2 ) { if ( i < 0 ) break; sout | i; } 1000 for ( i; 12.1 ~ ®@® ~ ®@® ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; } 1001 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; } 1002 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; } 1003 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; } 1004 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; } 1005 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; } 1006 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; } 1007 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; } 1008 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; } 1009 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; } 1010 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; } 1011 for ( i; 5 ®:® k; 1.5 ~ @ ®:® j; -5 -~ @ ~ 2 ) { sout | i | j | k; } 992 for ( N ) { sout | "N"; } $\C{sout | nl;}$ 993 for ( i; N ) { sout | i; } $\C{sout | nl;}$ 994 for ( i; -~ N ) { sout | i; } $\C{sout | nl | nl | nl;}$ 995 996 const int low = 3, high = 10, inc = 2; 997 for ( i; low ~ high ~ inc + 1 ) { sout | i; } $\C{sout | nl;}$ 998 for ( i; 1 ~ @ ) { if ( i > 10 ) break; sout | i; } $\C{sout | nl;}$ 999 for ( i; @ -~ 10 ) { if ( i < 0 ) break; sout | i; } $\C{sout | nl;}$ 1000 for ( i; 2 ~ @ ~ 2 ) { if ( i > 10 ) break; sout | i; } $\C{sout | nl;}$ 1001 for ( i; 2.1 ~ @ ~ @ ) { if ( i > 10.5 ) break; sout | i; i += 1.7; } $\C{sout | nl;}$ 1002 for ( i; @ -~ 10 ~ 2 ) { if ( i < 0 ) break; sout | i; } $\C{sout | nl;}$ 1003 for ( i; 12.1 ~ @ ~ @ ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; } $\C{sout | nl;}$ 1004 for ( i; 5 : j; -5 ~ @ ) { sout | i | j; } $\C{sout | nl;}$ 1005 for ( i; 5 : j; @ -~ -5 ) { sout | i | j; } $\C{sout | nl;}$ 1006 for ( i; 5 : j; -5 ~ @ ~ 2 ) { sout | i | j; } $\C{sout | nl;}$ 1007 for ( i; 5 : j; @ -~ -5 ~ 2 ) { sout | i | j; } $\C{sout | nl;}$ 1008 for ( i; 5 : j; -5 ~ @ ) { sout | i | j; } $\C{sout | nl;}$ 1009 for ( i; 5 : j; @ -~ -5 ) { sout | i | j; } $\C{sout | nl;}$ 1010 for ( i; 5 : j; -5 ~ @ ~ 2 ) { sout | i | j; } $\C{sout | nl;}$ 1011 for ( i; 5 : j; @ -~ -5 ~ 2 ) { sout | i | j; } $\C{sout | nl;}$ 1012 for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } $\C{sout | nl;}$ 1013 for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } $\C{sout | nl;}$ 1014 for ( i; 5 : k; 1.5 ~ @ : j; @ -~ -5 ~ 2 ) { sout | i | j | k; } $\C{sout | nl;}\CRT$ 1012 1015 \end{cfa} 1013 1016 & … … 1016 1019 empty 1017 1020 empty 1021 1018 1022 zero 1019 1023 A … … 1033 1037 10 8 6 4 2 1034 1038 1039 1035 1040 N N N N N N N N N N 1036 1041 0 1 2 3 4 5 6 7 8 9 1037 1042 10 9 8 7 6 5 4 3 2 1 1043 1038 1044 1039 1045 3 6 9 … … 1073 1079 1074 1080 Looping a fixed number of times, possibly with a loop index, occurs frequently. 1075 \CFA condenses simply looping to facilitate coding speed and safety. 1076 The \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control is augmented as follows \see{examples in \VRef[Figure]{f:LoopControlExamples}}: 1077 \begin{itemize}[itemsep=0pt] 1078 \item 1079 ©0© is the implicit start value; 1080 \item 1081 ©1© is the implicit increment value. 1082 \item 1083 The up-to range uses operator ©+=© for increment; 1084 \item 1085 The down-to range uses operator ©-=© for decrement. 1086 \item 1087 The loop index is polymorphic in the type of the comparison value N (when the start value is implicit) or the start value M. 1088 \begin{cfa} 1089 for ( i; ®5® ) $\C[2.5in]{// typeof(5) i; 5 is comparison value}$ 1090 for ( i; ®1.5®~5.5~0.5 ) $\C{// typeof(1.5) i; 1.5 is start value}$ 1091 \end{cfa} 1092 \item 1093 An empty conditional implies comparison value of ©1© (true). 1081 \CFA condenses simply looping to facilitate coding speed and safety~\see{examples in \VRef[Figure]{f:LoopControlExamples}}. 1082 1083 The \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control allow an empty conditional, which implies a comparison value of ©1© (true). 1094 1084 \begin{cfa} 1095 1085 while ( ®/*empty*/® ) $\C{// while ( true )}$ … … 1097 1087 do ... while ( ®/*empty*/® ) $\C{// do ... while ( true )}$ 1098 1088 \end{cfa} 1099 \item 1100 A comparison N is implicit up-to exclusive range [0,N\R{)}. 1089 1090 The ©for© control has 4 new loop-control operators that are not overloadable: 1091 \begin{description}[itemsep=0pt,parsep=0pt] 1092 \item 1093 \Indexc{\~} (tilde) \Index{up-to exclusive range}, 1094 \item 1095 \Indexc{\~=} (tilde equal) \Index{up-to inclusive range}, 1096 \item 1097 \Indexc{-\~} (minus tilde) \Index{down-to exclusive range}, 1098 \item 1099 \Indexc{-\~=} (minus tilde equal) \Index{down-to inclusive range}. 1100 \end{description} 1101 1102 The ©for© index components, low (L), high (H), and increment (I), are optional. 1103 If missing: 1104 \begin{itemize}[itemsep=0pt,parsep=0pt] 1105 \item 1106 \Indexc{0} is the implicit low value. 1107 \item 1108 \Indexc{1} is the implicit increment value. 1109 \item 1110 The up-to range uses operator \Indexc{+=} for increment. 1111 \item 1112 The down-to range uses operator \Indexc{-=} for decrement. 1113 \end{itemize} 1114 1115 If no type is specified for the loop index, it is the type of the high value H (when the low value is implicit) or the low value L. 1116 \begin{cfa} 1117 for ( ®5® ) $\C{// typeof(5) anonymous-index; 5 is high value}$ 1118 for ( i; ®1.5® ~ 5.5 ) $\C{// typeof(1.5) i; 1.5 is low value}$ 1119 for ( ®int i®; 0 ~ 10 ~ 2 ) $\C{// int i; type is explicit}$ 1120 \end{cfa} 1121 1122 The following are examples for constructing different for-control: 1123 \begin{itemize}[itemsep=0pt] 1124 \item 1125 H is implicit up-to exclusive range [0,H\R{)}. 1101 1126 \begin{cfa} 1102 1127 for ( ®5® ) $\C{// for ( typeof(5) i; i < 5; i += 1 )}$ 1103 1128 \end{cfa} 1104 1129 \item 1105 A comparison ©=© N is implicit up-to inclusive range [0,N\R{]}.1106 \begin{cfa} 1107 for ( ® =®5 ) $\C{// for ( typeof(5) i; i <= 5; i += 1 )}$1108 \end{cfa} 1109 \item 1110 The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N\R{)}.1111 \begin{cfa} 1112 for ( 1 ®~®5 ) $\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}$1113 \end{cfa} 1114 \item 1115 The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N\R{]}.1116 \begin{cfa} 1117 for ( 1 ®~=®5 ) $\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}$1118 \end{cfa} 1119 \item 1120 The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M\R{)}.1121 \begin{cfa} 1122 for ( 1 ®-~®5 ) $\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}$1123 \end{cfa} 1124 \item 1125 The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M\R{]}.1126 \begin{cfa} 1127 for ( 1 ®-~=®5 ) $\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}$1130 ©~=© H is implicit up-to inclusive range [0,H\R{]}. 1131 \begin{cfa} 1132 for ( ®~=® 5 ) $\C{// for ( typeof(5) i; i <= 5; i += 1 )}$ 1133 \end{cfa} 1134 \item 1135 L ©~©\index{~@©~©} H is explicit up-to exclusive range [L,H\R{)}. 1136 \begin{cfa} 1137 for ( 1 ®~® 5 ) $\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}$ 1138 \end{cfa} 1139 \item 1140 L ©~=©\index{~=@©~=©} H is explicit up-to inclusive range [L,H\R{]}. 1141 \begin{cfa} 1142 for ( 1 ®~=® 5 ) $\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}$ 1143 \end{cfa} 1144 \item 1145 L ©-~©\index{-~@©-~©} H is explicit down-to exclusive range [H,L\R{)}, where L and H are implicitly interchanged to make the range down-to. 1146 \begin{cfa} 1147 for ( 1 ®-~® 5 ) $\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}$ 1148 \end{cfa} 1149 \item 1150 L ©-~=©\index{-~=@©-~=©} H is explicit down-to inclusive range [H,L\R{]}, where L and H are implicitly interchanged to make the range down-to. 1151 \begin{cfa} 1152 for ( 1 ®-~=® 5 ) $\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}$ 1128 1153 \end{cfa} 1129 1154 \item 1130 1155 ©@© means put nothing in this field. 1131 1156 \begin{cfa} 1132 for ( 1~®@®~2 ) $\C{// for ( typeof(1) i = 1; /*empty*/; i += 2 )}$ 1133 \end{cfa} 1134 \item 1135 ©:© means start another index. 1136 \begin{cfa} 1137 for ( i; 5 ®:® j; 2~12~3 ) $\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}\CRT$ 1157 for ( i; 1 ~ ®@® ~ 2 ) $\C{// for ( typeof(1) i = 1; \R{/* empty */}; i += 2 )}$ 1158 for ( i; 1 ~ 10 ~ ®@® ) $\C{// for ( typeof(1) i = 1; i < 10; \R{/* empty */} )}$ 1159 for ( i; 1 ~ ®@® ~ ®@® ) $\C{// for ( typeof(1) i = 1; /*empty*/; \R{/* empty */} )}$ 1160 \end{cfa} 1161 L cannot be elided for the up-to range, \lstinline{@ ~ 5}, and H for the down-to range, \lstinline{1 -~ @}, because then the loop index is uninitialized. 1162 Similarly, the high value cannot be elided is an anonymous loop index (©1 ~ @©), as there is no index to stop the loop. 1163 \item 1164 ©:© means low another index. 1165 \begin{cfa} 1166 for ( i; 5 ®:® j; 2 ~ 12 ~ 3 ) $\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}$ 1138 1167 \end{cfa} 1139 1168 \end{itemize} 1169 \R{Warning}: specifying the down-to range maybe unexcepted because the loop control \emph{implicitly} switches the L and H values (and toggles the increment/decrement for I): 1170 \begin{cfa} 1171 for ( i; 1 ~ 10 ) ${\C[1.5in]{// up range}$ 1172 for ( i; 1 -~ 10 ) ${\C{// down range}$ 1173 for ( i; ®10 -~ 1® ) ${\C{// \R{WRONG down range!}}\CRT}$ 1174 \end{cfa} 1175 The reason for this sematics is that the range direction can be toggled by adding/removing the minus, ©'-'©, versus interchanging the L and H expressions, which has a greater chance of introducing errors. 1140 1176 1141 1177 … … 4794 4830 int id; 4795 4831 float size; 4796 Parts * optionalParts;4832 int * optionalint; 4797 4833 }; 4798 4834 … … 4801 4837 // Subsequent arguments can be specified for initialization 4802 4838 4803 void ?{}( Widget &w) { // default constructor4839 void ?{}( Widget & w ) { $\C{// default constructor}$ 4804 4840 w.id = -1; 4805 4841 w.size = 0.0; 4806 w.optional Parts = 0;4842 w.optionalint = 0p; 4807 4843 } 4808 4844 4809 4845 // constructor with values (does not need to include all fields) 4810 void ?{}( Widget &w, int id, float size) {4846 void ?{}( Widget & w, int id, float size ) { 4811 4847 w.id = id; 4812 4848 w.size = size; 4813 w.optional Parts = 0;4814 } 4815 4816 // ^? is the destructor operator identifier4817 void ^? (Widget &w) { // destructor4849 w.optionalint = 0p; 4850 } 4851 4852 // ^?{} is the destructor operator identifier 4853 void ^?{}( Widget & w ) { $\C{// destructor}$ 4818 4854 w.id = 0; 4819 4855 w.size = 0.0; 4820 if (w.optionalParts != 0) { 4821 // This is the only pointer to optionalParts, free it 4822 free(w.optionalParts); 4823 w.optionalParts = 0; 4856 if ( w.optionalint != 0p ) { 4857 free( w.optionalint ); 4858 w.optionalint = 0p; 4824 4859 } 4825 4860 } 4826 4861 4827 Widget baz; // reserve space only 4828 Widget foo{}; // calls default constructor 4829 Widget bar{23, 2.45}; // calls constructor with values 4830 baz{24, 0.91}; // calls constructor with values 4831 ?{}(baz, 24, 0.91}; // explicit call to constructor 4832 ^bar; // explicit call to destructor 4833 ^?(bar); // explicit call to destructor 4862 Widget baz; $\C{// reserve space only}$ 4863 Widget foo{}; $\C{// calls default constructor}$ 4864 Widget bar{ 23, 2.45 }; $\C{// calls constructor with values}$ 4865 baz{ 24, 0.91 }; $\C{// calls constructor with values}$ 4866 ?{}( baz, 24, 0.91 ); $\C{// explicit call to constructor}$ 4867 ^?{} (bar ); $\C{// explicit call to destructor}$ 4834 4868 \end{cfa} 4835 4869 \caption{Constructors and Destructors}
Note: See TracChangeset
for help on using the changeset viewer.