1
;; The following proof shows that the cantor pairing function, hl-nat-combine,
2
;; is bijective. The crux of the proof is an explicit definition of the inverse
3
;; of the cantor pairing function.
4
;; - harshrc [2014-12-27 Sat] -- adapted from proof dating May 2010.
5
;; [2015-1-3] Added sum-inverse bounds to fill gap in given proof (pointed by Grant Passmore).
9
(local (include-book "arithmetic-5/top" :dir :system))
11
; The cantor pairing function uses n*(n+1)/2, which is the sum of numbers upto n.
12
; Rather than working with *,/ arith ops, I prefer +, 1- and recursion.
14
"find sum of first n numbers"
17
(+ (nfix n) (sum (- n 1)))))
21
(and (integerp (sum n))
23
:rule-classes (:type-prescription :rewrite))
28
:rule-classes (:linear :rewrite))
30
; If (sum n) = M, then (sum-inverse M M) = n
31
(defun sum-inverse (M x)
32
"return greatest number n <= x such that (sum n) <= M"
37
(sum-inverse M (1- x)))))
42
(local (include-book "ordinals/lexicographic-ordering-without-arithmetic" :dir :system))
44
;explicit induction hint used below
46
(defun sum-inverse-ind-hint (n y i)
47
(declare (xargs :measure (llist n y)
48
:well-founded-relation l<))
49
(if (or (zp n) (zp y))
52
(sum-inverse-ind-hint n (1- y) i)
55
(sum-inverse-ind-hint (1- n) y (1- i))))))
59
(defthm sum-inverse-is-inverse-of-sum-helper
60
(implies (and (natp y)
62
(< (sum y) (sum (+ 1 p)))
64
(equal (equal y p) t)))
68
(defthm sum-inverse-is-inverse-of-sum
69
(implies (and (natp i)
74
(equal n (+ i (sum p))))
75
(equal (sum-inverse n y) p))
76
:hints (("Goal" :induct (sum-inverse-ind-hint n y i))))
79
(defthm sum-inverse-sum-corollary
80
(implies (and (natp i)
83
(equal (sum-inverse (+ i (sum p)) (+ i (sum p)))
88
(defthm sum-sum-inverse-bound1
89
(implies (and (natp n)
92
(<= (sum (sum-inverse n y)) n))
93
:hints (("Goal" :induct (sum-inverse-ind-hint n y i)))
94
:rule-classes (:linear :rewrite))
96
(defthm sum-sum-inverse-bound2
97
(implies (and (natp n)
101
(>= (sum (sum-inverse n y)) (- n (sum-inverse n y))))
102
:hints (("Goal" :induct (sum-inverse n y))
103
("Subgoal *1/3" :cases ((> (sum (- y 1)) n))))
104
:rule-classes (:linear :rewrite))
111
; (cantor-pairing a b) = (+ b (/ (* (+ a b) (+ a b 1)) 2))
112
(defun cantor-pairing (a b)
115
; the inverse of above -- this is the crux of the proof
116
(defun cantor-pairing-inverse (n)
119
(let* ((j (- n (sum (sum-inverse n n))))
120
(i (- (sum-inverse n n) j)))
125
; Cantor pairing is surjective
126
; To show: \forall n \exists a,b \in N s.t. (cantor-pairing a b) = n
127
; = { Skolemization }
128
; \forall n (cantor-pairing (f1 n) (f2 n)) = n
129
; Since ACL2 is first-order, we manually find such f1 and f2.
130
(defthm cantor-pairing-onto
131
(let ((a (car (cantor-pairing-inverse n)))
132
(b (cadr (cantor-pairing-inverse n))))
136
(equal (cantor-pairing a b) n))))
140
(defthm cantor-pairing-inverse-is-an-inverse
141
(implies (and (natp a)
143
(equal (cantor-pairing-inverse (cantor-pairing a b))
146
; Cantor pairing is injective
147
(defthm cantor-pairing-one-one
148
(implies (and (natp a1)
152
(equal (cantor-pairing a1 b1) (cantor-pairing a2 b2)))
153
(equal (list a1 b1) (list a2 b2)))
155
:hints (("Goal" :in-theory (disable cantor-pairing cantor-pairing-inverse)
156
:use ((:instance cantor-pairing-inverse-is-an-inverse (a a1) (b b1))
157
(:instance cantor-pairing-inverse-is-an-inverse (a a2) (b b2))))))#|ACL2s-ToDo-Line|#
166
; The following just shows that cantor-pairing is the same function as hl-nat-combine
167
(local (include-book "system/hl-addr-combine" :dir :system))
170
(defthm sum-closed-form
173
(/ (* n (+ n 1)) 2))))
174
;:hints (("Goal" :in-theory (disable FUNCTIONAL-COMMUTATIVITY-OF-MINUS-*-LEFT))))
178
(defthm hl-nat-combine-is-same-as-cantor-pairing
179
(implies (and (natp a)
181
(equal (hl-nat-combine a b)
182
(cantor-pairing a b)))
183
:hints (("Goal" :in-theory (e/d (hl-nat-combine) (sum)))))