• 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
            • Concatenation
            • Html-encoding
            • Character-kinds
            • Substrings
            • Strtok
            • Equivalences
            • Url-encoding
            • Lines
              • Strlines
              • Prefix-lines
                • Strline
              • 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
    • Lines

    Prefix-lines

    Add a prefix to every line in a string.

    (prefix-lines x prefix) builds a new string by adding prefix to the start of every line in the string x. The start of x is regarded as the start of a line, and also gets the prefix. For instance,

    (prefix-lines "hello world
    goodbye world" "  ** ")

    Would create the following result:

    "  ** hello world
      ** goodbye world"

    This is sometimes useful for indenting blobs of text when you are trying to pretty-print things. The operation is fairly efficient: we cons everything into a character list and then coerce it back into a string at the end.

    Definitions and Theorems

    Function: prefix-lines-aux

    (defun prefix-lines-aux (n x xl acc prefix)
      (declare (xargs :guard (and (natp n)
                                  (stringp x)
                                  (natp xl)
                                  (<= n xl)
                                  (= xl (length x))
                                  (stringp prefix))))
      (let ((n (lnfix n)) (xl (lnfix xl)))
        (if (mbe :logic (zp (- xl n))
                 :exec (int= n xl))
            acc
          (let* ((char (char x n))
                 (acc (cons char acc))
                 (acc (if (eql char #\Newline)
                          (revappend-chars prefix acc)
                        acc)))
            (prefix-lines-aux (+ 1 n)
                              x xl acc prefix)))))

    Function: prefix-lines

    (defun prefix-lines (x prefix)
      (declare (xargs :guard (and (stringp x) (stringp prefix))))
      (let* ((acc (revappend-chars prefix nil))
             (rchars (prefix-lines-aux 0 x (length x)
                                       acc prefix)))
        (rchars-to-string rchars)))

    Theorem: character-listp-of-prefix-lines-aux

    (defthm character-listp-of-prefix-lines-aux
      (implies (and (natp n)
                    (stringp x)
                    (<= n xl)
                    (= xl (length x))
                    (stringp rprefix)
                    (character-listp acc))
               (character-listp (prefix-lines-aux n x xl acc prefix))))

    Theorem: stringp-of-prefix-lines

    (defthm stringp-of-prefix-lines
      (stringp (prefix-lines x prefix))
      :rule-classes :type-prescription)