Parse a binary number from the beginning of a character list.
(parse-bits-from-charlist x val len) → (mv val len rest)
This function is somewhat complicated. See also (bin-digit-chars-value x), which is a simpler way to interpret strings where all of the characters are 0 or 1.
Function:
(defun parse-bits-from-charlist (x val len) (declare (xargs :guard (and (character-listp x) (natp val) (natp len)))) (declare (type unsigned-byte val len)) (declare (xargs :split-types t)) (let ((acl2::__function__ 'parse-bits-from-charlist)) (declare (ignorable acl2::__function__)) (mbe :logic (cond ((atom x) (mv (nfix val) (nfix len) nil)) ((bin-digit-char-p (car x)) (let ((digit-val (bin-digit-char-value (car x)))) (parse-bits-from-charlist (cdr x) (+ digit-val (ash (nfix val) 1)) (+ 1 (nfix len))))) (t (mv (nfix val) (nfix len) x))) :exec (b* (((when (atom x)) (mv val len nil)) (car (car x)) ((when (equal car #\0)) (parse-bits-from-charlist (cdr x) (the unsigned-byte (ash val 1)) (the unsigned-byte (+ 1 len)))) ((when (equal car #\1)) (parse-bits-from-charlist (cdr x) (the unsigned-byte (+ 1 (the unsigned-byte (ash val 1)))) (the unsigned-byte (+ 1 len))))) (mv val len x)))))
Theorem:
(defthm val-of-parse-bits-from-charlist (equal (mv-nth 0 (parse-bits-from-charlist x val len)) (+ (bin-digit-chars-value (take-leading-bin-digit-chars x)) (ash (nfix val) (len (take-leading-bin-digit-chars x))))))
Theorem:
(defthm len-of-parse-bits-from-charlist (equal (mv-nth 1 (parse-bits-from-charlist x val len)) (+ (nfix len) (len (take-leading-bin-digit-chars x)))))
Theorem:
(defthm rest-of-parse-bits-from-charlist (equal (mv-nth 2 (parse-bits-from-charlist x val len)) (skip-leading-bit-digits x)))