• 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
          • String-listp
          • Stringp
          • Length
          • Search
          • Remove-duplicates
          • Position
          • Coerce
            • Explode
            • Implode
            • Std/strings/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
  • 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.