4 Copyright 1994 TU Muenchen |
4 Copyright 1994 TU Muenchen |
5 |
5 |
6 Specification of sorting |
6 Specification of sorting |
7 *) |
7 *) |
8 |
8 |
9 theory Sorting = Main: |
9 theory Sorting = Main + Multiset: |
10 |
10 |
11 consts |
11 consts |
12 sorted1:: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" |
12 sorted1:: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" |
13 sorted :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" |
13 sorted :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" |
14 multiset :: "'a list => ('a => nat)" |
|
15 |
14 |
16 primrec |
15 primrec |
17 "sorted1 le [] = True" |
16 "sorted1 le [] = True" |
18 "sorted1 le (x#xs) = ((case xs of [] => True | y#ys => le x y) & |
17 "sorted1 le (x#xs) = ((case xs of [] => True | y#ys => le x y) & |
19 sorted1 le xs)" |
18 sorted1 le xs)" |
20 |
19 |
21 primrec |
20 primrec |
22 "sorted le [] = True" |
21 "sorted le [] = True" |
23 "sorted le (x#xs) = ((!y:set xs. le x y) & sorted le xs)" |
22 "sorted le (x#xs) = ((!y:set xs. le x y) & sorted le xs)" |
24 |
23 |
25 primrec |
|
26 "multiset [] y = 0" |
|
27 "multiset (x#xs) y = (if x=y then Suc(multiset xs y) else multiset xs y)" |
|
28 |
24 |
29 constdefs |
25 constdefs |
30 total :: "('a \<Rightarrow> 'a \<Rightarrow> bool) => bool" |
26 total :: "('a \<Rightarrow> 'a \<Rightarrow> bool) => bool" |
31 "total r == (ALL x y. r x y | r y x)" |
27 "total r == (ALL x y. r x y | r y x)" |
32 |
28 |
33 transf :: "('a \<Rightarrow> 'a \<Rightarrow> bool) => bool" |
29 transf :: "('a \<Rightarrow> 'a \<Rightarrow> bool) => bool" |
34 "transf f == (ALL x y z. f x y & f y z --> f x z)" |
30 "transf f == (ALL x y z. f x y & f y z --> f x z)" |
35 |
31 |
36 (* Some general lemmas *) |
|
37 |
32 |
38 lemma multiset_append[simp]: |
|
39 "multiset (xs@ys) x = multiset xs x + multiset ys x" |
|
40 by (induct xs, auto) |
|
41 |
|
42 lemma multiset_compl_add[simp]: |
|
43 "multiset [x:xs. ~p(x)] z + multiset [x:xs. p(x)] z = multiset xs z"; |
|
44 by (induct xs, auto) |
|
45 |
|
46 lemma set_via_multiset: "set xs = {x. multiset xs x ~= 0}"; |
|
47 by (induct xs, auto) |
|
48 |
33 |
49 (* Equivalence of two definitions of `sorted' *) |
34 (* Equivalence of two definitions of `sorted' *) |
50 |
35 |
51 lemma sorted1_is_sorted: |
36 lemma sorted1_is_sorted: |
52 "transf(le) ==> sorted1 le xs = sorted le xs"; |
37 "transf(le) ==> sorted1 le xs = sorted le xs"; |