File: multiset.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 (1034 lines) | stat: -rw-r--r-- 30,955 bytes parent folder | download
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
;;; multiset.lisp
;;; - Useful lemmas about multisets
;;; - Multiset extension of a well-founded relation is a well-founded relation.
;;; Created: 10-6-99 Last Revision: 19-09-00
;;; ============================================================================

;;; To certify this book:

; Includes mods by Daron Vroon and Pete Manolios for v2-8 ordinals changes.

#|
(in-package "ACL2")

(defpkg "MUL" (union-eq *acl2-exports*
			(union-eq
			 *common-lisp-symbols-from-main-lisp-package*
			 '(remove-one multiset-diff
				      make-ord ctoa atoc))))

(certify-book "multiset" 1)
|#

(in-package "MUL")
(include-book "../../../../ordinals/e0-ordinal")


(ACL2::set-verify-guards-eagerness 2)

;;; *****************************************************
;;; PART I: DEFINITIONS AND USEFUL LEMMAS ABOUT MULTISETS
;;; *****************************************************

;;; ============================================================================
;;; 1. Multisets (unordered lists).
;;; ============================================================================


;;; ----------------------------------------------------------------------------
;;; 1.1 equal-set and properties.
;;; ----------------------------------------------------------------------------

(defun equal-set (x y)
  (declare (xargs :guard
		  (if (acl2::eqlable-listp y)
		      (true-listp x)
		      (if (acl2::eqlable-listp x)
			  (true-listp y)
			  nil))))
  (and (subsetp x y) (subsetp y x)))

(local
 (defthm subsetp-cons
   (implies (subsetp l m)
	    (subsetp l (cons x m)))))

(local
 (defthm subsetp-reflexive
   (subsetp l l)))

(local
 (defthm subsetp-transitive
   (implies (and (subsetp l m)
		 (subsetp m n))
	    (subsetp l n))))

(acl2::defequiv equal-set)

(acl2::defcong equal-set equal (consp l) 1)

;;; ----------------------------------------------------------------------------
;;; 1.2 remove-one and properties.
;;; ----------------------------------------------------------------------------

(defun remove-one (x l)
  (cond ((atom l) l)
	((equal x (car l)) (cdr l))
	(t (cons (car l) (remove-one x (cdr l))))))

(defthm remove-one-true-listp
  (implies (true-listp l)
	   (true-listp (remove-one x l))))

(local
 (defthm member-remove-one
   (implies (and (member x l) (not (equal x y)))
	    (member x (remove-one y l)))))

(local
 (defthm remove-one-conmute
   (equal (remove-one x (remove-one y l))
	  (remove-one y (remove-one x l)))))

(local
 (defthm remove-one-member
   (implies (not (member x l))
	    (not (member x (remove-one y l))))))

(local
 (defthm remove-one-subsetp
   (subsetp (remove-one x l) l)))

;;; ----------------------------------------------------------------------------
;;; 1.3 Multiset difference and properties.
;;; ----------------------------------------------------------------------------

(defun multiset-diff (m n)
  (if (atom n)
      m
      (multiset-diff (remove-one (car n) m) (cdr n))))

(defthm multiset-diff-true-listp
  (implies (and (true-listp m)
		(true-listp n))
	   (true-listp (multiset-diff m n))))

(local
 (defthm multiset-diff-member
   (implies (and (member x n) (not (member x m)))
	    (member x (multiset-diff n m)))))

(local
 (defthm multiset-diff-removing-the-same-element
   (implies (and (member x n) (member x m))
	    (equal (multiset-diff (remove-one x m)
				  (remove-one x n))
		   (multiset-diff m n)))))

(local
 (defthm multiset-diff-nil
   (not (consp (multiset-diff nil l)))))

(local
 (defthm subsetp-multiset-diff
   (subsetp (multiset-diff n m) n)))

(defthm multiset-diff-remove-one-not-consp
  (not (consp (multiset-diff (remove-one x l) l)))
  :hints (("Goal" :induct (acl2::len l))))

(defthm list-multiset-diff-1
  (implies (not (member x l))
	   (equal (multiset-diff (list x) l)
		  (list x))))

(defthm list-multiset-diff-2
  (implies (not (member x l))
	   (equal (multiset-diff l (list x))
		  l)))

(defthm multiset-diff-append-1
  (equal (multiset-diff (append m1 m2)
			(append m1 m3))
	 (multiset-diff m2 m3)))

;;; REMARK: An analogue property is not true for (multiset-diff (append
;;; m1 m2) (append m3 m2)) (take for example '(3 2 1 3) and '(1
;;; 3)). Nevertheless, we prove below that (multiset-diff (append m1 m2)
;;; (append m3 m2)) is exactly the same set (although not the same list
;;; in general) as (multiset-diff m1 m3). This rewrite rule will be used
;;; together with two congruence rules relating forall-exists-rel-bigger
;;; and equal-se (see 2.3 in this file). Furthermore, using functional
;;; instantiation, these two congruence rules are generated for every
;;; particular relation "rel" by defmul (see defmul.lisp).



(encapsulate
 ()

 (local
  (defun my-occur (x l)
    (cond ((atom l) 0)
	  ((equal x (car l)) (1+ (my-occur x (cdr l))))
	  (t (my-occur x (cdr l))))))

 (local
  (defthm my-occur-append
    (equal (my-occur x (append l m))
	   (+ (my-occur x l) (my-occur x m)))))

 (local
  (defthm my-occur-remove-one
    (equal (my-occur x (remove-one y l))
	   (if (and (equal x y) (member x l))
	       (1- (my-occur x l))
	       (my-occur x l)))))

 (local
  (defthm subsetp-multiset-diff-member
    (implies (not (member x l))
	     (not (member x (multiset-diff l m))))))

 (local
  (defthm multiset-diff-my-occur
    (iff (member x (multiset-diff l m))
	 (> (my-occur x l) (my-occur x m)))))

 (local
  (defthm multiset-diff-append-2-lemma
    (iff (member x (multiset-diff (append m1 m2)
				  (append m3 m2)))
	 (member x (multiset-diff m1 m3)))))

 (local
  (defun not-subsetp-witness (m1 m2)
    (declare (xargs :verify-guards nil))
    (if (atom m1)
	nil
	(if (member (car m1) m2)
	    (not-subsetp-witness (cdr m1) m2)
	    (car m1)))))

 (local
  (defthm not-subsetp-witness-lemma
    (equal (subsetp m1 m2)
	   (implies (member (not-subsetp-witness m1 m2) m1)
		    (member (not-subsetp-witness m1 m2) m2)))))

 (defthm multiset-diff-append-2
   (equal-set (multiset-diff (append m1 m2)
			     (append m3 m2))
	      (multiset-diff m1 m3))))

;;; ----------------------------------------------------------------------------
;;; 1.4 A useful meta rule about "multiset-diff"
;;; ----------------------------------------------------------------------------

;;; This rule rewrites expressions of the form:

;;; (multiset-diff (list* x1 ... xm l)
;;; 	           (list* y1 ... yk l))

;;; to the equivalent (w.r.t. equal-set):

;;; (multiset-diff (list x1 ... xm)
;;;    	           (list y1 ... yk))



(defun initial-cars (l)
  (if (and (consp l)
	   (eq (car l) 'cons)
	   (consp (cdr l))
	   (consp (cddr l))
	   (eq (cdddr l) 'nil))
      (list 'cons (cadr l) (initial-cars (caddr l)))
      ''nil))

(defun last-cdr (l)
  (if (and (consp l)
	   (eq (car l) 'cons)
	   (consp (cdr l))
	   (consp (cddr l))
	   (eq (cdddr l) 'nil))
      (last-cdr (caddr l))
      l))

(defun exclude-last-cdr-multiset-diff (x)
  (if (and (consp x)
	   (eq (car x) 'multiset-diff)
	   (consp (cdr x))
	   (consp (cddr x))
	   (eq (cdddr x) 'nil))
      (list 'multiset-diff
	    (initial-cars (cadr x))
	    (initial-cars (caddr x)))
      x))

(acl2::defevaluator ev-multiset-diff ev-multiset-diff-list
		    ((cons x l)
		     (multiset-diff l1 l2)
		     (equal l1 l2)))

(local
 (defthm ev-multiset-diff-append-initial-cars-last-cdr
   (equal (append (ev-multiset-diff (initial-cars l) a)
		  (ev-multiset-diff (last-cdr l) a))
	  (ev-multiset-diff l a))))

(defun hypothesis-multiset-diff-meta (x)
  (if (and (consp x)
	   (consp (cdr x))
	   (consp (cddr x))
	   (eq (cdddr x) 'nil))
      (list 'equal
	    (last-cdr (cadr x))
	    (last-cdr (caddr x)))
      ''nil))

(defthm multiset-diff-meta
  (implies
   (ev-multiset-diff (hypothesis-multiset-diff-meta x) a)
   (equal-set (ev-multiset-diff x a)
	      (ev-multiset-diff (exclude-last-cdr-multiset-diff x) a)))
  :rule-classes ((:meta :trigger-fns (multiset-diff)))
  :hints (("Goal"
	   :use ((:instance
		  multiset-diff-append-2
		  (m1 (ev-multiset-diff (initial-cars (cadr x)) a))
		  (m2 (ev-multiset-diff (last-cdr (cadr x)) a))
		  (m3 (ev-multiset-diff (initial-cars (caddr x)) a))))
	   :in-theory (disable initial-cars))))




;;; **********************************************
;;; PART II: WELL-FOUNDEDNES OF MULTISET RELATIONS
;;; **********************************************


;;; ============================================================================
;;; 2. Multiset extension of a well-founded relation is a well-founded
;;;    relation.
;;; ============================================================================

;;; ----------------------------------------------------------------------------
;;; 2.1 A general well-founded relation
;;; ----------------------------------------------------------------------------

;;; Definition of a general well-founded relation on a set:
;;; - mp:  the measure property defining the set.
;;; - rel: the well-founded relation defined on elements satisfying mp.
;;; - fn:  the embedding justifying the well-foundedness of rel.

(encapsulate
 ((mp (x) booleanp)
  (rel (x y) booleanp)
  (fn (x) o-p))

 (local (defun mp (x) (declare (ignore x)) nil))
 (local (defun rel (x y) (declare (ignore x y)) nil))
 (local (defun fn (x) (declare (ignore x)) 1))

 (defthm mp-o-p-fn
   (implies (mp x) (o-p (fn x))))

 (defthm rel-o<-fn
   (implies (and (mp x)
                 (mp y)
                 (rel x y))
            (o< (fn x) (fn y))))

 (defthm rel-well-founded-relation-on-mp
   (and (implies (mp x) (o-p (fn x)))
	(implies (and (mp x)
		      (mp y)
		      (rel x y))
		 (o< (fn x) (fn y))))
   :rule-classes :well-founded-relation))

;;; daron: now we convert ops to the e0-ordinalps:

(defmacro fn0 (x)
  `(ctoa (fn ,x)))

(defthm mp-e0-ordinalp-fn0
  (implies (mp x) (e0-ordinalp (fn0 x))))

(defthm rel-e0-ord-<-fn0
  (implies (and (mp x)
                (mp y)
                (rel x y))
           (e0-ord-< (fn0 x) (fn0 y))))

;;; REMARK: For our purpouses, we need another property about "fn", its
;;; values must be greater than 0.  Thus, we define a new measure
;;; function "fn1" equal to "fn" except for integers, where 1 is added.


(defun add1-if-integer (x)
  (if (integerp x) (1+ x) x))

(local
 (defmacro fn1 (x)
   `(add1-if-integer (fn0 ,x))))

;;; We prove now that fn1 is also an embedding, with the aditional
;;; property that never returns zero

(local
 (defthm e0-ord-<-add1-if-integer
   (implies (and (e0-ordinalp x) (e0-ordinalp y) (e0-ord-< x y))
	    (e0-ord-< (add1-if-integer x) (add1-if-integer y)))))

(local
 (defthm add1-if-integer-e0-ordinalp-not-equal-0
   (implies (e0-ordinalp x)
	    (not (equal (add1-if-integer x) 0)))))

(local
 (defthm add1-if-integer-e0-ordinalp
   (implies (e0-ordinalp x)
	    (e0-ordinalp (add1-if-integer x)))))

(local (in-theory (disable add1-if-integer)))

(local
 (defthm fn-e0-ordinalp
   (implies (mp x)
	    (e0-ordinalp (fn0 x)))))
;   :hints (("Goal" :use rel-e0-ordinalp-embedding-on-mp))))

;;; Here they are:

(local
 (defthm fn1-e0-ordinalp
   (implies (mp x)
	    (and (e0-ordinalp (fn1 x))
		 (not (equal (fn1 x) 0))))))


(local
 (defthm rel-well-founded-relation-on-mp-rewrite
   (implies (and (mp x)
                 (mp y)
                 (rel x y))
	    (e0-ord-< (fn1 x) (fn1 y)))
   :hints (("Goal" :use rel-well-founded-relation-on-mp))))

;;; ----------------------------------------------------------------------------
;;; 2.2 Induced multiset relation
;;; ----------------------------------------------------------------------------

;;; Now we define the multiset extension on multisets of elements with
;;; the MP-property.  We need to define:
;;; - The measure property defining multisets of elements:
;;;   mp-true-listp.
;;; - The well-founded relation: mul-rel.
;;; - The embedding justifying well-foundedness: mp-fn-e0-ord.


;;; Measure property

(defun mp-true-listp (l)
  (if (atom l)
      (equal l nil)
    (and (mp (car l)) (mp-true-listp (cdr l)))))

;;; Multiset relation

(defun exists-rel-bigger (x l)
  (cond ((atom l) nil)
	((rel x (car l)) t)
	(t (exists-rel-bigger x (cdr l)))))

(defun forall-exists-rel-bigger (l m)
  (if (atom l)
      t
      (and (exists-rel-bigger (car l) m)
	   (forall-exists-rel-bigger (cdr l) m))))

(defun mul-rel (n m)
  (let ((m-n (multiset-diff m n))
	(n-m (multiset-diff n m)))
    (and (consp m-n) (forall-exists-rel-bigger n-m m-n))))
;;; Embedding

(defun insert-e0-ord-< (x l)
  (cond ((atom l) (cons x l))
	((not (e0-ord-< x (car l)))  (cons x l))
	(t (cons (car l) (insert-e0-ord-< x (cdr l))))))

(defun map-fn-e0-ord (l)
  (declare (xargs :guard (mp-true-listp l)))
  (if (consp l)
      (insert-e0-ord-< (add1-if-integer (fn0 (car l)))
		       (map-fn-e0-ord (cdr l)))
      0))

;;; ----------------------------------------------------------------------------
;;; 2.3 An interesting congruence.
;;; ----------------------------------------------------------------------------

;;; These rules are used in conjunction with rewite rules that simplify
;;; multiset differences (w.r.t equal or equal-set)

;;; REMARK: These two congruence rules will be instantiated by every
;;; defmul macro to prove the analogues for every particular rel.


(encapsulate
 ()

 (local (defthm exists-rel-bigger-subsetp
	  (implies (and
		    (exists-rel-bigger x a)
		    (subsetp a b))
		   (exists-rel-bigger x b))))

 (acl2::defcong equal-set iff (forall-exists-rel-bigger l m) 2))


(encapsulate
 ()

 (local
  (defthm forall-exists-rel-bigger-subsetp
    (implies (and
	      (forall-exists-rel-bigger b l)
	      (subsetp a b))
	     (forall-exists-rel-bigger a l))))

 (acl2::defcong equal-set iff (forall-exists-rel-bigger l m) 1))


(in-theory
 (disable
  equal-set-implies-iff-forall-exists-rel-bigger-2
  equal-set-implies-iff-forall-exists-rel-bigger-1))


;;; ----------------------------------------------------------------------------
;;; 2.4 Some ad hoc lemmas about ordinals.
;;; ----------------------------------------------------------------------------


(local
 (defthm weak-e0-ord-<-trichotomy
   (implies (e0-ord-< o1 o2)
	    (not (e0-ord-< o2 o1)))))

(local (in-theory (disable weak-e0-ord-<-trichotomy)))

(local
 (defthm e0-ord-<-trichotomy
   (implies (and (e0-ordinalp o1)
		 (e0-ordinalp o2)
		 (not (equal o1 o2))
		 (not (e0-ord-< o1 o2)))
	    (e0-ord-< o2 o1))
   :rule-classes :type-prescription))

(defthm e0-ord-<-irreflexive
  (not (e0-ord-< x x)))

(local (in-theory (disable e0-ord-<-irreflexive)))

(local
 (defthm e0-ord-<-transitive-corollary
   (implies (and (e0-ordinalp o1)
		 (e0-ordinalp o3)
		 (e0-ord-< o3 o2)
		 (not (e0-ord-< o1 o2)))
	    (e0-ord-< o3 o1))
   :rule-classes :type-prescription
   :hints (("Goal" :in-theory (enable e0-ord-<-trichotomy)))))


;;; ----------------------------------------------------------------------------
;;; 2.5 Well-foundedness of mul-rel
;;; ----------------------------------------------------------------------------


;;; Our goal is to prove that mul-rel is a well-founded relation on elements
;;;   satisfying mp-true-listp. We have to prove:
;;; 1) If (mp-true-listp m), the (map-fn-e0-ord m) is an ordinal.
;;; 2) If l and m are mp-true-listp and (mul-rel n m), then
;;;    (e0-ord-< (map-fn-e0-ord n) (map-fn-e0-ord m))


;;; ············································································
;;; 2.5.1 The measure is an ordinal.
;;; ············································································

(local
 (defthm e0-ordinalp-insert-e0-ord-<
   (implies (and (e0-ordinalp l)
		 (e0-ordinalp o)
		 (not (equal o 0)))
	    (e0-ordinalp (insert-e0-ord-< o l)))
   :hints (("Goal" :in-theory (enable weak-e0-ord-<-trichotomy)))))

(local
 (defthm e0-ordinalp-map-fn-e0-ord
   (implies (mp-true-listp m)
	    (e0-ordinalp (map-fn-e0-ord m)))))


;;; ············································································
;;; 2.5.2 The measure is an embedding
;;; ············································································



;;; SKETCH OF THE PROOF:
;;; Let N and M such that (mul-rel N M). We have to prove that
;;;      (e0-ord-< (map-fn-e0-ord N) (map-fn-e0-ord M))
;;;  Suppose (fn x) and (fn y) are the biggest
;;; (w.r.t. e0-ord-<) elements of fn[N] and fn[M], respectively. Since
;;; they are ordinals, three cases are possible:

;;; 1) (fn x) < (fn y). Done.

;;; 2) (fn x) > (fn y). This is not possible: in that case x is in N-M
;;; and by the multiset order definition, exists z in M-N such that (rel
;;; x z) and consequently (fn z) > (fn x) >= (fn y). This is a
;;; contradiction with the fact that (fn y) is the biggest element of
;;; fn[M].

;;; 3) (fn x) = (fn y). In that case, x is in M, since otherwise it
;;; would exist z in M-N such that (rel z x) and the same contradiction
;;; as in case 2) appears. So N-{x} and M-{x} are multisets and N-{x}
;;; bigger than M-{x} w.r.t. multiset relation with the property that
;;; the ordinal mesures of these two multisets are respectively the
;;; cdr's of the ordinal measures of N and M. Induction hypothesis can
;;; be applied here.
;;;
;;; We must define an induction scheme to mimic this proof sketch.
;;; This will be done later by induction-multiset
;;; But we previously prove some lemmas...



;;; max-fn1-list (an element in l with maximal (fn1 ..)) and properties.
;;; ······································································

(local
 (defun max-fn1-list (l)
   (declare (xargs :verify-guards nil))
   (cond ((atom l) nil)
	 ((atom (cdr l)) (car l))
	 (t (let ((max-cdr (max-fn1-list (cdr l))))
	      (if (e0-ord-< (fn1 max-cdr) (fn1 (car l)))
		  (car l)
		  max-cdr))))))

(local
 (defthm max-fn1-list-member
   (implies (consp l)
	    (member (max-fn1-list l) l))))

(local
 (defthm mp-member-true-listp
   (implies (and (mp-true-listp l)
		 (member x l))
	    (mp x))
   :rule-classes :type-prescription))

(local
 (defthm max-fn1-listp-mp
   (implies (and (consp l) (mp-true-listp l))
	    (mp (max-fn1-list l)))))

(local
 (defthm max-fn1-list-maximal
   (implies (member x l)
	    (not (e0-ord-< (fn1 (max-fn1-list l)) (fn1 x))))
   :hints (("Goal" :in-theory (enable e0-ord-<-irreflexive)))))




;;; An "alternative definition" of "map-fn-e0-ord".
;;; ···············································

(local
 (encapsulate
  ()

  (local
   (defthm insert-e0-ord-<-conmute
     (implies (and (e0-ordinalp x) (e0-ordinalp y))
	      (equal (insert-e0-ord-< x (insert-e0-ord-< y l))
		     (insert-e0-ord-< y (insert-e0-ord-< x l))))
     :hints (("Goal" :in-theory (enable
				 weak-e0-ord-<-trichotomy
				 e0-ord-<-trichotomy
				 e0-ord-<-transitive-corollary)))))

  (local
   (defthm another-definition-of-map-fn-e0-ord-lemma
     (implies (and (mp-true-listp l) (member x l))
	      (equal (map-fn-e0-ord l)
		     (insert-e0-ord-< (fn1 x)
				      (map-fn-e0-ord (remove-one x l)))))
     :rule-classes nil))

  (local
   (defun bigger-than-list (x l)
     (declare (xargs :verify-guards nil))
     (cond ((atom l) t)
	   ((e0-ord-< x (car l)) nil)
	   (t (bigger-than-list x (cdr l))))))

  (local
   (defthm bigger-than-list-insert
     (implies (and (bigger-than-list x l)
		   (not (e0-ord-< x y)))
	      (bigger-than-list x (insert-e0-ord-< y l)))))

  (local
   (defthm bigger-than-list-max-fn1-map-fn-e0-ord-subsetp
     (implies (subsetp m l)
	      (bigger-than-list (fn1 (max-fn1-list l))
				(map-fn-e0-ord m)))
     :rule-classes nil))

  (local
   (defthm bigger-than-list-max-fn1-map-fn-e0-ord-remove-one
     (implies (equal (fn1 x) (fn1 (max-fn1-list l)))
	      (bigger-than-list (fn1 x) (map-fn-e0-ord (remove-one x l))))
     :hints (("Goal"
	      :use (:instance bigger-than-list-max-fn1-map-fn-e0-ord-subsetp
			      (m (remove-one x l)))))))

  (local
   (defthm bigger-than-list-insert-e0-ord-<-cons
     (implies (bigger-than-list (fn1 x) l)
	      (equal (insert-e0-ord-< (fn1 x) l)
		     (cons (fn1 x) l)))))

  (defthm another-definition-of-map-fn-e0-ord
    (implies (and (equal (fn1 x) (fn1 (max-fn1-list l)))
		  (mp-true-listp l)
		  (member x l))
	     (equal (map-fn-e0-ord l)
		    (cons (fn1 x) (map-fn-e0-ord (remove-one x l)))))
    :hints (("Goal" :use another-definition-of-map-fn-e0-ord-lemma))
    :rule-classes ((:definition

; The :install-body field was added by Matt Kaufmann 3/25/06, after v2-9-4, in
; order to avoid an error caused by an attempt to install this rule for :expand
; hints in the presence of free variable X in the hypothesis.

                    :install-body nil
                    :controller-alist ((map-fn-e0-ord t)))))))

;;; REMARK:
;;; It's very interesting the use of this definition rule and
;;; the free variables in it, essential for expanding in an adequate
;;; manner for the induction scheme in map-fn-e0-ord-measure. Note that
;;; this rule has "x" as a free variable. We put the first hypotesis
;;; (equal (fn1 x) (fn1 (max-fn1-list l))), and the rule will only be
;;; used (and "x" conveniently instantiated) if such condition is among
;;; the assumptions. Contrary to most of cases, this is pretty good for
;;; our purpouses. (see "Subgoal *1/6" (where it is used) and "Subgoal
;;; *1/4" (where it is not used) of
;;; map-fn-e0-ord-measure, in the expansion of (map-fn-e0-ord m))

;;; Once we used the following commented rule, but with that rule
;;; definition of map-fn-e0-ord is expanded. This is convenient for
;;; Subgoal *1/6 but not for Subgoal *1/4 where we need
;;;  (map-fn-e0-ord m) to be expanded to the expression :
;;;  (cons (max-fn-list l) (map-fn-e0-ord (remove-one (max-fn-list l) m)))
;;;  and not to the expression:
;;;  (cons (max-fn-list l) (map-fn-e0-ord (remove-one (max-fn-list m) m)))
;;;  Such expression would be obtained if the following rewrite rule
;;;  were used:
;;; (defthm another-definition-of-map-fn-e0-ord-rewrite-rule
;;;   (implies (and (mp-true-listp l) (consp l))
;;;     (equal (map-fn-e0-ord l)
;;; 	   (cons (fn1 (max-fn1-list l))
;;; 		 (map-fn-e0-ord
;;; 		  (remove-one (max-fn1-list l) l)))))
;;;   :hints (("Goal"
;;; 	   :use ((:instance
;;; 		  another-definition-of-map-fn-e0-ord
;;; 		  (x (max-fn1-list l)))))))

;;; Solution: The rule another-definition-of-map-fn-e0-ord-rewrite-rules
;;; (see below) rewrites car and cdr of map-fn-e0-ord and the criteria
;;; of expanding definitions, when applied to e0-ord-<, determines that
;;; those rules are applied in (map-fn-e0-ord l) and NOT in
;;; (map-fn-e0-ord m). In this case, the role of the free variables is
;;; essential.


(local
 (defthm another-definition-of-map-fn-e0-ord-rewrite-rules
   (implies (and (mp-true-listp l)
		 (consp l))
	    (and (equal (car (map-fn-e0-ord l)) (fn1 (max-fn1-list l)))
		 (equal (cdr (map-fn-e0-ord l))
			(map-fn-e0-ord (remove-one (max-fn1-list l) l)))))
   :hints (("Goal" :use (:instance
			 another-definition-of-map-fn-e0-ord
			 (x (max-fn1-list l)))))))

;;; Expansion of the definition of (map-fn-e0-ord l) and (map-fn-e0-ord
;;; m) is always tried. The above rules help the system to satisfy the
;;; criteria for function expansion in the cases where it is really
;;; needed.  (see "Subgoal *1/6" or "Subgoal *1/4" of
;;; map-fn-e0-ord-measure)



;;; Needed for forall-exists-rel-bigger-max-fn1-list
;;; ················································

;;; We prove forall-exists-rel-bigger-max-fn1-list-lemma, establishing
;;; that if (mul-rel n m), and x is in n and not in m, then (fn1 x) is
;;; strictly smaller than (fn1 (max-fn1-list m)). This property will be
;;; used to show forall-exists-rel-bigger-max-fn1-list, essential for
;;; handling cases *1/7 and *1/6 of our map-fn-e0-ord-measure. And also
;;; is used to show
;;; forall-exists-rel-bigger-max-fn1-list-lemma-corollary, for handling
;;; case *1/5

(local
 (defthm member-fn1-ordinal
   (implies (and (member x l) (mp-true-listp l))
	    (e0-ordinalp (fn1 x)))))

(local
 (defthm mp-true-listp-remove-one
   (implies (mp-true-listp l)
	    (mp-true-listp (remove-one x l)))))


(defthm mp-true-listp-multiset-diff
  (implies (mp-true-listp m)
	   (mp-true-listp (multiset-diff m n))))

(local
 (encapsulate
  ()

  (local
   (defun exists-rel-bigger-witness (x l)
     (declare (xargs :verify-guards nil))
     (cond ((atom l) nil)
	   ((rel x (car l)) (car l))
	   (t (exists-rel-bigger-witness x (cdr l))))))

  (local
   (defthm exists-rel-bigger-witness-main-property-lemma
     (implies (and (mp-true-listp l) (exists-rel-bigger x l))
	      (mp (exists-rel-bigger-witness x l)))))

  (local
   (defthm exists-rel-bigger-witness-main-property
     (implies (exists-rel-bigger x l)
	      (rel x (exists-rel-bigger-witness x l)))))

  (local
   (defthm forall-exists-rel-bigger-lemma
     (implies (and
	       (forall-exists-rel-bigger l1 l2)
	       (member x l1)
	       (mp-true-listp l1)
	       (mp-true-listp l2))
	      (e0-ord-< (fn1 x) (fn1 (exists-rel-bigger-witness x l2))))))

  (local
   (defthm member-multiset-diff-exists-rel-bigger-witness-lemma
     (implies (and
	       (forall-exists-rel-bigger l1 l2)
	       (member x l1)
	       (subsetp l2 l3))
	      (member (exists-rel-bigger-witness x l2) l3))))

  (local
   (defthm member-multiset-diff-exists-rel-bigger-witness
     (implies (and
	       (member x n)
	       (not (member x m))
	       (forall-exists-rel-bigger (multiset-diff n m)
					 (multiset-diff m n)))
	      (member (exists-rel-bigger-witness x (multiset-diff m n)) m))))

  (defthm forall-exists-rel-bigger-max-fn1-list-lemma
    (implies (and
	      (consp m)
	      (mp-true-listp n)
	      (mp-true-listp m)
	      (member x n)
	      (not (member x m))
	      (forall-exists-rel-bigger (multiset-diff n m)
					(multiset-diff m n)))
	     (e0-ord-< (fn1 x) (fn1 (max-fn1-list m))))
    :hints (("Goal" :use ((:instance
			   e0-ord-<-transitive-corollary
			   (o3 (fn1 x))
			   (o2 (fn1
				(exists-rel-bigger-witness
				 x
				 (multiset-diff m n))))
			   (o1 (fn1 (max-fn1-list m))))))))))




;;; Previous lemmas to map-fn-e0-ord-measure, handling every subgoal
;;; ································································

;;; Needed for "Subgoal *1/8":

(local
 (defthm max-fn1-e0-ordinalp
   (implies (and (consp l) (mp-true-listp l))
	    (e0-ordinalp (fn1 (max-fn1-list l))))))

(local
 (defthm max-fn1-e0-ord-trichotomy-<
   (implies (and
	     (mp-true-listp n) (mp-true-listp m) (consp n) (consp m)
	     (not (equal (fn1 (max-fn1-list n))
			 (fn1 (max-fn1-list m))))
	     (not (e0-ord-< (fn1 (max-fn1-list n))
			    (fn1 (max-fn1-list m)))))
	    (e0-ord-< (fn1 (max-fn1-list m))
		      (fn1 (max-fn1-list n))))
   :hints (("Goal"
	    :use (:instance e0-ord-<-trichotomy
			    (o1 (add1-if-integer (fn0 (max-fn1-list n))))
			    (o2 (add1-if-integer (fn0 (max-fn1-list m)))))))))

;;; Needed for "Subgoal *1/7":
;;; - another-definition-of-map-fn-e0-ord-rewrite-rules
;;; - max-fn1-e0-ord-trichotomy-< and

(local
 (defthm forall-exists-rel-bigger-max-fn1-list
   (implies (and (consp n)
		 (consp m)
		 (mp-true-listp n)
		 (mp-true-listp m)
		 (forall-exists-rel-bigger (multiset-diff n m)
					   (multiset-diff m n)))
	    (not (e0-ord-< (fn1 (max-fn1-list m)) (fn1 (max-fn1-list n)))))
   :hints (("Goal"
	    :use (:instance forall-exists-rel-bigger-max-fn1-list-lemma
			    (x (max-fn1-list n)))
	    :in-theory (enable weak-e0-ord-<-trichotomy)))))


;;; Needed for "Subgoal *1/6":
;;; - another-definition-of-map-fn-e0-ord-rewrite-rules and

(local
 (defthm consp-map-fn1
   (implies (consp l) (consp (map-fn-e0-ord l)))
   :rule-classes :type-prescription))

;;; Needed for "Subgoal *1/5":

(local
 (defthm forall-exists-rel-bigger-max-fn1-list-lemma-corollary
   (implies (and (consp n)
		 (consp m)
		 (mp-true-listp n)
		 (mp-true-listp m)
		 (equal (fn1 (max-fn1-list n))
			(fn1 (max-fn1-list m)))
		 (forall-exists-rel-bigger (multiset-diff n m)
					   (multiset-diff m n)))
	    (member (max-fn1-list n) m))
   :hints (("Goal" :use ((:instance forall-exists-rel-bigger-max-fn1-list-lemma
				    (x (max-fn1-list n))))))))

;;; Needed for "Subgoal *1/4":
;;;   - another-definition-of-map-fn-e0-ord
;;;   - another-definition-of-map-fn-e0-ord-rewrite-rules
;;;   - max-fn1-list-member
;;;   - mp-true-listp-remove-one
;;;   - multiset-diff-removing-the-same-element and


;;; Needed for "Subgoal *1/3" and "Subgoal *1/2":
;;;   - multiset-diff-nil

;;; Subgoal *1/1 is solved simply expanding definitions


;;; At last: map-fn-e0-ord-measure.
;;; ·······························

(local
 (encapsulate
  ()

  ;;; Induction scheme.

  (local
   (defun induction-multiset (n m)
     (declare (xargs :measure (acl2::len n)
		     :verify-guards nil))
     (cond ((atom n) (if (atom m) 1 2))
	   ((atom m) 3)
	   (t (let* ((max-m (max-fn1-list m))
		     (max-n (max-fn1-list n))
		     (fn1-max-m (fn1 max-m))
		     (fn1-max-n (fn1 max-n)))
		(cond ((equal fn1-max-m fn1-max-n)
		       (if (member max-n m)
			   (induction-multiset
			    (remove-one max-n n)
			    (remove-one max-n m))
			   5))
		      ((e0-ord-< fn1-max-n fn1-max-m) 6)
		      ((e0-ord-< fn1-max-m fn1-max-n) 7)
		      (t 8)))))))

  (defthm map-fn-e0-ord-measure
    (implies (and (mp-true-listp n)
		  (mp-true-listp m)
		  (mul-rel n m))
	     (e0-ord-< (map-fn-e0-ord n)
		       (map-fn-e0-ord m)))
    :hints (("Goal" :induct (induction-multiset n m))))))


;;; ########################################################################
;;; THE MAIN THEOREM OF THIS BOOK:
;;; The multiset relation induced by a well-founded relation is well-founded
;;; ########################################################################

;;; The main theorem exported by this book:

(defthm multiset-extension-of-rel-well-founded-1
  (and (implies (mp-true-listp x) (e0-ordinalp (map-fn-e0-ord x)))
       (implies (and (mp-true-listp x)
		     (mp-true-listp y)
		     (mul-rel x y))
		(e0-ord-< (map-fn-e0-ord x) (map-fn-e0-ord y)))))

;;;added for the new ordinals:

(defun map-fn-op (x)
  (declare (xargs :verify-guards nil))
  (atoc (map-fn-e0-ord x)))

(defthm multiset-extension-of-rel-well-founded
  (and (implies (mp-true-listp x) (o-p (map-fn-op x)))
       (implies (and (mp-true-listp x)
		     (mp-true-listp y)
		     (mul-rel x y))
		(o< (map-fn-op x) (map-fn-op y))))
  :hints (("goal"
	   :use ((:instance  ACL2::e0-ordinal-well-founded-cnf
			    (ACL2::x (map-fn-e0-ord x))
			    (ACL2::y (map-fn-e0-ord y))))))
  :rule-classes :well-founded-relation)