isac-java/src/java/isac/gui/mawen/syntax/syntax.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 /*  THIS IS A DUMMY IMPLEMENTATION OF Isabelle2016-1 ...
     2     TTitle:      Pure/Syntax/syntax.ML
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     5 Standard Isabelle syntax, based on arbitrary context-free grammars
     6 (specified by mixfix declarations).
     7 */
     8 package isac.gui.mawen.syntax
     9 
    10 import isac.gui.mawen.syntax.isabelle.XLibrary
    11 import isac.gui.mawen.syntax.isabelle.XTerm
    12 import isac.gui.mawen.syntax.Ast._  //"._" simplifies "Ast.Ast" to "Ast"
    13 
    14 import edu.tum.cs.isabelle.pure._
    15 
    16 import scala.collection.immutable.TreeMap
    17 
    18 object XSyntax {
    19 
    20  /* Data for presentation of Const which are not contained in (Scala-) Term:
    21   * _0: string used in ASTs
    22   * _1: string as shown by Isabelle's pretty printing
    23   * _2: infixl | infixr | prefix | postfix
    24   * _3: priority, which determines parentheses to be set
    25   * 
    26   * This replaces structure Syntax in Pure/Syntax/syntax.ML etc:
    27   *   val lookup_const: syntax -> string -> string option
    28   *   fun lookup_const (Syntax ({consts, ...}, _)) = Symtab.lookup consts;
    29   */
    30   val isa_const = Map (
    31     "HOL.eq" -> ("=", " = ", "infixl", 50),                              //see ~~/src/HOL/HOL.thy
    32     "Groups.plus_class.plus" -> ("+", " + ", "infixl", 65),              //see ~~/src/HOL/Groups.thy
    33     "Groups.minus_class.minus" -> ("-", " - ", "infixl", 65),            //see ~~/src/HOL/Groups.thy
    34     "Groups.plus_class.uminus" -> ("-", " -", "prefix", 80),             //see ~~/src/HOL/Groups.thy
    35     "Groups.times_class.times" -> ("*", " * ", "infixl", 70),            //see ~~/src/HOL/Groups.thy
    36     "Fields.inverse_class.divide" -> ("/", " / ", "infixl", 70),         //see ~~/src/HOL/Fields.thy
    37     "Atools.pow" -> ("^", " ^ ", "infixr", 80),                          //see ~~/src/Tools/isac/Knowledge/Atools.thy
    38     "Power.power_class.power" -> ("^", " ^ ", "infixr", 80),             //see ~~/src/HOL/Power.thy
    39     "Transcendental.sin" -> ("sin", "sin ", "prefix", 70),               //see ~~/src/HOL/Transcendental.thy
    40     "Transcendental.cos" -> ("cos", "cos ", "prefix", 70),               //see ~~/src/HOL/Transcendental.thy
    41     "Integrate.Integral" -> ("Integral", "Integral ", "prefix", 91)      //see ~~/src/Tools/isac/Knowledge/Integrate.thy
    42     //...ERROR: implicit conversion found: ...
    43     )
    44   /* an exact copy of isa_const with another key:
    45    * _0: string used in Isabelle Term
    46    */
    47   val ast_const = Map (
    48     "=" -> ("HOL.eq",                      " = ",      "infixl", 50, "CMCSC8", '='.toInt),    //see ~~/src/HOL/HOL.thy
    49     "+" -> ("Groups.plus_class.plus",      " + ",      "infixl", 65, "CMCSC8", '+'.toInt),    //see ~~/src/HOL/Groups.thy
    50     "-" -> ("Groups.minus_class.minus",    " - ",      "infixl", 65, "CMCSC8", '-'.toInt),    //see ~~/src/HOL/Groups.thy
    51     "-" -> ("Groups.plus_class.uminus",    " -",       "prefix", 80, "CMCSC8", '-'.toInt),    //see ~~/src/HOL/Groups.thy
    52     "*" -> ("Groups.times_class.times",    " * ",      "infixl", 70, "CMCSC8", '*'.toInt),    //see ~~/src/HOL/Groups.thy
    53     "/" -> ("Fields.inverse_class.divide", " / ",      "infixl", 70, "",        0),           //see ~~/src/HOL/Fields.thy
    54     "^" -> ("Atools.pow",                  " ^ ",      "infixr", 80, "",        0),           //see ~~/src/Tools/isac/Knowledge/Atools.thy
    55     "^" -> ("Power.power_class.power",     "  ",       "infixr", 80, "",        0),           //see ~~/src/HOL/Power.thy
    56     "sin" -> ("Transcendental.sin",        "sin ",     "prefix", 70, "",        0),           //see ~~/src/HOL/Transcendental.thy
    57     "cos" -> ("Transcendental.cos",        "cos ",     "prefix", 70, "",        0),           //see ~~/src/HOL/Transcendental.thy
    58     "Integral" -> ("Integrate.Integral",  "Integral ", "prefix", 91, "",        0)            //see ~~/src/Tools/isac/Knowledge/Integrate.thy
    59     //...ERROR: implicit conversion found: ...
    60     )
    61   /* so not every constant needs to be in Util.isa_const */
    62   def isa_ast(str: String): String = { //key is the String in Term.Const
    63     isa_const.get(str) match {
    64       case Some((ast, _, _, _)) => ast 
    65       case None => str.substring(str.lastIndexOf(".") + 1)}}
    66   def ast_isa(str: String): String = { //key is the String in Ast.Constant
    67     ast_const.get(str) match {
    68       case Some((ast, _, _, _, _, _)) => ast 
    69       case None => str.substring(str.lastIndexOf(".") + 1)}}
    70 
    71   def isa_math(str: String): String = { //key is the String in Term.Const
    72     isa_const.get(str) match {
    73       case Some((_, isa, _, _)) => isa 
    74       case None => str.substring(str.lastIndexOf(".") + 1) + " "}}
    75   def ast_math(str: String): String = { //key is the String in Ast.Constant
    76     ast_const.get(str) match {
    77       case Some((_, isa, _, _, _, _)) => isa 
    78       case None => str.substring(str.lastIndexOf(".") + 1) + " "}}
    79 
    80   def isa_fix(str: String): String = { //key is the String in Term.Const
    81     isa_const.get(str) match {
    82       case Some((_, _, fix, _)) => fix 
    83       case None => "prefix"}}
    84   def ast_fix(str: String): String = { //key is the String in Ast.Constant
    85     ast_const.get(str) match {
    86       case Some((_, _, fix, _, _, _)) => fix 
    87       case None => "prefix"}}
    88 
    89   def isa_prior(str: String): Int = { //key is the String in Term.Const
    90     isa_const.get(str) match {
    91       case Some((_, _, _, prior)) => prior 
    92       case None => 99}}
    93   def ast_prior(str: String): Int = { //key is the String in Ast.Constant
    94     ast_const.get(str) match {
    95       case Some((_, _, _, prior, _, _)) => prior 
    96       case None => 99}}
    97 
    98   /* ~~/src/Pure/Syntax/syntax.ML:
    99    *   val print_rules: syntax -> string -> (Ast.ast * Ast.ast) list
   100    *   fun print_rules (Syntax ({print_ruletab, ...}, _)) = Symtab.lookup_list print_ruletab;
   101    */
   102   var symtab = TreeMap(
   103     "List.list.Cons" -> List(
   104 //ML try_rules lhs: Appl(List(Constant("\<^const>List.list.Cons"), Variable("x"), Constant("\<^const>List.list.Nil"))) 
   105 //ML try_rules rhs: Appl(List(Constant("_list"), Variable("x"))) 
   106       //rule for the last element
   107       (Appl(List(                     // lhs
   108          Constant("List.list.Cons"),
   109          Variable("x"),
   110          Constant("List.list.Nil"))),
   111        Appl(List(                     // rhs
   112          Constant("_list"),
   113          Variable("x")))),
   114 //ML try_rules lhs: Appl(List(Constant("\<^const>List.list.Cons"), Variable("x"), Appl(List(Constant("_list"), Variable("xs"))))) 
   115 //ML try_rules rhs: Appl(List(Constant("_list"), Appl(List(Constant("_args"), Variable("x"), Variable("xs")))))
   116       //rule for earlier elements: rewriting begins at the last element
   117       (Appl(List(                     // lhs
   118          Constant("List.list.Cons"),
   119          Variable("x"),
   120          Appl(List(
   121            Constant("_list"),
   122            Variable("xs"))))),
   123        Appl(List(                     // rhs
   124          Constant("_list"),
   125          Appl(List(Constant("_args"),
   126            Variable("x"),
   127            Variable("xs"))))))),
   128     "Product_Type.Pair" -> List(
   129 //ML try_rules lhs: Appl(List(Constant("\<^const>Product_Type.Pair"), Variable("x"), Variable("y"))) 
   130 //ML try_rules rhs: Appl(List(Constant("_tuple"), Variable("x"), Appl(List(Constant("_tuple_arg"), Variable("y"))))) 
   131       //rule for the last element
   132       (Appl(List(Constant("Product_Type.Pair"), Variable("x"), Variable("y"))),
   133        Appl(List(Constant("_tuple"), Variable("x"), Appl(List(Constant("_tuple_arg"), Variable("y"))))) )
   134     ),
   135     "" -> List( //called by Ast.try_headless_rules
   136 //ML try_rules lhs: Appl(List(Constant("_tuple"), Variable("x"), Appl(List(Constant("_tuple_arg"), Appl(List(Constant("_tuple"), Variable("y"), Variable("z"))))))) 
   137 //ML try_rules rhs: Appl(List(Constant("_tuple"), Variable("x"), Appl(List(Constant("_tuple_args"), Variable("y"), Variable("z"))))) 
   138       (Appl(List(Constant("_tuple"), Variable("x"), Appl(List(Constant("_tuple_arg"), Appl(List(Constant("_tuple"), Variable("y"), Variable("z"))))))),
   139        Appl(List(Constant("_tuple"), Variable("x"), Appl(List(Constant("_tuple_args"), Variable("y"), Variable("z"))))) ) )
   140   )
   141 
   142   def print_rules (key: String): List[Tuple2[Ast, Ast]] =
   143     if (symtab.contains(key)) symtab(key) else List()
   144 
   145 }