23 \cite{isabelle-isar-ref}; indeed, replacing the keyword \keyw{datatype} by |
23 \cite{isabelle-isar-ref}; indeed, replacing the keyword \keyw{datatype} by |
24 @{command datatype_new} is usually all that is needed to port existing theories |
24 @{command datatype_new} is usually all that is needed to port existing theories |
25 to use the new package. |
25 to use the new package. |
26 |
26 |
27 Perhaps the main advantage of the new package is that it supports recursion |
27 Perhaps the main advantage of the new package is that it supports recursion |
28 through a large class of non-datatypes, comprising finite sets: |
28 through a large class of non-datatypes, such as finite sets: |
29 *} |
29 *} |
30 |
30 |
31 datatype_new 'a tree\<^sub>f\<^sub>s = Node\<^sub>f\<^sub>s 'a "'a tree\<^sub>f\<^sub>s fset" |
31 datatype_new 'a tree\<^sub>f\<^sub>s = Node\<^sub>f\<^sub>s 'a "'a tree\<^sub>f\<^sub>s fset" |
32 |
32 |
33 text {* |
33 text {* |
1097 \item |
1100 \item |
1098 Derive the desired equation on @{text un_D} from the characteristic equations |
1101 Derive the desired equation on @{text un_D} from the characteristic equations |
1099 for @{text "un_D\<^sub>0"}. |
1102 for @{text "un_D\<^sub>0"}. |
1100 \end{enumerate} |
1103 \end{enumerate} |
1101 |
1104 |
|
1105 \noindent |
1102 The following example illustrates this procedure: |
1106 The following example illustrates this procedure: |
1103 *} |
1107 *} |
1104 |
1108 |
1105 consts termi\<^sub>0 :: 'a |
1109 consts termi\<^sub>0 :: 'a |
|
1110 |
|
1111 text {* \blankline *} |
1106 |
1112 |
1107 datatype_new ('a, 'b) tlist = |
1113 datatype_new ('a, 'b) tlist = |
1108 TNil (termi: 'b) (defaults ttl: TNil) |
1114 TNil (termi: 'b) (defaults ttl: TNil) |
1109 | TCons (thd: 'a) (ttl : "('a, 'b) tlist") (defaults termi: "\<lambda>_ xs. termi\<^sub>0 xs") |
1115 | TCons (thd: 'a) (ttl : "('a, 'b) tlist") (defaults termi: "\<lambda>_ xs. termi\<^sub>0 xs") |
|
1116 |
|
1117 text {* \blankline *} |
1110 |
1118 |
1111 overloading |
1119 overloading |
1112 termi\<^sub>0 \<equiv> "termi\<^sub>0 \<Colon> ('a, 'b) tlist \<Rightarrow> 'b" |
1120 termi\<^sub>0 \<equiv> "termi\<^sub>0 \<Colon> ('a, 'b) tlist \<Rightarrow> 'b" |
1113 begin |
1121 begin |
1114 primrec_new termi\<^sub>0 :: "('a, 'b) tlist \<Rightarrow> 'b" where |
1122 primrec_new termi\<^sub>0 :: "('a, 'b) tlist \<Rightarrow> 'b" where |
1115 "termi\<^sub>0 (TNil y) = y" | |
1123 "termi\<^sub>0 (TNil y) = y" | |
1116 "termi\<^sub>0 (TCons x xs) = termi\<^sub>0 xs" |
1124 "termi\<^sub>0 (TCons x xs) = termi\<^sub>0 xs" |
1117 end |
1125 end |
1118 |
1126 |
|
1127 text {* \blankline *} |
|
1128 |
1119 lemma terminal_TCons[simp]: "termi (TCons x xs) = termi xs" |
1129 lemma terminal_TCons[simp]: "termi (TCons x xs) = termi xs" |
1120 by (cases xs) auto |
1130 by (cases xs) auto |
1121 |
1131 |
1122 |
1132 |
1123 (* |
1133 (* |
1138 |
1148 |
1139 section {* Defining Codatatypes |
1149 section {* Defining Codatatypes |
1140 \label{sec:defining-codatatypes} *} |
1150 \label{sec:defining-codatatypes} *} |
1141 |
1151 |
1142 text {* |
1152 text {* |
1143 This section describes how to specify codatatypes using the @{command codatatype} |
1153 This section describes how to specify codatatypes using the @{command |
1144 command. |
1154 codatatype} command. The command is first illustrated through concrete examples |
1145 |
1155 featuring different flavors of corecursion. More examples can be found in the |
1146 % * libraries include some useful codatatypes, notably lazy lists; |
1156 directory \verb|~~/src/HOL/BNF/Examples|. The \emph{Archive of Formal Proofs} |
1147 % see the ``Coinductive'' AFP entry \cite{xxx} for an elaborate library |
1157 also includes some useful codatatypes, notably for lazy lists |
1148 *} |
1158 \cite{lochbihler-2010}. |
1149 |
1159 *} |
1150 |
1160 |
|
1161 |
|
1162 (* |
1151 subsection {* Introductory Examples |
1163 subsection {* Introductory Examples |
1152 \label{ssec:codatatype-introductory-examples} *} |
1164 \label{ssec:codatatype-introductory-examples} *} |
1153 |
1165 |
1154 text {* |
1166 text {* |
1155 More examples in \verb|~~/src/HOL/BNF/Examples|. |
1167 More examples in \verb|~~/src/HOL/BNF/Examples|. |
1156 *} |
1168 *} |
|
1169 *) |
1157 |
1170 |
1158 |
1171 |
1159 subsection {* Command Syntax |
1172 subsection {* Command Syntax |
1160 \label{ssec:codatatype-command-syntax} *} |
1173 \label{ssec:codatatype-command-syntax} *} |
|
1174 |
|
1175 |
|
1176 subsubsection {* \keyw{codatatype} *} |
1161 |
1177 |
1162 text {* |
1178 text {* |
1163 Definitions of codatatypes have almost exactly the same syntax as for datatypes |
1179 Definitions of codatatypes have almost exactly the same syntax as for datatypes |
1164 (Section~\ref{ssec:datatype-command-syntax}), with two exceptions: The command |
1180 (Section~\ref{ssec:datatype-command-syntax}), with two exceptions: The command |
1165 is called @{command codatatype}; the \keyw{no\_discs\_sels} option is not |
1181 is called @{command codatatype}; the \keyw{no\_discs\_sels} option is not |
1166 available, because destructors are a central notion for codatatypes. |
1182 available, because destructors are a central notion for codatatypes. |
1167 *} |
1183 *} |
1168 |
1184 |
1169 |
1185 |
|
1186 (* |
1170 subsection {* Generated Constants |
1187 subsection {* Generated Constants |
1171 \label{ssec:codatatype-generated-constants} *} |
1188 \label{ssec:codatatype-generated-constants} *} |
1172 |
1189 *) |
1173 |
1190 |
|
1191 |
|
1192 (* |
1174 subsection {* Generated Theorems |
1193 subsection {* Generated Theorems |
1175 \label{ssec:codatatype-generated-theorems} *} |
1194 \label{ssec:codatatype-generated-theorems} *} |
|
1195 *) |
1176 |
1196 |
1177 |
1197 |
1178 section {* Defining Corecursive Functions |
1198 section {* Defining Corecursive Functions |
1179 \label{sec:defining-corecursive-functions} *} |
1199 \label{sec:defining-corecursive-functions} *} |
1180 |
1200 |
1185 %%% TODO: partial_function? E.g. for defining tail recursive function on lazy |
1205 %%% TODO: partial_function? E.g. for defining tail recursive function on lazy |
1186 %%% lists (cf. terminal0 in TLList.thy) |
1206 %%% lists (cf. terminal0 in TLList.thy) |
1187 *} |
1207 *} |
1188 |
1208 |
1189 |
1209 |
|
1210 (* |
1190 subsection {* Introductory Examples |
1211 subsection {* Introductory Examples |
1191 \label{ssec:primcorec-introductory-examples} *} |
1212 \label{ssec:primcorec-introductory-examples} *} |
1192 |
1213 |
1193 text {* |
1214 text {* |
1194 More examples in \verb|~~/src/HOL/BNF/Examples|. |
1215 More examples in \verb|~~/src/HOL/BNF/Examples|. |
1195 |
1216 |
1196 Also, for default values, the same trick as for datatypes is possible for |
1217 Also, for default values, the same trick as for datatypes is possible for |
1197 codatatypes (Section~\ref{ssec:recursive-default-values-for-selectors}). |
1218 codatatypes (Section~\ref{ssec:recursive-default-values-for-selectors}). |
1198 *} |
1219 *} |
|
1220 *) |
1199 |
1221 |
1200 |
1222 |
1201 subsection {* Command Syntax |
1223 subsection {* Command Syntax |
1202 \label{ssec:primcorec-command-syntax} *} |
1224 \label{ssec:primcorec-command-syntax} *} |
1203 |
1225 |
1214 @{syntax_def primcorec_formula}: thmdecl? prop (@'of' (term * ))? |
1236 @{syntax_def primcorec_formula}: thmdecl? prop (@'of' (term * ))? |
1215 "} |
1237 "} |
1216 *} |
1238 *} |
1217 |
1239 |
1218 |
1240 |
|
1241 (* |
1219 subsection {* Generated Theorems |
1242 subsection {* Generated Theorems |
1220 \label{ssec:primcorec-generated-theorems} *} |
1243 \label{ssec:primcorec-generated-theorems} *} |
|
1244 *) |
1221 |
1245 |
1222 |
1246 |
1223 section {* Registering Bounded Natural Functors |
1247 section {* Registering Bounded Natural Functors |
1224 \label{sec:registering-bounded-natural-functors} *} |
1248 \label{sec:registering-bounded-natural-functors} *} |
1225 |
1249 |
1286 % * \keyw{no\_discs\_sels}, \keyw{rep\_compat} |
1310 % * \keyw{no\_discs\_sels}, \keyw{rep\_compat} |
1287 % * hack to have both co and nonco view via locale (cf. ext nats) |
1311 % * hack to have both co and nonco view via locale (cf. ext nats) |
1288 *} |
1312 *} |
1289 |
1313 |
1290 |
1314 |
|
1315 (* |
1291 subsection {* Introductory Example |
1316 subsection {* Introductory Example |
1292 \label{ssec:ctors-introductory-example} *} |
1317 \label{ssec:ctors-introductory-example} *} |
|
1318 *) |
1293 |
1319 |
1294 |
1320 |
1295 subsection {* Command Syntax |
1321 subsection {* Command Syntax |
1296 \label{ssec:ctors-command-syntax} *} |
1322 \label{ssec:ctors-command-syntax} *} |
1297 |
1323 |
1375 % |
1401 % |
1376 %* issues with HOL-Proofs? |
1402 %* issues with HOL-Proofs? |
1377 % |
1403 % |
1378 %* partial documentation |
1404 %* partial documentation |
1379 % |
1405 % |
1380 %* too much output by commands like "datatype_new" and "codatatype" |
|
1381 % |
|
1382 %* no direct way to define recursive functions for default values -- but show trick |
|
1383 % based on overloading |
|
1384 % |
|
1385 %* no way to register "sum" and "prod" as (co)datatypes to enable N2M reduction for them |
1406 %* no way to register "sum" and "prod" as (co)datatypes to enable N2M reduction for them |
1386 % (for @{command datatype_new_compat} and prim(co)rec) |
1407 % (for @{command datatype_new_compat} and prim(co)rec) |
1387 % |
1408 % |
1388 %* no way to register same type as both data- and codatatype? |
1409 % * a fortiori, no way to register same type as both data- and codatatype |
1389 % |
1410 % |
1390 %* no recursion through unused arguments (unlike with the old package) |
1411 %* no recursion through unused arguments (unlike with the old package) |
1391 % |
1412 % |
1392 %* in a locale, cannot use locally fixed types (because of limitation in typedef)? |
1413 %* in a locale, cannot use locally fixed types (because of limitation in typedef)? |
|
1414 % |
|
1415 % *names of variables suboptimal |
1393 *} |
1416 *} |
1394 |
1417 |
1395 |
1418 |
1396 section {* Acknowledgments |
1419 section {* Acknowledgments |
1397 \label{sec:acknowledgments} *} |
1420 \label{sec:acknowledgments} *} |