• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Hons-and-memoization
      • Events
      • History
      • Parallelism
      • Programming
        • Defun
        • Declare
        • System-utilities
        • Stobj
        • State
        • Memoize
        • Mbe
        • Io
        • Defpkg
        • Apply$
        • Mutual-recursion
        • Loop$
        • Programming-with-state
        • Arrays
        • Characters
          • Character-listp
          • Characterp
          • Coerce
            • Explode
            • Implode
            • Std/strings/coerce
          • Standard-char-p
          • Digit-char-p
          • Code-char
          • Char-code
          • Char-downcase
          • Char
          • Char-upcase
          • Explode-nonnegative-integer
          • Explode-atom
          • Alpha-char-p
          • Upper-case-p
          • Lower-case-p
          • Char-equal
          • Digit-to-char
          • Char<
          • Char>=
          • Make-character-list
          • Char>
          • Char<=
          • Standard-char-listp
          • Character-alistp
          • Standard-char-p+
          • Chars-upcase-gen
          • Chars-downcase-gen
          • Char-upcase-gen
          • Char-downcase-gen
        • Time$
        • Loop$-primer
        • Fast-alists
        • Defmacro
        • Defconst
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
        • Developers-guide
        • System-attachments
        • Advanced-features
        • Set-check-invariant-risk
        • Numbers
        • Irrelevant-formals
        • Efficiency
        • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
        • Redefining-programs
        • Lists
        • Invariant-risk
        • Errors
        • Defabbrev
        • Conses
        • Alists
        • Set-register-invariant-risk
        • Strings
        • Program-wrapper
        • Get-internal-time
        • Basics
        • Packages
        • Defmacro-untouchable
        • Primitive
        • <<
        • Revert-world
        • Set-duplicate-keys-action
        • Unmemoize
        • Symbols
        • Def-list-constructor
        • Easy-simplify-term
        • Defiteration
        • Defopen
        • Sleep
      • Start-here
      • Real
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Testing-utilities
    • Math
  • Strings
  • Characters
  • ACL2-built-ins

Coerce

Coerce a character list to a string and a string to a list

Completion Axiom (completion-of-coerce):

(equal (coerce x y)
       (cond
        ((equal y 'list)
         (if (stringp x)
             (coerce x 'list)
           nil))
        (t
         (coerce (make-character-list x) 'string))))

Guard for (coerce x y):

(if (equal y 'list)
    (stringp x)
  (if (equal y 'string)
      (character-listp x)
    nil))

Also see community book books/misc/fast-coerce.lisp, contributed by Jared Davis, for a version of coerce that may be faster for Common Lisp implementations other than CCL 1.3 or later, if the second argument is 'list (for coercing a string to a list).

Logical Note

The function coerce can be viewed as the constructor for strings. As discussed in Section 7 of "A Precise Description of the ACL2 Logic" (Matt Kaufmann and J Moore, April, 1998), a string may be built by coercing its list of characters: for example, "abc" is (coerce '(#\a #\b #\c) 'string). More precisely, "abc" is an abbreviation for (coerce '(#\a #\b #\c) 'string), where even more pedantically, '(#\a #\b #\c) is an abbreviation for (cons '#\a (cons '#\b (cons '#\c 'nil))).

Subtopics

Explode
Convert a string to a character list.
Implode
Convert a character list into a string.
Std/strings/coerce
Lemmas about coerce available in the std/strings library.