• 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
            • Parse-nat-from-string
            • Parse-nat-from-charlist
              • Nat-to-dec-chars
              • Dec-digit-char-p
              • Dec-digit-chars-value
              • Dec-digit-char-listp
              • Take-leading-dec-digit-chars
              • Dec-digit-char-value
              • Dec-digit-char-list*p
              • Nat-to-dec-string-width
              • Nat-to-dec-string
              • Dec-digit-string-p
              • Strval
              • Skip-leading-digits
              • Nat-to-dec-string-size
              • Int-to-dec-string-width
              • Int-to-dec-string
              • Nonzero-dec-digit-char-p
              • Nat-to-dec-string-list
              • Int-to-dec-string-list
              • Revappend-nat-to-dec-chars
            • Hex
            • Octal
            • Binary
          • 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
    • Decimal

    Parse-nat-from-charlist

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

    Signature
    (parse-nat-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 digits we have read so far; typically 0 to start with.
        Guard (natp val).
    len — Accumulator for the number of digits we have read; typically 0 to start with.
        Guard (natp len).
    Returns
    val — Value of the initial digits as a natural number.
    len — Number of initial digits we read.
    rest — The rest of x, past the leading digits.

    This function is somewhat complicated. See also (dec-digit-chars-value x), which is a simpler way to interpret strings where all of the characters are digits.

    Definitions and Theorems

    Function: parse-nat-from-charlist

    (defun parse-nat-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-nat-from-charlist))
      (declare (ignorable acl2::__function__))
      (mbe
       :logic
       (cond
           ((atom x)
            (mv (nfix val) (nfix len) nil))
           ((dec-digit-char-p (car x))
            (let ((dec-digit-char-value (dec-digit-char-value (car x))))
              (parse-nat-from-charlist
                   (cdr x)
                   (+ dec-digit-char-value (* 10 (nfix val)))
                   (+ 1 (nfix len)))))
           (t (mv (nfix val) (nfix len) x)))
       :exec
       (b* (((when (atom x)) (mv val len nil))
            ((the (unsigned-byte 8) code)
             (char-code (the character (car x))))
            ((unless (and (<= (the (unsigned-byte 8) code)
                              (the (unsigned-byte 8) 57))
                          (<= (the (unsigned-byte 8) 48)
                              (the (unsigned-byte 8) code))))
             (mv val len x))
            ((the (unsigned-byte 8)
                  dec-digit-char-value)
             (the (unsigned-byte 8)
                  (- (the (unsigned-byte 8) code)
                     (the (unsigned-byte 8) 48)))))
         (parse-nat-from-charlist
              (cdr x)
              (the unsigned-byte
                   (+ (the (unsigned-byte 8)
                           dec-digit-char-value)
                      (the unsigned-byte (* 10 val))))
              (the unsigned-byte
                   (+ 1 (the integer len))))))))

    Theorem: val-of-parse-nat-from-charlist

    (defthm val-of-parse-nat-from-charlist
      (equal (mv-nth 0 (parse-nat-from-charlist x val len))
             (+ (dec-digit-chars-value (take-leading-dec-digit-chars x))
                (* (nfix val)
                   (expt 10
                         (len (take-leading-dec-digit-chars x)))))))

    Theorem: len-of-parse-nat-from-charlist

    (defthm len-of-parse-nat-from-charlist
      (equal (mv-nth 1 (parse-nat-from-charlist x val len))
             (+ (nfix len)
                (len (take-leading-dec-digit-chars x)))))

    Theorem: rest-of-parse-nat-from-charlist

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