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