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 |
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 |
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 |