1000 and @{text "\<CODEGEN>"} commands: the list of constants |
1000 and @{text "\<CODEGEN>"} commands: the list of constants |
1001 may be omitted. Then, all constants with code theorems |
1001 may be omitted. Then, all constants with code theorems |
1002 in the current cache are referred to. |
1002 in the current cache are referred to. |
1003 *} |
1003 *} |
1004 |
1004 |
1005 subsection {* Axiomatic extensions *} |
1005 subsection {* Code generation and proof extraction *} |
1006 |
1006 |
1007 text {* |
1007 text {* |
1008 \begin{warn} |
1008 \fixme |
1009 The extensions introduced in this section, though working |
|
1010 in practice, are not the cream of the crop, as you |
|
1011 will notice during reading. They will |
|
1012 eventually be replaced by more mature approaches. |
|
1013 \end{warn} |
|
1014 |
|
1015 Sometimes equalities are taken for granted which are |
|
1016 not derivable inside the HOL logic but are silently assumed |
|
1017 to hold for executable code. For example, we may want |
|
1018 to identify the famous HOL constant @{const arbitrary} |
|
1019 of type @{typ "'a option"} with @{const None}. |
|
1020 By brute force: |
|
1021 *} |
|
1022 |
|
1023 axiomatization where |
|
1024 "arbitrary = None" |
|
1025 |
|
1026 text {* |
|
1027 However this has to be considered harmful since this axiom, |
|
1028 though probably justifiable for generated code, could |
|
1029 introduce serious inconsistencies into the logic. |
|
1030 |
|
1031 So, there is a distinguished construct for stating axiomatic |
|
1032 equalities of constants which apply only for code generation. |
|
1033 Before introducing this, here is a convenient place to describe |
|
1034 shortly how to deal with some restrictions the type discipline |
|
1035 imposes. |
|
1036 |
|
1037 By itself, the constant @{const arbitrary} is a non-overloaded |
|
1038 polymorphic constant. So, there is no way to distinguish |
|
1039 different versions of @{const arbitrary} for different types |
|
1040 inside the code generator framework. However, inlining |
|
1041 theorems together with auxiliary constants provide a solution: |
|
1042 *} |
|
1043 |
|
1044 definition |
|
1045 arbitrary_option :: "'a option" where |
|
1046 [symmetric, code inline]: "arbitrary_option = arbitrary" |
|
1047 |
|
1048 text {* |
|
1049 By that, we replace any @{const arbitrary} with option type |
|
1050 by @{const arbitrary_option} in defining equations. |
|
1051 |
|
1052 For technical reasons, we further have to provide a |
|
1053 synonym for @{const None} which in code generator view |
|
1054 is a function rather than a term constructor: |
|
1055 *} |
|
1056 |
|
1057 definition |
|
1058 "None' = None" |
|
1059 |
|
1060 text {* |
|
1061 Then finally we are enabled to use @{text "\<CODEAXIOMS>"}: |
|
1062 *} |
|
1063 |
|
1064 code_axioms |
|
1065 arbitrary_option \<equiv> None' |
|
1066 |
|
1067 text {* |
|
1068 A dummy example: |
|
1069 *} |
|
1070 |
|
1071 fun |
|
1072 dummy_option :: "'a list \<Rightarrow> 'a option" where |
|
1073 "dummy_option (x#xs) = Some x" |
|
1074 | "dummy_option [] = arbitrary" |
|
1075 |
|
1076 code_gen dummy_option (*<*)in SML(*>*)in SML file "examples/arbitrary.ML" |
|
1077 |
|
1078 text {* |
|
1079 \lstsml{Thy/examples/arbitrary.ML} |
|
1080 |
|
1081 \medskip |
|
1082 |
|
1083 Another axiomatic extension is code generation |
|
1084 for abstracted types. For this, the |
|
1085 @{text "Executable_Set"} theory (see \secref{exec_set}) |
|
1086 forms a good example. |
|
1087 *} |
1009 *} |
1088 |
1010 |
1089 |
1011 |
1090 section {* ML interfaces \label{sec:ml} *} |
1012 section {* ML interfaces \label{sec:ml} *} |
1091 |
1013 |
1094 a nice Isar interface but also to form a base for |
1016 a nice Isar interface but also to form a base for |
1095 code-generation-based applications, here a short |
1017 code-generation-based applications, here a short |
1096 description of the most important ML interfaces. |
1018 description of the most important ML interfaces. |
1097 *} |
1019 *} |
1098 |
1020 |
1099 subsection {* Constants with type discipline: codegen\_consts.ML *} |
1021 subsection {* Basics: @{text CodeUnit} *} |
1100 |
1022 |
1101 text {* |
1023 text {* |
1102 This Pure module manages identification of (probably overloaded) |
1024 This module provides identification of (probably overloaded) |
1103 constants by unique identifiers. |
1025 constants by unique identifiers. |
1104 *} |
1026 *} |
1105 |
1027 |
1106 text %mlref {* |
1028 text %mlref {* |
1107 \begin{mldecls} |
1029 \begin{mldecls} |
1108 @{index_ML_type CodegenConsts.const: "string * string option"} \\ |
1030 @{index_ML_type CodeUnit.const: "string * string option"} \\ |
1109 @{index_ML CodegenConsts.const_of_cexpr: "theory -> string * typ -> CodegenConsts.const"} \\ |
1031 @{index_ML CodeUnit.const_of_cexpr: "theory -> string * typ -> CodeUnit.const"} \\ |
1110 \end{mldecls} |
1032 \end{mldecls} |
1111 |
1033 |
1112 \begin{description} |
1034 \begin{description} |
1113 |
1035 |
1114 \item @{ML_type CodegenConsts.const} is the identifier type: |
1036 \item @{ML_type CodeUnit.const} is the identifier type: |
1115 the product of a \emph{string} with a list of \emph{typs}. |
1037 the product of a \emph{string} with a list of \emph{typs}. |
1116 The \emph{string} is the constant name as represented inside Isabelle; |
1038 The \emph{string} is the constant name as represented inside Isabelle; |
1117 for overloaded constants, the attached \emph{string option} |
1039 for overloaded constants, the attached \emph{string option} |
1118 is either @{text SOME} type constructor denoting an instance, |
1040 is either @{text SOME} type constructor denoting an instance, |
1119 or @{text NONE} for the polymorphic constant. |
1041 or @{text NONE} for the polymorphic constant. |
1120 |
1042 |
1121 \item @{ML CodegenConsts.const_of_cexpr}~@{text thy}~@{text "(constname, typ)"} |
1043 \item @{ML CodeUnit.const_of_cexpr}~@{text thy}~@{text "(constname, typ)"} |
1122 maps a constant expression @{text "(constname, typ)"} |
1044 maps a constant expression @{text "(constname, typ)"} |
1123 to its canonical identifier. |
1045 to its canonical identifier. |
1124 |
1046 |
1125 \end{description} |
1047 \end{description} |
1126 *} |
1048 *} |
1127 |
1049 |
1128 subsection {* Executable theory content: codegen\_data.ML *} |
1050 subsection {* Executable theory content: @{text Code} *} |
1129 |
1051 |
1130 text {* |
1052 text {* |
1131 This Pure module implements the core notions of |
1053 This Pure module implements the core notions of |
1132 executable content of a theory. |
1054 executable content of a theory. |
1133 *} |
1055 *} |
1134 |
1056 |
1135 subsubsection {* Suspended theorems *} |
1057 subsubsection {* Managing executable content *} |
1136 |
1058 |
1137 text %mlref {* |
1059 text %mlref {* |
1138 \begin{mldecls} |
1060 \begin{mldecls} |
1139 @{index_ML CodegenData.lazy_thms: "(unit -> thm list) -> thm list Susp.T"} |
1061 @{index_ML Code.add_func: "bool -> thm -> theory -> theory"} \\ |
|
1062 @{index_ML Code.del_func: "thm -> theory -> theory"} \\ |
|
1063 @{index_ML Code.add_funcl: "CodeUnit.const * thm list Susp.T -> theory -> theory"} \\ |
|
1064 @{index_ML Code.add_inline: "thm -> theory -> theory"} \\ |
|
1065 @{index_ML Code.del_inline: "thm -> theory -> theory"} \\ |
|
1066 @{index_ML Code.add_inline_proc: "string * (theory -> cterm list -> thm list) |
|
1067 -> theory -> theory"} \\ |
|
1068 @{index_ML Code.del_inline_proc: "string -> theory -> theory"} \\ |
|
1069 @{index_ML Code.add_preproc: "string * (theory -> thm list -> thm list) |
|
1070 -> theory -> theory"} \\ |
|
1071 @{index_ML Code.del_preproc: "string -> theory -> theory"} \\ |
|
1072 @{index_ML Code.add_datatype: "string * ((string * sort) list * (string * typ list) list) |
|
1073 -> theory -> theory"} \\ |
|
1074 @{index_ML Code.get_datatype: "theory -> string |
|
1075 -> (string * sort) list * (string * typ list) list"} \\ |
|
1076 @{index_ML Code.get_datatype_of_constr: "theory -> CodeUnit.const -> string option"} |
1140 \end{mldecls} |
1077 \end{mldecls} |
1141 |
1078 |
1142 \begin{description} |
1079 \begin{description} |
1143 |
1080 |
1144 \item @{ML CodegenData.lazy_thms}~@{text f} turns an abstract |
1081 \item @{ML Code.add_func}~@{text "thm"}~@{text "thy"} adds function |
1145 theorem computation @{text f} into a suspension of theorems. |
|
1146 |
|
1147 \end{description} |
|
1148 *} |
|
1149 |
|
1150 subsubsection {* Managing executable content *} |
|
1151 |
|
1152 text %mlref {* |
|
1153 \begin{mldecls} |
|
1154 @{index_ML CodegenData.add_func: "bool -> thm -> theory -> theory"} \\ |
|
1155 @{index_ML CodegenData.del_func: "thm -> theory -> theory"} \\ |
|
1156 @{index_ML CodegenData.add_funcl: "CodegenConsts.const * thm list Susp.T -> theory -> theory"} \\ |
|
1157 @{index_ML CodegenData.add_inline: "thm -> theory -> theory"} \\ |
|
1158 @{index_ML CodegenData.del_inline: "thm -> theory -> theory"} \\ |
|
1159 @{index_ML CodegenData.add_inline_proc: "string * (theory -> cterm list -> thm list) |
|
1160 -> theory -> theory"} \\ |
|
1161 @{index_ML CodegenData.del_inline_proc: "string -> theory -> theory"} \\ |
|
1162 @{index_ML CodegenData.add_preproc: "string * (theory -> thm list -> thm list) |
|
1163 -> theory -> theory"} \\ |
|
1164 @{index_ML CodegenData.del_preproc: "string -> theory -> theory"} \\ |
|
1165 @{index_ML CodegenData.add_datatype: "string * ((string * sort) list * (string * typ list) list) |
|
1166 -> theory -> theory"} \\ |
|
1167 @{index_ML CodegenData.get_datatype: "theory -> string |
|
1168 -> (string * sort) list * (string * typ list) list"} \\ |
|
1169 @{index_ML CodegenData.get_datatype_of_constr: "theory -> CodegenConsts.const -> string option"} |
|
1170 \end{mldecls} |
|
1171 |
|
1172 \begin{description} |
|
1173 |
|
1174 \item @{ML CodegenData.add_func}~@{text "thm"}~@{text "thy"} adds function |
|
1175 theorem @{text "thm"} to executable content. |
1082 theorem @{text "thm"} to executable content. |
1176 |
1083 |
1177 \item @{ML CodegenData.del_func}~@{text "thm"}~@{text "thy"} removes function |
1084 \item @{ML Code.del_func}~@{text "thm"}~@{text "thy"} removes function |
1178 theorem @{text "thm"} from executable content, if present. |
1085 theorem @{text "thm"} from executable content, if present. |
1179 |
1086 |
1180 \item @{ML CodegenData.add_funcl}~@{text "(const, lthms)"}~@{text "thy"} adds |
1087 \item @{ML Code.add_funcl}~@{text "(const, lthms)"}~@{text "thy"} adds |
1181 suspended defining equations @{text lthms} for constant |
1088 suspended defining equations @{text lthms} for constant |
1182 @{text const} to executable content. |
1089 @{text const} to executable content. |
1183 |
1090 |
1184 \item @{ML CodegenData.add_inline}~@{text "thm"}~@{text "thy"} adds |
1091 \item @{ML Code.add_inline}~@{text "thm"}~@{text "thy"} adds |
1185 inlining theorem @{text thm} to executable content. |
1092 inlining theorem @{text thm} to executable content. |
1186 |
1093 |
1187 \item @{ML CodegenData.del_inline}~@{text "thm"}~@{text "thy"} remove |
1094 \item @{ML Code.del_inline}~@{text "thm"}~@{text "thy"} remove |
1188 inlining theorem @{text thm} from executable content, if present. |
1095 inlining theorem @{text thm} from executable content, if present. |
1189 |
1096 |
1190 \item @{ML CodegenData.add_inline_proc}~@{text "(name, f)"}~@{text "thy"} adds |
1097 \item @{ML Code.add_inline_proc}~@{text "(name, f)"}~@{text "thy"} adds |
1191 inline procedure @{text f} (named @{text name}) to executable content; |
1098 inline procedure @{text f} (named @{text name}) to executable content; |
1192 @{text f} is a computation of rewrite rules dependent on |
1099 @{text f} is a computation of rewrite rules dependent on |
1193 the current theory context and the list of all arguments |
1100 the current theory context and the list of all arguments |
1194 and right hand sides of the defining equations belonging |
1101 and right hand sides of the defining equations belonging |
1195 to a certain function definition. |
1102 to a certain function definition. |
1196 |
1103 |
1197 \item @{ML CodegenData.del_inline_proc}~@{text "name"}~@{text "thy"} removes |
1104 \item @{ML Code.del_inline_proc}~@{text "name"}~@{text "thy"} removes |
1198 inline procedure named @{text name} from executable content. |
1105 inline procedure named @{text name} from executable content. |
1199 |
1106 |
1200 \item @{ML CodegenData.add_preproc}~@{text "(name, f)"}~@{text "thy"} adds |
1107 \item @{ML Code.add_preproc}~@{text "(name, f)"}~@{text "thy"} adds |
1201 generic preprocessor @{text f} (named @{text name}) to executable content; |
1108 generic preprocessor @{text f} (named @{text name}) to executable content; |
1202 @{text f} is a transformation of the defining equations belonging |
1109 @{text f} is a transformation of the defining equations belonging |
1203 to a certain function definition, depending on the |
1110 to a certain function definition, depending on the |
1204 current theory context. |
1111 current theory context. |
1205 |
1112 |
1206 \item @{ML CodegenData.del_preproc}~@{text "name"}~@{text "thy"} removes |
1113 \item @{ML Code.del_preproc}~@{text "name"}~@{text "thy"} removes |
1207 generic preprcoessor named @{text name} from executable content. |
1114 generic preprcoessor named @{text name} from executable content. |
1208 |
1115 |
1209 \item @{ML CodegenData.add_datatype}~@{text "(name, spec)"}~@{text "thy"} adds |
1116 \item @{ML Code.add_datatype}~@{text "(name, spec)"}~@{text "thy"} adds |
1210 a datatype to executable content, with type constructor |
1117 a datatype to executable content, with type constructor |
1211 @{text name} and specification @{text spec}; @{text spec} is |
1118 @{text name} and specification @{text spec}; @{text spec} is |
1212 a pair consisting of a list of type variable with sort |
1119 a pair consisting of a list of type variable with sort |
1213 constraints and a list of constructors with name |
1120 constraints and a list of constructors with name |
1214 and types of arguments. |
1121 and types of arguments. |
1215 |
1122 |
1216 \item @{ML CodegenData.get_datatype_of_constr}~@{text "thy"}~@{text "const"} |
1123 \item @{ML Code.get_datatype_of_constr}~@{text "thy"}~@{text "const"} |
1217 returns type constructor corresponding to |
1124 returns type constructor corresponding to |
1218 constructor @{text const}; returns @{text NONE} |
1125 constructor @{text const}; returns @{text NONE} |
1219 if @{text const} is no constructor. |
1126 if @{text const} is no constructor. |
1220 |
1127 |
1221 \end{description} |
1128 \end{description} |
1223 |
1130 |
1224 subsection {* Auxiliary *} |
1131 subsection {* Auxiliary *} |
1225 |
1132 |
1226 text %mlref {* |
1133 text %mlref {* |
1227 \begin{mldecls} |
1134 \begin{mldecls} |
1228 @{index_ML CodegenConsts.const_ord: "CodegenConsts.const * CodegenConsts.const -> order"} \\ |
1135 @{index_ML CodeUnit.const_ord: "CodeUnit.const * CodeUnit.const -> order"} \\ |
1229 @{index_ML CodegenConsts.eq_const: "CodegenConsts.const * CodegenConsts.const -> bool"} \\ |
1136 @{index_ML CodeUnit.eq_const: "CodeUnit.const * CodeUnit.const -> bool"} \\ |
1230 @{index_ML CodegenConsts.read_const: "theory -> string -> CodegenConsts.const"} \\ |
1137 @{index_ML CodeUnit.read_const: "theory -> string -> CodeUnit.const"} \\ |
1231 @{index_ML_structure CodegenConsts.Consttab} \\ |
1138 @{index_ML_structure CodeUnit.Consttab} \\ |
1232 @{index_ML CodegenFunc.head_func: "thm -> CodegenConsts.const * typ"} \\ |
1139 @{index_ML CodeUnit.head_func: "thm -> CodeUnit.const * typ"} \\ |
1233 @{index_ML CodegenFunc.rewrite_func: "thm list -> thm -> thm"} \\ |
1140 @{index_ML CodeUnit.rewrite_func: "thm list -> thm -> thm"} \\ |
1234 \end{mldecls} |
1141 \end{mldecls} |
1235 |
1142 |
1236 \begin{description} |
1143 \begin{description} |
1237 |
1144 |
1238 \item @{ML CodegenConsts.const_ord},~@{ML CodegenConsts.eq_const} |
1145 \item @{ML CodeUnit.const_ord},~@{ML CodeUnit.eq_const} |
1239 provide order and equality on constant identifiers. |
1146 provide order and equality on constant identifiers. |
1240 |
1147 |
1241 \item @{ML_struct CodegenConsts.Consttab} |
1148 \item @{ML_struct CodeUnit.Consttab} |
1242 provides table structures with constant identifiers as keys. |
1149 provides table structures with constant identifiers as keys. |
1243 |
1150 |
1244 \item @{ML CodegenConsts.read_const}~@{text thy}~@{text s} |
1151 \item @{ML CodeUnit.read_const}~@{text thy}~@{text s} |
1245 reads a constant as a concrete term expression @{text s}. |
1152 reads a constant as a concrete term expression @{text s}. |
1246 |
1153 |
1247 \item @{ML CodegenFunc.head_func}~@{text thm} |
1154 \item @{ML CodeUnit.head_func}~@{text thm} |
1248 extracts the constant and its type from a defining equation @{text thm}. |
1155 extracts the constant and its type from a defining equation @{text thm}. |
1249 |
1156 |
1250 \item @{ML CodegenFunc.rewrite_func}~@{text rews}~@{text thm} |
1157 \item @{ML CodeUnit.rewrite_func}~@{text rews}~@{text thm} |
1251 rewrites a defining equation @{text thm} with a set of rewrite |
1158 rewrites a defining equation @{text thm} with a set of rewrite |
1252 rules @{text rews}; only arguments and right hand side are rewritten, |
1159 rules @{text rews}; only arguments and right hand side are rewritten, |
1253 not the head of the defining equation. |
1160 not the head of the defining equation. |
1254 |
1161 |
1255 \end{description} |
1162 \end{description} |
1383 following operations: |
1290 following operations: |
1384 *} |
1291 *} |
1385 |
1292 |
1386 text %mlref {* |
1293 text %mlref {* |
1387 \begin{mldecls} |
1294 \begin{mldecls} |
1388 @{index_ML_type CodegenFuncgr.T} \\ |
1295 @{index_ML_type CodeFuncgr.T} \\ |
1389 @{index_ML CodegenFuncgr.funcs: "CodegenFuncgr.T -> CodegenConsts.const -> thm list"} \\ |
1296 @{index_ML CodeFuncgr.funcs: "CodeFuncgr.T -> CodeUnit.const -> thm list"} \\ |
1390 @{index_ML CodegenFuncgr.typ: "CodegenFuncgr.T -> CodegenConsts.const -> typ"} \\ |
1297 @{index_ML CodeFuncgr.typ: "CodeFuncgr.T -> CodeUnit.const -> typ"} \\ |
1391 @{index_ML CodegenFuncgr.deps: "CodegenFuncgr.T |
1298 @{index_ML CodeFuncgr.deps: "CodeFuncgr.T |
1392 -> CodegenConsts.const list -> CodegenConsts.const list list"} \\ |
1299 -> CodeUnit.const list -> CodeUnit.const list list"} \\ |
1393 @{index_ML CodegenFuncgr.all: "CodegenFuncgr.T -> CodegenConsts.const list"} |
1300 @{index_ML CodeFuncgr.all: "CodeFuncgr.T -> CodeUnit.const list"} |
1394 \end{mldecls} |
1301 \end{mldecls} |
1395 |
1302 |
1396 \begin{description} |
1303 \begin{description} |
1397 |
1304 |
1398 \item @{ML_type CodegenFuncgr.T} represents |
1305 \item @{ML_type CodeFuncgr.T} represents |
1399 a normalized defining equation system. |
1306 a normalized defining equation system. |
1400 |
1307 |
1401 \item @{ML CodegenFuncgr.funcs}~@{text funcgr}~@{text const} |
1308 \item @{ML CodeFuncgr.funcs}~@{text funcgr}~@{text const} |
1402 retrieves defining equiations for constant @{text const}. |
1309 retrieves defining equiations for constant @{text const}. |
1403 |
1310 |
1404 \item @{ML CodegenFuncgr.typ}~@{text funcgr}~@{text const} |
1311 \item @{ML CodeFuncgr.typ}~@{text funcgr}~@{text const} |
1405 retrieves function type for constant @{text const}. |
1312 retrieves function type for constant @{text const}. |
1406 |
1313 |
1407 \item @{ML CodegenFuncgr.deps}~@{text funcgr}~@{text consts} |
1314 \item @{ML CodeFuncgr.deps}~@{text funcgr}~@{text consts} |
1408 returns the transitive closure of dependencies for |
1315 returns the transitive closure of dependencies for |
1409 constants @{text consts} as a partitioning where each partition |
1316 constants @{text consts} as a partitioning where each partition |
1410 corresponds to a strongly connected component of |
1317 corresponds to a strongly connected component of |
1411 dependencies and any partition does \emph{not} |
1318 dependencies and any partition does \emph{not} |
1412 depend on partitions further left. |
1319 depend on partitions further left. |
1413 |
1320 |
1414 \item @{ML CodegenFuncgr.all}~@{text funcgr} |
1321 \item @{ML CodeFuncgr.all}~@{text funcgr} |
1415 returns all currently represented constants. |
1322 returns all currently represented constants. |
1416 |
1323 |
1417 \end{description} |
1324 \end{description} |
1418 *} |
1325 *} |
1419 |
1326 |