9 hide_const sorted |
9 hide_const sorted |
10 |
10 |
11 text \<open>Is a list sorted without duplicates, i.e., wrt @{text"<"}? |
11 text \<open>Is a list sorted without duplicates, i.e., wrt @{text"<"}? |
12 Could go into theory List under a name like @{term sorted_less}.\<close> |
12 Could go into theory List under a name like @{term sorted_less}.\<close> |
13 |
13 |
14 fun sorted :: "'a::linorder list \<Rightarrow> bool" where |
14 abbreviation sorted :: "'a::linorder list \<Rightarrow> bool" where |
15 "sorted [] = True" | |
15 "sorted \<equiv> sorted_wrt (op <)" |
16 "sorted [x] = True" | |
|
17 "sorted (x#y#zs) = (x < y \<and> sorted(y#zs))" |
|
18 |
|
19 lemma sorted_Cons_iff: |
|
20 "sorted(x # xs) = (sorted xs \<and> (\<forall>y \<in> set xs. x < y))" |
|
21 by(induction xs rule: sorted.induct) auto |
|
22 |
|
23 lemma sorted_snoc_iff: |
|
24 "sorted(xs @ [x]) = (sorted xs \<and> (\<forall>y \<in> set xs. y < x))" |
|
25 by(induction xs rule: sorted.induct) auto |
|
26 |
16 |
27 lemma sorted_cons: "sorted (x#xs) \<Longrightarrow> sorted xs" |
17 lemma sorted_cons: "sorted (x#xs) \<Longrightarrow> sorted xs" |
28 by(simp add: sorted_Cons_iff) |
18 by(simp add: sorted_wrt_Cons) |
29 |
19 |
30 lemma sorted_cons': "ASSUMPTION (sorted (x#xs)) \<Longrightarrow> sorted xs" |
20 lemma sorted_cons': "ASSUMPTION (sorted (x#xs)) \<Longrightarrow> sorted xs" |
31 by(rule ASSUMPTION_D [THEN sorted_cons]) |
21 by(rule ASSUMPTION_D [THEN sorted_cons]) |
32 |
22 |
33 lemma sorted_snoc: "sorted (xs @ [y]) \<Longrightarrow> sorted xs" |
23 lemma sorted_snoc: "sorted (xs @ [y]) \<Longrightarrow> sorted xs" |
34 by(simp add: sorted_snoc_iff) |
24 by(simp add: sorted_wrt_append) |
35 |
25 |
36 lemma sorted_snoc': "ASSUMPTION (sorted (xs @ [y])) \<Longrightarrow> sorted xs" |
26 lemma sorted_snoc': "ASSUMPTION (sorted (xs @ [y])) \<Longrightarrow> sorted xs" |
37 by(rule ASSUMPTION_D [THEN sorted_snoc]) |
27 by(rule ASSUMPTION_D [THEN sorted_snoc]) |
38 |
28 |
39 lemma sorted_mid_iff: |
29 lemma sorted_mid_iff: |
40 "sorted(xs @ y # ys) = (sorted(xs @ [y]) \<and> sorted(y # ys))" |
30 "sorted(xs @ y # ys) = (sorted(xs @ [y]) \<and> sorted(y # ys))" |
41 by(induction xs rule: sorted.induct) auto |
31 by(fastforce simp add: sorted_wrt_Cons sorted_wrt_append) |
42 |
32 |
43 lemma sorted_mid_iff2: |
33 lemma sorted_mid_iff2: |
44 "sorted(x # xs @ y # ys) = |
34 "sorted(x # xs @ y # ys) = |
45 (sorted(x # xs) \<and> x < y \<and> sorted(xs @ [y]) \<and> sorted(y # ys))" |
35 (sorted(x # xs) \<and> x < y \<and> sorted(xs @ [y]) \<and> sorted(y # ys))" |
46 by(induction xs rule: sorted.induct) auto |
36 by(fastforce simp add: sorted_wrt_Cons sorted_wrt_append) |
47 |
37 |
48 lemma sorted_mid_iff': "NO_MATCH [] ys \<Longrightarrow> |
38 lemma sorted_mid_iff': "NO_MATCH [] ys \<Longrightarrow> |
49 sorted(xs @ y # ys) = (sorted(xs @ [y]) \<and> sorted(y # ys))" |
39 sorted(xs @ y # ys) = (sorted(xs @ [y]) \<and> sorted(y # ys))" |
50 by(rule sorted_mid_iff) |
40 by(rule sorted_mid_iff) |
51 |
41 |
53 |
43 |
54 text\<open>Splay trees need two additional @{const sorted} lemmas:\<close> |
44 text\<open>Splay trees need two additional @{const sorted} lemmas:\<close> |
55 |
45 |
56 lemma sorted_snoc_le: |
46 lemma sorted_snoc_le: |
57 "ASSUMPTION(sorted(xs @ [x])) \<Longrightarrow> x \<le> y \<Longrightarrow> sorted (xs @ [y])" |
47 "ASSUMPTION(sorted(xs @ [x])) \<Longrightarrow> x \<le> y \<Longrightarrow> sorted (xs @ [y])" |
58 by (auto simp add: Sorted_Less.sorted_snoc_iff ASSUMPTION_def) |
48 by (auto simp add: sorted_wrt_append ASSUMPTION_def) |
59 |
49 |
60 lemma sorted_Cons_le: |
50 lemma sorted_Cons_le: |
61 "ASSUMPTION(sorted(x # xs)) \<Longrightarrow> y \<le> x \<Longrightarrow> sorted (y # xs)" |
51 "ASSUMPTION(sorted(x # xs)) \<Longrightarrow> y \<le> x \<Longrightarrow> sorted (y # xs)" |
62 by (auto simp add: Sorted_Less.sorted_Cons_iff ASSUMPTION_def) |
52 by (auto simp add: sorted_wrt_Cons ASSUMPTION_def) |
63 |
53 |
64 end |
54 end |