• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Community
    • Proof-automation
    • ACL2
    • Macro-libraries
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
      • Kestrel-books
        • Crypto-hdwallet
        • Apt
        • Error-checking
        • Fty-extensions
        • Isar
        • Kestrel-utilities
        • Set
        • Soft
        • C
          • Syntax-for-tools
          • Atc
            • Atc-implementation
              • Atc-abstract-syntax
              • Atc-pretty-printer
              • Atc-event-and-code-generation
                • Atc-symbolic-computation-states
                • Atc-symbolic-execution-rules
                • Atc-gen-ext-declon-lists
                • Atc-function-and-loop-generation
                  • Atc-gen-cfun-correct-thm
                  • Atc-typed-formals
                  • Atc-gen-outer-bindings-and-hyps
                  • Atc-gen-fundef
                  • Atc-gen-exec-stmt-while-for-loop
                  • Atc-gen-context-preamble
                  • Atc-gen-pop-frame-thm
                  • Atc-gen-loop-correct-thm
                  • Atc-gen-init-scope-thms
                  • Atc-gen-fun-correct-thm
                  • Atc-gen-fn-result-thm
                  • Atc-gen-loop-body-correct-thm
                  • Atc-gen-loop
                  • Atc-gen-loop-test-correct-thm
                  • Atc-check-guard-conjunct
                  • Atc-find-affected
                  • Atc-gen-cfun-final-compustate
                  • Atc-gen-init-inscope-auto
                  • Atc-gen-init-inscope-static
                  • Atc-gen-push-init-thm
                  • Atc-gen-loop-measure-fn
                    • Atc-gen-fun-endstate
                    • Atc-gen-loop-termination-thm
                    • Atc-gen-formal-thm
                    • Atc-gen-loop-final-compustate
                    • Atc-gen-loop-measure-thm
                    • Atc-gen-object-disjoint-hyps
                    • Atc-loop-body-term-subst
                    • Atc-gen-omap-update-formals
                    • Atc-gen-loop-tthm-formula
                    • Atc-gen-init-inscope
                    • Atc-gen-fn-def*
                    • Atc-gen-param-declon-list
                    • Atc-formal-affectablep
                    • Atc-gen-cfun-fun-env-thm
                    • Atc-gen-add-var-formals
                    • Atc-gen-cfun-fun-env-thm-name
                    • Atc-gen-fn-guard
                    • Atc-filter-exec-fun-args
                    • Atc-gen-context-preamble-aux-aux
                    • Atc-typed-formals-to-extobjs
                    • Atc-formal-affectable-listp
                  • Atc-statement-generation
                  • Atc-gen-fileset
                  • Atc-gen-everything
                  • Atc-gen-obj-declon
                  • Atc-gen-fileset-event
                  • Atc-tag-tables
                  • Atc-expression-generation
                  • Atc-generation-contexts
                  • Atc-gen-wf-thm
                  • Term-checkers-atc
                  • Atc-variable-tables
                  • Term-checkers-common
                  • Atc-gen-init-fun-env-thm
                  • Atc-gen-appconds
                  • Read-write-variables
                  • Atc-gen-thm-assert-events
                  • Test*
                  • Atc-gen-prog-const
                  • Atc-gen-expr-bool
                  • Atc-theorem-generation
                  • Atc-tag-generation
                  • Atc-gen-expr-pure
                  • Atc-function-tables
                  • Atc-object-tables
                • Fty-pseudo-term-utilities
                • Atc-term-recognizers
                • Atc-input-processing
                • Atc-shallow-embedding
                • Atc-process-inputs-and-gen-everything
                • Atc-table
                • Atc-fn
                • Atc-pretty-printing-options
                • Atc-types
                • Atc-macro-definition
              • Atc-tutorial
            • Language
            • Representation
            • Transformation-tools
            • Insertion-sort
            • Pack
          • Bv
          • Imp-language
          • Event-macros
          • Java
          • Bitcoin
          • Ethereum
          • Yul
          • Zcash
          • ACL2-programming-language
          • Prime-fields
          • Json
          • Syntheto
          • File-io-light
          • Cryptography
          • Number-theory
          • Lists-light
          • Axe
          • Builtins
          • Solidity
          • Helpers
          • Htclient
          • Typed-lists-light
          • Arithmetic-light
        • X86isa
        • Axe
        • Execloader
      • Math
      • Testing-utilities
    • Atc-function-and-loop-generation

    Atc-gen-loop-measure-fn

    Generate a measure function for a recursive target function.

    Signature
    (atc-gen-loop-measure-fn fn names-to-avoid state) 
      → 
    (mv event name formals updated-names-to-avoid)
    Arguments
    fn — Guard (symbolp fn).
    names-to-avoid — Guard (symbol-listp names-to-avoid).
    Returns
    event — Type (pseudo-event-formp event).
    name — Type (symbolp name).
    formals — Type (symbol-listp formals).
    updated-names-to-avoid — Type (symbol-listp updated-names-to-avoid), given (symbol-listp names-to-avoid).

    The correctness theorem for a loop involves the measure of the loop function. The measure may be a complex term. An early version of ATC was using the measure terms directly in the generated theorems, but that caused proof failures sometimes, due to ACL2 sometimes modifying those measure terms during a proof (e.g. due to equalities involving measure subterms arising from case analyses): after the terms were modified, some of the generated theorems about the measure terms no longer apply, making the proof fail. Thus, we ``protect'' the measure terms from modifications by generating functions for them, and using those functions in the generated theorems.

    The code of this ACL2 function generates a measure function for the recursive target function fn. The funcion is not guard-verified, because its is only logical. It is important that we take, as formal parameters of the generated measure function, only the variables that occur in the measure term. This facilitates the generation of the loop function's termination theorem expressed over the generated measure function.

    Definitions and Theorems

    Function: atc-gen-loop-measure-fn

    (defun atc-gen-loop-measure-fn (fn names-to-avoid state)
      (declare (xargs :stobjs (state)))
      (declare (xargs :guard (and (symbolp fn)
                                  (symbol-listp names-to-avoid))))
      (declare (xargs :guard (irecursivep+ fn (w state))))
      (let ((__function__ 'atc-gen-loop-measure-fn))
        (declare (ignorable __function__))
        (b* ((wrld (w state))
             (name (packn-pos (list 'measure-of- fn) fn))
             ((mv name names-to-avoid)
              (fresh-logical-name-with-$s-suffix name 'function
                                                 names-to-avoid wrld))
             ((when (eq name 'quote))
              (raise "Internal error: name is QUOTE.")
              (mv '(_) nil nil nil))
             (measure-term (measure+ fn wrld))
             (measure-vars (all-vars measure-term))
             ((mv & event)
              (evmac-generate-defun
                   name
                   :formals measure-vars
                   :body (untranslate$ measure-term nil state)
                   :verify-guards nil
                   :enable nil)))
          (mv event
              name measure-vars names-to-avoid))))

    Theorem: pseudo-event-formp-of-atc-gen-loop-measure-fn.event

    (defthm pseudo-event-formp-of-atc-gen-loop-measure-fn.event
      (b* (((mv acl2::?event
                ?name ?formals ?updated-names-to-avoid)
            (atc-gen-loop-measure-fn fn names-to-avoid state)))
        (pseudo-event-formp event))
      :rule-classes :rewrite)

    Theorem: symbolp-of-atc-gen-loop-measure-fn.name

    (defthm symbolp-of-atc-gen-loop-measure-fn.name
      (b* (((mv acl2::?event
                ?name ?formals ?updated-names-to-avoid)
            (atc-gen-loop-measure-fn fn names-to-avoid state)))
        (symbolp name))
      :rule-classes :rewrite)

    Theorem: symbol-listp-of-atc-gen-loop-measure-fn.formals

    (defthm symbol-listp-of-atc-gen-loop-measure-fn.formals
      (b* (((mv acl2::?event
                ?name ?formals ?updated-names-to-avoid)
            (atc-gen-loop-measure-fn fn names-to-avoid state)))
        (symbol-listp formals))
      :rule-classes :rewrite)

    Theorem: symbol-listp-of-atc-gen-loop-measure-fn.updated-names-to-avoid

    (defthm
         symbol-listp-of-atc-gen-loop-measure-fn.updated-names-to-avoid
      (implies (symbol-listp names-to-avoid)
               (b* (((mv acl2::?event
                         ?name ?formals ?updated-names-to-avoid)
                     (atc-gen-loop-measure-fn fn names-to-avoid state)))
                 (symbol-listp updated-names-to-avoid)))
      :rule-classes :rewrite)

    Theorem: atc-gen-loop-measure-fn-name-not-quote

    (defthm atc-gen-loop-measure-fn-name-not-quote
      (b* (((mv acl2::?event
                ?name ?formals ?updated-names-to-avoid)
            (atc-gen-loop-measure-fn fn names-to-avoid state)))
        (not (equal name 'quote))))