Generate the mv class for the given types.
(atj-gen-shallow-mv-class types) → class
As explained in atj-gen-shallow-mv-class-name, we generate Java classes that represent mv values, a different class for each tuple of types of interest. This is a public static final class, nested in the main class. It has a public instance field for each component type, and a private static final field that stores a singleton instance created in this field's initializer. It also has a package-private static factory method that reuses the singleton instance by assigning the arguments to the fields and then returning the singleton instance. This way, we can build an mv value without creating a new Java object.
The class and its instance fields are public because external Java code that calls the generated Java code must be able to access the results of mv functions. The factory method is not meant to be called by external Java code, but only internally by the generated Java code; it cannot be private though, only package-private (ideally, it would be accessible only within the generated main class, but Java does not provide that access control option).
Ideally, we would like to generate one generic Java class with 2 type parameters for 2-tuples of MV values, one generic Java class with 3 type parameters for 3-tuples of MV values, and so on. However, Java's restriction on generic types prevent us from doing that: the factory method, being static, cannot reference type parameters.
Function:
(defun atj-gen-shallow-mv-class (types) (declare (xargs :guard (atj-type-listp types))) (declare (xargs :guard (>= (len types) 2))) (let ((__function__ 'atj-gen-shallow-mv-class)) (declare (ignorable __function__)) (b* ((name (atj-gen-shallow-mv-class-name types)) (instance-fields (atj-gen-shallow-mv-fields types)) (static-field (make-jfield :access (jaccess-private) :static? t :final? t :transient? nil :volatile? nil :type (jtype-class name) :name *atj-mv-singleton-field-name* :init? (jexpr-newclass (jtype-class name) nil))) (method-body (append (atj-gen-shallow-mv-asgs types) (jblock-return (jexpr-name *atj-mv-singleton-field-name*)))) (method (make-jmethod :access (jaccess-default) :abstract? nil :static? t :final? nil :synchronized? nil :native? nil :strictfp? nil :result (jresult-type (jtype-class name)) :name *atj-mv-factory-method-name* :params (atj-gen-shallow-mv-params types) :throws nil :body method-body))) (make-jclass :access (jaccess-public) :abstract? nil :static? t :final? t :strictfp? nil :name name :superclass? nil :superinterfaces nil :body (append (jfields-to-jcbody-elements (append instance-fields (list static-field))) (jmethods-to-jcbody-elements (list method)))))))
Theorem:
(defthm jclassp-of-atj-gen-shallow-mv-class (b* ((class (atj-gen-shallow-mv-class types))) (jclassp class)) :rule-classes :rewrite)