• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
      • Std/lists
        • Std/lists/abstract
        • Rev
        • Defsort
        • List-fix
        • Std/lists/nth
        • Hons-remove-duplicates
        • Std/lists/update-nth
        • Set-equiv
        • Duplicity
        • Prefixp
        • Std/lists/take
        • Std/lists/intersection$
        • Nats-equiv
        • Repeat
        • Index-of
        • All-equalp
        • Sublistp
        • Std/lists/nthcdr
        • Std/lists/append
        • Listpos
        • List-equiv
        • Final-cdr
        • Std/lists/remove
        • Subseq-list
        • Rcons
        • Std/lists/revappend
        • Std/lists/remove-duplicates-equal
        • Std/lists/last
        • Std/lists/reverse
        • Std/lists/resize-list
        • Flatten
          • Suffixp
          • Std/lists/set-difference
          • Std/lists/butlast
          • Std/lists/len
          • Std/lists/intersectp
          • Std/lists/true-listp
          • Intersectp-witness
          • Subsetp-witness
          • Std/lists/remove1-equal
          • Rest-n
          • First-n
          • Std/lists/union
          • Append-without-guard
          • Std/lists/subsetp
          • Std/lists/member
        • Std/alists
        • Obags
        • Std/util
        • Std/strings
        • Std/io
        • Std/osets
        • Std/system
        • Std/basic
        • Std/typed-lists
        • Std/bitsets
        • Std/testing
        • Std/typed-alists
        • Std/stobjs
        • Std-extensions
      • Proof-automation
      • Macro-libraries
      • ACL2
      • Interfacing-tools
      • Hardware-verification
      • Software-verification
      • Testing-utilities
      • Math
    • Std/lists

    Flatten

    (flatten x) appends together the elements of x.

    Typically x is a list of lists that you want to merge together. For example:

    (flatten '((a b c) (1 2 3) (x y z)))
      -->
    (a b c 1 2 3 x y z)

    This is a "one-level" flatten that does not necessarily produce an atom-listp. For instance,

    (flatten '(((a . 1) (b . 2))
               ((x . 3) (y . 4)))
      -->
    ((a . 1) (b . 2) (x . 3) (y . 4))

    Definitions and Theorems

    Function: flatten

    (defun flatten (x)
           (declare (xargs :guard t))
           (if (consp x)
               (append-without-guard (car x)
                                     (flatten (cdr x)))
               nil))

    Theorem: true-listp-of-flatten

    (defthm true-listp-of-flatten
            (true-listp (flatten x))
            :rule-classes :type-prescription)

    Theorem: flatten-when-not-consp

    (defthm flatten-when-not-consp
            (implies (not (consp x))
                     (equal (flatten x) nil)))

    Theorem: flatten-of-cons

    (defthm flatten-of-cons
            (equal (flatten (cons a x))
                   (append a (flatten x))))

    Theorem: flatten-of-list-fix

    (defthm flatten-of-list-fix
            (equal (flatten (list-fix x))
                   (flatten x)))

    Theorem: list-equiv-implies-equal-flatten-1

    (defthm list-equiv-implies-equal-flatten-1
            (implies (list-equiv x x-equiv)
                     (equal (flatten x) (flatten x-equiv)))
            :rule-classes (:congruence))

    Theorem: flatten-of-append

    (defthm flatten-of-append
            (equal (flatten (append x y))
                   (append (flatten x) (flatten y))))