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