Count the votes and non-votes for the leader.
(tally-leader-votes leader voters) → (mv yes-count no-count)
The
Function:
(defun tally-leader-votes (leader voters) (declare (xargs :guard (and (addressp leader) (certificate-setp voters)))) (let ((__function__ 'tally-leader-votes)) (declare (ignorable __function__)) (b* (((when (emptyp voters)) (mv 0 0)) (voter (head voters)) ((mv yes-count no-count) (tally-leader-votes leader (tail voters)))) (if (in leader (certificate->previous voter)) (mv (1+ yes-count) no-count) (mv yes-count (1+ no-count))))))
Theorem:
(defthm natp-of-tally-leader-votes.yes-count (b* (((mv ?yes-count ?no-count) (tally-leader-votes leader voters))) (natp yes-count)) :rule-classes :rewrite)
Theorem:
(defthm natp-of-tally-leader-votes.no-count (b* (((mv ?yes-count ?no-count) (tally-leader-votes leader voters))) (natp no-count)) :rule-classes :rewrite)
Theorem:
(defthm tally-leader-votes-sum (b* (((mv ?yes-count ?no-count) (tally-leader-votes leader voters))) (equal (+ yes-count no-count) (cardinality voters))))
Theorem:
(defthm tally-leader-votes-of-insert (equal (tally-leader-votes leader (insert voter voters)) (if (in voter voters) (tally-leader-votes leader voters) (mv (if (in leader (certificate->previous voter)) (1+ (mv-nth 0 (tally-leader-votes leader voters))) (mv-nth 0 (tally-leader-votes leader voters))) (if (not (in leader (certificate->previous voter))) (1+ (mv-nth 1 (tally-leader-votes leader voters))) (mv-nth 1 (tally-leader-votes leader voters)))))))