File: ireps.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,138,276 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (960 lines) | stat: -rw-r--r-- 31,410 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
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
(in-package "ACL2")

; eric smith, david russinoff, with suggestions by matt kaufmann
; amd, june 2001

;this file was previously called irepsproofs.lisp

;note: the proofs in this file are messy.  i haven't taken much time to clean
;them up by eliminating hacks or increasing elegance. -eric

;todo: add t-p for isigf, etc.

(include-book "rtl") ;BOZO remove!
(include-book "bias")
(include-book "float") ;for the defns
(local (include-book "merge"))
(local (include-book "cat"))
(local (include-book "bvecp"))
(local (include-book "bits"))
(local (include-book "bitn"))
(local (include-book "../arithmetic/top"))

(local (in-theory (enable bvecp-forward)))

;;encoding of floating-point numbers with implicit msb
;;bit vectors of length p+q, consisting of 1-bit sign field,
;;q-bit exponent field (bias = 2**(q-1)-1), and (p-1)-bit
;;significand field:
;; p must be > 1



;;;**********************************************************************
;;;                       field extractors
;;;**********************************************************************

(defund isgnf  (x p q) (bitn x (1- (+ p q))))
(defund iexpof (x p q) (bits x (- (+ p q) 2) (1- p)))
(defund isigf  (x p)   (bits x (- p 2) 0))


;;;**********************************************************************
;;;                       representable numbers
;;;**********************************************************************

(defund nrepp (x p q)
  (and (rationalp x)
       (not (= x 0))
       (< 0 (+ (expo x) (bias q)))
       (< (+ (expo x) (bias q)) (- (expt 2 q) 1))
       (exactp x p)))

(defund drepp (x p q)
  (and (rationalp x)
       (not (= x 0))
       (<= (- 2 p) (+ (expo x) (bias q)))
       (<= (+ (expo x) (bias q)) 0)
       (exactp x (+ -2 p (expt 2 (- q 1)) (expo x))))) ;use bias here?
;bits available in the sig field = p-1-(-bias-expo(x))

(defund irepp (x p q)
  (or (nrepp x p q)
      (drepp x p q)))


;;;**********************************************************************
;;;                      valid encodings
;;;**********************************************************************

(defund nencodingp (x p q)
  (and (bvecp x (+ p q))
       (< 0 (iexpof x p q))
       (< (iexpof x p q) (- (expt 2 q) 1))))

(defund dencodingp (x p q)
  (and (bvecp x (+ p q))
       (= (iexpof x p q) 0)
       (not (= (isigf x p) 0))))

(defund iencodingp (x p q)
  (or (nencodingp x p q)
      (dencodingp x p q)))


;;;**********************************************************************
;;;                       encode
;;;**********************************************************************

; sig, expo, and sgn are defined in float.lisp

(defund dencode (x p q)
  (cat (cat (if (= (sgn x) 1) 0 1)
	    1
            0
            q)
       (1+ q)
       (* (sig x) (expt 2 (+ -2 p (expo x) (bias q))))
       (- p 1)))

(defund nencode (x p q)
  (cat (cat (if (= (sgn x) 1) 0 1)
	    1
            (+ (expo x) (bias q))
            q)
       (1+ q)
       (* (- (sig x) 1) (expt 2 (- p 1)))
       (- p 1)))

(defund iencode (x p q)
  (cond ((nrepp x p q)
	 (nencode x p q))
	((drepp x p q)
	 (dencode x p q))))

;;;**********************************************************************
;;;                       decode
;;;**********************************************************************

(defund ndecode (x p q)
  (* (if (= (isgnf x p q) 0) 1 -1)
     (+ (expt 2 (- (iexpof x p q) (bias q)))
        (* (isigf x p)
           (expt 2 (+ 1 (iexpof x p q) (- (bias q)) (- p)))))))


(defund ddecode (x p q)
  (* (if (= (isgnf x p q) 0) 1 -1)
     (isigf x p)
     (expt 2 (+ 2 (- (bias q)) (- p)))))


(defund idecode (x p q)
  (cond ((nencodingp x p q)
	 (ndecode x p q))
	((dencodingp x p q)
	 (ddecode x p q))))

;;;**********************************************************************
;;;                       theorems
;;;**********************************************************************

(defthm nencodingp-means-not-dencodingp
  (implies (nencodingp x p q)
	   (not (dencodingp x p q)))
  :rule-classes ((:rewrite :backchain-limit-lst (0)))
  :hints (("goal" :in-theory (enable iencodingp nencodingp dencodingp))))

(defthm dencodingp-means-not-nencodingp
  (implies (dencodingp x p q)
	   (not (nencodingp x p q)))
  :rule-classes ((:rewrite :backchain-limit-lst (0)))
  :hints (("goal" :in-theory (enable iencodingp nencodingp dencodingp))))

(defthm not-both-nencodingp-and-dencodingp
  (implies (iencodingp x p q)
	   (iff (nencodingp x p q) (not (dencodingp x p q))))
  :hints (("Goal" :in-theory (enable iencodingp)))
  :rule-classes ())



; the field extractors return bit-vectors.


;some of the rules below may be bad because they are put into both the
; forward-chaining and type-prescription rule classes, causing them
; not to always work.

;!! rc
(defthm bvecp-isigf-forward-3
  (implies (case-split (integerp p))
           (bvecp (isigf x p) (- p 1)))
  :hints (("goal" :in-theory (enable isigf)))
  :rule-classes (:rewrite
                 (:forward-chaining :trigger-terms ((isigf x p)))
                 ))

(defthm iexpof-bvecp
  (implies (case-split (integerp q))
           (bvecp (iexpof x p q) q))
  :hints (("goal" :in-theory (enable iexpof))))

(defthm isgnf-bvecp
  (bvecp (isgnf x p q) 1)
  :hints (("goal" :in-theory (enable isgnf))))

;forward-chaining-rules for encoding types

(defthm nencodingp-forward-to-iencodingp
  (implies (nencodingp x p q)
           (iencodingp x p q) )
  :hints (("goal" :in-theory (enable iencodingp nencodingp)))
  :rule-classes (:rewrite :forward-chaining)
  )

(defthm dencodingp-forward-to-iencodingp
  (implies (dencodingp x p q)
           (iencodingp x p q) )
  :hints (("goal" :in-theory (enable iencodingp dencodingp)))
  :rule-classes (:rewrite :forward-chaining)
  )

;needed? t-p?
(defthm not-zero-ddecode
  (implies (and (dencodingp x p q)
                (integerp p)
                (< 1 p)
                (integerp q)
                (< 0 q))
           (not (equal (ddecode x p q) 0)))
  :hints (("goal" :in-theory (enable ddecode dencodingp))))

(defthm not-zero-ndecode
  (implies (and ;(nencodingp x p q)
                (rationalp x)
                (>= x 0)
                (integerp p)
                (< 1 p)
                (integerp q)
                (< 0 q))
           (not (equal (ndecode x p q) 0)))
  :hints (("goal" :in-theory (enable ndecode nencodingp))))

;move
;BOZO make a negative-syntaxp version
;or just enable sgn?
(defthm sgn-minus-dist
  (implies (and (acl2-numberp x)
                (acl2-numberp y))
           (equal (sgn (+ (* -1 x) (* -1 y)))
                  (* -1 (sgn (+ x y)))))
  :hints (("Goal" :in-theory (disable sgn-minus)
          :use (:instance sgn-minus (x (* -1 (+ x y)))))))

(defthm expo-ndecode
  (implies (and (nencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (expo (ndecode x p q))
                  (- (iexpof x p q) (bias q))))
  :hints (("goal" :in-theory (e/d (ndecode nencodingp
                                           expt-split
                                           expt-minus
                                           isigf
                                           )
                                  ()))))

(defthm sgn-ndecode
  (implies (and ; (nencodingp x p q)
            (rationalp x)
            (>= x 0)
            (integerp p)
            (> p 1)
            (integerp q)
            (> q 0))
           (equal (sgn (ndecode x p q))
                  (if (= (isgnf x p q) 0) 1 -1)))
  :hints (("goal" :in-theory (enable ndecode sgn))))

;remove from :rewrite?
(defthm nencodingp-forward-to-positive-integerp
  (implies (nencodingp x p q)
           (and (integerp x)
                (<= 0 x)))
  :hints (("Goal" :in-theory (enable nencodingp)))
  :rule-classes (:forward-chaining))

(defthm dencodingp-forward-to-positive-integerp
  (implies (dencodingp x p q)
           (and (integerp x)
                (<= 0 x)))
  :hints (("Goal" :in-theory (enable dencodingp)))
  :rule-classes (:forward-chaining))

;BOZO do we even need this?
(defthmd ndecode-rewrite
  (implies (and (rationalp x)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (= (ndecode x p q)
              (* (if (= (isgnf x p q) 0) 1 -1)
                 (* (expt 2 (- (iexpof x p q) (bias q)))
                    (+ 1 (* (isigf x p)
                            (expt 2 (+ 1 (- p)))))))))
  :hints (("Goal" :in-theory (enable ndecode))))

(defthm isigf-upper-bound-linear
  (implies (case-split (integerp p))
           (< (isigf x p) (expt 2 (- p 1) )))
  :rule-classes (:rewrite :linear))

(defthm sig-ndecode
  (implies (and (nencodingp x p q) ;gen?
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (sig (ndecode x p q))
                  (+ 1 (/ (isigf x p) (expt 2 (- p 1))))
                  ))
  :otf-flg t
  :hints (("goal" :use (:instance expo-unique (x (+ 1
                                                    (* 2 (/ (EXPT 2 P))
                                                       (BITS X (+ -2 P) 0))))
                                  (n 0))
           :in-theory (set-difference-theories
                       (enable ndecode
                               expt-split
                               expt-minus
                               isigf iexpof ; isgnf
                               sig
;                                       nencodingp
                               )
                       '( EXPO-UNIQUE-ERIC-2)))))

;instead, just open up sgn?
(defthm sgn-ddecode
  (implies (and (dencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (sgn (ddecode x p q))
                  (if (= (isgnf x p q) 0) 1 -1)))
  :hints (("Goal" :in-theory (e/d (ddecode dencodingp sgn) ()))))

(defthm sig-ddecode
  (implies (and (dencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (sig (ddecode x p q))
                  (sig (isigf x p))))
  :hints (("Goal" :in-theory (e/d ( ddecode sgn ) ()))))

(defthm expo-ddecode
  (implies (and (dencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (expo (ddecode x p q))
                  (+ 2 (- p) (- (bias q)) (expo (isigf x p)))))
  :hints (("Goal" :in-theory (enable ddecode dencodingp ))))

(defthm sgn-idecode
  (implies (and (iencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (sgn (idecode x p q))
                  (if (= (isgnf x p q) 0) 1 -1)))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable idecode iencodingp ISGNF NDECODE)
                              '()))))

(defthm sig-idecode
  (implies (and (iencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (sig (idecode x p q))
                  (cond ((nencodingp x p q)
                         (+ 1 (/ (isigf x p) (expt 2 (- p 1)))))
                        ((dencodingp x p q)
                         (sig (isigf x p))))))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable idecode iencodingp)
                              '()))))

(defthm expo-idecode
  (implies (and (iencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (expo (idecode x p q))
                  (cond ((nencodingp x p q)
                         (- (iexpof x p q) (bias q)))
                        ((dencodingp x p q)
                         (+ 2 (- p) (- (bias q)) (expo (isigf x p)))))))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable idecode iencodingp)
                              '()))))


;move?
(defthm expo-bvecp-upper-bound
  (implies (and (bvecp x n)
                (integerp n)
                (< 0 n))
           (< (expo x) n))
  :hints (("Goal" :in-theory (enable bvecp)
           :use ((:instance expo<= ( n (- n 1))))))
  :rule-classes (:rewrite (:linear :match-free :all)))

(local (in-theory (disable bvecp-exactp))) ;why? efficiency?

;nice lemma?
(local
 (defthm drepp-decode-1
  (IMPLIES (AND (DENCODINGP X P Q)
                (INTEGERP P)
                (< 1 P)
                (INTEGERP Q)
                (< 0 Q))
           (EXACTP (DDECODE X P Q)
                   (+ (* -1 (BIAS Q))
                      (EXPT 2 (1- Q))
                      (EXPO (ISIGF X P)))))
  :hints (("Goal" :in-theory (enable ddecode dencodingp bias)))))

(defthm drepp-ddecode
  (implies (and (dencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (drepp (ddecode x p q) p q))
  :hints (("Goal" :use drepp-decode-1
           :in-theory (set-difference-theories
                       (enable drepp)
                       '(drepp-decode-1)))))

;BOZO yuck. better expo-shift rules would help here..
(defthm nrepp-ndecode-hack
  (implies (and (nencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (exactp (ndecode x p q) p))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable nrepp nencodingp ndecode exactp
                                      a15
                                      ndecode-rewrite
;expt-split
                                      )
                              '(a9 distributivity
                                   )))
          (and stable-under-simplificationp ;yuck?
               '(:in-theory (enable a9 distributivity a15)))))


#|

(defthm nrepp-ndecode-hack
  (implies (and (nencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (exactp (ndecode x p q) p))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable nrepp nencodingp ndecode exactp
                                      a15
                                      ndecode-rewrite
                                      expt-split
                                      expt-minus
                                      sig
                                      isigf
                                      isgnf
                                      iexpof
                                      )
                              '(;a9 distributivity
                                a15
                                   )))
          (and stable-under-simplificationp ;yuck?
               '(:in-theory (enable a9 distributivity a15)))))
|#




(defthm nrepp-ndecode
  (implies (and (nencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (nrepp (ndecode x p q) p q))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable nrepp nencodingp)
                              '()))))

(defthm irepp-idecode
  (implies (and (iencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (irepp (idecode x p q) p q))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable idecode irepp iencodingp)
                              '()))))


(local
 (defthm nencodingp-nencode-2-1
   (IMPLIES (AND (RATIONALP X)
                 (NOT (EQUAL X 0))
                 (EXACTP X P)
                 (INTEGERP P)
                 (<= 1 P)
                 )
            (integerp (+ (* -1 (EXPT 2 (1- P)))
                         (* (SIG X) (EXPT 2 (1- P))))
                      ))
   :hints (("Goal" :in-theory (enable exactp)
            :use ( sig-lower-bound)))))

(local
 (defthm nencodingp-nencode-2
   (IMPLIES (AND (NREPP X P Q)
                 (INTEGERP P)
                 (< 1 P)
                 (INTEGERP Q)
                 (< 0 Q))
            (<= 0 (NENCODE X P Q)))
 :hints (("Goal"  :in-theory (enable nencode nrepp)))))

(local
 (defthm nencodingp-nencode-3
   (IMPLIES (AND (NREPP X P Q)
                 (INTEGERP P)
                 (< 1 P)
                 (INTEGERP Q)
                 (< 0 Q))
            (< (NENCODE X P Q) (EXPT 2 (+ P Q))))
   :hints (("Goal" :in-theory (enable nencode)))))


(local
 (defthm nencodingp-nencode-4
   (IMPLIES (AND (NREPP X P Q)
                 (INTEGERP P)
                 (< 1 P)
                 (INTEGERP Q)
                 (< 0 Q))
            (< 0 (IEXPOF (NENCODE X P Q) P Q)))
   :hints (("Goal"  :in-theory (e/d ( nencode
                                      iexpof
                                      bvecp
                                      nrepp
                                      bits-tail) (sig-lower-bound sig-upper-bound))
            :use (sig-upper-bound sig-lower-bound)))))


(local
 (defthm nencodingp-nencode-5
   (IMPLIES (AND (NREPP X P Q)
                 (INTEGERP P)
                 (< 1 P)
                 (INTEGERP Q)
                 (< 0 Q))
            (< (IEXPOF (NENCODE X P Q) P Q)
               (1- (EXPT 2 Q))))
   :hints (("Goal"  :in-theory (e/d (nrepp nencode iexpof bvecp
                                       bits-tail) (sig-lower-bound sig-upper-bound))
            :use (sig-upper-bound sig-lower-bound)))))

(defthm nencodingp-nencode
  (implies (and (nrepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (nencodingp (nencode x p q) p q) )
  :hints (("Goal" :in-theory (enable nencodingp bvecp)))
  :OTF-FLG T)

(local
 (defthm dencodingp-dencode-hack-3
   (implies (and (drepp x p q)
                 (integerp p)
                 (> p 1)
                 (integerp q)
                 (> q 0))
            (equal (* (SIG X)
                      (EXPT 2 (+ -3 P (EXPO X) (EXPT 2 (1- Q)))))
                   (* (abs x) (EXPT 2 (+ -3 P (EXPT 2 (1- Q)))))))
   :hints (("Goal" :in-theory (set-difference-theories
                               (enable drepp sgn)
                               '(fp-rep-collapse abs))
            :use fp-abs))
   :rule-classes nil))


(local (in-theory (disable expt-compare))) ;yuck

(local
 (defthmd dencodingp-dencode-hack-4
   (implies (and (drepp x p q)
                 (integerp p)
                 (> p 1)
                 (integerp q)
                 (> q 0))
            (bvecp (* (SIG X)
                      (EXPT 2 (+ -3 P (EXPO X) (EXPT 2 (1- Q)))))
                   (- p 1)))

   :hints (("Goal" :in-theory (set-difference-theories (enable dencodingp drepp dencode
                                                               iexpof isigf bias exactp
                                                               expt-split
                                                               bvecp)
                                                       '(abs EXPT-COMPARE sig-upper-bound))
            :use (dencodingp-dencode-hack-3
                  sig-upper-bound
                  (:instance expt-weak-monotone
                             (n (+ -3 P (EXPO X) (EXPT 2 (1- Q))))
                             (m (- p 2))))))))

(defthm dencodingp-dencode
  (implies (and (drepp x p q)
                (integerp p)
;                (> p 1)
                (integerp q)
                (> q 0)
                )
           (dencodingp (dencode x p q) p q) )
  :hints (("Goal" :in-theory (e/d (exactp
                                     dencodingp
                                     drepp
                                     dencode
                                     iexpof
                                     isigf
                                     bias
                                     bits-tail

                                     bvecp
                                     bvecp-bits-0
                                     )
                                  (sig-upper-bound
                                   BITS-SHIFT
                                   BITS-SPLIT-AROUND-ZERO))
           :use (sig-upper-bound
                 sig-lower-bound
                 dencodingp-dencode-hack-4
                 (:instance expt-strong-monotone
                            (n (- p 1))
                            (m (+ p q)))
                 (:instance expt-strong-monotone
                            (n (- p 1))
                            (m (+ p q -1)))))))

(defthm drepp-forward-to-rationalp
  (implies (drepp x p q)
           (rationalp x))
  :hints (("Goal" :in-theory (enable drepp)))
  :rule-classes ((:forward-chaining :trigger-terms ((drepp x p q)))))

(defthm drepp-zero-false
  (not (drepp 0 p q))
  :hints (("Goal" :in-theory (enable drepp))))

(defthm iencodingp-iencode
  (implies (and (irepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (iencodingp (iencode x p q) p q) )
  :hints (("Goal" :in-theory (enable iencodingp irepp iencode))))

;prove a thm about bvecp of sig-1?
(defthm isigf-nencode-0
  (implies (and (rationalp x)
                (not (equal x 0))
                (exactp x p)
                (integerp p)
                (< 1 p)
                )
           (bvecp (+ (* -1 (expt 2 (1- p)))
                     (* (sig x) (expt 2 (1- p))))
                  (- p 1)))
  :hints (("goal" :in-theory (set-difference-theories
                              (enable bvecp)
                              '(nencodingp-nencode-2-1
                                sig-upper-bound))
           :use (sig-lower-bound sig-upper-bound nencodingp-nencode-2-1)))
  :rule-classes nil)

(defthm isgnf-nencode
  (implies (and (nrepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (isgnf (nencode x p q) p q)
                  (if (equal (sgn x) 1) 0 1)))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable isgnf nencode nrepp bvecp)
                              '(nencodingp-nencode-2-1
                                bitn-bvecp-0
                                sig-upper-bound
                                                        ))
           :use (nencodingp-nencode-2-1
                 sig-lower-bound
                 sig-upper-bound
                 (:instance bitn-bvecp-0 (m 0)
                            (x (+ (BIAS Q) (EXPO X)))
                            (n q))))))

(defthm isgnf-dencode
  (implies (and (drepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (isgnf (dencode x p q) p q)
                  (if (= (sgn x) 1) 0 1)))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable isgnf dencode drepp bvecp bias)
                              '(nencodingp-nencode-2-1 bitn-bvecp-0))
           :use (dencodingp-dencode-hack-4
                 (:instance expt-strong-monotone
                            (n (- p 1))
                            (m (+ p q -1)))
                 (:instance bitn-bvecp-0
                            (m q)
                            (x (* (SIG X)
                                  (EXPT 2 (+ -3 P (EXPO X) (EXPT 2 (1- Q))))))
                            (n (- p 1)))))))

(defthm isgnf-iencode
  (implies (and (irepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (isgnf (iencode x p q) p q)
                  (if (equal (sgn x) 1) 0 1)))

  :hints (("Goal" :in-theory (enable irepp iencode))))

(defthm isigf-nencode-1
  (implies (and (nrepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (bvecp (+ (* -2 (EXPT 2 (+ -2 P)))
                     (* 2 (SIG X) (EXPT 2 (+ -2 P)))) (- p 1)   ))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable isigf nencode nrepp bvecp expt-split)
                              '())
           :use isigf-nencode-0))
  :rule-classes nil)

(defthm isigf-nencode
  (implies (and (nrepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (isigf (nencode x p q) p)
                  (* (- (sig x) 1) (expt 2 (- p 1)))))
  :hints (("Goal" :in-theory (e/d (isigf nencode nrepp expt-split bvecp bias bits-tail)
                                  (bits-shift
                                   BITS-SUM-DROP-IRRELEVANT-TERM-1-OF-2
                                   BITS-SHIFT-BY-CONSTANT-POWER-OF-2))
           :use (isigf-nencode-1
                 ))))

(defthm isigf-dencode
  (implies (and (drepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (isigf (dencode x p q) p)
                  (* (sig x) (expt 2 (+ -2 p (expo x) (bias q))))))

  :hints (("Goal" :in-theory (set-difference-theories
                              (enable isigf dencode drepp bias bits-tail lead-bit-0)
                              '(BITS-SHIFT))
           :use dencodingp-dencode-hack-4)))

(defthm iexpof-nencode
  (implies (and (nrepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (iexpof (nencode x p q) p q)
                  (+ (expo x) (bias q))))
  :hints (("Goal" :in-theory (enable iexpof nencode nrepp bvecp
                                     bits-tail)
           :use (isigf-nencode-1))))

(defthm iexpof-dencode
  (implies (and (drepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (iexpof (dencode x p q) p q)
                  0))
  :otf-flg t
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable iexpof dencode drepp bias
                                      bvecp-bits-0
                                      )
                              '())
           :use (dencodingp-dencode-hack-4))))

(defthm ndecode-nencode
  (implies (and (nrepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (ndecode (nencode x p q) p q)
                  x))
  :hints (("Goal" :in-theory (enable nrepp ndecode sgn expt-split expt-minus))))

(defthm ddecode-dencode
  (implies (and (drepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (ddecode (dencode x p q) p q)
                  x))
  :hints (("Goal" :in-theory (enable drepp ddecode sgn expt-split expt-minus))))

(defthm not-both-nrepp-and-drepp
  (implies (irepp x p q)
	   (iff (nrepp x p q)
		(not (drepp x p q))))
  :rule-classes ()
  :hints (("Goal" :in-theory (enable irepp nrepp drepp))))

(defthm idecode-iencode
  (implies (and (irepp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal
            (idecode (iencode x p q) p q)
            x))
  :hints (("Goal" :in-theory (enable irepp idecode iencode idecode)
		  :use (dencodingp-dencode
			not-both-nrepp-and-drepp))))

(defthm nencode-ndecode
  (implies (and (nencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (nencode (ndecode x p q) p q)
                  x))
  :hints (("Goal" :in-theory (set-difference-theories
			      (enable nencodingp nencode iexpof isigf isgnf sgn
				      cat lead-bit-0
				      expt-split
				      bits-reduce ;why?
				      )
; Modification by Matt K. for v2-9, due to the change to rewrite-clause that
; avoids using forward-chaining facts derived from a literal that has been
; rewritten: use sgn-ndecode explicitly (see :use hint below) and hence disable
; it here.

			      '(sgn-ndecode))
           :use (sgn-ndecode
                 (:instance bits-plus-bits
                            (m 0)
                            (p (- p 1))
                            (n (+ -2 p q)))
                 (:instance bitn-plus-bits
                            (n (+ p q -1))
                            (m 0))
                 (:instance bitn-0-1 (x x) (n (+ -1 p q)))))))

(defthm dencode-ddecode
  (implies (and (dencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (dencode (ddecode x p q) p q)
                  x))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable dencodingp dencode iexpof isigf isgnf sgn
                                      cat lead-bit-0 expt-split expt-minus)
                              '())
           :use ((:instance bits-plus-bits
                            (m 0)
                            (p (- p 1))
                            (n (+ -2 p q)))
                 (:instance fp-rep (x (BITS X (+ -2 P) 0)))
                 (:instance bitn-plus-bits
                            (n (+ p q -1))
                            (m 0))
                 (:instance bitn-0-1 (x x) (n (+ -1 p q)))
                 ))))

(defthm iencode-idecode
  (implies (and (iencodingp x p q)
                (integerp p)
                (> p 1)
                (integerp q)
                (> q 0))
           (equal (iencode (idecode x p q) p q)
                  x))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable irepp idecode iencode idecode IENCODINGP)
                              '())
		  :use (
			(:instance not-both-nrepp-and-drepp (x (ddecode x p q)))))))

(defthm nrepp-bvecp-sig
  (implies (and (natp p)
                (> p 0)
                (nrepp x p q))
           (bvecp (* (1- (sig x)) (expt 2 (1- p)))
                  (1- p)))
  :rule-classes ()
  :hints (("Goal" :in-theory (e/d (nrepp exactp bvecp) ()))))

; Matt K., after v4-2:
; Commenting out the following rule, which rewrites a term to itself!
#||
(defthm nencode-nencode
  (implies (and (nrepp x p q)
                (integerp p)
                (> p 0)
                (integerp q)
                (> q 0))
           (equal (nencode x p q) (nencode x p q)))
  :hints (("Goal" :use (nrepp-bvecp-sig)
		  :in-theory (enable nencode nencode bits-tail nrepp bvecp))))
||#

(defthm drepp-bvecp-sig
    (implies (and (integerp p)
		  (natp q)
		  (> q 0)
		  (drepp x p q))
	     (bvecp (* (sig x) (expt 2 (+ -2 p (expo x) (bias q))))
		    (1- p)))
  :rule-classes ()
  :hints (("Goal" :in-theory (e/d (bias drepp exactp bvecp expt-split) (sig-upper-bound
                                                                        ))
		  :use (;sig-lower-bound
			sig-upper-bound
			(:instance expt-weak-monotone (n (+ -3 P (EXPO X) (EXPT 2 (+ -1 Q)))) (m (- p 2)))))))