File: inner-sums.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (597 lines) | stat: -rw-r--r-- 20,355 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
;;; This book proves some theorems about nested sums.  A sum
;;; $\sum_{i}{a_i} is handled as a function a(i) that computes the ith
;;; term and a function sum_i that adds up the function a(i).  Nested
;;; sums use an outer sum sum_i, and inner sum sum_i_j, and a function
;;; a(i,j) to compute the ith/jth term.

(in-package "ACL2")

(local (include-book "arithmetic/top" :dir :system))
(include-book "arithmetic/sumlist" :dir :system)

; Added by Matt K. for v2-7.
(add-match-free-override :once t)
(set-match-free-default :once)

;; We start by defining the function binop(i,j) which is used to
;; compute the term a_{i,j} in a nested sum.

(encapsulate
 ((binop (i j) t))

 (local
  (defun binop (i j)
    (+ i j)))

 (defthm binop-type-prescription
   (acl2-numberp (binop i j))
   :rule-classes (:rewrite :type-prescription))
 )

;; We will define some basic functions to compute various nested
;; sums.  Rather than computing the sum directoy, we build up a
;; sequence that has the elements we wish to add in the right order.
;; I.e., rather than returning a(i,1) + a(i,2) + ... + a(i,n), we
;; return the list (a(i,1), a(i,2), ..., a(i,n)).

;; Now, we define a function that "adds up" all the a_{i,j} elements
;; in the ith row.

(defun row-expansion-inner (i j n)
  (declare (xargs :measure (nfix (1+ (- n j)))))
  (if (and (integerp j) (integerp n) (<= 0 j) (<= j n))
      (cons (binop i j)
	    (row-expansion-inner i (1+ j) n))
    nil))

;; Using the function above, we can "sum" all the a_{i,j} elements by
;; "adding" each of the m rows.

(defun row-expansion-outer (i m n)
  (declare (xargs :measure (nfix (1+ (- m i)))))
  (if (and (integerp i) (integerp m) (<= 0 i) (<= i m))
      (cons (sumlist (row-expansion-inner i 0 n))
	    (row-expansion-outer (1+ i) m n))
    nil))

;; Similarly, we can "add up" all the a_{i,j} values in the jth column.

(defun col-expansion-inner (i j m)
  (declare (xargs :measure (nfix (1+ (- m i)))))
  (if (and (integerp i) (integerp m) (<= 0 i) (<= i m))
      (cons (binop i j) (col-expansion-inner (1+ i) j m))
    nil))

;; And them "sum" all the a_{i,j} by "adding up" all the columns.

(defun col-expansion-outer (j m n)
  (declare (xargs :measure (nfix (1+ (- n j)))))
  (if (and (integerp j) (integerp n) (<= 0 j) (<= j n))
      (cons (sumlist (col-expansion-inner 0 j m))
	    (col-expansion-outer (1+ j) m n))
    nil))

(in-theory (disable (:executable-counterpart row-expansion-inner)
		    (:executable-counterpart row-expansion-outer)
		    (:executable-counterpart col-expansion-inner)
		    (:executable-counterpart col-expansion-outer)))

;; This is just to specify that we want to "induct on n"....

(local
 (defun natural-induction-hint (n)
   (if (zp n)
       0
     (1+ (natural-induction-hint (1- n))))))

;; Now, if we add up all the rows up the (m-1)st row, we get the same
;; result as if we add up all the rows up to the mth row and then
;; subtract the mth row....

(defthm row-expansion-outer-split
  (implies (and (integerp i) (integerp m) (<= 0 i) (<= i m))
	   (equal (sumlist (row-expansion-outer i (1- m) n))
		  (- (sumlist (row-expansion-outer i m n))
		     (sumlist (row-expansion-outer m m n))))))

;; The sum of a column is zero when we start looking at elements
;; beyond the last row....

(defthm col-expansion-inner-out-of-bounds
  (implies (< m i)
	   (equal (col-expansion-inner i j m) nil)))

;; Now, we can see what happens when we add up all the a_{i,j} in
;; column j up to row m-1 -- it's the same as we add up all the
;; a_{i,j} up to row m and then subtract a_{m,j}.

(defthm col-expansion-inner-split
  (implies (and (integerp i) (integerp m) (<= 0 i) (<= i m))
	   (equal (sumlist (col-expansion-inner i j (1- m)))
		  (- (sumlist (col-expansion-inner i j m))
		     (binop m j)))))

;; From the above, we can conclude that if we add up all the columns
;; up to row m-1, it's the same as if we add up all the columns up
;; to row m and then subtract row m.

(defthm col-expansion-outer-split
  (implies (and (integerp m) (<= 0 m))
	   (equal (sumlist (col-expansion-outer j (1- m) n))
		  (- (sumlist (col-expansion-outer j m n))
		     (sumlist (row-expansion-inner m j n))))))

(in-theory (disable row-expansion-outer-split
		    col-expansion-inner-split
		    col-expansion-outer-split))

;; In the proofs to follow, we find many terms expanding empty
;; sequences (i.e., i>m or j>n or i<0, etc).  So, we show that these
;; terms can be ignored:

(defthm row-expansion-outer-negative-m
  (implies (< m 0)
	   (equal (row-expansion-outer i m n) nil)))

(defthm col-expansion-outer-negative-m
  (implies (< m 0)
	   (equal (sumlist (col-expansion-outer j m N)) 0)))

(defthm row-expansion-outer-negative-n
  (implies (< n 0)
	   (equal (sumlist (row-expansion-outer i m n)) 0)))

(defthm col-expansion-outer-negative-n
  (implies (< n 0)
	   (equal (col-expansion-outer j m n) nil)))

(defthm row-expansion-non-integer-m
  (implies (not (integerp m))
	   (equal (row-expansion-outer i m n) nil)))

(defthm col-expansion-outer-non-integer-m
  (implies (not (integerp m))
	   (equal (sumlist (col-expansion-outer j m n)) 0)))

(defthm row-expansion-non-integer-n
  (implies (not (integerp n))
	   (equal (sumlist (row-expansion-outer i m n)) 0)))

(defthm col-expansion-outer-non-integer-n
  (implies (not (integerp n))
	   (equal (col-expansion-outer j m n) nil)))

;; Next we consider the special case when the number of rows or
;; columns is equal to 1 -- i.e., when the largest index is zero (yes,
;; I'm a C++ programmer by day)

(defthm row-expansion-inner-i-j-0
  (equal (row-expansion-inner i j 0)
	 (if (equal j 0)
	     (list (binop i 0))
	   nil))
  :hints (("Goal" :expand (row-expansion-inner i j 0))))

(defthm row-expansion-outer-i-0-0
  (equal (row-expansion-outer i 0 0)
	 (if (and (equal i 0))
	     (list (binop 0 0))
	   nil))
  :hints (("Goal" :expand (row-expansion-outer i 0 0))))

(defthm row-expansion-outer-with-m=0
  (equal (sumlist (row-expansion-outer 0 0 n))
	 (sumlist (row-expansion-inner 0 0 n)))
  :hints (("Goal" :expand (row-expansion-outer 0 0 n))))

(defthm col-expansion-inner-i-j-0
  (equal (col-expansion-inner i j 0)
	 (if (equal i 0)
	     (list (binop 0 j))
	   nil))
  :hints (("Goal" :expand (col-expansion-inner i j 0))))

(defthm col-expansion-outer-j-0-0
  (equal (col-expansion-outer j 0 0)
	 (if (equal j 0)
	     (list (binop 0 0))
	   nil))
  :hints (("Goal" :expand (col-expansion-outer j 0 0))))

(defthm col-expansion-outer-with-m=0
  (equal (col-expansion-outer j 0 n)
	 (row-expansion-inner 0 j n))
  :hints (("Goal" :induct (col-expansion-outer j 0 n))))

;; This culminates to the following result, which happens to be the
;; base case for the proof that the row-sum is equal to the
;; column-sum.  In the case when there's only one row, you can compute
;; the sum by either adding "all" the row sums or by adding all the
;; column "sums".

(defthm ok-to-swap-inner-outer-expansions-base
  (implies (zp m)
	   (equal (sumlist (row-expansion-outer 0 m n))
		  (sumlist (col-expansion-outer 0 m n))))
  :hints (("Goal"
	   :cases ((not (integerp m))
		   (and (integerp m) (equal m 0))
		   (and (integerp m) (not (equal m 0)))))))

;; Another "empty" case:

(defthm row-expansion-outer-i>m
  (implies (> i m)
	   (equal (row-expansion-outer i m n) nil)))

;; The following lemma simply "opens up" some outer row sums.

(defthm row-expansion-outer-m-m-n
  (implies (and (integerp m) (<= 0 m))
	   (equal (sumlist (row-expansion-outer m m n))
		  (sumlist (row-expansion-inner m 0 n))))
  :hints (("Goal" :expand (row-expansion-outer m m n))))

;; And now the first important theorem!  The sum of all the a_{i,j}
;; can be computed either by adding the sum of the m rows or by adding
;; the sum of the n columns.  The results will be equal.

(defthm ok-to-swap-inner-outer-sums
  (equal (sumlist (row-expansion-outer 0 m n))
	 (sumlist (col-expansion-outer 0 m n)))
  :hints (("Goal"
	   :induct (natural-induction-hint m))
	  ("Subgoal *1/2"
	   :use ((:instance row-expansion-outer-split (i 0))
		 (:instance col-expansion-outer-split (j 0))))))

;; Now, we define a lower-triangular row sum.  Basically, instead of
;; adding up all the elements of the ith row, we only add up the
;; elements through the ith column -- a_{i,1} ... a_{i,i}.  So only
;; the values at or below the "diagonal" are non-zero.

(defun row-expansion-outer-lt (i m n)
  (declare (xargs :measure (nfix (1+ (- m i)))))
  (if (and (integerp i) (integerp m) (<= 0 i) (<= i m))
      (cons (sumlist (row-expansion-inner
		      i 0 (if (< i n) i n)))
	    (row-expansion-outer-lt (1+ i) m n))
    nil))


;; We define the same sum, but using column-sums instead.  The idea is
;; to add up the elements of column j but only starting at row j.
;; I.e., we add up a_{j,j}, a_{j+1,j}, ..., a_{m,j}.

(defun col-expansion-outer-lt (j m n)
  (declare (xargs :measure (nfix (1+ (- n j)))))
  (if (and (integerp j) (integerp n) (<= 0 j) (<= j n))
      (cons (sumlist (col-expansion-inner j j m))
	    (col-expansion-outer-lt (1+ j) m n))
    nil))

;; Here's a different approach on the same idea.  We define a new
;; b_{i,j} with the property that b_{i,j} = 0 for i<j.

(defun lt-binop (i j)
  (if (< i j)
      0
    (binop i j)))

;; So now, we can define its row-sum as before....

(defun row-expansion-inner-using-lt (i j n)
  (declare (xargs :measure (nfix (1+ (- n j)))))
  (if (and (integerp j) (integerp n) (<= 0 j) (<= j n))
      (cons (lt-binop i j)
	    (row-expansion-inner-using-lt i (1+ j) n))
    nil))

(defun row-expansion-outer-using-lt (i m n)
  (declare (xargs :measure (nfix (1+ (- m i)))))
  (if (and (integerp i) (integerp m) (<= 0 i) (<= i m))
      (cons (sumlist (row-expansion-inner-using-lt i 0 n))
	    (row-expansion-outer-using-lt (1+ i) m n))
    nil))

;; ... and also its column sum.

(defun col-expansion-inner-using-lt (i j m)
  (declare (xargs :measure (nfix (1+ (- m i)))))
  (if (and (integerp i) (integerp m) (<= 0 i) (<= i m))
      (cons (lt-binop i j)
	    (col-expansion-inner-using-lt (1+ i) j m))
    nil))

(defun col-expansion-outer-using-lt (j m n)
  (declare (xargs :measure (nfix (1+ (- n j)))))
  (if (and (integerp j) (integerp n) (<= 0 j) (<= j n))
      (cons (sumlist (col-expansion-inner-using-lt 0 j m))
	    (col-expansion-outer-using-lt (1+ j) m n))
    nil))

;; From the main theorem above, we can either add the rows or add the
;; columns, and the result won't matter.

(defthm ok-to-swap-inner-outer-sums-using-lt
  (equal (sumlist (row-expansion-outer-using-lt 0 m n))
	 (sumlist (col-expansion-outer-using-lt 0 m n)))
  :hints (("Goal"
	   :by (:functional-instance ok-to-swap-inner-outer-sums
				     (binop lt-binop)
				     (row-expansion-outer row-expansion-outer-using-lt)
				     (row-expansion-inner row-expansion-inner-using-lt)
				     (col-expansion-outer col-expansion-outer-using-lt)
				     (col-expansion-inner col-expansion-inner-using-lt)))))

;; Now suppose we look at a column sum starting at a_{i,j} when i >= j
;; -- it doesn't matter if we use the regular column sum or the
;; triangular sum.

(defthm col-expansion-inner-using-lt-to-binop-large-i
  (implies (and (integerp i) (integerp j) (integerp n)
		(<= 0 j) (<= j i))
	   (equal (col-expansion-inner i j n)
		  (col-expansion-inner-using-lt i j n)))
  :hints (("Goal" :induct (col-expansion-inner-using-lt i j n))))

;; If i<=j then the expansion of the column sum starting at a_{j,j} is
;; the same as the "triangular" expansion starting at a_{i,j}, since
;; the a_{i,j}, a_{i+1,j}, ..., a_{j,j} terms are all ignored by the
;; "triangular" sum.

(defthm col-expansion-inner-using-lt-to-binop
  (implies (and (integerp i) (integerp j) (integerp n)
		(<= 0 i) (<= i j))
	   (equal (sumlist (col-expansion-inner j j n))
		  (sumlist (col-expansion-inner-using-lt i j n))))
  :hints (("Goal" :induct (col-expansion-inner-using-lt i j n))))

;; So in particular, the triangular sum of the entire jth column is
;; equal to the sum of the column starting at a_{j,j}.

(defthm col-expansion-inner-j-j-n
  (implies (and (integerp j) (integerp n) (<= 0 j))
	   (equal (sumlist (col-expansion-inner j j n))
		  (sumlist (col-expansion-inner-using-lt 0 j n))))
  :hints (("Goal"
	   :by (:instance col-expansion-inner-using-lt-to-binop (i 0)))))

;; The triangular sum of the ith row starting from column j is zero if
;; j > i -- since a_{i,j} is ignored for i<j.

(defthm row-expansion-inner-using-lt-to-binop-large-j
  (implies (< i j)
	   (equal (sumlist (row-expansion-inner-using-lt i j m)) 0)))

;; From that, we can conclude that the sum of the ith row up to column
;; i is equal to the "triangular" sum of row i.

(defthm row-expansion-inner-using-lt-to-binop
  (implies (and (integerp i) (integerp n) (< i n))
	   (equal (sumlist (row-expansion-inner i j i))
		  (sumlist (row-expansion-inner-using-lt i j n)))))

;; Some more empty cases!  This time, we look at the sums of the
;; b_{i,j} -- recall that b_{i,j} is zero for i<j.

(defthm col-expansion-outer-lt-non-integerp-n
  (implies (not (integerp n))
	   (equal (col-expansion-outer-lt i m n) nil)))

(defthm col-expansion-outer-using-lt-non-integerp-n
  (implies (not (integerp n))
	   (equal (col-expansion-outer-using-lt i m n) nil)))

(defthm col-expansion-inner-using-lt-to-binop-large-j
  (implies (and (integerp i)
		(<= 0 i)
		(integerp j)
		(<= 0 j)
		(integerp m)
		(<= 0 m)
		(< m j))
	   (equal (sumlist (col-expansion-inner-using-lt i j m)) 0)))

;; What this means is that the triangular column sum of the a_{i,j} is
;; the same as the sum of the b_{i,j}, since b_{i,j} is equal to
;; a_{i,j} if i<=j and 0 otherwise.

(defthm col-expansion-outer-lt-=-col-expansion-outer-using-lt
  (implies (integerp m)
	   (equal (sumlist (col-expansion-outer-lt i m n))
		  (sumlist (col-expansion-outer-using-lt i m n))))
  :hints (("Goal" :induct (col-expansion-outer-using-lt i m n))
	  ("Subgoal *1/1.8"
	   :use ((:instance col-expansion-inner-using-lt-to-binop-large-j
			    (i 0)
			    (j i)
			    (m m)))
	   :in-theory (disable col-expansion-inner-using-lt-to-binop-large-j))
	  ("Subgoal *1/1.5"
	   :use ((:instance col-expansion-inner-using-lt-to-binop
			    (i 1)
			    (j i)
			    (n m)))
	   :in-theory (disable col-expansion-inner-using-lt-to-binop))

	  )
  :rule-classes
  ((:rewrite :corollary (equal (sumlist (col-expansion-outer-lt i m n))
			       (sumlist (col-expansion-outer-using-lt i m n))))))

;; Now, we try the same theorem, but using row sums instead.  First,
;; some boundary conditions.  When we're looking at a row beyond the
;; last row, both terms below are nil, and so they're equal.

(defthm row-expansion-inner-large-i
  (implies (<= n i)
	   (equal (row-expansion-inner i j n)
		  (row-expansion-inner-using-lt i j n))))

;; And since the terms b_{i,j} are zero for i<j, the sum of the ith
;; row can be computed by going up just to column i.

(defthm row-expansion-inner-large-n
  (implies (and (integerp i) (integerp n) (< i n))
	   (equal (sumlist (row-expansion-inner-using-lt i j n))
		  (sumlist (row-expansion-inner-using-lt i j i)))))

;; And so, the row sum of the b_{i,j} is the same as the triangular
;; sum of the a_{i,j}.

(defthm row-expansion-outer-lt-=-row-expansion-outer-using-lt
  (implies (integerp n)
	   (equal (row-expansion-outer-lt i m n)
		  (row-expansion-outer-using-lt i m n))))

;; Therefore, the triangular row sum and triangular column sum compute
;; the same value.

(defthm ok-to-swap-inner-outer-expansions-lt
  (implies (integerp n)
	   (equal (sumlist (row-expansion-outer-lt 0 m n))
		  (sumlist (col-expansion-outer-lt 0 m n)))))

;; Next, let's consider the special case of square "matrices".  I.e.,
;; when we have as many rows as columns.  First, we give the
;; definition of the row and column sums as just a special case of the
;; rectangular case.  Obviously, they compute the same value!

(defun row-expansion-outer-m=n-aux (i n)
  (row-expansion-outer i n n))

(defun col-expansion-outer-m=n-aux (j n)
  (col-expansion-outer j n n))

(defthm ok-to-swap-inner-outer-expansions-m=n-aux
  (equal (sumlist (row-expansion-outer-m=n-aux 0 n))
	 (sumlist (col-expansion-outer-m=n-aux 0 n))))

;; Now, we give a direct definition of the row sum...

(defun row-expansion-outer-m=n (i n)
  (declare (xargs :measure (nfix (1+ (- n i)))))
  (if (and (integerp i) (integerp n) (<= 0 i) (<= i n))
      (cons (sumlist (row-expansion-inner i 0 n))
	    (row-expansion-outer-m=n (1+ i) n))
    nil))

;; ... as well as the column sum.

(defun col-expansion-outer-m=n (j n)
  (declare (xargs :measure (nfix (1+ (- n j)))))
  (if (and (integerp j) (integerp n) (<= 0 j) (<= j n))
      (cons (sumlist (col-expansion-inner 0 j n))
	    (col-expansion-outer-m=n (1+ j) n))
    nil))

;; Clearly, these functions return the same value as the original ones
;; that used the underlying rectangular sums.

(local
 (defthm row-col-expansion-outer-m=n-aux
   (and (equal (row-expansion-outer-m=n i n)
	       (row-expansion-outer-m=n-aux i n))
	(equal (col-expansion-outer-m=n i n)
	       (col-expansion-outer-m=n-aux i n)))))

;; So we can conclude that the row sum is the same as the column sum.

(defthm ok-to-swap-inner-outer-expansions-m=n
  (equal (sumlist (row-expansion-outer-m=n 0 n))
	 (sumlist (col-expansion-outer-m=n 0 n))))

;; Now we do the same step but using triangular sums.  First, we
;; define triangular sums of square matrices using the rectangular
;; definitions.  We show the two triangular sums must be equal to each
;; other.

(defun row-expansion-outer-lt-m=n-aux (i n)
  (row-expansion-outer-lt i n n))

(defun col-expansion-outer-lt-m=n-aux (j n)
  (col-expansion-outer-lt j n n))

(defthm ok-to-swap-inner-outer-expansions-lt-m=n-aux
  (implies (integerp n)
	   (equal (sumlist (row-expansion-outer-lt-m=n-aux 0 n))
		  (sumlist (col-expansion-outer-lt-m=n-aux 0 n)))))

;; Now, we give an explicit definition for the triangular sums.

(defun row-expansion-outer-lt-m=n (i n)
  (declare (xargs :measure (nfix (1+ (- n i)))))
  (if (and (integerp i) (integerp n) (<= 0 i) (<= i n))
      (cons (sumlist (row-expansion-inner i 0 (if (< i n) i n)))
	    (row-expansion-outer-lt-m=n (1+ i)  n))
    nil))

(defun col-expansion-outer-lt-m=n (j n)
  (declare (xargs :measure (nfix (1+ (- n j)))))
  (if (and (integerp j) (integerp n) (<= 0 j) (<= j n))
      (cons (sumlist (col-expansion-inner j j n))
	    (col-expansion-outer-lt-m=n (1+ j) n))
    nil))

(local
 (defthm row-col-expansion-outer-lt-m=n-aux
   (and (equal (row-expansion-outer-lt-m=n i n)
	       (row-expansion-outer-lt-m=n-aux i n))
	(equal (col-expansion-outer-lt-m=n i n)
	       (col-expansion-outer-lt-m=n-aux i n)))))

;; And of course, we conclude that we can sum either rows first or
;; columns first.

(defthm ok-to-swap-inner-outer-expansions-lt-m=n
  (implies (integerp n)
	   (equal (sumlist (row-expansion-outer-lt-m=n 0 n))
		  (sumlist (col-expansion-outer-lt-m=n 0 n)))))

;; Finally, we consider a simple sum of a_0+a_1+...+a_n.  We show that
;; if we multiply each of a_i by a constant k, the sum of adding up
;; the k*a_i terms is the same as k times the sum of the a_i.

;; First, here is the definition of the a_i and k.

(encapsulate
 ((zeroop () t)
  (oneop (i) t))

 (local (defun zeroop () 0))
 (local (defun oneop (i) (fix i)))

 (defthm zeroop-type-prescription
   (acl2-numberp (zeroop))
   :rule-classes (:rewrite :type-prescription))

 (defthm oneop-type-prescription
   (acl2-numberp (oneop i))
   :rule-classes (:rewrite :type-prescription))
)

;; Now here is the sum of the a_i....

(defun expansion-oneop (i n)
  (declare (xargs :measure (nfix (1+ (- n i)))))
  (if (and (integerp i) (integerp n) (<= 0 i) (<= i n))
      (cons (oneop i)
	    (expansion-oneop (1+ i) n))
    nil))

;; And here is the sum of the k*a_i....

(defun expansion-oneop-times-zeroop (i n)
  (declare (xargs :measure (nfix (1+ (- n i)))))
  (if (and (integerp i) (integerp n) (<= 0 i) (<= i n))
      (cons (* (oneop i) (zeroop))
	    (expansion-oneop-times-zeroop (1+ i) n))
    nil))

;; And now, sum(k*a_0, k*a_1, ..., k*a_n) = k * sum(a_0, ..., a_n)

(defthm factor-constant-from-expansion
  (equal (sumlist (expansion-oneop-times-zeroop i n))
	 (* (sumlist (expansion-oneop i n)) (zeroop)))
  :hints (("Goal" :induct (expansion-oneop i n))))