
(/VERSIONCHECK 2) 

(DEFUN |FORMULA;new;$;1| ($)
  (VECTOR (LIST ".eq set blank @" ":df.") (LIST "") (LIST ":edf."))) 

(DEFUN |FORMULA;coerce;Of$;2| (|expr| $)
  (PROG (|f|)
    (RETURN
      (SEQ (LETT |f| (SPADCALL (QREFELT $ 23)) |FORMULA;coerce;Of$;2|)
           (QSETVELT |f| 1
               (LIST (|FORMULA;postcondition|
                         (|FORMULA;formatFormula|
                             (|FORMULA;precondition| |expr| $)
                             (QREFELT $ 9) $)
                         $)))
           (EXIT |f|))))) 

(DEFUN |FORMULA;convert;OfI$;3| (|expr| |stepNum| $)
  (PROG (|f|)
    (RETURN
      (SEQ (LETT |f| (SPADCALL (QREFELT $ 23))
                 |FORMULA;convert;OfI$;3|)
           (QSETVELT |f| 1
               (SPADCALL
                   (LIST "<leqno lparen " (STRINGIMAGE |stepNum|)
                         " rparen>")
                   (LIST (|FORMULA;postcondition|
                             (|FORMULA;formatFormula|
                                 (|FORMULA;precondition| |expr| $)
                                 (QREFELT $ 9) $)
                             $))
                   (QREFELT $ 27)))
           (EXIT |f|))))) 

(DEFUN |FORMULA;display;$IV;4| (|f| |len| $)
  (PROG (#0=#:G1842 #1=#:G1843 |t| #2=#:G1844 |s| #3=#:G1845)
    (RETURN
      (SEQ (SEQ (LETT |s| NIL . #4=(|FORMULA;display;$IV;4|))
                (LETT #0# (QVELT |f| 0) . #4#) G190
                (COND
                  ((OR (ATOM #0#)
                       (PROGN (LETT |s| (CAR #0#) . #4#) NIL))
                   #5=(GO G191)))
                (SEQ (EXIT (|sayFORMULA| |s|)))
                (LETT #0# (CDR #0#) . #4#) #6=(GO G190) G191
                (EXIT NIL))
           (SEQ (LETT |s| NIL . #4#) (LETT #1# (QVELT |f| 1) . #4#)
                G190
                (COND
                  ((OR (ATOM #1#)
                       (PROGN (LETT |s| (CAR #1#) . #4#) NIL))
                   #5#))
                (SEQ (EXIT (SEQ (LETT |t| NIL . #4#)
                                (LETT #2#
                                      (|FORMULA;splitLong| |s| |len| $) . #4#)
                                G190
                                (COND
                                  ((OR (ATOM #2#)
                                    (PROGN
                                      (LETT |t| (CAR #2#) . #4#)
                                      NIL))
                                   #5#))
                                (SEQ (EXIT (|sayFORMULA| |t|)))
                                (LETT #2# (CDR #2#) . #4#) #6# G191
                                (EXIT NIL))))
                (LETT #1# (CDR #1#) . #4#) #6# G191 (EXIT NIL))
           (SEQ (LETT |s| NIL . #4#) (LETT #3# (QVELT |f| 2) . #4#)
                G190
                (COND
                  ((OR (ATOM #3#)
                       (PROGN (LETT |s| (CAR #3#) . #4#) NIL))
                   #5#))
                (SEQ (EXIT (|sayFORMULA| |s|)))
                (LETT #3# (CDR #3#) . #4#) #6# G191 (EXIT NIL))
           (EXIT (SPADCALL (QREFELT $ 31))))))) 

(DEFUN |FORMULA;display;$V;5| (|f| $)
  (SPADCALL |f| $LINELENGTH (QREFELT $ 32))) 

(PUT '|FORMULA;prologue;$L;6| '|SPADreplace|
     '(XLAM (|f|) (QVELT |f| 0))) 

(DEFUN |FORMULA;prologue;$L;6| (|f| $) (QVELT |f| 0)) 

(PUT '|FORMULA;formula;$L;7| '|SPADreplace|
     '(XLAM (|f|) (QVELT |f| 1))) 

(DEFUN |FORMULA;formula;$L;7| (|f| $) (QVELT |f| 1)) 

(PUT '|FORMULA;epilogue;$L;8| '|SPADreplace|
     '(XLAM (|f|) (QVELT |f| 2))) 

(DEFUN |FORMULA;epilogue;$L;8| (|f| $) (QVELT |f| 2)) 

(PUT '|FORMULA;setPrologue!;$2L;9| '|SPADreplace|
     '(XLAM (|f| |l|) (QSETVELT |f| 0 |l|))) 

(DEFUN |FORMULA;setPrologue!;$2L;9| (|f| |l| $) (QSETVELT |f| 0 |l|)) 

(PUT '|FORMULA;setFormula!;$2L;10| '|SPADreplace|
     '(XLAM (|f| |l|) (QSETVELT |f| 1 |l|))) 

(DEFUN |FORMULA;setFormula!;$2L;10| (|f| |l| $) (QSETVELT |f| 1 |l|)) 

(PUT '|FORMULA;setEpilogue!;$2L;11| '|SPADreplace|
     '(XLAM (|f| |l|) (QSETVELT |f| 2 |l|))) 

(DEFUN |FORMULA;setEpilogue!;$2L;11| (|f| |l| $) (QSETVELT |f| 2 |l|)) 

(DEFUN |FORMULA;coerce;$Of;12| (|f| $)
  (PROG (#0=#:G1859 #1=#:G1860 |t| #2=#:G1861 |s| #3=#:G1862 |l|)
    (RETURN
      (SEQ (LETT |l| NIL . #4=(|FORMULA;coerce;$Of;12|))
           (SEQ (LETT |s| NIL . #4#) (LETT #0# (QVELT |f| 0) . #4#)
                G190
                (COND
                  ((OR (ATOM #0#)
                       (PROGN (LETT |s| (CAR #0#) . #4#) NIL))
                   #5=(GO G191)))
                (SEQ (EXIT (LETT |l| (CONS |s| |l|) . #4#)))
                (LETT #0# (CDR #0#) . #4#) #6=(GO G190) G191
                (EXIT NIL))
           (SEQ (LETT |s| NIL . #4#) (LETT #1# (QVELT |f| 1) . #4#)
                G190
                (COND
                  ((OR (ATOM #1#)
                       (PROGN (LETT |s| (CAR #1#) . #4#) NIL))
                   #5#))
                (SEQ (EXIT (SEQ (LETT |t| NIL . #4#)
                                (LETT #2#
                                      (|FORMULA;splitLong| |s|
                                       (- $LINELENGTH 4) $) . #4#)
                                G190
                                (COND
                                  ((OR (ATOM #2#)
                                    (PROGN
                                      (LETT |t| (CAR #2#) . #4#)
                                      NIL))
                                   #5#))
                                (SEQ (EXIT
                                      (LETT |l| (CONS |t| |l|) . #4#)))
                                (LETT #2# (CDR #2#) . #4#) #6# G191
                                (EXIT NIL))))
                (LETT #1# (CDR #1#) . #4#) #6# G191 (EXIT NIL))
           (SEQ (LETT |s| NIL . #4#) (LETT #3# (QVELT |f| 2) . #4#)
                G190
                (COND
                  ((OR (ATOM #3#)
                       (PROGN (LETT |s| (CAR #3#) . #4#) NIL))
                   #5#))
                (SEQ (EXIT (LETT |l| (CONS |s| |l|) . #4#)))
                (LETT #3# (CDR #3#) . #4#) #6# G191 (EXIT NIL))
           (EXIT (SPADCALL (REVERSE |l|) (QREFELT $ 40))))))) 

(DEFUN |FORMULA;postcondition| (|str| $)
  (PROG (|len| |plus| |minus| |i| #0=#:G1869)
    (RETURN
      (SEQ (LETT |len| (QCSIZE |str|) . #1=(|FORMULA;postcondition|))
           (EXIT (COND
                   ((< |len| 4) |str|)
                   ('T
                    (SEQ (LETT |plus|
                               (SPADCALL "+" (QREFELT $ 44)) . #1#)
                         (LETT |minus|
                               (SPADCALL "-" (QREFELT $ 44)) . #1#)
                         (SEQ (LETT |i| 1 . #1#)
                              (LETT #0# (- |len| 1) . #1#) G190
                              (COND ((QSGREATERP |i| #0#) (GO G191)))
                              (SEQ (EXIT
                                    (COND
                                      ((SPADCALL
                                        (SPADCALL |str| |i|
                                         (QREFELT $ 45))
                                        |plus| (QREFELT $ 47))
                                       (COND
                                         ((SPADCALL
                                           (SPADCALL |str| (+ |i| 1)
                                            (QREFELT $ 45))
                                           |minus| (QREFELT $ 47))
                                          (SPADCALL |str| |i|
                                           (SPADCALL " "
                                            (QREFELT $ 44))
                                           (QREFELT $ 48))))))))
                              (LETT |i| (QSADD1 |i|) . #1#) (GO G190)
                              G191 (EXIT NIL))
                         (EXIT |str|))))))))) 

(PUT '|FORMULA;stringify| '|SPADreplace| '|object2String|) 

(DEFUN |FORMULA;stringify| (|expr| $) (|object2String| |expr|)) 

(DEFUN |FORMULA;splitLong| (|str| |len| $)
  (SEQ (COND
         ((< |len| 20) (LETT |len| $LINELENGTH |FORMULA;splitLong|)))
       (EXIT (|FORMULA;splitLong1| |str| |len| $)))) 

(DEFUN |FORMULA;splitLong1| (|str| |len| $)
  (PROG (|ss| #0=#:G1880 |lss| |ls| |s| |l|)
    (RETURN
      (SEQ (LETT |l| NIL . #1=(|FORMULA;splitLong1|))
           (LETT |s| "" . #1#) (LETT |ls| 0 . #1#)
           (SEQ (LETT |ss| NIL . #1#)
                (LETT #0#
                      (SPADCALL |str| (SPADCALL " " (QREFELT $ 44))
                          (QREFELT $ 50)) . #1#)
                G190
                (COND
                  ((OR (ATOM #0#)
                       (PROGN (LETT |ss| (CAR #0#) . #1#) NIL))
                   (GO G191)))
                (SEQ (LETT |lss| (QCSIZE |ss|) . #1#)
                     (COND
                       ((< |len| (+ |ls| |lss|))
                        (SEQ (LETT |l| (CONS |s| |l|) . #1#)
                             (LETT |s| "" . #1#)
                             (EXIT (LETT |ls| 0 . #1#)))))
                     (EXIT (COND
                             ((< |len| |lss|)
                              (LETT |l| (CONS |ss| |l|) . #1#))
                             ('T
                              (SEQ (LETT |ls|
                                    (+ (+ |ls| |lss|) 1) . #1#)
                                   (EXIT
                                    (LETT |s|
                                     (STRCONC |s| (STRCONC |ss| " ")) . #1#)))))))
                (LETT #0# (CDR #0#) . #1#) (GO G190) G191 (EXIT NIL))
           (COND ((< 0 |ls|) (LETT |l| (CONS |s| |l|) . #1#)))
           (EXIT (REVERSE |l|)))))) 

(DEFUN |FORMULA;group| (|str| $)
  (SPADCALL (LIST "<" |str| ">") (QREFELT $ 51))) 

(DEFUN |FORMULA;addBraces| (|str| $)
  (SPADCALL (LIST "left lbrace " |str| " right rbrace") (QREFELT $ 51))) 

(DEFUN |FORMULA;addBrackets| (|str| $)
  (SPADCALL (LIST "left lb " |str| " right rb") (QREFELT $ 51))) 

(DEFUN |FORMULA;parenthesize| (|str| $)
  (SPADCALL (LIST "left lparen " |str| " right rparen") (QREFELT $ 51))) 

(PUT '|FORMULA;precondition| '|SPADreplace| '|outputTran|) 

(DEFUN |FORMULA;precondition| (|expr| $) (|outputTran| |expr|)) 

(DEFUN |FORMULA;formatSpecial| (|op| |args| |prec| $)
  (PROG (|tmp| |form|)
    (RETURN
      (SEQ (COND
             ((EQUAL |op| "AGGLST")
              (|FORMULA;formatNary| "," |args| |prec| $))
             ((EQUAL |op| "AGGSET")
              (|FORMULA;formatNary| ";" |args| |prec| $))
             ((EQUAL |op| "CONCATB")
              (|FORMULA;formatNary| " " |args| |prec| $))
             ((EQUAL |op| "CONCAT")
              (|FORMULA;formatNary| "" |args| |prec| $))
             ((EQUAL |op| "BRACKET")
              (|FORMULA;group|
                  (|FORMULA;addBrackets|
                      (|FORMULA;formatFormula| (|SPADfirst| |args|)
                          (QREFELT $ 9) $)
                      $)
                  $))
             ((EQUAL |op| "BRACE")
              (|FORMULA;group|
                  (|FORMULA;addBraces|
                      (|FORMULA;formatFormula| (|SPADfirst| |args|)
                          (QREFELT $ 9) $)
                      $)
                  $))
             ((EQUAL |op| "PAREN")
              (|FORMULA;group|
                  (|FORMULA;parenthesize|
                      (|FORMULA;formatFormula| (|SPADfirst| |args|)
                          (QREFELT $ 9) $)
                      $)
                  $))
             ((EQUAL |op| "OVERBAR")
              (COND
                ((NULL |args|) "")
                (#0='T
                 (|FORMULA;group|
                     (SPADCALL
                         (LIST (|FORMULA;formatFormula|
                                   (|SPADfirst| |args|) (QREFELT $ 9)
                                   $)
                               " bar")
                         (QREFELT $ 51))
                     $))))
             ((EQUAL |op| "ROOT")
              (COND
                ((NULL |args|) "")
                (#0#
                 (SEQ (LETT |tmp|
                            (|FORMULA;formatFormula|
                                (|SPADfirst| |args|) (QREFELT $ 9) $)
                            . #1=(|FORMULA;formatSpecial|))
                      (EXIT (COND
                              ((NULL (CDR |args|))
                               (|FORMULA;group|
                                   (SPADCALL (LIST "sqrt " |tmp|)
                                    (QREFELT $ 51))
                                   $))
                              ('T
                               (|FORMULA;group|
                                   (SPADCALL
                                    (LIST "midsup adjust(u 1.5 r 9) "
                                     (|FORMULA;formatFormula|
                                      (|SPADfirst| (CDR |args|))
                                      (QREFELT $ 9) $)
                                     " sqrt " |tmp|)
                                    (QREFELT $ 51))
                                   $))))))))
             ((EQUAL |op| "SC")
              (|FORMULA;formatNary| " labove " |args| |prec| $))
             ((EQUAL |op| "SUB")
              (|FORMULA;group|
                  (SPADCALL
                      (LIST (|FORMULA;formatFormula|
                                (|SPADfirst| |args|) (QREFELT $ 9) $)
                            " sub "
                            (|FORMULA;formatSpecial| "AGGLST"
                                (CDR |args|) (QREFELT $ 9) $))
                      (QREFELT $ 51))
                  $))
             ((EQUAL |op| "SUPERSUB")
              (SEQ (LETT |form|
                         (LIST (|FORMULA;formatFormula|
                                   (|SPADfirst| |args|) (QREFELT $ 9)
                                   $)) . #1#)
                   (LETT |args| (CDR |args|) . #1#)
                   (EXIT (COND
                           ((NULL |args|)
                            (SPADCALL |form| (QREFELT $ 51)))
                           (#0#
                            (SEQ (LETT |tmp|
                                       (|FORMULA;formatFormula|
                                        (|SPADfirst| |args|)
                                        (QREFELT $ 9) $) . #1#)
                                 (COND
                                   ((NULL (EQUAL |tmp| ""))
                                    (LETT |form|
                                     (APPEND |form|
                                      (LIST " sub " |tmp|)) . #1#)))
                                 (LETT |args| (CDR |args|) . #1#)
                                 (EXIT (COND
                                         ((NULL |args|)
                                          (|FORMULA;group|
                                           (SPADCALL |form|
                                            (QREFELT $ 51))
                                           $))
                                         (#0#
                                          (SEQ
                                           (LETT |tmp|
                                            (|FORMULA;formatFormula|
                                             (|SPADfirst| |args|)
                                             (QREFELT $ 9) $) . #1#)
                                           (COND
                                             ((NULL (EQUAL |tmp| ""))
                                              (LETT |form|
                                               (APPEND |form|
                                                (LIST " sup " |tmp|)) . #1#)))
                                           (LETT |args|
                                            (CDR |args|) . #1#)
                                           (EXIT
                                            (COND
                                              ((NULL |args|)
                                               (|FORMULA;group|
                                                (SPADCALL |form|
                                                 (QREFELT $ 51))
                                                $))
                                              (#0#
                                               (SEQ
                                                (LETT |tmp|
                                                 (|FORMULA;formatFormula|
                                                  (|SPADfirst| |args|)
                                                  (QREFELT $ 9) $) . #1#)
                                                (COND
                                                  ((NULL
                                                    (EQUAL |tmp| ""))
                                                   (LETT |form|
                                                    (APPEND |form|
                                                     (LIST " presup "
                                                      |tmp|)) . #1#)))
                                                (LETT |args|
                                                 (CDR |args|) . #1#)
                                                (EXIT
                                                 (COND
                                                   ((NULL |args|)
                                                    (|FORMULA;group|
                                                     (SPADCALL |form|
                                                      (QREFELT $ 51))
                                                     $))
                                                   (#0#
                                                    (SEQ
                                                     (LETT |tmp|
                                                      (|FORMULA;formatFormula|
                                                       (|SPADfirst|
                                                        |args|)
                                                       (QREFELT $ 9) $) . #1#)
                                                     (COND
                                                       ((NULL
                                                         (EQUAL |tmp|
                                                          ""))
                                                        (LETT |form|
                                                         (APPEND |form|
                                                          (LIST
                                                           " presub "
                                                           |tmp|)) . #1#)))
                                                     (EXIT
                                                      (|FORMULA;group|
                                                       (SPADCALL |form|
                                                        (QREFELT $ 51))
                                                       $))))))))))))))))))))
             ((EQUAL |op| "MATRIX")
              (|FORMULA;formatMatrix| (CDR |args|) $))
             (#0#
              (SPADCALL (LIST "not done yet for " |op|) (QREFELT $ 51)))))))) 

(DEFUN |FORMULA;formatPlex| (|op| |args| |prec| $)
  (PROG (|p| |opPrec| |n| |hold| |s|)
    (RETURN
      (SEQ (LETT |p| (SPADCALL |op| (QREFELT $ 18) (QREFELT $ 52))
                 . #0=(|FORMULA;formatPlex|))
           (EXIT (COND
                   ((< |p| 1)
                    (|error| "unknown Script Formula Formatter unary op"))
                   (#1='T
                    (SEQ (LETT |opPrec|
                               (SPADCALL (QREFELT $ 19) |p|
                                   (QREFELT $ 54)) . #0#)
                         (LETT |n| (LENGTH |args|) . #0#)
                         (COND
                           ((NULL (EQL |n| 2))
                            (COND
                              ((NULL (EQL |n| 3))
                               (EXIT (|error|
                                      "wrong number of arguments for plex"))))))
                         (LETT |s|
                               (COND
                                 ((EQUAL |op| "SIGMA") "sum")
                                 ((EQUAL |op| "PI") "product")
                                 ((OR (EQUAL |op| "INTSIGN")
                                      (EQUAL |op| "INDEFINTEGRAL"))
                                  "integral")
                                 (#1# "????")) . #0#)
                         (LETT |hold|
                               (|FORMULA;formatFormula|
                                   (|SPADfirst| |args|) (QREFELT $ 9)
                                   $) . #0#)
                         (LETT |args| (CDR |args|) . #0#)
                         (COND
                           ((EQUAL |op| "INDEFINTEGRAL")
                            (SEQ (LETT |hold|
                                       (|FORMULA;group|
                                        (SPADCALL
                                         (LIST |hold| " "
                                          (|FORMULA;formatFormula|
                                           (|SPADfirst| |args|)
                                           (QREFELT $ 9) $))
                                         (QREFELT $ 51))
                                        $) . #0#)
                                 (EXIT (LETT |s|
                                        (SPADCALL (LIST |s| " " |hold|)
                                         (QREFELT $ 51)) . #0#))))
                           (#1#
                            (SEQ (COND
                                   ((NULL (EQUAL |hold| ""))
                                    (LETT |s|
                                     (SPADCALL
                                      (LIST |s| " from"
                                       (|FORMULA;group|
                                        (SPADCALL
                                         (LIST "\\displaystyle "
                                          |hold|)
                                         (QREFELT $ 51))
                                        $))
                                      (QREFELT $ 51)) . #0#)))
                                 (COND
                                   ((NULL (NULL (CDR |args|)))
                                    (SEQ
                                     (LETT |hold|
                                      (|FORMULA;formatFormula|
                                       (|SPADfirst| |args|)
                                       (QREFELT $ 9) $) . #0#)
                                     (COND
                                       ((NULL (EQUAL |hold| ""))
                                        (LETT |s|
                                         (SPADCALL
                                          (LIST |s| " to"
                                           (|FORMULA;group|
                                            (SPADCALL
                                             (LIST "\\displaystyle "
                                              |hold|)
                                             (QREFELT $ 51))
                                            $))
                                          (QREFELT $ 51)) . #0#)))
                                     (EXIT
                                      (LETT |args| (CDR |args|) . #0#)))))
                                 (EXIT (LETT |s|
                                        (SPADCALL
                                         (LIST |s| " "
                                          (|FORMULA;formatFormula|
                                           (|SPADfirst| |args|)
                                           (QREFELT $ 9) $))
                                         (QREFELT $ 51)) . #0#)))))
                         (COND
                           ((< |opPrec| |prec|)
                            (LETT |s|
                                  (|FORMULA;parenthesize| |s| $) . #0#)))
                         (EXIT (|FORMULA;group| |s| $)))))))))) 

(DEFUN |FORMULA;formatMatrix| (|args| $)
  (|FORMULA;group|
      (|FORMULA;addBrackets|
          (|FORMULA;formatNary| " habove " |args| (QREFELT $ 9) $) $)
      $)) 

(DEFUN |FORMULA;formatFunction| (|op| |args| |prec| $)
  (|FORMULA;group|
      (SPADCALL
          (LIST |op| " "
                (|FORMULA;parenthesize|
                    (|FORMULA;formatNary| "," |args| (QREFELT $ 9) $)
                    $))
          (QREFELT $ 51))
      $)) 

(DEFUN |FORMULA;formatNullary| (|op| $)
  (COND
    ((EQUAL |op| "NOTHING") "")
    ('T (|FORMULA;group| (SPADCALL (LIST |op| "()") (QREFELT $ 51)) $)))) 

(DEFUN |FORMULA;formatUnary| (|op| |arg| |prec| $)
  (PROG (|p| |opPrec| |s|)
    (RETURN
      (SEQ (LETT |p| (SPADCALL |op| (QREFELT $ 11) (QREFELT $ 52))
                 . #0=(|FORMULA;formatUnary|))
           (EXIT (COND
                   ((< |p| 1)
                    (|error| "unknown Script Formula Formatter unary op"))
                   (#1='T
                    (SEQ (LETT |opPrec|
                               (SPADCALL (QREFELT $ 12) |p|
                                   (QREFELT $ 54)) . #0#)
                         (LETT |s|
                               (SPADCALL
                                   (LIST |op|
                                    (|FORMULA;formatFormula| |arg|
                                     |opPrec| $))
                                   (QREFELT $ 51)) . #0#)
                         (EXIT (COND
                                 ((< |opPrec| |prec|)
                                  (|FORMULA;group|
                                      (|FORMULA;parenthesize| |s| $) $))
                                 ((EQUAL |op| "-") |s|)
                                 (#1# (|FORMULA;group| |s| $)))))))))))) 

(DEFUN |FORMULA;formatBinary| (|op| |args| |prec| $)
  (PROG (|p| |opPrec| |s|)
    (RETURN
      (SEQ (LETT |p| (SPADCALL |op| (QREFELT $ 13) (QREFELT $ 52))
                 . #0=(|FORMULA;formatBinary|))
           (EXIT (COND
                   ((< |p| 1)
                    (|error| "unknown Script Formula Formatter binary op"))
                   (#1='T
                    (SEQ (LETT |op|
                               (COND
                                 ((EQUAL |op| "**") " sup ")
                                 ((OR (EQUAL |op| "/")
                                      (EQUAL |op| "OVER"))
                                  " over ")
                                 (#1# |op|)) . #0#)
                         (LETT |opPrec|
                               (SPADCALL (QREFELT $ 14) |p|
                                   (QREFELT $ 54)) . #0#)
                         (LETT |s|
                               (|FORMULA;formatFormula|
                                   (|SPADfirst| |args|) |opPrec| $) . #0#)
                         (LETT |s|
                               (SPADCALL
                                   (LIST |s| |op|
                                    (|FORMULA;formatFormula|
                                     (|SPADfirst| (CDR |args|))
                                     |opPrec| $))
                                   (QREFELT $ 51)) . #0#)
                         (EXIT (|FORMULA;group|
                                   (COND
                                     ((OR (EQUAL |op| " over ")
                                       (NULL (< |opPrec| |prec|)))
                                      |s|)
                                     ('T
                                      (|FORMULA;parenthesize| |s| $)))
                                   $)))))))))) 

(DEFUN |FORMULA;formatNary| (|op| |args| |prec| $)
  (|FORMULA;group| (|FORMULA;formatNaryNoGroup| |op| |args| |prec| $)
      $)) 

(DEFUN |FORMULA;formatNaryNoGroup| (|op| |args| |prec| $)
  (PROG (|p| |opPrec| |a| #0=#:G1920 |l| |s|)
    (RETURN
      (SEQ (COND
             ((NULL |args|) "")
             (#1='T
              (SEQ (LETT |p|
                         (SPADCALL |op| (QREFELT $ 15) (QREFELT $ 52))
                         . #2=(|FORMULA;formatNaryNoGroup|))
                   (EXIT (COND
                           ((< |p| 1)
                            (|error| "unknown Script Formula Formatter nary op"))
                           (#1#
                            (SEQ (LETT |op|
                                       (COND
                                         ((EQUAL |op| ",") ", @@ ")
                                         ((EQUAL |op| ";") "; @@ ")
                                         ((OR (EQUAL |op| "*")
                                           (EQUAL |op| " "))
                                          (QREFELT $ 7))
                                         ((EQUAL |op| "ROW") " here ")
                                         (#1# |op|)) . #2#)
                                 (LETT |l| NIL . #2#)
                                 (LETT |opPrec|
                                       (SPADCALL (QREFELT $ 16) |p|
                                        (QREFELT $ 54)) . #2#)
                                 (SEQ (LETT |a| NIL . #2#)
                                      (LETT #0# |args| . #2#) G190
                                      (COND
                                        ((OR (ATOM #0#)
                                          (PROGN
                                            (LETT |a| (CAR #0#) . #2#)
                                            NIL))
                                         (GO G191)))
                                      (SEQ
                                       (EXIT
                                        (LETT |l|
                                         (CONS |op|
                                          (CONS
                                           (|FORMULA;formatFormula| |a|
                                            |opPrec| $)
                                           |l|)) . #2#)))
                                      (LETT #0# (CDR #0#) . #2#)
                                      (GO G190) G191 (EXIT NIL))
                                 (LETT |s|
                                       (SPADCALL (REVERSE (CDR |l|))
                                        (QREFELT $ 51)) . #2#)
                                 (EXIT (COND
                                         ((< |opPrec| |prec|)
                                          (|FORMULA;parenthesize| |s|
                                           $))
                                         (#1# |s|)))))))))))))) 

(DEFUN |FORMULA;formatFormula| (|expr| |prec| $)
  (PROG (|str| |i| |l| |args| |nargs| |op|)
    (RETURN
      (SEQ (COND
             ((ATOM |expr|)
              (SEQ (LETT |str| (|FORMULA;stringify| |expr| $)
                         . #0=(|FORMULA;formatFormula|))
                   (EXIT (COND
                           ((FIXP |expr|)
                            (SEQ (LETT |i| |expr| . #0#)
                                 (EXIT (COND
                                         ((OR (< |i| 0) (< 9 |i|))
                                          (|FORMULA;group| |str| $))
                                         ('T |str|)))))
                           ('T
                            (SEQ (LETT |i|
                                       (SPADCALL |str| (QREFELT $ 21)
                                        (QREFELT $ 52)) . #0#)
                                 (EXIT (COND
                                         ((< 0 |i|)
                                          (SPADCALL (QREFELT $ 22) |i|
                                           (QREFELT $ 55)))
                                         ('T |str|)))))))))
             ('T
              (SEQ (LETT |l| |expr| . #0#)
                   (EXIT (COND
                           ((NULL |l|) (QREFELT $ 7))
                           ('T
                            (SEQ (LETT |op|
                                       (|FORMULA;stringify|
                                        (|SPADfirst| |l|) $) . #0#)
                                 (LETT |args| (CDR |l|) . #0#)
                                 (LETT |nargs| (LENGTH |args|) . #0#)
                                 (EXIT (COND
                                         ((SPADCALL |op| (QREFELT $ 20)
                                           (QREFELT $ 56))
                                          (|FORMULA;formatSpecial| |op|
                                           |args| |prec| $))
                                         ((SPADCALL |op| (QREFELT $ 18)
                                           (QREFELT $ 56))
                                          (|FORMULA;formatPlex| |op|
                                           |args| |prec| $))
                                         ((EQL 0 |nargs|)
                                          (|FORMULA;formatNullary| |op|
                                           $))
                                         ('T
                                          (SEQ
                                           (COND
                                             ((EQL 1 |nargs|)
                                              (COND
                                                ((SPADCALL |op|
                                                  (QREFELT $ 11)
                                                  (QREFELT $ 56))
                                                 (EXIT
                                                  (|FORMULA;formatUnary|
                                                   |op|
                                                   (|SPADfirst| |args|)
                                                   |prec| $))))))
                                           (COND
                                             ((EQL 2 |nargs|)
                                              (COND
                                                ((SPADCALL |op|
                                                  (QREFELT $ 13)
                                                  (QREFELT $ 56))
                                                 (EXIT
                                                  (|FORMULA;formatBinary|
                                                   |op| |args| |prec|
                                                   $))))))
                                           (EXIT
                                            (COND
                                              ((SPADCALL |op|
                                                (QREFELT $ 17)
                                                (QREFELT $ 56))
                                               (|FORMULA;formatNaryNoGroup|
                                                |op| |args| |prec| $))
                                              ((SPADCALL |op|
                                                (QREFELT $ 15)
                                                (QREFELT $ 56))
                                               (|FORMULA;formatNary|
                                                |op| |args| |prec| $))
                                              ('T
                                               (SEQ
                                                (LETT |op|
                                                 (|FORMULA;formatFormula|
                                                  (|SPADfirst| |l|)
                                                  (QREFELT $ 9) $) . #0#)
                                                (EXIT
                                                 (|FORMULA;formatFunction|
                                                  |op| |args| |prec| $))))))))))))))))))))) 

(DEFUN |ScriptFormulaFormat| ()
  (PROG ()
    (RETURN
      (PROG (#0=#:G1936)
        (RETURN
          (COND
            ((LETT #0#
                   (HGET |$ConstructorCache| '|ScriptFormulaFormat|)
                   . #1=(|ScriptFormulaFormat|))
             (|CDRwithIncrement| (CDAR #0#)))
            ('T
             (UNWIND-PROTECT
               (PROG1 (CDDAR (HPUT |$ConstructorCache|
                                   '|ScriptFormulaFormat|
                                   (LIST
                                    (CONS NIL
                                     (CONS 1 (|ScriptFormulaFormat;|))))))
                 (LETT #0# T . #1#))
               (COND
                 ((NOT #0#)
                  (HREM |$ConstructorCache| '|ScriptFormulaFormat|))))))))))) 

(DEFUN |ScriptFormulaFormat;| ()
  (PROG (|dv$| $ |pv$|)
    (RETURN
      (PROGN
        (LETT |dv$| '(|ScriptFormulaFormat|)
              . #0=(|ScriptFormulaFormat|))
        (LETT $ (GETREFV 58) . #0#)
        (QSETREFV $ 0 |dv$|)
        (QSETREFV $ 3 (LETT |pv$| (|buildPredVector| 0 0 NIL) . #0#))
        (|haddProp| |$ConstructorCache| '|ScriptFormulaFormat| NIL
            (CONS 1 $))
        (|stuffDomainSlots| $)
        (QSETREFV $ 6
            (|Record| (|:| |prolog| (|List| (|String|)))
                (|:| |formula| (|List| (|String|)))
                (|:| |epilog| (|List| (|String|)))))
        (QSETREFV $ 7 " @@ ")
        (QSETREFV $ 8 1000000)
        (QSETREFV $ 9 0)
        (QSETREFV $ 10 " <>[](){}+*=,-%")
        (QSETREFV $ 11 (LIST "-"))
        (QSETREFV $ 12 '(700))
        (QSETREFV $ 13 (LIST "+->" "|" "**" "/" "<" ">" "=" "OVER"))
        (QSETREFV $ 14 (LIST 0 0 900 700 400 400 400 700))
        (QSETREFV $ 15
            (LIST "-" "+" "*" (QREFELT $ 7) "," ";" " " "ROW" ""
                  " habove " " here " " labove "))
        (QSETREFV $ 16 (LIST 700 700 800 800 110 110 0 0 0 0 0 0))
        (QSETREFV $ 17 NIL)
        (QSETREFV $ 18 (LIST "SIGMA" "PI" "INTSIGN" "INDEFINTEGRAL"))
        (QSETREFV $ 19 '(700 800 700 700))
        (QSETREFV $ 20
            (LIST "MATRIX" "BRACKET" "BRACE" "CONCATB" "AGGLST"
                  "CONCAT" "OVERBAR" "ROOT" "SUB" "SUPERSUB" "ZAG"
                  "AGGSET" "SC" "PAREN"))
        (QSETREFV $ 21 (LIST "5" "..."))
        (QSETREFV $ 22 (LIST " alpha " " ellipsis "))
        $)))) 

(MAKEPROP '|ScriptFormulaFormat| '|infovec|
    (LIST '#(NIL NIL NIL NIL NIL NIL '|Rep| '|blank| '|maxPrec|
             '|minPrec| '|splitChars| '|unaryOps| '|unaryPrecs|
             '|binaryOps| '|binaryPrecs| '|naryOps| '|naryPrecs|
             '|naryNGOps| '|plexOps| '|plexPrecs| '|specialOps|
             '|specialStrings| '|specialStringsInFormula|
             |FORMULA;new;$;1| (|OutputForm|) |FORMULA;coerce;Of$;2|
             (|List| 42) (0 . |concat|) (|Integer|)
             |FORMULA;convert;OfI$;3| (|Void|) (6 . |void|)
             |FORMULA;display;$IV;4| |FORMULA;display;$V;5|
             |FORMULA;prologue;$L;6| |FORMULA;formula;$L;7|
             |FORMULA;epilogue;$L;8| |FORMULA;setPrologue!;$2L;9|
             |FORMULA;setFormula!;$2L;10| |FORMULA;setEpilogue!;$2L;11|
             (10 . |coerce|) |FORMULA;coerce;$Of;12| (|String|)
             (|Character|) (15 . |char|) (20 . |elt|) (|Boolean|)
             (26 . =) (32 . |setelt|) (|List| $) (39 . |split|)
             (45 . |concat|) (50 . |position|) (|List| 28) (56 . |elt|)
             (62 . |elt|) (68 . |member?|) (|SingleInteger|))
          '#(~= 74 |setPrologue!| 80 |setFormula!| 86 |setEpilogue!| 92
             |prologue| 98 |new| 103 |latex| 107 |hash| 112 |formula|
             117 |epilogue| 122 |display| 127 |convert| 138 |coerce|
             144 = 154)
          'NIL
          (CONS (|makeByteWordVec2| 1 '(0 0 0))
                (CONS '#(|SetCategory&| |BasicType&| NIL)
                      (CONS '#((|SetCategory|) (|BasicType|)
                               (|CoercibleTo| 24))
                            (|makeByteWordVec2| 57
                                '(2 26 0 0 0 27 0 30 0 31 1 26 24 0 40
                                  1 43 0 42 44 2 42 43 0 28 45 2 43 46
                                  0 0 47 3 42 43 0 28 43 48 2 42 49 0
                                  43 50 1 42 0 49 51 2 26 28 42 0 52 2
                                  53 28 0 28 54 2 26 42 0 28 55 2 26 46
                                  42 0 56 2 0 46 0 0 1 2 0 26 0 26 37 2
                                  0 26 0 26 38 2 0 26 0 26 39 1 0 26 0
                                  34 0 0 0 23 1 0 42 0 1 1 0 57 0 1 1 0
                                  26 0 35 1 0 26 0 36 1 0 30 0 33 2 0
                                  30 0 28 32 2 0 0 24 28 29 1 0 0 24 25
                                  1 0 24 0 41 2 0 46 0 0 1)))))
          '|lookupComplete|)) 

(MAKEPROP '|ScriptFormulaFormat| 'NILADIC T) 
