• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Community
    • Std
      • Std/lists
      • Omaps
      • Std/alists
      • Obags
      • Std/util
      • Std/strings
        • Pretty-printing
        • Printtree
        • Base64
        • Charset-p
        • Strtok!
        • Cases
        • Concatenation
        • Character-kinds
        • Html-encoding
        • Substrings
        • Strtok
        • Equivalences
        • Url-encoding
        • Lines
        • Explode-implode-equalities
        • Ordering
        • Numbers
          • Decimal
          • Hex
          • Octal
          • Binary
            • Parse-bits-from-string
            • Parse-bits-from-charlist
              • Nat-to-bin-chars
              • Bin-digit-chars-value
              • Take-leading-bin-digit-chars
              • Bin-digit-char-listp
              • Skip-leading-bit-digits
              • Bin-digit-char-list*p
              • Bin-digit-string-p
              • Bin-digit-char-value
              • Strval2
              • Nat-to-bin-string
              • Bin-digit-char-p
              • Nat-to-bin-string-list
              • Nat-to-bin-string-size
              • Revappend-nat-to-bin-chars
              • Binify-width
              • Binify
          • Pad-trim
          • Coercion
          • Std/strings/digit-to-char
          • Substitution
          • Symbols
        • Std/osets
        • Std/io
        • Std/basic
        • Std/system
        • Std/typed-lists
        • Std/bitsets
        • Std/testing
        • Std/typed-alists
        • Std/stobjs
      • Proof-automation
      • Macro-libraries
      • ACL2
      • Interfacing-tools
      • Hardware-verification
      • Software-verification
      • Math
      • Testing-utilities
    • Binary

    Parse-bits-from-charlist

    Parse a binary number from the beginning of a character list.

    Signature
    (parse-bits-from-charlist x val len) → (mv val len rest)
    Arguments
    x — Characters to read from.
        Guard (character-listp x).
    val — Accumulator for the value of the bits we have read so far; typically 0 to start with.
        Guard (natp val).
    len — Accumulator for the number of bits we have read; typically 0 to start with.
        Guard (natp len).
    Returns
    val — Value of the initial bits as a natural number.
    len — Number of initial bits we read.
    rest — The rest of x, past the leading bits.

    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.

    Definitions and Theorems

    Function: parse-bits-from-charlist

    (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: val-of-parse-bits-from-charlist

    (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: len-of-parse-bits-from-charlist

    (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: rest-of-parse-bits-from-charlist

    (defthm rest-of-parse-bits-from-charlist
      (equal (mv-nth 2 (parse-bits-from-charlist x val len))
             (skip-leading-bit-digits x)))