src/HOL/Matrix_LP/Matrix.thy
author wenzelm
Tue Oct 10 19:23:03 2017 +0200 (2017-10-10)
changeset 66831 29ea2b900a05
parent 66453 cc19f7ca2ed6
child 67399 eab6ce8368fa
permissions -rw-r--r--
tuned: each session has at most one defining entry;
     1 (*  Title:      HOL/Matrix_LP/Matrix.thy
     2     Author:     Steven Obua
     3 *)
     4 
     5 theory Matrix
     6 imports Main "HOL-Library.Lattice_Algebras"
     7 begin
     8 
     9 type_synonym 'a infmatrix = "nat \<Rightarrow> nat \<Rightarrow> 'a"
    10 
    11 definition nonzero_positions :: "(nat \<Rightarrow> nat \<Rightarrow> 'a::zero) \<Rightarrow> (nat \<times> nat) set" where
    12   "nonzero_positions A = {pos. A (fst pos) (snd pos) ~= 0}"
    13 
    14 definition "matrix = {(f::(nat \<Rightarrow> nat \<Rightarrow> 'a::zero)). finite (nonzero_positions f)}"
    15 
    16 typedef (overloaded) 'a matrix = "matrix :: (nat \<Rightarrow> nat \<Rightarrow> 'a::zero) set"
    17   unfolding matrix_def
    18 proof
    19   show "(\<lambda>j i. 0) \<in> {(f::(nat \<Rightarrow> nat \<Rightarrow> 'a::zero)). finite (nonzero_positions f)}"
    20     by (simp add: nonzero_positions_def)
    21 qed
    22 
    23 declare Rep_matrix_inverse[simp]
    24 
    25 lemma finite_nonzero_positions : "finite (nonzero_positions (Rep_matrix A))"
    26   by (induct A) (simp add: Abs_matrix_inverse matrix_def)
    27 
    28 definition nrows :: "('a::zero) matrix \<Rightarrow> nat" where
    29   "nrows A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image fst) (nonzero_positions (Rep_matrix A))))"
    30 
    31 definition ncols :: "('a::zero) matrix \<Rightarrow> nat" where
    32   "ncols A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image snd) (nonzero_positions (Rep_matrix A))))"
    33 
    34 lemma nrows:
    35   assumes hyp: "nrows A \<le> m"
    36   shows "(Rep_matrix A m n) = 0"
    37 proof cases
    38   assume "nonzero_positions(Rep_matrix A) = {}"
    39   then show "(Rep_matrix A m n) = 0" by (simp add: nonzero_positions_def)
    40 next
    41   assume a: "nonzero_positions(Rep_matrix A) \<noteq> {}"
    42   let ?S = "fst`(nonzero_positions(Rep_matrix A))"
    43   have c: "finite (?S)" by (simp add: finite_nonzero_positions)
    44   from hyp have d: "Max (?S) < m" by (simp add: a nrows_def)
    45   have "m \<notin> ?S"
    46     proof -
    47       have "m \<in> ?S \<Longrightarrow> m <= Max(?S)" by (simp add: Max_ge [OF c])
    48       moreover from d have "~(m <= Max ?S)" by (simp)
    49       ultimately show "m \<notin> ?S" by (auto)
    50     qed
    51   thus "Rep_matrix A m n = 0" by (simp add: nonzero_positions_def image_Collect)
    52 qed
    53 
    54 definition transpose_infmatrix :: "'a infmatrix \<Rightarrow> 'a infmatrix" where
    55   "transpose_infmatrix A j i == A i j"
    56 
    57 definition transpose_matrix :: "('a::zero) matrix \<Rightarrow> 'a matrix" where
    58   "transpose_matrix == Abs_matrix o transpose_infmatrix o Rep_matrix"
    59 
    60 declare transpose_infmatrix_def[simp]
    61 
    62 lemma transpose_infmatrix_twice[simp]: "transpose_infmatrix (transpose_infmatrix A) = A"
    63 by ((rule ext)+, simp)
    64 
    65 lemma transpose_infmatrix: "transpose_infmatrix (% j i. P j i) = (% j i. P i j)"
    66   apply (rule ext)+
    67   by simp
    68 
    69 lemma transpose_infmatrix_closed[simp]: "Rep_matrix (Abs_matrix (transpose_infmatrix (Rep_matrix x))) = transpose_infmatrix (Rep_matrix x)"
    70 apply (rule Abs_matrix_inverse)
    71 apply (simp add: matrix_def nonzero_positions_def image_def)
    72 proof -
    73   let ?A = "{pos. Rep_matrix x (snd pos) (fst pos) \<noteq> 0}"
    74   let ?swap = "% pos. (snd pos, fst pos)"
    75   let ?B = "{pos. Rep_matrix x (fst pos) (snd pos) \<noteq> 0}"
    76   have swap_image: "?swap`?A = ?B"
    77     apply (simp add: image_def)
    78     apply (rule set_eqI)
    79     apply (simp)
    80     proof
    81       fix y
    82       assume hyp: "\<exists>a b. Rep_matrix x b a \<noteq> 0 \<and> y = (b, a)"
    83       thus "Rep_matrix x (fst y) (snd y) \<noteq> 0"
    84         proof -
    85           from hyp obtain a b where "(Rep_matrix x b a \<noteq> 0 & y = (b,a))" by blast
    86           then show "Rep_matrix x (fst y) (snd y) \<noteq> 0" by (simp)
    87         qed
    88     next
    89       fix y
    90       assume hyp: "Rep_matrix x (fst y) (snd y) \<noteq> 0"
    91       show "\<exists> a b. (Rep_matrix x b a \<noteq> 0 & y = (b,a))"
    92         by (rule exI[of _ "snd y"], rule exI[of _ "fst y"]) (simp add: hyp)
    93     qed
    94   then have "finite (?swap`?A)"
    95     proof -
    96       have "finite (nonzero_positions (Rep_matrix x))" by (simp add: finite_nonzero_positions)
    97       then have "finite ?B" by (simp add: nonzero_positions_def)
    98       with swap_image show "finite (?swap`?A)" by (simp)
    99     qed
   100   moreover
   101   have "inj_on ?swap ?A" by (simp add: inj_on_def)
   102   ultimately show "finite ?A"by (rule finite_imageD[of ?swap ?A])
   103 qed
   104 
   105 lemma infmatrixforward: "(x::'a infmatrix) = y \<Longrightarrow> \<forall> a b. x a b = y a b" by auto
   106 
   107 lemma transpose_infmatrix_inject: "(transpose_infmatrix A = transpose_infmatrix B) = (A = B)"
   108 apply (auto)
   109 apply (rule ext)+
   110 apply (simp add: transpose_infmatrix)
   111 apply (drule infmatrixforward)
   112 apply (simp)
   113 done
   114 
   115 lemma transpose_matrix_inject: "(transpose_matrix A = transpose_matrix B) = (A = B)"
   116 apply (simp add: transpose_matrix_def)
   117 apply (subst Rep_matrix_inject[THEN sym])+
   118 apply (simp only: transpose_infmatrix_closed transpose_infmatrix_inject)
   119 done
   120 
   121 lemma transpose_matrix[simp]: "Rep_matrix(transpose_matrix A) j i = Rep_matrix A i j"
   122 by (simp add: transpose_matrix_def)
   123 
   124 lemma transpose_transpose_id[simp]: "transpose_matrix (transpose_matrix A) = A"
   125 by (simp add: transpose_matrix_def)
   126 
   127 lemma nrows_transpose[simp]: "nrows (transpose_matrix A) = ncols A"
   128 by (simp add: nrows_def ncols_def nonzero_positions_def transpose_matrix_def image_def)
   129 
   130 lemma ncols_transpose[simp]: "ncols (transpose_matrix A) = nrows A"
   131 by (simp add: nrows_def ncols_def nonzero_positions_def transpose_matrix_def image_def)
   132 
   133 lemma ncols: "ncols A <= n \<Longrightarrow> Rep_matrix A m n = 0"
   134 proof -
   135   assume "ncols A <= n"
   136   then have "nrows (transpose_matrix A) <= n" by (simp)
   137   then have "Rep_matrix (transpose_matrix A) n m = 0" by (rule nrows)
   138   thus "Rep_matrix A m n = 0" by (simp add: transpose_matrix_def)
   139 qed
   140 
   141 lemma ncols_le: "(ncols A <= n) = (! j i. n <= i \<longrightarrow> (Rep_matrix A j i) = 0)" (is "_ = ?st")
   142 apply (auto)
   143 apply (simp add: ncols)
   144 proof (simp add: ncols_def, auto)
   145   let ?P = "nonzero_positions (Rep_matrix A)"
   146   let ?p = "snd`?P"
   147   have a:"finite ?p" by (simp add: finite_nonzero_positions)
   148   let ?m = "Max ?p"
   149   assume "~(Suc (?m) <= n)"
   150   then have b:"n <= ?m" by (simp)
   151   fix a b
   152   assume "(a,b) \<in> ?P"
   153   then have "?p \<noteq> {}" by (auto)
   154   with a have "?m \<in>  ?p" by (simp)
   155   moreover have "!x. (x \<in> ?p \<longrightarrow> (? y. (Rep_matrix A y x) \<noteq> 0))" by (simp add: nonzero_positions_def image_def)
   156   ultimately have "? y. (Rep_matrix A y ?m) \<noteq> 0" by (simp)
   157   moreover assume ?st
   158   ultimately show "False" using b by (simp)
   159 qed
   160 
   161 lemma less_ncols: "(n < ncols A) = (? j i. n <= i & (Rep_matrix A j i) \<noteq> 0)"
   162 proof -
   163   have a: "!! (a::nat) b. (a < b) = (~(b <= a))" by arith
   164   show ?thesis by (simp add: a ncols_le)
   165 qed
   166 
   167 lemma le_ncols: "(n <= ncols A) = (\<forall> m. (\<forall> j i. m <= i \<longrightarrow> (Rep_matrix A j i) = 0) \<longrightarrow> n <= m)"
   168 apply (auto)
   169 apply (subgoal_tac "ncols A <= m")
   170 apply (simp)
   171 apply (simp add: ncols_le)
   172 apply (drule_tac x="ncols A" in spec)
   173 by (simp add: ncols)
   174 
   175 lemma nrows_le: "(nrows A <= n) = (! j i. n <= j \<longrightarrow> (Rep_matrix A j i) = 0)" (is ?s)
   176 proof -
   177   have "(nrows A <= n) = (ncols (transpose_matrix A) <= n)" by (simp)
   178   also have "\<dots> = (! j i. n <= i \<longrightarrow> (Rep_matrix (transpose_matrix A) j i = 0))" by (rule ncols_le)
   179   also have "\<dots> = (! j i. n <= i \<longrightarrow> (Rep_matrix A i j) = 0)" by (simp)
   180   finally show "(nrows A <= n) = (! j i. n <= j \<longrightarrow> (Rep_matrix A j i) = 0)" by (auto)
   181 qed
   182 
   183 lemma less_nrows: "(m < nrows A) = (? j i. m <= j & (Rep_matrix A j i) \<noteq> 0)"
   184 proof -
   185   have a: "!! (a::nat) b. (a < b) = (~(b <= a))" by arith
   186   show ?thesis by (simp add: a nrows_le)
   187 qed
   188 
   189 lemma le_nrows: "(n <= nrows A) = (\<forall> m. (\<forall> j i. m <= j \<longrightarrow> (Rep_matrix A j i) = 0) \<longrightarrow> n <= m)"
   190 apply (auto)
   191 apply (subgoal_tac "nrows A <= m")
   192 apply (simp)
   193 apply (simp add: nrows_le)
   194 apply (drule_tac x="nrows A" in spec)
   195 by (simp add: nrows)
   196 
   197 lemma nrows_notzero: "Rep_matrix A m n \<noteq> 0 \<Longrightarrow> m < nrows A"
   198 apply (case_tac "nrows A <= m")
   199 apply (simp_all add: nrows)
   200 done
   201 
   202 lemma ncols_notzero: "Rep_matrix A m n \<noteq> 0 \<Longrightarrow> n < ncols A"
   203 apply (case_tac "ncols A <= n")
   204 apply (simp_all add: ncols)
   205 done
   206 
   207 lemma finite_natarray1: "finite {x. x < (n::nat)}"
   208 apply (induct n)
   209 apply (simp)
   210 proof -
   211   fix n
   212   have "{x. x < Suc n} = insert n {x. x < n}"  by (rule set_eqI, simp, arith)
   213   moreover assume "finite {x. x < n}"
   214   ultimately show "finite {x. x < Suc n}" by (simp)
   215 qed
   216 
   217 lemma finite_natarray2: "finite {(x, y). x < (m::nat) & y < (n::nat)}"
   218 by simp
   219 
   220 lemma RepAbs_matrix:
   221   assumes aem: "? m. ! j i. m <= j \<longrightarrow> x j i = 0" (is ?em) and aen:"? n. ! j i. (n <= i \<longrightarrow> x j i = 0)" (is ?en)
   222   shows "(Rep_matrix (Abs_matrix x)) = x"
   223 apply (rule Abs_matrix_inverse)
   224 apply (simp add: matrix_def nonzero_positions_def)
   225 proof -
   226   from aem obtain m where a: "! j i. m <= j \<longrightarrow> x j i = 0" by (blast)
   227   from aen obtain n where b: "! j i. n <= i \<longrightarrow> x j i = 0" by (blast)
   228   let ?u = "{(i, j). x i j \<noteq> 0}"
   229   let ?v = "{(i, j). i < m & j < n}"
   230   have c: "!! (m::nat) a. ~(m <= a) \<Longrightarrow> a < m" by (arith)
   231   from a b have "(?u \<inter> (-?v)) = {}"
   232     apply (simp)
   233     apply (rule set_eqI)
   234     apply (simp)
   235     apply auto
   236     by (rule c, auto)+
   237   then have d: "?u \<subseteq> ?v" by blast
   238   moreover have "finite ?v" by (simp add: finite_natarray2)
   239   moreover have "{pos. x (fst pos) (snd pos) \<noteq> 0} = ?u" by auto
   240   ultimately show "finite {pos. x (fst pos) (snd pos) \<noteq> 0}"
   241     by (metis (lifting) finite_subset)
   242 qed
   243 
   244 definition apply_infmatrix :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a infmatrix \<Rightarrow> 'b infmatrix" where
   245   "apply_infmatrix f == % A. (% j i. f (A j i))"
   246 
   247 definition apply_matrix :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a::zero) matrix \<Rightarrow> ('b::zero) matrix" where
   248   "apply_matrix f == % A. Abs_matrix (apply_infmatrix f (Rep_matrix A))"
   249 
   250 definition combine_infmatrix :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a infmatrix \<Rightarrow> 'b infmatrix \<Rightarrow> 'c infmatrix" where
   251   "combine_infmatrix f == % A B. (% j i. f (A j i) (B j i))"
   252 
   253 definition combine_matrix :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a::zero) matrix \<Rightarrow> ('b::zero) matrix \<Rightarrow> ('c::zero) matrix" where
   254   "combine_matrix f == % A B. Abs_matrix (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))"
   255 
   256 lemma expand_apply_infmatrix[simp]: "apply_infmatrix f A j i = f (A j i)"
   257 by (simp add: apply_infmatrix_def)
   258 
   259 lemma expand_combine_infmatrix[simp]: "combine_infmatrix f A B j i = f (A j i) (B j i)"
   260 by (simp add: combine_infmatrix_def)
   261 
   262 definition commutative :: "('a \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> bool" where
   263 "commutative f == ! x y. f x y = f y x"
   264 
   265 definition associative :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool" where
   266 "associative f == ! x y z. f (f x y) z = f x (f y z)"
   267 
   268 text\<open>
   269 To reason about associativity and commutativity of operations on matrices,
   270 let's take a step back and look at the general situtation: Assume that we have
   271 sets $A$ and $B$ with $B \subset A$ and an abstraction $u: A \rightarrow B$. This abstraction has to fulfill $u(b) = b$ for all $b \in B$, but is arbitrary otherwise.
   272 Each function $f: A \times A \rightarrow A$ now induces a function $f': B \times B \rightarrow B$ by $f' = u \circ f$.
   273 It is obvious that commutativity of $f$ implies commutativity of $f'$: $f' x y = u (f x y) = u (f y x) = f' y x.$
   274 \<close>
   275 
   276 lemma combine_infmatrix_commute:
   277   "commutative f \<Longrightarrow> commutative (combine_infmatrix f)"
   278 by (simp add: commutative_def combine_infmatrix_def)
   279 
   280 lemma combine_matrix_commute:
   281 "commutative f \<Longrightarrow> commutative (combine_matrix f)"
   282 by (simp add: combine_matrix_def commutative_def combine_infmatrix_def)
   283 
   284 text\<open>
   285 On the contrary, given an associative function $f$ we cannot expect $f'$ to be associative. A counterexample is given by $A=\ganz$, $B=\{-1, 0, 1\}$,
   286 as $f$ we take addition on $\ganz$, which is clearly associative. The abstraction is given by  $u(a) = 0$ for $a \notin B$. Then we have
   287 \[ f' (f' 1 1) -1 = u(f (u (f 1 1)) -1) = u(f (u 2) -1) = u (f 0 -1) = -1, \]
   288 but on the other hand we have
   289 \[ f' 1 (f' 1 -1) = u (f 1 (u (f 1 -1))) = u (f 1 0) = 1.\]
   290 A way out of this problem is to assume that $f(A\times A)\subset A$ holds, and this is what we are going to do:
   291 \<close>
   292 
   293 lemma nonzero_positions_combine_infmatrix[simp]: "f 0 0 = 0 \<Longrightarrow> nonzero_positions (combine_infmatrix f A B) \<subseteq> (nonzero_positions A) \<union> (nonzero_positions B)"
   294 by (rule subsetI, simp add: nonzero_positions_def combine_infmatrix_def, auto)
   295 
   296 lemma finite_nonzero_positions_Rep[simp]: "finite (nonzero_positions (Rep_matrix A))"
   297 by (insert Rep_matrix [of A], simp add: matrix_def)
   298 
   299 lemma combine_infmatrix_closed [simp]:
   300   "f 0 0 = 0 \<Longrightarrow> Rep_matrix (Abs_matrix (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) = combine_infmatrix f (Rep_matrix A) (Rep_matrix B)"
   301 apply (rule Abs_matrix_inverse)
   302 apply (simp add: matrix_def)
   303 apply (rule finite_subset[of _ "(nonzero_positions (Rep_matrix A)) \<union> (nonzero_positions (Rep_matrix B))"])
   304 by (simp_all)
   305 
   306 text \<open>We need the next two lemmas only later, but it is analog to the above one, so we prove them now:\<close>
   307 lemma nonzero_positions_apply_infmatrix[simp]: "f 0 = 0 \<Longrightarrow> nonzero_positions (apply_infmatrix f A) \<subseteq> nonzero_positions A"
   308 by (rule subsetI, simp add: nonzero_positions_def apply_infmatrix_def, auto)
   309 
   310 lemma apply_infmatrix_closed [simp]:
   311   "f 0 = 0 \<Longrightarrow> Rep_matrix (Abs_matrix (apply_infmatrix f (Rep_matrix A))) = apply_infmatrix f (Rep_matrix A)"
   312 apply (rule Abs_matrix_inverse)
   313 apply (simp add: matrix_def)
   314 apply (rule finite_subset[of _ "nonzero_positions (Rep_matrix A)"])
   315 by (simp_all)
   316 
   317 lemma combine_infmatrix_assoc[simp]: "f 0 0 = 0 \<Longrightarrow> associative f \<Longrightarrow> associative (combine_infmatrix f)"
   318 by (simp add: associative_def combine_infmatrix_def)
   319 
   320 lemma comb: "f = g \<Longrightarrow> x = y \<Longrightarrow> f x = g y"
   321 by (auto)
   322 
   323 lemma combine_matrix_assoc: "f 0 0 = 0 \<Longrightarrow> associative f \<Longrightarrow> associative (combine_matrix f)"
   324 apply (simp(no_asm) add: associative_def combine_matrix_def, auto)
   325 apply (rule comb [of Abs_matrix Abs_matrix])
   326 by (auto, insert combine_infmatrix_assoc[of f], simp add: associative_def)
   327 
   328 lemma Rep_apply_matrix[simp]: "f 0 = 0 \<Longrightarrow> Rep_matrix (apply_matrix f A) j i = f (Rep_matrix A j i)"
   329 by (simp add: apply_matrix_def)
   330 
   331 lemma Rep_combine_matrix[simp]: "f 0 0 = 0 \<Longrightarrow> Rep_matrix (combine_matrix f A B) j i = f (Rep_matrix A j i) (Rep_matrix B j i)"
   332   by(simp add: combine_matrix_def)
   333 
   334 lemma combine_nrows_max: "f 0 0 = 0  \<Longrightarrow> nrows (combine_matrix f A B) <= max (nrows A) (nrows B)"
   335 by (simp add: nrows_le)
   336 
   337 lemma combine_ncols_max: "f 0 0 = 0 \<Longrightarrow> ncols (combine_matrix f A B) <= max (ncols A) (ncols B)"
   338 by (simp add: ncols_le)
   339 
   340 lemma combine_nrows: "f 0 0 = 0 \<Longrightarrow> nrows A <= q \<Longrightarrow> nrows B <= q \<Longrightarrow> nrows(combine_matrix f A B) <= q"
   341   by (simp add: nrows_le)
   342 
   343 lemma combine_ncols: "f 0 0 = 0 \<Longrightarrow> ncols A <= q \<Longrightarrow> ncols B <= q \<Longrightarrow> ncols(combine_matrix f A B) <= q"
   344   by (simp add: ncols_le)
   345 
   346 definition zero_r_neutral :: "('a \<Rightarrow> 'b::zero \<Rightarrow> 'a) \<Rightarrow> bool" where
   347   "zero_r_neutral f == ! a. f a 0 = a"
   348 
   349 definition zero_l_neutral :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> bool" where
   350   "zero_l_neutral f == ! a. f 0 a = a"
   351 
   352 definition zero_closed :: "(('a::zero) \<Rightarrow> ('b::zero) \<Rightarrow> ('c::zero)) \<Rightarrow> bool" where
   353   "zero_closed f == (!x. f x 0 = 0) & (!y. f 0 y = 0)"
   354 
   355 primrec foldseq :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a"
   356 where
   357   "foldseq f s 0 = s 0"
   358 | "foldseq f s (Suc n) = f (s 0) (foldseq f (% k. s(Suc k)) n)"
   359 
   360 primrec foldseq_transposed ::  "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a"
   361 where
   362   "foldseq_transposed f s 0 = s 0"
   363 | "foldseq_transposed f s (Suc n) = f (foldseq_transposed f s n) (s (Suc n))"
   364 
   365 lemma foldseq_assoc : "associative f \<Longrightarrow> foldseq f = foldseq_transposed f"
   366 proof -
   367   assume a:"associative f"
   368   then have sublemma: "!! n. ! N s. N <= n \<longrightarrow> foldseq f s N = foldseq_transposed f s N"
   369   proof -
   370     fix n
   371     show "!N s. N <= n \<longrightarrow> foldseq f s N = foldseq_transposed f s N"
   372     proof (induct n)
   373       show "!N s. N <= 0 \<longrightarrow> foldseq f s N = foldseq_transposed f s N" by simp
   374     next
   375       fix n
   376       assume b:"! N s. N <= n \<longrightarrow> foldseq f s N = foldseq_transposed f s N"
   377       have c:"!!N s. N <= n \<Longrightarrow> foldseq f s N = foldseq_transposed f s N" by (simp add: b)
   378       show "! N t. N <= Suc n \<longrightarrow> foldseq f t N = foldseq_transposed f t N"
   379       proof (auto)
   380         fix N t
   381         assume Nsuc: "N <= Suc n"
   382         show "foldseq f t N = foldseq_transposed f t N"
   383         proof cases
   384           assume "N <= n"
   385           then show "foldseq f t N = foldseq_transposed f t N" by (simp add: b)
   386         next
   387           assume "~(N <= n)"
   388           with Nsuc have Nsuceq: "N = Suc n" by simp
   389           have neqz: "n \<noteq> 0 \<Longrightarrow> ? m. n = Suc m & Suc m <= n" by arith
   390           have assocf: "!! x y z. f x (f y z) = f (f x y) z" by (insert a, simp add: associative_def)
   391           show "foldseq f t N = foldseq_transposed f t N"
   392             apply (simp add: Nsuceq)
   393             apply (subst c)
   394             apply (simp)
   395             apply (case_tac "n = 0")
   396             apply (simp)
   397             apply (drule neqz)
   398             apply (erule exE)
   399             apply (simp)
   400             apply (subst assocf)
   401             proof -
   402               fix m
   403               assume "n = Suc m & Suc m <= n"
   404               then have mless: "Suc m <= n" by arith
   405               then have step1: "foldseq_transposed f (% k. t (Suc k)) m = foldseq f (% k. t (Suc k)) m" (is "?T1 = ?T2")
   406                 apply (subst c)
   407                 by simp+
   408               have step2: "f (t 0) ?T2 = foldseq f t (Suc m)" (is "_ = ?T3") by simp
   409               have step3: "?T3 = foldseq_transposed f t (Suc m)" (is "_ = ?T4")
   410                 apply (subst c)
   411                 by (simp add: mless)+
   412               have step4: "?T4 = f (foldseq_transposed f t m) (t (Suc m))" (is "_=?T5") by simp
   413               from step1 step2 step3 step4 show sowhat: "f (f (t 0) ?T1) (t (Suc (Suc m))) = f ?T5 (t (Suc (Suc m)))" by simp
   414             qed
   415           qed
   416         qed
   417       qed
   418     qed
   419     show "foldseq f = foldseq_transposed f" by ((rule ext)+, insert sublemma, auto)
   420   qed
   421 
   422 lemma foldseq_distr: "\<lbrakk>associative f; commutative f\<rbrakk> \<Longrightarrow> foldseq f (% k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n)"
   423 proof -
   424   assume assoc: "associative f"
   425   assume comm: "commutative f"
   426   from assoc have a:"!! x y z. f (f x y) z = f x (f y z)" by (simp add: associative_def)
   427   from comm have b: "!! x y. f x y = f y x" by (simp add: commutative_def)
   428   from assoc comm have c: "!! x y z. f x (f y z) = f y (f x z)" by (simp add: commutative_def associative_def)
   429   have "!! n. (! u v. foldseq f (%k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n))"
   430     apply (induct_tac n)
   431     apply (simp+, auto)
   432     by (simp add: a b c)
   433   then show "foldseq f (% k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n)" by simp
   434 qed
   435 
   436 theorem "\<lbrakk>associative f; associative g; \<forall>a b c d. g (f a b) (f c d) = f (g a c) (g b d); ? x y. (f x) \<noteq> (f y); ? x y. (g x) \<noteq> (g y); f x x = x; g x x = x\<rbrakk> \<Longrightarrow> f=g | (! y. f y x = y) | (! y. g y x = y)"
   437 oops
   438 (* Model found
   439 
   440 Trying to find a model that refutes: \<lbrakk>associative f; associative g;
   441  \<forall>a b c d. g (f a b) (f c d) = f (g a c) (g b d); \<exists>x y. f x \<noteq> f y;
   442  \<exists>x y. g x \<noteq> g y; f x x = x; g x x = x\<rbrakk>
   443 \<Longrightarrow> f = g \<or> (\<forall>y. f y x = y) \<or> (\<forall>y. g y x = y)
   444 Searching for a model of size 1, translating term... invoking SAT solver... no model found.
   445 Searching for a model of size 2, translating term... invoking SAT solver... no model found.
   446 Searching for a model of size 3, translating term... invoking SAT solver...
   447 Model found:
   448 Size of types: 'a: 3
   449 x: a1
   450 g: (a0\<mapsto>(a0\<mapsto>a1, a1\<mapsto>a0, a2\<mapsto>a1), a1\<mapsto>(a0\<mapsto>a0, a1\<mapsto>a1, a2\<mapsto>a0), a2\<mapsto>(a0\<mapsto>a1, a1\<mapsto>a0, a2\<mapsto>a1))
   451 f: (a0\<mapsto>(a0\<mapsto>a0, a1\<mapsto>a0, a2\<mapsto>a0), a1\<mapsto>(a0\<mapsto>a1, a1\<mapsto>a1, a2\<mapsto>a1), a2\<mapsto>(a0\<mapsto>a0, a1\<mapsto>a0, a2\<mapsto>a0))
   452 *)
   453 
   454 lemma foldseq_zero:
   455 assumes fz: "f 0 0 = 0" and sz: "! i. i <= n \<longrightarrow> s i = 0"
   456 shows "foldseq f s n = 0"
   457 proof -
   458   have "!! n. ! s. (! i. i <= n \<longrightarrow> s i = 0) \<longrightarrow> foldseq f s n = 0"
   459     apply (induct_tac n)
   460     apply (simp)
   461     by (simp add: fz)
   462   then show "foldseq f s n = 0" by (simp add: sz)
   463 qed
   464 
   465 lemma foldseq_significant_positions:
   466   assumes p: "! i. i <= N \<longrightarrow> S i = T i"
   467   shows "foldseq f S N = foldseq f T N"
   468 proof -
   469   have "!! m . ! s t. (! i. i<=m \<longrightarrow> s i = t i) \<longrightarrow> foldseq f s m = foldseq f t m"
   470     apply (induct_tac m)
   471     apply (simp)
   472     apply (simp)
   473     apply (auto)
   474     proof -
   475       fix n
   476       fix s::"nat\<Rightarrow>'a"
   477       fix t::"nat\<Rightarrow>'a"
   478       assume a: "\<forall>s t. (\<forall>i\<le>n. s i = t i) \<longrightarrow> foldseq f s n = foldseq f t n"
   479       assume b: "\<forall>i\<le>Suc n. s i = t i"
   480       have c:"!! a b. a = b \<Longrightarrow> f (t 0) a = f (t 0) b" by blast
   481       have d:"!! s t. (\<forall>i\<le>n. s i = t i) \<Longrightarrow> foldseq f s n = foldseq f t n" by (simp add: a)
   482       show "f (t 0) (foldseq f (\<lambda>k. s (Suc k)) n) = f (t 0) (foldseq f (\<lambda>k. t (Suc k)) n)" by (rule c, simp add: d b)
   483     qed
   484   with p show ?thesis by simp
   485 qed
   486 
   487 lemma foldseq_tail:
   488   assumes "M <= N"
   489   shows "foldseq f S N = foldseq f (% k. (if k < M then (S k) else (foldseq f (% k. S(k+M)) (N-M)))) M"
   490 proof -
   491   have suc: "!! a b. \<lbrakk>a <= Suc b; a \<noteq> Suc b\<rbrakk> \<Longrightarrow> a <= b" by arith
   492   have a:"!! a b c . a = b \<Longrightarrow> f c a = f c b" by blast
   493   have "!! n. ! m s. m <= n \<longrightarrow> foldseq f s n = foldseq f (% k. (if k < m then (s k) else (foldseq f (% k. s(k+m)) (n-m)))) m"
   494     apply (induct_tac n)
   495     apply (simp)
   496     apply (simp)
   497     apply (auto)
   498     apply (case_tac "m = Suc na")
   499     apply (simp)
   500     apply (rule a)
   501     apply (rule foldseq_significant_positions)
   502     apply (auto)
   503     apply (drule suc, simp+)
   504     proof -
   505       fix na m s
   506       assume suba:"\<forall>m\<le>na. \<forall>s. foldseq f s na = foldseq f (\<lambda>k. if k < m then s k else foldseq f (\<lambda>k. s (k + m)) (na - m))m"
   507       assume subb:"m <= na"
   508       from suba have subc:"!! m s. m <= na \<Longrightarrow>foldseq f s na = foldseq f (\<lambda>k. if k < m then s k else foldseq f (\<lambda>k. s (k + m)) (na - m))m" by simp
   509       have subd: "foldseq f (\<lambda>k. if k < m then s (Suc k) else foldseq f (\<lambda>k. s (Suc (k + m))) (na - m)) m =
   510         foldseq f (% k. s(Suc k)) na"
   511         by (rule subc[of m "% k. s(Suc k)", THEN sym], simp add: subb)
   512       from subb have sube: "m \<noteq> 0 \<Longrightarrow> ? mm. m = Suc mm & mm <= na" by arith
   513       show "f (s 0) (foldseq f (\<lambda>k. if k < m then s (Suc k) else foldseq f (\<lambda>k. s (Suc (k + m))) (na - m)) m) =
   514         foldseq f (\<lambda>k. if k < m then s k else foldseq f (\<lambda>k. s (k + m)) (Suc na - m)) m"
   515         apply (simp add: subd)
   516         apply (cases "m = 0")
   517          apply simp
   518         apply (drule sube)
   519         apply auto
   520         apply (rule a)
   521         apply (simp add: subc cong del: if_weak_cong)
   522         done
   523     qed
   524   then show ?thesis using assms by simp
   525 qed
   526 
   527 lemma foldseq_zerotail:
   528   assumes
   529   fz: "f 0 0 = 0"
   530   and sz: "! i.  n <= i \<longrightarrow> s i = 0"
   531   and nm: "n <= m"
   532   shows
   533   "foldseq f s n = foldseq f s m"
   534 proof -
   535   show "foldseq f s n = foldseq f s m"
   536     apply (simp add: foldseq_tail[OF nm, of f s])
   537     apply (rule foldseq_significant_positions)
   538     apply (auto)
   539     apply (subst foldseq_zero)
   540     by (simp add: fz sz)+
   541 qed
   542 
   543 lemma foldseq_zerotail2:
   544   assumes "! x. f x 0 = x"
   545   and "! i. n < i \<longrightarrow> s i = 0"
   546   and nm: "n <= m"
   547   shows "foldseq f s n = foldseq f s m"
   548 proof -
   549   have "f 0 0 = 0" by (simp add: assms)
   550   have b:"!! m n. n <= m \<Longrightarrow> m \<noteq> n \<Longrightarrow> ? k. m-n = Suc k" by arith
   551   have c: "0 <= m" by simp
   552   have d: "!! k. k \<noteq> 0 \<Longrightarrow> ? l. k = Suc l" by arith
   553   show ?thesis
   554     apply (subst foldseq_tail[OF nm])
   555     apply (rule foldseq_significant_positions)
   556     apply (auto)
   557     apply (case_tac "m=n")
   558     apply (simp+)
   559     apply (drule b[OF nm])
   560     apply (auto)
   561     apply (case_tac "k=0")
   562     apply (simp add: assms)
   563     apply (drule d)
   564     apply (auto)
   565     apply (simp add: assms foldseq_zero)
   566     done
   567 qed
   568 
   569 lemma foldseq_zerostart:
   570   "! x. f 0 (f 0 x) = f 0 x \<Longrightarrow>  ! i. i <= n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))"
   571 proof -
   572   assume f00x: "! x. f 0 (f 0 x) = f 0 x"
   573   have "! s. (! i. i<=n \<longrightarrow> s i = 0) \<longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))"
   574     apply (induct n)
   575     apply (simp)
   576     apply (rule allI, rule impI)
   577     proof -
   578       fix n
   579       fix s
   580       have a:"foldseq f s (Suc (Suc n)) = f (s 0) (foldseq f (% k. s(Suc k)) (Suc n))" by simp
   581       assume b: "! s. ((\<forall>i\<le>n. s i = 0) \<longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n)))"
   582       from b have c:"!! s. (\<forall>i\<le>n. s i = 0) \<Longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))" by simp
   583       assume d: "! i. i <= Suc n \<longrightarrow> s i = 0"
   584       show "foldseq f s (Suc (Suc n)) = f 0 (s (Suc (Suc n)))"
   585         apply (subst a)
   586         apply (subst c)
   587         by (simp add: d f00x)+
   588     qed
   589   then show "! i. i <= n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s (Suc n) = f 0 (s (Suc n))" by simp
   590 qed
   591 
   592 lemma foldseq_zerostart2:
   593   "! x. f 0 x = x \<Longrightarrow>  ! i. i < n \<longrightarrow> s i = 0 \<Longrightarrow> foldseq f s n = s n"
   594 proof -
   595   assume a:"! i. i<n \<longrightarrow> s i = 0"
   596   assume x:"! x. f 0 x = x"
   597   from x have f00x: "! x. f 0 (f 0 x) = f 0 x" by blast
   598   have b: "!! i l. i < Suc l = (i <= l)" by arith
   599   have d: "!! k. k \<noteq> 0 \<Longrightarrow> ? l. k = Suc l" by arith
   600   show "foldseq f s n = s n"
   601   apply (case_tac "n=0")
   602   apply (simp)
   603   apply (insert a)
   604   apply (drule d)
   605   apply (auto)
   606   apply (simp add: b)
   607   apply (insert f00x)
   608   apply (drule foldseq_zerostart)
   609   by (simp add: x)+
   610 qed
   611 
   612 lemma foldseq_almostzero:
   613   assumes f0x:"! x. f 0 x = x" and fx0: "! x. f x 0 = x" and s0:"! i. i \<noteq> j \<longrightarrow> s i = 0"
   614   shows "foldseq f s n = (if (j <= n) then (s j) else 0)"
   615 proof -
   616   from s0 have a: "! i. i < j \<longrightarrow> s i = 0" by simp
   617   from s0 have b: "! i. j < i \<longrightarrow> s i = 0" by simp
   618   show ?thesis
   619     apply auto
   620     apply (subst foldseq_zerotail2[of f, OF fx0, of j, OF b, of n, THEN sym])
   621     apply simp
   622     apply (subst foldseq_zerostart2)
   623     apply (simp add: f0x a)+
   624     apply (subst foldseq_zero)
   625     by (simp add: s0 f0x)+
   626 qed
   627 
   628 lemma foldseq_distr_unary:
   629   assumes "!! a b. g (f a b) = f (g a) (g b)"
   630   shows "g(foldseq f s n) = foldseq f (% x. g(s x)) n"
   631 proof -
   632   have "! s. g(foldseq f s n) = foldseq f (% x. g(s x)) n"
   633     apply (induct_tac n)
   634     apply (simp)
   635     apply (simp)
   636     apply (auto)
   637     apply (drule_tac x="% k. s (Suc k)" in spec)
   638     by (simp add: assms)
   639   then show ?thesis by simp
   640 qed
   641 
   642 definition mult_matrix_n :: "nat \<Rightarrow> (('a::zero) \<Rightarrow> ('b::zero) \<Rightarrow> ('c::zero)) \<Rightarrow> ('c \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> 'a matrix \<Rightarrow> 'b matrix \<Rightarrow> 'c matrix" where
   643   "mult_matrix_n n fmul fadd A B == Abs_matrix(% j i. foldseq fadd (% k. fmul (Rep_matrix A j k) (Rep_matrix B k i)) n)"
   644 
   645 definition mult_matrix :: "(('a::zero) \<Rightarrow> ('b::zero) \<Rightarrow> ('c::zero)) \<Rightarrow> ('c \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> 'a matrix \<Rightarrow> 'b matrix \<Rightarrow> 'c matrix" where
   646   "mult_matrix fmul fadd A B == mult_matrix_n (max (ncols A) (nrows B)) fmul fadd A B"
   647 
   648 lemma mult_matrix_n:
   649   assumes "ncols A \<le>  n" (is ?An) "nrows B \<le> n" (is ?Bn) "fadd 0 0 = 0" "fmul 0 0 = 0"
   650   shows c:"mult_matrix fmul fadd A B = mult_matrix_n n fmul fadd A B"
   651 proof -
   652   show ?thesis using assms
   653     apply (simp add: mult_matrix_def mult_matrix_n_def)
   654     apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
   655     apply (rule foldseq_zerotail, simp_all add: nrows_le ncols_le assms)
   656     done
   657 qed
   658 
   659 lemma mult_matrix_nm:
   660   assumes "ncols A <= n" "nrows B <= n" "ncols A <= m" "nrows B <= m" "fadd 0 0 = 0" "fmul 0 0 = 0"
   661   shows "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B"
   662 proof -
   663   from assms have "mult_matrix_n n fmul fadd A B = mult_matrix fmul fadd A B"
   664     by (simp add: mult_matrix_n)
   665   also from assms have "\<dots> = mult_matrix_n m fmul fadd A B"
   666     by (simp add: mult_matrix_n[THEN sym])
   667   finally show "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B" by simp
   668 qed
   669 
   670 definition r_distributive :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> bool" where
   671   "r_distributive fmul fadd == ! a u v. fmul a (fadd u v) = fadd (fmul a u) (fmul a v)"
   672 
   673 definition l_distributive :: "('a \<Rightarrow> 'b \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool" where
   674   "l_distributive fmul fadd == ! a u v. fmul (fadd u v) a = fadd (fmul u a) (fmul v a)"
   675 
   676 definition distributive :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> bool" where
   677   "distributive fmul fadd == l_distributive fmul fadd & r_distributive fmul fadd"
   678 
   679 lemma max1: "!! a x y. (a::nat) <= x \<Longrightarrow> a <= max x y" by (arith)
   680 lemma max2: "!! b x y. (b::nat) <= y \<Longrightarrow> b <= max x y" by (arith)
   681 
   682 lemma r_distributive_matrix:
   683  assumes
   684   "r_distributive fmul fadd"
   685   "associative fadd"
   686   "commutative fadd"
   687   "fadd 0 0 = 0"
   688   "! a. fmul a 0 = 0"
   689   "! a. fmul 0 a = 0"
   690  shows "r_distributive (mult_matrix fmul fadd) (combine_matrix fadd)"
   691 proof -
   692   from assms show ?thesis
   693     apply (simp add: r_distributive_def mult_matrix_def, auto)
   694     proof -
   695       fix a::"'a matrix"
   696       fix u::"'b matrix"
   697       fix v::"'b matrix"
   698       let ?mx = "max (ncols a) (max (nrows u) (nrows v))"
   699       from assms show "mult_matrix_n (max (ncols a) (nrows (combine_matrix fadd u v))) fmul fadd a (combine_matrix fadd u v) =
   700         combine_matrix fadd (mult_matrix_n (max (ncols a) (nrows u)) fmul fadd a u) (mult_matrix_n (max (ncols a) (nrows v)) fmul fadd a v)"
   701         apply (subst mult_matrix_nm[of _ _ _ ?mx fadd fmul])
   702         apply (simp add: max1 max2 combine_nrows combine_ncols)+
   703         apply (subst mult_matrix_nm[of _ _ v ?mx fadd fmul])
   704         apply (simp add: max1 max2 combine_nrows combine_ncols)+
   705         apply (subst mult_matrix_nm[of _ _ u ?mx fadd fmul])
   706         apply (simp add: max1 max2 combine_nrows combine_ncols)+
   707         apply (simp add: mult_matrix_n_def r_distributive_def foldseq_distr[of fadd])
   708         apply (simp add: combine_matrix_def combine_infmatrix_def)
   709         apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
   710         apply (simplesubst RepAbs_matrix)
   711         apply (simp, auto)
   712         apply (rule exI[of _ "nrows a"], simp add: nrows_le foldseq_zero)
   713         apply (rule exI[of _ "ncols v"], simp add: ncols_le foldseq_zero)
   714         apply (subst RepAbs_matrix)
   715         apply (simp, auto)
   716         apply (rule exI[of _ "nrows a"], simp add: nrows_le foldseq_zero)
   717         apply (rule exI[of _ "ncols u"], simp add: ncols_le foldseq_zero)
   718         done
   719     qed
   720 qed
   721 
   722 lemma l_distributive_matrix:
   723  assumes
   724   "l_distributive fmul fadd"
   725   "associative fadd"
   726   "commutative fadd"
   727   "fadd 0 0 = 0"
   728   "! a. fmul a 0 = 0"
   729   "! a. fmul 0 a = 0"
   730  shows "l_distributive (mult_matrix fmul fadd) (combine_matrix fadd)"
   731 proof -
   732   from assms show ?thesis
   733     apply (simp add: l_distributive_def mult_matrix_def, auto)
   734     proof -
   735       fix a::"'b matrix"
   736       fix u::"'a matrix"
   737       fix v::"'a matrix"
   738       let ?mx = "max (nrows a) (max (ncols u) (ncols v))"
   739       from assms show "mult_matrix_n (max (ncols (combine_matrix fadd u v)) (nrows a)) fmul fadd (combine_matrix fadd u v) a =
   740                combine_matrix fadd (mult_matrix_n (max (ncols u) (nrows a)) fmul fadd u a) (mult_matrix_n (max (ncols v) (nrows a)) fmul fadd v a)"
   741         apply (subst mult_matrix_nm[of v _ _ ?mx fadd fmul])
   742         apply (simp add: max1 max2 combine_nrows combine_ncols)+
   743         apply (subst mult_matrix_nm[of u _ _ ?mx fadd fmul])
   744         apply (simp add: max1 max2 combine_nrows combine_ncols)+
   745         apply (subst mult_matrix_nm[of _ _ _ ?mx fadd fmul])
   746         apply (simp add: max1 max2 combine_nrows combine_ncols)+
   747         apply (simp add: mult_matrix_n_def l_distributive_def foldseq_distr[of fadd])
   748         apply (simp add: combine_matrix_def combine_infmatrix_def)
   749         apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
   750         apply (simplesubst RepAbs_matrix)
   751         apply (simp, auto)
   752         apply (rule exI[of _ "nrows v"], simp add: nrows_le foldseq_zero)
   753         apply (rule exI[of _ "ncols a"], simp add: ncols_le foldseq_zero)
   754         apply (subst RepAbs_matrix)
   755         apply (simp, auto)
   756         apply (rule exI[of _ "nrows u"], simp add: nrows_le foldseq_zero)
   757         apply (rule exI[of _ "ncols a"], simp add: ncols_le foldseq_zero)
   758         done
   759     qed
   760 qed
   761 
   762 instantiation matrix :: (zero) zero
   763 begin
   764 
   765 definition zero_matrix_def: "0 = Abs_matrix (\<lambda>j i. 0)"
   766 
   767 instance ..
   768 
   769 end
   770 
   771 lemma Rep_zero_matrix_def[simp]: "Rep_matrix 0 j i = 0"
   772   apply (simp add: zero_matrix_def)
   773   apply (subst RepAbs_matrix)
   774   by (auto)
   775 
   776 lemma zero_matrix_def_nrows[simp]: "nrows 0 = 0"
   777 proof -
   778   have a:"!! (x::nat). x <= 0 \<Longrightarrow> x = 0" by (arith)
   779   show "nrows 0 = 0" by (rule a, subst nrows_le, simp)
   780 qed
   781 
   782 lemma zero_matrix_def_ncols[simp]: "ncols 0 = 0"
   783 proof -
   784   have a:"!! (x::nat). x <= 0 \<Longrightarrow> x = 0" by (arith)
   785   show "ncols 0 = 0" by (rule a, subst ncols_le, simp)
   786 qed
   787 
   788 lemma combine_matrix_zero_l_neutral: "zero_l_neutral f \<Longrightarrow> zero_l_neutral (combine_matrix f)"
   789   by (simp add: zero_l_neutral_def combine_matrix_def combine_infmatrix_def)
   790 
   791 lemma combine_matrix_zero_r_neutral: "zero_r_neutral f \<Longrightarrow> zero_r_neutral (combine_matrix f)"
   792   by (simp add: zero_r_neutral_def combine_matrix_def combine_infmatrix_def)
   793 
   794 lemma mult_matrix_zero_closed: "\<lbrakk>fadd 0 0 = 0; zero_closed fmul\<rbrakk> \<Longrightarrow> zero_closed (mult_matrix fmul fadd)"
   795   apply (simp add: zero_closed_def mult_matrix_def mult_matrix_n_def)
   796   apply (auto)
   797   by (subst foldseq_zero, (simp add: zero_matrix_def)+)+
   798 
   799 lemma mult_matrix_n_zero_right[simp]: "\<lbrakk>fadd 0 0 = 0; !a. fmul a 0 = 0\<rbrakk> \<Longrightarrow> mult_matrix_n n fmul fadd A 0 = 0"
   800   apply (simp add: mult_matrix_n_def)
   801   apply (subst foldseq_zero)
   802   by (simp_all add: zero_matrix_def)
   803 
   804 lemma mult_matrix_n_zero_left[simp]: "\<lbrakk>fadd 0 0 = 0; !a. fmul 0 a = 0\<rbrakk> \<Longrightarrow> mult_matrix_n n fmul fadd 0 A = 0"
   805   apply (simp add: mult_matrix_n_def)
   806   apply (subst foldseq_zero)
   807   by (simp_all add: zero_matrix_def)
   808 
   809 lemma mult_matrix_zero_left[simp]: "\<lbrakk>fadd 0 0 = 0; !a. fmul 0 a = 0\<rbrakk> \<Longrightarrow> mult_matrix fmul fadd 0 A = 0"
   810 by (simp add: mult_matrix_def)
   811 
   812 lemma mult_matrix_zero_right[simp]: "\<lbrakk>fadd 0 0 = 0; !a. fmul a 0 = 0\<rbrakk> \<Longrightarrow> mult_matrix fmul fadd A 0 = 0"
   813 by (simp add: mult_matrix_def)
   814 
   815 lemma apply_matrix_zero[simp]: "f 0 = 0 \<Longrightarrow> apply_matrix f 0 = 0"
   816   apply (simp add: apply_matrix_def apply_infmatrix_def)
   817   by (simp add: zero_matrix_def)
   818 
   819 lemma combine_matrix_zero: "f 0 0 = 0 \<Longrightarrow> combine_matrix f 0 0 = 0"
   820   apply (simp add: combine_matrix_def combine_infmatrix_def)
   821   by (simp add: zero_matrix_def)
   822 
   823 lemma transpose_matrix_zero[simp]: "transpose_matrix 0 = 0"
   824 apply (simp add: transpose_matrix_def zero_matrix_def RepAbs_matrix)
   825 apply (subst Rep_matrix_inject[symmetric], (rule ext)+)
   826 apply (simp add: RepAbs_matrix)
   827 done
   828 
   829 lemma apply_zero_matrix_def[simp]: "apply_matrix (% x. 0) A = 0"
   830   apply (simp add: apply_matrix_def apply_infmatrix_def)
   831   by (simp add: zero_matrix_def)
   832 
   833 definition singleton_matrix :: "nat \<Rightarrow> nat \<Rightarrow> ('a::zero) \<Rightarrow> 'a matrix" where
   834   "singleton_matrix j i a == Abs_matrix(% m n. if j = m & i = n then a else 0)"
   835 
   836 definition move_matrix :: "('a::zero) matrix \<Rightarrow> int \<Rightarrow> int \<Rightarrow> 'a matrix" where
   837   "move_matrix A y x == Abs_matrix(% j i. if (((int j)-y) < 0) | (((int i)-x) < 0) then 0 else Rep_matrix A (nat ((int j)-y)) (nat ((int i)-x)))"
   838 
   839 definition take_rows :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix" where
   840   "take_rows A r == Abs_matrix(% j i. if (j < r) then (Rep_matrix A j i) else 0)"
   841 
   842 definition take_columns :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix" where
   843   "take_columns A c == Abs_matrix(% j i. if (i < c) then (Rep_matrix A j i) else 0)"
   844 
   845 definition column_of_matrix :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix" where
   846   "column_of_matrix A n == take_columns (move_matrix A 0 (- int n)) 1"
   847 
   848 definition row_of_matrix :: "('a::zero) matrix \<Rightarrow> nat \<Rightarrow> 'a matrix" where
   849   "row_of_matrix A m == take_rows (move_matrix A (- int m) 0) 1"
   850 
   851 lemma Rep_singleton_matrix[simp]: "Rep_matrix (singleton_matrix j i e) m n = (if j = m & i = n then e else 0)"
   852 apply (simp add: singleton_matrix_def)
   853 apply (auto)
   854 apply (subst RepAbs_matrix)
   855 apply (rule exI[of _ "Suc m"], simp)
   856 apply (rule exI[of _ "Suc n"], simp+)
   857 by (subst RepAbs_matrix, rule exI[of _ "Suc j"], simp, rule exI[of _ "Suc i"], simp+)+
   858 
   859 lemma apply_singleton_matrix[simp]: "f 0 = 0 \<Longrightarrow> apply_matrix f (singleton_matrix j i x) = (singleton_matrix j i (f x))"
   860 apply (subst Rep_matrix_inject[symmetric])
   861 apply (rule ext)+
   862 apply (simp)
   863 done
   864 
   865 lemma singleton_matrix_zero[simp]: "singleton_matrix j i 0 = 0"
   866   by (simp add: singleton_matrix_def zero_matrix_def)
   867 
   868 lemma nrows_singleton[simp]: "nrows(singleton_matrix j i e) = (if e = 0 then 0 else Suc j)"
   869 proof-
   870 have th: "\<not> (\<forall>m. m \<le> j)" "\<exists>n. \<not> n \<le> i" by arith+
   871 from th show ?thesis 
   872 apply (auto)
   873 apply (rule le_antisym)
   874 apply (subst nrows_le)
   875 apply (simp add: singleton_matrix_def, auto)
   876 apply (subst RepAbs_matrix)
   877 apply auto
   878 apply (simp add: Suc_le_eq)
   879 apply (rule not_le_imp_less)
   880 apply (subst nrows_le)
   881 by simp
   882 qed
   883 
   884 lemma ncols_singleton[simp]: "ncols(singleton_matrix j i e) = (if e = 0 then 0 else Suc i)"
   885 proof-
   886 have th: "\<not> (\<forall>m. m \<le> j)" "\<exists>n. \<not> n \<le> i" by arith+
   887 from th show ?thesis 
   888 apply (auto)
   889 apply (rule le_antisym)
   890 apply (subst ncols_le)
   891 apply (simp add: singleton_matrix_def, auto)
   892 apply (subst RepAbs_matrix)
   893 apply auto
   894 apply (simp add: Suc_le_eq)
   895 apply (rule not_le_imp_less)
   896 apply (subst ncols_le)
   897 by simp
   898 qed
   899 
   900 lemma combine_singleton: "f 0 0 = 0 \<Longrightarrow> combine_matrix f (singleton_matrix j i a) (singleton_matrix j i b) = singleton_matrix j i (f a b)"
   901 apply (simp add: singleton_matrix_def combine_matrix_def combine_infmatrix_def)
   902 apply (subst RepAbs_matrix)
   903 apply (rule exI[of _ "Suc j"], simp)
   904 apply (rule exI[of _ "Suc i"], simp)
   905 apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
   906 apply (subst RepAbs_matrix)
   907 apply (rule exI[of _ "Suc j"], simp)
   908 apply (rule exI[of _ "Suc i"], simp)
   909 by simp
   910 
   911 lemma transpose_singleton[simp]: "transpose_matrix (singleton_matrix j i a) = singleton_matrix i j a"
   912 apply (subst Rep_matrix_inject[symmetric], (rule ext)+)
   913 apply (simp)
   914 done
   915 
   916 lemma Rep_move_matrix[simp]:
   917   "Rep_matrix (move_matrix A y x) j i =
   918   (if (((int j)-y) < 0) | (((int i)-x) < 0) then 0 else Rep_matrix A (nat((int j)-y)) (nat((int i)-x)))"
   919 apply (simp add: move_matrix_def)
   920 apply (auto)
   921 by (subst RepAbs_matrix,
   922   rule exI[of _ "(nrows A)+(nat \<bar>y\<bar>)"], auto, rule nrows, arith,
   923   rule exI[of _ "(ncols A)+(nat \<bar>x\<bar>)"], auto, rule ncols, arith)+
   924 
   925 lemma move_matrix_0_0[simp]: "move_matrix A 0 0 = A"
   926 by (simp add: move_matrix_def)
   927 
   928 lemma move_matrix_ortho: "move_matrix A j i = move_matrix (move_matrix A j 0) 0 i"
   929 apply (subst Rep_matrix_inject[symmetric])
   930 apply (rule ext)+
   931 apply (simp)
   932 done
   933 
   934 lemma transpose_move_matrix[simp]:
   935   "transpose_matrix (move_matrix A x y) = move_matrix (transpose_matrix A) y x"
   936 apply (subst Rep_matrix_inject[symmetric], (rule ext)+)
   937 apply (simp)
   938 done
   939 
   940 lemma move_matrix_singleton[simp]: "move_matrix (singleton_matrix u v x) j i = 
   941   (if (j + int u < 0) | (i + int v < 0) then 0 else (singleton_matrix (nat (j + int u)) (nat (i + int v)) x))"
   942   apply (subst Rep_matrix_inject[symmetric])
   943   apply (rule ext)+
   944   apply (case_tac "j + int u < 0")
   945   apply (simp, arith)
   946   apply (case_tac "i + int v < 0")
   947   apply (simp, arith)
   948   apply simp
   949   apply arith
   950   done
   951 
   952 lemma Rep_take_columns[simp]:
   953   "Rep_matrix (take_columns A c) j i =
   954   (if i < c then (Rep_matrix A j i) else 0)"
   955 apply (simp add: take_columns_def)
   956 apply (simplesubst RepAbs_matrix)
   957 apply (rule exI[of _ "nrows A"], auto, simp add: nrows_le)
   958 apply (rule exI[of _ "ncols A"], auto, simp add: ncols_le)
   959 done
   960 
   961 lemma Rep_take_rows[simp]:
   962   "Rep_matrix (take_rows A r) j i =
   963   (if j < r then (Rep_matrix A j i) else 0)"
   964 apply (simp add: take_rows_def)
   965 apply (simplesubst RepAbs_matrix)
   966 apply (rule exI[of _ "nrows A"], auto, simp add: nrows_le)
   967 apply (rule exI[of _ "ncols A"], auto, simp add: ncols_le)
   968 done
   969 
   970 lemma Rep_column_of_matrix[simp]:
   971   "Rep_matrix (column_of_matrix A c) j i = (if i = 0 then (Rep_matrix A j c) else 0)"
   972   by (simp add: column_of_matrix_def)
   973 
   974 lemma Rep_row_of_matrix[simp]:
   975   "Rep_matrix (row_of_matrix A r) j i = (if j = 0 then (Rep_matrix A r i) else 0)"
   976   by (simp add: row_of_matrix_def)
   977 
   978 lemma column_of_matrix: "ncols A <= n \<Longrightarrow> column_of_matrix A n = 0"
   979 apply (subst Rep_matrix_inject[THEN sym])
   980 apply (rule ext)+
   981 by (simp add: ncols)
   982 
   983 lemma row_of_matrix: "nrows A <= n \<Longrightarrow> row_of_matrix A n = 0"
   984 apply (subst Rep_matrix_inject[THEN sym])
   985 apply (rule ext)+
   986 by (simp add: nrows)
   987 
   988 lemma mult_matrix_singleton_right[simp]:
   989   assumes
   990   "! x. fmul x 0 = 0"
   991   "! x. fmul 0 x = 0"
   992   "! x. fadd 0 x = x"
   993   "! x. fadd x 0 = x"
   994   shows "(mult_matrix fmul fadd A (singleton_matrix j i e)) = apply_matrix (% x. fmul x e) (move_matrix (column_of_matrix A j) 0 (int i))"
   995   apply (simp add: mult_matrix_def)
   996   apply (subst mult_matrix_nm[of _ _ _ "max (ncols A) (Suc j)"])
   997   apply (auto)
   998   apply (simp add: assms)+
   999   apply (simp add: mult_matrix_n_def apply_matrix_def apply_infmatrix_def)
  1000   apply (rule comb[of "Abs_matrix" "Abs_matrix"], auto, (rule ext)+)
  1001   apply (subst foldseq_almostzero[of _ j])
  1002   apply (simp add: assms)+
  1003   apply (auto)
  1004   done
  1005 
  1006 lemma mult_matrix_ext:
  1007   assumes
  1008   eprem:
  1009   "? e. (! a b. a \<noteq> b \<longrightarrow> fmul a e \<noteq> fmul b e)"
  1010   and fprems:
  1011   "! a. fmul 0 a = 0"
  1012   "! a. fmul a 0 = 0"
  1013   "! a. fadd a 0 = a"
  1014   "! a. fadd 0 a = a"
  1015   and contraprems:
  1016   "mult_matrix fmul fadd A = mult_matrix fmul fadd B"
  1017   shows
  1018   "A = B"
  1019 proof(rule contrapos_np[of "False"], simp)
  1020   assume a: "A \<noteq> B"
  1021   have b: "!! f g. (! x y. f x y = g x y) \<Longrightarrow> f = g" by ((rule ext)+, auto)
  1022   have "? j i. (Rep_matrix A j i) \<noteq> (Rep_matrix B j i)"
  1023     apply (rule contrapos_np[of "False"], simp+)
  1024     apply (insert b[of "Rep_matrix A" "Rep_matrix B"], simp)
  1025     by (simp add: Rep_matrix_inject a)
  1026   then obtain J I where c:"(Rep_matrix A J I) \<noteq> (Rep_matrix B J I)" by blast
  1027   from eprem obtain e where eprops:"(! a b. a \<noteq> b \<longrightarrow> fmul a e \<noteq> fmul b e)" by blast
  1028   let ?S = "singleton_matrix I 0 e"
  1029   let ?comp = "mult_matrix fmul fadd"
  1030   have d: "!!x f g. f = g \<Longrightarrow> f x = g x" by blast
  1031   have e: "(% x. fmul x e) 0 = 0" by (simp add: assms)
  1032   have "~(?comp A ?S = ?comp B ?S)"
  1033     apply (rule notI)
  1034     apply (simp add: fprems eprops)
  1035     apply (simp add: Rep_matrix_inject[THEN sym])
  1036     apply (drule d[of _ _ "J"], drule d[of _ _ "0"])
  1037     by (simp add: e c eprops)
  1038   with contraprems show "False" by simp
  1039 qed
  1040 
  1041 definition foldmatrix :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a infmatrix) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a" where
  1042   "foldmatrix f g A m n == foldseq_transposed g (% j. foldseq f (A j) n) m"
  1043 
  1044 definition foldmatrix_transposed :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ('a infmatrix) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a" where
  1045   "foldmatrix_transposed f g A m n == foldseq g (% j. foldseq_transposed f (A j) n) m"
  1046 
  1047 lemma foldmatrix_transpose:
  1048   assumes
  1049   "! a b c d. g(f a b) (f c d) = f (g a c) (g b d)"
  1050   shows
  1051   "foldmatrix f g A m n = foldmatrix_transposed g f (transpose_infmatrix A) n m"
  1052 proof -
  1053   have forall:"!! P x. (! x. P x) \<Longrightarrow> P x" by auto
  1054   have tworows:"! A. foldmatrix f g A 1 n = foldmatrix_transposed g f (transpose_infmatrix A) n 1"
  1055     apply (induct n)
  1056     apply (simp add: foldmatrix_def foldmatrix_transposed_def assms)+
  1057     apply (auto)
  1058     by (drule_tac x="(% j i. A j (Suc i))" in forall, simp)
  1059   show "foldmatrix f g A m n = foldmatrix_transposed g f (transpose_infmatrix A) n m"
  1060     apply (simp add: foldmatrix_def foldmatrix_transposed_def)
  1061     apply (induct m, simp)
  1062     apply (simp)
  1063     apply (insert tworows)
  1064     apply (drule_tac x="% j i. (if j = 0 then (foldseq_transposed g (\<lambda>u. A u i) m) else (A (Suc m) i))" in spec)
  1065     by (simp add: foldmatrix_def foldmatrix_transposed_def)
  1066 qed
  1067 
  1068 lemma foldseq_foldseq:
  1069 assumes
  1070   "associative f"
  1071   "associative g"
  1072   "! a b c d. g(f a b) (f c d) = f (g a c) (g b d)"
  1073 shows
  1074   "foldseq g (% j. foldseq f (A j) n) m = foldseq f (% j. foldseq g ((transpose_infmatrix A) j) m) n"
  1075   apply (insert foldmatrix_transpose[of g f A m n])
  1076   by (simp add: foldmatrix_def foldmatrix_transposed_def foldseq_assoc[THEN sym] assms)
  1077 
  1078 lemma mult_n_nrows:
  1079 assumes
  1080 "! a. fmul 0 a = 0"
  1081 "! a. fmul a 0 = 0"
  1082 "fadd 0 0 = 0"
  1083 shows "nrows (mult_matrix_n n fmul fadd A B) \<le> nrows A"
  1084 apply (subst nrows_le)
  1085 apply (simp add: mult_matrix_n_def)
  1086 apply (subst RepAbs_matrix)
  1087 apply (rule_tac x="nrows A" in exI)
  1088 apply (simp add: nrows assms foldseq_zero)
  1089 apply (rule_tac x="ncols B" in exI)
  1090 apply (simp add: ncols assms foldseq_zero)
  1091 apply (simp add: nrows assms foldseq_zero)
  1092 done
  1093 
  1094 lemma mult_n_ncols:
  1095 assumes
  1096 "! a. fmul 0 a = 0"
  1097 "! a. fmul a 0 = 0"
  1098 "fadd 0 0 = 0"
  1099 shows "ncols (mult_matrix_n n fmul fadd A B) \<le> ncols B"
  1100 apply (subst ncols_le)
  1101 apply (simp add: mult_matrix_n_def)
  1102 apply (subst RepAbs_matrix)
  1103 apply (rule_tac x="nrows A" in exI)
  1104 apply (simp add: nrows assms foldseq_zero)
  1105 apply (rule_tac x="ncols B" in exI)
  1106 apply (simp add: ncols assms foldseq_zero)
  1107 apply (simp add: ncols assms foldseq_zero)
  1108 done
  1109 
  1110 lemma mult_nrows:
  1111 assumes
  1112 "! a. fmul 0 a = 0"
  1113 "! a. fmul a 0 = 0"
  1114 "fadd 0 0 = 0"
  1115 shows "nrows (mult_matrix fmul fadd A B) \<le> nrows A"
  1116 by (simp add: mult_matrix_def mult_n_nrows assms)
  1117 
  1118 lemma mult_ncols:
  1119 assumes
  1120 "! a. fmul 0 a = 0"
  1121 "! a. fmul a 0 = 0"
  1122 "fadd 0 0 = 0"
  1123 shows "ncols (mult_matrix fmul fadd A B) \<le> ncols B"
  1124 by (simp add: mult_matrix_def mult_n_ncols assms)
  1125 
  1126 lemma nrows_move_matrix_le: "nrows (move_matrix A j i) <= nat((int (nrows A)) + j)"
  1127   apply (auto simp add: nrows_le)
  1128   apply (rule nrows)
  1129   apply (arith)
  1130   done
  1131 
  1132 lemma ncols_move_matrix_le: "ncols (move_matrix A j i) <= nat((int (ncols A)) + i)"
  1133   apply (auto simp add: ncols_le)
  1134   apply (rule ncols)
  1135   apply (arith)
  1136   done
  1137 
  1138 lemma mult_matrix_assoc:
  1139   assumes
  1140   "! a. fmul1 0 a = 0"
  1141   "! a. fmul1 a 0 = 0"
  1142   "! a. fmul2 0 a = 0"
  1143   "! a. fmul2 a 0 = 0"
  1144   "fadd1 0 0 = 0"
  1145   "fadd2 0 0 = 0"
  1146   "! a b c d. fadd2 (fadd1 a b) (fadd1 c d) = fadd1 (fadd2 a c) (fadd2 b d)"
  1147   "associative fadd1"
  1148   "associative fadd2"
  1149   "! a b c. fmul2 (fmul1 a b) c = fmul1 a (fmul2 b c)"
  1150   "! a b c. fmul2 (fadd1 a b) c = fadd1 (fmul2 a c) (fmul2 b c)"
  1151   "! a b c. fmul1 c (fadd2 a b) = fadd2 (fmul1 c a) (fmul1 c b)"
  1152   shows "mult_matrix fmul2 fadd2 (mult_matrix fmul1 fadd1 A B) C = mult_matrix fmul1 fadd1 A (mult_matrix fmul2 fadd2 B C)"
  1153 proof -
  1154   have comb_left:  "!! A B x y. A = B \<Longrightarrow> (Rep_matrix (Abs_matrix A)) x y = (Rep_matrix(Abs_matrix B)) x y" by blast
  1155   have fmul2fadd1fold: "!! x s n. fmul2 (foldseq fadd1 s n)  x = foldseq fadd1 (% k. fmul2 (s k) x) n"
  1156     by (rule_tac g1 = "% y. fmul2 y x" in ssubst [OF foldseq_distr_unary], insert assms, simp_all)
  1157   have fmul1fadd2fold: "!! x s n. fmul1 x (foldseq fadd2 s n) = foldseq fadd2 (% k. fmul1 x (s k)) n"
  1158     using assms by (rule_tac g1 = "% y. fmul1 x y" in ssubst [OF foldseq_distr_unary], simp_all)
  1159   let ?N = "max (ncols A) (max (ncols B) (max (nrows B) (nrows C)))"
  1160   show ?thesis
  1161     apply (simp add: Rep_matrix_inject[THEN sym])
  1162     apply (rule ext)+
  1163     apply (simp add: mult_matrix_def)
  1164     apply (simplesubst mult_matrix_nm[of _ "max (ncols (mult_matrix_n (max (ncols A) (nrows B)) fmul1 fadd1 A B)) (nrows C)" _ "max (ncols B) (nrows C)"])
  1165     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+
  1166     apply (simplesubst mult_matrix_nm[of _ "max (ncols A) (nrows (mult_matrix_n (max (ncols B) (nrows C)) fmul2 fadd2 B C))" _ "max (ncols A) (nrows B)"])
  1167     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+
  1168     apply (simplesubst mult_matrix_nm[of _ _ _ "?N"])
  1169     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+
  1170     apply (simplesubst mult_matrix_nm[of _ _ _ "?N"])
  1171     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+
  1172     apply (simplesubst mult_matrix_nm[of _ _ _ "?N"])
  1173     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+
  1174     apply (simplesubst mult_matrix_nm[of _ _ _ "?N"])
  1175     apply (simp add: max1 max2 mult_n_ncols mult_n_nrows assms)+
  1176     apply (simp add: mult_matrix_n_def)
  1177     apply (rule comb_left)
  1178     apply ((rule ext)+, simp)
  1179     apply (simplesubst RepAbs_matrix)
  1180     apply (rule exI[of _ "nrows B"])
  1181     apply (simp add: nrows assms foldseq_zero)
  1182     apply (rule exI[of _ "ncols C"])
  1183     apply (simp add: assms ncols foldseq_zero)
  1184     apply (subst RepAbs_matrix)
  1185     apply (rule exI[of _ "nrows A"])
  1186     apply (simp add: nrows assms foldseq_zero)
  1187     apply (rule exI[of _ "ncols B"])
  1188     apply (simp add: assms ncols foldseq_zero)
  1189     apply (simp add: fmul2fadd1fold fmul1fadd2fold assms)
  1190     apply (subst foldseq_foldseq)
  1191     apply (simp add: assms)+
  1192     apply (simp add: transpose_infmatrix)
  1193     done
  1194 qed
  1195 
  1196 lemma
  1197   assumes
  1198   "! a. fmul1 0 a = 0"
  1199   "! a. fmul1 a 0 = 0"
  1200   "! a. fmul2 0 a = 0"
  1201   "! a. fmul2 a 0 = 0"
  1202   "fadd1 0 0 = 0"
  1203   "fadd2 0 0 = 0"
  1204   "! a b c d. fadd2 (fadd1 a b) (fadd1 c d) = fadd1 (fadd2 a c) (fadd2 b d)"
  1205   "associative fadd1"
  1206   "associative fadd2"
  1207   "! a b c. fmul2 (fmul1 a b) c = fmul1 a (fmul2 b c)"
  1208   "! a b c. fmul2 (fadd1 a b) c = fadd1 (fmul2 a c) (fmul2 b c)"
  1209   "! a b c. fmul1 c (fadd2 a b) = fadd2 (fmul1 c a) (fmul1 c b)"
  1210   shows
  1211   "(mult_matrix fmul1 fadd1 A) o (mult_matrix fmul2 fadd2 B) = mult_matrix fmul2 fadd2 (mult_matrix fmul1 fadd1 A B)"
  1212 apply (rule ext)+
  1213 apply (simp add: comp_def )
  1214 apply (simp add: mult_matrix_assoc assms)
  1215 done
  1216 
  1217 lemma mult_matrix_assoc_simple:
  1218   assumes
  1219   "! a. fmul 0 a = 0"
  1220   "! a. fmul a 0 = 0"
  1221   "fadd 0 0 = 0"
  1222   "associative fadd"
  1223   "commutative fadd"
  1224   "associative fmul"
  1225   "distributive fmul fadd"
  1226   shows "mult_matrix fmul fadd (mult_matrix fmul fadd A B) C = mult_matrix fmul fadd A (mult_matrix fmul fadd B C)"
  1227 proof -
  1228   have "!! a b c d. fadd (fadd a b) (fadd c d) = fadd (fadd a c) (fadd b d)"
  1229     using assms by (simp add: associative_def commutative_def)
  1230   then show ?thesis
  1231     apply (subst mult_matrix_assoc)
  1232     using assms
  1233     apply simp_all
  1234     apply (simp_all add: associative_def distributive_def l_distributive_def r_distributive_def)
  1235     done
  1236 qed
  1237 
  1238 lemma transpose_apply_matrix: "f 0 = 0 \<Longrightarrow> transpose_matrix (apply_matrix f A) = apply_matrix f (transpose_matrix A)"
  1239 apply (simp add: Rep_matrix_inject[THEN sym])
  1240 apply (rule ext)+
  1241 by simp
  1242 
  1243 lemma transpose_combine_matrix: "f 0 0 = 0 \<Longrightarrow> transpose_matrix (combine_matrix f A B) = combine_matrix f (transpose_matrix A) (transpose_matrix B)"
  1244 apply (simp add: Rep_matrix_inject[THEN sym])
  1245 apply (rule ext)+
  1246 by simp
  1247 
  1248 lemma Rep_mult_matrix:
  1249   assumes
  1250   "! a. fmul 0 a = 0"
  1251   "! a. fmul a 0 = 0"
  1252   "fadd 0 0 = 0"
  1253   shows
  1254   "Rep_matrix(mult_matrix fmul fadd A B) j i =
  1255   foldseq fadd (% k. fmul (Rep_matrix A j k) (Rep_matrix B k i)) (max (ncols A) (nrows B))"
  1256 apply (simp add: mult_matrix_def mult_matrix_n_def)
  1257 apply (subst RepAbs_matrix)
  1258 apply (rule exI[of _ "nrows A"], insert assms, simp add: nrows foldseq_zero)
  1259 apply (rule exI[of _ "ncols B"], insert assms, simp add: ncols foldseq_zero)
  1260 apply simp
  1261 done
  1262 
  1263 lemma transpose_mult_matrix:
  1264   assumes
  1265   "! a. fmul 0 a = 0"
  1266   "! a. fmul a 0 = 0"
  1267   "fadd 0 0 = 0"
  1268   "! x y. fmul y x = fmul x y"
  1269   shows
  1270   "transpose_matrix (mult_matrix fmul fadd A B) = mult_matrix fmul fadd (transpose_matrix B) (transpose_matrix A)"
  1271   apply (simp add: Rep_matrix_inject[THEN sym])
  1272   apply (rule ext)+
  1273   using assms
  1274   apply (simp add: Rep_mult_matrix ac_simps)
  1275   done
  1276 
  1277 lemma column_transpose_matrix: "column_of_matrix (transpose_matrix A) n = transpose_matrix (row_of_matrix A n)"
  1278 apply (simp add:  Rep_matrix_inject[THEN sym])
  1279 apply (rule ext)+
  1280 by simp
  1281 
  1282 lemma take_columns_transpose_matrix: "take_columns (transpose_matrix A) n = transpose_matrix (take_rows A n)"
  1283 apply (simp add: Rep_matrix_inject[THEN sym])
  1284 apply (rule ext)+
  1285 by simp
  1286 
  1287 instantiation matrix :: ("{zero, ord}") ord
  1288 begin
  1289 
  1290 definition
  1291   le_matrix_def: "A \<le> B \<longleftrightarrow> (\<forall>j i. Rep_matrix A j i \<le> Rep_matrix B j i)"
  1292 
  1293 definition
  1294   less_def: "A < (B::'a matrix) \<longleftrightarrow> A \<le> B \<and> \<not> B \<le> A"
  1295 
  1296 instance ..
  1297 
  1298 end
  1299 
  1300 instance matrix :: ("{zero, order}") order
  1301 apply intro_classes
  1302 apply (simp_all add: le_matrix_def less_def)
  1303 apply (auto)
  1304 apply (drule_tac x=j in spec, drule_tac x=j in spec)
  1305 apply (drule_tac x=i in spec, drule_tac x=i in spec)
  1306 apply (simp)
  1307 apply (simp add: Rep_matrix_inject[THEN sym])
  1308 apply (rule ext)+
  1309 apply (drule_tac x=xa in spec, drule_tac x=xa in spec)
  1310 apply (drule_tac x=xb in spec, drule_tac x=xb in spec)
  1311 apply simp
  1312 done
  1313 
  1314 lemma le_apply_matrix:
  1315   assumes
  1316   "f 0 = 0"
  1317   "! x y. x <= y \<longrightarrow> f x <= f y"
  1318   "(a::('a::{ord, zero}) matrix) <= b"
  1319   shows
  1320   "apply_matrix f a <= apply_matrix f b"
  1321   using assms by (simp add: le_matrix_def)
  1322 
  1323 lemma le_combine_matrix:
  1324   assumes
  1325   "f 0 0 = 0"
  1326   "! a b c d. a <= b & c <= d \<longrightarrow> f a c <= f b d"
  1327   "A <= B"
  1328   "C <= D"
  1329   shows
  1330   "combine_matrix f A C <= combine_matrix f B D"
  1331   using assms by (simp add: le_matrix_def)
  1332 
  1333 lemma le_left_combine_matrix:
  1334   assumes
  1335   "f 0 0 = 0"
  1336   "! a b c. a <= b \<longrightarrow> f c a <= f c b"
  1337   "A <= B"
  1338   shows
  1339   "combine_matrix f C A <= combine_matrix f C B"
  1340   using assms by (simp add: le_matrix_def)
  1341 
  1342 lemma le_right_combine_matrix:
  1343   assumes
  1344   "f 0 0 = 0"
  1345   "! a b c. a <= b \<longrightarrow> f a c <= f b c"
  1346   "A <= B"
  1347   shows
  1348   "combine_matrix f A C <= combine_matrix f B C"
  1349   using assms by (simp add: le_matrix_def)
  1350 
  1351 lemma le_transpose_matrix: "(A <= B) = (transpose_matrix A <= transpose_matrix B)"
  1352   by (simp add: le_matrix_def, auto)
  1353 
  1354 lemma le_foldseq:
  1355   assumes
  1356   "! a b c d . a <= b & c <= d \<longrightarrow> f a c <= f b d"
  1357   "! i. i <= n \<longrightarrow> s i <= t i"
  1358   shows
  1359   "foldseq f s n <= foldseq f t n"
  1360 proof -
  1361   have "! s t. (! i. i<=n \<longrightarrow> s i <= t i) \<longrightarrow> foldseq f s n <= foldseq f t n"
  1362     by (induct n) (simp_all add: assms)
  1363   then show "foldseq f s n <= foldseq f t n" using assms by simp
  1364 qed
  1365 
  1366 lemma le_left_mult:
  1367   assumes
  1368   "! a b c d. a <= b & c <= d \<longrightarrow> fadd a c <= fadd b d"
  1369   "! c a b.   0 <= c & a <= b \<longrightarrow> fmul c a <= fmul c b"
  1370   "! a. fmul 0 a = 0"
  1371   "! a. fmul a 0 = 0"
  1372   "fadd 0 0 = 0"
  1373   "0 <= C"
  1374   "A <= B"
  1375   shows
  1376   "mult_matrix fmul fadd C A <= mult_matrix fmul fadd C B"
  1377   using assms
  1378   apply (simp add: le_matrix_def Rep_mult_matrix)
  1379   apply (auto)
  1380   apply (simplesubst foldseq_zerotail[of _ _ _ "max (ncols C) (max (nrows A) (nrows B))"], simp_all add: nrows ncols max1 max2)+
  1381   apply (rule le_foldseq)
  1382   apply (auto)
  1383   done
  1384 
  1385 lemma le_right_mult:
  1386   assumes
  1387   "! a b c d. a <= b & c <= d \<longrightarrow> fadd a c <= fadd b d"
  1388   "! c a b. 0 <= c & a <= b \<longrightarrow> fmul a c <= fmul b c"
  1389   "! a. fmul 0 a = 0"
  1390   "! a. fmul a 0 = 0"
  1391   "fadd 0 0 = 0"
  1392   "0 <= C"
  1393   "A <= B"
  1394   shows
  1395   "mult_matrix fmul fadd A C <= mult_matrix fmul fadd B C"
  1396   using assms
  1397   apply (simp add: le_matrix_def Rep_mult_matrix)
  1398   apply (auto)
  1399   apply (simplesubst foldseq_zerotail[of _ _ _ "max (nrows C) (max (ncols A) (ncols B))"], simp_all add: nrows ncols max1 max2)+
  1400   apply (rule le_foldseq)
  1401   apply (auto)
  1402   done
  1403 
  1404 lemma spec2: "! j i. P j i \<Longrightarrow> P j i" by blast
  1405 lemma neg_imp: "(\<not> Q \<longrightarrow> \<not> P) \<Longrightarrow> P \<longrightarrow> Q" by blast
  1406 
  1407 lemma singleton_matrix_le[simp]: "(singleton_matrix j i a <= singleton_matrix j i b) = (a <= (b::_::order))"
  1408   by (auto simp add: le_matrix_def)
  1409 
  1410 lemma singleton_le_zero[simp]: "(singleton_matrix j i x <= 0) = (x <= (0::'a::{order,zero}))"
  1411   apply (auto)
  1412   apply (simp add: le_matrix_def)
  1413   apply (drule_tac j=j and i=i in spec2)
  1414   apply (simp)
  1415   apply (simp add: le_matrix_def)
  1416   done
  1417 
  1418 lemma singleton_ge_zero[simp]: "(0 <= singleton_matrix j i x) = ((0::'a::{order,zero}) <= x)"
  1419   apply (auto)
  1420   apply (simp add: le_matrix_def)
  1421   apply (drule_tac j=j and i=i in spec2)
  1422   apply (simp)
  1423   apply (simp add: le_matrix_def)
  1424   done
  1425 
  1426 lemma move_matrix_le_zero[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (move_matrix A j i <= 0) = (A <= (0::('a::{order,zero}) matrix))"
  1427   apply (auto simp add: le_matrix_def)
  1428   apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
  1429   apply (auto)
  1430   done
  1431 
  1432 lemma move_matrix_zero_le[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (0 <= move_matrix A j i) = ((0::('a::{order,zero}) matrix) <= A)"
  1433   apply (auto simp add: le_matrix_def)
  1434   apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
  1435   apply (auto)
  1436   done
  1437 
  1438 lemma move_matrix_le_move_matrix_iff[simp]: "0 <= j \<Longrightarrow> 0 <= i \<Longrightarrow> (move_matrix A j i <= move_matrix B j i) = (A <= (B::('a::{order,zero}) matrix))"
  1439   apply (auto simp add: le_matrix_def)
  1440   apply (drule_tac j="ja+(nat j)" and i="ia+(nat i)" in spec2)
  1441   apply (auto)
  1442   done  
  1443 
  1444 instantiation matrix :: ("{lattice, zero}") lattice
  1445 begin
  1446 
  1447 definition "inf = combine_matrix inf"
  1448 
  1449 definition "sup = combine_matrix sup"
  1450 
  1451 instance
  1452   by standard (auto simp add: le_infI le_matrix_def inf_matrix_def sup_matrix_def)
  1453 
  1454 end
  1455 
  1456 instantiation matrix :: ("{plus, zero}") plus
  1457 begin
  1458 
  1459 definition
  1460   plus_matrix_def: "A + B = combine_matrix (op +) A B"
  1461 
  1462 instance ..
  1463 
  1464 end
  1465 
  1466 instantiation matrix :: ("{uminus, zero}") uminus
  1467 begin
  1468 
  1469 definition
  1470   minus_matrix_def: "- A = apply_matrix uminus A"
  1471 
  1472 instance ..
  1473 
  1474 end
  1475 
  1476 instantiation matrix :: ("{minus, zero}") minus
  1477 begin
  1478 
  1479 definition
  1480   diff_matrix_def: "A - B = combine_matrix (op -) A B"
  1481 
  1482 instance ..
  1483 
  1484 end
  1485 
  1486 instantiation matrix :: ("{plus, times, zero}") times
  1487 begin
  1488 
  1489 definition
  1490   times_matrix_def: "A * B = mult_matrix (op *) (op +) A B"
  1491 
  1492 instance ..
  1493 
  1494 end
  1495 
  1496 instantiation matrix :: ("{lattice, uminus, zero}") abs
  1497 begin
  1498 
  1499 definition
  1500   abs_matrix_def: "\<bar>A :: 'a matrix\<bar> = sup A (- A)"
  1501 
  1502 instance ..
  1503 
  1504 end
  1505 
  1506 instance matrix :: (monoid_add) monoid_add
  1507 proof
  1508   fix A B C :: "'a matrix"
  1509   show "A + B + C = A + (B + C)"    
  1510     apply (simp add: plus_matrix_def)
  1511     apply (rule combine_matrix_assoc[simplified associative_def, THEN spec, THEN spec, THEN spec])
  1512     apply (simp_all add: add.assoc)
  1513     done
  1514   show "0 + A = A"
  1515     apply (simp add: plus_matrix_def)
  1516     apply (rule combine_matrix_zero_l_neutral[simplified zero_l_neutral_def, THEN spec])
  1517     apply (simp)
  1518     done
  1519   show "A + 0 = A"
  1520     apply (simp add: plus_matrix_def)
  1521     apply (rule combine_matrix_zero_r_neutral [simplified zero_r_neutral_def, THEN spec])
  1522     apply (simp)
  1523     done
  1524 qed
  1525 
  1526 instance matrix :: (comm_monoid_add) comm_monoid_add
  1527 proof
  1528   fix A B :: "'a matrix"
  1529   show "A + B = B + A"
  1530     apply (simp add: plus_matrix_def)
  1531     apply (rule combine_matrix_commute[simplified commutative_def, THEN spec, THEN spec])
  1532     apply (simp_all add: add.commute)
  1533     done
  1534   show "0 + A = A"
  1535     apply (simp add: plus_matrix_def)
  1536     apply (rule combine_matrix_zero_l_neutral[simplified zero_l_neutral_def, THEN spec])
  1537     apply (simp)
  1538     done
  1539 qed
  1540 
  1541 instance matrix :: (group_add) group_add
  1542 proof
  1543   fix A B :: "'a matrix"
  1544   show "- A + A = 0" 
  1545     by (simp add: plus_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
  1546   show "A + - B = A - B"
  1547     by (simp add: plus_matrix_def diff_matrix_def minus_matrix_def Rep_matrix_inject [symmetric] ext)
  1548 qed
  1549 
  1550 instance matrix :: (ab_group_add) ab_group_add
  1551 proof
  1552   fix A B :: "'a matrix"
  1553   show "- A + A = 0" 
  1554     by (simp add: plus_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
  1555   show "A - B = A + - B" 
  1556     by (simp add: plus_matrix_def diff_matrix_def minus_matrix_def Rep_matrix_inject[symmetric] ext)
  1557 qed
  1558 
  1559 instance matrix :: (ordered_ab_group_add) ordered_ab_group_add
  1560 proof
  1561   fix A B C :: "'a matrix"
  1562   assume "A <= B"
  1563   then show "C + A <= C + B"
  1564     apply (simp add: plus_matrix_def)
  1565     apply (rule le_left_combine_matrix)
  1566     apply (simp_all)
  1567     done
  1568 qed
  1569   
  1570 instance matrix :: (lattice_ab_group_add) semilattice_inf_ab_group_add ..
  1571 instance matrix :: (lattice_ab_group_add) semilattice_sup_ab_group_add ..
  1572 
  1573 instance matrix :: (semiring_0) semiring_0
  1574 proof
  1575   fix A B C :: "'a matrix"
  1576   show "A * B * C = A * (B * C)"
  1577     apply (simp add: times_matrix_def)
  1578     apply (rule mult_matrix_assoc)
  1579     apply (simp_all add: associative_def algebra_simps)
  1580     done
  1581   show "(A + B) * C = A * C + B * C"
  1582     apply (simp add: times_matrix_def plus_matrix_def)
  1583     apply (rule l_distributive_matrix[simplified l_distributive_def, THEN spec, THEN spec, THEN spec])
  1584     apply (simp_all add: associative_def commutative_def algebra_simps)
  1585     done
  1586   show "A * (B + C) = A * B + A * C"
  1587     apply (simp add: times_matrix_def plus_matrix_def)
  1588     apply (rule r_distributive_matrix[simplified r_distributive_def, THEN spec, THEN spec, THEN spec])
  1589     apply (simp_all add: associative_def commutative_def algebra_simps)
  1590     done
  1591   show "0 * A = 0" by (simp add: times_matrix_def)
  1592   show "A * 0 = 0" by (simp add: times_matrix_def)
  1593 qed
  1594 
  1595 instance matrix :: (ring) ring ..
  1596 
  1597 instance matrix :: (ordered_ring) ordered_ring
  1598 proof
  1599   fix A B C :: "'a matrix"
  1600   assume a: "A \<le> B"
  1601   assume b: "0 \<le> C"
  1602   from a b show "C * A \<le> C * B"
  1603     apply (simp add: times_matrix_def)
  1604     apply (rule le_left_mult)
  1605     apply (simp_all add: add_mono mult_left_mono)
  1606     done
  1607   from a b show "A * C \<le> B * C"
  1608     apply (simp add: times_matrix_def)
  1609     apply (rule le_right_mult)
  1610     apply (simp_all add: add_mono mult_right_mono)
  1611     done
  1612 qed
  1613 
  1614 instance matrix :: (lattice_ring) lattice_ring
  1615 proof
  1616   fix A B C :: "('a :: lattice_ring) matrix"
  1617   show "\<bar>A\<bar> = sup A (-A)" 
  1618     by (simp add: abs_matrix_def)
  1619 qed
  1620 
  1621 lemma Rep_matrix_add[simp]:
  1622   "Rep_matrix ((a::('a::monoid_add)matrix)+b) j i  = (Rep_matrix a j i) + (Rep_matrix b j i)"
  1623   by (simp add: plus_matrix_def)
  1624 
  1625 lemma Rep_matrix_mult: "Rep_matrix ((a::('a::semiring_0) matrix) * b) j i = 
  1626   foldseq (op +) (% k.  (Rep_matrix a j k) * (Rep_matrix b k i)) (max (ncols a) (nrows b))"
  1627 apply (simp add: times_matrix_def)
  1628 apply (simp add: Rep_mult_matrix)
  1629 done
  1630 
  1631 lemma apply_matrix_add: "! x y. f (x+y) = (f x) + (f y) \<Longrightarrow> f 0 = (0::'a)
  1632   \<Longrightarrow> apply_matrix f ((a::('a::monoid_add) matrix) + b) = (apply_matrix f a) + (apply_matrix f b)"
  1633 apply (subst Rep_matrix_inject[symmetric])
  1634 apply (rule ext)+
  1635 apply (simp)
  1636 done
  1637 
  1638 lemma singleton_matrix_add: "singleton_matrix j i ((a::_::monoid_add)+b) = (singleton_matrix j i a) + (singleton_matrix j i b)"
  1639 apply (subst Rep_matrix_inject[symmetric])
  1640 apply (rule ext)+
  1641 apply (simp)
  1642 done
  1643 
  1644 lemma nrows_mult: "nrows ((A::('a::semiring_0) matrix) * B) <= nrows A"
  1645 by (simp add: times_matrix_def mult_nrows)
  1646 
  1647 lemma ncols_mult: "ncols ((A::('a::semiring_0) matrix) * B) <= ncols B"
  1648 by (simp add: times_matrix_def mult_ncols)
  1649 
  1650 definition
  1651   one_matrix :: "nat \<Rightarrow> ('a::{zero,one}) matrix" where
  1652   "one_matrix n = Abs_matrix (% j i. if j = i & j < n then 1 else 0)"
  1653 
  1654 lemma Rep_one_matrix[simp]: "Rep_matrix (one_matrix n) j i = (if (j = i & j < n) then 1 else 0)"
  1655 apply (simp add: one_matrix_def)
  1656 apply (simplesubst RepAbs_matrix)
  1657 apply (rule exI[of _ n], simp add: if_split)+
  1658 by (simp add: if_split)
  1659 
  1660 lemma nrows_one_matrix[simp]: "nrows ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _")
  1661 proof -
  1662   have "?r <= n" by (simp add: nrows_le)
  1663   moreover have "n <= ?r" by (simp add:le_nrows, arith)
  1664   ultimately show "?r = n" by simp
  1665 qed
  1666 
  1667 lemma ncols_one_matrix[simp]: "ncols ((one_matrix n) :: ('a::zero_neq_one)matrix) = n" (is "?r = _")
  1668 proof -
  1669   have "?r <= n" by (simp add: ncols_le)
  1670   moreover have "n <= ?r" by (simp add: le_ncols, arith)
  1671   ultimately show "?r = n" by simp
  1672 qed
  1673 
  1674 lemma one_matrix_mult_right[simp]: "ncols A <= n \<Longrightarrow> (A::('a::{semiring_1}) matrix) * (one_matrix n) = A"
  1675 apply (subst Rep_matrix_inject[THEN sym])
  1676 apply (rule ext)+
  1677 apply (simp add: times_matrix_def Rep_mult_matrix)
  1678 apply (rule_tac j1="xa" in ssubst[OF foldseq_almostzero])
  1679 apply (simp_all)
  1680 by (simp add: ncols)
  1681 
  1682 lemma one_matrix_mult_left[simp]: "nrows A <= n \<Longrightarrow> (one_matrix n) * A = (A::('a::semiring_1) matrix)"
  1683 apply (subst Rep_matrix_inject[THEN sym])
  1684 apply (rule ext)+
  1685 apply (simp add: times_matrix_def Rep_mult_matrix)
  1686 apply (rule_tac j1="x" in ssubst[OF foldseq_almostzero])
  1687 apply (simp_all)
  1688 by (simp add: nrows)
  1689 
  1690 lemma transpose_matrix_mult: "transpose_matrix ((A::('a::comm_ring) matrix)*B) = (transpose_matrix B) * (transpose_matrix A)"
  1691 apply (simp add: times_matrix_def)
  1692 apply (subst transpose_mult_matrix)
  1693 apply (simp_all add: mult.commute)
  1694 done
  1695 
  1696 lemma transpose_matrix_add: "transpose_matrix ((A::('a::monoid_add) matrix)+B) = transpose_matrix A + transpose_matrix B"
  1697 by (simp add: plus_matrix_def transpose_combine_matrix)
  1698 
  1699 lemma transpose_matrix_diff: "transpose_matrix ((A::('a::group_add) matrix)-B) = transpose_matrix A - transpose_matrix B"
  1700 by (simp add: diff_matrix_def transpose_combine_matrix)
  1701 
  1702 lemma transpose_matrix_minus: "transpose_matrix (-(A::('a::group_add) matrix)) = - transpose_matrix (A::'a matrix)"
  1703 by (simp add: minus_matrix_def transpose_apply_matrix)
  1704 
  1705 definition right_inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool" where
  1706   "right_inverse_matrix A X == (A * X = one_matrix (max (nrows A) (ncols X))) \<and> nrows X \<le> ncols A" 
  1707 
  1708 definition left_inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool" where
  1709   "left_inverse_matrix A X == (X * A = one_matrix (max(nrows X) (ncols A))) \<and> ncols X \<le> nrows A" 
  1710 
  1711 definition inverse_matrix :: "('a::{ring_1}) matrix \<Rightarrow> 'a matrix \<Rightarrow> bool" where
  1712   "inverse_matrix A X == (right_inverse_matrix A X) \<and> (left_inverse_matrix A X)"
  1713 
  1714 lemma right_inverse_matrix_dim: "right_inverse_matrix A X \<Longrightarrow> nrows A = ncols X"
  1715 apply (insert ncols_mult[of A X], insert nrows_mult[of A X])
  1716 by (simp add: right_inverse_matrix_def)
  1717 
  1718 lemma left_inverse_matrix_dim: "left_inverse_matrix A Y \<Longrightarrow> ncols A = nrows Y"
  1719 apply (insert ncols_mult[of Y A], insert nrows_mult[of Y A]) 
  1720 by (simp add: left_inverse_matrix_def)
  1721 
  1722 lemma left_right_inverse_matrix_unique: 
  1723   assumes "left_inverse_matrix A Y" "right_inverse_matrix A X"
  1724   shows "X = Y"
  1725 proof -
  1726   have "Y = Y * one_matrix (nrows A)" 
  1727     apply (subst one_matrix_mult_right)
  1728     using assms
  1729     apply (simp_all add: left_inverse_matrix_def)
  1730     done
  1731   also have "\<dots> = Y * (A * X)" 
  1732     apply (insert assms)
  1733     apply (frule right_inverse_matrix_dim)
  1734     by (simp add: right_inverse_matrix_def)
  1735   also have "\<dots> = (Y * A) * X" by (simp add: mult.assoc)
  1736   also have "\<dots> = X" 
  1737     apply (insert assms)
  1738     apply (frule left_inverse_matrix_dim)
  1739     apply (simp_all add:  left_inverse_matrix_def right_inverse_matrix_def one_matrix_mult_left)
  1740     done
  1741   ultimately show "X = Y" by (simp)
  1742 qed
  1743 
  1744 lemma inverse_matrix_inject: "\<lbrakk> inverse_matrix A X; inverse_matrix A Y \<rbrakk> \<Longrightarrow> X = Y"
  1745   by (auto simp add: inverse_matrix_def left_right_inverse_matrix_unique)
  1746 
  1747 lemma one_matrix_inverse: "inverse_matrix (one_matrix n) (one_matrix n)"
  1748   by (simp add: inverse_matrix_def left_inverse_matrix_def right_inverse_matrix_def)
  1749 
  1750 lemma zero_imp_mult_zero: "(a::'a::semiring_0) = 0 | b = 0 \<Longrightarrow> a * b = 0"
  1751 by auto
  1752 
  1753 lemma Rep_matrix_zero_imp_mult_zero:
  1754   "! j i k. (Rep_matrix A j k = 0) | (Rep_matrix B k i) = 0  \<Longrightarrow> A * B = (0::('a::lattice_ring) matrix)"
  1755 apply (subst Rep_matrix_inject[symmetric])
  1756 apply (rule ext)+
  1757 apply (auto simp add: Rep_matrix_mult foldseq_zero zero_imp_mult_zero)
  1758 done
  1759 
  1760 lemma add_nrows: "nrows (A::('a::monoid_add) matrix) <= u \<Longrightarrow> nrows B <= u \<Longrightarrow> nrows (A + B) <= u"
  1761 apply (simp add: plus_matrix_def)
  1762 apply (rule combine_nrows)
  1763 apply (simp_all)
  1764 done
  1765 
  1766 lemma move_matrix_row_mult: "move_matrix ((A::('a::semiring_0) matrix) * B) j 0 = (move_matrix A j 0) * B"
  1767 apply (subst Rep_matrix_inject[symmetric])
  1768 apply (rule ext)+
  1769 apply (auto simp add: Rep_matrix_mult foldseq_zero)
  1770 apply (rule_tac foldseq_zerotail[symmetric])
  1771 apply (auto simp add: nrows zero_imp_mult_zero max2)
  1772 apply (rule order_trans)
  1773 apply (rule ncols_move_matrix_le)
  1774 apply (simp add: max1)
  1775 done
  1776 
  1777 lemma move_matrix_col_mult: "move_matrix ((A::('a::semiring_0) matrix) * B) 0 i = A * (move_matrix B 0 i)"
  1778 apply (subst Rep_matrix_inject[symmetric])
  1779 apply (rule ext)+
  1780 apply (auto simp add: Rep_matrix_mult foldseq_zero)
  1781 apply (rule_tac foldseq_zerotail[symmetric])
  1782 apply (auto simp add: ncols zero_imp_mult_zero max1)
  1783 apply (rule order_trans)
  1784 apply (rule nrows_move_matrix_le)
  1785 apply (simp add: max2)
  1786 done
  1787 
  1788 lemma move_matrix_add: "((move_matrix (A + B) j i)::(('a::monoid_add) matrix)) = (move_matrix A j i) + (move_matrix B j i)" 
  1789 apply (subst Rep_matrix_inject[symmetric])
  1790 apply (rule ext)+
  1791 apply (simp)
  1792 done
  1793 
  1794 lemma move_matrix_mult: "move_matrix ((A::('a::semiring_0) matrix)*B) j i = (move_matrix A j 0) * (move_matrix B 0 i)"
  1795 by (simp add: move_matrix_ortho[of "A*B"] move_matrix_col_mult move_matrix_row_mult)
  1796 
  1797 definition scalar_mult :: "('a::ring) \<Rightarrow> 'a matrix \<Rightarrow> 'a matrix" where
  1798   "scalar_mult a m == apply_matrix (op * a) m"
  1799 
  1800 lemma scalar_mult_zero[simp]: "scalar_mult y 0 = 0" 
  1801 by (simp add: scalar_mult_def)
  1802 
  1803 lemma scalar_mult_add: "scalar_mult y (a+b) = (scalar_mult y a) + (scalar_mult y b)"
  1804 by (simp add: scalar_mult_def apply_matrix_add algebra_simps)
  1805 
  1806 lemma Rep_scalar_mult[simp]: "Rep_matrix (scalar_mult y a) j i = y * (Rep_matrix a j i)" 
  1807 by (simp add: scalar_mult_def)
  1808 
  1809 lemma scalar_mult_singleton[simp]: "scalar_mult y (singleton_matrix j i x) = singleton_matrix j i (y * x)"
  1810 apply (subst Rep_matrix_inject[symmetric])
  1811 apply (rule ext)+
  1812 apply (auto)
  1813 done
  1814 
  1815 lemma Rep_minus[simp]: "Rep_matrix (-(A::_::group_add)) x y = - (Rep_matrix A x y)"
  1816 by (simp add: minus_matrix_def)
  1817 
  1818 lemma Rep_abs[simp]: "Rep_matrix \<bar>A::_::lattice_ab_group_add\<bar> x y = \<bar>Rep_matrix A x y\<bar>"
  1819 by (simp add: abs_lattice sup_matrix_def)
  1820 
  1821 end