| author | aspinall |
| Sat, 17 Feb 2007 17:18:47 +0100 | |
| changeset 22334 | 4c96d3370186 |
| parent 22179 | 1a3575de2afc |
| child 22384 | 33a46e6c7f04 |
| permissions | -rw-r--r-- |
| 22068 | 1 |
(* ID: $Id$ |
| 20436 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
*) |
|
4 |
||
5 |
header {* Collection classes as examples for code generation *}
|
|
6 |
||
7 |
theory CodeCollections |
|
| 21460 | 8 |
imports Main Product_ord List_lexord |
| 20436 | 9 |
begin |
10 |
||
| 21460 | 11 |
fun |
12 |
abs_sorted :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
|
|
13 |
"abs_sorted cmp [] \<longleftrightarrow> True" |
|
14 |
"abs_sorted cmp [x] \<longleftrightarrow> True" |
|
15 |
"abs_sorted cmp (x#y#xs) \<longleftrightarrow> cmp x y \<and> abs_sorted cmp (y#xs)" |
|
| 20436 | 16 |
|
| 21460 | 17 |
abbreviation (in ord) |
18 |
"sorted \<equiv> abs_sorted less_eq" |
|
| 20436 | 19 |
|
20 |
abbreviation |
|
| 21460 | 21 |
"sorted \<equiv> abs_sorted less_eq" |
| 20436 | 22 |
|
| 22068 | 23 |
lemma (in order) sorted_weakening: |
| 20436 | 24 |
assumes "sorted (x # xs)" |
25 |
shows "sorted xs" |
|
26 |
using prems proof (induct xs) |
|
27 |
case Nil show ?case by simp |
|
28 |
next |
|
29 |
case (Cons x' xs) |
|
| 21460 | 30 |
from this have "sorted (x # x' # xs)" by auto |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20453
diff
changeset
|
31 |
then show "sorted (x' # xs)" |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20453
diff
changeset
|
32 |
by auto |
| 20436 | 33 |
qed |
34 |
||
| 21460 | 35 |
instance unit :: order |
36 |
"u \<le> v \<equiv> True" |
|
37 |
"u < v \<equiv> False" |
|
| 21924 | 38 |
by default (simp_all add: less_eq_unit_def less_unit_def) |
| 20436 | 39 |
|
| 21460 | 40 |
fun le_option' :: "'a\<Colon>order option \<Rightarrow> 'a option \<Rightarrow> bool" |
41 |
where "le_option' None y \<longleftrightarrow> True" |
|
42 |
| "le_option' (Some x) None \<longleftrightarrow> False" |
|
43 |
| "le_option' (Some x) (Some y) \<longleftrightarrow> x \<le> y" |
|
| 20436 | 44 |
|
| 21460 | 45 |
instance option :: (order) order |
46 |
"x \<le> y \<equiv> le_option' x y" |
|
47 |
"x < y \<equiv> x \<le> y \<and> x \<noteq> y" |
|
| 21924 | 48 |
proof (default, unfold less_eq_option_def less_option_def) |
| 20436 | 49 |
fix x |
50 |
show "le_option' x x" by (cases x) simp_all |
|
51 |
next |
|
52 |
fix x y z |
|
53 |
assume "le_option' x y" "le_option' y z" |
|
54 |
then show "le_option' x z" |
|
55 |
by (cases x, simp_all, cases y, simp_all, cases z, simp_all) |
|
56 |
next |
|
57 |
fix x y |
|
58 |
assume "le_option' x y" "le_option' y x" |
|
59 |
then show "x = y" |
|
60 |
by (cases x, simp_all, cases y, simp_all, cases y, simp_all) |
|
61 |
next |
|
| 21460 | 62 |
fix x y |
63 |
show "le_option' x y \<and> x \<noteq> y \<longleftrightarrow> le_option' x y \<and> x \<noteq> y" .. |
|
| 20436 | 64 |
qed |
65 |
||
66 |
lemma [simp, code]: |
|
| 21460 | 67 |
"None \<le> y \<longleftrightarrow> True" |
68 |
"Some x \<le> None \<longleftrightarrow> False" |
|
69 |
"Some v \<le> Some w \<longleftrightarrow> v \<le> w" |
|
| 21924 | 70 |
unfolding less_eq_option_def less_option_def le_option'.simps by rule+ |
| 20436 | 71 |
|
| 21460 | 72 |
lemma forall_all [simp]: |
73 |
"list_all P xs \<longleftrightarrow> (\<forall>x\<in>set xs. P x)" |
|
74 |
by (induct xs) auto |
|
| 20436 | 75 |
|
| 21460 | 76 |
lemma exists_ex [simp]: |
77 |
"list_ex P xs \<longleftrightarrow> (\<exists>x\<in>set xs. P x)" |
|
78 |
by (induct xs) auto |
|
| 20436 | 79 |
|
| 22179 | 80 |
class finite = |
| 21460 | 81 |
fixes fin :: "'a list" |
82 |
assumes member_fin: "x \<in> set fin" |
|
83 |
begin |
|
| 20436 | 84 |
|
| 21460 | 85 |
lemma set_enum_UNIV: |
86 |
"set fin = UNIV" |
|
87 |
using member_fin by auto |
|
| 20436 | 88 |
|
| 21460 | 89 |
lemma all_forall [code func, code inline]: |
90 |
"(\<forall>x. P x) \<longleftrightarrow> list_all P fin" |
|
91 |
using set_enum_UNIV by simp_all |
|
| 20436 | 92 |
|
| 21460 | 93 |
lemma ex_exists [code func, code inline]: |
94 |
"(\<exists>x. P x) \<longleftrightarrow> list_ex P fin" |
|
95 |
using set_enum_UNIV by simp_all |
|
| 20436 | 96 |
|
| 21460 | 97 |
end |
98 |
||
| 22179 | 99 |
instance bool :: finite |
| 21924 | 100 |
"fin == [False, True]" |
| 21460 | 101 |
by default (simp_all add: fin_bool_def) |
| 20436 | 102 |
|
| 22179 | 103 |
instance unit :: finite |
| 21924 | 104 |
"fin == [()]" |
| 21460 | 105 |
by default (simp_all add: fin_unit_def) |
| 20436 | 106 |
|
| 21460 | 107 |
fun |
| 20436 | 108 |
product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a * 'b) list"
|
| 21460 | 109 |
where |
| 20436 | 110 |
"product [] ys = []" |
111 |
"product (x#xs) ys = map (Pair x) ys @ product xs ys" |
|
112 |
||
113 |
lemma product_all: |
|
114 |
assumes "x \<in> set xs" "y \<in> set ys" |
|
115 |
shows "(x, y) \<in> set (product xs ys)" |
|
116 |
using prems proof (induct xs) |
|
117 |
case Nil |
|
118 |
then have False by auto |
|
119 |
then show ?case .. |
|
120 |
next |
|
121 |
case (Cons z xs) |
|
122 |
then show ?case |
|
123 |
proof (cases "x = z") |
|
124 |
case True |
|
125 |
with Cons have "(x, y) \<in> set (product (x # xs) ys)" by simp |
|
126 |
with True show ?thesis by simp |
|
127 |
next |
|
128 |
case False |
|
129 |
with Cons have "x \<in> set xs" by auto |
|
130 |
with Cons have "(x, y) \<in> set (product xs ys)" by auto |
|
131 |
then show "(x, y) \<in> set (product (z#xs) ys)" by auto |
|
132 |
qed |
|
133 |
qed |
|
134 |
||
| 22179 | 135 |
instance * :: (finite, finite) finite |
| 21924 | 136 |
"fin == product fin fin" |
| 20436 | 137 |
apply default |
| 21460 | 138 |
apply (simp_all add: "fin_*_def") |
| 20436 | 139 |
apply (unfold split_paired_all) |
140 |
apply (rule product_all) |
|
| 22179 | 141 |
apply (rule finite_class.member_fin)+ |
| 21460 | 142 |
done |
| 20436 | 143 |
|
| 22179 | 144 |
instance option :: (finite) finite |
| 21924 | 145 |
"fin == None # map Some fin" |
| 21460 | 146 |
proof (default, unfold fin_option_def) |
| 22179 | 147 |
fix x :: "'a::finite option" |
| 21460 | 148 |
show "x \<in> set (None # map Some fin)" |
| 20436 | 149 |
proof (cases x) |
150 |
case None then show ?thesis by auto |
|
151 |
next |
|
| 22179 | 152 |
case (Some x) then show ?thesis by (auto intro: finite_class.member_fin) |
| 20436 | 153 |
qed |
154 |
qed |
|
155 |
||
156 |
consts |
|
157 |
get_first :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a option"
|
|
158 |
||
159 |
primrec |
|
160 |
"get_first p [] = None" |
|
161 |
"get_first p (x#xs) = (if p x then Some x else get_first p xs)" |
|
162 |
||
163 |
consts |
|
164 |
get_index :: "('a \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> nat option"
|
|
165 |
||
166 |
primrec |
|
167 |
"get_index p n [] = None" |
|
168 |
"get_index p n (x#xs) = (if p x then Some n else get_index p (Suc n) xs)" |
|
169 |
||
| 21460 | 170 |
(*definition |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21319
diff
changeset
|
171 |
between :: "'a::enum \<Rightarrow> 'a \<Rightarrow> 'a option" where |
| 20436 | 172 |
"between x y = get_first (\<lambda>z. x << z & z << y) enum" |
173 |
||
174 |
definition |
|
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21319
diff
changeset
|
175 |
index :: "'a::enum \<Rightarrow> nat" where |
| 20436 | 176 |
"index x = the (get_index (\<lambda>y. y = x) 0 enum)" |
177 |
||
178 |
definition |
|
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21319
diff
changeset
|
179 |
add :: "'a::enum \<Rightarrow> 'a \<Rightarrow> 'a" where |
| 20436 | 180 |
"add x y = |
181 |
(let |
|
182 |
enm = enum |
|
183 |
in enm ! ((index x + index y) mod length enm))" |
|
184 |
||
185 |
consts |
|
186 |
sum :: "'a::{enum, infimum} list \<Rightarrow> 'a"
|
|
187 |
||
188 |
primrec |
|
189 |
"sum [] = inf" |
|
| 21460 | 190 |
"sum (x#xs) = add x (sum xs)"*) |
| 20436 | 191 |
|
| 21460 | 192 |
(*definition "test1 = sum [None, Some True, None, Some False]"*) |
193 |
(*definition "test2 = (inf :: nat \<times> unit)"*) |
|
194 |
definition "test3 \<longleftrightarrow> (\<exists>x \<Colon> bool option. case x of Some P \<Rightarrow> P | None \<Rightarrow> False)" |
|
195 |
||
| 22179 | 196 |
code_gen test3 (SML #) (OCaml -) (Haskell -) |
| 20436 | 197 |
|
198 |
end |