Rename a formal parameters of a defined function or lambda expression.
(atj-rename-formal var indices curr-pkg vars-by-name) → (mv new-var new-indices)
As explained in atj-rename-formals, the renaming of a variable is established when the variable is encountered as a formal parameter. This motivates the name of this function.
This function is called only on formal parameters marked as `new'.
Formal parameters marked as `old' are just renamed
according to the existing renaming map
Each ACL2 function is turned into a Java method, whose body is a shallowly embedded representation of the ACL2 function body. The ACL2 function body may reference the ACL2 function's parameter, as well as let-bound variables (via lambda expressions). Thus, the same variable symbol may in fact denote different variables in different parts of an ACL2 function body. Java does not allow different local variables with the same name in (nested scopes in) the same method, and so we need to map homonymous but different ACL2 variables in the same ACL2 function to differently named Java variables in the same Java method. We use numeric indices, one for each variable name, which is appended (as explained below) to the Java variable name to make it unique within the Java mehtod.
Another need for disambiguation arises because of package prefixes. An ACL2 variable is a symbol, which consists of a name and also a package name: two distinct variables may have the same name but different package names. However, when we append the package name and the name of the symbol, we have unique Java variable names.
We use atj-var-to-jvar to turn
The name obtained by optionally appending the index
may not be a valid Java identifier:
this happens if it is a Java keyword or literal, or if it is empty.
(This may actually happen only if no index is appended.)
If this is the case, we add a single
The index to use for this variable
is retrieved from the
The keys of the alist are not the original ACL2 variables, but the renamed variables resulting from atj-var-to-jvar. This gives us more flexibility, by obviating the requirement that atj-var-to-jvar be injective: if this function is not injective, then different ACL2 variables may become the same Java variable, and the next index must be the same for all of these variables, so that they can be properly disambiguated.
This pre-translation step is performed
after the type annotation and new/old marking steps,
but the caller of this function decomposes the marked annotated variable
into its unmarked unannotated name, type, and marking,
and only passes the unannotated name
Function:
(defun atj-rename-formal (var indices curr-pkg vars-by-name) (declare (xargs :guard (and (symbolp var) (symbol-pos-alistp indices) (stringp curr-pkg) (string-symbollist-alistp vars-by-name)))) (declare (xargs :guard (not (equal curr-pkg "")))) (let ((__function__ 'atj-rename-formal)) (declare (ignorable __function__)) (b* ((jvar (atj-var-to-jvar var curr-pkg vars-by-name)) (jvar+index? (assoc-eq jvar indices)) (index (if jvar+index? (cdr jvar+index?) 0)) (indices (acons jvar (1+ index) indices)) (jvar (atj-var-add-index jvar index))) (mv jvar indices))))
Theorem:
(defthm symbolp-of-atj-rename-formal.new-var (b* (((mv ?new-var ?new-indices) (atj-rename-formal var indices curr-pkg vars-by-name))) (symbolp new-var)) :rule-classes :rewrite)
Theorem:
(defthm symbol-pos-alistp-of-atj-rename-formal.new-indices (implies (and (symbol-pos-alistp indices) (symbolp var)) (b* (((mv ?new-var ?new-indices) (atj-rename-formal var indices curr-pkg vars-by-name))) (symbol-pos-alistp new-indices))) :rule-classes :rewrite)