| 76032 |      1 | (*  Author:     Tobias Nipkow
 | 
| 76031 |      2 |     Copyright   2000 TUM
 | 
|  |      3 | *)
 | 
|  |      4 | 
 | 
|  |      5 | section \<open>Fixed Length Lists\<close>
 | 
|  |      6 | 
 | 
|  |      7 | theory NList
 | 
|  |      8 | imports Main
 | 
|  |      9 | begin
 | 
|  |     10 | 
 | 
|  |     11 | definition nlists :: "nat \<Rightarrow> 'a set \<Rightarrow> 'a list set"
 | 
|  |     12 |   where "nlists n A = {xs. size xs = n \<and> set xs \<subseteq> A}"
 | 
|  |     13 | 
 | 
|  |     14 | lemma nlistsI: "\<lbrakk> size xs = n; set xs \<subseteq> A \<rbrakk> \<Longrightarrow> xs \<in> nlists n A"
 | 
|  |     15 |   by (simp add: nlists_def)
 | 
|  |     16 | 
 | 
|  |     17 | text \<open>These [simp] attributes are double-edged.
 | 
|  |     18 |  Many proofs in Jinja rely on it but they can degrade performance.\<close>
 | 
|  |     19 | 
 | 
|  |     20 | lemma nlistsE_length [simp]: "xs \<in> nlists n A \<Longrightarrow> size xs = n"
 | 
|  |     21 |   by (simp add: nlists_def)
 | 
|  |     22 | 
 | 
| 78922 |     23 | lemma in_nlists_UNIV: "xs \<in> nlists k UNIV \<longleftrightarrow> length xs = k"
 | 
|  |     24 | unfolding nlists_def by(auto)
 | 
|  |     25 | 
 | 
| 76031 |     26 | lemma less_lengthI: "\<lbrakk> xs \<in> nlists n A; p < n \<rbrakk> \<Longrightarrow> p < size xs"
 | 
|  |     27 | by (simp)
 | 
|  |     28 | 
 | 
|  |     29 | lemma nlistsE_set[simp]: "xs \<in> nlists n A \<Longrightarrow> set xs \<subseteq> A"
 | 
|  |     30 | unfolding nlists_def by (simp)
 | 
|  |     31 | 
 | 
|  |     32 | lemma nlists_mono:
 | 
|  |     33 | assumes "A \<subseteq> B" shows "nlists n A \<subseteq> nlists n B"
 | 
|  |     34 | proof
 | 
|  |     35 |   fix xs assume "xs \<in> nlists n A"
 | 
|  |     36 |   then obtain size: "size xs = n" and inA: "set xs \<subseteq> A" by (simp)
 | 
|  |     37 |   with assms have "set xs \<subseteq> B" by simp
 | 
|  |     38 |   with size show "xs \<in> nlists n B" by(clarsimp intro!: nlistsI)
 | 
|  |     39 | qed
 | 
|  |     40 | 
 | 
| 78835 |     41 | lemma nlists_singleton: "nlists n {a} = {replicate n a}"
 | 
|  |     42 | unfolding nlists_def by(auto simp: replicate_length_same dest!: subset_singletonD)
 | 
|  |     43 | 
 | 
| 76031 |     44 | lemma nlists_n_0 [simp]: "nlists 0 A = {[]}"
 | 
|  |     45 | unfolding nlists_def by (auto)
 | 
|  |     46 | 
 | 
|  |     47 | lemma in_nlists_Suc_iff: "(xs \<in> nlists (Suc n) A) = (\<exists>y\<in>A. \<exists>ys \<in> nlists n A. xs = y#ys)"
 | 
|  |     48 | unfolding nlists_def by (cases "xs") auto
 | 
|  |     49 | 
 | 
|  |     50 | lemma Cons_in_nlists_Suc [iff]: "(x#xs \<in> nlists (Suc n) A) \<longleftrightarrow> (x\<in>A \<and> xs \<in> nlists n A)"
 | 
|  |     51 | unfolding nlists_def by (auto)
 | 
|  |     52 | 
 | 
| 78834 |     53 | lemma nlists_Suc: "nlists (Suc n) A = (\<Union>a\<in>A. (#) a ` nlists n A)"
 | 
|  |     54 | by(auto simp: set_eq_iff image_iff in_nlists_Suc_iff)
 | 
|  |     55 | 
 | 
| 76031 |     56 | lemma nlists_not_empty: "A\<noteq>{} \<Longrightarrow> \<exists>xs. xs \<in> nlists n A"
 | 
|  |     57 | by (induct "n") (auto simp: in_nlists_Suc_iff)
 | 
|  |     58 | 
 | 
|  |     59 | lemma nlistsE_nth_in: "\<lbrakk> xs \<in> nlists n A; i < n \<rbrakk> \<Longrightarrow> xs!i \<in> A"
 | 
|  |     60 | unfolding nlists_def by (auto)
 | 
|  |     61 | 
 | 
|  |     62 | lemma nlists_Cons_Suc [elim!]:
 | 
|  |     63 |   "l#xs \<in> nlists n A \<Longrightarrow> (\<And>n'. n = Suc n' \<Longrightarrow> l \<in> A \<Longrightarrow> xs \<in> nlists n' A \<Longrightarrow> P) \<Longrightarrow> P"
 | 
|  |     64 | unfolding nlists_def by (auto)
 | 
|  |     65 | 
 | 
|  |     66 | lemma nlists_appendE [elim!]:
 | 
|  |     67 |   "a@b \<in> nlists n A \<Longrightarrow> (\<And>n1 n2. n=n1+n2 \<Longrightarrow> a \<in> nlists n1 A \<Longrightarrow> b \<in> nlists n2 A \<Longrightarrow> P) \<Longrightarrow> P"
 | 
|  |     68 | proof -
 | 
|  |     69 |   have "\<And>n. a@b \<in> nlists n A \<Longrightarrow> \<exists>n1 n2. n=n1+n2 \<and> a \<in> nlists n1 A \<and> b \<in> nlists n2 A"
 | 
|  |     70 |     (is "\<And>n. ?list a n \<Longrightarrow> \<exists>n1 n2. ?P a n n1 n2")
 | 
|  |     71 |   proof (induct a)
 | 
|  |     72 |     fix n assume "?list [] n"
 | 
|  |     73 |     hence "?P [] n 0 n" by simp
 | 
|  |     74 |     thus "\<exists>n1 n2. ?P [] n n1 n2" by fast
 | 
|  |     75 |   next
 | 
|  |     76 |     fix n l ls
 | 
|  |     77 |     assume "?list (l#ls) n"
 | 
|  |     78 |     then obtain n' where n: "n = Suc n'" "l \<in> A" and n': "ls@b \<in> nlists n' A" by fastforce
 | 
|  |     79 |     assume "\<And>n. ls @ b \<in> nlists n A \<Longrightarrow> \<exists>n1 n2. n = n1 + n2 \<and> ls \<in> nlists n1 A \<and> b \<in> nlists n2 A"
 | 
|  |     80 |     from this and n' have "\<exists>n1 n2. n' = n1 + n2 \<and> ls \<in> nlists n1 A \<and> b \<in> nlists n2 A" .
 | 
|  |     81 |     then obtain n1 n2 where "n' = n1 + n2" "ls \<in> nlists n1 A" "b \<in> nlists n2 A" by fast
 | 
|  |     82 |     with n have "?P (l#ls) n (n1+1) n2" by simp
 | 
|  |     83 |     thus "\<exists>n1 n2. ?P (l#ls) n n1 n2" by fastforce
 | 
|  |     84 |   qed
 | 
|  |     85 |   moreover assume "a@b \<in> nlists n A" "\<And>n1 n2. n=n1+n2 \<Longrightarrow> a \<in> nlists n1 A \<Longrightarrow> b \<in> nlists n2 A \<Longrightarrow> P"
 | 
|  |     86 |   ultimately show ?thesis by blast
 | 
|  |     87 | qed
 | 
|  |     88 | 
 | 
|  |     89 | 
 | 
|  |     90 | lemma nlists_update_in_list [simp, intro!]:
 | 
|  |     91 |   "\<lbrakk> xs \<in> nlists n A; x\<in>A \<rbrakk> \<Longrightarrow> xs[i := x] \<in> nlists n A"
 | 
|  |     92 |   by (metis length_list_update nlistsE_length nlistsE_set nlistsI set_update_subsetI)
 | 
|  |     93 | 
 | 
|  |     94 | lemma nlists_appendI [intro?]:
 | 
|  |     95 |   "\<lbrakk> a \<in> nlists n A; b \<in> nlists m A \<rbrakk> \<Longrightarrow> a @ b \<in> nlists (n+m) A"
 | 
|  |     96 | unfolding nlists_def by (auto)
 | 
|  |     97 | 
 | 
|  |     98 | lemma nlists_append:
 | 
|  |     99 |   "xs @ ys \<in> nlists k A \<longleftrightarrow>
 | 
|  |    100 |    k = length(xs @ ys) \<and> xs \<in> nlists (length xs) A \<and> ys \<in> nlists (length ys) A"
 | 
|  |    101 | unfolding nlists_def by (auto)
 | 
|  |    102 | 
 | 
|  |    103 | lemma nlists_map [simp]: "(map f xs \<in> nlists (size xs) A) = (f ` set xs \<subseteq> A)"
 | 
|  |    104 | unfolding nlists_def by (auto)
 | 
|  |    105 | 
 | 
|  |    106 | lemma nlists_replicateI [intro]: "x \<in> A \<Longrightarrow> replicate n x \<in> nlists n A"
 | 
|  |    107 |  by (induct n) auto
 | 
|  |    108 | 
 | 
| 78835 |    109 | text \<open>Link to an executable version on lists in List.\<close>
 | 
|  |    110 | lemma nlists_set[code]: "nlists n (set xs) = set(List.n_lists n xs)"
 | 
|  |    111 | by (metis nlists_def set_n_lists)
 | 
| 76031 |    112 | 
 | 
|  |    113 | end
 |