• 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
        • Bv
        • Imp-language
        • Event-macros
        • Java
          • Atj
            • Atj-implementation
              • Atj-types
              • Atj-java-primitive-array-model
              • Atj-java-abstract-syntax
              • Atj-input-processing
              • Atj-java-pretty-printer
              • Atj-code-generation
                • Atj-gen-test-method
                • Atj-shallow-code-generation
                  • Atj-gen-shallow-term-fns
                  • String-jmethodlist-alistp
                  • Atj-gen-shallow-fndef-method
                    • String-jfieldlist-alistp
                    • Atj-gen-shallow-test-code
                    • Atj-adapt-expr-to-type
                    • Atj-gen-shallow-fn-call
                    • Atj-check-marked-annotated-mv-let-call
                    • Atj-gen-shallow-main-class
                    • Atj-gen-shallow-fnnative-method
                    • Atj-gen-shallow-synonym-method
                    • Atj-gen-shallow-if-call
                    • Atj-gen-shallow-and-call
                    • Atj-gen-shallow-pkg-methods
                    • Atj-convert-expr-to-jprim
                    • Atj-gen-shallow-or-call
                    • Atj-convert-expr-from-jprimarr-method
                    • Atj-adapt-expr-to-types
                    • Atj-gen-shallow-all-pkg-methods
                    • Atj-convert-expr-to-jprimarr-method
                    • Atj-gen-shallow-fndef-all-methods
                    • Atj-convert-expr-from-jprim
                    • Atj-gen-shallow-mv-class
                    • Atj-gen-shallow-main-cunit
                    • Atj-gen-shallow-fndef-methods
                    • Atj-gen-shallow-mv-class-name
                    • Atj-shallow-fns-that-may-throw
                    • Atj-gen-shallow-term
                    • Atj-gen-shallow-let-bindings
                    • Atj-gen-shallow-fn-methods
                    • Atj-gen-shallow-jprimarr-new-init-call
                    • Atj-gen-shallow-fnname
                    • Atj-gen-shallow-all-fn-methods
                    • Atj-gen-shallow-not-call
                    • Atj-fnnative-method-name
                    • Atj-gen-shallow-mv-let
                    • Atj-gen-shallow-jprim-constr-call
                    • Atj-gen-shallow-jprimarr-write-call
                    • Atj-gen-shallow-fnnative-all-methods
                    • Atj-gen-shallow-pkg-class
                    • Atj-gen-shallow-jprimarr-length-call
                    • Atj-gen-shallow-pkg-fields
                    • Atj-all-mv-output-types
                    • Atj-gen-shallow-mv-call
                    • Atj-gen-shallow-jprim-binop-call
                    • Atj-gen-shallow-jprim-conv-call
                    • Atj-gen-shallow-primarray-write-method
                    • Atj-gen-shallow-mv-fields
                    • Atj-gen-shallow-jprimarr-read-call
                    • Atj-gen-shallow-jprimarr-new-len-call
                    • Atj-gen-shallow-jprimarr-conv-tolist-call
                    • Atj-gen-shallow-jprimarr-conv-fromlist-call
                    • Atj-gen-shallow-synonym-all-methods
                    • Atj-gen-shallow-jprim-deconstr-call
                    • Atj-gen-shallow-all-pkg-fields
                    • Atj-gen-shallow-test-code-asgs
                    • Atj-gen-shallow-lambda
                    • Atj-gen-shallow-jprim-unop-call
                    • Atj-jprim-binop-fn-to-jbinop
                    • Atj-gen-shallow-mv-asgs
                    • Atj-gen-shallow-env-class
                    • Atj-gen-shallow-mv-params
                    • Atj-gen-shallow-fnnative-methods
                    • Atj-gen-shallow-pkg-classes
                    • Atj-gen-shallow-env-cunit
                    • Atj-gen-shallow-all-synonym-methods
                    • Atj-convert-expr-to-jprimarr
                    • Atj-convert-expr-from-jprimarr
                    • Atj-jprim-constr-fn-of-qconst-to-expr
                    • Atj-gen-shallow-test-code-mv-asgs
                    • Atj-gen-shallow-synonym-methods
                    • Atj-gen-shallow-synonym-method-params
                    • Atj-convert-expr-to-jprimarr-method-name
                    • Atj-convert-expr-from-jprimarr-method-name
                    • Atj-jexpr-list-to-3-jexprs
                    • Atj-jblock-list-to-3-jblocks
                    • Atj-gen-shallow-test-code-comps
                    • Atj-jprim-conv-fn-to-jtype
                    • Atj-gen-shallow-terms
                    • Atj-gen-shallow-mv-field-name
                    • Atj-adapt-exprs-to-types
                    • Atj-jblock-list-to-2-jblocks
                    • Atj-gen-shallow-primarray-write-methods
                    • Atj-gen-shallow-mv-classes
                    • Atj-gen-shallow-jtype
                    • Atj-gen-shallow-build-method
                    • Atj-jexpr-list-to-2-jexprs
                    • Atj-jprimarr-write-to-method-name
                    • Atj-gen-shallow-all-jprimarr-conv-methods
                    • Atj-jprimarr-new-len-fn-to-comp-jtype
                    • Atj-jprimarr-new-init-fn-to-comp-jtype
                    • Atj-jprim-unop-fn-to-junop
                    • *atj-gen-cond-exprs*
                    • Atj-primarray-write-method-name
                    • Atj-gen-shallow-jprimarr-fromlist-methods
                    • Atj-gen-shallow-jprimarr-tolist-methods
                    • Atj-gen-shallow-mv-classes-guard
                    • *atj-mv-singleton-field-name*
                    • *atj-mv-factory-method-name*
                  • Atj-common-code-generation
                  • Atj-shallow-quoted-constant-generation
                  • Atj-pre-translation
                  • Atj-gen-everything
                  • Atj-name-translation
                  • Atj-gen-test-cunit
                  • Atj-gen-test-class
                  • Atj-gen-main-file
                  • Atj-post-translation
                  • Atj-deep-code-generation
                  • Atj-gen-test-methods
                  • Atj-gen-test-file
                  • Atj-gen-env-file
                  • Atj-gen-output-subdir
                • Atj-java-primitives
                • Atj-java-primitive-arrays
                • Atj-type-macros
                • Atj-java-syntax-operations
                • Atj-fn
                • Atj-library-extensions
                • Atj-java-input-types
                • Atj-test-structures
                • Aij-notions
                • Atj-macro-definition
              • Atj-tutorial
            • Aij
            • Language
          • 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
    • Atj-shallow-code-generation

    Atj-gen-shallow-fndef-method

    Generate a Java method with the given types for an ACL2 function definition.

    Signature
    (atj-gen-shallow-fndef-method 
         fn fns-that-may-throw 
         fn-type formals body method-name qconsts 
         pkg-class-names fn-method-names curr-pkg 
         mv-typess guards$ no-aij-types$ wrld) 
     
      → 
    (mv method new-qconsts new-mv-typess)
    Arguments
    fn — Guard (symbolp fn).
    fns-that-may-throw — Guard (symbol-listp fns-that-may-throw).
    fn-type — Guard (atj-function-type-p fn-type).
    formals — Guard (symbol-listp formals).
    body — Guard (pseudo-termp body).
    method-name — Guard (stringp method-name).
    qconsts — Guard (atj-qconstants-p qconsts).
    pkg-class-names — Guard (string-string-alistp pkg-class-names).
    fn-method-names — Guard (symbol-string-alistp fn-method-names).
    curr-pkg — Guard (stringp curr-pkg).
    mv-typess — Guard (atj-type-list-listp mv-typess).
    guards$ — Guard (booleanp guards$).
    no-aij-types$ — Guard (booleanp no-aij-types$).
    wrld — Guard (plist-worldp wrld).
    Returns
    method — Type (jmethodp method).
    new-qconsts — Type (atj-qconstants-p new-qconsts), given (atj-qconstants-p qconsts).
    new-mv-typess — Type (and (atj-type-list-listp new-mv-typess) (cons-listp new-mv-typess)).

    In the shallow embedding approach, each ACL2 function definition is turned into a Java method. This is a public static method with the same number of parameters as the ACL2 function. More precisely, we generate an overloaded method for each primary and secondary function type associated to the function via atj-main-function-type and atj-other-function-type.

    This function generates one such method, based on the (primary or secondary) function type passed as input. First, we pre-translate the function. Then, we translate the pre-translated function to a Java method. Finally, we post-translate the Java method.

    We also collect all the quoted constants in the pre-translated function body, and add it to the collection that it threaded through.

    The formals and body of the function, as well as the method name, are the same for all the overloaded methods, so they are calculated once and passed to this function. However, the generation of the Java method (pre-translation, translation, and post-translation) must be done afresh for each overloaded methods, because it is affected by the function types, which are turned into the method's argument and result types: with different types, there may be different type annotations, and in particular different type conversions. In fact, it is expected that, with narrower types, there will be fewer type conversions. The pre-translation steps before the type annotation step could be actually factored out and done once, but for greater implementation simplicity here we repeat them for every overloaded method.

    We collect all the mv types in the body for which we will need to generate mv classes.

    Definitions and Theorems

    Function: atj-gen-shallow-fndef-method

    (defun atj-gen-shallow-fndef-method
           (fn fns-that-may-throw
               fn-type formals body method-name qconsts
               pkg-class-names fn-method-names curr-pkg
               mv-typess guards$ no-aij-types$ wrld)
     (declare (xargs :guard (and (symbolp fn)
                                 (symbol-listp fns-that-may-throw)
                                 (atj-function-type-p fn-type)
                                 (symbol-listp formals)
                                 (pseudo-termp body)
                                 (stringp method-name)
                                 (atj-qconstants-p qconsts)
                                 (string-string-alistp pkg-class-names)
                                 (symbol-string-alistp fn-method-names)
                                 (stringp curr-pkg)
                                 (atj-type-list-listp mv-typess)
                                 (booleanp guards$)
                                 (booleanp no-aij-types$)
                                 (plist-worldp wrld))))
     (declare (xargs :guard (and (not (aij-nativep fn))
                                 (not (equal curr-pkg ""))
                                 (cons-listp mv-typess))))
     (let ((__function__ 'atj-gen-shallow-fndef-method))
      (declare (ignorable __function__))
      (b*
       ((in-types (atj-function-type->inputs fn-type))
        (out-types (atj-function-type->outputs fn-type))
        (out-arrays (atj-function-type->arrays fn-type))
        ((unless (= (len in-types) (len formals)))
         (raise
          "Internal error: ~
                    the number ~x0 of parameters of ~x1 ~
                    does not match the number ~x2 of input types of ~x1."
          (len formals)
          fn (len in-types))
         (mv (ec-call (jmethod-fix :irrelevant))
             qconsts nil))
        ((unless (consp out-types))
         (raise "Internal error: no output types ~x0 for ~x1."
                out-types fn)
         (mv (ec-call (jmethod-fix :irrelevant))
             qconsts nil))
        ((mv formals body mv-typess)
         (atj-pre-translate fn formals body
                            in-types out-types out-arrays mv-typess
                            nil guards$ no-aij-types$ wrld))
        (qconsts (atj-add-qconstants-in-term body qconsts))
        ((mv formals &)
         (atj-unmark-vars formals))
        (formals (atj-type-unannotate-vars formals))
        (method-params
            (atj-gen-paramlist (symbol-name-lst formals)
                               (atj-type-list-to-jitype-list in-types)))
        ((mv body-block body-expr &)
         (atj-gen-shallow-term body
                               "$tmp" 1 pkg-class-names fn-method-names
                               curr-pkg (atj-qconstants->pairs qconsts)
                               guards$ wrld))
        (method-body (append body-block (jblock-return body-expr)))
        (tailrecp (and (logicp fn wrld)
                       (= 1 (len (irecursivep fn wrld)))
                       (tail-recursive-p fn wrld)))
        (method-body
           (atj-post-translate-body method-name
                                    method-params method-body tailrecp))
        ((unless (consp out-types))
         (raise
          "Internal error: ~
                    the function ~x0 has no output types ~x1."
          fn out-types)
         (mv (ec-call (jmethod-fix :irrelevant))
             qconsts nil))
        (out-jtype (atj-gen-shallow-jtype out-types))
        (throws (and (member-eq fn fns-that-may-throw)
                     (list *aij-class-undef-pkg-exc*)))
        (method (make-jmethod :access (jaccess-public)
                              :abstract? nil
                              :static? t
                              :final? nil
                              :synchronized? nil
                              :native? nil
                              :strictfp? nil
                              :result (jresult-type out-jtype)
                              :name method-name
                              :params method-params
                              :throws throws
                              :body method-body)))
       (mv method qconsts mv-typess))))

    Theorem: jmethodp-of-atj-gen-shallow-fndef-method.method

    (defthm jmethodp-of-atj-gen-shallow-fndef-method.method
      (b* (((mv common-lisp::?method
                ?new-qconsts ?new-mv-typess)
            (atj-gen-shallow-fndef-method
                 fn fns-that-may-throw
                 fn-type formals body method-name qconsts
                 pkg-class-names fn-method-names curr-pkg
                 mv-typess guards$ no-aij-types$ wrld)))
        (jmethodp method))
      :rule-classes :rewrite)

    Theorem: atj-qconstants-p-of-atj-gen-shallow-fndef-method.new-qconsts

    (defthm atj-qconstants-p-of-atj-gen-shallow-fndef-method.new-qconsts
      (implies (atj-qconstants-p qconsts)
               (b* (((mv common-lisp::?method
                         ?new-qconsts ?new-mv-typess)
                     (atj-gen-shallow-fndef-method
                          fn fns-that-may-throw
                          fn-type formals body method-name qconsts
                          pkg-class-names fn-method-names curr-pkg
                          mv-typess guards$ no-aij-types$ wrld)))
                 (atj-qconstants-p new-qconsts)))
      :rule-classes :rewrite)

    Theorem: return-type-of-atj-gen-shallow-fndef-method.new-mv-typess

    (defthm return-type-of-atj-gen-shallow-fndef-method.new-mv-typess
      (b* (((mv common-lisp::?method
                ?new-qconsts ?new-mv-typess)
            (atj-gen-shallow-fndef-method
                 fn fns-that-may-throw
                 fn-type formals body method-name qconsts
                 pkg-class-names fn-method-names curr-pkg
                 mv-typess guards$ no-aij-types$ wrld)))
        (and (atj-type-list-listp new-mv-typess)
             (cons-listp new-mv-typess)))
      :rule-classes :rewrite)