src/HOL/Matrix_LP/SparseMatrix.thy
author wenzelm
Fri Apr 13 14:00:26 2012 +0200 (2012-04-13)
changeset 47455 26315a545e26
parent 47108 2a1953f0d20d
child 54892 64c2d4f8d981
permissions -rw-r--r--
updated headers;
     1 (*  Title:      HOL/Matrix_LP/SparseMatrix.thy
     2     Author:     Steven Obua
     3 *)
     4 
     5 theory SparseMatrix
     6 imports Matrix
     7 begin
     8 
     9 type_synonym 'a spvec = "(nat * 'a) list"
    10 type_synonym 'a spmat = "'a spvec spvec"
    11 
    12 definition sparse_row_vector :: "('a::ab_group_add) spvec \<Rightarrow> 'a matrix"
    13   where "sparse_row_vector arr = foldl (% m x. m + (singleton_matrix 0 (fst x) (snd x))) 0 arr"
    14 
    15 definition sparse_row_matrix :: "('a::ab_group_add) spmat \<Rightarrow> 'a matrix"
    16   where "sparse_row_matrix arr = foldl (% m r. m + (move_matrix (sparse_row_vector (snd r)) (int (fst r)) 0)) 0 arr"
    17 
    18 code_datatype sparse_row_vector sparse_row_matrix
    19 
    20 lemma sparse_row_vector_empty [simp]: "sparse_row_vector [] = 0"
    21   by (simp add: sparse_row_vector_def)
    22 
    23 lemma sparse_row_matrix_empty [simp]: "sparse_row_matrix [] = 0"
    24   by (simp add: sparse_row_matrix_def)
    25 
    26 lemmas [code] = sparse_row_vector_empty [symmetric]
    27 
    28 lemma foldl_distrstart: "! a x y. (f (g x y) a = g x (f y a)) \<Longrightarrow> (foldl f (g x y) l = g x (foldl f y l))"
    29   by (induct l arbitrary: x y, auto)
    30 
    31 lemma sparse_row_vector_cons[simp]:
    32   "sparse_row_vector (a # arr) = (singleton_matrix 0 (fst a) (snd a)) + (sparse_row_vector arr)"
    33   apply (induct arr)
    34   apply (auto simp add: sparse_row_vector_def)
    35   apply (simp add: foldl_distrstart [of "\<lambda>m x. m + singleton_matrix 0 (fst x) (snd x)" "\<lambda>x m. singleton_matrix 0 (fst x) (snd x) + m"])
    36   done
    37 
    38 lemma sparse_row_vector_append[simp]:
    39   "sparse_row_vector (a @ b) = (sparse_row_vector a) + (sparse_row_vector b)"
    40   by (induct a) auto
    41 
    42 lemma nrows_spvec[simp]: "nrows (sparse_row_vector x) <= (Suc 0)"
    43   apply (induct x)
    44   apply (simp_all add: add_nrows)
    45   done
    46 
    47 lemma sparse_row_matrix_cons: "sparse_row_matrix (a#arr) = ((move_matrix (sparse_row_vector (snd a)) (int (fst a)) 0)) + sparse_row_matrix arr"
    48   apply (induct arr)
    49   apply (auto simp add: sparse_row_matrix_def)
    50   apply (simp add: foldl_distrstart[of "\<lambda>m x. m + (move_matrix (sparse_row_vector (snd x)) (int (fst x)) 0)" 
    51     "% a m. (move_matrix (sparse_row_vector (snd a)) (int (fst a)) 0) + m"])
    52   done
    53 
    54 lemma sparse_row_matrix_append: "sparse_row_matrix (arr@brr) = (sparse_row_matrix arr) + (sparse_row_matrix brr)"
    55   apply (induct arr)
    56   apply (auto simp add: sparse_row_matrix_cons)
    57   done
    58 
    59 primrec sorted_spvec :: "'a spvec \<Rightarrow> bool"
    60 where
    61   "sorted_spvec [] = True"
    62 | sorted_spvec_step: "sorted_spvec (a#as) = (case as of [] \<Rightarrow> True | b#bs \<Rightarrow> ((fst a < fst b) & (sorted_spvec as)))" 
    63 
    64 primrec sorted_spmat :: "'a spmat \<Rightarrow> bool"
    65 where
    66   "sorted_spmat [] = True"
    67 | "sorted_spmat (a#as) = ((sorted_spvec (snd a)) & (sorted_spmat as))"
    68 
    69 declare sorted_spvec.simps [simp del]
    70 
    71 lemma sorted_spvec_empty[simp]: "sorted_spvec [] = True"
    72 by (simp add: sorted_spvec.simps)
    73 
    74 lemma sorted_spvec_cons1: "sorted_spvec (a#as) \<Longrightarrow> sorted_spvec as"
    75 apply (induct as)
    76 apply (auto simp add: sorted_spvec.simps)
    77 done
    78 
    79 lemma sorted_spvec_cons2: "sorted_spvec (a#b#t) \<Longrightarrow> sorted_spvec (a#t)"
    80 apply (induct t)
    81 apply (auto simp add: sorted_spvec.simps)
    82 done
    83 
    84 lemma sorted_spvec_cons3: "sorted_spvec(a#b#t) \<Longrightarrow> fst a < fst b"
    85 apply (auto simp add: sorted_spvec.simps)
    86 done
    87 
    88 lemma sorted_sparse_row_vector_zero[rule_format]: "m <= n \<Longrightarrow> sorted_spvec ((n,a)#arr) \<longrightarrow> Rep_matrix (sparse_row_vector arr) j m = 0"
    89 apply (induct arr)
    90 apply (auto)
    91 apply (frule sorted_spvec_cons2,simp)+
    92 apply (frule sorted_spvec_cons3, simp)
    93 done
    94 
    95 lemma sorted_sparse_row_matrix_zero[rule_format]: "m <= n \<Longrightarrow> sorted_spvec ((n,a)#arr) \<longrightarrow> Rep_matrix (sparse_row_matrix arr) m j = 0"
    96   apply (induct arr)
    97   apply (auto)
    98   apply (frule sorted_spvec_cons2, simp)
    99   apply (frule sorted_spvec_cons3, simp)
   100   apply (simp add: sparse_row_matrix_cons)
   101   done
   102 
   103 primrec minus_spvec :: "('a::ab_group_add) spvec \<Rightarrow> 'a spvec"
   104 where
   105   "minus_spvec [] = []"
   106 | "minus_spvec (a#as) = (fst a, -(snd a))#(minus_spvec as)"
   107 
   108 primrec abs_spvec :: "('a::lattice_ab_group_add_abs) spvec \<Rightarrow> 'a spvec"
   109 where
   110   "abs_spvec [] = []"
   111 | "abs_spvec (a#as) = (fst a, abs (snd a))#(abs_spvec as)"
   112 
   113 lemma sparse_row_vector_minus: 
   114   "sparse_row_vector (minus_spvec v) = - (sparse_row_vector v)"
   115   apply (induct v)
   116   apply (simp_all add: sparse_row_vector_cons)
   117   apply (simp add: Rep_matrix_inject[symmetric])
   118   apply (rule ext)+
   119   apply simp
   120   done
   121 
   122 instance matrix :: (lattice_ab_group_add_abs) lattice_ab_group_add_abs
   123 apply default
   124 unfolding abs_matrix_def .. (*FIXME move*)
   125 
   126 lemma sparse_row_vector_abs:
   127   "sorted_spvec (v :: 'a::lattice_ring spvec) \<Longrightarrow> sparse_row_vector (abs_spvec v) = abs (sparse_row_vector v)"
   128   apply (induct v)
   129   apply simp_all
   130   apply (frule_tac sorted_spvec_cons1, simp)
   131   apply (simp only: Rep_matrix_inject[symmetric])
   132   apply (rule ext)+
   133   apply auto
   134   apply (subgoal_tac "Rep_matrix (sparse_row_vector v) 0 a = 0")
   135   apply (simp)
   136   apply (rule sorted_sparse_row_vector_zero)
   137   apply auto
   138   done
   139 
   140 lemma sorted_spvec_minus_spvec:
   141   "sorted_spvec v \<Longrightarrow> sorted_spvec (minus_spvec v)"
   142   apply (induct v)
   143   apply (simp)
   144   apply (frule sorted_spvec_cons1, simp)
   145   apply (simp add: sorted_spvec.simps split:list.split_asm)
   146   done
   147 
   148 lemma sorted_spvec_abs_spvec:
   149   "sorted_spvec v \<Longrightarrow> sorted_spvec (abs_spvec v)"
   150   apply (induct v)
   151   apply (simp)
   152   apply (frule sorted_spvec_cons1, simp)
   153   apply (simp add: sorted_spvec.simps split:list.split_asm)
   154   done
   155   
   156 definition "smult_spvec y = map (% a. (fst a, y * snd a))"  
   157 
   158 lemma smult_spvec_empty[simp]: "smult_spvec y [] = []"
   159   by (simp add: smult_spvec_def)
   160 
   161 lemma smult_spvec_cons: "smult_spvec y (a#arr) = (fst a, y * (snd a)) # (smult_spvec y arr)"
   162   by (simp add: smult_spvec_def)
   163 
   164 fun addmult_spvec :: "('a::ring) \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec"
   165 where
   166   "addmult_spvec y arr [] = arr"
   167 | "addmult_spvec y [] brr = smult_spvec y brr"
   168 | "addmult_spvec y ((i,a)#arr) ((j,b)#brr) = (
   169     if i < j then ((i,a)#(addmult_spvec y arr ((j,b)#brr))) 
   170     else (if (j < i) then ((j, y * b)#(addmult_spvec y ((i,a)#arr) brr))
   171     else ((i, a + y*b)#(addmult_spvec y arr brr))))"
   172 (* Steven used termination "measure (% (y, a, b). length a + (length b))" *)
   173 
   174 lemma addmult_spvec_empty1[simp]: "addmult_spvec y [] a = smult_spvec y a"
   175   by (induct a) auto
   176 
   177 lemma addmult_spvec_empty2[simp]: "addmult_spvec y a [] = a"
   178   by (induct a) auto
   179 
   180 lemma sparse_row_vector_map: "(! x y. f (x+y) = (f x) + (f y)) \<Longrightarrow> (f::'a\<Rightarrow>('a::lattice_ring)) 0 = 0 \<Longrightarrow> 
   181   sparse_row_vector (map (% x. (fst x, f (snd x))) a) = apply_matrix f (sparse_row_vector a)"
   182   apply (induct a)
   183   apply (simp_all add: apply_matrix_add)
   184   done
   185 
   186 lemma sparse_row_vector_smult: "sparse_row_vector (smult_spvec y a) = scalar_mult y (sparse_row_vector a)"
   187   apply (induct a)
   188   apply (simp_all add: smult_spvec_cons scalar_mult_add)
   189   done
   190 
   191 lemma sparse_row_vector_addmult_spvec: "sparse_row_vector (addmult_spvec (y::'a::lattice_ring) a b) = 
   192   (sparse_row_vector a) + (scalar_mult y (sparse_row_vector b))"
   193   apply (induct y a b rule: addmult_spvec.induct)
   194   apply (simp add: scalar_mult_add smult_spvec_cons sparse_row_vector_smult singleton_matrix_add)+
   195   done
   196 
   197 lemma sorted_smult_spvec: "sorted_spvec a \<Longrightarrow> sorted_spvec (smult_spvec y a)"
   198   apply (auto simp add: smult_spvec_def)
   199   apply (induct a)
   200   apply (auto simp add: sorted_spvec.simps split:list.split_asm)
   201   done
   202 
   203 lemma sorted_spvec_addmult_spvec_helper: "\<lbrakk>sorted_spvec (addmult_spvec y ((a, b) # arr) brr); aa < a; sorted_spvec ((a, b) # arr); 
   204   sorted_spvec ((aa, ba) # brr)\<rbrakk> \<Longrightarrow> sorted_spvec ((aa, y * ba) # addmult_spvec y ((a, b) # arr) brr)"  
   205   apply (induct brr)
   206   apply (auto simp add: sorted_spvec.simps)
   207   done
   208 
   209 lemma sorted_spvec_addmult_spvec_helper2: 
   210  "\<lbrakk>sorted_spvec (addmult_spvec y arr ((aa, ba) # brr)); a < aa; sorted_spvec ((a, b) # arr); sorted_spvec ((aa, ba) # brr)\<rbrakk>
   211        \<Longrightarrow> sorted_spvec ((a, b) # addmult_spvec y arr ((aa, ba) # brr))"
   212   apply (induct arr)
   213   apply (auto simp add: smult_spvec_def sorted_spvec.simps)
   214   done
   215 
   216 lemma sorted_spvec_addmult_spvec_helper3[rule_format]:
   217   "sorted_spvec (addmult_spvec y arr brr) \<longrightarrow> sorted_spvec ((aa, b) # arr) \<longrightarrow> sorted_spvec ((aa, ba) # brr)
   218      \<longrightarrow> sorted_spvec ((aa, b + y * ba) # (addmult_spvec y arr brr))"
   219   apply (induct y arr brr rule: addmult_spvec.induct)
   220   apply (simp_all add: sorted_spvec.simps smult_spvec_def split:list.split)
   221   done
   222 
   223 lemma sorted_addmult_spvec: "sorted_spvec a \<Longrightarrow> sorted_spvec b \<Longrightarrow> sorted_spvec (addmult_spvec y a b)"
   224   apply (induct y a b rule: addmult_spvec.induct)
   225   apply (simp_all add: sorted_smult_spvec)
   226   apply (rule conjI, intro strip)
   227   apply (case_tac "~(i < j)")
   228   apply (simp_all)
   229   apply (frule_tac as=brr in sorted_spvec_cons1)
   230   apply (simp add: sorted_spvec_addmult_spvec_helper)
   231   apply (intro strip | rule conjI)+
   232   apply (frule_tac as=arr in sorted_spvec_cons1)
   233   apply (simp add: sorted_spvec_addmult_spvec_helper2)
   234   apply (intro strip)
   235   apply (frule_tac as=arr in sorted_spvec_cons1)
   236   apply (frule_tac as=brr in sorted_spvec_cons1)
   237   apply (simp)
   238   apply (simp_all add: sorted_spvec_addmult_spvec_helper3)
   239   done
   240 
   241 fun mult_spvec_spmat :: "('a::lattice_ring) spvec \<Rightarrow> 'a spvec \<Rightarrow> 'a spmat  \<Rightarrow> 'a spvec"
   242 where
   243 (* recdef mult_spvec_spmat "measure (% (c, arr, brr). (length arr) + (length brr))" *)
   244   "mult_spvec_spmat c [] brr = c"
   245 | "mult_spvec_spmat c arr [] = c"
   246 | "mult_spvec_spmat c ((i,a)#arr) ((j,b)#brr) = (
   247      if (i < j) then mult_spvec_spmat c arr ((j,b)#brr)
   248      else if (j < i) then mult_spvec_spmat c ((i,a)#arr) brr 
   249      else mult_spvec_spmat (addmult_spvec a c b) arr brr)"
   250 
   251 lemma sparse_row_mult_spvec_spmat[rule_format]: "sorted_spvec (a::('a::lattice_ring) spvec) \<longrightarrow> sorted_spvec B \<longrightarrow> 
   252   sparse_row_vector (mult_spvec_spmat c a B) = (sparse_row_vector c) + (sparse_row_vector a) * (sparse_row_matrix B)"
   253 proof -
   254   have comp_1: "!! a b. a < b \<Longrightarrow> Suc 0 <= nat ((int b)-(int a))" by arith
   255   have not_iff: "!! a b. a = b \<Longrightarrow> (~ a) = (~ b)" by simp
   256   have max_helper: "!! a b. ~ (a <= max (Suc a) b) \<Longrightarrow> False"
   257     by arith
   258   {
   259     fix a 
   260     fix v
   261     assume a:"a < nrows(sparse_row_vector v)"
   262     have b:"nrows(sparse_row_vector v) <= 1" by simp
   263     note dummy = less_le_trans[of a "nrows (sparse_row_vector v)" 1, OF a b]   
   264     then have "a = 0" by simp
   265   }
   266   note nrows_helper = this
   267   show ?thesis
   268     apply (induct c a B rule: mult_spvec_spmat.induct)
   269     apply simp+
   270     apply (rule conjI)
   271     apply (intro strip)
   272     apply (frule_tac as=brr in sorted_spvec_cons1)
   273     apply (simp add: algebra_simps sparse_row_matrix_cons)
   274     apply (simplesubst Rep_matrix_zero_imp_mult_zero) 
   275     apply (simp)
   276     apply (rule disjI2)
   277     apply (intro strip)
   278     apply (subst nrows)
   279     apply (rule  order_trans[of _ 1])
   280     apply (simp add: comp_1)+
   281     apply (subst Rep_matrix_zero_imp_mult_zero)
   282     apply (intro strip)
   283     apply (case_tac "k <= j")
   284     apply (rule_tac m1 = k and n1 = i and a1 = a in ssubst[OF sorted_sparse_row_vector_zero])
   285     apply (simp_all)
   286     apply (rule disjI2)
   287     apply (rule nrows)
   288     apply (rule order_trans[of _ 1])
   289     apply (simp_all add: comp_1)
   290     
   291     apply (intro strip | rule conjI)+
   292     apply (frule_tac as=arr in sorted_spvec_cons1)
   293     apply (simp add: algebra_simps)
   294     apply (subst Rep_matrix_zero_imp_mult_zero)
   295     apply (simp)
   296     apply (rule disjI2)
   297     apply (intro strip)
   298     apply (simp add: sparse_row_matrix_cons)
   299     apply (case_tac "i <= j")  
   300     apply (erule sorted_sparse_row_matrix_zero)  
   301     apply (simp_all)
   302     apply (intro strip)
   303     apply (case_tac "i=j")
   304     apply (simp_all)
   305     apply (frule_tac as=arr in sorted_spvec_cons1)
   306     apply (frule_tac as=brr in sorted_spvec_cons1)
   307     apply (simp add: sparse_row_matrix_cons algebra_simps sparse_row_vector_addmult_spvec)
   308     apply (rule_tac B1 = "sparse_row_matrix brr" in ssubst[OF Rep_matrix_zero_imp_mult_zero])
   309     apply (auto)
   310     apply (rule sorted_sparse_row_matrix_zero)
   311     apply (simp_all)
   312     apply (rule_tac A1 = "sparse_row_vector arr" in ssubst[OF Rep_matrix_zero_imp_mult_zero])
   313     apply (auto)
   314     apply (rule_tac m=k and n = j and a = a and arr=arr in sorted_sparse_row_vector_zero)
   315     apply (simp_all)
   316     apply (drule nrows_notzero)
   317     apply (drule nrows_helper)
   318     apply (arith)
   319     
   320     apply (subst Rep_matrix_inject[symmetric])
   321     apply (rule ext)+
   322     apply (simp)
   323     apply (subst Rep_matrix_mult)
   324     apply (rule_tac j1=j in ssubst[OF foldseq_almostzero])
   325     apply (simp_all)
   326     apply (intro strip, rule conjI)
   327     apply (intro strip)
   328     apply (drule_tac max_helper)
   329     apply (simp)
   330     apply (auto)
   331     apply (rule zero_imp_mult_zero)
   332     apply (rule disjI2)
   333     apply (rule nrows)
   334     apply (rule order_trans[of _ 1])
   335     apply (simp)
   336     apply (simp)
   337     done
   338 qed
   339 
   340 lemma sorted_mult_spvec_spmat[rule_format]: 
   341   "sorted_spvec (c::('a::lattice_ring) spvec) \<longrightarrow> sorted_spmat B \<longrightarrow> sorted_spvec (mult_spvec_spmat c a B)"
   342   apply (induct c a B rule: mult_spvec_spmat.induct)
   343   apply (simp_all add: sorted_addmult_spvec)
   344   done
   345 
   346 primrec mult_spmat :: "('a::lattice_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
   347 where
   348   "mult_spmat [] A = []"
   349 | "mult_spmat (a#as) A = (fst a, mult_spvec_spmat [] (snd a) A)#(mult_spmat as A)"
   350 
   351 lemma sparse_row_mult_spmat: 
   352   "sorted_spmat A \<Longrightarrow> sorted_spvec B \<Longrightarrow>
   353    sparse_row_matrix (mult_spmat A B) = (sparse_row_matrix A) * (sparse_row_matrix B)"
   354   apply (induct A)
   355   apply (auto simp add: sparse_row_matrix_cons sparse_row_mult_spvec_spmat algebra_simps move_matrix_mult)
   356   done
   357 
   358 lemma sorted_spvec_mult_spmat[rule_format]:
   359   "sorted_spvec (A::('a::lattice_ring) spmat) \<longrightarrow> sorted_spvec (mult_spmat A B)"
   360   apply (induct A)
   361   apply (auto)
   362   apply (drule sorted_spvec_cons1, simp)
   363   apply (case_tac A)
   364   apply (auto simp add: sorted_spvec.simps)
   365   done
   366 
   367 lemma sorted_spmat_mult_spmat:
   368   "sorted_spmat (B::('a::lattice_ring) spmat) \<Longrightarrow> sorted_spmat (mult_spmat A B)"
   369   apply (induct A)
   370   apply (auto simp add: sorted_mult_spvec_spmat) 
   371   done
   372 
   373 
   374 fun add_spvec :: "('a::lattice_ab_group_add) spvec \<Rightarrow> 'a spvec \<Rightarrow> 'a spvec"
   375 where
   376 (* "measure (% (a, b). length a + (length b))" *)
   377   "add_spvec arr [] = arr"
   378 | "add_spvec [] brr = brr"
   379 | "add_spvec ((i,a)#arr) ((j,b)#brr) = (
   380      if i < j then (i,a)#(add_spvec arr ((j,b)#brr)) 
   381      else if (j < i) then (j,b) # add_spvec ((i,a)#arr) brr
   382      else (i, a+b) # add_spvec arr brr)"
   383 
   384 lemma add_spvec_empty1[simp]: "add_spvec [] a = a"
   385 by (cases a, auto)
   386 
   387 lemma sparse_row_vector_add: "sparse_row_vector (add_spvec a b) = (sparse_row_vector a) + (sparse_row_vector b)"
   388   apply (induct a b rule: add_spvec.induct)
   389   apply (simp_all add: singleton_matrix_add)
   390   done
   391 
   392 fun add_spmat :: "('a::lattice_ab_group_add) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
   393 where
   394 (* "measure (% (A,B). (length A)+(length B))" *)
   395   "add_spmat [] bs = bs"
   396 | "add_spmat as [] = as"
   397 | "add_spmat ((i,a)#as) ((j,b)#bs) = (
   398     if i < j then 
   399       (i,a) # add_spmat as ((j,b)#bs)
   400     else if j < i then
   401       (j,b) # add_spmat ((i,a)#as) bs
   402     else
   403       (i, add_spvec a b) # add_spmat as bs)"
   404 
   405 lemma add_spmat_Nil2[simp]: "add_spmat as [] = as"
   406 by(cases as) auto
   407 
   408 lemma sparse_row_add_spmat: "sparse_row_matrix (add_spmat A B) = (sparse_row_matrix A) + (sparse_row_matrix B)"
   409   apply (induct A B rule: add_spmat.induct)
   410   apply (auto simp add: sparse_row_matrix_cons sparse_row_vector_add move_matrix_add)
   411   done
   412 
   413 lemmas [code] = sparse_row_add_spmat [symmetric]
   414 lemmas [code] = sparse_row_vector_add [symmetric]
   415 
   416 lemma sorted_add_spvec_helper1[rule_format]: "add_spvec ((a,b)#arr) brr = (ab, bb) # list \<longrightarrow> (ab = a | (brr \<noteq> [] & ab = fst (hd brr)))"
   417   proof - 
   418     have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spvec x brr = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))"
   419       by (induct brr rule: add_spvec.induct) (auto split:if_splits)
   420     then show ?thesis
   421       by (case_tac brr, auto)
   422   qed
   423 
   424 lemma sorted_add_spmat_helper1[rule_format]: "add_spmat ((a,b)#arr) brr = (ab, bb) # list \<longrightarrow> (ab = a | (brr \<noteq> [] & ab = fst (hd brr)))"
   425   proof - 
   426     have "(! x ab a. x = (a,b)#arr \<longrightarrow> add_spmat x brr = (ab, bb) # list \<longrightarrow> (ab = a | (ab = fst (hd brr))))"
   427       by (rule add_spmat.induct) (auto split:if_splits)
   428     then show ?thesis
   429       by (case_tac brr, auto)
   430   qed
   431 
   432 lemma sorted_add_spvec_helper: "add_spvec arr brr = (ab, bb) # list \<Longrightarrow> ((arr \<noteq> [] & ab = fst (hd arr)) | (brr \<noteq> [] & ab = fst (hd brr)))"
   433   apply (induct arr brr rule: add_spvec.induct)
   434   apply (auto split:if_splits)
   435   done
   436 
   437 lemma sorted_add_spmat_helper: "add_spmat arr brr = (ab, bb) # list \<Longrightarrow> ((arr \<noteq> [] & ab = fst (hd arr)) | (brr \<noteq> [] & ab = fst (hd brr)))"
   438   apply (induct arr brr rule: add_spmat.induct)
   439   apply (auto split:if_splits)
   440   done
   441 
   442 lemma add_spvec_commute: "add_spvec a b = add_spvec b a"
   443 by (induct a b rule: add_spvec.induct) auto
   444 
   445 lemma add_spmat_commute: "add_spmat a b = add_spmat b a"
   446   apply (induct a b rule: add_spmat.induct)
   447   apply (simp_all add: add_spvec_commute)
   448   done
   449   
   450 lemma sorted_add_spvec_helper2: "add_spvec ((a,b)#arr) brr = (ab, bb) # list \<Longrightarrow> aa < a \<Longrightarrow> sorted_spvec ((aa, ba) # brr) \<Longrightarrow> aa < ab"
   451   apply (drule sorted_add_spvec_helper1)
   452   apply (auto)
   453   apply (case_tac brr)
   454   apply (simp_all)
   455   apply (drule_tac sorted_spvec_cons3)
   456   apply (simp)
   457   done
   458 
   459 lemma sorted_add_spmat_helper2: "add_spmat ((a,b)#arr) brr = (ab, bb) # list \<Longrightarrow> aa < a \<Longrightarrow> sorted_spvec ((aa, ba) # brr) \<Longrightarrow> aa < ab"
   460   apply (drule sorted_add_spmat_helper1)
   461   apply (auto)
   462   apply (case_tac brr)
   463   apply (simp_all)
   464   apply (drule_tac sorted_spvec_cons3)
   465   apply (simp)
   466   done
   467 
   468 lemma sorted_spvec_add_spvec[rule_format]: "sorted_spvec a \<longrightarrow> sorted_spvec b \<longrightarrow> sorted_spvec (add_spvec a b)"
   469   apply (induct a b rule: add_spvec.induct)
   470   apply (simp_all)
   471   apply (rule conjI)
   472   apply (clarsimp)
   473   apply (frule_tac as=brr in sorted_spvec_cons1)
   474   apply (simp)
   475   apply (subst sorted_spvec_step)
   476   apply (clarsimp simp: sorted_add_spvec_helper2 split: list.split)
   477   apply (clarify)
   478   apply (rule conjI)
   479   apply (clarify)
   480   apply (frule_tac as=arr in sorted_spvec_cons1, simp)
   481   apply (subst sorted_spvec_step)
   482   apply (clarsimp simp: sorted_add_spvec_helper2 add_spvec_commute split: list.split)
   483   apply (clarify)
   484   apply (frule_tac as=arr in sorted_spvec_cons1)
   485   apply (frule_tac as=brr in sorted_spvec_cons1)
   486   apply (simp)
   487   apply (subst sorted_spvec_step)
   488   apply (simp split: list.split)
   489   apply (clarsimp)
   490   apply (drule_tac sorted_add_spvec_helper)
   491   apply (auto simp: neq_Nil_conv)
   492   apply (drule sorted_spvec_cons3)
   493   apply (simp)
   494   apply (drule sorted_spvec_cons3)
   495   apply (simp)
   496   done
   497 
   498 lemma sorted_spvec_add_spmat[rule_format]: "sorted_spvec A \<longrightarrow> sorted_spvec B \<longrightarrow> sorted_spvec (add_spmat A B)"
   499   apply (induct A B rule: add_spmat.induct)
   500   apply (simp_all)
   501   apply (rule conjI)
   502   apply (intro strip)
   503   apply (simp)
   504   apply (frule_tac as=bs in sorted_spvec_cons1)
   505   apply (simp)
   506   apply (subst sorted_spvec_step)
   507   apply (simp split: list.split)
   508   apply (clarify, simp)
   509   apply (simp add: sorted_add_spmat_helper2)
   510   apply (clarify)
   511   apply (rule conjI)
   512   apply (clarify)
   513   apply (frule_tac as=as in sorted_spvec_cons1, simp)
   514   apply (subst sorted_spvec_step)
   515   apply (clarsimp simp: sorted_add_spmat_helper2 add_spmat_commute split: list.split)
   516   apply (clarsimp)
   517   apply (frule_tac as=as in sorted_spvec_cons1)
   518   apply (frule_tac as=bs in sorted_spvec_cons1)
   519   apply (simp)
   520   apply (subst sorted_spvec_step)
   521   apply (simp split: list.split)
   522   apply (clarify, simp)
   523   apply (drule_tac sorted_add_spmat_helper)
   524   apply (auto simp:neq_Nil_conv)
   525   apply (drule sorted_spvec_cons3)
   526   apply (simp)
   527   apply (drule sorted_spvec_cons3)
   528   apply (simp)
   529   done
   530 
   531 lemma sorted_spmat_add_spmat[rule_format]: "sorted_spmat A \<Longrightarrow> sorted_spmat B \<Longrightarrow> sorted_spmat (add_spmat A B)"
   532   apply (induct A B rule: add_spmat.induct)
   533   apply (simp_all add: sorted_spvec_add_spvec)
   534   done
   535 
   536 fun le_spvec :: "('a::lattice_ab_group_add) spvec \<Rightarrow> 'a spvec \<Rightarrow> bool"
   537 where
   538 (* "measure (% (a,b). (length a) + (length b))" *)
   539   "le_spvec [] [] = True"
   540 | "le_spvec ((_,a)#as) [] = (a <= 0 & le_spvec as [])"
   541 | "le_spvec [] ((_,b)#bs) = (0 <= b & le_spvec [] bs)"
   542 | "le_spvec ((i,a)#as) ((j,b)#bs) = (
   543     if (i < j) then a <= 0 & le_spvec as ((j,b)#bs)
   544     else if (j < i) then 0 <= b & le_spvec ((i,a)#as) bs
   545     else a <= b & le_spvec as bs)"
   546 
   547 fun le_spmat :: "('a::lattice_ab_group_add) spmat \<Rightarrow> 'a spmat \<Rightarrow> bool"
   548 where
   549 (* "measure (% (a,b). (length a) + (length b))" *)
   550   "le_spmat [] [] = True"
   551 | "le_spmat ((i,a)#as) [] = (le_spvec a [] & le_spmat as [])"
   552 | "le_spmat [] ((j,b)#bs) = (le_spvec [] b & le_spmat [] bs)"
   553 | "le_spmat ((i,a)#as) ((j,b)#bs) = (
   554     if i < j then (le_spvec a [] & le_spmat as ((j,b)#bs))
   555     else if j < i then (le_spvec [] b & le_spmat ((i,a)#as) bs)
   556     else (le_spvec a b & le_spmat as bs))"
   557 
   558 definition disj_matrices :: "('a::zero) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool" where
   559   "disj_matrices A B \<longleftrightarrow>
   560     (! j i. (Rep_matrix A j i \<noteq> 0) \<longrightarrow> (Rep_matrix B j i = 0)) & (! j i. (Rep_matrix B j i \<noteq> 0) \<longrightarrow> (Rep_matrix A j i = 0))"  
   561 
   562 declare [[simp_depth_limit = 6]]
   563 
   564 lemma disj_matrices_contr1: "disj_matrices A B \<Longrightarrow> Rep_matrix A j i \<noteq> 0 \<Longrightarrow> Rep_matrix B j i = 0"
   565    by (simp add: disj_matrices_def)
   566 
   567 lemma disj_matrices_contr2: "disj_matrices A B \<Longrightarrow> Rep_matrix B j i \<noteq> 0 \<Longrightarrow> Rep_matrix A j i = 0"
   568    by (simp add: disj_matrices_def)
   569 
   570 
   571 lemma disj_matrices_add: "disj_matrices A B \<Longrightarrow> disj_matrices C D \<Longrightarrow> disj_matrices A D \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
   572   (A + B <= C + D) = (A <= C & B <= (D::('a::lattice_ab_group_add) matrix))"
   573   apply (auto)
   574   apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def)
   575   apply (intro strip)
   576   apply (erule conjE)+
   577   apply (drule_tac j=j and i=i in spec2)+
   578   apply (case_tac "Rep_matrix B j i = 0")
   579   apply (case_tac "Rep_matrix D j i = 0")
   580   apply (simp_all)
   581   apply (simp (no_asm_use) only: le_matrix_def disj_matrices_def)
   582   apply (intro strip)
   583   apply (erule conjE)+
   584   apply (drule_tac j=j and i=i in spec2)+
   585   apply (case_tac "Rep_matrix A j i = 0")
   586   apply (case_tac "Rep_matrix C j i = 0")
   587   apply (simp_all)
   588   apply (erule add_mono)
   589   apply (assumption)
   590   done
   591 
   592 lemma disj_matrices_zero1[simp]: "disj_matrices 0 B"
   593 by (simp add: disj_matrices_def)
   594 
   595 lemma disj_matrices_zero2[simp]: "disj_matrices A 0"
   596 by (simp add: disj_matrices_def)
   597 
   598 lemma disj_matrices_commute: "disj_matrices A B = disj_matrices B A"
   599 by (auto simp add: disj_matrices_def)
   600 
   601 lemma disj_matrices_add_le_zero: "disj_matrices A B \<Longrightarrow>
   602   (A + B <= 0) = (A <= 0 & (B::('a::lattice_ab_group_add) matrix) <= 0)"
   603 by (rule disj_matrices_add[of A B 0 0, simplified])
   604  
   605 lemma disj_matrices_add_zero_le: "disj_matrices A B \<Longrightarrow>
   606   (0 <= A + B) = (0 <= A & 0 <= (B::('a::lattice_ab_group_add) matrix))"
   607 by (rule disj_matrices_add[of 0 0 A B, simplified])
   608 
   609 lemma disj_matrices_add_x_le: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
   610   (A <= B + C) = (A <= C & 0 <= (B::('a::lattice_ab_group_add) matrix))"
   611 by (auto simp add: disj_matrices_add[of 0 A B C, simplified])
   612 
   613 lemma disj_matrices_add_le_x: "disj_matrices A B \<Longrightarrow> disj_matrices B C \<Longrightarrow> 
   614   (B + A <= C) = (A <= C &  (B::('a::lattice_ab_group_add) matrix) <= 0)"
   615 by (auto simp add: disj_matrices_add[of B A 0 C,simplified] disj_matrices_commute)
   616 
   617 lemma disj_sparse_row_singleton: "i <= j \<Longrightarrow> sorted_spvec((j,y)#v) \<Longrightarrow> disj_matrices (sparse_row_vector v) (singleton_matrix 0 i x)"
   618   apply (simp add: disj_matrices_def)
   619   apply (rule conjI)
   620   apply (rule neg_imp)
   621   apply (simp)
   622   apply (intro strip)
   623   apply (rule sorted_sparse_row_vector_zero)
   624   apply (simp_all)
   625   apply (intro strip)
   626   apply (rule sorted_sparse_row_vector_zero)
   627   apply (simp_all)
   628   done 
   629 
   630 lemma disj_matrices_x_add: "disj_matrices A B \<Longrightarrow> disj_matrices A C \<Longrightarrow> disj_matrices (A::('a::lattice_ab_group_add) matrix) (B+C)"
   631   apply (simp add: disj_matrices_def)
   632   apply (auto)
   633   apply (drule_tac j=j and i=i in spec2)+
   634   apply (case_tac "Rep_matrix B j i = 0")
   635   apply (case_tac "Rep_matrix C j i = 0")
   636   apply (simp_all)
   637   done
   638 
   639 lemma disj_matrices_add_x: "disj_matrices A B \<Longrightarrow> disj_matrices A C \<Longrightarrow> disj_matrices (B+C) (A::('a::lattice_ab_group_add) matrix)" 
   640   by (simp add: disj_matrices_x_add disj_matrices_commute)
   641 
   642 lemma disj_singleton_matrices[simp]: "disj_matrices (singleton_matrix j i x) (singleton_matrix u v y) = (j \<noteq> u | i \<noteq> v | x = 0 | y = 0)" 
   643   by (auto simp add: disj_matrices_def)
   644 
   645 lemma disj_move_sparse_vec_mat[simplified disj_matrices_commute]: 
   646   "j <= a \<Longrightarrow> sorted_spvec((a,c)#as) \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector b) (int j) i) (sparse_row_matrix as)"
   647   apply (auto simp add: disj_matrices_def)
   648   apply (drule nrows_notzero)
   649   apply (drule less_le_trans[OF _ nrows_spvec])
   650   apply (subgoal_tac "ja = j")
   651   apply (simp add: sorted_sparse_row_matrix_zero)
   652   apply (arith)
   653   apply (rule nrows)
   654   apply (rule order_trans[of _ 1 _])
   655   apply (simp)
   656   apply (case_tac "nat (int ja - int j) = 0")
   657   apply (case_tac "ja = j")
   658   apply (simp add: sorted_sparse_row_matrix_zero)
   659   apply arith+
   660   done
   661 
   662 lemma disj_move_sparse_row_vector_twice:
   663   "j \<noteq> u \<Longrightarrow> disj_matrices (move_matrix (sparse_row_vector a) j i) (move_matrix (sparse_row_vector b) u v)"
   664   apply (auto simp add: disj_matrices_def)
   665   apply (rule nrows, rule order_trans[of _ 1], simp, drule nrows_notzero, drule less_le_trans[OF _ nrows_spvec], arith)+
   666   done
   667 
   668 lemma le_spvec_iff_sparse_row_le[rule_format]: "(sorted_spvec a) \<longrightarrow> (sorted_spvec b) \<longrightarrow> (le_spvec a b) = (sparse_row_vector a <= sparse_row_vector b)"
   669   apply (induct a b rule: le_spvec.induct)
   670   apply (simp_all add: sorted_spvec_cons1 disj_matrices_add_le_zero disj_matrices_add_zero_le 
   671     disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
   672   apply (rule conjI, intro strip)
   673   apply (simp add: sorted_spvec_cons1)
   674   apply (subst disj_matrices_add_x_le)
   675   apply (simp add: disj_sparse_row_singleton[OF less_imp_le] disj_matrices_x_add disj_matrices_commute)
   676   apply (simp add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
   677   apply (simp, blast)
   678   apply (intro strip, rule conjI, intro strip)
   679   apply (simp add: sorted_spvec_cons1)
   680   apply (subst disj_matrices_add_le_x)
   681   apply (simp_all add: disj_sparse_row_singleton[OF order_refl] disj_sparse_row_singleton[OF less_imp_le] disj_matrices_commute disj_matrices_x_add)
   682   apply (blast)
   683   apply (intro strip)
   684   apply (simp add: sorted_spvec_cons1)
   685   apply (case_tac "a=b", simp_all)
   686   apply (subst disj_matrices_add)
   687   apply (simp_all add: disj_sparse_row_singleton[OF order_refl] disj_matrices_commute)
   688   done
   689 
   690 lemma le_spvec_empty2_sparse_row[rule_format]: "sorted_spvec b \<longrightarrow> le_spvec b [] = (sparse_row_vector b <= 0)"
   691   apply (induct b)
   692   apply (simp_all add: sorted_spvec_cons1)
   693   apply (intro strip)
   694   apply (subst disj_matrices_add_le_zero)
   695   apply (auto simp add: disj_matrices_commute disj_sparse_row_singleton[OF order_refl] sorted_spvec_cons1)
   696   done
   697 
   698 lemma le_spvec_empty1_sparse_row[rule_format]: "(sorted_spvec b) \<longrightarrow> (le_spvec [] b = (0 <= sparse_row_vector b))"
   699   apply (induct b)
   700   apply (simp_all add: sorted_spvec_cons1)
   701   apply (intro strip)
   702   apply (subst disj_matrices_add_zero_le)
   703   apply (auto simp add: disj_matrices_commute disj_sparse_row_singleton[OF order_refl] sorted_spvec_cons1)
   704   done
   705 
   706 lemma le_spmat_iff_sparse_row_le[rule_format]: "(sorted_spvec A) \<longrightarrow> (sorted_spmat A) \<longrightarrow> (sorted_spvec B) \<longrightarrow> (sorted_spmat B) \<longrightarrow> 
   707   le_spmat A B = (sparse_row_matrix A <= sparse_row_matrix B)"
   708   apply (induct A B rule: le_spmat.induct)
   709   apply (simp add: sparse_row_matrix_cons disj_matrices_add_le_zero disj_matrices_add_zero_le disj_move_sparse_vec_mat[OF order_refl] 
   710     disj_matrices_commute sorted_spvec_cons1 le_spvec_empty2_sparse_row le_spvec_empty1_sparse_row)+ 
   711   apply (rule conjI, intro strip)
   712   apply (simp add: sorted_spvec_cons1)
   713   apply (subst disj_matrices_add_x_le)
   714   apply (rule disj_matrices_add_x)
   715   apply (simp add: disj_move_sparse_row_vector_twice)
   716   apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute)
   717   apply (simp add: disj_move_sparse_vec_mat[OF order_refl] disj_matrices_commute)
   718   apply (simp, blast)
   719   apply (intro strip, rule conjI, intro strip)
   720   apply (simp add: sorted_spvec_cons1)
   721   apply (subst disj_matrices_add_le_x)
   722   apply (simp add: disj_move_sparse_vec_mat[OF order_refl])
   723   apply (rule disj_matrices_x_add)
   724   apply (simp add: disj_move_sparse_row_vector_twice)
   725   apply (simp add: disj_move_sparse_vec_mat[OF less_imp_le] disj_matrices_commute)
   726   apply (simp, blast)
   727   apply (intro strip)
   728   apply (case_tac "i=j")
   729   apply (simp_all)
   730   apply (subst disj_matrices_add)
   731   apply (simp_all add: disj_matrices_commute disj_move_sparse_vec_mat[OF order_refl])
   732   apply (simp add: sorted_spvec_cons1 le_spvec_iff_sparse_row_le)
   733   done
   734 
   735 declare [[simp_depth_limit = 999]]
   736 
   737 primrec abs_spmat :: "('a::lattice_ring) spmat \<Rightarrow> 'a spmat"
   738 where
   739   "abs_spmat [] = []"
   740 | "abs_spmat (a#as) = (fst a, abs_spvec (snd a))#(abs_spmat as)"
   741 
   742 primrec minus_spmat :: "('a::lattice_ring) spmat \<Rightarrow> 'a spmat"
   743 where
   744   "minus_spmat [] = []"
   745 | "minus_spmat (a#as) = (fst a, minus_spvec (snd a))#(minus_spmat as)"
   746 
   747 lemma sparse_row_matrix_minus:
   748   "sparse_row_matrix (minus_spmat A) = - (sparse_row_matrix A)"
   749   apply (induct A)
   750   apply (simp_all add: sparse_row_vector_minus sparse_row_matrix_cons)
   751   apply (subst Rep_matrix_inject[symmetric])
   752   apply (rule ext)+
   753   apply simp
   754   done
   755 
   756 lemma Rep_sparse_row_vector_zero: "x \<noteq> 0 \<Longrightarrow> Rep_matrix (sparse_row_vector v) x y = 0"
   757 proof -
   758   assume x:"x \<noteq> 0"
   759   have r:"nrows (sparse_row_vector v) <= Suc 0" by (rule nrows_spvec)
   760   show ?thesis
   761     apply (rule nrows)
   762     apply (subgoal_tac "Suc 0 <= x")
   763     apply (insert r)
   764     apply (simp only:)
   765     apply (insert x)
   766     apply arith
   767     done
   768 qed
   769     
   770 lemma sparse_row_matrix_abs:
   771   "sorted_spvec A \<Longrightarrow> sorted_spmat A \<Longrightarrow> sparse_row_matrix (abs_spmat A) = abs (sparse_row_matrix A)"
   772   apply (induct A)
   773   apply (simp_all add: sparse_row_vector_abs sparse_row_matrix_cons)
   774   apply (frule_tac sorted_spvec_cons1, simp)
   775   apply (simplesubst Rep_matrix_inject[symmetric])
   776   apply (rule ext)+
   777   apply auto
   778   apply (case_tac "x=a")
   779   apply (simp)
   780   apply (simplesubst sorted_sparse_row_matrix_zero)
   781   apply auto
   782   apply (simplesubst Rep_sparse_row_vector_zero)
   783   apply simp_all
   784   done
   785 
   786 lemma sorted_spvec_minus_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec (minus_spmat A)"
   787   apply (induct A)
   788   apply (simp)
   789   apply (frule sorted_spvec_cons1, simp)
   790   apply (simp add: sorted_spvec.simps split:list.split_asm)
   791   done 
   792 
   793 lemma sorted_spvec_abs_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec (abs_spmat A)" 
   794   apply (induct A)
   795   apply (simp)
   796   apply (frule sorted_spvec_cons1, simp)
   797   apply (simp add: sorted_spvec.simps split:list.split_asm)
   798   done
   799 
   800 lemma sorted_spmat_minus_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat (minus_spmat A)"
   801   apply (induct A)
   802   apply (simp_all add: sorted_spvec_minus_spvec)
   803   done
   804 
   805 lemma sorted_spmat_abs_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat (abs_spmat A)"
   806   apply (induct A)
   807   apply (simp_all add: sorted_spvec_abs_spvec)
   808   done
   809 
   810 definition diff_spmat :: "('a::lattice_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat"
   811   where "diff_spmat A B = add_spmat A (minus_spmat B)"
   812 
   813 lemma sorted_spmat_diff_spmat: "sorted_spmat A \<Longrightarrow> sorted_spmat B \<Longrightarrow> sorted_spmat (diff_spmat A B)"
   814   by (simp add: diff_spmat_def sorted_spmat_minus_spmat sorted_spmat_add_spmat)
   815 
   816 lemma sorted_spvec_diff_spmat: "sorted_spvec A \<Longrightarrow> sorted_spvec B \<Longrightarrow> sorted_spvec (diff_spmat A B)"
   817   by (simp add: diff_spmat_def sorted_spvec_minus_spmat sorted_spvec_add_spmat)
   818 
   819 lemma sparse_row_diff_spmat: "sparse_row_matrix (diff_spmat A B ) = (sparse_row_matrix A) - (sparse_row_matrix B)"
   820   by (simp add: diff_spmat_def sparse_row_add_spmat sparse_row_matrix_minus)
   821 
   822 definition sorted_sparse_matrix :: "'a spmat \<Rightarrow> bool"
   823   where "sorted_sparse_matrix A \<longleftrightarrow> sorted_spvec A & sorted_spmat A"
   824 
   825 lemma sorted_sparse_matrix_imp_spvec: "sorted_sparse_matrix A \<Longrightarrow> sorted_spvec A"
   826   by (simp add: sorted_sparse_matrix_def)
   827 
   828 lemma sorted_sparse_matrix_imp_spmat: "sorted_sparse_matrix A \<Longrightarrow> sorted_spmat A"
   829   by (simp add: sorted_sparse_matrix_def)
   830 
   831 lemmas sorted_sp_simps = 
   832   sorted_spvec.simps
   833   sorted_spmat.simps
   834   sorted_sparse_matrix_def
   835 
   836 lemma bool1: "(\<not> True) = False"  by blast
   837 lemma bool2: "(\<not> False) = True"  by blast
   838 lemma bool3: "((P\<Colon>bool) \<and> True) = P" by blast
   839 lemma bool4: "(True \<and> (P\<Colon>bool)) = P" by blast
   840 lemma bool5: "((P\<Colon>bool) \<and> False) = False" by blast
   841 lemma bool6: "(False \<and> (P\<Colon>bool)) = False" by blast
   842 lemma bool7: "((P\<Colon>bool) \<or> True) = True" by blast
   843 lemma bool8: "(True \<or> (P\<Colon>bool)) = True" by blast
   844 lemma bool9: "((P\<Colon>bool) \<or> False) = P" by blast
   845 lemma bool10: "(False \<or> (P\<Colon>bool)) = P" by blast
   846 lemmas boolarith = bool1 bool2 bool3 bool4 bool5 bool6 bool7 bool8 bool9 bool10
   847 
   848 lemma if_case_eq: "(if b then x else y) = (case b of True => x | False => y)" by simp
   849 
   850 primrec pprt_spvec :: "('a::{lattice_ab_group_add}) spvec \<Rightarrow> 'a spvec"
   851 where
   852   "pprt_spvec [] = []"
   853 | "pprt_spvec (a#as) = (fst a, pprt (snd a)) # (pprt_spvec as)"
   854 
   855 primrec nprt_spvec :: "('a::{lattice_ab_group_add}) spvec \<Rightarrow> 'a spvec"
   856 where
   857   "nprt_spvec [] = []"
   858 | "nprt_spvec (a#as) = (fst a, nprt (snd a)) # (nprt_spvec as)"
   859 
   860 primrec pprt_spmat :: "('a::{lattice_ab_group_add}) spmat \<Rightarrow> 'a spmat"
   861 where
   862   "pprt_spmat [] = []"
   863 | "pprt_spmat (a#as) = (fst a, pprt_spvec (snd a))#(pprt_spmat as)"
   864 
   865 primrec nprt_spmat :: "('a::{lattice_ab_group_add}) spmat \<Rightarrow> 'a spmat"
   866 where
   867   "nprt_spmat [] = []"
   868 | "nprt_spmat (a#as) = (fst a, nprt_spvec (snd a))#(nprt_spmat as)"
   869 
   870 
   871 lemma pprt_add: "disj_matrices A (B::(_::lattice_ring) matrix) \<Longrightarrow> pprt (A+B) = pprt A + pprt B"
   872   apply (simp add: pprt_def sup_matrix_def)
   873   apply (simp add: Rep_matrix_inject[symmetric])
   874   apply (rule ext)+
   875   apply simp
   876   apply (case_tac "Rep_matrix A x xa \<noteq> 0")
   877   apply (simp_all add: disj_matrices_contr1)
   878   done
   879 
   880 lemma nprt_add: "disj_matrices A (B::(_::lattice_ring) matrix) \<Longrightarrow> nprt (A+B) = nprt A + nprt B"
   881   apply (simp add: nprt_def inf_matrix_def)
   882   apply (simp add: Rep_matrix_inject[symmetric])
   883   apply (rule ext)+
   884   apply simp
   885   apply (case_tac "Rep_matrix A x xa \<noteq> 0")
   886   apply (simp_all add: disj_matrices_contr1)
   887   done
   888 
   889 lemma pprt_singleton[simp]: "pprt (singleton_matrix j i (x::_::lattice_ring)) = singleton_matrix j i (pprt x)"
   890   apply (simp add: pprt_def sup_matrix_def)
   891   apply (simp add: Rep_matrix_inject[symmetric])
   892   apply (rule ext)+
   893   apply simp
   894   done
   895 
   896 lemma nprt_singleton[simp]: "nprt (singleton_matrix j i (x::_::lattice_ring)) = singleton_matrix j i (nprt x)"
   897   apply (simp add: nprt_def inf_matrix_def)
   898   apply (simp add: Rep_matrix_inject[symmetric])
   899   apply (rule ext)+
   900   apply simp
   901   done
   902 
   903 lemma less_imp_le: "a < b \<Longrightarrow> a <= (b::_::order)" by (simp add: less_def)
   904 
   905 lemma sparse_row_vector_pprt: "sorted_spvec (v :: 'a::lattice_ring spvec) \<Longrightarrow> sparse_row_vector (pprt_spvec v) = pprt (sparse_row_vector v)"
   906   apply (induct v)
   907   apply (simp_all)
   908   apply (frule sorted_spvec_cons1, auto)
   909   apply (subst pprt_add)
   910   apply (subst disj_matrices_commute)
   911   apply (rule disj_sparse_row_singleton)
   912   apply auto
   913   done
   914 
   915 lemma sparse_row_vector_nprt: "sorted_spvec (v :: 'a::lattice_ring spvec) \<Longrightarrow> sparse_row_vector (nprt_spvec v) = nprt (sparse_row_vector v)"
   916   apply (induct v)
   917   apply (simp_all)
   918   apply (frule sorted_spvec_cons1, auto)
   919   apply (subst nprt_add)
   920   apply (subst disj_matrices_commute)
   921   apply (rule disj_sparse_row_singleton)
   922   apply auto
   923   done
   924   
   925   
   926 lemma pprt_move_matrix: "pprt (move_matrix (A::('a::lattice_ring) matrix) j i) = move_matrix (pprt A) j i"
   927   apply (simp add: pprt_def)
   928   apply (simp add: sup_matrix_def)
   929   apply (simp add: Rep_matrix_inject[symmetric])
   930   apply (rule ext)+
   931   apply (simp)
   932   done
   933 
   934 lemma nprt_move_matrix: "nprt (move_matrix (A::('a::lattice_ring) matrix) j i) = move_matrix (nprt A) j i"
   935   apply (simp add: nprt_def)
   936   apply (simp add: inf_matrix_def)
   937   apply (simp add: Rep_matrix_inject[symmetric])
   938   apply (rule ext)+
   939   apply (simp)
   940   done
   941 
   942 lemma sparse_row_matrix_pprt: "sorted_spvec (m :: 'a::lattice_ring spmat) \<Longrightarrow> sorted_spmat m \<Longrightarrow> sparse_row_matrix (pprt_spmat m) = pprt (sparse_row_matrix m)"
   943   apply (induct m)
   944   apply simp
   945   apply simp
   946   apply (frule sorted_spvec_cons1)
   947   apply (simp add: sparse_row_matrix_cons sparse_row_vector_pprt)
   948   apply (subst pprt_add)
   949   apply (subst disj_matrices_commute)
   950   apply (rule disj_move_sparse_vec_mat)
   951   apply auto
   952   apply (simp add: sorted_spvec.simps)
   953   apply (simp split: list.split)
   954   apply auto
   955   apply (simp add: pprt_move_matrix)
   956   done
   957 
   958 lemma sparse_row_matrix_nprt: "sorted_spvec (m :: 'a::lattice_ring spmat) \<Longrightarrow> sorted_spmat m \<Longrightarrow> sparse_row_matrix (nprt_spmat m) = nprt (sparse_row_matrix m)"
   959   apply (induct m)
   960   apply simp
   961   apply simp
   962   apply (frule sorted_spvec_cons1)
   963   apply (simp add: sparse_row_matrix_cons sparse_row_vector_nprt)
   964   apply (subst nprt_add)
   965   apply (subst disj_matrices_commute)
   966   apply (rule disj_move_sparse_vec_mat)
   967   apply auto
   968   apply (simp add: sorted_spvec.simps)
   969   apply (simp split: list.split)
   970   apply auto
   971   apply (simp add: nprt_move_matrix)
   972   done
   973 
   974 lemma sorted_pprt_spvec: "sorted_spvec v \<Longrightarrow> sorted_spvec (pprt_spvec v)"
   975   apply (induct v)
   976   apply (simp)
   977   apply (frule sorted_spvec_cons1)
   978   apply simp
   979   apply (simp add: sorted_spvec.simps split:list.split_asm)
   980   done
   981 
   982 lemma sorted_nprt_spvec: "sorted_spvec v \<Longrightarrow> sorted_spvec (nprt_spvec v)"
   983   apply (induct v)
   984   apply (simp)
   985   apply (frule sorted_spvec_cons1)
   986   apply simp
   987   apply (simp add: sorted_spvec.simps split:list.split_asm)
   988   done
   989 
   990 lemma sorted_spvec_pprt_spmat: "sorted_spvec m \<Longrightarrow> sorted_spvec (pprt_spmat m)"
   991   apply (induct m)
   992   apply (simp)
   993   apply (frule sorted_spvec_cons1)
   994   apply simp
   995   apply (simp add: sorted_spvec.simps split:list.split_asm)
   996   done
   997 
   998 lemma sorted_spvec_nprt_spmat: "sorted_spvec m \<Longrightarrow> sorted_spvec (nprt_spmat m)"
   999   apply (induct m)
  1000   apply (simp)
  1001   apply (frule sorted_spvec_cons1)
  1002   apply simp
  1003   apply (simp add: sorted_spvec.simps split:list.split_asm)
  1004   done
  1005 
  1006 lemma sorted_spmat_pprt_spmat: "sorted_spmat m \<Longrightarrow> sorted_spmat (pprt_spmat m)"
  1007   apply (induct m)
  1008   apply (simp_all add: sorted_pprt_spvec)
  1009   done
  1010 
  1011 lemma sorted_spmat_nprt_spmat: "sorted_spmat m \<Longrightarrow> sorted_spmat (nprt_spmat m)"
  1012   apply (induct m)
  1013   apply (simp_all add: sorted_nprt_spvec)
  1014   done
  1015 
  1016 definition mult_est_spmat :: "('a::lattice_ring) spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat \<Rightarrow> 'a spmat" where
  1017   "mult_est_spmat r1 r2 s1 s2 =
  1018   add_spmat (mult_spmat (pprt_spmat s2) (pprt_spmat r2)) (add_spmat (mult_spmat (pprt_spmat s1) (nprt_spmat r2)) 
  1019   (add_spmat (mult_spmat (nprt_spmat s2) (pprt_spmat r1)) (mult_spmat (nprt_spmat s1) (nprt_spmat r1))))"  
  1020 
  1021 lemmas sparse_row_matrix_op_simps =
  1022   sorted_sparse_matrix_imp_spmat sorted_sparse_matrix_imp_spvec
  1023   sparse_row_add_spmat sorted_spvec_add_spmat sorted_spmat_add_spmat
  1024   sparse_row_diff_spmat sorted_spvec_diff_spmat sorted_spmat_diff_spmat
  1025   sparse_row_matrix_minus sorted_spvec_minus_spmat sorted_spmat_minus_spmat
  1026   sparse_row_mult_spmat sorted_spvec_mult_spmat sorted_spmat_mult_spmat
  1027   sparse_row_matrix_abs sorted_spvec_abs_spmat sorted_spmat_abs_spmat
  1028   le_spmat_iff_sparse_row_le
  1029   sparse_row_matrix_pprt sorted_spvec_pprt_spmat sorted_spmat_pprt_spmat
  1030   sparse_row_matrix_nprt sorted_spvec_nprt_spmat sorted_spmat_nprt_spmat
  1031 
  1032 lemmas sparse_row_matrix_arith_simps = 
  1033   mult_spmat.simps mult_spvec_spmat.simps 
  1034   addmult_spvec.simps 
  1035   smult_spvec_empty smult_spvec_cons
  1036   add_spmat.simps add_spvec.simps
  1037   minus_spmat.simps minus_spvec.simps
  1038   abs_spmat.simps abs_spvec.simps
  1039   diff_spmat_def
  1040   le_spmat.simps le_spvec.simps
  1041   pprt_spmat.simps pprt_spvec.simps
  1042   nprt_spmat.simps nprt_spvec.simps
  1043   mult_est_spmat_def
  1044 
  1045 
  1046 (*lemma spm_linprog_dual_estimate_1:
  1047   assumes  
  1048   "sorted_sparse_matrix A1"
  1049   "sorted_sparse_matrix A2"
  1050   "sorted_sparse_matrix c1"
  1051   "sorted_sparse_matrix c2"
  1052   "sorted_sparse_matrix y"
  1053   "sorted_spvec b"
  1054   "sorted_spvec r"
  1055   "le_spmat ([], y)"
  1056   "A * x \<le> sparse_row_matrix (b::('a::lattice_ring) spmat)"
  1057   "sparse_row_matrix A1 <= A"
  1058   "A <= sparse_row_matrix A2"
  1059   "sparse_row_matrix c1 <= c"
  1060   "c <= sparse_row_matrix c2"
  1061   "abs x \<le> sparse_row_matrix r"
  1062   shows
  1063   "c * x \<le> sparse_row_matrix (add_spmat (mult_spmat y b, mult_spmat (add_spmat (add_spmat (mult_spmat y (diff_spmat A2 A1), 
  1064   abs_spmat (diff_spmat (mult_spmat y A1) c1)), diff_spmat c2 c1)) r))"
  1065   by (insert prems, simp add: sparse_row_matrix_op_simps linprog_dual_estimate_1[where A=A])
  1066 *)
  1067 
  1068 end