• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Recursion-and-induction
      • Hons-and-memoization
      • Events
      • Parallelism
      • History
      • Programming
        • Defun
        • Declare
        • System-utilities
        • Stobj
        • State
        • Mutual-recursion
        • Memoize
        • Mbe
        • Io
        • Defpkg
        • Apply$
        • Loop$
        • Programming-with-state
        • Arrays
        • Characters
        • Time$
        • Defmacro
        • Loop$-primer
        • Fast-alists
        • Defconst
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
        • Developers-guide
        • System-attachments
        • Advanced-features
        • Set-check-invariant-risk
        • Numbers
        • Efficiency
        • Irrelevant-formals
        • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
        • Redefining-programs
        • Lists
        • Invariant-risk
        • Errors
        • Defabbrev
        • Conses
        • Alists
        • Set-register-invariant-risk
        • Strings
          • Std/strings
            • Pretty-printing
            • Printtree
            • Base64
            • Charset-p
            • Strtok!
            • Cases
              • Icharlisteqv
              • Upcase-charlist
              • Downcase-charlist
              • Upcase-string
                • Upcase-string-aux
                • String-has-some-down-alpha-p
              • Istreqv
              • Downcase-char
              • Upcase-char
              • Ichareqv
              • Downcase-string
              • Upcase-first-charlist
              • Downcase-first-charlist
              • Downcase-first
              • Upcase-first
              • Down-alpha-p
              • Up-alpha-p
              • Upcase-char-str
              • Downcase-char-str
              • Upcase-string-list
              • Downcase-string-list
            • Concatenation
            • Html-encoding
            • Character-kinds
            • Substrings
            • Strtok
            • Equivalences
            • Url-encoding
            • Lines
            • Explode-implode-equalities
            • Ordering
            • Numbers
            • Pad-trim
            • Coercion
            • Std/strings/digit-to-char
            • Substitution
            • Symbols
          • String-listp
          • Stringp
          • Length
          • Search
          • Remove-duplicates
          • Position
          • Coerce
          • Concatenate
          • Reverse
          • String
          • Subseq
          • Substitute
          • String-upcase
          • String-downcase
          • Count
          • Char
          • String<
          • String-equal
          • String-utilities
          • String-append
          • String>=
          • String<=
          • String>
          • Hex-digit-char-theorems
          • String-downcase-gen
          • String-upcase-gen
        • Program-wrapper
        • Get-internal-time
        • Basics
        • Packages
        • Oracle-eval
        • Defmacro-untouchable
        • <<
        • Primitive
        • Revert-world
        • Unmemoize
        • Set-duplicate-keys-action
        • Symbols
        • Def-list-constructor
        • Easy-simplify-term
        • Defiteration
        • Fake-oracle-eval
        • Defopen
        • Sleep
      • Operational-semantics
      • Real
      • Start-here
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Math
    • Testing-utilities
  • Cases
  • String-upcase

Upcase-string

Convert a string to upper case.

Signature
(upcase-string x) → *

(upcase-string x) converts a string to upper case, effectively by transforming each of its characters with upcase-char.

ACL2 has a built-in alternative to this function, common-lisp::string-upcase, but it is irritating to use because it has standard-char-p guards. In contrast, upcase-string works on strings with arbitrary characters.

We try to make this fast. For better performance, we avoid consing and simply return x unchanged when it has no characters that need to be converted. Of course, deciding whether some conversion is necessary will marginally slow this function down when some conversion is necessary, but we think the gain of not consing outweighs this. At any rate, this optimization does not affect the logical definition.

Despite trying to make this fast, the builtin string-upcase can really outperform us since it doesn't have to build the intermediate list, etc. It's really a shame that string-upcase has such a terrible guard. Well, at least we're better when no work needs to be done:

(time (loop for i fixnum from 1 to 1000000 do
        (str::upcase-string "Hello, World!")))  ;; 1.2 seconds, 336 MB
(time (loop for i fixnum from 1 to 1000000 do
        (string-upcase "Hello, World!")))       ;; .26 seconds, 64 MB

(time (loop for i fixnum from 1 to 1000000 do
        (str::upcase-string "HELLO, WORLD!")))  ;; .15 seconds, 0 MB
(time (loop for i fixnum from 1 to 1000000 do
        (string-upcase "HELLO, WORLD!")))       ;; .23 seconds, 64 MB

Definitions and Theorems

Function: upcase-string

(defun upcase-string (x)
  (declare (type string x))
  (let ((acl2::__function__ 'upcase-string))
    (declare (ignorable acl2::__function__))
    (mbe :logic
         (implode (upcase-charlist (explode x)))
         :exec
         (let ((xl (length x)))
           (if (not (string-has-some-down-alpha-p x 0 xl))
               x
             (rchars-to-string (upcase-string-aux x 0 xl nil)))))))

Theorem: istreqv-implies-equal-upcase-string-1

(defthm istreqv-implies-equal-upcase-string-1
  (implies (istreqv x x-equiv)
           (equal (upcase-string x)
                  (upcase-string x-equiv)))
  :rule-classes (:congruence))

Theorem: len-of-upcase-string

(defthm len-of-upcase-string
  (equal (len (explode (upcase-string x)))
         (len (explode x))))

Theorem: length-of-upcase-string

(defthm length-of-upcase-string
  (equal (length (upcase-string x))
         (len (explode x))))

Theorem: equal-of-empty-string-with-upcase-string

(defthm equal-of-empty-string-with-upcase-string
  (equal (equal "" (upcase-string x))
         (atom (explode x))))

Theorem: string-upcase-is-upcase-string

(defthm string-upcase-is-upcase-string
  (implies (standard-char-listp (explode x))
           (equal (common-lisp::string-upcase x)
                  (upcase-string (double-rewrite x)))))

Subtopics

Upcase-string-aux
String-has-some-down-alpha-p