Parse a binary number from a string, at some offset.
(parse-bits-from-string x val len n xl) → (mv val len)
This function is flexible but very complicated. See strval2 for a very simple alternative that may do what you want.
The final
Because of leading zeroes, the
See also parse-bits-from-charlist for a simpler function that reads a number from the start of a character list. This function also serves as part of our logical definition.
Function:
(defun parse-bits-from-string (x val len n xl) (declare (xargs :guard (and (stringp x) (natp val) (natp len) (natp n) (eql xl (length x))))) (declare (type string x) (type unsigned-byte val len n xl)) (declare (xargs :split-types t :guard (<= n xl))) (let ((acl2::__function__ 'parse-bits-from-string)) (declare (ignorable acl2::__function__)) (mbe :logic (b* (((mv val len ?rest) (parse-bits-from-charlist (nthcdr n (explode x)) val len))) (mv val len)) :exec (b* (((when (eql n xl)) (mv val len)) ((the character char) (char (the string x) (the unsigned-byte n))) ((when (equal char #\0)) (parse-bits-from-string (the string x) (the unsigned-byte (ash val 1)) (the unsigned-byte (+ 1 len)) (the unsigned-byte (+ 1 n)) (the unsigned-byte xl))) ((when (equal char #\1)) (parse-bits-from-string (the string x) (the unsigned-byte (+ 1 (the unsigned-byte (ash val 1)))) (the unsigned-byte (+ 1 len)) (the unsigned-byte (+ 1 n)) (the unsigned-byte xl)))) (mv val len)))))
Theorem:
(defthm natp-of-parse-bits-from-string.val (b* (((mv ?val acl2::?len) (parse-bits-from-string x val len n xl))) (natp val)) :rule-classes :type-prescription)
Theorem:
(defthm natp-of-parse-bits-from-string.len (b* (((mv ?val acl2::?len) (parse-bits-from-string x val len n xl))) (natp len)) :rule-classes :type-prescription)