278 print_codesetup |
278 print_codesetup |
279 |
279 |
280 text {* |
280 text {* |
281 \noindent which displays a table of constant with corresponding |
281 \noindent which displays a table of constant with corresponding |
282 defining equations (the additional stuff displayed |
282 defining equations (the additional stuff displayed |
283 shall not bother us for the moment). If this table does |
283 shall not bother us for the moment). |
284 not provide at least one defining |
|
285 equation for a particular constant, the table of primitive |
|
286 definitions is searched |
|
287 whether it provides one. |
|
288 |
284 |
289 The typical HOL tools are already set up in a way that |
285 The typical HOL tools are already set up in a way that |
290 function definitions introduced by @{text "\<FUN>"}, |
286 function definitions introduced by @{text "\<DEFINITION>"}, |
|
287 @{text "\<FUN>"}, |
291 @{text "\<FUNCTION>"}, @{text "\<PRIMREC>"} |
288 @{text "\<FUNCTION>"}, @{text "\<PRIMREC>"} |
292 @{text "\<RECDEF>"} are implicitly propagated |
289 @{text "\<RECDEF>"} are implicitly propagated |
293 to this defining equation table. Specific theorems may be |
290 to this defining equation table. Specific theorems may be |
294 selected using an attribute: \emph{code func}. As example, |
291 selected using an attribute: \emph{code func}. As example, |
295 a weight selector function: |
292 a weight selector function: |
859 are dependent on operational equality. For example, let |
856 are dependent on operational equality. For example, let |
860 us define a lexicographic ordering on tuples: |
857 us define a lexicographic ordering on tuples: |
861 *} |
858 *} |
862 |
859 |
863 instance * :: (ord, ord) ord |
860 instance * :: (ord, ord) ord |
864 "p1 < p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in |
861 less_prod_def: "p1 < p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in |
865 x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
862 x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
866 "p1 \<le> p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in |
863 less_eq_prod_def: "p1 \<le> p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in |
867 x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" .. |
864 x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" .. |
|
865 |
|
866 lemmas [code nofunc] = less_prod_def less_eq_prod_def |
868 |
867 |
869 lemma ord_prod [code func]: |
868 lemma ord_prod [code func]: |
870 "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
869 "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
871 "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" |
870 "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" |
872 unfolding "less_eq_*_def" "less_*_def" by simp_all |
871 unfolding less_prod_def less_eq_prod_def by simp_all |
873 |
872 |
874 text {* |
873 text {* |
875 Then code generation will fail. Why? The definition |
874 Then code generation will fail. Why? The definition |
876 of @{const "op \<le>"} depends on equality on both arguments, |
875 of @{const "op \<le>"} depends on equality on both arguments, |
877 which are polymorphic and impose an additional @{text eq} |
876 which are polymorphic and impose an additional @{text eq} |
881 The solution is to add @{text eq} explicitly to the first sort arguments in the |
880 The solution is to add @{text eq} explicitly to the first sort arguments in the |
882 code theorems: |
881 code theorems: |
883 *} |
882 *} |
884 |
883 |
885 (*<*) |
884 (*<*) |
886 declare ord_prod [code del] |
885 lemmas [code nofunc] = ord_prod |
887 (*>*) |
886 (*>*) |
888 |
887 |
889 lemma ord_prod_code [code func]: |
888 lemma ord_prod_code [code func]: |
890 "(x1 \<Colon> 'a\<Colon>{ord, eq}, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
889 "(x1 \<Colon> 'a\<Colon>{ord, eq}, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
891 "(x1 \<Colon> 'a\<Colon>{ord, eq}, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" |
890 "(x1 \<Colon> 'a\<Colon>{ord, eq}, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" |
905 text {* |
904 text {* |
906 In general, code theorems for overloaded constants may have more |
905 In general, code theorems for overloaded constants may have more |
907 restrictive sort constraints than the underlying instance relation |
906 restrictive sort constraints than the underlying instance relation |
908 between class and type constructor as long as the whole system of |
907 between class and type constructor as long as the whole system of |
909 constraints is coregular; code theorems violating coregularity |
908 constraints is coregular; code theorems violating coregularity |
910 are rejected immediately. |
909 are rejected immediately. Consequently, it might be necessary |
|
910 to delete disturbing theorems in the code theorem table, |
|
911 as we have done here with the original definitions @{text less_prod_def} |
|
912 and @{text less_eq_prod_def}. |
911 *} |
913 *} |
912 |
914 |
913 subsubsection {* Haskell serialization *} |
915 subsubsection {* Haskell serialization *} |
914 |
916 |
915 text {* |
917 text {* |
1000 text {* |
1002 text {* |
1001 In this case the serializer would complain that @{const insert} |
1003 In this case the serializer would complain that @{const insert} |
1002 expects dictionaries (namely an \emph{eq} dictionary) but |
1004 expects dictionaries (namely an \emph{eq} dictionary) but |
1003 has also been given a customary serialization. |
1005 has also been given a customary serialization. |
1004 |
1006 |
1005 The solution to this dilemma: |
1007 \fixme[needs rewrite] The solution to this dilemma: |
1006 *} |
1008 *} |
1007 |
1009 |
1008 lemma [code func]: |
1010 lemma [code func]: |
1009 "insert = insert" .. |
1011 "insert = insert" .. |
1010 |
1012 |
1011 code_gen dummy_set foobar_set (*<*)(SML #)(*>*)(SML "examples/dirty_set.ML") |
1013 code_gen dummy_set foobar_set (*<*)(SML #)(*>*)(SML "examples/dirty_set.ML") |
1012 |
1014 |
1013 text {* |
1015 text {* |
1014 \lstsml{Thy/examples/dirty_set.ML} |
1016 \lstsml{Thy/examples/dirty_set.ML} |
1015 |
1017 |
1016 Reflexive defining equations by convention are dropped. |
1018 Reflexive defining equations by convention are dropped: |
1017 But their presence prevents primitive definitions to be |
|
1018 used as defining equations: |
|
1019 *} |
1019 *} |
1020 |
1020 |
1021 code_thms insert |
1021 code_thms insert |
1022 |
1022 |
1023 text {* |
1023 text {* |
1292 \begin{mldecls} |
1292 \begin{mldecls} |
1293 @{index_ML CodegenConsts.const_ord: "CodegenConsts.const * CodegenConsts.const -> order"} \\ |
1293 @{index_ML CodegenConsts.const_ord: "CodegenConsts.const * CodegenConsts.const -> order"} \\ |
1294 @{index_ML CodegenConsts.eq_const: "CodegenConsts.const * CodegenConsts.const -> bool"} \\ |
1294 @{index_ML CodegenConsts.eq_const: "CodegenConsts.const * CodegenConsts.const -> bool"} \\ |
1295 @{index_ML CodegenConsts.read_const: "theory -> string -> CodegenConsts.const"} \\ |
1295 @{index_ML CodegenConsts.read_const: "theory -> string -> CodegenConsts.const"} \\ |
1296 @{index_ML_structure CodegenConsts.Consttab} \\ |
1296 @{index_ML_structure CodegenConsts.Consttab} \\ |
1297 @{index_ML CodegenFunc.typ_func: "thm -> typ"} \\ |
1297 @{index_ML CodegenFunc.head_func: "thm -> CodegenConsts.const * typ"} \\ |
1298 @{index_ML CodegenFunc.rewrite_func: "thm list -> thm -> thm"} \\ |
1298 @{index_ML CodegenFunc.rewrite_func: "thm list -> thm -> thm"} \\ |
1299 \end{mldecls} |
1299 \end{mldecls} |
1300 |
1300 |
1301 \begin{description} |
1301 \begin{description} |
1302 |
1302 |
1307 provides table structures with constant identifiers as keys. |
1307 provides table structures with constant identifiers as keys. |
1308 |
1308 |
1309 \item @{ML CodegenConsts.read_const}~@{text thy}~@{text s} |
1309 \item @{ML CodegenConsts.read_const}~@{text thy}~@{text s} |
1310 reads a constant as a concrete term expression @{text s}. |
1310 reads a constant as a concrete term expression @{text s}. |
1311 |
1311 |
1312 \item @{ML CodegenFunc.typ_func}~@{text thm} |
1312 \item @{ML CodegenFunc.head_func}~@{text thm} |
1313 extracts the type of a constant in a defining equation @{text thm}. |
1313 extracts the constant and its type from a defining equation @{text thm}. |
1314 |
1314 |
1315 \item @{ML CodegenFunc.rewrite_func}~@{text rews}~@{text thm} |
1315 \item @{ML CodegenFunc.rewrite_func}~@{text rews}~@{text thm} |
1316 rewrites a defining equation @{text thm} with a set of rewrite |
1316 rewrites a defining equation @{text thm} with a set of rewrite |
1317 rules @{text rews}; only arguments and right hand side are rewritten, |
1317 rules @{text rews}; only arguments and right hand side are rewritten, |
1318 not the head of the defining equation. |
1318 not the head of the defining equation. |