• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
      • Std/lists
      • Std/alists
      • Obags
      • Std/util
      • Std/strings
        • Pretty-printing
        • Printtree
        • Base64
        • Charset-p
        • Strtok!
        • Cases
        • Concatenation
        • Html-encoding
        • Character-kinds
        • Substrings
          • Istrpos
          • Strrpos
          • Strpos
          • Collect-syms-with-isubstr
          • Istrprefixp
          • Collect-strs-with-isubstr
          • Iprefixp
          • Strsuffixp
          • Firstn-chars
            • Strprefixp
            • Isubstrp
            • Substrp
          • Strtok
          • Equivalences
          • Url-encoding
          • Lines
          • Explode-implode-equalities
          • Ordering
          • Numbers
          • 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
    • Substrings

    Firstn-chars

    Efficient way to take leading characters from a string.

    (firstn-chars n x) is logically equal to:

    (take (min n (length x)) (explode x))

    But it is implemented more efficiently, via char.

    Definitions and Theorems

    Function: firstn-chars-aux

    (defun firstn-chars-aux (x n acc)
      (declare (xargs :guard (and (stringp x)
                                  (natp n)
                                  (< n (length x))))
               (type string x)
               (type (integer 0 *) n))
      (if (zp n)
          (cons (char x 0) acc)
        (firstn-chars-aux x (the (integer 0 *) (- n 1))
                          (cons (char x n) acc))))

    Function: firstn-chars

    (defun firstn-chars (n x)
      (declare (xargs :guard (and (stringp x) (natp n)))
               (type string x)
               (type (integer 0 *) n))
      (mbe :logic
           (take (min (nfix n) (len (explode x)))
                 (explode x))
           :exec
           (let ((n (min n (length x))))
             (if (zp n)
                 nil
               (firstn-chars-aux x (the (integer 0 *) (- n 1))
                                 nil)))))

    Theorem: firstn-chars-aux-removal

    (defthm firstn-chars-aux-removal
      (implies (and (stringp x)
                    (natp n)
                    (< n (length x)))
               (equal (firstn-chars-aux x n acc)
                      (append (take (+ n 1) (coerce x 'list))
                              acc))))

    Theorem: character-listp-of-firstn-chars

    (defthm character-listp-of-firstn-chars
      (character-listp (firstn-chars n x)))

    Theorem: streqv-implies-equal-firstn-chars-2

    (defthm streqv-implies-equal-firstn-chars-2
      (implies (streqv x x-equiv)
               (equal (firstn-chars n x)
                      (firstn-chars n x-equiv)))
      :rule-classes (:congruence))

    Theorem: istreqv-implies-icharlisteqv-firstn-chars-2

    (defthm istreqv-implies-icharlisteqv-firstn-chars-2
      (implies (istreqv x x-equiv)
               (icharlisteqv (firstn-chars n x)
                             (firstn-chars n x-equiv)))
      :rule-classes (:congruence))