• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Debugging
    • Projects
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Hons-and-memoization
      • Events
      • History
      • Parallelism
      • Programming
      • Start-here
        • Gentle-introduction-to-ACL2-programming
        • ACL2-tutorial
          • Introduction-to-the-theorem-prover
            • Logic-knowledge-taken-for-granted
            • Logic-knowledge-taken-for-granted-inductive-proof
            • Introduction-to-rewrite-rules-part-1
            • Introduction-to-key-checkpoints
            • Introduction-to-rewrite-rules-part-2
            • Logic-knowledge-taken-for-granted-propositional-calculus
            • Introduction-to-a-few-system-considerations
            • Introduction-to-the-database
            • Programming-knowledge-taken-for-granted
            • Logic-knowledge-taken-for-granted-rewriting
            • Introductory-challenge-problem-4-answer
            • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
            • Introduction-to-hints
            • Dealing-with-key-combinations-of-function-symbols
            • Architecture-of-the-prover
            • Further-information-on-rewriting
            • Frequently-asked-questions-by-newcomers
            • Specific-kinds-of-formulas-as-rewrite-rules
            • Strong-rewrite-rules
            • Practice-formulating-strong-rules-3
            • Practice-formulating-strong-rules-1
            • Generalizing-key-checkpoints
            • Logic-knowledge-taken-for-granted-q1-answer
            • Practice-formulating-strong-rules-6
            • Example-inductions
            • Logic-knowledge-taken-for-granted-q2-answer
            • Logic-knowledge-taken-for-granted-rewriting-repeatedly
            • Introductory-challenge-problem-4
              • Equivalent-formulas-different-rewrite-rules
              • Post-induction-key-checkpoints
              • Special-cases-for-rewrite-rules
              • Example-induction-scheme-with-accumulators
              • Practice-formulating-strong-rules-5
              • Practice-formulating-strong-rules
              • Practice-formulating-strong-rules-2
              • Introductory-challenges
              • Logic-knowledge-taken-for-granted-equals-for-equals
              • Logic-knowledge-taken-for-granted-evaluation
              • Example-induction-scheme-nat-recursion
              • Example-induction-scheme-down-by-2
              • Logic-knowledge-taken-for-granted-instance
              • Introductory-challenge-problem-3-answer
              • Example-induction-scheme-on-lists
              • Example-induction-scheme-upwards
              • Example-induction-scheme-with-multiple-induction-steps
              • Practice-formulating-strong-rules-4
              • Logic-knowledge-taken-for-granted-q3-answer
              • Example-induction-scheme-binary-trees
              • Introductory-challenge-problem-3
              • Introductory-challenge-problem-1
              • Logic-knowledge-taken-for-granted-base-case
              • Introductory-challenge-problem-1-answer
              • Example-induction-scheme-on-several-variables
              • Introductory-challenge-problem-2-answer
              • Introductory-challenge-problem-2
            • Pages Written Especially for the Tours
            • The-method
            • Advanced-features
            • Interesting-applications
            • Tips
            • Alternative-introduction
            • Tidbits
            • Annotated-ACL2-scripts
            • Startup
            • ACL2-as-standalone-program
            • ACL2-sedan
            • Talks
            • Nqthm-to-ACL2
            • Emacs
          • About-ACL2
        • Real
        • Debugging
        • Miscellaneous
        • Output-controls
        • Macros
        • Interfacing-tools
      • Interfacing-tools
      • Hardware-verification
      • Software-verification
      • Testing-utilities
      • Math
    • Introduction-to-the-theorem-prover

    Introductory-challenge-problem-4

    Challenge problem 4 for the new user of ACL2

    Start in a fresh ACL2, either by restarting your ACL2 image from scratch or executing :ubt! 1.

    This problem is much more open ended than the preceding ones. The challenge is to define a function that collects exactly one copy of each element of a list and to prove that it returns a subset of the list with no duplications.

    (Is this all that one might want to prove? It is a good idea to think about that question, for any application; an answer for this example is at the end of this topic.)

    Hint: We recommend that you read this hint to align your function names with our solution, to make comparisons easier. Our answer is shown in see introductory-challenge-problem-4-answer. In that page you'll see a definition of a function collect-once and the proofs of two theorems:

    (defthm main-theorem-1-about-collect-once
      (subsetp (collect-once x) x))
    
    (defthm main-theorem-2-about-collect-once
      (not (dupsp (collect-once x))))

    The function dupsp is as defined in see introductory-challenge-problem-3. This is quite easy.

    Then, we define a tail-recursive version of the method based on the pseudo-code:

    a = nil;
    while (x not empty) {
     a = if (member (car x) a) then a else (cons (car x) a);
     x = (cdr x);
     }
    return a;

    We formalize this with the function while-loop-version, where (while-loop-version x nil) is the ``semantics'' of the code above. I.e., the function while-loop-version captures the while loop in the pseudo-code above and returns the final value of a, and it should be invoked with the initial value of a being nil.

    We prove (while-loop-version x nil) returns a subset of x that contains no duplications. Furthermore, we do it two ways: first ``indirectly'' by relating while-loop-version to collect-once, and second (``directly'') without using collect-once. Both of these proofs are much harder than the collect-once approach, involving about a dozen lemmas each.

    Compare your solutions to ours at see introductory-challenge-problem-4-answer.

    Then, use your browser's Back Button to return to introductory-challenges.

    We conclude this topic by returning to the question posed earlier above: Is this all that one might want to prove? Notice that we didn't prove that every element of the given list is indeed an element of the returned list, which could be formalized as follows.

    (thm
      (subsetp x (collect-once x)))