doc-src/IsarAdvanced/Codegen/Thy/Codegen.thy
changeset 21545 54cc492d80a9
parent 21452 f825e0b4d566
child 21993 4b802a9e0738
equal deleted inserted replaced
21544:a9ceeb182cfc 21545:54cc492d80a9
   151 
   151 
   152 text {*
   152 text {*
   153   This executable specification is now turned to SML code:
   153   This executable specification is now turned to SML code:
   154 *}
   154 *}
   155 
   155 
   156 code_gen fac (*<*)(SML *)(*>*)(SML "examples/fac.ML")
   156 code_gen fac (*<*)(SML #)(*>*)(SML "examples/fac.ML")
   157 
   157 
   158 text {*
   158 text {*
   159   The @{text "\<CODEGEN>"} command takes a space-separated list of
   159   The @{text "\<CODEGEN>"} command takes a space-separated list of
   160   constants together with \qn{serialization directives}
   160   constants together with \qn{serialization directives}
   161   in parentheses. These start with a \qn{target language}
   161   in parentheses. These start with a \qn{target language}
   267 
   267 
   268 lemma [code func]:
   268 lemma [code func]:
   269   "pick ((k, v)#xs) n = (if n < k then v else pick xs (n - k))"
   269   "pick ((k, v)#xs) n = (if n < k then v else pick xs (n - k))"
   270   by simp
   270   by simp
   271 
   271 
   272 code_gen pick (*<*)(SML *)(*>*)(SML "examples/pick1.ML")
   272 code_gen pick (*<*)(SML #)(*>*)(SML "examples/pick1.ML")
   273 
   273 
   274 text {*
   274 text {*
   275   This theorem is now added to the function equation table:
   275   This theorem is now added to the function equation table:
   276 
   276 
   277   \lstsml{Thy/examples/pick1.ML}
   277   \lstsml{Thy/examples/pick1.ML}
   280   equation, using the \emph{nofunc} attribute:
   280   equation, using the \emph{nofunc} attribute:
   281 *}
   281 *}
   282 
   282 
   283 lemmas [code nofunc] = pick.simps 
   283 lemmas [code nofunc] = pick.simps 
   284 
   284 
   285 code_gen pick (*<*)(SML *)(*>*)(SML "examples/pick2.ML")
   285 code_gen pick (*<*)(SML #)(*>*)(SML "examples/pick2.ML")
   286 
   286 
   287 text {*
   287 text {*
   288   \lstsml{Thy/examples/pick2.ML}
   288   \lstsml{Thy/examples/pick2.ML}
   289 
   289 
   290   Syntactic redundancies are implicitly dropped. For example,
   290   Syntactic redundancies are implicitly dropped. For example,
   296 
   296 
   297 lemma [code func]:
   297 lemma [code func]:
   298   "fac n = (case n of 0 \<Rightarrow> 1 | Suc m \<Rightarrow> n * fac m)"
   298   "fac n = (case n of 0 \<Rightarrow> 1 | Suc m \<Rightarrow> n * fac m)"
   299   by (cases n) simp_all
   299   by (cases n) simp_all
   300 
   300 
   301 code_gen fac (*<*)(SML *)(*>*)(SML "examples/fac_case.ML")
   301 code_gen fac (*<*)(SML #)(*>*)(SML "examples/fac_case.ML")
   302 
   302 
   303 text {*
   303 text {*
   304   \lstsml{Thy/examples/fac_case.ML}
   304   \lstsml{Thy/examples/fac_case.ML}
   305 
   305 
   306   \begin{warn}
   306   \begin{warn}
   375   (we have left out all other modules).
   375   (we have left out all other modules).
   376 
   376 
   377   The whole code in SML with explicit dictionary passing:
   377   The whole code in SML with explicit dictionary passing:
   378 *}
   378 *}
   379 
   379 
   380 code_gen dummy (*<*)(SML *)(*>*)(SML "examples/class.ML")
   380 code_gen dummy (*<*)(SML #)(*>*)(SML "examples/class.ML")
   381 
   381 
   382 text {*
   382 text {*
   383   \lstsml{Thy/examples/class.ML}
   383   \lstsml{Thy/examples/class.ML}
   384 *}
   384 *}
   385 
   385 
   555   (SML)
   555   (SML)
   556 code_const %tt True and False and "op \<and>" and Not
   556 code_const %tt True and False and "op \<and>" and Not
   557   (SML and and and)
   557   (SML and and and)
   558 (*>*)
   558 (*>*)
   559 
   559 
   560 code_gen in_interval (*<*)(SML *)(*>*)(SML "examples/bool_literal.ML")
   560 code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_literal.ML")
   561 
   561 
   562 text {*
   562 text {*
   563   \lstsml{Thy/examples/bool_literal.ML}
   563   \lstsml{Thy/examples/bool_literal.ML}
   564 
   564 
   565   Though this is correct code, it is a little bit unsatisfactory:
   565   Though this is correct code, it is a little bit unsatisfactory:
   598   is not used for generated code, we use @{text "\<CODERESERVED>"}.
   598   is not used for generated code, we use @{text "\<CODERESERVED>"}.
   599 
   599 
   600   After this setup, code looks quite more readable:
   600   After this setup, code looks quite more readable:
   601 *}
   601 *}
   602 
   602 
   603 code_gen in_interval (*<*)(SML *)(*>*)(SML "examples/bool_mlbool.ML")
   603 code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_mlbool.ML")
   604 
   604 
   605 text {*
   605 text {*
   606   \lstsml{Thy/examples/bool_mlbool.ML}
   606   \lstsml{Thy/examples/bool_mlbool.ML}
   607 
   607 
   608   This still is not perfect: the parentheses
   608   This still is not perfect: the parentheses
   614 *}
   614 *}
   615 
   615 
   616 code_const %tt "op \<and>"
   616 code_const %tt "op \<and>"
   617   (SML infixl 1 "andalso")
   617   (SML infixl 1 "andalso")
   618 
   618 
   619 code_gen in_interval (*<*)(SML *)(*>*)(SML "examples/bool_infix.ML")
   619 code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_infix.ML")
   620 
   620 
   621 text {*
   621 text {*
   622   \lstsml{Thy/examples/bool_infix.ML}
   622   \lstsml{Thy/examples/bool_infix.ML}
   623 
   623 
   624   Next, we try to map HOL pairs to SML pairs, using the
   624   Next, we try to map HOL pairs to SML pairs, using the
   673 
   673 
   674 code_const %tt "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   674 code_const %tt "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   675     and "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   675     and "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   676   (SML "IntInf.+ (_, _)" and "IntInf.* (_, _)")
   676   (SML "IntInf.+ (_, _)" and "IntInf.* (_, _)")
   677 
   677 
   678 code_gen double_inc (*<*)(SML *)(*>*)(SML "examples/integers.ML")
   678 code_gen double_inc (*<*)(SML #)(*>*)(SML "examples/integers.ML")
   679 
   679 
   680 text {*
   680 text {*
   681   resulting in:
   681   resulting in:
   682 
   682 
   683   \lstsml{Thy/examples/integers.ML}
   683   \lstsml{Thy/examples/integers.ML}
   725   The membership test during preprocessing is rewritten,
   725   The membership test during preprocessing is rewritten,
   726   resulting in @{const List.memberl}, which itself
   726   resulting in @{const List.memberl}, which itself
   727   performs an explicit equality check.
   727   performs an explicit equality check.
   728 *}
   728 *}
   729 
   729 
   730 code_gen collect_duplicates (*<*)(SML *)(*>*)(SML "examples/collect_duplicates.ML")
   730 code_gen collect_duplicates (*<*)(SML #)(*>*)(SML "examples/collect_duplicates.ML")
   731 
   731 
   732 text {*
   732 text {*
   733   \lstsml{Thy/examples/collect_duplicates.ML}
   733   \lstsml{Thy/examples/collect_duplicates.ML}
   734 *}
   734 *}
   735 
   735 
   801 
   801 
   802 text {*
   802 text {*
   803   Then everything goes fine:
   803   Then everything goes fine:
   804 *}
   804 *}
   805 
   805 
   806 code_gen lookup (*<*)(SML *)(*>*)(SML "examples/lookup.ML")
   806 code_gen lookup (*<*)(SML #)(*>*)(SML "examples/lookup.ML")
   807 
   807 
   808 text {*
   808 text {*
   809   \lstsml{Thy/examples/lookup.ML}
   809   \lstsml{Thy/examples/lookup.ML}
   810 *}
   810 *}
   811 
   811 
   854 text {*
   854 text {*
   855   Then code generation succeeds:
   855   Then code generation succeeds:
   856 *}
   856 *}
   857 
   857 
   858 code_gen "op \<le> \<Colon> 'a\<Colon>{eq, ord} \<times> 'b\<Colon>{eq, ord} \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool"
   858 code_gen "op \<le> \<Colon> 'a\<Colon>{eq, ord} \<times> 'b\<Colon>{eq, ord} \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool"
   859   (*<*)(SML *)(*>*)(SML "examples/lexicographic.ML")
   859   (*<*)(SML #)(*>*)(SML "examples/lexicographic.ML")
   860 
   860 
   861 text {*
   861 text {*
   862   \lstsml{Thy/examples/lexicographic.ML}
   862   \lstsml{Thy/examples/lexicographic.ML}
   863 *}
   863 *}
   864 
   864 
   958 *}
   958 *}
   959 
   959 
   960 lemma [code func]:
   960 lemma [code func]:
   961   "insert = insert" ..
   961   "insert = insert" ..
   962 
   962 
   963 code_gen dummy_set foobar_set (*<*)(SML *)(*>*)(SML "examples/dirty_set.ML")
   963 code_gen dummy_set foobar_set (*<*)(SML #)(*>*)(SML "examples/dirty_set.ML")
   964 
   964 
   965 text {*
   965 text {*
   966   \lstsml{Thy/examples/dirty_set.ML}
   966   \lstsml{Thy/examples/dirty_set.ML}
   967 
   967 
   968   Reflexive function equations by convention are dropped.
   968   Reflexive function equations by convention are dropped.
  1085   "dummy_option [] = arbitrary"
  1085   "dummy_option [] = arbitrary"
  1086 (*<*)
  1086 (*<*)
  1087 declare dummy_option.simps [code func]
  1087 declare dummy_option.simps [code func]
  1088 (*>*)
  1088 (*>*)
  1089 
  1089 
  1090 code_gen dummy_option (*<*)(SML *)(*>*)(SML "examples/arbitrary.ML")
  1090 code_gen dummy_option (*<*)(SML #)(*>*)(SML "examples/arbitrary.ML")
  1091 
  1091 
  1092 text {*
  1092 text {*
  1093   \lstsml{Thy/examples/arbitrary.ML}
  1093   \lstsml{Thy/examples/arbitrary.ML}
  1094 
  1094 
  1095   Another axiomatic extension is code generation
  1095   Another axiomatic extension is code generation