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