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