isac-java/src/java/isac/gui/mawen/scalaterm/Util.scala
author Walther Neuper <walther.neuper@jku.at>
Fri, 26 Mar 2021 10:45:05 +0100
changeset 5239 b4e3883d7b66
parent 5229 6bf0e95981e3
permissions -rw-r--r--
reset mathematics-engine to Isabelle2015

note: for this version libisabelle was available,
which connects front-end (Java) and back-end (Isabelle/ML)
     1 package isac.gui.mawen.scalaterm
     2 
     3 import isac.gui.mawen.syntax.XSyntax
     4 import isac.gui.mawen.syntax.Ast
     5 import isac.gui.mawen.termtree.TreeNodeContent
     6 import javax.swing.tree.DefaultMutableTreeNode
     7 
     8 import edu.tum.cs.isabelle.api.XML
     9 import edu.tum.cs.isabelle.pure._
    10 import edu.tum.cs.isabelle._      // for Codec
    11 
    12 object Util {
    13 
    14   def int(s: String): Integer = new Integer(s)
    15     
    16   /* transform a Term into a String, which corresponds to Isabelle's 
    17    * pretty_printing within Isac's Knowledge.
    18    * mawen uses this for string representation of Terms.
    19    */
    20   def str_of(t: Term, prior: Int, l_r: String): String = t match {
    21     case App(App(Const("Product_Type.Pair", _), t1), t2) 
    22     => str_of(t1, prior, "none") + ", " + str_of(t2, prior, "none")
    23     case App(App(Const(str, _), t1), t2) 
    24     => { val pri = XSyntax.isa_prior(str)
    25       if (prior > pri) {
    26          XSyntax.isa_fix(str) match {
    27          case "infixl" => "(" + str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right") + ")"
    28          case "infixr" => "(" + str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right") + ")"
    29          case "prefix" => "(" + XSyntax.isa_math(str) + str_of(t1, pri, "left") + " (2" + str_of(t2, pri, "right") + "2)" + ")"}
    30       }else{
    31          XSyntax.isa_fix(str) match {
    32          case "infixl" => {
    33            if (prior == pri & l_r == "right") {
    34              "(" + str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right") + ")"
    35            }else{
    36              str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right")
    37            }
    38          }
    39          case "infixr" => {
    40            if (prior == pri & l_r == "left") {
    41              "(" + str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right") + ")"
    42            }else{
    43              str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right")
    44            }
    45          }
    46          case "prefix" => XSyntax.isa_math(str) + str_of(t1, pri, "none") + " " + str_of(t2, pri, "none")}
    47       }}
    48     case App(Const(str, _), t1) => XSyntax.isa_math(str) + "(" + str_of(t1, prior, "none") + ")"
    49     case Free(str, _) =>  str
    50     case Const(str, _) => str
    51     case _ => throw new IllegalArgumentException("str_of WRONG arg: " + t)
    52   }
    53   // infix: "left" or "right" argument ----------vvvv
    54   def string_of(t: Term): String = str_of(t, 0, "none")
    55 
    56   
    57   def insert_nodes(t: Ast.Ast, n: DefaultMutableTreeNode): DefaultMutableTreeNode = t match {
    58     case Ast.Appl(List(Ast.Appl(List(Ast.Constant("Product_Type.Pair"), t1)), t2))
    59     => n //TODO
    60     case Ast.Appl(List(Ast.Appl(List(Ast.Constant(str), t1)), t2))
    61     => { val tmp1 = n.clone().asInstanceOf[DefaultMutableTreeNode]
    62          n.setUserObject(new TreeNodeContent(XSyntax.ast_math(str),
    63              Ast.math_string_of(Ast.Appl(List(Ast.Appl(List(Ast.Constant(str), t1)), t2)))))
    64          n.add(insert_nodes(t1, tmp1))
    65          val tmp2 = n.clone().asInstanceOf[DefaultMutableTreeNode]
    66          n.add(insert_nodes(t2, tmp2))
    67          n 
    68       }
    69     case Ast.Appl(List(Ast.Constant(str), t1))
    70     => { val tmp = n.clone().asInstanceOf[DefaultMutableTreeNode]
    71          n.setUserObject(new TreeNodeContent(XSyntax.ast_math(str),
    72              Ast.math_string_of(Ast.Appl(List(Ast.Constant(str), t1)))))
    73          n.add(insert_nodes(t1, tmp))
    74          n 
    75       }
    76     case Ast.Variable(str) => { n.setUserObject(str); n }
    77     case Ast.Constant(str) => { n.setUserObject(str); n }
    78     case _ => throw new IllegalArgumentException("jtree_of WRONG arg: " + t)
    79   }
    80   def jtree_of(t: Ast.Ast): DefaultMutableTreeNode = insert_nodes(t, new DefaultMutableTreeNode)
    81 
    82  /*
    83   * Code not adapted to migration from Term to Ast.
    84   * 
    85   structure beeped in java class SoundUtils - necessary to beep other components?
    86   def sound_of_sub(t: Term, line: Integer, indent: Integer): Unit = t match {
    87     case App(App(Const("Product_Type.Pair", _), t1), t2) 
    88     => isac.gui.mawen.termtree.SoundUtils.tone(261, 500)
    89     case App(App(Const(str, typ), t1), t2)
    90     => { isac.gui.mawen.termtree.SoundUtils.repeatTone(261+(10*line), 500,indent) //tonhoehe: line, wiederholung: intent
    91          sound_of_sub(t1, line + 1, indent + 1)
    92          sound_of_sub(t2, line + 1, indent + 1)
    93       }
    94     case App(Const(str, typ), t1) 
    95     => { isac.gui.mawen.termtree.SoundUtils.repeatTone(261+(10*line), 500,indent) //tonhoehe: line, wiederholung: intent
    96          sound_of_sub(t1, line + 1, indent + 1)
    97       }
    98     case Free(str, _) => { 
    99        isac.gui.mawen.termtree.SoundUtils.repeatTone(1000+(10*line), 500,indent)
   100       }
   101     case Const(str, _) => { 
   102        isac.gui.mawen.termtree.SoundUtils.repeatTone(2000+(10*line), 500,indent)
   103       }
   104     case _ => throw new IllegalArgumentException("sound_of WRONG arg: " + t)
   105   }
   106   def sound_of(t: Term) = sound_of_sub(t, 0, 0)
   107 */
   108   
   109   def is_numeral(s: String): Boolean = {
   110     try {
   111       s.toInt; true
   112     } catch {
   113       case e: Exception => false
   114     }
   115   }
   116   def matml_of(t: Term, prior: Int, l_r: String): String = t match {
   117     case App(App(Const("Product_Type.Pair", _), t1), t2) 
   118     => matml_of(t1, prior, "none") + ", " + matml_of(t2, prior, "none")
   119     case App(App(Const("Fields.inverse_class.divide", _), t1), t2) 
   120     => { 
   121       val str = "Fields.inverse_class.divide"
   122       val pri = XSyntax.isa_prior(str)
   123       "<mfrac><mrow>" + matml_of(t1, pri, "left") + "</mrow><mrow>" + matml_of(t2, pri, "right") + "</mrow></mfrac>"
   124     }
   125     case App(App(Const(str, _), t1), t2) 
   126     => { val pri = XSyntax.isa_prior(str)
   127       if (prior > pri) {
   128          XSyntax.isa_fix(str) match {
   129          case "prefix" => "<mfenced separators=\'\'>" + "<mo>" + XSyntax.isa_math(str) + "</mo>" + matml_of(t1, pri, "left") + 
   130            "<mfenced separators=\'\'>" + matml_of(t2, pri, "right") + "</mfenced>" + "</mfenced>"
   131          case _ => "<mfenced separators=\'\'>" + matml_of(t1, pri, "left") + "<mo>" + 
   132            XSyntax.isa_math(str) + "</mo>" + matml_of(t2, pri, "right") + "</mfenced>"
   133          }
   134       }else{
   135          XSyntax.isa_fix(str) match {
   136          case "prefix" => "<mo>" + XSyntax.isa_math(str) + "</mo>" + matml_of(t1, pri, "none") + " " + matml_of(t2, pri, "none")
   137          case _ => {
   138            if (prior == pri & l_r == "right") {
   139              matml_of(t1, pri, "left") + "<mo>" + XSyntax.isa_math(str) + "</mo>" + matml_of(t2, pri, "right")
   140            }else{
   141              matml_of(t1, pri, "left") + "<mo>" + XSyntax.isa_math(str) + "</mo>" + matml_of(t2, pri, "right")
   142            }
   143          }}
   144       }
   145     }
   146     case App(Const(str, _), t1) => "<mo>" + XSyntax.isa_math(str) + "</mo>" + "<mfenced separators=\'\'>" + matml_of(t1, prior, "none") + "</mfenced>"
   147     case Free(str, _) => if (is_numeral(str)) "<mn>" + str + "</mn>" else "<mi>" + str + "</mi>"
   148     case Const(str, _) => str
   149     case _ => throw new IllegalArgumentException("matml_of WRONG arg: " + t)
   150   }
   151   def mathml_of(t: Term): String = "<math xmlns="+"\"&mathml;\""+"><mrow>" + matml_of(t, 0, "none") + "</mrow></math>"
   152   
   153 }