• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Community
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
      • Kestrel-books
        • Crypto-hdwallet
        • Apt
          • Simplify-defun
          • Isodata
          • Tailrec
            • Tailrec-implementation
              • Tailrec-event-generation
              • Tailrec-fn
              • Tailrec-macro-definition
              • Tailrec-input-processing
                • Tailrec-process-inputs
                • Tailrec-process-old
                • Tailrec-process-domain
                • Tailrec-decompose-recursive-branch
                  • Tailrec-check-nonrec-conditions
                  • Tailrec-process-variant
                  • Tailrec-infer-domain
                  • Tailrec-find-nonrec-term-in-term/terms
                  • Tailrec-process-accumulator
                  • Tailrec-variantp
            • Schemalg
            • Restrict
            • Expdata
            • Casesplit
            • Simplify-term
            • Simplify-defun-sk
            • Parteval
            • Solve
            • Wrap-output
            • Propagate-iso
            • Simplify
            • Finite-difference
            • Drop-irrelevant-params
            • Copy-function
            • Lift-iso
            • Rename-params
            • Utilities
            • Simplify-term-programmatic
            • Simplify-defun-sk-programmatic
            • Simplify-defun-programmatic
            • Simplify-defun+
            • Common-options
            • Common-concepts
          • Error-checking
          • Fty-extensions
          • Isar
          • Kestrel-utilities
          • Set
          • Soft
          • C
          • 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
    • Tailrec-input-processing

    Tailrec-decompose-recursive-branch

    Decompose the recursive branch of the target function into its components, as described in the documentation.

    Signature
    (tailrec-decompose-recursive-branch old$ rec-branch ctx state) 
      → 
    (mv erp result state)
    Arguments
    old$ — Guard (symbolp old$).
    rec-branch — Guard (pseudo-termp rec-branch).
    Returns
    result — A tuple (nonrec<x1,...,xn> (... update-xi<x1...,xn> ...) combine<q,r> q r), whose components are described in the documentation, satisfying (typed-tuplep pseudo-termp pseudo-term-listp pseudo-termp symbolp symbolp result).

    Definitions and Theorems

    Function: tailrec-decompose-recursive-branch

    (defun tailrec-decompose-recursive-branch
           (old$ rec-branch ctx state)
     (declare (xargs :stobjs (state)))
     (declare (xargs :guard (and (symbolp old$)
                                 (pseudo-termp rec-branch))))
     (let ((__function__ 'tailrec-decompose-recursive-branch))
      (declare (ignorable __function__))
      (b*
       ((rec-calls (all-calls (list old$)
                              rec-branch nil nil))
        (rec-calls (remove-duplicates-equal rec-calls))
        ((when (/= (len rec-calls) 1))
         (er-soft+
          ctx t nil
          "After translation and LET expansion, ~
                       the recursive branch ~x0 of the target function ~x1 ~
                       must not contain different calls to ~x1."
          rec-branch old$))
        (rec-call (car rec-calls))
        ((when (equal rec-call rec-branch))
         (er-soft+ ctx t nil
                   "The target function ~x0 is already tail-recursive."
                   old$))
        (updates (fargs rec-call))
        (formals (formals old$ (w state)))
        (r (genvar old$ "R" nil formals))
        (q (genvar old$ "Q" nil formals))
        (combine-nonrec (subst-expr r rec-call rec-branch))
        ((er &)
         (ensure-term-not-call-of$
          combine-nonrec 'if
          (msg
           "After translation and LET expansion, ~
                          and after replacing the calls to ~x0 ~
                          with a fresh variable ~x1, ~
                          the recursive branch ~x2 of the target function ~x0"
           old$ r combine-nonrec)
          t nil))
        ((mv found nonrec combine)
         (tailrec-find-nonrec-term combine-nonrec combine-nonrec r q))
        ((unless found)
         (er-soft+
          ctx t nil
          "Unable to decompose the recursive branch ~x0 ~
                       of the target function ~x1."
          rec-branch old$)))
       (value (list nonrec updates combine q r)))))