doc-src/IsarAdvanced/Codegen/Thy/Codegen.thy
changeset 25870 a6a21adf3b55
parent 25533 0140cc7b26ad
child 26513 6f306c8c2c54
equal deleted inserted replaced
25869:d49bf150c925 25870:a6a21adf3b55
   109   to the class of total orders, @{text linorder}.
   109   to the class of total orders, @{text linorder}.
   110 
   110 
   111   We define @{text find} and @{text update} functions:
   111   We define @{text find} and @{text update} functions:
   112 *}
   112 *}
   113 
   113 
   114 fun
   114 primrec
   115   find :: "('a\<Colon>linorder, 'b) searchtree \<Rightarrow> 'a \<Rightarrow> 'b option" where
   115   find :: "('a\<Colon>linorder, 'b) searchtree \<Rightarrow> 'a \<Rightarrow> 'b option" where
   116   "find (Leaf key val) it = (if it = key then Some val else None)"
   116   "find (Leaf key val) it = (if it = key then Some val else None)"
   117   | "find (Branch t1 key t2) it = (if it \<le> key then find t1 it else find t2 it)"
   117   | "find (Branch t1 key t2) it = (if it \<le> key then find t1 it else find t2 it)"
   118 
   118 
   119 fun
   119 fun
   213 text {*
   213 text {*
   214   Thanks to a reasonable setup of the @{text HOL} theories, in
   214   Thanks to a reasonable setup of the @{text HOL} theories, in
   215   most cases code generation proceeds without further ado:
   215   most cases code generation proceeds without further ado:
   216 *}
   216 *}
   217 
   217 
   218 fun
   218 primrec
   219   fac :: "nat \<Rightarrow> nat" where
   219   fac :: "nat \<Rightarrow> nat" where
   220     "fac 0 = 1"
   220     "fac 0 = 1"
   221   | "fac (Suc n) = Suc n * fac n"
   221   | "fac (Suc n) = Suc n * fac n"
   222 
   222 
   223 text {*
   223 text {*
   260 
   260 
   261 definition
   261 definition
   262   pick_some :: "'a list \<Rightarrow> 'a" where
   262   pick_some :: "'a list \<Rightarrow> 'a" where
   263   "pick_some = hd"
   263   "pick_some = hd"
   264 (*>*)
   264 (*>*)
       
   265 
   265 export_code pick_some in SML file "examples/fail_const.ML"
   266 export_code pick_some in SML file "examples/fail_const.ML"
   266 
   267 
   267 text {* \noindent will fail. *}
   268 text {* \noindent will fail. *}
   268 
   269 
   269 subsection {* Theorem selection *}
   270 subsection {* Theorem selection *}
   280   defining equations (the additional stuff displayed
   281   defining equations (the additional stuff displayed
   281   shall not bother us for the moment).
   282   shall not bother us for the moment).
   282 
   283 
   283   The typical @{text HOL} tools are already set up in a way that
   284   The typical @{text HOL} tools are already set up in a way that
   284   function definitions introduced by @{text "\<DEFINITION>"},
   285   function definitions introduced by @{text "\<DEFINITION>"},
   285   @{text "\<FUN>"},
   286   @{text "\<PRIMREC>"}, @{text "\<FUN>"},
   286   @{text "\<FUNCTION>"}, @{text "\<PRIMREC>"},
   287   @{text "\<FUNCTION>"}, @{text "\<CONSTDEFS>"},
   287   @{text "\<RECDEF>"} are implicitly propagated
   288   @{text "\<RECDEF>"} are implicitly propagated
   288   to this defining equation table. Specific theorems may be
   289   to this defining equation table. Specific theorems may be
   289   selected using an attribute: \emph{code func}. As example,
   290   selected using an attribute: \emph{code func}. As example,
   290   a weight selector function:
   291   a weight selector function:
   291 *}
   292 *}
   292 
   293 
   293 consts
       
   294   pick :: "(nat \<times> 'a) list \<Rightarrow> nat \<Rightarrow> 'a"
       
   295 
       
   296 primrec
   294 primrec
       
   295   pick :: "(nat \<times> 'a) list \<Rightarrow> nat \<Rightarrow> 'a" where
   297   "pick (x#xs) n = (let (k, v) = x in
   296   "pick (x#xs) n = (let (k, v) = x in
   298     if n < k then v else pick xs (n - k))"
   297     if n < k then v else pick xs (n - k))"
   299 
   298 
   300 text {*
   299 text {*
   301   \noindent We want to eliminate the explicit destruction
   300   \noindent We want to eliminate the explicit destruction
   363 *}
   362 *}
   364 
   363 
   365 class null = type +
   364 class null = type +
   366   fixes null :: 'a
   365   fixes null :: 'a
   367 
   366 
   368 fun
   367 primrec
   369   head :: "'a\<Colon>null list \<Rightarrow> 'a"
   368   head :: "'a\<Colon>null list \<Rightarrow> 'a" where
   370 where
       
   371   "head [] = null"
   369   "head [] = null"
   372   | "head (x#xs) = x"
   370   | "head (x#xs) = x"
   373 
   371 
   374 text {*
   372 text {*
   375  \noindent  We provide some instances for our @{text null}:
   373  \noindent  We provide some instances for our @{text null}:
   584 text {*
   582 text {*
   585   Surely you have already noticed how equality is treated
   583   Surely you have already noticed how equality is treated
   586   by the code generator:
   584   by the code generator:
   587 *}
   585 *}
   588 
   586 
   589 fun
   587 primrec
   590   collect_duplicates :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
   588   collect_duplicates :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
   591     "collect_duplicates xs ys [] = xs"
   589     "collect_duplicates xs ys [] = xs"
   592   | "collect_duplicates xs ys (z#zs) = (if z \<in> set xs
   590   | "collect_duplicates xs ys (z#zs) = (if z \<in> set xs
   593       then if z \<in> set ys
   591       then if z \<in> set ys
   594         then collect_duplicates xs ys zs
   592         then collect_duplicates xs ys zs
   634 (*<*)
   632 (*<*)
   635 hide (open) "class" eq
   633 hide (open) "class" eq
   636 hide (open) const "op ="
   634 hide (open) const "op ="
   637 setup {* Sign.parent_path *}
   635 setup {* Sign.parent_path *}
   638 (*>*)
   636 (*>*)
   639 instance * :: (ord, ord) ord
   637 instantiation * :: (ord, ord) ord
   640   less_prod_def:
   638 begin
   641     "p1 < p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in
   639 
   642     x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)"
   640 definition
   643   less_eq_prod_def:
   641   [code func del]: "p1 < p2 \<longleftrightarrow> (let (x1, y1) = p1; (x2, y2) = p2 in
   644     "p1 \<le> p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in
   642     x1 < x2 \<or> (x1 = x2 \<and> y1 < y2))"
   645     x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" ..
   643 
   646 
   644 definition
   647 lemmas [code func del] = less_prod_def less_eq_prod_def
   645   [code func del]: "p1 \<le> p2 \<longleftrightarrow> (let (x1, y1) = p1; (x2, y2) = p2 in
       
   646     x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2))"
       
   647 
       
   648 instance ..
       
   649 
       
   650 end
   648 
   651 
   649 lemma ord_prod [code func(*<*), code func del(*>*)]:
   652 lemma ord_prod [code func(*<*), code func del(*>*)]:
   650   "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)"
   653   "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)"
   651   "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)"
   654   "(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)"
   652   unfolding less_prod_def less_eq_prod_def by simp_all
   655   unfolding less_prod_def less_eq_prod_def by simp_all
   807 text {*
   810 text {*
   808   Consider the following function and its corresponding
   811   Consider the following function and its corresponding
   809   SML code:
   812   SML code:
   810 *}
   813 *}
   811 
   814 
   812 fun
   815 primrec
   813   in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
   816   in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
   814   "in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
   817   "in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
   815 (*<*)
   818 (*<*)
   816 code_type %tt bool
   819 code_type %tt bool
   817   (SML)
   820   (SML)